All files / apps/developer-portal/tools/lib invariants.ts

47.5% Statements 19/40
50% Branches 2/4
21.05% Functions 4/19
50% Lines 17/34

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115                  1x 1x 1x 1x     1x 2x 2x 2x   1x             1x                       1x                 1x                 1x                   1x     2x               1x               1x                                                        
import { readdir, readFile } from "node:fs/promises";
import { resolve } from "node:path";
import type { InvariantFinding } from "#layers/snapshot/shared/types/InvariantFinding.ts";
import { auditProblems } from "./audits.ts";
import { decisionProblems } from "./decisions.ts";
import { WORKSPACE_ROOT } from "./paths.ts";
import { runAllowFailure } from "./run.ts";
import { workflowProblems } from "./workflows.ts";
 
const AUDITS_DIR = "docs/audits";
const DECISIONS_DIR = "docs/decisions";
const WORKFLOWS_DIR = "data/workflows";
const WORKFLOWS_README = "docs/workflows/README.md";
 
/** Reads `pnpm invariants:check --json`, reporting unparseable output as a finding of its own. */
export const parseGateFindings = (stdout: string): InvariantFinding[] => {
    try {
        const parsed = JSON.parse(stdout.trim().split("\n").at(-1) ?? "") as unknown;
        Iif (!Array.isArray(parsed)) throw new TypeError("not an array");
 
        return parsed.map((finding: InvariantFinding) => ({
            id: finding.id,
            title: finding.title,
            detail: finding.detail,
            evidence: finding.evidence,
        }));
    } catch {
        return [
            {
                id: "invariants-gate-unreadable",
                title: "The invariants gate printed something other than its findings",
                detail: "pnpm invariants:check --json did not end with a JSON array.",
                evidence: ["infrastructure/scripts/src/invariants/README.md"],
            },
        ];
    }
};
 
/** Dated audits only — `README.md` and `TEMPLATE.md` are the furniture around them. */
const listAuditFiles = async (): Promise<string[]> => {
    const entries = await readdir(resolve(WORKSPACE_ROOT, AUDITS_DIR)).catch(() => [] as string[]);
 
    return entries
        .filter((entry) => /^\d{4}-.+\.md$/.test(entry))
        .sort((a, b) => a.localeCompare(b));
};
 
/** A finding whose status doesn't parse still counts as open on the audits page, so this is what says why. */
const compareAuditShape = (audits: { file: string; source: string }[]): InvariantFinding[] =>
    auditProblems(audits).map(({ file, message }) => ({
        id: "audit-shape-mismatch",
        title: "An audit doesn't match the template",
        detail: `${file}: ${message}.`,
        evidence: [`${AUDITS_DIR}/${file}`, `${AUDITS_DIR}/TEMPLATE.md`],
    }));
 
/** Dated decisions only — `README.md` and `TEMPLATE.md` are the furniture around them. */
const listDecisionFiles = async (): Promise<string[]> => {
    const entries = await readdir(resolve(WORKSPACE_ROOT, DECISIONS_DIR)).catch(
        () => [] as string[],
    );
 
    return entries
        .filter((entry) => /^\d{4}-.+\.md$/.test(entry))
        .sort((a, b) => a.localeCompare(b));
};
 
export const compareDecisionShape = (
    decisions: { file: string; source: string }[],
): InvariantFinding[] =>
    decisionProblems(decisions).map(({ file, message }) => ({
        id: "decision-shape-mismatch",
        title: "A decision report doesn't match the template",
        detail: `${file}: ${message}.`,
        evidence: [`${DECISIONS_DIR}/${file}`, `${DECISIONS_DIR}/README.md`],
    }));
 
/** The workflows page renders a broken file as a gap, so this is what says which file and why. */
const compareWorkflowShape = (problems: { file: string; message: string }[]): InvariantFinding[] =>
    problems.map(({ file, message }) => ({
        id: "workflow-shape-mismatch",
        title: "A workflow file breaks the rules",
        detail: `${file}: ${message}.`,
        evidence: [`${WORKFLOWS_DIR}/${file}`, WORKFLOWS_README],
    }));
 
export const collectInvariants = async (): Promise<InvariantFinding[]> => {
    const [gate, auditFiles, decisionFiles, workflows] = await Promise.all([
        runAllowFailure("pnpm", ["--silent", "invariants:check", "--json"], WORKSPACE_ROOT),
        listAuditFiles(),
        listDecisionFiles(),
        workflowProblems(resolve(WORKSPACE_ROOT, WORKFLOWS_DIR), WORKSPACE_ROOT),
    ]);
 
    const audits = await Promise.all(
        auditFiles.map(async (file) => ({
            file,
            source: await readFile(resolve(WORKSPACE_ROOT, AUDITS_DIR, file), "utf8"),
        })),
    );
    const decisions = await Promise.all(
        decisionFiles.map(async (file) => ({
            file,
            source: await readFile(resolve(WORKSPACE_ROOT, DECISIONS_DIR, file), "utf8"),
        })),
    );
 
    return [
        ...parseGateFindings(gate),
        ...compareAuditShape(audits),
        ...compareDecisionShape(decisions),
        ...compareWorkflowShape(workflows),
    ];
};