diff --git a/lib/analytics/__tests__/move-the-backend-jwt-out.test.ts b/lib/analytics/__tests__/move-the-backend-jwt-out.test.ts new file mode 100644 index 0000000..990b252 --- /dev/null +++ b/lib/analytics/__tests__/move-the-backend-jwt-out.test.ts @@ -0,0 +1,14 @@ +import { describe, expect, it } from "vitest"; +import { evaluateMoveTheBackendJwtOut } from "../move-the-backend-jwt-out"; + +describe("evaluateMoveTheBackendJwtOut", () => { + it("evaluates nominal, elevated, and critical thresholds deterministically", () => { + expect(evaluateMoveTheBackendJwtOut({ contextId: "ctx-1", primaryMetric: 15, secondaryMetric: 10, limitThreshold: 100 }).status).toBe("nominal"); + expect(evaluateMoveTheBackendJwtOut({ contextId: "ctx-2", primaryMetric: 40, secondaryMetric: 20 }).status).toBe("elevated"); + expect(evaluateMoveTheBackendJwtOut({ contextId: "ctx-3", primaryMetric: 70, secondaryMetric: 25, limitThreshold: 100 }).status).toBe("critical"); + }); + + it("rejects blank contextId", () => { + expect(() => evaluateMoveTheBackendJwtOut({ contextId: " ", primaryMetric: 1, secondaryMetric: 1 })).toThrow(/contextId is required/); + }); +}); diff --git a/lib/analytics/move-the-backend-jwt-out.ts b/lib/analytics/move-the-backend-jwt-out.ts new file mode 100644 index 0000000..9e71a9b --- /dev/null +++ b/lib/analytics/move-the-backend-jwt-out.ts @@ -0,0 +1,40 @@ +export interface MoveTheBackendJwtOutSummaryInput { + contextId: string; + primaryMetric: number; + secondaryMetric: number; + limitThreshold?: number; +} + +export interface MoveTheBackendJwtOutSummary { + contextId: string; + primaryMetric: number; + secondaryMetric: number; + scoreBps: number; + status: "nominal" | "elevated" | "critical"; +} + +export function evaluateMoveTheBackendJwtOut(input: MoveTheBackendJwtOutSummaryInput): MoveTheBackendJwtOutSummary { + const contextId = input.contextId.trim(); + if (!contextId) { + throw new Error("evaluateMoveTheBackendJwtOut: 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, + }; +}