From 12b5835d7ace4407d2207fbbc5372588f600b1c6 Mon Sep 17 00:00:00 2001 From: ryder777777 Date: Tue, 20 Oct 2026 22:06:18 +0000 Subject: [PATCH] feat(services): add partition-indexing-per-contract-with service and unit tests (#21) --- ...tion-indexing-per-contract-with.service.ts | 40 +++++++++++++++++++ ...rtition-indexing-per-contract-with.test.ts | 10 +++++ 2 files changed, 50 insertions(+) create mode 100644 src/services/partition-indexing-per-contract-with.service.ts create mode 100644 tests/partition-indexing-per-contract-with.test.ts diff --git a/src/services/partition-indexing-per-contract-with.service.ts b/src/services/partition-indexing-per-contract-with.service.ts new file mode 100644 index 0000000..28b45fa --- /dev/null +++ b/src/services/partition-indexing-per-contract-with.service.ts @@ -0,0 +1,40 @@ +export interface PartitionIndexingPerContractWithReportInput { + marketKey: string; + primaryUnits: number; + secondaryUnits: number; + maxThreshold?: number; +} + +export interface PartitionIndexingPerContractWithReport { + marketKey: string; + primaryUnits: number; + secondaryUnits: number; + utilizationBps: number; + verdict: 'healthy' | 'degraded' | 'halted'; +} + +export function evaluatePartitionIndexingPerContractWith(input: PartitionIndexingPerContractWithReportInput): PartitionIndexingPerContractWithReport { + const marketKey = input.marketKey.trim(); + if (!marketKey) { + throw new Error('evaluatePartitionIndexingPerContractWith: marketKey is required'); + } + const primaryUnits = Math.max(0, Math.trunc(input.primaryUnits)); + const secondaryUnits = Math.max(0, Math.trunc(input.secondaryUnits)); + const limit = Math.max(1, Math.trunc(input.maxThreshold ?? 100)); + + const utilizationBps = Math.min(10000, Math.round(((primaryUnits + secondaryUnits) * 10000) / limit)); + let verdict: 'healthy' | 'degraded' | 'halted' = 'healthy'; + if (utilizationBps >= 8500) { + verdict = 'halted'; + } else if (utilizationBps >= 5000) { + verdict = 'degraded'; + } + + return { + marketKey, + primaryUnits, + secondaryUnits, + utilizationBps, + verdict, + }; +} diff --git a/tests/partition-indexing-per-contract-with.test.ts b/tests/partition-indexing-per-contract-with.test.ts new file mode 100644 index 0000000..8dc4996 --- /dev/null +++ b/tests/partition-indexing-per-contract-with.test.ts @@ -0,0 +1,10 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import { evaluatePartitionIndexingPerContractWith } from '../src/services/partition-indexing-per-contract-with.service'; + +test('evaluatePartitionIndexingPerContractWith: evaluates healthy, degraded, and halted verdicts', () => { + assert.equal(evaluatePartitionIndexingPerContractWith({ marketKey: 'mkt-1', primaryUnits: 15, secondaryUnits: 10, maxThreshold: 100 }).verdict, 'healthy'); + assert.equal(evaluatePartitionIndexingPerContractWith({ marketKey: 'mkt-2', primaryUnits: 35, secondaryUnits: 25 }).verdict, 'degraded'); + assert.equal(evaluatePartitionIndexingPerContractWith({ marketKey: 'mkt-3', primaryUnits: 70, secondaryUnits: 20, maxThreshold: 100 }).verdict, 'halted'); + assert.throws(() => evaluatePartitionIndexingPerContractWith({ marketKey: ' ', primaryUnits: 1, secondaryUnits: 1 }), /marketKey is required/); +});