|
| 1 | +import { NodeFileSystem } from "@effect/platform-node" |
| 2 | +import { afterEach, expect, test } from "bun:test" |
| 3 | +import { Effect } from "effect" |
| 4 | +import { mkdtemp, rm, writeFile } from "node:fs/promises" |
| 5 | +import { tmpdir } from "node:os" |
| 6 | +import { join } from "node:path" |
| 7 | +import { Service } from "../src/effect/index" |
| 8 | + |
| 9 | +const fixture = join(import.meta.dir, "fixture/service.ts") |
| 10 | +const processes: Bun.Subprocess[] = [] |
| 11 | +const directories: string[] = [] |
| 12 | + |
| 13 | +afterEach(async () => { |
| 14 | + processes.forEach((process) => process.kill("SIGTERM")) |
| 15 | + await Promise.all(processes.splice(0).map((process) => process.exited)) |
| 16 | + await Promise.all(directories.splice(0).map((directory) => rm(directory, { recursive: true, force: true }))) |
| 17 | +}) |
| 18 | + |
| 19 | +test("a concurrent same-version start cannot invalidate a resolved endpoint", async () => { |
| 20 | + const directory = await temp() |
| 21 | + const registration = join(directory, "service.json") |
| 22 | + spawn(registration, "modern") |
| 23 | + await waitForFile(registration) |
| 24 | + const original = await Bun.file(registration).json() |
| 25 | + |
| 26 | + const starts: Service.StartReason[] = [] |
| 27 | + const first = run( |
| 28 | + Service.start({ |
| 29 | + file: registration, |
| 30 | + version: "test", |
| 31 | + command: [], |
| 32 | + onStart: (reason) => starts.push(reason), |
| 33 | + }), |
| 34 | + ) |
| 35 | + await waitForFile(registration + ".first-request") |
| 36 | + |
| 37 | + const resolved = await run(Service.start({ file: registration, version: "test" })) |
| 38 | + expect(resolved.url).toBe(original.url) |
| 39 | + |
| 40 | + await writeFile(registration + ".release", "") |
| 41 | + await first |
| 42 | + |
| 43 | + expect(starts).toEqual([]) |
| 44 | + expect(await Bun.file(registration).json()).toEqual(original) |
| 45 | + expect(await health(resolved.url)).toEqual({ healthy: true, version: "test", pid: original.pid }) |
| 46 | +}) |
| 47 | + |
| 48 | +test("a legacy health response is still replaced", async () => { |
| 49 | + const directory = await temp() |
| 50 | + const registration = join(directory, "service.json") |
| 51 | + const existing = spawn(registration, "legacy") |
| 52 | + await waitForFile(registration) |
| 53 | + |
| 54 | + const starts: Service.StartReason[] = [] |
| 55 | + const result = run(Service.start({ file: registration, command: [], onStart: (reason) => starts.push(reason) })) |
| 56 | + |
| 57 | + await expect(result).rejects.toThrow("Missing service command") |
| 58 | + expect(starts).toEqual(["version-mismatch"]) |
| 59 | + await existing.exited |
| 60 | +}) |
| 61 | + |
| 62 | +function run<A, E>(effect: Effect.Effect<A, E, never>) { |
| 63 | + return Effect.runPromise(effect.pipe(Effect.provide(NodeFileSystem.layer))) |
| 64 | +} |
| 65 | + |
| 66 | +function spawn(registration: string, mode: string, ...args: string[]) { |
| 67 | + const subprocess = Bun.spawn([process.execPath, fixture, registration, mode, ...args], { |
| 68 | + stdout: "ignore", |
| 69 | + stderr: "inherit", |
| 70 | + }) |
| 71 | + processes.push(subprocess) |
| 72 | + return subprocess |
| 73 | +} |
| 74 | + |
| 75 | +async function temp() { |
| 76 | + const directory = await mkdtemp(join(tmpdir(), "opencode-client-service-")) |
| 77 | + directories.push(directory) |
| 78 | + return directory |
| 79 | +} |
| 80 | + |
| 81 | +async function waitForFile(file: string) { |
| 82 | + for (let attempt = 0; attempt < 600; attempt++) { |
| 83 | + if (await Bun.file(file).exists()) return |
| 84 | + await Bun.sleep(5) |
| 85 | + } |
| 86 | + throw new Error(`Timed out waiting for ${file}`) |
| 87 | +} |
| 88 | + |
| 89 | +async function health(url: string) { |
| 90 | + return fetch(new URL("/api/health", url), { signal: AbortSignal.timeout(1_000) }).then((response) => response.json()) |
| 91 | +} |
0 commit comments