|
2 | 2 | import { test } from "node:test"; |
3 | 3 | import assert from "node:assert/strict"; |
4 | 4 |
|
5 | | -import { classifyDrandRound, DEFAULT_STALE_THRESHOLD_MS } from "./freshness.js"; |
| 5 | +import { classifyDrandRound, computePublishAtMs, DEFAULT_STALE_THRESHOLD_MS } from "./freshness.js"; |
6 | 6 |
|
7 | 7 | test("freshness: missing or invalid round returns unknown", () => { |
8 | 8 | const info = { genesis_time: 1677685200, period: 3 }; |
@@ -76,3 +76,32 @@ test("freshness: stale round", () => { |
76 | 76 | // Custom threshold stale |
77 | 77 | assert.equal(classifyDrandRound(round, info, 1030011, 10).status, "stale"); |
78 | 78 | }); |
| 79 | + |
| 80 | +test("computePublishAtMs rejects unsafe round and period combinations", () => { |
| 81 | + const info = { genesis_time: 1_000_000_000, period: 3 }; |
| 82 | + |
| 83 | + assert.equal(computePublishAtMs(info, Number.MAX_SAFE_INTEGER), null); |
| 84 | + assert.equal( |
| 85 | + computePublishAtMs({ genesis_time: Number.MAX_SAFE_INTEGER, period: 2 }, 1_000), |
| 86 | + null, |
| 87 | + ); |
| 88 | + assert.equal(computePublishAtMs({ genesis_time: 0, period: 3 }, 1), 3000); |
| 89 | +}); |
| 90 | + |
| 91 | +test("freshness: unsafe timestamp math returns unknown near MAX_SAFE_INTEGER", () => { |
| 92 | + const info = { genesis_time: Number.MAX_SAFE_INTEGER - 1, period: 2 }; |
| 93 | + const round = 2; |
| 94 | + const now = 1_700_000_000_000; |
| 95 | + |
| 96 | + const res = classifyDrandRound(round, info, now); |
| 97 | + assert.equal(res.status, "unknown"); |
| 98 | + assert.match(String(res.reason ?? ""), /overflow|unsafe/i); |
| 99 | +}); |
| 100 | + |
| 101 | +test("freshness: valid boundary round still classifies correctly", () => { |
| 102 | + const info = { genesis_time: 1000, period: 1 }; |
| 103 | + const round = 1_000_000; |
| 104 | + const publishAtMs = computePublishAtMs(info, round); |
| 105 | + assert.equal(publishAtMs, 1_001_000_000); |
| 106 | + assert.equal(classifyDrandRound(round, info, publishAtMs!).status, "fresh"); |
| 107 | +}); |
0 commit comments