"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 path = __importStar(require("path"));
const os = __importStar(require("os"));
const database_1 = require("../walls/06-memory/database/database");
const handoff_store_1 = require("../walls/06-memory/handoff/handoff-store");
function makeTmpDir() {
    const dir = path.join(os.tmpdir(), `test-project`);
    if (fs.existsSync(dir))
        fs.rmSync(dir, { recursive: true, force: true });
    fs.mkdirSync(dir, { recursive: true });
    return dir;
}
function cleanTmpDir(dir) {
    try {
        fs.rmSync(dir, { recursive: true, force: true });
    }
    catch { }
}
describe('Handoff Store', () => {
    let tmpDir;
    beforeAll(() => {
        (0, database_1.getDb)();
    });
    beforeEach(() => {
        tmpDir = makeTmpDir();
        const db = (0, database_1.getDb)();
        db.prepare('DELETE FROM handoff_entries WHERE project = ?').run('test-project');
    });
    afterEach(() => {
        cleanTmpDir(tmpDir);
    });
    it('saveHandoffEntry creates current.md', () => {
        (0, handoff_store_1.saveHandoffEntry)(tmpDir, {
            project: 'test-project',
            title: 'Test entry',
            summary: 'Did something important',
            status: 'done',
            filesChanged: ['src/foo.ts'],
        });
        const currentPath = path.join(tmpDir, '.cell', 'handoff', 'current.md');
        expect(fs.existsSync(currentPath)).toBe(true);
        const content = fs.readFileSync(currentPath, 'utf-8');
        expect(content).toContain('Test entry');
        expect(content).toContain('Did something important');
        expect(content).toContain('src/foo.ts');
    });
    it('getCurrentHandoff returns current content', () => {
        (0, handoff_store_1.saveHandoffEntry)(tmpDir, {
            project: 'test-project',
            title: 'Get test',
            summary: 'Summary text',
            status: 'done',
        });
        const content = (0, handoff_store_1.getCurrentHandoff)(tmpDir);
        expect(content).toContain('Get test');
        expect(content).toContain('Summary text');
    });
    it('getCurrentHandoff returns default when no entries', () => {
        const content = (0, handoff_store_1.getCurrentHandoff)(tmpDir);
        expect(content).toContain('No handoff entries yet');
    });
    it('getHistoryHandoff returns history', () => {
        const content = (0, handoff_store_1.getHistoryHandoff)(tmpDir);
        expect(typeof content).toBe('string');
    });
    it('getHandoffEntries returns DB entries', () => {
        (0, handoff_store_1.saveHandoffEntry)(tmpDir, {
            project: 'test-project',
            title: 'DB entry',
            summary: 'Test',
            status: 'done',
        });
        const entries = (0, handoff_store_1.getHandoffEntries)(tmpDir);
        expect(entries.length).toBeGreaterThanOrEqual(1);
        expect(entries[0].title).toBe('DB entry');
    });
    it('rotateHandoffIfNeeded returns false when no rotation needed', () => {
        (0, handoff_store_1.saveHandoffEntry)(tmpDir, {
            project: 'test-project',
            title: 'Small',
            summary: 'Small entry',
            status: 'done',
        });
        const rotated = (0, handoff_store_1.rotateHandoffIfNeeded)(tmpDir);
        expect(rotated).toBe(false);
    });
    it('getHandoffStats returns stats', () => {
        (0, handoff_store_1.saveHandoffEntry)(tmpDir, {
            project: 'test-project',
            title: 'Stats test',
            summary: 'Stats',
            status: 'done',
        });
        const stats = (0, handoff_store_1.getHandoffStats)(tmpDir);
        expect(stats.currentLines).toBeGreaterThan(0);
        expect(stats.dbEntries).toBeGreaterThanOrEqual(1);
        expect(stats.needsRotation).toBe(false);
    });
    it('saves to SQLite and file', () => {
        (0, handoff_store_1.saveHandoffEntry)(tmpDir, {
            project: 'test-project',
            title: 'Both stores',
            summary: 'SQLite + file',
            status: 'in-progress',
            why: 'Testing dual storage',
            nextSteps: 'Run tests',
        });
        const entries = (0, handoff_store_1.getHandoffEntries)(tmpDir);
        expect(entries.length).toBeGreaterThanOrEqual(1);
        expect(entries[0].why).toBe('Testing dual storage');
        expect(entries[0].next_steps || entries[0].nextSteps).toBe('Run tests');
        const content = (0, handoff_store_1.getCurrentHandoff)(tmpDir);
        expect(content).toContain('Both stores');
    });
});
//# sourceMappingURL=handoff-store.test.js.map