| 1 | import { describe, expect, it } from 'vitest'; |
| 2 | |
| 3 | import { bytesToUlid, createUlid, ulidToBytes } from './ulid.js'; |
| 4 | |
| 5 | describe('ulid', () => { |
| 6 | it('round-trips through its 16-byte form', () => { |
| 7 | const ulid = createUlid(); |
| 8 | |
| 9 | expect(ulid).toHaveLength(26); |
| 10 | expect(ulidToBytes(ulid)).toHaveLength(16); |
| 11 | expect(bytesToUlid(ulidToBytes(ulid))).toBe(ulid); |
| 12 | }); |
| 13 | |
| 14 | it('sorts lexicographically by creation time', () => { |
| 15 | const earlier = createUlid(1_700_000_000_000); |
| 16 | const later = createUlid(1_700_000_001_000); |
| 17 | |
| 18 | expect(earlier < later).toBe(true); |
| 19 | }); |
| 20 | |
| 21 | it('rejects malformed input', () => { |
| 22 | expect(() => ulidToBytes('nope')).toThrow(TypeError); |
| 23 | expect(() => bytesToUlid(Buffer.alloc(8))).toThrow(TypeError); |
| 24 | }); |
| 25 | }); |