"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 session_memory_1 = require("../walls/01-context/sessions/session-memory");
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('session memory normalized tables (Unit 2.2)', () => {
    let mem;
    let originalHome;
    let tmpHome;
    beforeAll(() => {
        tmpHome = path.join(os.tmpdir(), 'fivo-cell-test-' + Date.now());
        fs.mkdirSync(tmpHome, { recursive: true });
        originalHome = process.env.USERPROFILE;
        process.env.USERPROFILE = tmpHome;
        try {
            (0, database_1.closeDb)();
        }
        catch { /* ignore */ }
        mem = new session_memory_1.SessionMemory();
    });
    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 */ }
        mem = new session_memory_1.SessionMemory();
        try {
            const db = (0, database_1.getDb)();
            db.exec('DELETE FROM sessions; DELETE FROM session_approaches; DELETE FROM session_files; DELETE FROM session_questions;');
        }
        catch { /* ignore */ }
    });
    it('recordApproach persists an approach with status and outcome', () => {
        const sessionId = mem.startSession('claude-code', 'fivo-cell');
        const approachId = mem.recordApproach({
            sessionId,
            project: 'fivo-cell',
            approach: 'chokidar for file watching',
            status: 'failed',
            reason: 'too heavy, battery drain',
        });
        expect(approachId).toBeGreaterThan(0);
        const approaches = mem.getSessionApproaches(sessionId);
        expect(approaches.length).toBe(1);
        expect(approaches[0].approach).toBe('chokidar for file watching');
        expect(approaches[0].status).toBe('failed');
    });
    it('getRecentApproaches aggregates across all sessions (anti-repetition list)', () => {
        const sessionId1 = mem.startSession('claude-code', 'fivo-cell');
        mem.recordApproach({ sessionId: sessionId1, project: 'fivo-cell', approach: 'chokidar for file watching', status: 'failed' });
        mem.recordApproach({ sessionId: sessionId1, project: 'fivo-cell', approach: 'JSON files for session state', status: 'partial' });
        const sessionId2 = mem.startSession('cursor', 'fivo-cell');
        mem.recordApproach({ sessionId: sessionId2, project: 'fivo-cell', approach: 'chokidar for file watching', status: 'failed' });
        const recent = mem.getRecentApproaches('fivo-cell', 10);
        expect(recent.length).toBeGreaterThanOrEqual(2);
        const chokidar = recent.find(a => a.approach === 'chokidar for file watching');
        expect(chokidar).toBeDefined();
        expect(chokidar?.count).toBeGreaterThanOrEqual(2);
    });
    it('recordFileTouch persists file activity for a session', () => {
        const sessionId = mem.startSession('cursor', 'fivo-cell');
        mem.recordFileTouch({ sessionId, project: 'fivo-cell', filePath: 'src/session-memory.ts' });
        mem.recordFileTouch({ sessionId, project: 'fivo-cell', filePath: 'src/core/database.ts' });
        mem.recordFileTouch({ sessionId, project: 'fivo-cell', filePath: 'src/session-memory.ts' });
        const files = mem.getSessionFiles(sessionId);
        expect(files.length).toBe(2);
        const mem2 = files.find(f => f.filePath === 'src/session-memory.ts');
        expect(mem2?.touchCount).toBe(2);
    });
    it('getMostTouchedFiles returns top files across sessions', () => {
        const sId = mem.startSession('claude-code', 'fivo-cell');
        mem.recordFileTouch({ sessionId: sId, project: 'fivo-cell', filePath: 'src/core/prompt-builder.ts' });
        mem.recordFileTouch({ sessionId: sId, project: 'fivo-cell', filePath: 'src/core/prompt-builder.ts' });
        mem.recordFileTouch({ sessionId: sId, project: 'fivo-cell', filePath: 'src/core/prompt-builder.ts' });
        const top = mem.getMostTouchedFiles('fivo-cell', 5);
        expect(top.length).toBeGreaterThan(0);
        const builder = top.find(f => f.filePath === 'src/core/prompt-builder.ts');
        expect(builder).toBeDefined();
        expect(builder?.touchCount).toBeGreaterThanOrEqual(3);
    });
    it('recordQuestion persists an open question with priority', () => {
        const sessionId = mem.startSession('claude-code', 'fivo-cell');
        const qId = mem.recordQuestion({
            sessionId,
            project: 'fivo-cell',
            question: 'Should session end after 30min inactivity or explicit close?',
            priority: 'high',
        });
        expect(qId).toBeGreaterThan(0);
        const questions = mem.getOpenQuestions('fivo-cell');
        expect(questions.length).toBeGreaterThan(0);
        const found = questions.find(q => q.id === qId);
        expect(found?.question).toContain('30min');
        expect(found?.resolved).toBe(false);
    });
    it('resolveQuestion marks a question as answered', () => {
        const sessionId = mem.startSession('claude-code', 'fivo-cell');
        const qId = mem.recordQuestion({
            sessionId,
            project: 'fivo-cell',
            question: 'How to detect tool switch (Cursor → Claude Code)?',
            priority: 'medium',
        });
        mem.resolveQuestion(qId, 'Compare tool_name on new session start with previous session end');
        const questions = mem.getOpenQuestions('fivo-cell');
        const found = questions.find(q => q.id === qId);
        expect(found).toBeUndefined();
    });
    it('SessionMemory class still works end-to-end (regression)', () => {
        const sessionId = mem.startSession('windsurf', 'fivo-cell');
        mem.recordFileTouch({ sessionId, project: 'fivo-cell', filePath: 'src/foo.ts' });
        mem.recordApproach({ sessionId, project: 'fivo-cell', approach: 'use better-sqlite3', status: 'succeeded' });
        mem.recordQuestion({ sessionId, project: 'fivo-cell', question: 'which migration approach?' });
        mem.endSession(sessionId, {
            filesTouched: ['src/foo.ts'],
            keyDecisions: ['better-sqlite3 over sql.js'],
            contextSnapshot: 'Test context',
        });
        const recent = mem.getRecentSessions(5);
        expect(recent.length).toBeGreaterThan(0);
    });
});
//# sourceMappingURL=session-memory-tables.test.js.map