test.build/core/persistence/route.persistence.test.ts

test.build/core/persistence/route.persistence.test.ts is a file in Codebase Testing. 40 lines of code and 0 definitions.

import { describe, expect, it } from "vitest";
import { mkdtempSync, readFileSync, rmSync } from "node:fs";
import {
    persistLedger,
    readLedger,
    stampLedger,
} from "@banes-lab/build-scripts/core/persistence/route.persistence.ts";
import { DISCOVERY } from "../converters/site.fixture.ts";
import { join } from "node:path";
import { stampRoutes } from "@banes-lab/build-scripts/core/converters/route.converter.ts";
import { tmpdir } from "node:os";

describe("readLedger and persistLedger", () => {
    it("answers empty for a missing file and round-trips through the canonical writer", async () => {
        const dir = mkdtempSync(join(tmpdir(), "ledger-"));
        try {
            const file = join(dir, "sitemap.generated.json");
            expect(readLedger(file)).toStrictEqual({});
            const { ledger } = stampRoutes({}, DISCOVERY, new Date("2026-09-19T14:00:00Z"));
            await persistLedger(file, ledger);
            expect(readLedger(file)).toStrictEqual(ledger);
            expect(readFileSync(file, "utf8").endsWith("\n")).toBe(true);
        } finally {
            rmSync(dir, { force: true, recursive: true });
        }
    });
});

describe("stampLedger", () => {
    it("stamps the routes against the held ledger and writes the result back", async () => {
        const dir = mkdtempSync(join(tmpdir(), "ledger-"));
        try {
            const file = join(dir, "sitemap.generated.json");
            const stamped = await stampLedger(DISCOVERY, new Date("2026-09-19T14:00:00Z"), file);
            expect(readLedger(file)).toStrictEqual(stamped.ledger);
            const again = await stampLedger(DISCOVERY, new Date("2026-09-25T14:00:00Z"), file);
            expect([...again.dates.values()].every((dates) => dates.published === "2026-09-19")).toBe(true);
        } finally {
            rmSync(dir, { force: true, recursive: true });
        }
    });
});