test.build/core/loaders/diagram.loader.test.ts

test.build/core/loaders/diagram.loader.test.ts is a file in Codebase Testing. 31 lines of code and 0 definitions.

import { describe, expect, it } from "vitest";
import { discoverSources } from "@banes-lab/build-scripts/core/loaders/diagram.loader.ts";

const FIRST = "flowchart LR\n  a --> b";
const SECOND = "flowchart TD\n  c --> d";

const isModule = function isModule<T>(value: unknown): value is T {
    return typeof value === "object" && value !== null;
};

describe("discoverSources", () => {
    it("imports every strings module through the runner and collects each mermaid source once, sorted", async () => {
        const imported: string[] = [];
        const shared = { kind: "mermaid", text: SECOND };
        const runner = {
            import: async <T>(url: string): Promise<T> => {
                await Promise.resolve();
                imported.push(url);
                const module: unknown = {
                    SECTIONS: [{ blocks: [shared, { kind: "mermaid", text: FIRST }, { kind: "prose", text: "x" }] }],
                    SHARED: shared,
                };
                if (!isModule<T>(module)) {
                    throw new TypeError(url);
                }
                return module;
            },
        };
        const sources = await discoverSources(runner);
        expect(imported.length).toBeGreaterThan(0);
        expect(imported.every((url) => url.endsWith(".strings.ts"))).toBe(true);
        expect(sources).toStrictEqual([FIRST, SECOND]);
    });
});