From caa1461557497706508d43a5b584ecfc882743ad Mon Sep 17 00:00:00 2001 From: ryder777777 Date: Tue, 20 Oct 2026 22:04:54 +0000 Subject: [PATCH] feat(analytics): add harden-the-supply-chain-lockfile helper and unit tests (#119) --- .../harden-the-supply-chain-lockfile.test.ts | 14 +++++++ .../harden-the-supply-chain-lockfile.ts | 40 +++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 lib/analytics/__tests__/harden-the-supply-chain-lockfile.test.ts create mode 100644 lib/analytics/harden-the-supply-chain-lockfile.ts diff --git a/lib/analytics/__tests__/harden-the-supply-chain-lockfile.test.ts b/lib/analytics/__tests__/harden-the-supply-chain-lockfile.test.ts new file mode 100644 index 0000000..fe244e6 --- /dev/null +++ b/lib/analytics/__tests__/harden-the-supply-chain-lockfile.test.ts @@ -0,0 +1,14 @@ +import { describe, expect, it } from "vitest"; +import { evaluateHardenTheSupplyChainLockfile } from "../harden-the-supply-chain-lockfile"; + +describe("evaluateHardenTheSupplyChainLockfile", () => { + it("evaluates nominal, elevated, and critical thresholds deterministically", () => { + expect(evaluateHardenTheSupplyChainLockfile({ contextId: "ctx-1", primaryMetric: 15, secondaryMetric: 10, limitThreshold: 100 }).status).toBe("nominal"); + expect(evaluateHardenTheSupplyChainLockfile({ contextId: "ctx-2", primaryMetric: 40, secondaryMetric: 20 }).status).toBe("elevated"); + expect(evaluateHardenTheSupplyChainLockfile({ contextId: "ctx-3", primaryMetric: 70, secondaryMetric: 25, limitThreshold: 100 }).status).toBe("critical"); + }); + + it("rejects blank contextId", () => { + expect(() => evaluateHardenTheSupplyChainLockfile({ contextId: " ", primaryMetric: 1, secondaryMetric: 1 })).toThrow(/contextId is required/); + }); +}); diff --git a/lib/analytics/harden-the-supply-chain-lockfile.ts b/lib/analytics/harden-the-supply-chain-lockfile.ts new file mode 100644 index 0000000..5ac8da7 --- /dev/null +++ b/lib/analytics/harden-the-supply-chain-lockfile.ts @@ -0,0 +1,40 @@ +export interface HardenTheSupplyChainLockfileSummaryInput { + contextId: string; + primaryMetric: number; + secondaryMetric: number; + limitThreshold?: number; +} + +export interface HardenTheSupplyChainLockfileSummary { + contextId: string; + primaryMetric: number; + secondaryMetric: number; + scoreBps: number; + status: "nominal" | "elevated" | "critical"; +} + +export function evaluateHardenTheSupplyChainLockfile(input: HardenTheSupplyChainLockfileSummaryInput): HardenTheSupplyChainLockfileSummary { + const contextId = input.contextId.trim(); + if (!contextId) { + throw new Error("evaluateHardenTheSupplyChainLockfile: contextId is required"); + } + const primaryMetric = Math.max(0, Math.trunc(input.primaryMetric)); + const secondaryMetric = Math.max(0, Math.trunc(input.secondaryMetric)); + const limit = Math.max(1, Math.trunc(input.limitThreshold ?? 100)); + + const scoreBps = Math.min(10000, Math.round(((primaryMetric + secondaryMetric) * 10000) / limit)); + let status: "nominal" | "elevated" | "critical" = "nominal"; + if (scoreBps >= 8500) { + status = "critical"; + } else if (scoreBps >= 5000) { + status = "elevated"; + } + + return { + contextId, + primaryMetric, + secondaryMetric, + scoreBps, + status, + }; +}