All files / apps/developer-portal/layers/audits/shared auditReports.ts

97.91% Statements 47/48
79.41% Branches 27/34
100% Functions 19/19
100% Lines 36/36

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 116                                          34x 54x         24x   18x     34x 23x 9x   6x 6x   6x                                       34x 3x   3x 9x 5x 4x   3x       34x       34x   34x 15x 10x 10x   4x     34x 1x     3x 3x   2x     34x           12x 36x         12x   12x       34x 11x  
import type { AuditFinding, AuditState, FindingStatus } from "#layers/snapshot/shared/audits.ts";
import type { DocPage } from "#layers/snapshot/shared/types/DocPage.ts";
import { AUDIT_STATES, FINDING_STATUSES, auditStateOf } from "#layers/snapshot/shared/audits.ts";
import { stripLeadingEmoji } from "#layers/docs/shared/docPages.ts";
 
export interface AuditReport {
    /** The repo path, `docs/audits/2026-09-25-developer-portal.md`. */
    path: string;
    /** The filename without its extension — the segment the `/audits/:id` route carries. */
    id: string;
    date: string;
    title: string;
    scope: string | null;
    commit: string | null;
    findings: AuditFinding[];
    state: AuditState;
    /** Seeded from the vocabulary, so a status nothing carries yet still shows its zero. Null statuses count as open. */
    counts: Record<FindingStatus, number>;
    updatedAt: string | null;
}
 
const countFindings = (findings: AuditFinding[]): Record<FindingStatus, number> => {
    const counts = Object.fromEntries(FINDING_STATUSES.map((status) => [status, 0])) as Record<
        FindingStatus,
        number
    >;
 
    for (const finding of findings) counts[finding.status ?? "open"] += 1;
 
    return counts;
};
 
export const toAuditReports = (pages: DocPage[]): AuditReport[] =>
    pages
        .filter((page) => page.kind === "audit" && page.audit !== null)
        .map((page) => {
            const id = (page.path.split("/").at(-1) ?? page.path).replace(/\.md$/i, "");
            const findings = page.audit?.findings ?? [];
 
            return {
                path: page.path,
                id,
                date: id.slice(0, 10),
                title: stripLeadingEmoji(page.title),
                scope: page.audit?.scope ?? null,
                commit: page.audit?.commit ?? null,
                findings,
                state: auditStateOf(findings),
                counts: countFindings(findings),
                updatedAt: page.updatedAt,
            };
        });
 
interface AuditReportFilter {
    state?: AuditState | "all";
    scope?: string;
    search?: string;
}
 
export const filterAudits = (reports: AuditReport[], filter: AuditReportFilter): AuditReport[] => {
    const needle = filter.search?.trim().toLowerCase() ?? "";
 
    return reports.filter((report) => {
        if (filter.state && filter.state !== "all" && report.state !== filter.state) return false;
        if (filter.scope && filter.scope !== "all" && report.scope !== filter.scope) return false;
        if (needle.length === 0) return true;
 
        return `${report.title} ${report.scope ?? ""}`.toLowerCase().includes(needle);
    });
};
 
export const AUDIT_SORTS = ["newest", "oldest", "most-open"] as const;
 
export type AuditSort = (typeof AUDIT_SORTS)[number];
 
const newestFirst = (a: AuditReport, b: AuditReport): number => b.id.localeCompare(a.id);
 
export const sortAudits = (reports: AuditReport[], sort: AuditSort): AuditReport[] =>
    reports.slice().sort((a, b) => {
        Iif (sort === "oldest") return a.id.localeCompare(b.id);
        if (sort === "most-open") return b.counts.open - a.counts.open || newestFirst(a, b);
 
        return newestFirst(a, b);
    });
 
export const scopesOf = (reports: AuditReport[]): string[] =>
    [
        ...new Set(
            reports
                .map((report) => report.scope)
                .filter((scope): scope is string => scope !== null),
        ),
    ].sort((a, b) => a.localeCompare(b));
 
/** Across every audit, so the tiles answer "how much is still owed" rather than "how many files". */
export const countAll = (
    reports: AuditReport[],
): {
    findings: Record<FindingStatus, number>;
    states: Record<AuditState, number>;
} => {
    const findings = countFindings(reports.flatMap((report) => report.findings));
    const states = Object.fromEntries(AUDIT_STATES.map((state) => [state, 0])) as Record<
        AuditState,
        number
    >;
 
    for (const report of reports) states[report.state] += 1;
 
    return { findings, states };
};
 
/** Audits with a finding still open, the most open first. */
export const openAudits = (reports: AuditReport[]): AuditReport[] =>
    sortAudits(reports, "most-open").filter((report) => report.state !== "closed");