Release Random Tools 0.1.0
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
import { expect, test, type Page } from "@playwright/test";
|
||||
|
||||
const ORIGIN = "http://127.0.0.1:4173";
|
||||
async function localOnly(page: Page) {
|
||||
const external: string[] = [];
|
||||
await page.route("**/*", async (route) => {
|
||||
const url = new URL(route.request().url());
|
||||
if (url.origin !== ORIGIN) {
|
||||
external.push(url.href);
|
||||
await route.abort();
|
||||
} else await route.continue();
|
||||
});
|
||||
return external;
|
||||
}
|
||||
|
||||
test("runs from a nested path without external requests", async ({ page }) => {
|
||||
const errors: string[] = [];
|
||||
page.on("pageerror", (error) => errors.push(error.message));
|
||||
page.on("console", (message) => {
|
||||
if (message.type() === "error") errors.push(message.text());
|
||||
});
|
||||
const external = await localOnly(page);
|
||||
await page.goto("/deep/nested/rand/");
|
||||
await expect(
|
||||
page.getByRole("heading", { name: "Random Tools" }),
|
||||
).toBeVisible();
|
||||
expect(external).toEqual([]);
|
||||
expect(errors).toEqual([]);
|
||||
});
|
||||
|
||||
test("repeats deterministic output without contacting the remote source", async ({
|
||||
page,
|
||||
}) => {
|
||||
const external = await localOnly(page);
|
||||
await page.goto("/deep/nested/rand/");
|
||||
await page.getByLabel("Random source").selectOption("deterministic");
|
||||
await page
|
||||
.getByRole("textbox", { name: "Seed", exact: true })
|
||||
.fill("browser-recipe");
|
||||
await page.getByLabel("Count").fill("5");
|
||||
await page.getByRole("button", { name: "Generate numbers" }).click();
|
||||
const result = page.locator(".result > pre");
|
||||
const first = await result.textContent();
|
||||
expect(first?.trim().split("\n")).toHaveLength(5);
|
||||
await page.getByRole("button", { name: "Generate numbers" }).click();
|
||||
await expect(result).toHaveText(first ?? "");
|
||||
await expect(
|
||||
page.getByText(/deterministic-non-cryptographic/u),
|
||||
).toBeAttached();
|
||||
expect(external).toEqual([]);
|
||||
});
|
||||
|
||||
test("serves the release identity and hardened headers", async ({
|
||||
request,
|
||||
}) => {
|
||||
const index = await request.get("/deep/nested/rand/");
|
||||
expect(index.ok()).toBe(true);
|
||||
expect(index.headers()["content-security-policy"]).toContain(
|
||||
"default-src 'self'",
|
||||
);
|
||||
expect(index.headers()["content-security-policy"]).toContain(
|
||||
"connect-src 'self' https://www.random.org",
|
||||
);
|
||||
expect(await index.text()).not.toMatch(/\b(?:src|href)=["']\//u);
|
||||
const manifest = await request.get("/deep/nested/rand/toolbox-app.json");
|
||||
await expect(manifest.json()).resolves.toMatchObject({
|
||||
id: "de.add-ideas.rand-tools",
|
||||
version: "0.1.0",
|
||||
entry: "./",
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,17 @@
|
||||
import { render, screen } from "@testing-library/react";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { App } from "../../src/App";
|
||||
|
||||
describe("Random Tools", () => {
|
||||
it("renders the local workbench and standard shell", async () => {
|
||||
vi.stubGlobal(
|
||||
"fetch",
|
||||
vi.fn(async () => new Response("Not found", { status: 404 })),
|
||||
);
|
||||
render(<App />);
|
||||
expect(
|
||||
await screen.findByRole("heading", { name: "Random Tools" }),
|
||||
).toBeVisible();
|
||||
expect(await screen.findByText("WebCrypto by default")).toBeVisible();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,57 @@
|
||||
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,
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user