"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const conflict_detector_1 = require("../conflict-detector");
function makePattern(id, category, description, from, to, confidence) {
    return {
        patternId: id,
        category,
        description,
        from,
        to,
        conditions: [],
        confidence: {
            distribution: 'beta',
            alpha: 20,
            beta: 5,
            mean: confidence,
            ci95: [confidence - 0.1, confidence + 0.1],
        },
        signals: { positive: 20, negative: 5, total: 25, lastSignal: Date.now() },
        attribution: {
            primaryCondition: 'test',
            conditionConfidence: 0.9,
            sourceFiles: [],
            note: 'test',
        },
        scope: 'global',
        survivedDays: 10,
        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 } },
    };
}
describe('ConflictDetector', () => {
    let detector;
    beforeEach(() => {
        detector = new conflict_detector_1.ConflictDetector('.fivo/cell');
    });
    test('detects conflicts between patterns in same category', () => {
        const p1 = makePattern('p1', 'export_style', 'Named exports', 'default', 'named', 0.9);
        const p2 = makePattern('p2', 'export_style', 'Default exports', 'named', 'default', 0.85);
        const conflicts = detector.detectConflicts([p1, p2]);
        expect(conflicts.length).toBeGreaterThan(0);
    });
    test('no conflicts for patterns in different categories', () => {
        const p1 = makePattern('p1', 'export_style', 'Named exports', 'default', 'named', 0.9);
        const p2 = makePattern('p2', 'quote_style', 'Single quotes', 'double', 'single', 0.85);
        const conflicts = detector.detectConflicts([p1, p2]);
        expect(conflicts).toHaveLength(0);
    });
    test('resolves conflict via data-driven method', () => {
        const p1 = makePattern('p1', 'export_style', 'Named', 'default', 'named', 0.9);
        const p2 = makePattern('p2', 'export_style', 'Default', 'named', 'default', 0.85);
        const result = detector.resolveConflict(p1, p2, { patternA_bugs: 5, patternB_bugs: 25 });
        expect(result.resolved).toBe(true);
        expect(result.resolutionMethod).toBe('data_driven');
        expect(result.winner).toBe(p1);
    });
    test('resolves conflict via codebase majority', () => {
        const p1 = makePattern('p1', 'export_style', 'Named', 'default', 'named', 0.9);
        const p2 = makePattern('p2', 'export_style', 'Default', 'named', 'default', 0.85);
        const result = detector.resolveConflict(p1, p2, undefined, {
            patternA_usage: 78,
            patternB_usage: 22,
        });
        expect(result.resolved).toBe(true);
        expect(result.resolutionMethod).toBe('codebase_majority');
    });
    test('resolves conflict via lead decision', () => {
        const p1 = makePattern('p1', 'export_style', 'Named', 'default', 'named', 0.9);
        const p2 = makePattern('p2', 'export_style', 'Default', 'named', 'default', 0.85);
        const result = detector.resolveConflict(p1, p2, undefined, undefined, 'A');
        expect(result.resolved).toBe(true);
        expect(result.resolutionMethod).toBe('lead_decision');
    });
    test('unresolved when no data available', () => {
        const p1 = makePattern('p1', 'export_style', 'Named', 'default', 'named', 0.9);
        const p2 = makePattern('p2', 'export_style', 'Default', 'named', 'default', 0.85);
        const result = detector.resolveConflict(p1, p2);
        expect(result.resolved).toBe(false);
        expect(result.resolutionMethod).toBe('unresolved');
    });
    test('creates and tallies vote session', () => {
        const p1 = makePattern('p1', 'export_style', 'Named', 'default', 'named', 0.9);
        const p2 = makePattern('p2', 'export_style', 'Default', 'named', 'default', 0.85);
        const session = detector.createVote(p1, p2, 10);
        expect(session.quorum).toBe(5);
        expect(session.totalMembers).toBe(10);
        detector.castVote(session.patternId, 'user1', 'A');
        detector.castVote(session.patternId, 'user2', 'A');
        detector.castVote(session.patternId, 'user3', 'B');
        detector.castVote(session.patternId, 'user4', 'A');
        detector.castVote(session.patternId, 'user5', 'A');
        const updated = detector.castVote(session.patternId, 'user6', 'A');
        expect(updated).not.toBeNull();
        const pastSession = { ...updated, deadline: new Date(Date.now() - 3600000).toISOString() };
        const tally = detector.tallyVote(pastSession);
        expect(tally.resolved).toBe(true);
        expect(tally.winner).toBe('A');
    });
    test('vote fails without quorum', () => {
        const p1 = makePattern('p1', 'export_style', 'Named', 'default', 'named', 0.9);
        const p2 = makePattern('p2', 'export_style', 'Default', 'named', 'default', 0.85);
        const session = detector.createVote(p1, p2, 10);
        detector.castVote(session.patternId, 'user1', 'A');
        const pastSession = { ...session, deadline: new Date(Date.now() - 3600000).toISOString() };
        const tally = detector.tallyVote(pastSession);
        expect(tally.resolved).toBe(false);
    });
    test('getVoteStatus returns formatted string', () => {
        const p1 = makePattern('p1', 'export_style', 'Named', 'default', 'named', 0.9);
        const p2 = makePattern('p2', 'export_style', 'Default', 'named', 'default', 0.85);
        const session = detector.createVote(p1, p2, 8);
        const status = detector.getVoteStatus(session);
        expect(status).toContain('Team vote open');
        expect(status).toContain('0/8 voted');
    });
});
//# sourceMappingURL=conflict-detector.test.js.map