Make DatasetStore the collision-free id authority

This commit is contained in:
2026-06-07 23:05:51 +03:00
parent 1cad9ba140
commit 5ea2a0d038
4 changed files with 96 additions and 15 deletions
+60
View File
@@ -186,3 +186,63 @@ describe('remove & view transitions', () => {
expect(store().view).toBe('detail');
});
});
describe('id assignment — the store is the collision-free id authority', () => {
test('add reassigns a fresh id even when two creates share a Date.now() id', () => {
// Simulate the tight-loop hazard: two datasets minted with the SAME injected id
// (what Date.now() yields within one millisecond). add must still give them
// distinct, monotonically increasing ids so they cannot collide in IndexedDB.
store().add(
createDataset({
name: 'A',
data: [{ a: 1 }],
format: 'json',
source: 'inline',
now: T,
id: 42,
}),
);
store().add(
createDataset({
name: 'B',
data: [{ a: 1 }],
format: 'json',
source: 'inline',
now: T,
id: 42,
}),
);
const ids = store().datasets.map((d) => d.id);
expect(new Set(ids).size).toBe(2);
// selectedId tracks the reassigned id of the most recent add, not the input 42.
expect(store().selectedId).toBe(store().datasets[0].id);
});
test('addDatasets gives a batch distinct ids past the existing maximum', () => {
store().add(
createDataset({ name: 'Seed', data: [{ a: 1 }], format: 'json', source: 'inline', now: T }),
);
const seedId = store().datasets[0].id;
store().addDatasets([
createDataset({
name: 'X',
data: [{ a: 1 }],
format: 'json',
source: 'inline',
now: T,
id: 42,
}),
createDataset({
name: 'Y',
data: [{ a: 1 }],
format: 'json',
source: 'inline',
now: T,
id: 42,
}),
]);
const ids = store().datasets.map((d) => d.id);
expect(new Set(ids).size).toBe(3);
expect(Math.min(...ids.filter((i) => i !== seedId))).toBeGreaterThan(seedId);
});
});