Skip to content

Commit 2b53c28

Browse files
committed
test(plugins): keep npm query registries local to each case
1 parent bec94ae commit 2b53c28

1 file changed

Lines changed: 61 additions & 30 deletions

File tree

‎apps/web/src/components/settings/PluginsSettings.npm.browser.test.tsx‎

Lines changed: 61 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,15 @@ const npmPackage = (staged: string | null = null): PluginNpmPackage => ({
114114
stagedAt: "2026-10-04T01:00:00.000Z",
115115
},
116116
});
117-
let registry: AtomRegistry.AtomRegistry;
118117
const fake = () => server.current!;
119118
const npmAtom = pluginNpmEnvironment.packages({ environmentId, input: {} });
120119
const catalogAtom = pluginEnvironment.catalog({ environmentId, input: {} });
121120
/** Resolves once `atom` holds a value `matches` accepts; the registry's own notification is the receipt. */
122-
function until<A>(atom: Atom.Atom<A>, matches: (value: A) => boolean) {
121+
function until<A>(
122+
registry: AtomRegistry.AtomRegistry,
123+
atom: Atom.Atom<A>,
124+
matches: (value: A) => boolean,
125+
) {
123126
return act(
124127
() =>
125128
new Promise<void>((resolve) => {
@@ -139,14 +142,22 @@ function until<A>(atom: Atom.Atom<A>, matches: (value: A) => boolean) {
139142
}
140143
const nextCall = (method: string) => act(() => fake().next(method));
141144
/** Answers a list read and waits until the client holds that exact list. */
142-
async function answerList(call: HeldCall, list: PluginNpmListResult) {
145+
async function answerList(
146+
registry: AtomRegistry.AtomRegistry,
147+
call: HeldCall,
148+
list: PluginNpmListResult,
149+
) {
143150
await act(async () => call.reply(list));
144-
await until(npmAtom, (result) => result._tag === "Success" && result.value === list);
151+
await until(registry, npmAtom, (result) => result._tag === "Success" && result.value === list);
145152
}
146153
/** Publishes a catalogue and waits for the client to deliver it. */
147-
async function publish(installations: ReadonlyArray<PluginInstallation>) {
154+
async function publish(
155+
registry: AtomRegistry.AtomRegistry,
156+
installations: ReadonlyArray<PluginInstallation>,
157+
) {
148158
fake().publish(installations);
149159
await until(
160+
registry,
150161
catalogAtom,
151162
(result) =>
152163
result._tag === "Success" &&
@@ -181,6 +192,7 @@ const type = (input: HTMLInputElement, value: string) =>
181192
});
182193
const acknowledge = () => click(document.querySelector<HTMLElement>("[role=checkbox]")!);
183194
async function publishAndRender(
195+
registry: AtomRegistry.AtomRegistry,
184196
screen: Screen,
185197
installations: ReadonlyArray<PluginInstallation>,
186198
target = environment,
@@ -195,6 +207,7 @@ async function publishAndRender(
195207
),
196208
);
197209
await until(
210+
registry,
198211
catalogAtom,
199212
(result) =>
200213
result._tag === "Success" &&
@@ -204,43 +217,48 @@ async function publishAndRender(
204217
}
205218
/** Mounts the environment's plugin list and answers its first npm list read with `list`. */
206219
async function mount(
220+
registry: AtomRegistry.AtomRegistry,
207221
screen: Screen,
208222
installations: ReadonlyArray<PluginInstallation>,
209223
list: PluginNpmListResult,
210224
) {
211-
await publishAndRender(screen, installations);
212-
await answerList(await nextCall(WS_METHODS.pluginsNpmList), list);
225+
await publishAndRender(registry, screen, installations);
226+
await answerList(registry, await nextCall(WS_METHODS.pluginsNpmList), list);
213227
}
214228
describe("installing a plugin from npm", () => {
215229
it("offers nothing from npm and reads no npm list on a server without it", async () => {
216-
using _setup = await setup();
230+
using fixture = await setup();
231+
const { registry } = fixture;
232+
// Plugin queries are driven directly; this suite does not navigate.
217233
await using screen = await renderScreen(null, { router: false });
218234
const older = {
219235
...environment,
220236
serverConfig: {
221237
environment: { platform: { machine: "server" }, capabilities: { plugins: true } },
222238
},
223239
} as unknown as EnvironmentPresentation;
224-
await publishAndRender(screen, [installation()], older);
240+
await publishAndRender(registry, screen, [installation()], older);
225241
expect(elements("button", "Install from npm")).toEqual([]);
226242
expect(button("Review")).toBeTruthy();
227243
expect(fake().calls(WS_METHODS.pluginsNpmList)).toEqual([]);
228244
});
229245
it("downloads the exact version typed and keeps its reply over a list read that started before it", async () => {
230-
using _setup = await setup();
246+
using fixture = await setup();
247+
const { registry } = fixture;
248+
// Plugin queries are driven directly; this suite does not navigate.
231249
await using screen = await renderScreen(null, { router: false });
232-
await mount(screen, [], { packages: [] });
250+
await mount(registry, screen, [], { packages: [] });
233251
await click(button("Install from npm"));
234252
await type(field("Package"), " @acme/t3-notifier ");
235253
await type(field("Version or tag"), "1.2.3");
236254
await click(button("Download and review"));
237255
const add = await nextCall(WS_METHODS.pluginsNpmAdd);
238256
expect(add.input).toEqual({ name: "@acme/t3-notifier", version: "1.2.3" });
239257
// While the download runs, another client adds a plugin and the list is read again.
240-
await publish([other]);
241-
await answerList(await nextCall(WS_METHODS.pluginsNpmList), { packages: [] });
258+
await publish(registry, [other]);
259+
await answerList(registry, await nextCall(WS_METHODS.pluginsNpmList), { packages: [] });
242260
// The server lists the install just before replying, which starts another read.
243-
await publish([other, installation()]);
261+
await publish(registry, [other, installation()]);
244262
const readDuring = await nextCall(WS_METHODS.pluginsNpmList);
245263
const reply = npmPackage();
246264
await act(async () => add.reply({ installation: installation(), package: reply }));
@@ -249,7 +267,7 @@ describe("installing a plugin from npm", () => {
249267
expect(readDuring.interrupted).toBe(true);
250268
expect(reviewField("Package")).toBe("@acme/[email protected]");
251269
expect(reviewField("Integrity")).toContain(INTEGRITY);
252-
await answerList(afterInstall, { packages: [reply] });
270+
await answerList(registry, afterInstall, { packages: [reply] });
253271
expect(reviewField("Package")).toBe("@acme/[email protected]");
254272
await acknowledge();
255273
await click(button("Approve and enable"));
@@ -262,9 +280,11 @@ describe("installing a plugin from npm", () => {
262280
expect(enable.input).toEqual({ installationId });
263281
});
264282
it("sends nothing for a version range", async () => {
265-
using _setup = await setup();
283+
using fixture = await setup();
284+
const { registry } = fixture;
285+
// Plugin queries are driven directly; this suite does not navigate.
266286
await using screen = await renderScreen(null, { router: false });
267-
await mount(screen, [], { packages: [] });
287+
await mount(registry, screen, [], { packages: [] });
268288
await click(button("Install from npm"));
269289
await type(field("Package"), "t3-notifier");
270290
await type(field("Version or tag"), "^1.0.0");
@@ -275,9 +295,11 @@ describe("installing a plugin from npm", () => {
275295
});
276296
describe("reviewing an npm download", () => {
277297
it("discards an unapproved download by removing it, once", async () => {
278-
using _setup = await setup();
298+
using fixture = await setup();
299+
const { registry } = fixture;
300+
// Plugin queries are driven directly; this suite does not navigate.
279301
await using screen = await renderScreen(null, { router: false });
280-
await mount(screen, [installation()], { packages: [npmPackage()] });
302+
await mount(registry, screen, [installation()], { packages: [npmPackage()] });
281303
await click(button("Review"));
282304
expect(reviewField("Package")).toBe("@acme/[email protected]");
283305
await click(button("Discard"));
@@ -289,19 +311,21 @@ describe("reviewing an npm download", () => {
289311
});
290312
describe("updating an npm plugin", () => {
291313
it("refreshes provenance after a catalogue failure recovers with changed files", async () => {
292-
using _setup = await setup();
293-
// This test drives the plugin queries directly, without navigation.
314+
using fixture = await setup();
315+
const { registry } = fixture;
316+
// Plugin queries are driven directly; this suite does not navigate.
294317
await using screen = await renderScreen(null, { router: false });
295-
await mount(screen, [approved], { packages: [npmPackage()] });
318+
await mount(registry, screen, [approved], { packages: [npmPackage()] });
296319
await act(async () => fake().failCatalog(new Error("Catalogue unavailable")));
297-
await until(catalogAtom, (result) => result._tag === "Failure");
320+
await until(registry, catalogAtom, (result) => result._tag === "Failure");
298321
await expect.element(screen.getByText("Catalogue unavailable")).toBeVisible();
299322
const updated = [
300323
installation({ ...approved, generation: 2, source: { ...approved.source!, digest: STAGED } }),
301324
];
302325
fake().publish(updated);
303326
await click(button("Retry"));
304327
await until(
328+
registry,
305329
catalogAtom,
306330
(result) =>
307331
result._tag === "Success" &&
@@ -310,29 +334,35 @@ describe("updating an npm plugin", () => {
310334
);
311335
await expect.element(screen.getByText("Notifier", { exact: true })).toBeVisible();
312336
expect(fake().calls(WS_METHODS.pluginsNpmList)).toHaveLength(2);
313-
await answerList(await nextCall(WS_METHODS.pluginsNpmList), { packages: [npmPackage()] });
337+
await answerList(registry, await nextCall(WS_METHODS.pluginsNpmList), {
338+
packages: [npmPackage()],
339+
});
314340
});
315341

316342
it("checks what is installed after a failed download, even when a list arrived during it", async () => {
317-
using _setup = await setup();
343+
using fixture = await setup();
344+
const { registry } = fixture;
345+
// Plugin queries are driven directly; this suite does not navigate.
318346
await using screen = await renderScreen(null, { router: false });
319-
await mount(screen, [approved], { packages: [npmPackage()] });
347+
await mount(registry, screen, [approved], { packages: [npmPackage()] });
320348
await click(button("Actions for Notifier"));
321349
await click(button("Details"));
322350
await type(field("Version or tag"), "1.1.0");
323351
await click(button("Download update"));
324352
const stage = await nextCall(WS_METHODS.pluginsNpmStageUpdate);
325353
expect(stage.input).toEqual({ installationId, version: "1.1.0" });
326-
await publish([approved, other]);
327-
await answerList(await nextCall(WS_METHODS.pluginsNpmList), { packages: [npmPackage()] });
354+
await publish(registry, [approved, other]);
355+
await answerList(registry, await nextCall(WS_METHODS.pluginsNpmList), {
356+
packages: [npmPackage()],
357+
});
328358
await act(async () => stage.fail(new Error("The registry did not answer.")));
329359
const afterFailure = await nextCall(WS_METHODS.pluginsNpmList);
330360
expect(elements("[role=status]", "Checking what is installed…")).toHaveLength(1);
331361
expect(document.querySelector("[role=alert]")?.textContent).toBe(
332362
"The registry did not answer.",
333363
);
334364
// The download had landed after all: the next read shows it for review.
335-
await answerList(afterFailure, { packages: [npmPackage(STAGED)] });
365+
await answerList(registry, afterFailure, { packages: [npmPackage(STAGED)] });
336366
expect(reviewField("New version")).toBe("1.1.0");
337367
expect(button("Apply update").disabled).toBe(true);
338368
await acknowledge();
@@ -344,8 +374,9 @@ describe("updating an npm plugin", () => {
344374

345375
async function setup() {
346376
fake().reset();
347-
registry = AtomRegistry.make();
377+
const registry = AtomRegistry.make();
348378
return {
379+
registry,
349380
[Symbol.dispose]() {
350381
registry.dispose();
351382
},

0 commit comments

Comments
 (0)