"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: () => rows, get: () => rows[0] || undefined };
}
describe('formatForAntigravity rich output (v4.2.4)', () => {
    function setupMocks(opts = {}) {
        const ts = new Date().toISOString();
        mockDb.prepare.mockReset();
        mockDb.prepare.mockImplementation((sql) => {
            const s = sql.trim().toUpperCase();
            if (s.startsWith('SELECT * FROM DEVELOPER_PROFILES')) {
                return fakeStmt([{ id: 1, project: 'Uos', files_scanned: 100, total_lines: 50000, scanned_at: ts }]);
            }
            if (s.startsWith('SELECT * FROM SCANS')) {
                return fakeStmt([{ id: 1, project: 'Uos', started_at: ts, completed_at: ts, files_scanned: 100 }]);
            }
            if (s.startsWith('SELECT PATTERN_TYPE, PATTERN_VALUE, CATEGORY, COUNT, TRUST_SCORE FROM CODE_PATTERNS')) {
                if (s.includes("CATEGORY = 'BLIND_SPOTS'") || s.includes("CATEGORY = 'BLIND_SPOTS'")) {
                    return fakeStmt(opts.blindSpots || []);
                }
                return fakeStmt([{ pattern_type: 'naming', pattern_value: 'camelCase', category: 'naming', count: 10, trust_score: 90 }]);
            }
            // Production readBlindSpots query (no CATEGORY column in SELECT)
            if (s.startsWith("SELECT PATTERN_TYPE, PATTERN_VALUE, COUNT, TRUST_SCORE FROM CODE_PATTERNS") || s.includes("FROM CODE_PATTERNS WHERE CATEGORY = 'BLIND_SPOTS'")) {
                return fakeStmt(opts.blindSpots || []);
            }
            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([]);
            if (s.startsWith('SELECT * FROM LIVE_EVENTS'))
                return fakeStmt(opts.liveEvents || []);
            if (s.includes('SESSION_APPROACHES') && s.includes('GROUP BY'))
                return fakeStmt([]);
            if (s.startsWith('SELECT FILE_PATH') && s.includes('SESSION_FILES')) {
                return fakeStmt(opts.mostTouched || []);
            }
            if (s.startsWith('SELECT * FROM SESSION_QUESTIONS'))
                return fakeStmt([]);
            return fakeStmt([]);
        });
    }
    it('includes blind_spots in YAML when present', () => {
        setupMocks({
            blindSpots: [
                { pattern_type: 'async_without_trycatch', pattern_value: '81/191', count: 81, trust_score: 95 },
                { pattern_type: 'missing_return_types', pattern_value: '176', count: 176, trust_score: 90 },
            ],
        });
        const ctx = (0, prompt_builder_1.buildContext)('Uos', 'antigravity');
        const out = (0, prompt_builder_1.formatContextForTool)(ctx, 'antigravity', 'Uos');
        expect(out).toContain('blind_spots:');
        expect(out).toContain('[81x');
    });
    it('includes last_live_file fallback when no session exists', () => {
        const eventTs = new Date(Date.now() - 60_000).toISOString();
        setupMocks({
            liveEvents: [{ id: 1, project: 'Uos', event_type: 'file_save', file_path: 'src/Zavi.tsx', created_at: eventTs }],
        });
        const ctx = (0, prompt_builder_1.buildContext)('Uos', 'antigravity');
        const out = (0, prompt_builder_1.formatContextForTool)(ctx, 'antigravity', 'Uos');
        expect(out).toContain('last_live_file:');
        expect(out).toContain('file_save');
        expect(out).toContain('src/Zavi.tsx');
        expect(out).toContain('no session open');
    });
    it('includes hot_files section when session_files data exists', () => {
        setupMocks({
            mostTouched: [
                { file_path: 'src/App.tsx', total_touches: 10, session_count: 3, last_touched: new Date().toISOString() },
                { file_path: 'server/index.ts', total_touches: 8, session_count: 2, last_touched: new Date().toISOString() },
            ],
        });
        const ctx = (0, prompt_builder_1.buildContext)('Uos', 'antigravity');
        const out = (0, prompt_builder_1.formatContextForTool)(ctx, 'antigravity', 'Uos');
        expect(out).toContain('hot_files:');
        expect(out).toContain('src/App.tsx');
        expect(out).toContain('touches: 10');
    });
    it('includes style summary line', () => {
        setupMocks();
        const ctx = (0, prompt_builder_1.buildContext)('Uos', 'antigravity');
        const out = (0, prompt_builder_1.formatContextForTool)(ctx, 'antigravity', 'Uos');
        expect(out).toMatch(/style: "/);
    });
    it('routes opencode tool name to formatForAntigravity (rich YAML)', () => {
        setupMocks();
        const ctx = (0, prompt_builder_1.buildContext)('Uos', 'opencode');
        const out = (0, prompt_builder_1.formatContextForTool)(ctx, 'opencode', 'Uos');
        expect(out).toContain('## Cell Context — Structured');
        expect(out).toContain('project: Uos');
    });
    it('routes gemini tool name to formatForAntigravity', () => {
        setupMocks();
        const ctx = (0, prompt_builder_1.buildContext)('Uos', 'gemini');
        const out = (0, prompt_builder_1.formatContextForTool)(ctx, 'gemini', 'Uos');
        expect(out).toContain('## Cell Context — Structured');
    });
});
//# sourceMappingURL=antigravity-rich-format.test.js.map