All files / src coverage.ts

100% Statements 41/41
90% Branches 18/20
100% Functions 13/13
100% Lines 38/38

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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131                                                    248x 248x 744x   2742x   2742x     248x         410x       924x 343x   410x             31x                                             377x 377x 377x 686x 377x         1435x 1559x   849x     1435x                     377x 444x 444x 496x 496x 498x 498x 498x       1028x 1x     497x 497x 497x   495x 495x 495x   443x   376x    
import { canonicalId } from "./id.js";
import type { MouraManifest } from "./manifest.js";
import type {
  VerificationCheckResult,
  VerificationCheckStatus,
  VerificationProjectCheckResult,
} from "./check.js";
 
export interface CoverageCount {
  readonly covered: number;
  readonly total: number;
}
 
export interface CoverageSummary {
  readonly requirements: CoverageCount;
  readonly scenarios: CoverageCount;
  readonly cases: CoverageCount;
  readonly pairs: CoverageCount;
  readonly layers: readonly (CoverageCount & { readonly layer: string })[];
}
 
/** Roll up authoritative pair statuses; only PASS is coverage. */
export function summarizeCoverage(
  manifest: MouraManifest,
  check: VerificationProjectCheckResult,
): CoverageSummary {
  const hierarchy = aggregateCoverageHierarchy(manifest, check);
  const nodes = [...hierarchy.values()];
  const countKind = (kind: CoverageNodeStatus["kind"]): CoverageCount => ({
    covered: nodes.filter(
      (node) => node.kind === kind && node.status === "PASS",
    ).length,
    total: nodes.filter((node) => node.kind === kind).length,
  });
 
  return {
    requirements: countKind("requirement"),
    scenarios: countKind("scenario"),
    cases: countKind("case"),
    pairs: {
      covered: check.entries.filter((entry) => entry.status === "PASS").length,
      total: check.entries.length,
    },
    layers: manifest.verificationLayers.map((layer) => {
      const entries = check.entries.filter((entry) => entry.layer === layer);
      return {
        layer,
        covered: entries.filter((entry) => entry.status === "PASS").length,
        total: entries.length,
      };
    }),
  };
}
 
export const coverageStatuses: readonly VerificationCheckStatus[] = [
  "PASS",
  "FAIL",
  "BROKEN",
  "SKIPPED",
  "UNIMPLEMENTED",
  "MISSING",
];
 
export interface CoverageNodeStatus {
  readonly kind: "requirement" | "scenario" | "case";
  readonly status: "PASS" | "INCOMPLETE" | "FAIL";
  readonly severity: "success" | "warning" | "error";
}
 
/** The summary and map share this roll-up of authoritative Check pair severity.
 * MISSING remains error; SKIPPED and UNIMPLEMENTED remain warning.
 * Exact pair statuses are retained by Check, rather than replaced by this roll-up.
 */
export function aggregateCoverageHierarchy(
  manifest: MouraManifest,
  check: VerificationProjectCheckResult,
): ReadonlyMap<string, CoverageNodeStatus> {
  const nodes = new Map<string, CoverageNodeStatus>();
  const byCase = new Map<string, VerificationCheckResult[]>();
  for (const entry of check.entries)
    byCase.set(entry.caseId, [...(byCase.get(entry.caseId) ?? []), entry]);
  const rollUp = (
    kind: CoverageNodeStatus["kind"],
    children: readonly { readonly severity: CoverageNodeStatus["severity"] }[],
  ): CoverageNodeStatus => {
    const severity =
      children.length === 0 ||
      children.some((child) => child.severity === "error")
        ? "error"
        : children.some((child) => child.severity === "warning")
          ? "warning"
          : "success";
    return {
      kind,
      severity,
      status:
        severity === "success"
          ? "PASS"
          : severity === "warning"
            ? "INCOMPLETE"
            : "FAIL",
    };
  };
  for (const requirement of manifest.requirements) {
    const scenarios: CoverageNodeStatus[] = [];
    for (const scenario of requirement.scenarios) {
      const cases: CoverageNodeStatus[] = [];
      for (const testCase of scenario.cases) {
        const id = canonicalId([requirement, scenario, testCase]);
        const entries = byCase.get(id) ?? [];
        for (const layer of [
          ...testCase.verify,
          ...(testCase.unimplemented ?? []),
        ])
          if (!entries.some((entry) => entry.layer === layer))
            throw new Error(
              `Check result omitted required pair ${id} × ${layer}`,
            );
        const node = rollUp("case", entries);
        nodes.set(id, node);
        cases.push(node);
      }
      const node = rollUp("scenario", cases);
      nodes.set(canonicalId([requirement, scenario]), node);
      scenarios.push(node);
    }
    nodes.set(canonicalId([requirement]), rollUp("requirement", scenarios));
  }
  return nodes;
}