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 } from "@dispatch/transport-contract";
import { describe, expect, it } from "vitest";
import {
decideFrontendLoads,
frontendCandidates,
type ImportedFrontendModule,
usablePluginIds,
} from "./load-diff.js";
import { FRONTEND_ABI_VERSION } from "./version.js";
const httpBase = "http://localhost:24203";
function listWith(
...rows: readonly {
readonly id: string;
readonly apiVersion?: string;
readonly bust?: string;
}[]
): PluginListResponse {
return {
loaded: rows.map((r) => ({
id: r.id,
name: r.id,
version: "1.0.0",
tier: "standard" as const,
trust: "local" as const,
locked: false,
dependsOn: [],
...(r.apiVersion === undefined
? {}
: { frontend: { apiVersion: r.apiVersion, entry: "frontend.js", bust: r.bust ?? "1" } }),
})),
disabledWithReason: [],
discovered: [],
missing: [],
};
}
describe("frontendCandidates", () => {
it("derives candidates from loaded ∧ frontend-declaring ∧ workspace-enabled rows", () => {
const candidates = frontendCandidates(
listWith(
{ id: "a", apiVersion: "^1", bust: "111" },
{ id: "no-fe" },
{ id: "b", apiVersion: "^1", bust: "222" },
),
{ workspaceId: "team-a", usableIds: new Set(["a", "b", "no-fe"]), httpBase },
);
expect(candidates).toEqual([
{
id: "a",
apiVersion: "^1",
url: `${httpBase}/plugins/a/frontend?workspace=team-a&v=111`,
},
{
id: "b",
apiVersion: "^1",
url: `${httpBase}/plugins/b/frontend?workspace=team-a&v=222`,
},
]);
});
it("excludes plugins disabled in the active workspace", () => {
const candidates = frontendCandidates(listWith({ id: "a", apiVersion: "^1" }), {
workspaceId: "team-a",
usableIds: new Set<string>(),
httpBase,
});
expect(candidates).toEqual([]);
});
it("URL-encodes the workspace and bust", () => {
const candidates = frontendCandidates(listWith({ id: "a", apiVersion: "^1", bust: "1 2" }), {
workspaceId: "team a",
usableIds: new Set(["a"]),
httpBase,
});
expect(candidates[0]?.url).toBe(`${httpBase}/plugins/a/frontend?workspace=team%20a&v=1%202`);
});
});
describe("usablePluginIds", () => {
it("mirrors isPluginUsable: core usable unless disabled, others enabled-only", () => {
const usable = usablePluginIds([
{ pluginId: "core-one", tier: "core", state: "undefined", loaded: true, locked: true },
{ pluginId: "core-off", tier: "core", state: "disabled", loaded: true, locked: true },
{ pluginId: "std-on", tier: "standard", state: "enabled", loaded: true, locked: false },
{ pluginId: "std-blank", tier: "standard", state: "undefined", loaded: true, locked: false },
{ pluginId: "prov-on", tier: "providers", state: "enabled", loaded: true, locked: false },
{ pluginId: "prov-off", tier: "providers", state: "disabled", loaded: true, locked: false },
]);
expect([...usable].sort()).toEqual(["core-one", "prov-on", "std-on"]);
});
});
describe("decideFrontendLoads — the load diff (boot / extensions-changed / workspace.set)", () => {
const c = (id: string, bust = "1"): { id: string; url: string; apiVersion: string } => ({
id,
url: `${httpBase}/plugins/${id}/frontend?workspace=w&v=${bust}`,
apiVersion: "^1",
});
it("boot: every desired candidate imports (nothing imported yet)", () => {
const d = decideFrontendLoads([c("a"), c("b")], new Map());
expect(d.toLoad.map((x) => x.id)).toEqual(["a", "b"]);
expect(d.toUnload).toEqual([]);
expect(d.incompatible).toEqual([]);
});
it("extensions-changed: a removed plugin unloads, a newcomer imports", () => {
const imported = new Map<string, ImportedFrontendModule>([
["a", { url: c("a").url }],
["gone", { url: `${httpBase}/plugins/gone/frontend?workspace=w&v=1` }],
]);
const d = decideFrontendLoads([c("a"), c("new")], imported);
expect(d.toLoad.map((x) => x.id)).toEqual(["new"]);
expect(d.toUnload).toEqual(["gone"]);
});
it("workspace.set: the enablement set change drives load AND unload", () => {
// The candidate list already reflects the NEW workspace (composition
// root re-derives per sync) — previously-imported ids absent from it unload.
const imported = new Map<string, ImportedFrontendModule>([
["a", { url: c("a").url }],
["b", { url: c("b").url }],
]);
const d = decideFrontendLoads([c("a")], imported);
expect(d.toLoad).toEqual([]);
expect(d.toUnload).toEqual(["b"]);
});
it("a stable desired set is a no-op (idempotent sync)", () => {
const imported = new Map<string, ImportedFrontendModule>([
["a", { url: c("a").url }],
["b", { url: c("b").url }],
]);
const d = decideFrontendLoads([c("a"), c("b")], imported);
expect(d.toLoad).toEqual([]);
expect(d.toUnload).toEqual([]);
});
it("a changed URL (backend reload → new bust) is unload + re-import", () => {
const imported = new Map<string, ImportedFrontendModule>([["a", { url: c("a", "old").url }]]);
const d = decideFrontendLoads([c("a", "new")], imported);
expect(d.toLoad.map((x) => x.id)).toEqual(["a"]);
expect(d.toUnload).toEqual(["a"]);
});
it("handshake failure → incompatible, never imported, still unloaded if it was", () => {
const bad = { ...c("bad"), apiVersion: "^2" };
const imported = new Map<string, ImportedFrontendModule>([["bad", { url: bad.url }]]);
const d = decideFrontendLoads([bad], imported);
expect(d.toLoad).toEqual([]);
expect(d.incompatible).toEqual([{ id: "bad", apiVersion: "^2" }]);
// The previously-imported instance must be swept (the manifest changed
// under us — the UI must not keep running incompatible code).
expect(d.toUnload).toEqual(["bad"]);
});
it("the handshake checks against FRONTEND_ABI_VERSION", () => {
// A range that includes the host version passes — proving the version
// constant feeds the check.
expect(FRONTEND_ABI_VERSION).toBe("1.1");
const d = decideFrontendLoads([c("ok")], new Map());
expect(d.incompatible).toEqual([]);
});
it("the additive bump keeps v1 plugins loading (plan 04: ^1 spans 1.1)", () => {
// c() declares apiVersion "^1" — a bare-major range is a
// major-compatibility claim, so the 1.1 host (layout member added) still
// loads every v1 plugin.
const d = decideFrontendLoads([c("v1-plugin")], new Map());
expect(d.toLoad.map((x) => x.id)).toEqual(["v1-plugin"]);
expect(d.incompatible).toEqual([]);
});
it("plugins requiring the 1.1 ABI (^1.1) load on the 1.1 host", () => {
const layout = { ...c("layout-plugin"), apiVersion: "^1.1" };
const d = decideFrontendLoads([layout], new Map());
expect(d.toLoad.map((x) => x.id)).toEqual(["layout-plugin"]);
expect(d.incompatible).toEqual([]);
});
});
|