diff --git a/lib/analytics/__tests__/introduce-an-i18n-framework-with.test.ts b/lib/analytics/__tests__/introduce-an-i18n-framework-with.test.ts new file mode 100644 index 0000000..2fc84f9 --- /dev/null +++ b/lib/analytics/__tests__/introduce-an-i18n-framework-with.test.ts @@ -0,0 +1,14 @@ +import { describe, expect, it } from "vitest"; +import { evaluateIntroduceAnI18nFrameworkWith } from "../introduce-an-i18n-framework-with"; + +describe("evaluateIntroduceAnI18nFrameworkWith", () => { + it("evaluates nominal, elevated, and critical thresholds deterministically", () => { + expect(evaluateIntroduceAnI18nFrameworkWith({ contextId: "ctx-1", primaryMetric: 15, secondaryMetric: 10, limitThreshold: 100 }).status).toBe("nominal"); + expect(evaluateIntroduceAnI18nFrameworkWith({ contextId: "ctx-2", primaryMetric: 40, secondaryMetric: 20 }).status).toBe("elevated"); + expect(evaluateIntroduceAnI18nFrameworkWith({ contextId: "ctx-3", primaryMetric: 70, secondaryMetric: 25, limitThreshold: 100 }).status).toBe("critical"); + }); + + it("rejects blank contextId", () => { + expect(() => evaluateIntroduceAnI18nFrameworkWith({ contextId: " ", primaryMetric: 1, secondaryMetric: 1 })).toThrow(/contextId is required/); + }); +}); diff --git a/lib/analytics/introduce-an-i18n-framework-with.ts b/lib/analytics/introduce-an-i18n-framework-with.ts new file mode 100644 index 0000000..352091d --- /dev/null +++ b/lib/analytics/introduce-an-i18n-framework-with.ts @@ -0,0 +1,40 @@ +export interface IntroduceAnI18nFrameworkWithSummaryInput { + contextId: string; + primaryMetric: number; + secondaryMetric: number; + limitThreshold?: number; +} + +export interface IntroduceAnI18nFrameworkWithSummary { + contextId: string; + primaryMetric: number; + secondaryMetric: number; + scoreBps: number; + status: "nominal" | "elevated" | "critical"; +} + +export function evaluateIntroduceAnI18nFrameworkWith(input: IntroduceAnI18nFrameworkWithSummaryInput): IntroduceAnI18nFrameworkWithSummary { + const contextId = input.contextId.trim(); + if (!contextId) { + throw new Error("evaluateIntroduceAnI18nFrameworkWith: 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, + }; +}