src/ids/clientOrderIndex.test.ts
v0.2.1 · 868 B
import { describe, expect, it } from 'vitest';
import { clientOrderIndexSeed, createClientOrderIndex } from './clientOrderIndex.js';
describe('createClientOrderIndex', () => {
it('seeds from the clock and counts modulo 2e9', () => {
const now = 1_757_000_000_000;
expect(clientOrderIndexSeed(now)).toBe(757_000_000);
const c = createClientOrderIndex({ now: () => now });
expect(c.current()).toBe(757_000_000);
expect(c.next()).toBe(757_000_001);
expect(c.next()).toBe(757_000_002);
});
it('wraps at the modulus and accepts an explicit seed', () => {
const c = createClientOrderIndex({ seed: 9, modulo: 10 });
expect(c.next()).toBe(0);
expect(c.next()).toBe(1);
expect(() => createClientOrderIndex({ modulo: 1 })).toThrow(RangeError);
expect(() => createClientOrderIndex({ seed: -1 })).toThrow(RangeError);
});
});