|
| 1 | +import { once } from "node:events"; |
| 2 | +import nodeFs from "node:fs"; |
1 | 3 | import fs from "node:fs/promises"; |
2 | 4 | import os from "node:os"; |
3 | 5 | import path from "node:path"; |
4 | 6 | import { afterEach, describe, expect, it, vi } from "vitest"; |
| 7 | +import { createDeferred } from "../../../helpers/promise.js"; |
5 | 8 | import { useAutoCleanupTempDirTracker } from "../../../helpers/temp-dir.js"; |
6 | 9 | import { |
7 | 10 | createChildEnv, |
8 | 11 | parseNodeMcpTextRecord, |
9 | 12 | processIsAlive, |
10 | 13 | startHttpFixture, |
11 | 14 | stopChild, |
| 15 | + waitForMcpFixtureGate, |
12 | 16 | } from "./gateway-node-mcp.test-support.js"; |
13 | 17 |
|
14 | 18 | const tempDirs = useAutoCleanupTempDirTracker(afterEach); |
15 | 19 |
|
16 | 20 | describe("gateway node MCP fixture ownership", () => { |
| 21 | + it.each(["existing", "published"] as const)( |
| 22 | + "joins its watcher when the gate is %s", |
| 23 | + async (mode) => { |
| 24 | + const root = tempDirs.make("mcp-gate-publication-"); |
| 25 | + const gate = path.join(root, "gate"); |
| 26 | + if (mode === "existing") { |
| 27 | + await fs.writeFile(gate, "ready"); |
| 28 | + } |
| 29 | + const watching = createDeferred<nodeFs.FSWatcher>(); |
| 30 | + const watch = nodeFs.watch; |
| 31 | + const observeWatch = vi.fn((...args: Parameters<typeof watch>) => { |
| 32 | + const watcher = watch(...args); |
| 33 | + watching.resolve(watcher); |
| 34 | + return watcher; |
| 35 | + }); |
| 36 | + vi.resetModules(); |
| 37 | + vi.doMock("node:fs", () => ({ ...nodeFs, watch: observeWatch })); |
| 38 | + const { waitForMcpFixtureGate: waitForGate } = |
| 39 | + await import("./gateway-node-mcp.test-support.js"); |
| 40 | + let watcher: nodeFs.FSWatcher | undefined; |
| 41 | + const waiting = waitForGate(gate); |
| 42 | + try { |
| 43 | + if (mode === "published") { |
| 44 | + watcher = await watching.promise; |
| 45 | + const closed = once(watcher, "close"); |
| 46 | + await fs.writeFile(gate, "ready"); |
| 47 | + await waiting; |
| 48 | + await closed; |
| 49 | + } else { |
| 50 | + await waiting; |
| 51 | + expect(observeWatch).not.toHaveBeenCalled(); |
| 52 | + } |
| 53 | + expect(await fs.readFile(gate, "utf8")).toBe("ready"); |
| 54 | + } finally { |
| 55 | + watcher?.close(); |
| 56 | + await waiting; |
| 57 | + vi.doUnmock("node:fs"); |
| 58 | + vi.resetModules(); |
| 59 | + } |
| 60 | + }, |
| 61 | + ); |
| 62 | + |
| 63 | + it("releases its deadline when the real gate watcher cannot be constructed", async () => { |
| 64 | + const root = tempDirs.make("mcp-gate-watch-failure-"); |
| 65 | + const timers = vi.spyOn(globalThis, "setTimeout"); |
| 66 | + const cleared = vi.spyOn(globalThis, "clearTimeout"); |
| 67 | + try { |
| 68 | + await expect(waitForMcpFixtureGate(path.join(root, "missing", "gate"))).rejects.toMatchObject( |
| 69 | + { |
| 70 | + code: "ENOENT", |
| 71 | + syscall: "watch", |
| 72 | + }, |
| 73 | + ); |
| 74 | + const allocated = timers.mock.results.flatMap((result, index) => |
| 75 | + result.type === "return" && timers.mock.calls[index]?.[1] === 30_000 ? [result.value] : [], |
| 76 | + ); |
| 77 | + expect( |
| 78 | + allocated.filter((timer) => !cleared.mock.calls.some(([value]) => value === timer)).length, |
| 79 | + ).toBe(0); |
| 80 | + } finally { |
| 81 | + for (const result of timers.mock.results) { |
| 82 | + if (result.type === "return") { |
| 83 | + clearTimeout(result.value); |
| 84 | + } |
| 85 | + } |
| 86 | + timers.mockRestore(); |
| 87 | + cleared.mockRestore(); |
| 88 | + } |
| 89 | + }); |
| 90 | + |
17 | 91 | it.each([ |
18 | 92 | ["direct", (payload: object) => payload], |
19 | 93 | ["node.invoke", (payload: object) => ({ ok: true, payload })], |
|
0 commit comments