"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 fs = __importStar(require("fs"));
const os = __importStar(require("os"));
const path = __importStar(require("path"));
describe('Cell State (Phase 2)', () => {
    let testHome;
    let originalUserProfile;
    beforeAll(() => {
        originalUserProfile = process.env.USERPROFILE;
        testHome = path.join(os.tmpdir(), 'fivo-cell-state-' + Date.now());
        process.env.USERPROFILE = testHome;
    });
    afterAll(() => {
        process.env.USERPROFILE = originalUserProfile;
        if (fs.existsSync(testHome)) {
            try {
                fs.rmSync(testHome, { recursive: true, force: true });
            }
            catch { }
        }
    });
    describe('loadCellState / saveCellState', () => {
        it('returns empty object when no state file exists', async () => {
            const { loadCellState, saveCellState } = await Promise.resolve().then(() => __importStar(require('../walls/07-runtime/setup/cell-state')));
            const state = loadCellState();
            expect(state).toBeDefined();
            expect(typeof state).toBe('object');
        });
        it('saves and loads state correctly', async () => {
            const { saveCellState, loadCellState } = await Promise.resolve().then(() => __importStar(require('../walls/07-runtime/setup/cell-state')));
            saveCellState({ setup_completed: '2026-06-09T12:00:00Z' });
            const state = loadCellState();
            expect(state.setup_completed).toBe('2026-06-09T12:00:00Z');
        });
        it('merges updates into existing state', async () => {
            const { saveCellState, loadCellState } = await Promise.resolve().then(() => __importStar(require('../walls/07-runtime/setup/cell-state')));
            saveCellState({ setup_completed: '2026-06-09T12:00:00Z' });
            saveCellState({ first_scan_at: '2026-06-09T12:01:00Z' });
            const state = loadCellState();
            expect(state.setup_completed).toBe('2026-06-09T12:00:00Z');
            expect(state.first_scan_at).toBe('2026-06-09T12:01:00Z');
        });
    });
    describe('markSetupCompleted', () => {
        it('writes setup_completed timestamp', async () => {
            const { markSetupCompleted, loadCellState } = await Promise.resolve().then(() => __importStar(require('../walls/07-runtime/setup/cell-state')));
            markSetupCompleted();
            const state = loadCellState();
            expect(state.setup_completed).toBeDefined();
            expect(new Date(state.setup_completed).getTime()).not.toBeNaN();
        });
    });
    describe('markFirstScan', () => {
        it('writes first_scan_at only once', async () => {
            const { markFirstScan, loadCellState, saveCellState } = await Promise.resolve().then(() => __importStar(require('../walls/07-runtime/setup/cell-state')));
            saveCellState({});
            markFirstScan();
            const state1 = loadCellState();
            const first = state1.first_scan_at;
            expect(first).toBeDefined();
            markFirstScan();
            const state2 = loadCellState();
            expect(state2.first_scan_at).toBe(first);
            expect(state2.last_scan_at).toBeDefined();
        });
        it('updates last_scan_at every time', async () => {
            const { markFirstScan, loadCellState, saveCellState } = await Promise.resolve().then(() => __importStar(require('../walls/07-runtime/setup/cell-state')));
            saveCellState({});
            markFirstScan();
            const s1 = loadCellState();
            const first = s1.last_scan_at;
            await new Promise(r => setTimeout(r, 10));
            markFirstScan();
            const s2 = loadCellState();
            expect(s2.last_scan_at).not.toBe(first);
        });
    });
    describe('markFirstContext', () => {
        it('writes first_context_at once, updates last_context_at', async () => {
            const { markFirstContext, loadCellState, saveCellState } = await Promise.resolve().then(() => __importStar(require('../walls/07-runtime/setup/cell-state')));
            saveCellState({});
            markFirstContext();
            const s1 = loadCellState();
            expect(s1.first_context_at).toBeDefined();
            markFirstContext();
            const s2 = loadCellState();
            expect(s2.first_context_at).toBe(s1.first_context_at);
            expect(s2.last_context_at).toBeDefined();
        });
    });
    describe('markBridgeStarted', () => {
        it('writes bridge_started_at once', async () => {
            const { markBridgeStarted, loadCellState, saveCellState } = await Promise.resolve().then(() => __importStar(require('../walls/07-runtime/setup/cell-state')));
            saveCellState({});
            markBridgeStarted();
            const s1 = loadCellState();
            expect(s1.bridge_started_at).toBeDefined();
            const first = s1.bridge_started_at;
            markBridgeStarted();
            const s2 = loadCellState();
            expect(s2.bridge_started_at).toBe(first);
        });
    });
    describe('markDoctorRun', () => {
        it('writes doctor_run_at', async () => {
            const { markDoctorRun, loadCellState, saveCellState } = await Promise.resolve().then(() => __importStar(require('../walls/07-runtime/setup/cell-state')));
            saveCellState({});
            markDoctorRun();
            const state = loadCellState();
            expect(state.doctor_run_at).toBeDefined();
        });
    });
});
describe('Setup module (Phase 2)', () => {
    let originalUserProfile;
    beforeAll(() => {
        originalUserProfile = process.env.USERPROFILE;
    });
    afterAll(() => {
        process.env.USERPROFILE = originalUserProfile;
    });
    describe('detectProject', () => {
        it('uses package.json name when available', async () => {
            const { detectProject } = await Promise.resolve().then(() => __importStar(require('../walls/07-runtime/setup/setup')));
            const cwd = process.cwd();
            const result = detectProject(cwd);
            expect(result.name).toBeDefined();
            expect(result.source).toMatch(/package\.json|folder-basename/);
        });
        it('uses override when provided', async () => {
            const { detectProject } = await Promise.resolve().then(() => __importStar(require('../walls/07-runtime/setup/setup')));
            const result = detectProject('/some/path', 'my-project');
            expect(result.name).toBe('my-project');
            expect(result.source).toBe('override');
        });
    });
    describe('checkScanState', () => {
        it('returns done=true when .cell/personal/ has files', async () => {
            const { checkScanState } = await Promise.resolve().then(() => __importStar(require('../walls/07-runtime/setup/setup')));
            const cwd = process.cwd();
            const result = checkScanState(cwd);
            expect(result).toHaveProperty('done');
            expect(result).toHaveProperty('reason');
        });
    });
    describe('checkMcpState', () => {
        it('returns valid McpState', async () => {
            const { checkMcpState } = await Promise.resolve().then(() => __importStar(require('../walls/07-runtime/setup/setup')));
            const result = checkMcpState(os.homedir());
            expect(result).toHaveProperty('done');
            expect(result).toHaveProperty('total');
            expect(result).toHaveProperty('needsWrite');
        });
    });
});
//# sourceMappingURL=cell-state-setup.test.js.map