import { describe, it, expect, beforeEach } from 'vitest';
import { StubSetupProcessGateway } from '../../../../../tests/stubs/setupProcess.stub.js';
import { SetupRunRegistry } from '../../../../../modules/setup-wizard/usecases/streamSetupRun.usecase.js';
describe('SetupRunRegistry', () => {
    let registry;
    let gateway;
    beforeEach(() => {
        gateway = new StubSetupProcessGateway();
        registry = new SetupRunRegistry(gateway);
    });
    it('starts a run and returns a run id', () => {
        const result = registry.start({ projectPath: null });
        expect(result.status).toBe('started');
        if (result.status === 'started') {
            expect(result.runId).toMatch(/.+/);
        }
        expect(gateway.spawnCount).toBe(1);
    });
    it('rejects a second start while a run is already active', () => {
        registry.start({ projectPath: null });
        const second = registry.start({ projectPath: null });
        expect(second.status).toBe('already-active');
        expect(gateway.spawnCount).toBe(1);
    });
    it('allows a new run after the active run exits', () => {
        registry.start({ projectPath: null });
        gateway.exit(0);
        const second = registry.start({ projectPath: null });
        expect(second.status).toBe('started');
        expect(gateway.spawnCount).toBe(2);
    });
    it('forwards stdout lines to subscribed listeners of the active run', () => {
        const started = registry.start({ projectPath: null });
        const received = [];
        if (started.status === 'started') {
            registry.subscribe(started.runId, {
                onEvent: (line) => received.push(line),
                onClose: () => { },
            });
        }
        gateway.emitLine('{"step":"dependencies","status":"in_progress","message":"x"}');
        expect(received).toEqual(['{"step":"dependencies","status":"in_progress","message":"x"}']);
    });
    it('notifies subscribers when the process exits', () => {
        const started = registry.start({ projectPath: null });
        let closedCode;
        if (started.status === 'started') {
            registry.subscribe(started.runId, {
                onEvent: () => { },
                onClose: (code) => {
                    closedCode = code;
                },
            });
        }
        gateway.exit(0);
        expect(closedCode).toBe(0);
    });
    it('reports no active run when none has started', () => {
        expect(registry.hasActiveRun()).toBe(false);
    });
    it('reports an active run after start', () => {
        registry.start({ projectPath: null });
        expect(registry.hasActiveRun()).toBe(true);
    });
    it('writes a submitted input line to the active run stdin', () => {
        const started = registry.start({ projectPath: null });
        const runId = started.status === 'started' ? started.runId : '';
        const result = registry.submitInput(runId, '/home/u/api');
        expect(result.status).toBe('written');
        expect(gateway.writtenLines).toEqual(['/home/u/api']);
    });
    it('rejects input for an unknown run id', () => {
        registry.start({ projectPath: null });
        const result = registry.submitInput('unknown-run', '/home/u/api');
        expect(result.status).toBe('no-active-run');
        expect(gateway.writtenLines).toEqual([]);
    });
    it('rejects input when no run has started', () => {
        const result = registry.submitInput('any', '/home/u/api');
        expect(result.status).toBe('no-active-run');
    });
    it('rejects input after the active run has exited', () => {
        const started = registry.start({ projectPath: null });
        const runId = started.status === 'started' ? started.runId : '';
        gateway.exit(0);
        const result = registry.submitInput(runId, '/home/u/api');
        expect(result.status).toBe('no-active-run');
        expect(gateway.writtenLines).toEqual([]);
    });
});
//# sourceMappingURL=streamSetupRun.usecase.test.js.map