test.build/core/validators/build.validator.test.ts

test.build/core/validators/build.validator.test.ts is a file in Codebase Testing. 59 lines of code and 0 definitions.

import {
    MISSING_BUILD_FILE,
    oversizePage,
    pageHeadings,
} from "@banes-lab/build-scripts/configuration/strings/validation.strings.ts";
import {
    checkFile,
    checkPageBytes,
    checkPageHeadings,
    isServedUrl,
} from "@banes-lab/build-scripts/core/validators/build.validator.ts";
import { describe, expect, it } from "vitest";
import type { Finding } from "@banes-lab/build-scripts/types/validation.types.ts";
import { PAGE_BYTE_BUDGET } from "@banes-lab/build-scripts/configuration/constants/site.constants.ts";
import { SITE_URL } from "@banes-lab/web/core/assets/link.assets.ts";
import { readOrNull } from "@banes-lab/build-scripts/core/loaders/build.loader.ts";

const ROBOTS_FILE = "robots.txt";

describe("isServedUrl", () => {
    it("serves an address on the site whose file the build wrote, and refuses a foreign or missing one", () => {
        expect(isServedUrl(`https://example.test/${ROBOTS_FILE}`)).toBe(false);
        expect(isServedUrl(`${SITE_URL}/no/such/file.txt`)).toBe(false);
        expect(isServedUrl(`${SITE_URL}/${ROBOTS_FILE}`)).toBe(readOrNull(ROBOTS_FILE) !== null);
    });
});

describe("checkFile", () => {
    it("names a file the build did not write and hands a written one's text to its check", () => {
        const seen: string[] = [];
        const record = (text: string): Finding[] => {
            seen.push(text);
            return [];
        };
        expect(checkFile("no/such/file.txt", record)).toStrictEqual([
            { file: "no/such/file.txt", message: MISSING_BUILD_FILE },
        ]);
        expect(seen).toStrictEqual([]);
        const robots = readOrNull(ROBOTS_FILE);
        expect(checkFile(ROBOTS_FILE, record)).toStrictEqual(
            robots === null ? [{ file: ROBOTS_FILE, message: MISSING_BUILD_FILE }] : [],
        );
        expect(seen).toStrictEqual(robots === null ? [] : [robots]);
    });
});

describe("checkPageBytes", () => {
    it("refuses a page over the bytes a crawler reads, and passes one at the budget", () => {
        expect(checkPageBytes("a.html", "x".repeat(PAGE_BYTE_BUDGET))).toStrictEqual([]);
        expect(checkPageBytes("a.html", "x".repeat(PAGE_BYTE_BUDGET + 1))).toStrictEqual([
            { file: "a.html", message: oversizePage(PAGE_BYTE_BUDGET + 1, PAGE_BYTE_BUDGET) },
        ]);
    });
});

describe("checkPageHeadings", () => {
    it("passes a page with one h1, and refuses none or two while ignoring h1-prefixed names", () => {
        expect(checkPageHeadings("a.html", '<h1 class="t">A</h1><h2>B</h2><h1x>')).toStrictEqual([]);
        expect(checkPageHeadings("a.html", "<h2>B</h2>")).toStrictEqual([{ file: "a.html", message: pageHeadings(0) }]);
        expect(checkPageHeadings("a.html", "<h1>A</h1><h1>B</h1>")).toStrictEqual([
            { file: "a.html", message: pageHeadings(2) },
        ]);
    });
});