|
| 1 | +import { afterEach, beforeEach, expect, test } from "bun:test" |
| 2 | +import { mkdtemp, rm } from "node:fs/promises" |
| 3 | +import { tmpdir } from "node:os" |
| 4 | +import path from "node:path" |
| 5 | + |
| 6 | +const QUERY = |
| 7 | + "with recursive t(n) as (select 1 union all select n+1 from t where n < 15000) select n as n, '0123456789012345678901234567890123456789' as pad from t" |
| 8 | +const ROOT = path.resolve(import.meta.dir, "../..") |
| 9 | + |
| 10 | +let tmp: string |
| 11 | + |
| 12 | +beforeEach(async () => { |
| 13 | + tmp = await mkdtemp(path.join(tmpdir(), "opencode-stdout-")) |
| 14 | +}) |
| 15 | + |
| 16 | +afterEach(async () => { |
| 17 | + await rm(tmp, { recursive: true, force: true }) |
| 18 | +}) |
| 19 | + |
| 20 | +function spawn(stdout: "pipe"): Bun.Subprocess<"ignore", "pipe", "pipe"> |
| 21 | +function spawn(stdout: ReturnType<typeof Bun.file>): Bun.Subprocess<"ignore", ReturnType<typeof Bun.file>, "pipe"> |
| 22 | +function spawn(stdout: "pipe" | ReturnType<typeof Bun.file>) { |
| 23 | + const isolated = { |
| 24 | + OPENCODE_TEST_HOME: tmp, |
| 25 | + HOME: tmp, |
| 26 | + XDG_CONFIG_HOME: path.join(tmp, ".config"), |
| 27 | + XDG_DATA_HOME: path.join(tmp, ".local/share"), |
| 28 | + XDG_STATE_HOME: path.join(tmp, ".local/state"), |
| 29 | + XDG_CACHE_HOME: path.join(tmp, ".cache"), |
| 30 | + OPENCODE_CONFIG_CONTENT: "{}", |
| 31 | + OPENCODE_DISABLE_PROJECT_CONFIG: "1", |
| 32 | + OPENCODE_PURE: "1", |
| 33 | + OPENCODE_DISABLE_AUTOUPDATE: "1", |
| 34 | + OPENCODE_DISABLE_AUTOCOMPACT: "1", |
| 35 | + OPENCODE_DISABLE_MODELS_FETCH: "1", |
| 36 | + OPENCODE_AUTH_CONTENT: "{}", |
| 37 | + } |
| 38 | + return Bun.spawn([process.execPath, "src/index.ts", "db", "--format", "json", QUERY], { |
| 39 | + cwd: ROOT, |
| 40 | + stdout, |
| 41 | + stderr: "pipe", |
| 42 | + env: { ...process.env, ...isolated }, |
| 43 | + }) |
| 44 | +} |
| 45 | + |
| 46 | +// Delay the first read until the pipe fills so returning before stdout drains |
| 47 | +// deterministically exposes truncated output. |
| 48 | +test("large JSON output is complete when the reader is slow", async () => { |
| 49 | + const proc = spawn("pipe") |
| 50 | + await Bun.sleep(2000) |
| 51 | + const text = await new Response(proc.stdout).text() |
| 52 | + await proc.exited |
| 53 | + expect(proc.exitCode).toBe(0) |
| 54 | + expect(text.length).toBeGreaterThan(65536) |
| 55 | + expect(JSON.parse(text)).toHaveLength(15000) |
| 56 | + |
| 57 | + const file = path.join(tmp, "full.json") |
| 58 | + const ctl = spawn(Bun.file(file)) |
| 59 | + await ctl.exited |
| 60 | + expect(text).toBe(await Bun.file(file).text()) |
| 61 | +}, 60_000) |
| 62 | + |
| 63 | +test("exits cleanly when the reader goes away", async () => { |
| 64 | + const proc = spawn("pipe") |
| 65 | + const reader = proc.stdout.getReader() |
| 66 | + await reader.read() |
| 67 | + await reader.cancel() |
| 68 | + const code = await Promise.race([proc.exited, Bun.sleep(5000).then(() => "timeout")]) |
| 69 | + expect(code).toBe(0) |
| 70 | + const err = await new Response(proc.stderr).text() |
| 71 | + expect(err).not.toMatch(/EPIPE|Unhandled/) |
| 72 | +}, 60_000) |
0 commit comments