"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,
    };
}
function setupEmptyProject() {
    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'"))
                return fakeStmt([]);
            return fakeStmt([{ pattern_type: 'naming', pattern_value: 'camelCase', category: 'naming', count: 10, trust_score: 90 }]);
        }
        if (s.startsWith('SELECT * FROM SESSIONS'))
            return fakeStmt([]);
        if (s.startsWith('SELECT * FROM LIVE_EVENTS'))
            return fakeStmt([]);
        return fakeStmt([]);
    });
}
describe('projectNameOverride threaded through tool-specific formatters (v4.2.1)', () => {
    it('formatForAntigravity uses projectNameOverride when no session exists', () => {
        setupEmptyProject();
        const ctx = (0, prompt_builder_1.buildContext)('Uos', 'antigravity');
        const out = (0, prompt_builder_1.formatContextForTool)(ctx, 'antigravity', 'Uos');
        expect(out).toContain('project: Uos');
        expect(out).not.toContain('project: unknown');
    });
    it('formatForCursor uses projectNameOverride in @context line', () => {
        setupEmptyProject();
        const ctx = (0, prompt_builder_1.buildContext)('Uos', 'cursor');
        const out = (0, prompt_builder_1.formatContextForTool)(ctx, 'cursor', 'Uos');
        expect(out).toContain('@context Uos');
    });
    it('formatForWindsurf uses projectNameOverride in heading', () => {
        setupEmptyProject();
        const ctx = (0, prompt_builder_1.buildContext)('Uos', 'windsurf');
        const out = (0, prompt_builder_1.formatContextForTool)(ctx, 'windsurf', 'Uos');
        expect(out).toContain('# Cell Context — Uos');
    });
    it('formatForCopilot uses projectNameOverride in Project line', () => {
        setupEmptyProject();
        const ctx = (0, prompt_builder_1.buildContext)('Uos', 'copilot');
        const out = (0, prompt_builder_1.formatContextForTool)(ctx, 'copilot', 'Uos');
        expect(out).toContain('Project: Uos');
    });
    it('falls back to lastSession.project when no override', () => {
        const ts = new Date().toISOString();
        mockDb.prepare.mockReset();
        mockDb.prepare.mockImplementation((sql) => {
            const s = sql.trim().toUpperCase();
            if (s.startsWith('SELECT * FROM SESSIONS'))
                return fakeStmt([{
                        id: 5, tool_name: 'cursor', project: 'OldProject',
                        start_time: ts, end_time: ts,
                        files_touched: '[]', key_decisions: '[]', context_snapshot: 'snap',
                    }]);
            if (s.startsWith('SELECT * FROM DEVELOPER_PROFILES'))
                return fakeStmt([{ id: 1, project: 'OldProject', files_scanned: 100, total_lines: 5000, scanned_at: ts }]);
            if (s.startsWith('SELECT * FROM SCANS'))
                return fakeStmt([{ id: 1, project: 'OldProject', started_at: ts, completed_at: ts, files_scanned: 100 }]);
            if (s.startsWith('SELECT PATTERN_TYPE, PATTERN_VALUE, CATEGORY, COUNT, TRUST_SCORE FROM CODE_PATTERNS')) {
                return fakeStmt([{ pattern_type: 'naming', pattern_value: 'camelCase', category: 'naming', count: 10, trust_score: 90 }]);
            }
            return fakeStmt([]);
        });
        const ctx = (0, prompt_builder_1.buildContext)('OldProject', 'antigravity');
        const out = (0, prompt_builder_1.formatContextForTool)(ctx, 'antigravity', undefined);
        expect(out).toContain('project: OldProject');
    });
    it('override wins over lastSession.project when both exist', () => {
        const ts = new Date().toISOString();
        mockDb.prepare.mockReset();
        mockDb.prepare.mockImplementation((sql) => {
            const s = sql.trim().toUpperCase();
            if (s.startsWith('SELECT * FROM SESSIONS'))
                return fakeStmt([{
                        id: 5, tool_name: 'cursor', project: 'OldProject',
                        start_time: ts, end_time: ts,
                        files_touched: '[]', key_decisions: '[]', context_snapshot: 'snap',
                    }]);
            if (s.startsWith('SELECT * FROM DEVELOPER_PROFILES'))
                return fakeStmt([{ id: 1, project: 'OldProject', files_scanned: 100, total_lines: 5000, scanned_at: ts }]);
            if (s.startsWith('SELECT * FROM SCANS'))
                return fakeStmt([{ id: 1, project: 'OldProject', started_at: ts, completed_at: ts, files_scanned: 100 }]);
            if (s.startsWith('SELECT PATTERN_TYPE, PATTERN_VALUE, CATEGORY, COUNT, TRUST_SCORE FROM CODE_PATTERNS')) {
                return fakeStmt([{ pattern_type: 'naming', pattern_value: 'camelCase', category: 'naming', count: 10, trust_score: 90 }]);
            }
            return fakeStmt([]);
        });
        const ctx = (0, prompt_builder_1.buildContext)('OldProject', 'antigravity');
        const out = (0, prompt_builder_1.formatContextForTool)(ctx, 'antigravity', 'Uos');
        expect(out).toContain('project: Uos');
        expect(out).not.toContain('project: OldProject');
    });
});
//# sourceMappingURL=project-name-override.test.js.map