Skip to content

test: cover record_usage event payload and new-total return contract#141

Merged
mikewheeleer merged 1 commit into
Agentpay-Org:mainfrom
fikrah-Tech:test/contracts-record-usage-event-tests
Jun 29, 2026
Merged

test: cover record_usage event payload and new-total return contract#141
mikewheeleer merged 1 commit into
Agentpay-Org:mainfrom
fikrah-Tech:test/contracts-record-usage-event-tests

Conversation

@yunus-dev-codecrafter

Copy link
Copy Markdown
Contributor

closes #104

test: cover record_usage event payload and new-total return contract

Summary

Adds four targeted contract-specification tests for record_usage that verify:

  1. Return-value contractUsageRecord.requests carries the accumulated total, not the per-call delta (e.g. calls of 3 then 5 return 8, never 5).
  2. Event payload contract — The usage event's third tuple element is the per-call delta, the fourth is the running total.
  3. Exactly-one event — Exactly one usage event is emitted per successful record_usage invocation.
  4. Lifetime countersget_total_usage_by_agent and get_total_requests_all_time advance by the delta on each call.

No production code was changed — the existing lib.rs implementation already satisfies the documented contract. These tests make the contract explicit and regression-proof.

Changes

contracts/escrow/src/test.rs (+144 lines)

What Detail
Module header Added ### Covered contracts (record_usage) section documenting the new contract tests
Section header record_usage return-value contract and event-payload semantics
test_record_usage_contract_return_is_new_total Calls 3 then 5; asserts r2.requests == 8 (not 5). Uses assert_ne! to explicitly reject the per-call delta.
test_record_usage_contract_event_fields Three calls (3, 5, 1); asserts each event payload has (delta, total) semantics via assert_latest_usage_event.
test_record_usage_contract_exactly_one_event_per_call Counts usage events (0→1→2→3) via assert_usage_event_count, proving exactly one emission per call.
test_record_usage_contract_lifetime_counters Baseline 0, then 3→5→2 deltas; asserts both counters advance by exactly the delta (3→8→10).

Test output (expected)

running N tests
test test_record_usage_contract_return_is_new_total ... ok
test test_record_usage_contract_event_fields ... ok
test test_record_usage_contract_exactly_one_event_per_call ... ok
test test_record_usage_contract_lifetime_counters ... ok
...
test result: ok. N passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

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 existing test_record_usage_accumulates_across_calls and test_record_usage_emits_usage_event_with_payload tests.

Edge cases covered

Scenario Test
First call on new agent/service pair test_record_usage_contract_return_is_new_total (delta=3, total=3)
Repeated calls accumulating 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)
Multi-service isolation Existing test_record_usage_is_keyed_per_service (unchanged)
Single large delta Existing test_record_usage_isolates_services_and_large_deltas (unchanged)

Security notes

  • Event-audit consistency: The emitted usage event and the UsageRecord return 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.
  • No data disclosure: The event payload exposes only (agent, service_id, delta, total) which is already derivable from the function arguments and the public get_usage entrypoint — no additional information is revealed.
  • Exactly-once guarantee: The test test_record_usage_contract_exactly_one_event_per_call proves that no duplicate or extra events are emitted, so event sinks will not double-count deltas.
  • Lifetime counters monotonicity: get_total_usage_by_agent and get_total_requests_all_time advance by the delta (not the total), so off-chain analytics pipelines that sum usage event deltas will match the contract's lifetime counters exactly.

@mikewheeleer

Copy link
Copy Markdown
Contributor

great stuff — shipping it 🚀

@mikewheeleer mikewheeleer merged commit ca77d08 into Agentpay-Org:main Jun 29, 2026
1 check failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add tests for the usage event payload and the new-total return contract of record_usage

2 participants