const Graph = require('./index'); describe('Graph', () => { let graph; beforeEach(() => { graph = new Graph(); }); test('should add nodes and retrieve them', () => { graph.addNode('a', { value: 1 }); graph.addNode('b', { value: 2 }); expect(graph.getNode('a')).toEqual({ value: 1 }); expect(graph.getNode('b')).toEqual({ value: 2 }); expect(graph.getAllNodes()).toEqual(expect.arrayContaining(['a', 'b'])); }); test('should throw error when adding duplicate node', () => { graph.addNode('a'); expect(() => graph.addNode('a')).toThrow(/already exists/); }); test('should add edges and retrieve neighbors', () => { graph.addNode('a'); graph.addNode('b'); graph.addNode('c'); graph.addEdge('a', 'b', { weight: 5 }); graph.addEdge('a', 'c', { weight: 3 }); expect(graph.getNeighbors('a')).toEqual(expect.arrayContaining(['b', 'c'])); expect(graph.getNeighbors('b')).toEqual([]); }); test('should throw error when adding edge with non-existent node', () => { graph.addNode('a'); expect(() => graph.addEdge('a', 'x')).toThrow(/Both nodes must exist/); }); test('should retrieve edge data', () => { graph.addNode('a'); graph.addNode('b'); graph.addEdge('a', 'b', { weight: 10 }); expect(graph.getEdgeData('a', 'b')).toEqual({ weight: 10 }); }); test('should retrieve all edges', () => { graph.addNode('a'); graph.addNode('b'); graph.addNode('c'); graph.addEdge('a', 'b', { weight: 1 }); graph.addEdge('b', 'c', { weight: 2 }); const edges = graph.getAllEdges(); expect(edges).toEqual( expect.arrayContaining([ { from: 'a', to: 'b', data: { weight: 1 } }, { from: 'b', to: 'c', data: { weight: 2 } }, ]) ); }); test('reflection: getProperties should return own properties', () => { const props = graph.getProperties(); expect(props).toEqual(expect.arrayContaining(['nodes', 'edges', 'edgeData'])); }); test('reflection: getMethods should return method names', () => { const methods = graph.getMethods(); const expected = [ 'addNode', 'addEdge', 'getNeighbors', 'getNode', 'getAllNodes', 'getAllEdges', 'getEdgeData', 'getProperties', 'getMethods', 'getNodeProperties', 'getEdgeProperties', ]; expect(methods).toEqual(expect.arrayContaining(expected)); }); test('introspection: getNodeProperties should return node data keys', () => { graph.addNode('a', { x: 1, y: 2 }); expect(graph.getNodeProperties('a')).toEqual(expect.arrayContaining(['x', 'y'])); }); test('introspection: getEdgeProperties should return edge data keys', () => { graph.addNode('a'); graph.addNode('b'); graph.addEdge('a', 'b', { weight: 5, label: 'ab' }); expect(graph.getEdgeProperties('a', 'b')).toEqual(expect.arrayContaining(['weight', 'label'])); }); });