"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const prompt_builder_1 = require("../walls/01-context/context/prompt-builder");
const prompt_builder_2 = 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 setupMocks(opts = {}) {
    const profileScannedAt = opts.profileScannedAt || new Date().toISOString();
    const scanCompletedAt = opts.scanCompletedAt || new Date().toISOString();
    const lastSessionEnd = opts.lastSessionEnd || 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: 'fivo-cell', files_scanned: 100, total_lines: 5000, scanned_at: profileScannedAt }]);
        }
        if (s.startsWith('SELECT * FROM SCANS')) {
            return fakeStmt([{ id: 1, project: 'fivo-cell', started_at: scanCompletedAt, completed_at: scanCompletedAt, files_scanned: 100 }]);
        }
        if (s.includes('SESSION_APPROACHES') && s.includes('GROUP BY'))
            return fakeStmt([]);
        if (s.startsWith('SELECT FILE_PATH') && s.includes('SESSION_FILES'))
            return fakeStmt([]);
        if (s.startsWith('SELECT * FROM SESSION_QUESTIONS'))
            return fakeStmt([]);
        if (s.startsWith('SELECT * FROM SESSIONS')) {
            return fakeStmt([{ id: 1, tool_name: 'cursor', project: 'fivo-cell', start_time: lastSessionEnd, end_time: lastSessionEnd, files_touched: '[]', key_decisions: '[]', context_snapshot: '' }]);
        }
        return fakeStmt([]);
    });
}
describe('staleness detection (Unit 2.4)', () => {
    it('getStalenessStatus returns "fresh" for data < 1 day old', () => {
        expect((0, prompt_builder_2.getStalenessStatus)(new Date().toISOString(), 1)).toBe('fresh');
    });
    it('getStalenessStatus returns "fresh" for data < 7 days old (default)', () => {
        const threeDaysAgo = new Date(Date.now() - 3 * 24 * 3600 * 1000).toISOString();
        expect((0, prompt_builder_2.getStalenessStatus)(threeDaysAgo)).toBe('fresh');
    });
    it('getStalenessStatus returns "stale" for data 7-30 days old', () => {
        const fifteenDaysAgo = new Date(Date.now() - 15 * 24 * 3600 * 1000).toISOString();
        expect((0, prompt_builder_2.getStalenessStatus)(fifteenDaysAgo)).toBe('stale');
    });
    it('getStalenessStatus returns "expired" for data > 30 days old', () => {
        const sixtyDaysAgo = new Date(Date.now() - 60 * 24 * 3600 * 1000).toISOString();
        expect((0, prompt_builder_2.getStalenessStatus)(sixtyDaysAgo)).toBe('expired');
    });
    it('getStalenessStatus returns "unknown" for missing data', () => {
        expect((0, prompt_builder_2.getStalenessStatus)(undefined)).toBe('unknown');
        expect((0, prompt_builder_2.getStalenessStatus)('')).toBe('unknown');
    });
    it('getStalenessStatus returns "unknown" for invalid dates', () => {
        expect((0, prompt_builder_2.getStalenessStatus)('not-a-date')).toBe('unknown');
    });
    it('context includes staleness info for fresh data', () => {
        setupMocks({ profileScannedAt: new Date().toISOString() });
        const ctx = (0, prompt_builder_1.buildContext)('fivo-cell', 'claude-code');
        expect(ctx.staleness).toBeDefined();
        expect(ctx.staleness?.profile).toBe('fresh');
        expect(ctx.staleness?.shouldRescan).toBe(false);
    });
    it('context includes staleness warning for stale data', () => {
        const tenDaysAgo = new Date(Date.now() - 10 * 24 * 3600 * 1000).toISOString();
        setupMocks({ profileScannedAt: tenDaysAgo });
        const ctx = (0, prompt_builder_1.buildContext)('fivo-cell', 'claude-code');
        expect(ctx.staleness?.profile).toBe('stale');
        expect(ctx.staleness?.shouldRescan).toBe(true);
    });
    it('context includes staleness critical for expired data', () => {
        const sixtyDaysAgo = new Date(Date.now() - 60 * 24 * 3600 * 1000).toISOString();
        setupMocks({ profileScannedAt: sixtyDaysAgo });
        const ctx = (0, prompt_builder_1.buildContext)('fivo-cell', 'claude-code');
        expect(ctx.staleness?.profile).toBe('expired');
        expect(ctx.staleness?.shouldRescan).toBe(true);
    });
    it('formatContextForInjection shows staleness warning when data is stale', () => {
        const tenDaysAgo = new Date(Date.now() - 10 * 24 * 3600 * 1000).toISOString();
        setupMocks({ profileScannedAt: tenDaysAgo });
        const ctx = (0, prompt_builder_1.buildContext)('fivo-cell', 'claude-code');
        const out = (0, prompt_builder_1.formatContextForInjection)(ctx, false);
        expect(out).toMatch(/STALE|stale|rescan|RUN `cell scan`|re-scan/i);
    });
    it('formatContextForInjection does NOT show staleness warning for fresh data', () => {
        setupMocks({ profileScannedAt: new Date().toISOString() });
        const ctx = (0, prompt_builder_1.buildContext)('fivo-cell', 'claude-code');
        const out = (0, prompt_builder_1.formatContextForInjection)(ctx, false);
        expect(out).not.toMatch(/stale|expired|rescan/i);
    });
    it('staleness threshold is configurable (default 7 days)', () => {
        const fiveDaysAgo = new Date(Date.now() - 5 * 24 * 3600 * 1000).toISOString();
        expect((0, prompt_builder_2.getStalenessStatus)(fiveDaysAgo, 7)).toBe('fresh');
        expect((0, prompt_builder_2.getStalenessStatus)(fiveDaysAgo, 3)).toBe('stale');
    });
});
//# sourceMappingURL=staleness-detection.test.js.map