Skip to content
25 linesCodeBlameRaw
1import assert from "node:assert/strict";
2import { test } from "node:test";
3
4import { idFloor, newId } from "./ids.ts";
5
6test("an id has its prefix and a 26-character suffix, and sorts by time", () => {
7 const earlier = newId("evt", 1_790_000_000_000);
8 const later = newId("evt", 1_790_000_000_001);
9 assert.match(earlier, /^evt_[0-9a-hjkmnp-tv-z]{26}$/);
10 assert.ok(earlier < later);
11});
12
13test("a floor bounds the ids of a span, as a range of any index that ends in the id", () => {
14 const from = 1_790_000_000_000;
15 const before = newId("msg", from - 1);
16 const at = newId("msg", from);
17 const later = newId("msg", from + 60_000);
18 const floor = idFloor("msg", from);
19 const ceiling = idFloor("msg", from + 60_000);
20 assert.equal(floor.length, at.length);
21 assert.ok(before < floor);
22 assert.ok(floor < at && at < ceiling);
23 // An id made at the very millisecond of the ceiling is past it.
24 assert.ok(later >= ceiling);
25});