import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import fs from 'node:fs';
import path from 'node:path';
import os from 'node:os';
import { createFileWatcher } from '../file-watcher.js';
describe('file-watcher squad.yaml', () => {
    let squadsDir;
    beforeEach(() => {
        squadsDir = fs.mkdtempSync(path.join(os.tmpdir(), 'watcher-test-'));
    });
    afterEach(() => {
        fs.rmSync(squadsDir, { recursive: true, force: true });
    });
    it('should emit squad:config_changed when squad.yaml is modified', async () => {
        const squadDir = path.join(squadsDir, 'test-squad');
        fs.mkdirSync(squadDir, { recursive: true });
        fs.writeFileSync(path.join(squadDir, 'squad.yaml'), 'squad:\n  code: test\n');
        const events = [];
        const watcher = createFileWatcher(squadsDir, (event) => {
            events.push(event);
        });
        await watcher.ready;
        // Modify squad.yaml
        fs.writeFileSync(path.join(squadDir, 'squad.yaml'), 'squad:\n  code: test\n  updated: true\n');
        // Wait for debounce + chokidar
        await new Promise((r) => setTimeout(r, 200));
        watcher.stop();
        const configEvents = events.filter((e) => e.type === 'squad:config_changed');
        expect(configEvents.length).toBeGreaterThanOrEqual(1);
        expect(configEvents[0].squadName).toBe('test-squad');
    });
    it('emits a2ui:update when ui.json is added in squad dir', async () => {
        const squadDir = path.join(squadsDir, 'my-squad');
        fs.mkdirSync(squadDir);
        const events = [];
        const watcher = createFileWatcher(squadsDir, (e) => events.push(e));
        await watcher.ready;
        const components = [{ type: 'metric', label: 'Score', value: 42 }];
        fs.writeFileSync(path.join(squadDir, 'ui.json'), JSON.stringify({ components }));
        await vi.waitFor(() => {
            expect(events).toContainEqual({
                type: 'a2ui:update',
                squadName: 'my-squad',
                components,
            });
        }, { timeout: 2000 });
        watcher.stop();
    });
    it('should detect state.json changes in a 4-level nested squad directory', async () => {
        const nestedDir = path.join(squadsDir, 'marketing', 'redes-sociais', 'instagram', 'publicador');
        fs.mkdirSync(nestedDir, { recursive: true });
        const stateFile = path.join(nestedDir, 'state.json');
        fs.writeFileSync(stateFile, JSON.stringify({
            squad: 'publicador',
            status: 'idle',
            step: { current: 0, total: 1, label: '' },
            agents: [],
            handoff: null,
            startedAt: null,
            updatedAt: new Date().toISOString(),
        }));
        const events = [];
        const watcher = createFileWatcher(squadsDir, (event) => events.push(event));
        await watcher.ready;
        fs.writeFileSync(stateFile, JSON.stringify({
            squad: 'publicador',
            status: 'running',
            step: { current: 1, total: 1, label: 'Working' },
            agents: [],
            handoff: null,
            startedAt: new Date().toISOString(),
            updatedAt: new Date().toISOString(),
        }));
        await new Promise((r) => setTimeout(r, 300));
        watcher.stop();
        const stateEvents = events.filter((e) => e.type !== 'squad:inactive');
        expect(stateEvents.length).toBeGreaterThanOrEqual(1);
        expect(stateEvents.some((e) => e.squadName === 'publicador')).toBe(true);
    });
});
//# sourceMappingURL=file-watcher.test.js.map