import assert from 'node:assert/strict';
import { describe, test } from 'node:test';
import { SuccinctGraph } from '../../src/core/succinct/SuccinctGraph.js';

describe('SuccinctGraph (Copied from graph-core)', () => {
  test('basic graph construction', () => {
    const graph = new SuccinctGraph();
    
    graph.addNode('user:alice');
    graph.addNode('user:bob');
    graph.addNode('doc:report');
    
    graph.addOutEdge('user:alice', 'user:bob');
    graph.addOutEdge('user:alice', 'doc:finance');
    graph.addOutEdge('user:bob', 'doc:report');
    
    assert.strictEqual(graph.n, 3);
    assert.strictEqual(graph.numEdges(), 3);
  });

  test('get outgoing edges', () => {
    const graph = new SuccinctGraph();
    
    graph.addNode('user:alice');
    graph.addNode('user:bob');
    graph.addNode('doc:report');
    
    graph.addOutEdge('user:alice', 'owner', 'doc:report');
    graph.addOutEdge('user:alice', 'viewer', 'doc:report');
    
    const edges = graph.getOutEdges('user:alice');
    assert.strictEqual(edges.length, 2);
  });

  test('find edge by relation', () => {
    const graph = new SuccinctGraph();
    
    graph.addNode('user:alice');
    graph.addNode('doc:report');
    graph.addNode('doc:finance');
    graph.addOutEdge('user:alice', 'owner', 'doc:report');
    
    const idx = graph.findEdge('user:alice', graph.getRelationId('owner'), 'doc:report');
    assert.ok(idx !== null, 'Should find owner edge');
    
    const edge = graph.getEdge(idx);
    assert.strictEqual(edge.src, 'user:alice');
    assert.strictEqual(edge.dst, 'doc:report');
  });

  test('iteration performance', () => {
    const graph = new SuccinctGraph();
    
    for (let i = 0; i < 10; i++) {
      graph.addNode(`user:${i}`);
      graph.addOutEdge(`user:${i}`, 'relation', `user:${i + 1}`);
    }
    
    let count = 0;
    graph.forEachOutEdge('user:5', (edge) => {
      count++;
    });
    
    assert.strictEqual(count, 1);
  });
});
