"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const prompt_builder_1 = require("../walls/01-context/context/prompt-builder");
const mockDb = {
    prepare: jest.fn(),
};
jest.mock('../walls/06-memory/database/database', () => ({
    getDb: () => mockDb,
}));
function fakeStmt(rows) {
    return {
        all: (..._args) => rows,
        get: (..._args) => rows[0] || undefined,
    };
}
describe('prompt-builder session data integration (Unit 2.3)', () => {
    beforeEach(() => {
        mockDb.prepare.mockReset();
        mockDb.prepare.mockImplementation((sql) => {
            const s = sql.trim().toUpperCase();
            if (s.startsWith('SELECT * FROM DEVELOPER_PROFILES')) {
                return fakeStmt([{ id: 1, project: 'fivo-cell', files_scanned: 100, total_lines: 5000, scanned_at: new Date().toISOString() }]);
            }
            if (s.startsWith('SELECT PATTERN_TYPE, PATTERN_VALUE, CATEGORY, COUNT, TRUST_SCORE FROM CODE_PATTERNS')) {
                if (s.includes("CATEGORY = 'BLIND_SPOTS'"))
                    return fakeStmt([]);
                return fakeStmt([{ pattern_type: 'naming', pattern_value: 'camelCase', category: 'naming', count: 10, trust_score: 90 }]);
            }
            if (s.startsWith('SELECT * FROM SCANS')) {
                return fakeStmt([{ id: 1, project: 'fivo-cell', started_at: new Date().toISOString(), completed_at: new Date().toISOString(), files_scanned: 100 }]);
            }
            if (s.startsWith('SELECT * FROM ERROR_LOG'))
                return fakeStmt([]);
            if (s.startsWith('SELECT PATTERN_VALUE FROM CODE_PATTERNS'))
                return fakeStmt([]);
            if (s.startsWith('SELECT * FROM SESSIONS')) {
                return fakeStmt([{
                        id: 5, tool_name: 'cursor', project: 'fivo-cell',
                        start_time: new Date(Date.now() - 7200000).toISOString(),
                        end_time: new Date().toISOString(),
                        files_touched: '["src/session-tracker.ts"]',
                        key_decisions: '["Phase 2 Usage Learning Engine"]',
                        context_snapshot: 'Working on Phase 2.3 wiring session data',
                    }]);
            }
            if (s.includes('SESSION_APPROACHES') && s.includes('GROUP BY')) {
                return fakeStmt([
                    { approach: 'chokidar for file watching', status: 'failed', count: 2, last_reason: 'too heavy', last_seen: new Date().toISOString() },
                    { approach: 'JSON files for session state', status: 'partial', count: 1, last_reason: 'concurrency issues', last_seen: new Date().toISOString() },
                ]);
            }
            if (s.startsWith('SELECT FILE_PATH') && s.includes('SESSION_FILES')) {
                return fakeStmt([
                    { file_path: 'src/session-tracker.ts', total_touches: 8, session_count: 3, last_touched: new Date().toISOString() },
                    { file_path: 'src/core/prompt-builder.ts', total_touches: 5, session_count: 2, last_touched: new Date().toISOString() },
                ]);
            }
            if (s.startsWith('SELECT * FROM SESSION_QUESTIONS')) {
                return fakeStmt([
                    { id: 1, session_id: 5, project: 'fivo-cell', question: 'Should session end after 30min inactivity?', priority: 'high', resolved: 0, answer: null, created_at: new Date().toISOString(), resolved_at: null },
                    { id: 2, session_id: 5, project: 'fivo-cell', question: 'How to detect tool switch?', priority: 'medium', resolved: 0, answer: null, created_at: new Date().toISOString(), resolved_at: null },
                ]);
            }
            return fakeStmt([]);
        });
    });
    it('context includes last session info (tool, project, recency)', () => {
        const ctx = (0, prompt_builder_1.buildContext)('fivo-cell', 'claude-code');
        expect(ctx.lastSession).toBeDefined();
        expect(ctx.lastSession?.toolName).toBe('cursor');
        expect(ctx.lastSession?.project).toBe('fivo-cell');
    });
    it('context includes tried approaches (anti-repetition list)', () => {
        const ctx = (0, prompt_builder_1.buildContext)('fivo-cell', 'claude-code');
        expect(ctx.triedApproaches).toBeDefined();
        expect(ctx.triedApproaches?.length).toBe(2);
        const chokidar = ctx.triedApproaches?.find(a => a.approach === 'chokidar for file watching');
        expect(chokidar?.status).toBe('failed');
        expect(chokidar?.count).toBe(2);
    });
    it('context includes most touched files', () => {
        const ctx = (0, prompt_builder_1.buildContext)('fivo-cell', 'claude-code');
        expect(ctx.mostTouchedFiles).toBeDefined();
        expect(ctx.mostTouchedFiles?.length).toBe(2);
        expect(ctx.mostTouchedFiles?.[0].filePath).toBe('src/session-tracker.ts');
        expect(ctx.mostTouchedFiles?.[0].touchCount).toBe(8);
    });
    it('context includes open questions', () => {
        const ctx = (0, prompt_builder_1.buildContext)('fivo-cell', 'claude-code');
        expect(ctx.openQuestions).toBeDefined();
        expect(ctx.openQuestions?.length).toBe(2);
        const q = ctx.openQuestions?.find(q => q.question.includes('30min'));
        expect(q?.priority).toBe('high');
    });
    it('formatContextForInjection includes "Tried already" section', () => {
        const ctx = (0, prompt_builder_1.buildContext)('fivo-cell', 'claude-code');
        const out = (0, prompt_builder_1.formatContextForInjection)(ctx, false);
        expect(out).toContain('TRIED ALREADY');
        expect(out).toContain('chokidar for file watching');
        expect(out).toContain('failed');
    });
    it('formatContextForInjection includes "Open questions" section', () => {
        const ctx = (0, prompt_builder_1.buildContext)('fivo-cell', 'claude-code');
        const out = (0, prompt_builder_1.formatContextForInjection)(ctx, false);
        expect(out).toContain('OPEN QUESTIONS');
        expect(out).toContain('30min');
        expect(out).toContain('tool switch');
    });
    it('formatContextForInjection includes "Last session" section', () => {
        const ctx = (0, prompt_builder_1.buildContext)('fivo-cell', 'claude-code');
        const out = (0, prompt_builder_1.formatContextForInjection)(ctx, false);
        expect(out).toContain('LAST SESSION');
        expect(out).toContain('cursor');
    });
    it('context handles missing session data gracefully', () => {
        mockDb.prepare.mockImplementation(() => fakeStmt([]));
        const ctx = (0, prompt_builder_1.buildContext)('unknown-project', 'claude-code');
        expect(ctx.lastSession).toBeUndefined();
        expect(ctx.triedApproaches).toEqual([]);
        expect(ctx.openQuestions).toEqual([]);
        expect(ctx.mostTouchedFiles).toEqual([]);
    });
});
//# sourceMappingURL=prompt-builder-sessions.test.js.map