diff --git a/src/main/java/com/clearfolio/viewer/analytics/KpiSnapshotLedger.java b/src/main/java/com/clearfolio/viewer/analytics/KpiSnapshotLedger.java index f5cbde5d..6793afe4 100644 --- a/src/main/java/com/clearfolio/viewer/analytics/KpiSnapshotLedger.java +++ b/src/main/java/com/clearfolio/viewer/analytics/KpiSnapshotLedger.java @@ -213,7 +213,11 @@ private static int integer(String field) { private static double rate(String field) { try { - return Double.parseDouble(field); + double parsed = Double.parseDouble(field); + if (!Double.isFinite(parsed) || parsed < 0.0 || parsed > 1.0) { + throw invalidLine(); + } + return parsed; } catch (NumberFormatException ex) { throw invalidLine(ex); } @@ -224,7 +228,11 @@ private static Long nullableLong(String field) { return null; } try { - return Long.parseLong(field); + long parsed = Long.parseLong(field); + if (parsed < 0L) { + throw invalidLine(); + } + return parsed; } catch (NumberFormatException ex) { throw invalidLine(ex); } diff --git a/src/test/java/com/clearfolio/viewer/analytics/KpiSnapshotLedgerTest.java b/src/test/java/com/clearfolio/viewer/analytics/KpiSnapshotLedgerTest.java index d42349fd..61b64962 100644 --- a/src/test/java/com/clearfolio/viewer/analytics/KpiSnapshotLedgerTest.java +++ b/src/test/java/com/clearfolio/viewer/analytics/KpiSnapshotLedgerTest.java @@ -86,6 +86,23 @@ void rejectsInvalidPersistedLedgerLines() throws Exception { assertInvalidLedger(snapshotLine().replace(encoded("tenant-a"), "!")); } + @Test + void rejectsNonFiniteOrOutOfRangePersistedKpiEvidence() throws Exception { + assertInvalidLedger(snapshotLine().replace("\t0.5\t", "\tNaN\t")); + assertInvalidLedger(snapshotLine().replace("\t0.5\t", "\tInfinity\t")); + assertInvalidLedger(snapshotLine().replace("\t0.5\t", "\t-Infinity\t")); + assertInvalidLedger(snapshotLine().replace("\t0.5\t", "\t-0.01\t")); + assertInvalidLedger(snapshotLine().replace("\t0.5\t", "\t1.01\t")); + assertInvalidLedger(snapshotLine().replace("\t123", "\t-1")); + } + + @Test + void acceptsInclusiveKpiNumericBoundaries() throws Exception { + assertValidLedger(snapshotLine().replace("\t0.5\t", "\t0.0\t"), 0.0, 123L); + assertValidLedger(snapshotLine().replace("\t0.5\t", "\t1.0\t"), 1.0, 123L); + assertValidLedger(snapshotLine().replace("\t123", "\t0"), 0.5, 0L); + } + @Test void reportsLoadAndWriteFailures() throws Exception { Path directory = tempDir.resolve("directory-ledger"); @@ -107,7 +124,24 @@ private void assertInvalidLedger(String line) throws Exception { StandardCharsets.UTF_8 ); - assertThrows(IllegalStateException.class, () -> new KpiSnapshotLedger(ledgerPath, CLOCK)); + IllegalStateException error = assertThrows( + IllegalStateException.class, + () -> new KpiSnapshotLedger(ledgerPath, CLOCK) + ); + assertEquals("kpi snapshot ledger contains an invalid line", error.getMessage()); + } + + private void assertValidLedger(String line, double expectedRate, Long expectedP95) throws Exception { + Path ledgerPath = Files.writeString( + tempDir.resolve(UUID.randomUUID() + ".log"), + line + System.lineSeparator(), + StandardCharsets.UTF_8 + ); + + KpiSnapshotLedger ledger = new KpiSnapshotLedger(ledgerPath, CLOCK); + var snapshot = ledger.snapshotsFor("tenant-a").getFirst(); + assertEquals(expectedRate, snapshot.conversionSuccessRate()); + assertEquals(expectedP95, snapshot.p95TimeToPreviewMs()); } private static TenantContext context(String tenantId) {