Skip to content

Commit 5cfd464

Browse files
Add more tests
1 parent 9766396 commit 5cfd464

File tree

4 files changed

+74
-3
lines changed

4 files changed

+74
-3
lines changed

source/uuid.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export function ulidToUUID(ulid: ULID): UUID {
2828
uuid.substring(16, 20) +
2929
"-" +
3030
uuid.substring(20);
31-
return uuid as UUID;
31+
return uuid.toUpperCase() as UUID;
3232
}
3333

3434
/**

test/node/crockford.spec.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { describe, expect, it } from "vitest";
2-
import { incrementBase32 } from "../../";
2+
import { fixULIDBase32, incrementBase32 } from "../../";
3+
4+
describe("fixULIDBase32", () => {
5+
it("fixes mis-encoded ULIDs", () => {
6+
expect(fixULIDBase32("oLARYZ6-S41TSV4RRF-FQ69G5FAV")).to.equal("01ARYZ6S41TSV4RRFFQ69G5FAV");
7+
});
8+
});
39

410
describe("incrementBase32", () => {
511
it("increments correctly", () => {

test/node/ulid.spec.ts

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import { describe, expect, it } from "vitest";
2-
import { monotonicFactory, ulid } from "../../";
2+
import { monotonicFactory, ulid, ULIDFactory } from "../../";
33

44
const ULID_REXP = /^[0-7][0-9a-hjkmnp-tv-zA-HJKMNP-TV-Z]{25}$/;
55

66
describe("monotonicFactory", () => {
7+
function stubbedPrng() {
8+
return 0.96
9+
}
10+
711
it("creates a factory", () => {
812
const factory = monotonicFactory();
913
expect(factory).toBeTypeOf("function");
@@ -15,6 +19,51 @@ describe("monotonicFactory", () => {
1519
const id = factory();
1620
expect(id).toMatch(ULID_REXP);
1721
});
22+
23+
describe("during single point in time", function() {
24+
const SEED_TIME = 1469918176385;
25+
const stubbedUlid: ULIDFactory = monotonicFactory(stubbedPrng);
26+
27+
it("first call", function() {
28+
expect(stubbedUlid(SEED_TIME)).to.equal("01ARYZ6S41YYYYYYYYYYYYYYYY");
29+
})
30+
31+
it("second call", function() {
32+
expect(stubbedUlid(SEED_TIME)).to.equal("01ARYZ6S41YYYYYYYYYYYYYYYZ");
33+
})
34+
35+
it("third call", function() {
36+
expect(stubbedUlid(SEED_TIME)).to.equal("01ARYZ6S41YYYYYYYYYYYYYYZ0");
37+
})
38+
39+
it("fourth call", function() {
40+
expect(stubbedUlid(SEED_TIME)).to.equal("01ARYZ6S41YYYYYYYYYYYYYYZ1");
41+
})
42+
})
43+
44+
describe("with specific seedTime", function() {
45+
const stubbedUlid: ULIDFactory = monotonicFactory(stubbedPrng);
46+
47+
it("first call", function() {
48+
expect(stubbedUlid(1469918176385)).to.equal("01ARYZ6S41YYYYYYYYYYYYYYYY");
49+
})
50+
51+
it("second call with the same", function() {
52+
expect(stubbedUlid(1469918176385)).to.equal("01ARYZ6S41YYYYYYYYYYYYYYYZ");
53+
})
54+
55+
it("third call with less than", function() {
56+
expect(stubbedUlid(100000000)).to.equal("01ARYZ6S41YYYYYYYYYYYYYYZ0");
57+
})
58+
59+
it("fourth call with even more less than", function() {
60+
expect(stubbedUlid(10000)).to.equal("01ARYZ6S41YYYYYYYYYYYYYYZ1");
61+
})
62+
63+
it("fifth call with 1 greater than", function() {
64+
expect(stubbedUlid(1469918176386)).to.equal("01ARYZ6S42YYYYYYYYYYYYYYYY");
65+
})
66+
});
1867
});
1968
});
2069

test/node/uuid.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { describe, expect, it } from "vitest";
2+
import { ulidToUUID, uuidToULID } from "../../";
3+
4+
describe("ulidToUUID", () => {
5+
it("converts ULIDs to UUIDs", () => {
6+
expect(ulidToUUID("01ARYZ6S41TSV4RRFFQ69G5FAV")).to.equal("01563DF3-6481-D676-4C61-EFB99302BD5B");
7+
expect(ulidToUUID("01JQ4T23H220KM7X0B3V1109DQ")).to.equal("0195C9A1-0E22-1027-43F4-0B1EC21025B7");
8+
});
9+
});
10+
11+
describe("uuidToULID", () => {
12+
it("converts UUIDs to ULIDs", () => {
13+
expect(uuidToULID("0195C9A4-2E32-C014-5F4F-A7CEF5BE83D5")).to.equal("01JQ4T8BHJR0A5YKX7SVTVX0YN");
14+
expect(uuidToULID("0195C9A4-74CC-5149-BCC4-0A556A0CF19D")).to.equal("01JQ4T8X6CA54VSH0AANN0SWCX");
15+
});
16+
});

0 commit comments

Comments
 (0)