"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 });
describe('Explainability', () => {
    let module;
    beforeEach(async () => {
        // Clear outcome recorder and failure memory state
        const outcomeModule = await Promise.resolve().then(() => __importStar(require('../../walls/03-knowledge/dna/explainability')));
        const failureModule = await Promise.resolve().then(() => __importStar(require('../../walls/03-knowledge/dna/explainability')));
        // Can't easily clear internal state of these modules without the functions
        // Just import explainability module fresh
        module = await Promise.resolve().then(() => __importStar(require('../../walls/03-knowledge/dna/explainability')));
    });
    const makePattern = (id, mean, ci95, totalSignals, from, to, conditions) => ({
        patternId: id,
        category: 'test',
        description: 'test pattern',
        from,
        to,
        conditions: conditions || [],
        confidence: { distribution: 'beta', alpha: 1, beta: 1, mean, ci95 },
        signals: { positive: 0, negative: 0, total: totalSignals, 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('strong confidence pattern', () => {
        const p = makePattern('p1', 0.95, [0.8, 0.99], 100, 'var x = 1', 'const x = 1');
        const exp = module.explainPattern(p);
        expect(exp.confidence.tier).toBe('strong');
        expect(exp.recommendation).toContain('Recommended');
        expect(exp.risks.length).toBe(0);
    });
    test('low confidence pattern generates warning', () => {
        const p = makePattern('p2', 0.55, [0.3, 0.8], 2, 'x', 'y');
        const exp = module.explainPattern(p);
        expect(exp.confidence.tier).toBe('weak');
        expect(exp.risks.some((r) => r.type === 'low_confidence')).toBe(true);
        expect(exp.risks.some((r) => r.type === 'few_signals')).toBe(true);
    });
    test('uncertain confidence generates critical risk', () => {
        const p = makePattern('p3', 0.4, [0.1, 0.7], 1, 'x', 'y');
        const exp = module.explainPattern(p);
        expect(exp.confidence.tier).toBe('uncertain');
        expect(exp.risks.some((r) => r.severity === 'critical')).toBe(true);
        expect(exp.recommendation).toContain('alternatives');
    });
    test('explainPatternList returns array of explanations', () => {
        const p1 = makePattern('p1', 0.95, [0.8, 0.99], 50, 'x', 'y');
        const p2 = makePattern('p2', 0.55, [0.3, 0.8], 2, 'x', 'y');
        const explanations = module.explainPatternList([p1, p2]);
        expect(explanations.length).toBe(2);
        expect(explanations[0].patternId).toBe('p1');
        expect(explanations[1].patternId).toBe('p2');
    });
    test('conditions are explained', () => {
        const p = makePattern('p4', 0.9, [0.8, 0.99], 20, 'x', 'y', [
            { type: 'language', value: 'typescript', matchScore: 0.9 },
            { type: 'framework', value: 'react', matchScore: 0.8 },
        ]);
        const exp = module.explainPattern(p);
        expect(exp.conditions.length).toBe(2);
        expect(exp.conditions[0].explanation).toContain('typescript');
        expect(exp.conditions[1].explanation).toContain('react');
    });
    test('time estimate varies by line count', () => {
        const simple = makePattern('p5', 0.9, [0.8, 0.99], 10, 'x = 1', 'y = 1');
        const exp1 = module.explainPattern(simple);
        expect(exp1.outcomes.timeSavedEstimate).toBe('~15s');
        const complex = makePattern('p6', 0.9, [0.8, 0.99], 10, Array(10).fill('line').join('\n'), Array(10).fill('line').join('\n'));
        const exp2 = module.explainPattern(complex);
        expect(exp2.outcomes.timeSavedEstimate).toBe('~2m');
    });
});
//# sourceMappingURL=explainability.test.js.map