"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
const project_registry_1 = require("../core/project-registry");
const database_1 = require("../walls/06-memory/database/database");
const os = __importStar(require("os"));
const path = __importStar(require("path"));
const fs = __importStar(require("fs"));
describe('multi-project support (Unit 2.8)', () => {
    let reg;
    let originalHome;
    let tmpHome;
    beforeAll(() => {
        tmpHome = path.join(os.tmpdir(), 'fivo-cell-projects-' + Date.now());
        fs.mkdirSync(tmpHome, { recursive: true });
        originalHome = process.env.USERPROFILE;
        process.env.USERPROFILE = tmpHome;
        try {
            (0, database_1.closeDb)();
        }
        catch { /* ignore */ }
        reg = new project_registry_1.ProjectRegistry();
    });
    afterAll(() => {
        process.env.USERPROFILE = originalHome;
        try {
            (0, database_1.closeDb)();
        }
        catch { /* ignore */ }
        try {
            fs.rmSync(tmpHome, { recursive: true, force: true });
        }
        catch { /* ignore */ }
    });
    beforeEach(() => {
        try {
            (0, database_1.closeDb)();
        }
        catch { /* ignore */ }
        reg = new project_registry_1.ProjectRegistry();
        try {
            const db = (0, database_1.getDb)();
            db.exec('DELETE FROM projects;');
        }
        catch { /* ignore */ }
    });
    it('registerProject stores a new project with path and metadata', () => {
        const id = reg.registerProject({
            name: 'fivo-cell',
            path: 'C:\\Users\\dev\\projects\\fivo-cell',
            stack: 'TypeScript + Express + SQLite',
            fileCount: 297,
        });
        expect(id).toBeGreaterThan(0);
        const projects = reg.listProjects();
        expect(projects.length).toBe(1);
        expect(projects[0].name).toBe('fivo-cell');
        expect(projects[0].path).toContain('fivo-cell');
    });
    it('listProjects returns all registered projects', () => {
        reg.registerProject({ name: 'proj-a', path: '/home/dev/proj-a', stack: 'Python', fileCount: 50 });
        reg.registerProject({ name: 'proj-b', path: '/home/dev/proj-b', stack: 'TypeScript', fileCount: 100 });
        reg.registerProject({ name: 'proj-c', path: '/home/dev/proj-c', stack: 'Go', fileCount: 30 });
        const projects = reg.listProjects();
        expect(projects.length).toBe(3);
    });
    it('detectProjectFromCwd matches path suffix', () => {
        reg.registerProject({ name: 'fivo-cell', path: 'C:\\Users\\dev\\projects\\fivo-cell', stack: 'TS', fileCount: 297 });
        const detected = reg.detectProjectFromCwd('C:\\Users\\dev\\projects\\fivo-cell\\src');
        expect(detected).toBe('fivo-cell');
    });
    it('detectProjectFromCwd returns null for unregistered path', () => {
        reg.registerProject({ name: 'fivo-cell', path: 'C:\\dev\\fivo-cell', stack: 'TS', fileCount: 100 });
        const detected = reg.detectProjectFromCwd('C:\\Users\\other\\unrelated');
        expect(detected).toBeNull();
    });
    it('getProject returns full project metadata', () => {
        reg.registerProject({ name: 'fivo-cell', path: '/dev/fivo-cell', stack: 'TS+SQLite', fileCount: 297 });
        const project = reg.getProject('fivo-cell');
        expect(project).not.toBeNull();
        expect(project?.stack).toBe('TS+SQLite');
        expect(project?.fileCount).toBe(297);
    });
    it('getProject returns null for missing project', () => {
        const project = reg.getProject('nonexistent');
        expect(project).toBeNull();
    });
    it('updateLastScan updates lastScanAt timestamp and file count', async () => {
        reg.registerProject({ name: 'fivo-cell', path: '/dev/fc', stack: 'TS', fileCount: 100 });
        const before = reg.getProject('fivo-cell');
        const beforeTime = before?.lastScanAt;
        await new Promise(r => setTimeout(r, 10));
        reg.updateLastScan('fivo-cell', 350, 80000);
        const after = reg.getProject('fivo-cell');
        expect(after?.lastScanAt).toBeDefined();
        expect(after?.fileCount).toBe(350);
        expect(after?.lastScanAt).not.toBe(beforeTime);
    });
    it('removeProject deletes a project', () => {
        reg.registerProject({ name: 'to-remove', path: '/tmp/x', stack: 'X', fileCount: 0 });
        expect(reg.listProjects().length).toBe(1);
        reg.removeProject('to-remove');
        expect(reg.listProjects().length).toBe(0);
    });
    it('detectProjectFromCwd handles forward and back slashes', () => {
        reg.registerProject({ name: 'mixed', path: 'C:\\dev\\mixed-slashes\\project', stack: 'TS', fileCount: 50 });
        const detected1 = reg.detectProjectFromCwd('C:\\dev\\mixed-slashes\\project\\src');
        const detected2 = reg.detectProjectFromCwd('C:/dev/mixed-slashes/project/src');
        expect(detected1).toBe('mixed');
        expect(detected2).toBe('mixed');
    });
});
//# sourceMappingURL=multi-project.test.js.map