|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import { describe, it } from "node:test"; |
| 3 | + |
| 4 | +import type { KeeperRoundStatusView, RoundStatus } from "./status.js"; |
| 5 | +import { |
| 6 | + ACTIVE_ROUND_STATUSES, |
| 7 | + ERROR_ROUND_STATUSES, |
| 8 | + TERMINAL_ROUND_STATUSES, |
| 9 | + classifyRoundStatus, |
| 10 | + isActiveRoundStatus, |
| 11 | + isErrorRoundStatus, |
| 12 | + isKeeperRoundActive, |
| 13 | + isKeeperRoundSettlementPending, |
| 14 | + isKeeperRoundTerminal, |
| 15 | + isTerminalRoundStatus, |
| 16 | + roundStatusLabel, |
| 17 | +} from "./round-status.js"; |
| 18 | + |
| 19 | +const ALL_STATUSES: RoundStatus[] = [ |
| 20 | + "Unknown", |
| 21 | + "Open", |
| 22 | + "Revealing", |
| 23 | + "Cleared", |
| 24 | + "Settled", |
| 25 | + "Voided", |
| 26 | + "NotFound", |
| 27 | +]; |
| 28 | + |
| 29 | +function viewFor(status: RoundStatus, settlement: KeeperRoundStatusView["settlement"] = "none"): KeeperRoundStatusView { |
| 30 | + return { |
| 31 | + roundId: "1", |
| 32 | + status, |
| 33 | + phase: "complete", |
| 34 | + nextAction: "none", |
| 35 | + commitDeadline: null, |
| 36 | + revealDeadline: null, |
| 37 | + revealRound: null, |
| 38 | + revealReady: false, |
| 39 | + commitClosed: false, |
| 40 | + revealWindowOpen: false, |
| 41 | + voidableAfter: null, |
| 42 | + bidderCount: null, |
| 43 | + revealedCount: null, |
| 44 | + winner: null, |
| 45 | + winningValue: null, |
| 46 | + clearingRule: null, |
| 47 | + settlement, |
| 48 | + lastKeeperAction: null, |
| 49 | + lastError: null, |
| 50 | + retryCount: 0, |
| 51 | + updatedAt: "2026-01-01T00:00:00.000Z", |
| 52 | + }; |
| 53 | +} |
| 54 | + |
| 55 | +describe("round-status classification", () => { |
| 56 | + it("partitions every status into exactly one class", () => { |
| 57 | + const covered = new Set([ |
| 58 | + ...ACTIVE_ROUND_STATUSES, |
| 59 | + ...TERMINAL_ROUND_STATUSES, |
| 60 | + ...ERROR_ROUND_STATUSES, |
| 61 | + ]); |
| 62 | + assert.equal(covered.size, ALL_STATUSES.length); |
| 63 | + for (const status of ALL_STATUSES) { |
| 64 | + assert.ok(covered.has(status), `status ${status} must be classified`); |
| 65 | + } |
| 66 | + }); |
| 67 | + |
| 68 | + it("classifies active statuses", () => { |
| 69 | + for (const status of ["Open", "Revealing", "Cleared"] as RoundStatus[]) { |
| 70 | + assert.equal(isActiveRoundStatus(status), true); |
| 71 | + assert.equal(classifyRoundStatus(status), "active"); |
| 72 | + } |
| 73 | + }); |
| 74 | + |
| 75 | + it("classifies terminal statuses", () => { |
| 76 | + for (const status of ["Settled", "Voided"] as RoundStatus[]) { |
| 77 | + assert.equal(isTerminalRoundStatus(status), true); |
| 78 | + assert.equal(classifyRoundStatus(status), "terminal"); |
| 79 | + } |
| 80 | + }); |
| 81 | + |
| 82 | + it("classifies error statuses", () => { |
| 83 | + for (const status of ["Unknown", "NotFound"] as RoundStatus[]) { |
| 84 | + assert.equal(isErrorRoundStatus(status), true); |
| 85 | + assert.equal(classifyRoundStatus(status), "error"); |
| 86 | + } |
| 87 | + }); |
| 88 | + |
| 89 | + it("does not double-count across buckets", () => { |
| 90 | + for (const status of ALL_STATUSES) { |
| 91 | + const hits = |
| 92 | + Number(isActiveRoundStatus(status)) + |
| 93 | + Number(isTerminalRoundStatus(status)) + |
| 94 | + Number(isErrorRoundStatus(status)); |
| 95 | + assert.equal(hits, 1, `status ${status} should match exactly one predicate`); |
| 96 | + } |
| 97 | + }); |
| 98 | +}); |
| 99 | + |
| 100 | +describe("roundStatusLabel", () => { |
| 101 | + it("returns a human-readable label for every status", () => { |
| 102 | + for (const status of ALL_STATUSES) { |
| 103 | + const label = roundStatusLabel(status); |
| 104 | + assert.ok(typeof label === "string" && label.length > 0); |
| 105 | + assert.ok(label.toLowerCase().startsWith(status.toLowerCase())); |
| 106 | + } |
| 107 | + }); |
| 108 | +}); |
| 109 | + |
| 110 | +describe("keeper round view helpers", () => { |
| 111 | + it("mirror status classification", () => { |
| 112 | + assert.equal(isKeeperRoundActive(viewFor("Open")), true); |
| 113 | + assert.equal(isKeeperRoundActive(viewFor("Settled")), false); |
| 114 | + assert.equal(isKeeperRoundTerminal(viewFor("Voided")), true); |
| 115 | + assert.equal(isKeeperRoundTerminal(viewFor("Revealing")), false); |
| 116 | + }); |
| 117 | + |
| 118 | + it("treats pending and submitted settlement as pending", () => { |
| 119 | + assert.equal(isKeeperRoundSettlementPending(viewFor("Cleared", "pending")), true); |
| 120 | + assert.equal(isKeeperRoundSettlementPending(viewFor("Cleared", "submitted")), true); |
| 121 | + assert.equal(isKeeperRoundSettlementPending(viewFor("Cleared", "none")), false); |
| 122 | + assert.equal(isKeeperRoundSettlementPending(viewFor("Settled", "terminal")), false); |
| 123 | + }); |
| 124 | +}); |
0 commit comments