"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const failure_memory_1 = require("../walls/03-knowledge/decisions/failure-memory");
const database_1 = require("../walls/06-memory/database/database");
beforeAll(() => {
    (0, database_1.initializeDatabase)();
});
beforeEach(() => {
    (0, failure_memory_1.clearFailures)();
});
function makePattern(id, desc, category = 'test') {
    return { patternId: id, description: desc, from: 'A', to: 'B', category, confidence: { distribution: 'beta', alpha: 1, beta: 1, mean: 0.5, ci95: [0.1, 0.9] }, conditions: [], signals: { positive: 0, negative: 0, total: 0, lastSignal: 0 }, attribution: { primaryCondition: 'test', conditionConfidence: 0.5, sourceFiles: [], note: '' }, scope: 'project', survivedDays: 0, meta: { language: 'typescript', subcategory: 'general', complexity: 'low', risk: 'low', source: 'test' }, lifecycle: { firstSeen: 0, lastSeen: 0, version: 1, history: [] }, quality: { score: 0.5, reuseRate: 0, successRate: 0, failureRate: 0, acceptanceRate: 0, daysSinceLastSignal: 0, age: 0 }, relationships: { related: [], conflicting: [], complementary: [], dependencies: [] }, ranking: { score: 0.5, rank: 1, factors: { confidence: 0.5, quality: 0.5, usage: 0, recency: 0, acceptance: 0 } } };
}
describe('Failure Memory — SQLite persistence', () => {
    it('recordFailure persists to SQLite', () => {
        const p = makePattern('p1', 'test pattern');
        (0, failure_memory_1.recordFailure)(p, true, 'typescript');
        (0, failure_memory_1.recordFailure)(p, true, 'typescript');
        (0, failure_memory_1.recordFailure)(p, false, 'typescript');
        const records = (0, failure_memory_1.getAllFailureRecords)();
        expect(records.length).toBe(1);
        expect(records[0].rejectCount).toBe(2);
        expect(records[0].acceptCount).toBe(1);
        expect(records[0].failureRate).toBeCloseTo(2 / 3);
    });
    it('getAvoidSuggestions returns patterns above threshold', () => {
        const p = makePattern('p2', 'bad pattern');
        for (let i = 0; i < 5; i++)
            (0, failure_memory_1.recordFailure)(p, true, 'ts');
        (0, failure_memory_1.recordFailure)(p, false, 'ts');
        const suggestions = (0, failure_memory_1.getAvoidSuggestions)();
        expect(suggestions.length).toBeGreaterThanOrEqual(1);
        const s = suggestions.find(s => s.patternId === 'p2');
        expect(s).toBeDefined();
        expect(s.failureRate).toBeGreaterThanOrEqual(0.5);
    });
    it('getAvoidSuggestions skips patterns with < 3 signals', () => {
        const p = makePattern('p3', 'low signal');
        (0, failure_memory_1.recordFailure)(p, true, 'ts');
        (0, failure_memory_1.recordFailure)(p, true, 'ts');
        const suggestions = (0, failure_memory_1.getAvoidSuggestions)();
        expect(suggestions.find(s => s.patternId === 'p3')).toBeUndefined();
    });
    it('getFailureStats returns correct counts', () => {
        const p1 = makePattern('p4', 'critical');
        const p2 = makePattern('p5', 'medium');
        for (let i = 0; i < 10; i++)
            (0, failure_memory_1.recordFailure)(p1, true, 'ts');
        (0, failure_memory_1.recordFailure)(p1, false, 'ts');
        for (let i = 0; i < 4; i++)
            (0, failure_memory_1.recordFailure)(p2, true, 'ts');
        (0, failure_memory_1.recordFailure)(p2, false, 'ts');
        const stats = (0, failure_memory_1.getFailureStats)();
        expect(stats.totalTracked).toBe(2);
        expect(stats.criticalCount).toBeGreaterThanOrEqual(1);
    });
    it('clearFailures empties all records', () => {
        const p = makePattern('p6', 'temp');
        (0, failure_memory_1.recordFailure)(p, true, 'ts');
        expect((0, failure_memory_1.getAllFailureRecords)().length).toBe(1);
        (0, failure_memory_1.clearFailures)();
        expect((0, failure_memory_1.getAllFailureRecords)().length).toBe(0);
    });
    it('getFailureRecord returns specific record', () => {
        const p = makePattern('p7', 'specific');
        (0, failure_memory_1.recordFailure)(p, true, 'ts');
        (0, failure_memory_1.recordFailure)(p, false, 'ts');
        const record = (0, failure_memory_1.getFailureRecord)('p7');
        expect(record).toBeDefined();
        expect(record.rejectCount).toBe(1);
        expect(record.acceptCount).toBe(1);
    });
    it('findAlternatives queries same-category patterns with low failure rate', () => {
        const good = makePattern('good1', 'good approach', 'errors');
        for (let i = 0; i < 5; i++)
            (0, failure_memory_1.recordFailure)(good, false, 'ts');
        (0, failure_memory_1.recordFailure)(good, true, 'ts');
        const bad = makePattern('bad1', 'bad approach', 'errors');
        for (let i = 0; i < 5; i++)
            (0, failure_memory_1.recordFailure)(bad, true, 'ts');
        const suggestions = (0, failure_memory_1.getAvoidSuggestions)(0.5);
        const badSuggestion = suggestions.find(s => s.patternId === 'bad1');
        expect(badSuggestion).toBeDefined();
    });
    it('data persists across re-reads (simulating restart)', () => {
        const p = makePattern('persist1', 'persistent');
        (0, failure_memory_1.recordFailure)(p, true, 'ts');
        (0, failure_memory_1.recordFailure)(p, true, 'ts');
        (0, failure_memory_1.recordFailure)(p, false, 'ts');
        const records = (0, failure_memory_1.getAllFailureRecords)();
        expect(records.length).toBe(1);
        expect(records[0].failureRate).toBeCloseTo(2 / 3);
        const recordAgain = (0, failure_memory_1.getAllFailureRecords)();
        expect(recordAgain.length).toBe(1);
        expect(recordAgain[0].failureRate).toBeCloseTo(2 / 3);
    });
    it('severity classification works', () => {
        const p = makePattern('sev-crit', 'critical issue');
        for (let i = 0; i < 10; i++)
            (0, failure_memory_1.recordFailure)(p, true, 'ts');
        (0, failure_memory_1.recordFailure)(p, false, 'ts');
        const suggestions = (0, failure_memory_1.getAvoidSuggestions)(0.5);
        const s = suggestions.find(s => s.patternId === 'sev-crit');
        expect(s).toBeDefined();
        expect(s.severity).toBe('critical');
    });
});
//# sourceMappingURL=failure-memory.test.js.map