"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 real data wiring (Unit 2.1.1)', () => {
    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',
                        naming_style: 'camelCase', quote_style: 'single', semicolon_style: 'always',
                        indent_style: '2-spaces', async_style: 'uses-async', error_handling: 'try-catch',
                        test_pattern: 'jest', top_patterns: '["style:single","naming:camelCase","async:await"]',
                        strengths: '["Uses async/await"]', improvements: '["Add error handling"]',
                        files_scanned: 100, total_lines: 5000,
                    }]);
            }
            if (s.startsWith('SELECT PATTERN_TYPE, PATTERN_VALUE, CATEGORY, COUNT, TRUST_SCORE FROM CODE_PATTERNS')) {
                if (s.includes("CATEGORY = 'BLIND_SPOTS'")) {
                    return fakeStmt([
                        { pattern_type: 'blind_spot_async_notry', pattern_value: 'async function missing try/catch', category: 'blind_spots', count: 14, trust_score: 88 },
                        { pattern_type: 'blind_spot_null_check', pattern_value: 'API response without null check', category: 'blind_spots', count: 7, trust_score: 80 },
                    ]);
                }
                return fakeStmt([
                    { pattern_type: 'naming_convention', pattern_value: 'camelCase', category: 'naming', count: 50, trust_score: 92 },
                    { pattern_type: 'quote_style', pattern_value: 'single', category: 'style', count: 40, 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([{ id: 1, project: 'fivo-cell', file_path: 'src/foo.ts', error_type: 'TypeError', error_message: 'cannot read property of undefined', line: 42, created_at: new Date().toISOString() }]);
            }
            if (s.startsWith('SELECT PATTERN_VALUE FROM CODE_PATTERNS')) {
                return fakeStmt([]);
            }
            return fakeStmt([]);
        });
    });
    it('buildContext reads developer_profiles (not user_profile)', () => {
        const ctx = (0, prompt_builder_1.buildContext)('fivo-cell', 'claude-code');
        expect(ctx.whoYouAre).toContain('fivo-cell');
        expect(ctx.whoYouAre).not.toContain('run `cell scan` to build profile');
    });
    it('buildContext reads code_patterns (not patterns) for top patterns', () => {
        const ctx = (0, prompt_builder_1.buildContext)('fivo-cell', 'claude-code');
        expect(ctx.yourStyle).toContain('camelCase');
        expect(ctx.yourStyle).toContain('single');
    });
    it('buildContext reads code_patterns blind_spots (not behavior_events)', () => {
        const ctx = (0, prompt_builder_1.buildContext)('fivo-cell', 'claude-code');
        expect(ctx.watchOut).toContain('blind spot');
        expect(ctx.watchOut).not.toContain('Run `cell scan` to detect blind spots');
    });
    it('buildContext returns non-empty whoYouAre from real profile data', () => {
        const ctx = (0, prompt_builder_1.buildContext)('fivo-cell', 'claude-code');
        expect(ctx.whoYouAre.length).toBeGreaterThan(20);
        expect(ctx.whoYouAre).toContain('fivo-cell');
    });
    it('formatContextForInjection returns full context block (not generic placeholder)', () => {
        const ctx = (0, prompt_builder_1.buildContext)('fivo-cell', 'claude-code');
        const out = (0, prompt_builder_1.formatContextForInjection)(ctx, false);
        expect(out).toContain('CELL CONTEXT');
        expect(out).toContain('WHO YOU ARE');
        expect(out).toContain('fivo-cell');
        expect(out).toContain('camelCase');
        expect(out).toContain('blind spot');
    });
    it('Phase 1: context shows top current risk and recommended next fix', () => {
        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',
                        naming_style: 'camelCase', quote_style: 'single', semicolon_style: 'always',
                        indent_style: '2-spaces', async_style: 'uses-async', error_handling: 'try-catch',
                        test_pattern: 'jest', top_patterns: '[]', strengths: '[]', improvements: '[]',
                        files_scanned: 100, total_lines: 5000,
                    }]);
            }
            if (s.includes("CATEGORY = 'BLIND_SPOTS'")) {
                return fakeStmt([
                    { pattern_type: 'blind_spot_async_notry', pattern_value: 'async function missing try/catch', category: 'blind_spots', count: 14, trust_score: 88 },
                ]);
            }
            if (s.startsWith('SELECT PATTERN_TYPE, PATTERN_VALUE, CATEGORY, COUNT, TRUST_SCORE FROM CODE_PATTERNS')) {
                return fakeStmt([]);
            }
            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 }]);
            }
            return fakeStmt([]);
        });
        const ctx = (0, prompt_builder_1.buildContext)('fivo-cell', 'claude-code');
        expect(ctx.watchOut).toContain('Top current risk:');
        expect(ctx.watchOut).toContain('Recommended next fix:');
    });
    it('Phase 1: context top risk matches first blind spot', () => {
        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',
                        naming_style: 'camelCase', quote_style: 'single', semicolon_style: 'always',
                        indent_style: '2-spaces', async_style: 'uses-async', error_handling: 'try-catch',
                        test_pattern: 'jest', top_patterns: '[]', strengths: '[]', improvements: '[]',
                        files_scanned: 100, total_lines: 5000,
                    }]);
            }
            if (s.includes("CATEGORY = 'BLIND_SPOTS'")) {
                return fakeStmt([
                    { pattern_type: 'blind_spot_async_notry', pattern_value: 'async function missing try/catch', category: 'blind_spots', count: 14, trust_score: 88 },
                ]);
            }
            if (s.startsWith('SELECT PATTERN_TYPE, PATTERN_VALUE, CATEGORY, COUNT, TRUST_SCORE FROM CODE_PATTERNS')) {
                return fakeStmt([]);
            }
            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 }]);
            }
            return fakeStmt([]);
        });
        const ctx = (0, prompt_builder_1.buildContext)('fivo-cell', 'claude-code');
        expect(ctx.watchOut).toContain('Top current risk: async function missing try/catch');
    });
    it('Phase 1: recommended fix for async_notry is try/catch', () => {
        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',
                        naming_style: 'camelCase', quote_style: 'single', semicolon_style: 'always',
                        indent_style: '2-spaces', async_style: 'uses-async', error_handling: 'try-catch',
                        test_pattern: 'jest', top_patterns: '[]', strengths: '[]', improvements: '[]',
                        files_scanned: 100, total_lines: 5000,
                    }]);
            }
            if (s.includes("CATEGORY = 'BLIND_SPOTS'")) {
                return fakeStmt([
                    { pattern_type: 'blind_spot_async_notry', pattern_value: 'async function missing try/catch', category: 'blind_spots', count: 14, trust_score: 88 },
                ]);
            }
            if (s.startsWith('SELECT PATTERN_TYPE, PATTERN_VALUE, CATEGORY, COUNT, TRUST_SCORE FROM CODE_PATTERNS')) {
                return fakeStmt([]);
            }
            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 }]);
            }
            return fakeStmt([]);
        });
        const ctx = (0, prompt_builder_1.buildContext)('fivo-cell', 'claude-code');
        expect(ctx.watchOut).toContain('Recommended next fix: add try/catch to async paths');
    });
    it('buildContext handles missing data gracefully (no crash)', () => {
        mockDb.prepare.mockImplementation(() => fakeStmt([]));
        const ctx = (0, prompt_builder_1.buildContext)('unknown-project', 'cursor');
        expect(ctx.whoYouAre).toBeDefined();
        expect(ctx.rightNow).toBeDefined();
        expect(ctx.yourStyle).toBeDefined();
        expect(ctx.watchOut).toBeDefined();
        expect(ctx.predictions).toBeDefined();
    });
});
//# sourceMappingURL=prompt-builder-realdata.test.js.map