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

describe('CondensedGraph - Simple Validation', () => {
  test('add and retrieve single edge', () => {
    const graph = new CondensedGraph();
    
    graph.addEdge('user:alice', 'owner', 'doc:report');
    
    const edges = graph.getOutEdges('user:alice');
    assert.strictEqual(edges.length, 1, 'Should have 1 edge');
    
    const edge = graph.getEdge(edges[0]);
    assert.ok(edge, 'Should retrieve edge');
    assert.strictEqual(edge.src, 'user:alice');
    assert.strictEqual(edge.dst, 'doc:report');
  });

  test('multiple edges from same source', () => {
    const graph = new CondensedGraph();
    
    graph.addEdge('user:alice', 'owner', 'doc:report');
    graph.addEdge('user:alice', 'member', 'group:eng');
    graph.addEdge('user:alice', 'viewer', 'doc:report');
    
    const edges = graph.getOutEdges('user:alice');
    assert.strictEqual(edges.length, 3);
  });

  test('edge existence', () => {
    const graph = new CondensedGraph();
    
    graph.addEdge('user:alice', 'owner', 'doc:report');
    
    assert.ok(graph.hasEdge('user:alice', graph.getRelationId('owner'), 'doc:report'), 'Edge should exist');
    assert.ok(!graph.hasEdge('user:bob', graph.getRelationId('owner'), 'doc:report'), 'Non-existent edge should not exist');
  });

  test('memory efficiency - 50K edges', () => {
    const graph = new CondensedGraph();
    const numEdges = 50000;
    
    const startMem = process.memoryUsage().heapUsed;
    
    for (let i = 0; i < numEdges; i++) {
      graph.addEdge(`user:${i % 100}`, 'relation', `user:${(i + 1) % 1000}`);
    }
    
    const endMem = process.memoryUsage().heapUsed;
    const memDelta = endMem - startMem;
    const stats = graph.getStats();
    
    console.log('Memory efficiency (50K edges):');
    console.log('  - Heap delta:', (memDelta / 1024 / 1024).toFixed(2), 'MB');
    console.log('  - Bytes per edge:', (memDelta / numEdges).toFixed(2));
    console.log('  - Utilization:', (stats.utilization * 100).toFixed(2), '%');
    
    assert.strictEqual(graph.numEdges, numEdges);
    assert.ok(memDelta < 50 * 1024 * 1024, 'Memory usage should be reasonable (< 50MB)');
  });
});
