|
| 1 | +/** |
| 2 | + * #3039: `chain` with no arguments reads its JSON flow from stdin. On Windows |
| 3 | + * an awaited Bun.stdin.text() inside the CLI's un-awaited main() let the |
| 4 | + * process exit 0 before reading, so a replayed flow "passed" with nothing |
| 5 | + * sent to the daemon. The CLI reads stdin synchronously; this drives the real |
| 6 | + * CLI against a stub daemon (the Windows free lane runs it natively). |
| 7 | + */ |
| 8 | +import { afterEach, beforeEach, describe, expect, test } from 'bun:test'; |
| 9 | +import * as fs from 'node:fs'; |
| 10 | +import * as os from 'node:os'; |
| 11 | +import * as path from 'node:path'; |
| 12 | + |
| 13 | +const CLI = path.resolve(import.meta.dir, '../src/cli.ts'); |
| 14 | + |
| 15 | +describe('#3039: chain reads its flow from piped stdin', () => { |
| 16 | + let scratch: string; |
| 17 | + let daemon: ReturnType<typeof Bun.serve>; |
| 18 | + let received: Array<{ command: string; args: string[] }>; |
| 19 | + |
| 20 | + beforeEach(() => { |
| 21 | + scratch = fs.mkdtempSync(path.join(os.tmpdir(), 'browse-chain-stdin-')); |
| 22 | + received = []; |
| 23 | + daemon = Bun.serve({ |
| 24 | + hostname: '127.0.0.1', |
| 25 | + port: 0, |
| 26 | + fetch: async (req) => { |
| 27 | + const url = new URL(req.url); |
| 28 | + if (url.pathname === '/health') return Response.json({ status: 'healthy' }); |
| 29 | + if (url.pathname === '/command') { |
| 30 | + const body = await req.json(); |
| 31 | + received.push(body); |
| 32 | + return body.args?.[0] ? new Response('chain ran') : new Response('Usage: echo \'[["goto","url"]]\' | browse chain', { status: 400 }); |
| 33 | + } |
| 34 | + return new Response('not found', { status: 404 }); |
| 35 | + }, |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + afterEach(() => { |
| 40 | + try { daemon.stop(true); } catch {} |
| 41 | + fs.rmSync(scratch, { recursive: true, force: true }); |
| 42 | + }); |
| 43 | + |
| 44 | + async function runChain(stdin: string) { |
| 45 | + const stateFile = path.join(scratch, 'browse.json'); |
| 46 | + fs.writeFileSync(stateFile, JSON.stringify({ pid: process.pid, port: daemon.port, token: 'chain-stdin-test', mode: 'launched' })); |
| 47 | + const env: Record<string, string> = {}; |
| 48 | + for (const [key, value] of Object.entries(process.env)) { |
| 49 | + if (value !== undefined && !/^(BROWSE_|GSTACK_)/.test(key)) env[key] = value; |
| 50 | + } |
| 51 | + Object.assign(env, { HOME: scratch, GSTACK_HOME: path.join(scratch, '.gstack'), BROWSE_STATE_FILE: stateFile }); |
| 52 | + const cli = Bun.spawn([process.execPath, CLI, 'chain'], { |
| 53 | + cwd: scratch, env, stdin: new TextEncoder().encode(stdin), stdout: 'pipe', stderr: 'pipe', |
| 54 | + }); |
| 55 | + const timer = setTimeout(() => cli.kill('SIGKILL'), 30_000); |
| 56 | + const [code, stdout, stderr] = await Promise.all([cli.exited, new Response(cli.stdout).text(), new Response(cli.stderr).text()]); |
| 57 | + clearTimeout(timer); |
| 58 | + return { code, out: stdout + stderr }; |
| 59 | + } |
| 60 | + |
| 61 | + test('a piped flow reaches the daemon as the chain argument', async () => { |
| 62 | + const flow = '[["js","1+1"]]'; |
| 63 | + const r = await runChain(`${flow}\n`); |
| 64 | + expect(received).toEqual([{ command: 'chain', args: [flow] }]); |
| 65 | + expect(r.out).toContain('chain ran'); |
| 66 | + expect(r.code).toBe(0); |
| 67 | + }, 45_000); |
| 68 | + |
| 69 | + test('empty stdin is a usage error, never a silent exit 0', async () => { |
| 70 | + const r = await runChain(''); |
| 71 | + expect(r.code).not.toBe(0); |
| 72 | + expect(r.out).toContain('Usage'); |
| 73 | + }, 45_000); |
| 74 | +}); |
0 commit comments