Skip to content

test: verify get_total_key_supply is scoped per creator - #704

Merged
Chucks1093 merged 2 commits into
accesslayerorg:mainfrom
stableprogrammer:test/get-total-supply-per-creator-701
Aug 23, 2026
Merged

test: verify get_total_key_supply is scoped per creator#704
Chucks1093 merged 2 commits into
accesslayerorg:mainfrom
stableprogrammer:test/get-total-supply-per-creator-701

Conversation

@stableprogrammer

Copy link
Copy Markdown
Contributor

Closes #701

Problem

Issue #701 requires unit tests confirming that get_total_key_supply tracks supply per creator address, not as a global counter. Without explicit isolation tests, a regression that accidentally shares a single supply counter across all creators — or that lets one creator's buy/sell mutate another's supply — would go undetected.

The contract exposes two supply views:

  • get_total_key_supply(creator) → u32 — unchecked, returns 0 for unregistered creators
  • get_creator_supply(creator) → Result<u32, ContractError> — checked, returns Err(NotRegistered) for unregistered creators

Both are tested.

What changed

New file: creator-keys/tests/total_supply_per_creator.rs

17 unit tests, each targeting one narrow invariant so failures pinpoint exactly what broke:

Test What it proves
test_supply_is_zero_immediately_after_creator_registration Baseline — fresh creator starts at 0 before any buys
test_supply_equals_ten_after_ten_buys_for_creator_a Issue spec step 1 — buy 10, assert supply = 10
test_supply_equals_five_after_five_buys_for_creator_b Issue spec step 2 — buy 5, assert supply = 5
test_creator_a_supply_unaffected_by_creator_b_buys Issue spec step 3 — A stays at 10 after B buys 5
test_creator_b_supply_unaffected_by_creator_a_buys Symmetrical check — B stays at 5 after A buys 10
test_sell_three_keys_decrements_creator_a_supply_from_ten_to_seven Issue spec step 4 — sell 3 from A, assert supply = 7
test_selling_from_creator_a_does_not_affect_creator_b_supply Issue spec: sells on A must not bleed into B
test_selling_from_creator_b_does_not_affect_creator_a_supply Symmetrical — sells on B must not bleed into A
test_supply_increments_by_one_per_buy Supply counter increments exactly 1 per buy, checked after every individual purchase
test_supply_decrements_by_one_per_sell Supply counter decrements exactly 1 per sell, checked after every individual sell
test_supply_accumulates_across_multiple_buyers_for_same_creator 3 + 4 + 2 buys across three wallets → supply = 9 (sum, not per-wallet cap)
test_three_creators_maintain_independent_supply_counters Three registrations with 4 / 7 / 2 buys each — all three supplies remain exact
test_supply_reaches_zero_after_selling_all_keys Selling all 3 keys drops supply back to 0
test_unregistered_creator_returns_not_registered_from_checked_view Issue spec step 5 — try_get_creator_supply on unknown address returns Err(Ok(NotRegistered))
test_unregistered_creator_returns_zero_from_unchecked_view get_total_key_supply on unknown address returns 0 (safe-read design)
test_get_total_key_supply_is_idempotent_and_read_only Calling the view three times in a row returns the same value — no hidden state mutation
test_total_supply_equals_sum_of_all_holder_balances Supply invariant: total supply = Σ holder balances, verified after mixed buys and sells

Test approach

  • All tests use the contract_test_env shared helpers (register_creator_keys, register_test_creator, set_pricing_and_fees, test_env_with_auths) so they work under both flat and bonding-curve pricing.
  • A local buy_n_keys helper fetches a live get_buy_quote before every individual purchase — this keeps tests correct under any future pricing change.
  • Each test asserts one invariant with a descriptive failure message that names the expected and actual values and explains why the check matters.

Acceptance criteria

  • Supply scoped correctly per creator
  • Creator A supply unaffected by creator B buys
  • Sell correctly decrements supply
  • Unregistered creator returns NotRegistered from the checked view

Test run

running 17 tests
test test_supply_is_zero_immediately_after_creator_registration ... ok
test test_supply_equals_ten_after_ten_buys_for_creator_a ... ok
test test_supply_equals_five_after_five_buys_for_creator_b ... ok
test test_creator_a_supply_unaffected_by_creator_b_buys ... ok
test test_creator_b_supply_unaffected_by_creator_a_buys ... ok
test test_sell_three_keys_decrements_creator_a_supply_from_ten_to_seven ... ok
test test_selling_from_creator_a_does_not_affect_creator_b_supply ... ok
test test_selling_from_creator_b_does_not_affect_creator_a_supply ... ok
test test_supply_increments_by_one_per_buy ... ok
test test_supply_decrements_by_one_per_sell ... ok
test test_supply_accumulates_across_multiple_buyers_for_same_creator ... ok
test test_three_creators_maintain_independent_supply_counters ... ok
test test_supply_reaches_zero_after_selling_all_keys ... ok
test test_unregistered_creator_returns_not_registered_from_checked_view ... ok
test test_unregistered_creator_returns_zero_from_unchecked_view ... ok
test test_get_total_key_supply_is_idempotent_and_read_only ... ok
test test_total_supply_equals_sum_of_all_holder_balances ... ok

test result: ok. 17 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 1.39s

…rg#701)

Add unit tests confirming that the supply view tracks keys independently
per creator and does not bleed across registrations:

- Supply increments correctly after 10 buys (creator A) and 5 buys (creator B)
- Creator A supply stays at 10 after creator B buys 5 keys
- Selling 3 keys from creator A decrements supply to 7
- Creator B supply is unaffected by creator A sells
- Unregistered creator address returns ContractError::NotRegistered from
  the checked get_creator_supply view

Closes accesslayerorg#701
…layerorg#701)

Adds 17 unit tests covering the full acceptance criteria for issue accesslayerorg#701:
supply isolation per creator, cross-creator non-interference on both
buys and sells, monotonic increment/decrement, multi-buyer aggregation,
three-creator independence, sell-to-zero, idempotent read behaviour, and
NotRegistered enforcement on the checked supply view.

Closes accesslayerorg#701
@Chucks1093
Chucks1093 merged commit 248e4e2 into accesslayerorg:main Aug 23, 2026
1 check passed
@Chucks1093

Copy link
Copy Markdown
Member

Nice work Stable programmer !!!

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 unit tests for the get_total_supply view returning the correct cumulative supply across multiple creators

2 participants