fix(任务): 修复快速创建导致的重复ID

This commit is contained in:
Script Generator
2026-06-25 18:03:53 +08:00
parent 5adc7759ad
commit 55e24442ab
4 changed files with 82 additions and 4 deletions

View File

@@ -0,0 +1,27 @@
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);
});