import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import { mkdirSync, writeFileSync, rmSync, mkdtempSync } from 'node:fs';
import { join } from 'node:path';
import { tmpdir } from 'node:os';
import { scanInstalledSquads } from '../outdated.js';
describe('scanInstalledSquads', () => {
    let tempDir;
    beforeEach(() => {
        tempDir = mkdtempSync(join(tmpdir(), 'squads-test-'));
    });
    afterEach(() => {
        rmSync(tempDir, { recursive: true, force: true });
    });
    it('returns empty array when directory does not exist', () => {
        const result = scanInstalledSquads('/nonexistent/path');
        expect(result).toEqual([]);
    });
    it('returns empty array when no @scope directories exist', () => {
        mkdirSync(join(tempDir, 'local-squad'), { recursive: true });
        writeFileSync(join(tempDir, 'local-squad', 'squad.yaml'), 'squad:\n  version: "1.0.0"');
        const result = scanInstalledSquads(tempDir);
        expect(result).toEqual([]);
    });
    it('finds squads under @scope directories', () => {
        const squadDir = join(tempDir, '@thulio', 'brand-squad');
        mkdirSync(squadDir, { recursive: true });
        writeFileSync(join(squadDir, 'squad.yaml'), 'squad:\n  code: brand-squad\n  version: "1.2.0"');
        const result = scanInstalledSquads(tempDir);
        expect(result).toEqual([
            { fullName: '@thulio/brand-squad', currentVersion: '1.2.0' },
        ]);
    });
    it('skips squads without squad.yaml', () => {
        const squadDir = join(tempDir, '@thulio', 'no-yaml');
        mkdirSync(squadDir, { recursive: true });
        const result = scanInstalledSquads(tempDir);
        expect(result).toEqual([]);
    });
    it('skips squads with invalid yaml', () => {
        const squadDir = join(tempDir, '@thulio', 'bad-yaml');
        mkdirSync(squadDir, { recursive: true });
        writeFileSync(join(squadDir, 'squad.yaml'), '}{invalid');
        const result = scanInstalledSquads(tempDir);
        expect(result).toEqual([]);
    });
    it('skips squads without version field', () => {
        const squadDir = join(tempDir, '@thulio', 'no-version');
        mkdirSync(squadDir, { recursive: true });
        writeFileSync(join(squadDir, 'squad.yaml'), 'squad:\n  code: no-version');
        const result = scanInstalledSquads(tempDir);
        expect(result).toEqual([]);
    });
    it('finds squads with nested tarball structure (@scope/name/name/squad.yaml)', () => {
        const nestedDir = join(tempDir, '@community', 'copy-squad', 'copy-squad');
        mkdirSync(nestedDir, { recursive: true });
        writeFileSync(join(nestedDir, 'squad.yaml'), 'squad:\n  code: copy-squad\n  version: "2.0.0"');
        const result = scanInstalledSquads(tempDir);
        expect(result).toEqual([
            { fullName: '@community/copy-squad', currentVersion: '2.0.0' },
        ]);
    });
    it('finds multiple squads across scopes', () => {
        const squad1 = join(tempDir, '@thulio', 'squad-a');
        const squad2 = join(tempDir, '@community', 'squad-b');
        mkdirSync(squad1, { recursive: true });
        mkdirSync(squad2, { recursive: true });
        writeFileSync(join(squad1, 'squad.yaml'), 'squad:\n  version: "1.0.0"');
        writeFileSync(join(squad2, 'squad.yaml'), 'squad:\n  version: "2.0.0"');
        const result = scanInstalledSquads(tempDir);
        expect(result).toHaveLength(2);
        expect(result).toEqual(expect.arrayContaining([
            { fullName: '@thulio/squad-a', currentVersion: '1.0.0' },
            { fullName: '@community/squad-b', currentVersion: '2.0.0' },
        ]));
    });
});
