58 lines
1.9 KiB
TypeScript
58 lines
1.9 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import {
|
|
passphrase,
|
|
randomIntegers,
|
|
randomString,
|
|
rollDice,
|
|
ulid,
|
|
uuidV4,
|
|
uuidV7,
|
|
} from "../../src/random/generators";
|
|
import { randomSource } from "../../src/random/source";
|
|
|
|
describe("random generators", () => {
|
|
it("repeats deterministic recipes", () =>
|
|
expect(
|
|
randomIntegers(randomSource("deterministic", "seed"), 10, 1, 6),
|
|
).toEqual(randomIntegers(randomSource("deterministic", "seed"), 10, 1, 6)));
|
|
it("produces RFC-shaped UUIDs", () => {
|
|
const source = randomSource("deterministic", "id");
|
|
expect(uuidV4(source)).toMatch(
|
|
/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/u,
|
|
);
|
|
expect(uuidV7(source, 0)).toMatch(
|
|
/^[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/u,
|
|
);
|
|
});
|
|
it("creates a 26-character ULID", () =>
|
|
expect(ulid(randomSource("deterministic", "id"), 0)).toMatch(
|
|
/^[0-9A-HJKMNP-TV-Z]{26}$/u,
|
|
));
|
|
it("rejects biased duplicate alphabets", () =>
|
|
expect(() =>
|
|
randomString(randomSource("deterministic", "x"), 3, "aab"),
|
|
).toThrow(/duplicate/u));
|
|
it("bounds dice and reports the modifier", () =>
|
|
expect(
|
|
rollDice(randomSource("deterministic", "dice"), "4d6+2"),
|
|
).toMatchObject({ expression: "4d6+2", modifier: 2 }));
|
|
it("reports only the uniform-list entropy model", () =>
|
|
expect(
|
|
passphrase(randomSource("deterministic", "words"), 4, [
|
|
"one",
|
|
"two",
|
|
"three",
|
|
"four",
|
|
]).entropy,
|
|
).toBe(8));
|
|
it("bounds alphabet and word-list allocation before generation", () => {
|
|
const source = randomSource("deterministic", "bounds");
|
|
expect(() => randomString(source, 1, "ab".repeat(70_000))).toThrow(
|
|
/131,072/u,
|
|
);
|
|
expect(() => passphrase(source, 1, ["x".repeat(10_001), "y"])).toThrow(
|
|
/10,000/u,
|
|
);
|
|
});
|
|
});
|