summaryrefslogtreecommitdiffhomepage
path: root/src/features/plugins/store.test.ts
blob: be301ee65553b2f718260ebacec604e97e6d83e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import type { PluginListResponse, WorkspacePluginsResponse } from "@dispatch/transport-contract";
import { describe, expect, it, vi } from "vitest";
import type {
  PluginOpResult,
  PluginScanResult,
  PluginsHttp,
  WorkspaceStateResult,
} from "./adapter/http";
import { createPluginsStore } from "./store.svelte";

const listResponse: PluginListResponse = {
  loaded: [
    {
      id: "kernel",
      name: "Kernel",
      version: "1.0.0",
      tier: "core",
      trust: "bundled",
      locked: true,
      dependsOn: [],
    },
  ],
  disabledWithReason: [],
  discovered: [
    {
      id: "fresh",
      name: "Fresh",
      version: "1.0.0",
      tier: "standard",
      trust: "external",
      dir: "/plugins/fresh",
    },
  ],
  missing: [],
};

const wsResponse: WorkspacePluginsResponse = {
  workspaceId: "ws",
  plugins: [
    { pluginId: "kernel", tier: "core", state: "enabled", loaded: true, locked: true },
    { pluginId: "mcp", tier: "standard", state: "undefined", loaded: true, locked: false },
  ],
  enabledProviderCount: 1,
};

/** A fake PluginsHttp with a controllable list + workspace response. */
function fakeHttp(opts: {
  list?: PluginListResponse | null;
  workspacePlugins?: WorkspacePluginsResponse | null;
}) {
  // `=== undefined` (not `??`) so an explicit `null` override defeats the default.
  const list = () => (opts.list === undefined ? listResponse : opts.list);
  const ws = () => (opts.workspacePlugins === undefined ? wsResponse : opts.workspacePlugins);
  const opOk = (action: "load" | "unload" | "reload") => ({ ok: true, action, id: "x" }) as const;
  return {
    list: vi.fn(async (): Promise<PluginListResponse | null> => list()),
    scan: vi.fn(
      async (): Promise<PluginScanResult> => ({
        ok: true,
        response: { root: "/plugins", newPlugins: [], invalid: [], incompatible: [] },
      }),
    ),
    load: vi.fn(async (): Promise<PluginOpResult> => ({ ok: true, response: opOk("load") })),
    unload: vi.fn(async (): Promise<PluginOpResult> => ({ ok: true, response: opOk("unload") })),
    reload: vi.fn(async (): Promise<PluginOpResult> => ({ ok: true, response: opOk("reload") })),
    workspacePlugins: vi.fn(async (): Promise<WorkspacePluginsResponse | null> => ws()),
    setWorkspaceState: vi.fn(
      async (): Promise<WorkspaceStateResult> => ({
        ok: true,
        response: { workspaceId: "ws", pluginId: "mcp", state: "enabled" },
      }),
    ),
    providersStatus: vi.fn(async () => ({ workspaceId: "ws", enabledProviderCount: 1 })),
  } satisfies PluginsHttp;
}

describe("createPluginsStore — manager list", () => {
  it("fetches + normalizes the list into nodes", async () => {
    const store = createPluginsStore(fakeHttp({}));
    expect(store.list).toBeNull();
    expect(store.nodes).toEqual([]);
    await store.refresh();
    expect(store.list).toEqual(listResponse);
    expect(store.nodes.map((n) => n.id)).toEqual(["kernel", "fresh"]);
  });

  it("mutation results are surfaced and the list is refetched", async () => {
    const http = fakeHttp({});
    const store = createPluginsStore(http);
    await store.refresh();
    const result = await store.unload("kernel", false);
    expect(result.ok).toBe(true);
    expect(http.unload).toHaveBeenCalledWith("kernel", false);
    expect(http.list).toHaveBeenCalledTimes(2);
  });

  it("scan records the result + refreshes", async () => {
    const http = fakeHttp({});
    const store = createPluginsStore(http);
    await store.scan();
    expect(store.lastScan?.ok).toBe(true);
    expect(http.scan).toHaveBeenCalledOnce();
    expect(http.list).toHaveBeenCalledTimes(1); // refresh after scan (initial list fetch absent)
  });

  it("a mutation error lands in store.error", async () => {
    const http = fakeHttp({});
    http.unload = vi.fn(async (): Promise<PluginOpResult> => ({ ok: false, error: "blocked" }));
    const store = createPluginsStore(http);
    await store.unload("kernel", false);
    expect(store.error).toBe("blocked");
  });
});

describe("createPluginsStore — workspace view", () => {
  it("openWorkspace loads the rows + enabled provider count", async () => {
    const store = createPluginsStore(fakeHttp({}));
    await store.openWorkspace("ws");
    expect(store.workspaceView).toEqual({
      workspaceId: "ws",
      rows: wsResponse.plugins,
      enabledProviderCount: 1,
    });
  });

  it("setWorkspaceState updates the row in place from the response", async () => {
    const store = createPluginsStore(fakeHttp({}));
    await store.openWorkspace("ws");
    await store.setWorkspaceState("ws", "mcp", "enabled");
    const row = store.workspaceView?.rows.find((r) => r.pluginId === "mcp");
    expect(row?.state).toBe("enabled");
  });

  it("a refused setWorkspaceState refetches (server-authoritative reconcile)", async () => {
    const http = fakeHttp({});
    http.setWorkspaceState = vi.fn(
      async (): Promise<WorkspaceStateResult> => ({
        ok: false,
        error: "core is locked",
        reason: "core-locked",
      }),
    );
    const store = createPluginsStore(http);
    await store.openWorkspace("ws");
    await store.setWorkspaceState("ws", "kernel", "disabled");
    expect(store.error).toBe("core is locked");
    expect(http.workspacePlugins).toHaveBeenCalledTimes(2);
  });

  it("a failed openWorkspace clears the view (no stale rows from another workspace)", async () => {
    const http = fakeHttp({ workspacePlugins: null });
    const store = createPluginsStore(http);
    await store.openWorkspace("other");
    expect(store.workspaceView).toBeNull();
    expect(store.error).toContain("other");
  });
});

describe("createPluginsStore — multi-client coherence (§11)", () => {
  it("onExtensionsChanged refetches the list + the open workspace view", async () => {
    const http = fakeHttp({});
    const store = createPluginsStore(http);
    await store.openWorkspace("ws");
    expect(http.list).toHaveBeenCalledTimes(0);
    expect(http.workspacePlugins).toHaveBeenCalledTimes(1);

    store.onExtensionsChanged();
    await vi.waitFor(() => {
      expect(http.list).toHaveBeenCalledTimes(1);
      expect(http.workspacePlugins).toHaveBeenCalledTimes(2);
    });
  });

  it("onExtensionsChanged does not refetch a workspace view when none is open", async () => {
    const http = fakeHttp({});
    const store = createPluginsStore(http);
    store.onExtensionsChanged();
    await vi.waitFor(() => {
      expect(http.list).toHaveBeenCalledTimes(1);
      expect(http.workspacePlugins).toHaveBeenCalledTimes(0);
    });
  });
});