import { describe, it, expect } from 'vitest';
import { createInterface } from 'readline/promises';
import { Readable, Writable } from 'stream';
import { createPrompter } from './prompts.js';
import { runMenu } from './orchestrator.js';
import { ConfigBuilder } from './config.js';

function mockReadline(inputs: string[]) {
  let idx = 0;
  const input = new Readable({
    read() {
      if (idx < inputs.length) {
        this.push(inputs[idx++] + '\n');
      } else {
        this.push(null);
      }
    },
  });
  const output = new Writable({ write(_, __, cb) { cb(); } });
  return createInterface({ input, output });
}

describe('runMenu', () => {
  it('exits immediately on 0', async () => {
    const rl = mockReadline(['0']);
    const p = createPrompter(rl);
    const builder = new ConfigBuilder();
    builder.setProvider('default', { provider: 'anthropic', model: 'test', api_key: 'k' });
    await runMenu(p, builder);
    // Should return without error
  });
});
