test.web/configuration/strings/ontology.fragment.strings.test.ts

test.web/configuration/strings/ontology.fragment.strings.test.ts is a file in Codebase Testing. 71 lines of code and 0 definitions.

import { RECORDS_SEGMENT, tabPath } from "@banes-lab/web/core/assets/link.assets.ts";
import { describe, expect, it } from "vitest";
import type { Block } from "@banes-lab/web/types/block.types.ts";
import { ONTOLOGY_PAGE } from "@banes-lab/web/core/ids/page.ids.ts";
import { ONTOLOGY_TABS } from "@banes-lab/web/configuration/strings/ontology.fragment.strings.ts";
import { anchorOf } from "@banes-lab/web/domain/converters/ontology.converter.ts";

const HREF_OPEN = 'href="';
const QUOTE = '"';
const ANCHOR = "#";
const SLASH = "/";

const textsOf = function textsOf(block: Block): string[] {
    if (block.kind === "text") {
        return [block.text];
    }
    if (block.kind === "list") {
        return [...block.items];
    }
    return block.kind === "glossary" ? block.entries.flatMap((entry) => [entry.term, entry.description]) : [];
};

const hrefsOf = function hrefsOf(text: string): string[] {
    const found: string[] = [];
    let at = text.indexOf(HREF_OPEN);
    while (at !== -1) {
        const close = text.indexOf(QUOTE, at + HREF_OPEN.length);
        found.push(text.slice(at + HREF_OPEN.length, close));
        at = text.indexOf(HREF_OPEN, close);
    }
    return found;
};

const anchorsOf = function anchorsOf(tab: (typeof ONTOLOGY_TABS)[number]): string[] {
    return tab.sections.flatMap((section) => [
        section.id,
        ...section.subsections.flatMap((sub) => (sub.id === undefined ? [] : [sub.id])),
    ]);
};

describe("ONTOLOGY_TABS", () => {
    it("builds each section once and gives every record a unique anchor", () => {
        const built = ONTOLOGY_TABS.flatMap((tab) => tab.sections.map((section) => section.id));
        expect(new Set(built).size).toBe(built.length);
        const anchors = ONTOLOGY_TABS.flatMap(anchorsOf);
        expect(new Set(anchors).size).toBe(anchors.length);
    });

    it("points every link it emits at the page of a record a tab renders, or at a tab and an anchor that tab renders", () => {
        const known = new Map(
            ONTOLOGY_TABS.map((tab) => [tabPath(ONTOLOGY_PAGE, ONTOLOGY_TABS, tab.id), new Set(anchorsOf(tab))]),
        );
        const rendered = new Set(ONTOLOGY_TABS.flatMap(anchorsOf));
        const isRenderedRecord = function isRenderedRecord(href: string): boolean {
            const [, segment, face, id] = href.split(SLASH);
            const anchor = segment === RECORDS_SEGMENT && face !== undefined && id !== undefined ? anchorOf(`${face}:${id}`) : null;
            return anchor !== null && rendered.has(anchor);
        };
        const hrefs = ONTOLOGY_TABS.flatMap((tab) =>
            tab.sections.flatMap((section) => [
                ...(section.blocks ?? []),
                ...section.subsections.flatMap((sub) => sub.blocks ?? []),
            ]),
        )
            .flatMap(textsOf)
            .flatMap(hrefsOf);
        expect(hrefs.length).toBeGreaterThan(5000);
        const broken = hrefs.filter((href) => {
            if (isRenderedRecord(href)) {
                return false;
            }
            const cut = href.indexOf(ANCHOR);
            return cut === -1 || known.get(href.slice(0, cut))?.has(href.slice(cut + 1)) !== true;
        });
        expect(broken).toStrictEqual([]);
    });
});