Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

bootstrap.test.ts

149 lines5.1 KB

1import * as NFS from "node:fs";
2import * as path from "node:path";
3import { execFileSync, spawn } from "node:child_process";
4import * as NodeServices from "@effect/platform-node/NodeServices";
5import { assert, it } from "@effect/vitest";
6import { FileSystem, Schema } from "effect";
7import * as Duration from "effect/Duration";
8import * as Effect from "effect/Effect";
9import * as Fiber from "effect/Fiber";
10import { TestClock } from "effect/testing";
11import { vi } from "vitest";
12​
13import { readBootstrapEnvelope, resolveFdPath } from "./bootstrap.ts";
14import { assertNone, assertSome } from "@effect/vitest/utils";
15​
16const openSyncInterceptor = vi.hoisted(() => ({ failPath: null as string | null }));
17​
18vi.mock("node:fs", async (importOriginal) => {
19 const actual = await importOriginal<typeof import("node:fs")>();
20 return {
21 ...actual,
22 openSync: (...args: Parameters<typeof actual.openSync>) => {
23 const [filePath, flags] = args;
24 if (
25 typeof filePath === "string" &&
26 filePath === openSyncInterceptor.failPath &&
27 flags === "r"
28 ) {
29 const error = new Error("no such device or address");
30 Object.assign(error, { code: "ENXIO" });
31 throw error;
32 }
33 return (actual.openSync as (...a: typeof args) => number)(...args);
34 },
35 };
36});
37​
38const TestEnvelopeSchema = Schema.Struct({ mode: Schema.String });
39​
40it.layer(NodeServices.layer)("readBootstrapEnvelope", (it) => {
41 it.effect("uses platform-specific fd paths", () =>
42 Effect.sync(() => {
43 assert.equal(resolveFdPath(3, "linux"), "/proc/self/fd/3");
44 assert.equal(resolveFdPath(3, "darwin"), "/dev/fd/3");
45 assert.equal(resolveFdPath(3, "win32"), undefined);
46 }),
47 );
48​
49 it.effect("reads a bootstrap envelope from a provided fd", () =>
50 Effect.gen(function* () {
51 const fs = yield* FileSystem.FileSystem;
52 const filePath = yield* fs.makeTempFileScoped({ prefix: "t3-bootstrap-", suffix: ".ndjson" });
53​
54 yield* fs.writeFileString(
55 filePath,
56 `${yield* Schema.encodeEffect(Schema.fromJsonString(TestEnvelopeSchema))({
57 mode: "desktop",
58 })}\n`,
59 );
60​
61 const fd = yield* Effect.acquireRelease(
62 Effect.sync(() => NFS.openSync(filePath, "r")),
63 (fd) => Effect.sync(() => NFS.closeSync(fd)),
64 );
65​
66 const payload = yield* readBootstrapEnvelope(TestEnvelopeSchema, fd, { timeoutMs: 100 });
67 assertSome(payload, {
68 mode: "desktop",
69 });
70 }),
71 );
72​
73 it.effect("falls back to reading the inherited fd when path duplication fails", () =>
74 Effect.gen(function* () {
75 const fs = yield* FileSystem.FileSystem;
76 const filePath = yield* fs.makeTempFileScoped({ prefix: "t3-bootstrap-", suffix: ".ndjson" });
77​
78 yield* fs.writeFileString(
79 filePath,
80 `${yield* Schema.encodeEffect(Schema.fromJsonString(TestEnvelopeSchema))({
81 mode: "desktop",
82 })}\n`,
83 );
84​
85 // Open without acquireRelease: the direct-stream fallback uses autoClose: true,
86 // so the stream owns the fd lifecycle and closes it asynchronously on end.
87 // Attempting to also close it synchronously in a finalizer races with the
88 // stream's async close and produces an uncaught EBADF.
89 const fd = NFS.openSync(filePath, "r");
90​
91 openSyncInterceptor.failPath = `/proc/self/fd/${fd}`;
92 try {
93 const payload = yield* readBootstrapEnvelope(TestEnvelopeSchema, fd, { timeoutMs: 100 });
94 assertSome(payload, {
95 mode: "desktop",
96 });
97 } finally {
98 openSyncInterceptor.failPath = null;
99 }
100 }),
101 );
102​
103 it.effect("returns none when the fd is unavailable", () =>
104 Effect.gen(function* () {
105 const fd = NFS.openSync("/dev/null", "r");
106 NFS.closeSync(fd);
107​
108 const payload = yield* readBootstrapEnvelope(TestEnvelopeSchema, fd, { timeoutMs: 100 });
109 assertNone(payload);
110 }),
111 );
112​
113 it.effect("returns none when the bootstrap read times out before any value arrives", () =>
114 Effect.gen(function* () {
115 const fs = yield* FileSystem.FileSystem;
116 const tempDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-bootstrap-" });
117 const fifoPath = path.join(tempDir, "bootstrap.pipe");
118​
119 yield* Effect.sync(() => execFileSync("mkfifo", [fifoPath]));
120​
121 const _writer = yield* Effect.acquireRelease(
122 Effect.sync(() =>
123 spawn("sh", ["-c", 'exec 3>"$1"; sleep 60', "sh", fifoPath], {
124 stdio: ["ignore", "ignore", "ignore"],
125 }),
126 ),
127 (writer) =>
128 Effect.sync(() => {
129 writer.kill("SIGKILL");
130 }),
131 );
132​
133 const fd = yield* Effect.acquireRelease(
134 Effect.sync(() => NFS.openSync(fifoPath, "r")),
135 (fd) => Effect.sync(() => NFS.closeSync(fd)),
136 );
137​
138 const fiber = yield* readBootstrapEnvelope(TestEnvelopeSchema, fd, {
139 timeoutMs: 100,
140 }).pipe(Effect.forkScoped);
141​
142 yield* Effect.yieldNow;
143 yield* TestClock.adjust(Duration.millis(100));
144​
145 const payload = yield* Fiber.join(fiber);
146 assertNone(payload);
147 }).pipe(Effect.provide(TestClock.layer())),
148 );
149});