| ... | @@ -8,10 +8,33 @@ import * as Duration from "effect/Duration"; | ... | @@ -8,10 +8,33 @@ import * as Duration from "effect/Duration"; |
| 8 | import * as Effect from "effect/Effect"; | 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as Fiber from "effect/Fiber"; | 9 | import * as Fiber from "effect/Fiber"; |
| 10 | import { TestClock } from "effect/testing"; | 10 | import { TestClock } from "effect/testing"; |
| | 11 | import { vi } from "vitest"; |
| 11 | | 12 | |
| 12 | import { readBootstrapEnvelope, resolveFdPath } from "./bootstrap"; | 13 | import { readBootstrapEnvelope, resolveFdPath } from "./bootstrap"; |
| 13 | import { assertNone, assertSome } from "@effect/vitest/utils"; | 14 | import { assertNone, assertSome } from "@effect/vitest/utils"; |
| 14 | | 15 | |
| | 16 | const openSyncInterceptor = vi.hoisted(() => ({ failPath: null as string | null })); |
| | 17 | |
| | 18 | vi.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 | |
| 15 | const TestEnvelopeSchema = Schema.Struct({ mode: Schema.String }); | 38 | const TestEnvelopeSchema = Schema.Struct({ mode: Schema.String }); |
| 16 | | 39 | |
| 17 | it.layer(NodeServices.layer)("readBootstrapEnvelope", (it) => { | 40 | it.layer(NodeServices.layer)("readBootstrapEnvelope", (it) => { |
| ... | @@ -47,6 +70,36 @@ it.layer(NodeServices.layer)("readBootstrapEnvelope", (it) => { | ... | @@ -47,6 +70,36 @@ it.layer(NodeServices.layer)("readBootstrapEnvelope", (it) => { |
| 47 | }), | 70 | }), |
| 48 | ); | 71 | ); |
| 49 | | 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 | |
| 50 | it.effect("returns none when the fd is unavailable", () => | 103 | it.effect("returns none when the fd is unavailable", () => |
| 51 | Effect.gen(function* () { | 104 | Effect.gen(function* () { |
| 52 | const fd = NFS.openSync("/dev/null", "r"); | 105 | const fd = NFS.openSync("/dev/null", "r"); |