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

describe('Reachability edge cases', () => {
  test('reachability detects path after initialization', () => {
    const arbiter = new Arbiter();
    arbiter.addNode('a', 'node');
    arbiter.addNode('b', 'node');
    arbiter.addNode('c', 'node');
    arbiter.addRelation('a', 'links', 'b');
    arbiter.addRelation('b', 'links', 'c');

    arbiter.graphManager.initializeReachabilityChecker();

    assert.strictEqual(arbiter.isReachable('a', 'c'), true);
    assert.strictEqual(arbiter.isReachable('c', 'a'), false);
  });

  test('reachability returns false for missing nodes', () => {
    const arbiter = new Arbiter();
    arbiter.addNode('a', 'node');
    arbiter.graphManager.initializeReachabilityChecker();
    assert.strictEqual(arbiter.isReachable('a', 'missing'), false);
  });

  test('backward reachability checks reverse direction', () => {
    const arbiter = new Arbiter();
    arbiter.addNode('a', 'node');
    arbiter.addNode('b', 'node');
    arbiter.addRelation('a', 'links', 'b');

    arbiter.graphManager.initializeReachabilityChecker({ enableBackwardIndex: true });

    const srcId = arbiter.nodeIdByKey.get('b');
    const dstId = arbiter.nodeIdByKey.get('a');
    const backwardReachable = arbiter.reachabilityChecker.isReachable(srcId, dstId, { direction: 'backward' });

    assert.strictEqual(backwardReachable, true);
  });
});
