"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
// Re-initialize module state by importing fresh
// We test the exported functions directly
describe('FailureMemory', () => {
    let module;
    beforeEach(async () => {
        module = await Promise.resolve().then(() => __importStar(require('../../walls/03-knowledge/decisions/failure-memory')));
        module.clearFailures();
    });
    const makePattern = (id, desc, from, to, category = 'test') => ({
        patternId: id,
        category,
        description: desc,
        from,
        to,
        conditions: [],
        confidence: { distribution: 'beta', alpha: 1, beta: 1, mean: 0.5, ci95: [0.1, 0.9] },
        signals: { positive: 0, negative: 0, total: 0, lastSignal: Date.now() },
        attribution: { primaryCondition: '', conditionConfidence: 0, sourceFiles: [], note: '' },
        scope: 'global',
        survivedDays: 0,
        meta: { language: 'unknown', subcategory: 'general', complexity: 'low', risk: 'medium', source: 'manual' },
        lifecycle: { firstSeen: Date.now(), lastSeen: Date.now(), version: 1, history: [] },
        quality: { score: 50, reuseRate: 0, successRate: 0, failureRate: 0, acceptanceRate: 0, daysSinceLastSignal: 0, age: 0 },
        relationships: { related: [], conflicting: [], complementary: [], dependencies: [] },
        ranking: { score: 0, rank: 0, factors: { confidence: 0, quality: 0, usage: 0, recency: 0, acceptance: 0 } },
    });
    test('records a failure', () => {
        const p = makePattern('p1', 'uses var', 'var', 'const', 'variable_declaration');
        const record = module.recordFailure(p, true, 'typescript');
        expect(record.rejectCount).toBe(1);
        expect(record.failureRate).toBe(1.0);
        expect(record.patternId).toBe('p1');
    });
    test('records both accept and reject', () => {
        const p = makePattern('p2', 'uses ==', '==', '===');
        module.recordFailure(p, false, 'typescript');
        module.recordFailure(p, true, 'typescript');
        module.recordFailure(p, true, 'typescript');
        const record = module.getFailureRecord('p2');
        expect(record.acceptCount).toBe(1);
        expect(record.rejectCount).toBe(2);
        expect(record.totalSignals).toBe(3);
        expect(record.failureRate).toBeCloseTo(2 / 3);
    });
    test('getAvoidSuggestions returns high failure patterns', () => {
        const bad = makePattern('p_bad', 'bad pattern', 'x', 'y');
        const good = makePattern('p_good', 'good pattern', 'a', 'b');
        for (let i = 0; i < 8; i++)
            module.recordFailure(bad, true, 'ts');
        for (let i = 0; i < 5; i++)
            module.recordFailure(good, false, 'ts');
        const suggestions = module.getAvoidSuggestions(0.5);
        const badSuggestion = suggestions.find((s) => s.patternId === 'p_bad');
        const goodSuggestion = suggestions.find((s) => s.patternId === 'p_good');
        expect(badSuggestion).toBeDefined();
        expect(badSuggestion.severity).toBe('critical');
        expect(goodSuggestion).toBeUndefined();
    });
    test('ignores patterns with too few signals', () => {
        const p = makePattern('p_new', 'new pattern', 'x', 'y');
        module.recordFailure(p, true, 'ts');
        const suggestions = module.getAvoidSuggestions(0.5);
        expect(suggestions.find((s) => s.patternId === 'p_new')).toBeUndefined();
    });
    test('getFailureStats returns correct counts', () => {
        const p1 = makePattern('p1', 'a', 'x', 'y');
        const p2 = makePattern('p2', 'b', 'x', 'y');
        const p3 = makePattern('p3', 'c', 'x', 'y');
        for (let i = 0; i < 10; i++)
            module.recordFailure(p1, true, 'ts');
        for (let i = 0; i < 7; i++)
            module.recordFailure(p2, true, 'ts');
        for (let i = 0; i < 3; i++)
            module.recordFailure(p3, false, 'ts');
        const stats = module.getFailureStats();
        expect(stats.totalTracked).toBe(3);
        expect(stats.criticalCount).toBe(2);
        expect(stats.highCount).toBe(0);
        expect(stats.lowCount).toBe(0);
    });
    test('clearFailures resets all data', () => {
        const p = makePattern('p1', 'test', 'x', 'y');
        module.recordFailure(p, true, 'ts');
        expect(module.getAllFailureRecords().length).toBe(1);
        module.clearFailures();
        expect(module.getAllFailureRecords().length).toBe(0);
    });
});
//# sourceMappingURL=failure-memory.test.js.map