import { describe, it, expect } from 'vitest';
import { PassThrough } from 'node:stream';
import { startCli } from '../../src/interfaces/cli.js';
describe('cli interface', () => {
it('responds to health and getJoke commands via stdio', async () => {
const input = new PassThrough();
const output = new PassThrough();
const lines: string[] = [];
const waiters: Array<(line: string) => void> = [];
let buffer = '';
output.on('data', (chunk) => {
buffer += chunk.toString();
let newlineIndex = buffer.indexOf('\n');
while (newlineIndex !== -1) {
const line = buffer.slice(0, newlineIndex);
buffer = buffer.slice(newlineIndex + 1);
if (waiters.length > 0) {
const resolve = waiters.shift();
resolve?.(line);
} else {
lines.push(line);
}
newlineIndex = buffer.indexOf('\n');
}
});
const nextLine = () => {
if (lines.length > 0) {
return Promise.resolve(lines.shift()!);
}
return new Promise<string>((resolve) => {
waiters.push(resolve);
});
};
const cli = startCli({
input,
output,
env: {
...process.env,
ALLOW_NET: 'false',
LOG_VERBOSE: 'false',
},
});
input.write('health\n');
const healthLine = await nextLine();
expect(JSON.parse(healthLine)).toEqual({ ok: true });
input.write('getJoke {"category":"programming","lang":"en"}\n');
const jokeLine = await nextLine();
const jokeResponse = JSON.parse(jokeLine);
expect(jokeResponse.text.length).toBeGreaterThan(0);
expect(jokeResponse.source).toBe('local-fallback');
expect(jokeResponse.category).toBe('programming');
expect(typeof jokeResponse.latency_ms).toBe('number');
cli.close();
input.end();
await cli.closed;
});
});