test: cover record_usage event payload and new-total return contract#141
Merged
mikewheeleer merged 1 commit intoJun 29, 2026
Conversation
Contributor
|
great stuff — shipping it 🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
closes #104
test: cover record_usage event payload and new-total return contract
Summary
Adds four targeted contract-specification tests for
record_usagethat verify:UsageRecord.requestscarries the accumulated total, not the per-call delta (e.g. calls of 3 then 5 return 8, never 5).usageevent's third tuple element is the per-call delta, the fourth is the running total.usageevent is emitted per successfulrecord_usageinvocation.get_total_usage_by_agentandget_total_requests_all_timeadvance by the delta on each call.No production code was changed — the existing
lib.rsimplementation already satisfies the documented contract. These tests make the contract explicit and regression-proof.Changes
contracts/escrow/src/test.rs(+144 lines)### Covered contracts (record_usage)section documenting the new contract testsrecord_usage return-value contract and event-payload semanticstest_record_usage_contract_return_is_new_totalr2.requests == 8(not 5). Usesassert_ne!to explicitly reject the per-call delta.test_record_usage_contract_event_fields(delta, total)semantics viaassert_latest_usage_event.test_record_usage_contract_exactly_one_event_per_callassert_usage_event_count, proving exactly one emission per call.test_record_usage_contract_lifetime_countersTest output (expected)
All four new tests follow the same assertion patterns (
assert_latest_usage_event,assert_usage_event_count) and setup helpers (setup_initialized,make_agent,make_service) already proven in the existingtest_record_usage_accumulates_across_callsandtest_record_usage_emits_usage_event_with_payloadtests.Edge cases covered
test_record_usage_contract_return_is_new_total(delta=3, total=3)test_record_usage_contract_return_is_new_total(3+5=8),test_record_usage_contract_event_fields(3+5+1=9),test_record_usage_contract_lifetime_counters(3+5+2=10)test_record_usage_is_keyed_per_service(unchanged)test_record_usage_isolates_services_and_large_deltas(unchanged)Security notes
usageevent and theUsageRecordreturn value always agree on the post-write total. An off-chain loop consuming the event stream and one consuming the RPC return value will converge on the same counter state, preventing settlement mismatches.(agent, service_id, delta, total)which is already derivable from the function arguments and the publicget_usageentrypoint — no additional information is revealed.test_record_usage_contract_exactly_one_event_per_callproves that no duplicate or extra events are emitted, so event sinks will not double-count deltas.get_total_usage_by_agentandget_total_requests_all_timeadvance by the delta (not the total), so off-chain analytics pipelines that sumusageevent deltas will match the contract's lifetime counters exactly.