|
| 1 | +// @effect-diagnostics nodeBuiltinImport:off |
| 2 | +import * as NodeChildProcess from "node:child_process"; |
| 3 | +import * as NodeReadline from "node:readline"; |
| 4 | +import type * as NodeStream from "node:stream"; |
| 5 | + |
| 6 | +import { afterEach, describe, expect, it } from "@effect/vitest"; |
| 7 | + |
| 8 | +// Children run the real CLI entry, which routes `__plugin-host` to the child runtime. |
| 9 | +const BIN_PATH = `${import.meta.dirname}/../bin.ts`; |
| 10 | +const FIXTURE_DIR = `${import.meta.dirname}/testFixtures/plugin`; |
| 11 | + |
| 12 | +const children = new Set<NodeChildProcess.ChildProcess>(); |
| 13 | +afterEach(() => { |
| 14 | + for (const child of children) child.kill(); |
| 15 | + children.clear(); |
| 16 | +}); |
| 17 | + |
| 18 | +/** Speaks the raw fd 3 protocol with one plugin child, as the supervisor would. */ |
| 19 | +const startChild = () => { |
| 20 | + const child = NodeChildProcess.spawn(process.execPath, [BIN_PATH, "__plugin-host"], { |
| 21 | + cwd: FIXTURE_DIR, |
| 22 | + stdio: ["ignore", "ignore", "ignore", "pipe"], |
| 23 | + }); |
| 24 | + children.add(child); |
| 25 | + const exited = new Promise<[number | null, NodeJS.Signals | null]>((resolve) => |
| 26 | + child.once("exit", (code, signal) => resolve([code, signal])), |
| 27 | + ); |
| 28 | + const channel = child.stdio[3] as NodeStream.Duplex; |
| 29 | + const lines = NodeReadline.createInterface({ input: channel })[Symbol.asyncIterator](); |
| 30 | + return { |
| 31 | + exited, |
| 32 | + send: (line: string) => channel.write(`${line}\n`), |
| 33 | + read: async () => { |
| 34 | + const next = await lines.next(); |
| 35 | + return next.done ? undefined : (JSON.parse(next.value) as Record<string, unknown>); |
| 36 | + }, |
| 37 | + }; |
| 38 | +}; |
| 39 | + |
| 40 | +const activateMessage = (entry: string) => |
| 41 | + JSON.stringify({ |
| 42 | + _tag: "Activate", |
| 43 | + pluginId: "test.child", |
| 44 | + version: "1.0.0", |
| 45 | + apiVersion: 1, |
| 46 | + entryPath: `${FIXTURE_DIR}/${entry}`, |
| 47 | + proposedApi: true, |
| 48 | + maxMessageBytes: 64 * 1024, |
| 49 | + capabilities: [], |
| 50 | + }); |
| 51 | + |
| 52 | +describe("plugin host child", () => { |
| 53 | + it("drops what a plugin registered before its activation failed", async () => { |
| 54 | + const child = startChild(); |
| 55 | + child.send(activateMessage("registerThenFail.mjs")); |
| 56 | + // The activation signal aborts before the failure is reported. |
| 57 | + expect(await child.read()).toEqual({ |
| 58 | + _tag: "Log", |
| 59 | + level: "info", |
| 60 | + message: "activation-aborted", |
| 61 | + }); |
| 62 | + expect(await child.read()).toEqual({ |
| 63 | + _tag: "ActivationFailed", |
| 64 | + message: "activation refused", |
| 65 | + }); |
| 66 | + |
| 67 | + child.send(JSON.stringify({ _tag: "Invoke", requestId: 1, handler: "leftover", input: null })); |
| 68 | + expect(await child.read()).toEqual({ |
| 69 | + _tag: "Failed", |
| 70 | + requestId: 1, |
| 71 | + message: 'No handler named "leftover".', |
| 72 | + }); |
| 73 | + |
| 74 | + // Deactivation skips the module's deactivate: it never started. |
| 75 | + child.send(JSON.stringify({ _tag: "Deactivate" })); |
| 76 | + expect(await child.read()).toEqual({ _tag: "Deactivated" }); |
| 77 | + expect(await child.read()).toBeUndefined(); |
| 78 | + expect(await child.exited).toEqual([0, null]); |
| 79 | + }); |
| 80 | + |
| 81 | + it("exits with a failure on a line that is not JSON", async () => { |
| 82 | + const child = startChild(); |
| 83 | + child.send("{not json"); |
| 84 | + expect(await child.exited).toEqual([1, null]); |
| 85 | + }); |
| 86 | +}); |
0 commit comments