import test from 'node:test';
import assert from 'node:assert/strict';

import { createEntityId, dedupeEntityIds } from './entity-id';

test('createEntityId stays unique within the same millisecond', () => {
  const ids = Array.from({ length: 20 }, () => createEntityId('task', 1782361373857));

  assert.equal(new Set(ids).size, ids.length);
});

test('dedupeEntityIds rewrites duplicate ids while preserving the first item id', () => {
  const result = dedupeEntityIds(
    [
      { id: 'task-1', title: 'A' },
      { id: 'task-1', title: 'B' },
      { id: 'task-2', title: 'C' },
      { id: 'task-1', title: 'D' },
    ],
    'task',
    1782361373857,
  );

  assert.equal(result.changed, true);
  assert.equal(result.items[0].id, 'task-1');
  assert.equal(new Set(result.items.map((item) => item.id)).size, result.items.length);
});
