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

describe('Key manager consistency across subsystems', () => {
  test('relation ids align between arbiter and indices', () => {
    const arbiter = new Arbiter();
    const arbiterRelId = arbiter.keyManager._getRelationId('can_read');
    const indicesRelId = arbiter.indices._getRelationId('can_read');
    assert.strictEqual(arbiterRelId, indicesRelId);
  });

  test('relation cache keys use the same relation id source', () => {
    const arbiter = new Arbiter();
    arbiter.addNode('user', 'user');
    arbiter.addNode('doc', 'document');
    const srcId = arbiter.nodeManager.getNodeId('user');
    const dstId = arbiter.nodeManager.getNodeId('doc');

    const directKeyFromIds = arbiter.relationManager._makeDirectCacheKey(srcId, 'can_read', dstId);
    const directKeyFromStrings = arbiter.relationManager._makeDirectCacheKeyFromStrings('user', 'can_read', 'doc');

    assert.strictEqual(directKeyFromIds, directKeyFromStrings);
  });

  test('value manager and arbiter key managers agree on composite keys', () => {
    const arbiter = new Arbiter();
    arbiter.addNode('user', 'user');
    arbiter.addNode('doc', 'document');

    const srcId = arbiter.keyManager.getStringId('user');
    const dstId = arbiter.keyManager.getStringId('doc');
    const arbiterKey = arbiter.keyManager.createCompositeKey(srcId, 'can_read', dstId);

    const valueManagerKeyManager = new UnifiedKeyManager();
    const valueKey = valueManagerKeyManager.createCompositeKey(srcId, 'can_read', dstId);

    assert.strictEqual(arbiterKey, valueKey);
  });
});
