mirror of
https://github.com/olehomelchenko/astrolabe.git
synced 2026-08-08 02:02:33 +00:00
Make DatasetStore the collision-free id authority
This commit is contained in:
@@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user