|
| 1 | +import { assert, describe, it } from "@effect/vitest"; |
| 2 | +import { Duration, Effect, Layer, Schema, Sink, Stream } from "effect"; |
| 3 | +import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/unstable/http"; |
| 4 | +import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process"; |
| 5 | + |
| 6 | +import { |
| 7 | + DesktopBackendBootstrap, |
| 8 | + type DesktopBackendBootstrap as DesktopBackendBootstrapValue, |
| 9 | +} from "@t3tools/contracts"; |
| 10 | +import { runBackendProcess } from "./backendProcess.ts"; |
| 11 | + |
| 12 | +const bootstrap: DesktopBackendBootstrapValue = { |
| 13 | + mode: "desktop", |
| 14 | + noBrowser: true, |
| 15 | + port: 3773, |
| 16 | + t3Home: "/tmp/t3", |
| 17 | + host: "127.0.0.1", |
| 18 | + desktopBootstrapToken: "token", |
| 19 | + tailscaleServeEnabled: true, |
| 20 | + tailscaleServePort: 443, |
| 21 | + otlpTracesUrl: "http://127.0.0.1:4318/v1/traces", |
| 22 | +}; |
| 23 | + |
| 24 | +function makeProcess(options?: { |
| 25 | + readonly stdout?: Stream.Stream<Uint8Array>; |
| 26 | + readonly stderr?: Stream.Stream<Uint8Array>; |
| 27 | + readonly exitCode?: Effect.Effect<ChildProcessSpawner.ExitCode>; |
| 28 | + readonly kill?: ChildProcessSpawner.ChildProcessHandle["kill"]; |
| 29 | +}): ChildProcessSpawner.ChildProcessHandle { |
| 30 | + return ChildProcessSpawner.makeHandle({ |
| 31 | + pid: ChildProcessSpawner.ProcessId(123), |
| 32 | + stdout: options?.stdout ?? Stream.empty, |
| 33 | + stderr: options?.stderr ?? Stream.empty, |
| 34 | + all: Stream.merge(options?.stdout ?? Stream.empty, options?.stderr ?? Stream.empty), |
| 35 | + exitCode: options?.exitCode ?? Effect.succeed(ChildProcessSpawner.ExitCode(0)), |
| 36 | + isRunning: Effect.succeed(false), |
| 37 | + kill: options?.kill ?? (() => Effect.void), |
| 38 | + stdin: Sink.drain, |
| 39 | + getInputFd: () => Sink.drain, |
| 40 | + getOutputFd: () => Stream.empty, |
| 41 | + unref: Effect.succeed(Effect.void), |
| 42 | + }); |
| 43 | +} |
| 44 | + |
| 45 | +function httpClientLayer(status: number) { |
| 46 | + return Layer.succeed( |
| 47 | + HttpClient.HttpClient, |
| 48 | + HttpClient.make((request: HttpClientRequest.HttpClientRequest) => |
| 49 | + Effect.succeed(HttpClientResponse.fromWeb(request, new Response(null, { status }))), |
| 50 | + ), |
| 51 | + ); |
| 52 | +} |
| 53 | + |
| 54 | +function decodeBootstrap(raw: string) { |
| 55 | + return Schema.decodeEffect(Schema.fromJsonString(DesktopBackendBootstrap))(raw); |
| 56 | +} |
| 57 | + |
| 58 | +describe("runBackendProcess", () => { |
| 59 | + it.effect("spawns the backend with fd3 bootstrap JSON and reports HTTP readiness", () => |
| 60 | + Effect.gen(function* () { |
| 61 | + let spawnedCommand: ChildProcess.Command | undefined; |
| 62 | + let bootstrapJson = ""; |
| 63 | + let finishExit: (() => void) | undefined; |
| 64 | + let readyCount = 0; |
| 65 | + |
| 66 | + const spawnerLayer = Layer.succeed( |
| 67 | + ChildProcessSpawner.ChildProcessSpawner, |
| 68 | + ChildProcessSpawner.make((command) => |
| 69 | + Effect.gen(function* () { |
| 70 | + spawnedCommand = command; |
| 71 | + if (command._tag === "StandardCommand") { |
| 72 | + const fd3 = command.options.additionalFds?.fd3; |
| 73 | + if (fd3?.type === "input" && fd3.stream) { |
| 74 | + bootstrapJson = yield* fd3.stream.pipe(Stream.decodeText(), Stream.mkString); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return makeProcess({ |
| 79 | + exitCode: Effect.callback<ChildProcessSpawner.ExitCode>((resume) => { |
| 80 | + finishExit = () => resume(Effect.succeed(ChildProcessSpawner.ExitCode(0))); |
| 81 | + }), |
| 82 | + }); |
| 83 | + }), |
| 84 | + ), |
| 85 | + ); |
| 86 | + |
| 87 | + const exit = yield* runBackendProcess({ |
| 88 | + executablePath: "/electron", |
| 89 | + entryPath: "/server/bin.mjs", |
| 90 | + cwd: "/server", |
| 91 | + env: { ELECTRON_RUN_AS_NODE: "1" }, |
| 92 | + bootstrap, |
| 93 | + httpBaseUrl: new URL("http://127.0.0.1:3773"), |
| 94 | + captureOutput: true, |
| 95 | + onReady: () => |
| 96 | + Effect.sync(() => { |
| 97 | + readyCount += 1; |
| 98 | + finishExit?.(); |
| 99 | + }), |
| 100 | + }).pipe(Effect.scoped, Effect.provide(Layer.merge(spawnerLayer, httpClientLayer(200)))); |
| 101 | + |
| 102 | + assert.equal(exit.code, 0); |
| 103 | + assert.equal(readyCount, 1); |
| 104 | + assert.isDefined(spawnedCommand); |
| 105 | + if (spawnedCommand?._tag === "StandardCommand") { |
| 106 | + assert.equal(spawnedCommand.command, "/electron"); |
| 107 | + assert.deepEqual(spawnedCommand.args, ["/server/bin.mjs", "--bootstrap-fd", "3"]); |
| 108 | + assert.equal(spawnedCommand.options.cwd, "/server"); |
| 109 | + assert.equal(spawnedCommand.options.stdout, "pipe"); |
| 110 | + assert.equal(spawnedCommand.options.stderr, "pipe"); |
| 111 | + assert.equal(spawnedCommand.options.killSignal, "SIGTERM"); |
| 112 | + assert.isDefined(spawnedCommand.options.forceKillAfter); |
| 113 | + assert.equal( |
| 114 | + Duration.toMillis(Duration.fromInputUnsafe(spawnedCommand.options.forceKillAfter)), |
| 115 | + 2_000, |
| 116 | + ); |
| 117 | + } |
| 118 | + assert.deepEqual(yield* decodeBootstrap(bootstrapJson), bootstrap); |
| 119 | + }), |
| 120 | + ); |
| 121 | + |
| 122 | + it.effect("inherits child output when captureOutput is false", () => |
| 123 | + Effect.gen(function* () { |
| 124 | + let spawnedCommand: ChildProcess.Command | undefined; |
| 125 | + let finishExit: (() => void) | undefined; |
| 126 | + let readyCount = 0; |
| 127 | + |
| 128 | + const spawnerLayer = Layer.succeed( |
| 129 | + ChildProcessSpawner.ChildProcessSpawner, |
| 130 | + ChildProcessSpawner.make((command) => |
| 131 | + Effect.sync(() => { |
| 132 | + spawnedCommand = command; |
| 133 | + return makeProcess({ |
| 134 | + exitCode: Effect.callback<ChildProcessSpawner.ExitCode>((resume) => { |
| 135 | + finishExit = () => resume(Effect.succeed(ChildProcessSpawner.ExitCode(0))); |
| 136 | + }), |
| 137 | + }); |
| 138 | + }), |
| 139 | + ), |
| 140 | + ); |
| 141 | + |
| 142 | + const exit = yield* runBackendProcess({ |
| 143 | + executablePath: "/electron", |
| 144 | + entryPath: "/server/bin.mjs", |
| 145 | + cwd: "/server", |
| 146 | + env: { ELECTRON_RUN_AS_NODE: "1" }, |
| 147 | + bootstrap, |
| 148 | + httpBaseUrl: new URL("http://127.0.0.1:3773"), |
| 149 | + captureOutput: false, |
| 150 | + onReady: () => |
| 151 | + Effect.sync(() => { |
| 152 | + readyCount += 1; |
| 153 | + finishExit?.(); |
| 154 | + }), |
| 155 | + }).pipe(Effect.scoped, Effect.provide(Layer.merge(spawnerLayer, httpClientLayer(200)))); |
| 156 | + |
| 157 | + assert.equal(exit.code, 0); |
| 158 | + assert.equal(readyCount, 1); |
| 159 | + assert.isDefined(spawnedCommand); |
| 160 | + if (spawnedCommand?._tag === "StandardCommand") { |
| 161 | + assert.equal(spawnedCommand.options.stdout, "inherit"); |
| 162 | + assert.equal(spawnedCommand.options.stderr, "inherit"); |
| 163 | + } |
| 164 | + }), |
| 165 | + ); |
| 166 | +}); |
0 commit comments