Repository navigation
Expand file tree
/
Copy pathplugin-sdk.test.ts
More file actions
216 lines (199 loc) · 7.35 KB
/
Copy pathplugin-sdk.test.ts
File metadata and controls
216 lines (199 loc) · 7.35 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import { readFileSync } from "node:fs";
import { describe, expect, it } from "vitest";
import { satisfiesPluginVersion } from "../packages/plugin-contracts/src/index.js";
import {
Context,
ContributionStore,
defineDocaConfig,
defineContributionPoint,
defineEvent,
defineService,
plugin,
PLUGIN_SDK_VERSION,
} from "../packages/plugin-sdk/src/index.js";
import { PLUGIN_SDK_VERSION as publishedSdkVersion } from "../packages/plugin-sdk/src/version.js";
import { runPluginContractHarness } from "../packages/plugin-sdk/src/testing.js";
describe("plugin SDK context", () => {
it("reads the host SDK version from the SDK package", () => {
const packageVersion = JSON.parse(
readFileSync(new URL("../packages/plugin-sdk/package.json", import.meta.url), "utf8"),
).version;
expect(PLUGIN_SDK_VERSION).toBe(packageVersion);
expect(publishedSdkVersion).toBe(packageVersion);
expect(satisfiesPluginVersion(PLUGIN_SDK_VERSION, `^${PLUGIN_SDK_VERSION}`)).toBe(true);
});
it("defines one build-time plugin list and rejects duplicate packages", () => {
expect(
defineDocaConfig({
plugins: [
plugin("@doca/plugin-files"),
plugin("@doca/plugin-mail", { targets: ["server", "web"] }),
],
}).plugins.map((entry) => entry.package),
).toEqual(["@doca/plugin-files", "@doca/plugin-mail"]);
expect(() =>
defineDocaConfig({
plugins: [plugin("@doca/plugin-files"), plugin("@doca/plugin-files")],
}),
).toThrow(/Duplicate plugin package/);
});
it("runs external plugins through the contract lifecycle and cleanup", async () => {
const calls: string[] = [];
const result = await runPluginContractHarness({
manifest: {
schemaVersion: 1,
id: "example.fixture",
version: "1.0.0",
displayName: "Fixture",
},
discover(context) {
context.effect(() => () => {
calls.push("effect");
});
},
mount() {
calls.push("mount");
},
ready() {
calls.push("ready");
},
dispose() {
calls.push("dispose");
},
});
expect(result.phases).toEqual([
"discover",
"initialize",
"mount",
"ready",
"dispose",
]);
expect(result.disposed).toBe(true);
expect(calls).toEqual(["mount", "ready", "dispose", "effect"]);
});
it("shares one provider registry, rejects ambiguity and supports optional injection", async () => {
const root = new Context();
const provider = root.child("provider");
const consumer = root.child("consumer");
const clock = defineService<{ now(): number }>("doca.clock");
const service = { now: () => 42 };
const release = provider.provide(clock, service);
expect(consumer.inject(clock)).toBe(service);
expect(consumer.injectOptional(clock)?.now()).toBe(42);
expect(() => consumer.provide(clock, { now: () => 0 })).toThrow(
/already provided/,
);
await release();
expect(consumer.injectOptional(clock)).toBeUndefined();
expect(() => consumer.inject(clock)).toThrow(/required service/);
await root.dispose();
});
it("owns child scopes and reverses interleaved effects during cleanup", async () => {
const root = new Context();
const scope = root.child("plugin.example");
const calls: string[] = [];
const resource = defineService<{ name: string }>("doca.resource");
scope.provide(resource, { name: "open" });
scope.effect(() => () => {
calls.push("first");
});
const child = scope.child("request");
child.effect(() => async () => {
await Promise.resolve();
calls.push("child");
});
scope.effect(() => () => {
calls.push(`last:${scope.inject(resource).name}`);
});
await scope.dispose();
expect(calls).toEqual(["last:open", "child", "first"]);
expect(scope.disposed).toBe(true);
expect(root.disposed).toBe(false);
await scope.dispose();
expect(calls).toHaveLength(3);
await root.dispose();
});
it("rejects contribution collisions and unregisters contributions with their scope", async () => {
const root = new Context();
const store = new ContributionStore();
const menus = defineContributionPoint<{ title: string }>("doca.menus");
const alphaScope = root.child("alpha");
const betaScope = root.child("beta");
const alpha = store.forContext(alphaScope, "alpha");
const beta = store.forContext(betaScope, "beta");
alpha.register(menus, "document.open", { title: "Open" });
expect(alpha.get(menus, "document.open")).toMatchObject({
pluginId: "alpha",
value: { title: "Open" },
});
expect(() =>
beta.register(menus, "document.open", { title: "Other" }),
).toThrow(/already registered by alpha/);
await alphaScope.dispose();
beta.register(menus, "document.open", { title: "Replacement" });
expect(beta.list(menus)).toHaveLength(1);
await root.dispose();
expect(beta.list(menus)).toEqual([]);
});
it("delegates all five dispatch modes to Cordis, including waterfall next", async () => {
const context = new Context();
const emit = defineEvent<number, void>("doca.changed");
const parallel = defineEvent<number, void, "parallel">(
"doca.parallel",
"parallel",
);
const serial = defineEvent<number, string, "serial">(
"doca.serial",
"serial",
);
const bail = defineEvent<number, string, "bail">("doca.bail", "bail");
const waterfall = defineEvent<string, string, "waterfall">(
"doca.waterfall",
"waterfall",
);
const calls: string[] = [];
context.on(emit, (value) => {
calls.push(`emit:${value}`);
});
context.on(parallel, async (value) => {
await Promise.resolve();
calls.push(`parallel:${value}`);
});
context.on(serial, () => false);
context.on(serial, async (value) => `serial:${value}`);
context.on(serial, () => "unreachable");
context.on(bail, () => undefined);
context.on(bail, (value) => `bail:${value}`);
context.on(bail, () => "unreachable");
context.on(waterfall, (value, next) => `outer:${value}[${next()}]`);
context.on(waterfall, (value, next) => `inner:${value}[${next()}]`);
expect(context.dispatch(emit, 3)).toBeUndefined();
await expect(context.dispatch(parallel, 4)).resolves.toBeUndefined();
await expect(context.dispatch(serial, 5)).resolves.toBe("serial:5");
expect(context.dispatch(bail, 6)).toBe("bail:6");
expect(context.dispatch(waterfall, "x", () => "base")).toBe(
"outer:x[inner:x[base]]",
);
expect(calls).toEqual(["emit:3", "parallel:4"]);
await context.dispose();
});
it("uses Cordis Fibers for plugin scopes and cleans their effects in reverse", async () => {
const root = new Context();
const scope = await root.createFiberScope("plugin.fiber-test");
const service = defineService<string>("doca.fiber-service");
const cleanup: string[] = [];
scope.provide(service, "live");
scope.effect(() => () => {
cleanup.push(`first:${scope.inject(service)}`);
});
scope.effect(() => async () => {
await Promise.resolve();
cleanup.push(`last:${scope.inject(service)}`);
});
expect(scope.inject(service)).toBe("live");
await scope.dispose();
expect(cleanup).toEqual(["last:live", "first:live"]);
expect(root.injectOptional(service)).toBeUndefined();
await root.dispose();
});
});