* Start `codegraph ui` and wait for the URL it prints. * * The banner IS the readiness signal: the server is bound before the URL is * printed, so anything the test does after this line is talking to a live * socket.
( args: string[], env: Record<string, string> )
| 73 | * socket. |
| 74 | */ |
| 75 | function startViewer( |
| 76 | args: string[], |
| 77 | env: Record<string, string> |
| 78 | ): Promise<{ child: ChildProcess; port: number; output: () => string }> { |
| 79 | return new Promise((resolve, reject) => { |
| 80 | const child = spawn(process.execPath, [BIN, 'ui', ...args], { |
| 81 | env: { ...BASE_ENV, ...env }, |
| 82 | stdio: ['ignore', 'pipe', 'pipe'], |
| 83 | }); |
| 84 | let output = ''; |
| 85 | const timer = setTimeout(() => { |
| 86 | child.kill('SIGKILL'); |
| 87 | reject(new Error(`codegraph ui never printed a URL. Output:\n${output}`)); |
| 88 | }, 30_000); |
| 89 | |
| 90 | const onChunk = (chunk: Buffer): void => { |
| 91 | output += chunk.toString('utf-8'); |
| 92 | const match = output.match(/http:\/\/127\.0\.0\.1:(\d+)/); |
| 93 | if (match?.[1]) { |
| 94 | clearTimeout(timer); |
| 95 | resolve({ child, port: Number(match[1]), output: () => output }); |
| 96 | } |
| 97 | }; |
| 98 | child.stdout?.on('data', onChunk); |
| 99 | child.stderr?.on('data', onChunk); |
| 100 | child.on('error', (err) => { |
| 101 | clearTimeout(timer); |
| 102 | reject(err); |
| 103 | }); |
| 104 | child.on('exit', (code) => { |
| 105 | clearTimeout(timer); |
| 106 | reject(new Error(`codegraph ui exited with ${code} before serving. Output:\n${output}`)); |
| 107 | }); |
| 108 | }); |
| 109 | } |
| 110 | |
| 111 | async function stopViewer(child: ChildProcess): Promise<void> { |
| 112 | if (child.exitCode !== null) return; |
no test coverage detected