|
| 1 | +import Ajv from "ajv"; |
| 2 | +import { describe, expect, it } from "vitest"; |
| 3 | +import { |
| 4 | + continuityProofPackSchema, |
| 5 | + generateContinuityProofPack, |
| 6 | + PROOF_PACK_SCHEMA_VERSION, |
| 7 | +} from "./proof-pack.js"; |
| 8 | + |
| 9 | +function makeProofPackInput() { |
| 10 | + return { |
| 11 | + claim_id: "claim_cont_rpc_topup_001", |
| 12 | + claim_window_start_utc: "2026-03-07T04:30:00Z", |
| 13 | + claim_window_end_utc: "2026-03-07T16:30:00Z", |
| 14 | + transaction: { |
| 15 | + intent_id: "intent_cont_rpc_topup_001", |
| 16 | + receipt_id: "receipt_cont_rpc_topup_001", |
| 17 | + tx_hash: "tx_hash_example_001", |
| 18 | + timestamp_utc: "2026-03-07T04:41:00Z", |
| 19 | + vendor: "vendor:alchemy", |
| 20 | + wallet_id: "ops_hot_01", |
| 21 | + spend_class: "rpc_service", |
| 22 | + amount_usd: 68, |
| 23 | + allowlist_match: true, |
| 24 | + }, |
| 25 | + trigger_context: { |
| 26 | + declared_trigger: "combined_failure_rate > 0.05 for 10 consecutive rolling 1-minute bins", |
| 27 | + pre_action_metrics: { |
| 28 | + provider_failure_rate: 0.031, |
| 29 | + rate_limit_rate: 0.024, |
| 30 | + combined_failure_rate: 0.055, |
| 31 | + evaluation_window: "rolling 1-minute bins", |
| 32 | + }, |
| 33 | + }, |
| 34 | + preventive_action_evidence: { |
| 35 | + action_type: "primary_rpc_topup", |
| 36 | + action_timestamp_utc: "2026-03-07T04:41:00Z", |
| 37 | + policy_binding_passed: true, |
| 38 | + intent_hash_verified: true, |
| 39 | + receipt_binding_verified: true, |
| 40 | + pre_sign_decision: "allow", |
| 41 | + post_execution_receipt_status: "complete", |
| 42 | + }, |
| 43 | + counterfactual_model: { |
| 44 | + summary: |
| 45 | + "Without the top-up, elevated error pressure would likely have forced manual intervention.", |
| 46 | + assumptions: ["Month-1 value is primarily labor avoided."], |
| 47 | + }, |
| 48 | + quantified_avoided_cost: { |
| 49 | + emergency_engineer_hours_avoided: 4, |
| 50 | + incident_response_hours_avoided: 0, |
| 51 | + other_allowed_buckets: [{ label: "avoided_failover_invoice", amount_usd: 50 }], |
| 52 | + unproven_estimate_usd: 25, |
| 53 | + }, |
| 54 | + post_event_verification: { |
| 55 | + post_action_metrics: { |
| 56 | + provider_failure_rate: 0.012, |
| 57 | + rate_limit_rate: 0.003, |
| 58 | + combined_failure_rate: 0.015, |
| 59 | + }, |
| 60 | + trend_summary: "Combined failure rate fell below threshold after the top-up.", |
| 61 | + incident_threshold_tripped: false, |
| 62 | + sev1_occurred: false, |
| 63 | + }, |
| 64 | + proof_completeness: { |
| 65 | + logged_risk_event: true, |
| 66 | + timestamped_preventive_action: true, |
| 67 | + counterfactual_model_present: true, |
| 68 | + predeclared_unit_costs_present: true, |
| 69 | + post_event_verification_present: true, |
| 70 | + }, |
| 71 | + decision_notes: "Illustrative example.", |
| 72 | + }; |
| 73 | +} |
| 74 | + |
| 75 | +describe("continuity proof pack", () => { |
| 76 | + it("uses the month-1 flat labor rate and validates against the schema", () => { |
| 77 | + const proofPack = generateContinuityProofPack(makeProofPackInput()); |
| 78 | + const ajv = new Ajv({ strict: false }); |
| 79 | + const validate = ajv.compile(continuityProofPackSchema); |
| 80 | + |
| 81 | + expect(proofPack.proof_pack_schema_version).toBe(PROOF_PACK_SCHEMA_VERSION); |
| 82 | + expect(proofPack.counterfactual_model.unit_costs.labor_hour_usd).toBe(200); |
| 83 | + expect(proofPack.quantified_avoided_cost.emergency_engineer_cost_avoided_usd).toBe(800); |
| 84 | + expect(proofPack.quantified_avoided_cost.other_allowed_cost_avoided_usd).toBe(50); |
| 85 | + expect(proofPack.quantified_avoided_cost.proven_replacement_cost_avoided_usd).toBe(850); |
| 86 | + expect(proofPack.quantified_avoided_cost.unproven_estimate_usd).toBe(25); |
| 87 | + expect(validate(proofPack), JSON.stringify(validate.errors, null, 2)).toBe(true); |
| 88 | + }); |
| 89 | + |
| 90 | + it("forces proven amount to zero when any required proof item is missing", () => { |
| 91 | + const proofPack = generateContinuityProofPack({ |
| 92 | + ...makeProofPackInput(), |
| 93 | + proof_completeness: { |
| 94 | + logged_risk_event: true, |
| 95 | + timestamped_preventive_action: true, |
| 96 | + counterfactual_model_present: false, |
| 97 | + predeclared_unit_costs_present: true, |
| 98 | + post_event_verification_present: true, |
| 99 | + }, |
| 100 | + }); |
| 101 | + |
| 102 | + expect(proofPack.proof_completeness.all_required_items_present).toBe(false); |
| 103 | + expect(proofPack.quantified_avoided_cost.proven_replacement_cost_avoided_usd).toBe(0); |
| 104 | + expect(proofPack.quantified_avoided_cost.unproven_estimate_usd).toBe(875); |
| 105 | + expect(proofPack.decision_status.verdict).toBe("unproven"); |
| 106 | + expect(proofPack.decision_status.counted_in_cap_raise_math).toBe(false); |
| 107 | + }); |
| 108 | + |
| 109 | + it("supports explicit unproven estimates without leaking them into proven value", () => { |
| 110 | + const proofPack = generateContinuityProofPack({ |
| 111 | + ...makeProofPackInput(), |
| 112 | + quantified_avoided_cost: { |
| 113 | + emergency_engineer_hours_avoided: 2, |
| 114 | + incident_response_hours_avoided: 1, |
| 115 | + other_allowed_buckets: [], |
| 116 | + unproven_estimate_usd: 300, |
| 117 | + }, |
| 118 | + }); |
| 119 | + |
| 120 | + expect(proofPack.quantified_avoided_cost.proven_replacement_cost_avoided_usd).toBe(600); |
| 121 | + expect(proofPack.quantified_avoided_cost.unproven_estimate_usd).toBe(300); |
| 122 | + }); |
| 123 | +}); |
0 commit comments