"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 database_1 = require("../walls/06-memory/database/database");
const memory_event_store_1 = require("../walls/06-memory/stores/memory-event-store");
const cell_mirror_1 = require("../walls/06-memory/mirror/cell-mirror");
const TEST_DIR = path.join(__dirname, '__test_cell_mirror__');
const TEST_PROJECT = 'mirror-test';
function cleanup() {
    try {
        fs.rmSync(TEST_DIR, { recursive: true, force: true });
    }
    catch { /* ignore */ }
}
describe('Cell Mirror', () => {
    beforeAll(() => {
        (0, database_1.getDb)();
        cleanup();
        fs.mkdirSync(TEST_DIR, { recursive: true });
    });
    afterEach(() => {
        const db = (0, database_1.getDb)();
        db.prepare('DELETE FROM memory_events WHERE project = ?').run(TEST_PROJECT);
        db.prepare('DELETE FROM memory_patterns WHERE project = ?').run(TEST_PROJECT);
        cleanup();
    });
    afterAll(cleanup);
    it('syncCellMirror creates base directory structure', () => {
        (0, cell_mirror_1.syncCellMirror)(TEST_DIR, TEST_PROJECT);
        expect(fs.existsSync(path.join(TEST_DIR, '.cell'))).toBe(true);
        expect(fs.existsSync(path.join(TEST_DIR, '.cell', 'memory', 'summaries', 'daily'))).toBe(true);
        expect(fs.existsSync(path.join(TEST_DIR, '.cell', 'memory', 'summaries', 'weekly'))).toBe(true);
        expect(fs.existsSync(path.join(TEST_DIR, '.cell', 'memory', 'summaries', 'monthly'))).toBe(true);
        expect(fs.existsSync(path.join(TEST_DIR, '.cell', 'memory', 'patterns'))).toBe(true);
        expect(fs.existsSync(path.join(TEST_DIR, '.cell', 'memory', 'style'))).toBe(true);
        expect(fs.existsSync(path.join(TEST_DIR, '.cell', 'memory', 'context'))).toBe(true);
        expect(fs.existsSync(path.join(TEST_DIR, '.cell', 'memory', 'archive'))).toBe(true);
        expect(fs.existsSync(path.join(TEST_DIR, '.cell', 'private'))).toBe(true);
    });
    it('syncPatternsToCell writes pattern files', () => {
        // Insert test patterns
        const db = (0, database_1.getDb)();
        db.prepare(`INSERT INTO memory_patterns (project, pattern, category, confidence, importance) VALUES (?, ?, ?, ?, ?)`).run(TEST_PROJECT, 'Use Startup folder', 'windows-autostart', 0.95, 9);
        db.prepare(`INSERT INTO memory_patterns (project, pattern, category, confidence, importance) VALUES (?, ?, ?, ?, ?)`).run(TEST_PROJECT, 'TypeScript errors fixed by restart', 'error-pattern', 0.8, 7);
        db.prepare(`INSERT INTO memory_patterns (project, pattern, category, confidence, importance) VALUES (?, ?, ?, ?, ?)`).run(TEST_PROJECT, 'Restart fixes type errors', 'fix-pattern', 0.85, 6);
        db.prepare(`INSERT INTO memory_patterns (project, pattern, category, confidence, importance) VALUES (?, ?, ?, ?, ?)`).run(TEST_PROJECT, 'Feature-based walls architecture', 'architecture', 0.9, 8);
        (0, cell_mirror_1.syncPatternsToCell)(TEST_DIR, TEST_PROJECT);
        const codePath = path.join(TEST_DIR, '.cell', 'memory', 'patterns', 'code-patterns.json');
        const errorPath = path.join(TEST_DIR, '.cell', 'memory', 'patterns', 'error-patterns.json');
        const fixPath = path.join(TEST_DIR, '.cell', 'memory', 'patterns', 'fix-patterns.json');
        const archPath = path.join(TEST_DIR, '.cell', 'memory', 'patterns', 'architecture-patterns.json');
        expect(fs.existsSync(codePath)).toBe(true);
        expect(fs.existsSync(errorPath)).toBe(true);
        expect(fs.existsSync(fixPath)).toBe(true);
        expect(fs.existsSync(archPath)).toBe(true);
        const codePatterns = JSON.parse(fs.readFileSync(codePath, 'utf-8'));
        expect(codePatterns.length).toBeGreaterThanOrEqual(1);
        const errorPatterns = JSON.parse(fs.readFileSync(errorPath, 'utf-8'));
        expect(errorPatterns.length).toBe(1);
        expect(errorPatterns[0].pattern).toContain('TypeScript');
    });
    it('syncContextToCell writes context files', () => {
        (0, memory_event_store_1.recordMemoryEvent)({ project: TEST_PROJECT, type: 'decision', summary: 'Use React', importance: 8, topic: 'frontend' });
        (0, memory_event_store_1.recordMemoryEvent)({ project: TEST_PROJECT, type: 'error', summary: 'Build failed', importance: 7 });
        (0, cell_mirror_1.syncContextToCell)(TEST_DIR, TEST_PROJECT);
        const shortPath = path.join(TEST_DIR, '.cell', 'memory', 'context', 'short.md');
        const fullPath = path.join(TEST_DIR, '.cell', 'memory', 'context', 'full.md');
        const lastSessionPath = path.join(TEST_DIR, '.cell', 'memory', 'context', 'last-session.md');
        expect(fs.existsSync(shortPath)).toBe(true);
        expect(fs.existsSync(fullPath)).toBe(true);
        expect(fs.existsSync(lastSessionPath)).toBe(true);
        const shortContent = fs.readFileSync(shortPath, 'utf-8');
        expect(shortContent).toContain('Short Context');
        expect(shortContent).toContain('Use React');
    });
    it('verifyCellHealth reports fresh status for empty project', () => {
        const health = (0, cell_mirror_1.verifyCellHealth)(TEST_DIR);
        expect(health.status).toBe('fresh');
        expect(health.totalFiles).toBe(0);
    });
    it('verifyCellHealth passes after sync', () => {
        (0, cell_mirror_1.syncCellMirror)(TEST_DIR, TEST_PROJECT);
        const health = (0, cell_mirror_1.verifyCellHealth)(TEST_DIR);
        expect(health.archiveDir).toBe(true);
        expect(health.privateDir).toBe(true);
    });
});
//# sourceMappingURL=cell-mirror.test.js.map