"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;
    };
})();
describe('KnowledgeGraph', () => {
    let module;
    beforeEach(async () => {
        module = await Promise.resolve().then(() => __importStar(require('../../walls/03-knowledge/graph/knowledge-graph')));
        // Build with empty to reset
        module.buildKnowledgeGraph([]);
    });
    test('buildKnowledgeGraph creates nodes and edges', () => {
        const patterns = [
            { patternId: 'p1', category: 'variable_declaration', description: 'use const', from: 'var x', to: 'const x', language: 'typescript', framework: 'react' },
            { patternId: 'p2', category: 'function_declaration', description: 'use arrow', from: 'function f', to: 'const f = ()', language: 'typescript', framework: 'react' },
        ];
        const g = module.buildKnowledgeGraph(patterns);
        expect(g.nodes.length).toBeGreaterThanOrEqual(5); // 2 patterns + cat + lang + fw
        expect(g.edges.length).toBeGreaterThanOrEqual(2); // category_of edges
    });
    test('getNodeNeighbors returns edges for a node', () => {
        const patterns = [
            { patternId: 'p1', category: 'a', description: 'p1', from: 'x', to: 'y', language: 'ts' },
        ];
        module.buildKnowledgeGraph(patterns);
        const neighbors = module.getNodeNeighbors('p1');
        expect(neighbors).not.toBeNull();
        expect(neighbors.edges.length).toBeGreaterThanOrEqual(1);
    });
    test('getNodeNeighbors returns null for missing node', () => {
        expect(module.getNodeNeighbors('nonexistent')).toBeNull();
    });
    test('findRelatedPatterns returns similar patterns', () => {
        const patterns = [
            { patternId: 'p1', category: 'a', description: 'use const', from: 'var x = 1', to: 'const x = 1', language: 'ts' },
            { patternId: 'p2', category: 'a', description: 'use const', from: 'var y = 2', to: 'const y = 2', language: 'ts' },
            { patternId: 'p3', category: 'b', description: 'use let', from: 'function f', to: '() =>', language: 'ts' },
        ];
        module.buildKnowledgeGraph(patterns);
        const related = module.findRelatedPatterns('p1');
        expect(related.length).toBeGreaterThan(0);
        // p1 should be similar to p2 (both use const pattern)
        const p2Rel = related.find((r) => r.patternId === 'p2');
        expect(p2Rel).toBeDefined();
        expect(p2Rel.relationship).toBe('similar');
    });
    test('getGraphStats returns correct counts', () => {
        const patterns = [
            { patternId: 'p1', category: 'a', description: 'p1', from: 'x', to: 'y', language: 'ts' },
            { patternId: 'p2', category: 'b', description: 'p2', from: 'x', to: 'y', language: 'ts' },
        ];
        module.buildKnowledgeGraph(patterns);
        const stats = module.getGraphStats();
        expect(stats.nodes).toBeGreaterThan(0);
        expect(stats.edges).toBeGreaterThan(0);
        expect(stats.patternNodes).toBe(2);
    });
    test('similar patterns get similarity edges', () => {
        const patterns = [
            { patternId: 'p1', category: 'a', description: 'one', from: 'let x = 1', to: 'const x = 1', language: 'ts' },
            { patternId: 'p2', category: 'a', description: 'two', from: 'let y = 2', to: 'const y = 2', language: 'ts' },
        ];
        const g = module.buildKnowledgeGraph(patterns);
        const similarEdge = g.edges.find((e) => e.type === 'similar');
        expect(similarEdge).toBeDefined();
        expect(similarEdge.weight).toBeGreaterThanOrEqual(0.3);
    });
    test('extractEntities extracts classes and frameworks', () => {
        const patterns = [
            { patternId: 'p1', from: 'MyComponent', to: 'AnotherClass', description: 'React component', category: 'react' },
            { patternId: 'p2', from: 'OldApi', to: 'NewApi', description: 'useEffect cleanup', category: 'react' },
        ];
        const entities = module.extractEntities(patterns);
        expect(entities.length).toBeGreaterThanOrEqual(1);
    });
    test('inferRelationships finds category relationships', () => {
        const patterns = [
            { patternId: 'p1', from: 'a', to: 'b', description: 'A to B', category: 'react' },
            { patternId: 'p2', from: 'a', to: 'c', description: 'A to C', category: 'react' },
        ];
        const rels = module.inferRelationships(patterns);
        expect(rels.some((r) => r.type === 'alternative_to')).toBe(true);
    });
    test('findPaths returns traversal paths', () => {
        const patterns = [
            { patternId: 'p1', category: 'a', description: 'one', from: 'let x', to: 'const x', language: 'ts' },
            { patternId: 'p2', category: 'a', description: 'two', from: 'const x', to: 'let y', language: 'ts' },
        ];
        module.buildKnowledgeGraph(patterns);
        const paths = module.findPaths('p1', 'p2');
        expect(paths).toBeDefined();
    });
});
//# sourceMappingURL=knowledge-graph.test.js.map