# test.build/core/converters/route.converter.test.ts

> 80 lines of code and 0 definitions.

Tree: Codebase Testing
Language: typescript
Canonical: https://banes-lab.com/anatomy/testing#file-testing-test-build-core-converters-route-converter-test-ts
Source text: https://banes-lab.com/source/testing/test.build/core/converters/route.converter.test.ts.txt

Listed in [test.build/core/converters](https://banes-lab.com/api/source/testing/test.build/core/converters.md), after [test.build/core/converters/report.converter.test.ts](https://banes-lab.com/source/testing/test.build/core/converters/report.converter.test.ts.md) and before [test.build/core/converters/schema.converter.test.ts](https://banes-lab.com/source/testing/test.build/core/converters/schema.converter.test.ts.md).

## Contained in

- [test.build/core/converters](https://banes-lab.com/anatomy/testing/folder-testing-test-build-core-converters.md)

## Source

```typescript
import { DISCOVERY, HOME, TERMS } from "./site.fixture.ts";
import {
    datedRoutes,
    latestStamp,
    parseLedger,
    routeFingerprint,
    stampRoutes,
} from "@banes-lab/build-scripts/core/converters/route.converter.ts";
import { describe, expect, it } from "vitest";
import { undatedRoute } from "@banes-lab/build-scripts/configuration/strings/route.strings.ts";

const FIRST = new Date("2026-09-19T14:00:00Z");
const FIRST_DAY = "2026-09-19";
const LATER = new Date("2026-09-22T09:00:00Z");
const PAGES = { ...DISCOVERY, routes: [HOME, TERMS] };

describe("datedRoutes", () => {
    it("gives each route its published and modified day from the stamps, and stops on a route with no stamp", () => {
        const { dates } = stampRoutes({}, PAGES, FIRST);
        const routes = [
            { page: "home", path: "/" },
            { page: "terms", path: "/terms" },
        ];
        expect(datedRoutes(routes, dates).map((route) => route.dates)).toStrictEqual([
            { modified: FIRST_DAY, published: FIRST_DAY },
            { modified: FIRST_DAY, published: FIRST_DAY },
        ]);
        expect(() => datedRoutes([...routes, { page: "faq", path: "/faq" }], dates)).toThrow(undatedRoute("/faq"));
    });
});

describe("stampRoutes and routeFingerprint", () => {
    it("dates every route on first sight, keeps the date while the content is unchanged, and moves it when the content changes", () => {
        const first = stampRoutes({}, PAGES, FIRST);
        expect(first.stamps.get("/")).toBe(FIRST_DAY);
        expect(first.stamps.get("/terms")).toBe(FIRST_DAY);
        const same = stampRoutes(first.ledger, PAGES, LATER);
        expect(same.stamps.get("/terms")).toBe(FIRST_DAY);
        const edited = { ...PAGES, routes: [HOME, { ...TERMS, markdown: "The terms, revised." }] };
        const changed = stampRoutes(first.ledger, edited, LATER);
        expect(changed.stamps.get("/")).toBe(FIRST_DAY);
        expect(changed.stamps.get("/terms")).toBe("2026-09-22");
        expect(changed.ledger["/terms"]?.fingerprint).toBe(routeFingerprint(edited, edited.routes[1] ?? TERMS));
    });

    it("keeps a route's published date through every change, and seeds it from the held lastmod once", () => {
        const first = stampRoutes({}, PAGES, FIRST);
        expect(first.dates.get("/terms")).toStrictEqual({ lastmod: FIRST_DAY, published: FIRST_DAY });
        const edited = { ...PAGES, routes: [HOME, { ...TERMS, markdown: "The terms, revised." }] };
        const changed = stampRoutes(first.ledger, edited, LATER);
        expect(changed.dates.get("/terms")).toStrictEqual({ lastmod: "2026-09-22", published: FIRST_DAY });
        const held = { "/terms": { fingerprint: "old", lastmod: "2026-09-01" } };
        expect(stampRoutes(held, PAGES, LATER).ledger["/terms"]?.published).toBe("2026-09-01");
    });

    it("drops a route that no longer exists from the ledger", () => {
        const first = stampRoutes({}, PAGES, FIRST);
        const fewer = stampRoutes(first.ledger, { ...PAGES, routes: [HOME] }, LATER);
        expect(Object.keys(fewer.ledger)).toStrictEqual(["/"]);
    });
});

describe("parseLedger", () => {
    it("reads well-formed stamps and refuses a ledger with a malformed stamp or of the wrong shape", () => {
        expect(parseLedger('{"/": {"fingerprint": "a", "lastmod": "2026-01-01"}}')).toStrictEqual({
            "/": { fingerprint: "a", lastmod: "2026-01-01" },
        });
        expect(() => parseLedger('{"/": {"fingerprint": "a", "lastmod": "2026-01-01"}, "/x": {"lastmod": 3}}')).toThrow(
            "/x",
        );
        expect(() =>
            parseLedger('{"/y": {"fingerprint": "a", "lastmod": "2026-01-01", "published": 7}}'),
        ).toThrow("/y");
        expect(() => parseLedger("[]")).toThrow("route ledger");
    });
});

describe("latestStamp", () => {
    it("gives the newest date the ledger holds, and nothing for an empty ledger", () => {
        const ledger = {
            "/": { fingerprint: "a", lastmod: FIRST_DAY },
            "/terms": { fingerprint: "b", lastmod: "2026-09-22" },
        };
        expect(latestStamp(ledger)).toBe("2026-09-22");
        expect(latestStamp({})).toBeNull();
    });
});
```
