Skip to content

test(e2e-live): close audit-flagged false positives + claim-coverage gaps - #13

Open
vrogojin wants to merge 1 commit into
fix/e2e-live-real-issuesfrom
feat/e2e-claim-coverage-fixes
Open

vrogojin wants to merge 1 commit into
fix/e2e-live-real-issuesfrom
feat/e2e-claim-coverage-fixes

Conversation

@vrogojin

@vrogojin vrogojin commented May 3, 2026

Copy link
Copy Markdown
Owner

Summary

Audit (May 2026) compared the e2e-live suite against six explicit user claims. Multiple tests passed on weak proxies that wouldn't catch real regressions. This PR closes the highest-leverage gaps.

# Claim Status before After
1 Real Unicity infra Partially true (Market API not pinned) Pinned in constants + contracts
2 Spawned via Host Manager Agent FALSE (vitest config lied; helpers use direct Docker) Stale comment removed
3 Find each other via intents DB Implicit only Pinned URL + still implicit (full assertion deferred)
4 Real cryptoasset swaps Only basic-roundtrip verified balances Multi-agent + disjoint now have exact-delta + conservation assertions
5a(d) Surplus refund to original payer NOT TESTED New surplus-refund.e2e-live.test.ts (wiring assertion)
5b Unhappy paths return assets NOT TESTED (state-only) All 3 negotiation-failures tests now assert balance restoration via pollUntilBalanceRestored
6 No false positives Many state-only proxies Each scenario now has a balance-level assertion

New helper

test/e2e-live/helpers/portfolio-assertions.tssnapshotPortfolio, expectBalanceDelta, expectBalanceUnchanged, pollUntilBalanceRestored. Extracted from basic-roundtrip.ts:127-170.

Concrete additions

  • multi-agent.test.ts: conservation invariant (sum of deltas = 0) + no-op detection on 3-trader pairwise; exact deltas on partial-fill (-30/+30, +30/-30) and concurrent-matching (-200/+200, +200/-200, 0/0).
  • multi-agent-disjoint.test.ts: exact deltas on all four traders for both pairs through the same escrow.
  • negotiation-failures.test.ts: pollUntilBalanceRestored after every FAILED transition, including the canonical deposit-timeout "did Alice get her tokens back?" case.
  • basic-roundtrip.test.ts: cancel-before-match and intent-expires assert balance unchanged.
  • surplus-refund.e2e-live.test.ts (new): trader's accounting_auto_return_enabled log line asserted at startup.

Known limitations

  • Edge-cases tests (incompatible rate ranges, self-match, blocked counterparty, volume-floor mismatch) intentionally do NOT add strict balance-unchanged assertions: the testnet aggregator is shared, and a fresh intent could legitimately match an unrelated peer during the quiet window. The intent-level assertions (volume_filled === 0n on the named intents) remain the right granularity.
  • Surplus-refund: the wiring is asserted but a full end-to-end "over-pay → refund propagates" requires a fault-injection knob or a separate SDK-level test — deferred. The SDK's own unit tests cover the mechanism.

Expected behavior on first run

The negotiation-failures balance-restoration assertions may fail under current product behavior — that is the intended surfacing of gaps Claim 5b was designed to catch. If those fail, the right fix is product-side (escrow auto-return + trader's setAutoReturn must actually refund deposited funds).

Test plan

  • typecheck clean (src + test trees)
  • 671/671 unit tests pass
  • e2e-live full suite — some new assertions may surface real product gaps

Audit (May 2026) compared the e2e-live suite against six explicit user
claims (real infra, HMA, intents DB, real swaps, happy paths inc.
surplus refund, unhappy paths return assets, no false positives) and
found multiple false positives — tests passing on weak proxies that
would not actually catch a regression.

This change closes the highest-leverage gaps:

## 1. Stale HMA dependency claim removed (Claim 2)

`vitest.e2e-live.config.ts` header claimed the suite required the Host
Manager Agent (`createHostManager`, `hm.spawn`, HMCP-0). Every helper
file explicitly contradicted this — `helpers/contracts.ts` and
`helpers/tenant-fixture.ts` both say "NO host-manager, NO HMCP" and
spawn containers via direct `docker run`. The vitest config's claim
was stale documentation. Replace with an accurate description of what
the suite actually does.

## 2. Pin Market API URL (Claim 3)

`MARKET_API_URL = 'https://market-api.unicity.network'` added to
`constants.ts` and `TestnetConstants` interface. Previously the trader
image's hard-coded default was the only reference; tests had no way to
assert which Market API was being exercised.

## 3. Portfolio snapshot/assert helpers (foundation)

New `helpers/portfolio-assertions.ts` extracts the pre/post balance
snapshot pattern from `basic-roundtrip.test.ts:127-170` (the only file
that did it correctly). Exports:
  - `snapshotPortfolio(tenant)` → compact { UCT: bigint, USDU: bigint }
  - `expectBalanceDelta(before, after, expected)` → exact-delta assert
  - `expectBalanceUnchanged(before, after, tolerance?)` → no-op assert
  - `pollUntilBalanceRestored(tenant, baseline, opts?)` → wait for refund

## 4. Multi-agent + multi-agent-disjoint: balance-delta assertions (Claim 4)

Previously `state === 'COMPLETED'` only — a regression that left state
machines green but skipped token transfer would silently pass.

  - `multi-agent` 3-trader pairwise: conservation invariant (sum of
    deltas across all 3 traders = 0 per coin) + each trader's balance
    must have changed (no-op detection).
  - `multi-agent` partial-fill: exact deltas asserted (Alice -30 UCT
    +30 USDU; Bob +30 UCT -30 USDU).
  - `multi-agent` concurrent-matching: Alice -200/+200; winner +200/-200;
    loser strictly unchanged.
  - `multi-agent-disjoint` 2-pair through same escrow: all four exact
    deltas asserted (-200/+200, +200/-200, -100/+200, +100/-200).

## 5. Negotiation-failures: balance restoration after FAILED (Claim 5b)

Previously every unhappy-path test asserted `state === 'FAILED'` and
non-empty `error_code` only. The deposit-timeout case — the canonical
"did Alice get her tokens back?" scenario — would silently pass even
if Alice's deposit was permanently stranded.

  - `untrusted escrow`: rejection happens pre-deposit, so balances
    must be unchanged (`expectBalanceUnchanged`).
  - `deposit timeout`: Alice DOES deposit; her tokens MUST be refunded
    via the escrow's auto-return-on-cancel mechanism. Use
    `pollUntilBalanceRestored` with a 5-min budget; fail loud if the
    refund doesn't propagate.
  - `escrow unreachable`: similar — both Alice's and Bob's deposits
    must come back. The test budget bumped to 14 min to accommodate
    the dual balance polls. If this assertion fires repeatedly, it
    surfaces a real product gap (no client-side recovery for "escrow
    died holding my deposit") that Claim 5b is designed to catch.

## 6. basic-roundtrip cancel/expire: balance unchanged

Cancel-before-match and intent-expires both assert no swap occurred
via `expectBalanceUnchanged`. Comments document the soft-signal
nature (shared-aggregator interference is theoretically possible
within the test window) — a spurious failure surfaces deltas in the
error message so the operator can investigate.

## 7. New surplus-refund.e2e-live.test.ts (Claim 5a(d))

Previously zero coverage for surplus refund. New test asserts the
trader emits `accounting_auto_return_enabled` at startup (proof that
PR #12's `setAutoReturn('*', true)` wiring is in place). A full
end-to-end "over-pay → refund propagates back to original payer" test
requires either a trader fault-injection knob to deliberately
over-deposit, or a separate SDK-level e2e that bypasses the trader —
both larger fixtures than this commit. The wiring assertion is the
minimum-viable proof that the auto-return MECHANISM is enabled; the
SDK's own unit tests cover the mechanism's correctness.

## Drive-by

- `tenant-fixture.ts`: inline `generatePrivateKey` (sphere-sdk no
  longer exports it at the package root).
- `src/trader/main.ts`: guarded `mintFungibleToken` shim (sphere-sdk
  feature-branch-only API).

## Test plan

- typecheck clean for both src and test trees
- 671/671 unit tests pass
- e2e-live tests will exercise these new assertions; some may fail
  under current product behavior — that is the intended surfacing of
  the gaps Claim 5b/Claim 4 were designed to catch
vrogojin added a commit that referenced this pull request May 4, 2026
Recovery merge of PR #13 used --theirs for src/trader/main.ts which
silently reverted PR #12's setAutoReturn block. Without
setAutoReturn('*', true), the trader does NOT refund surplus on
terminated invoices — exactly the leak surfaced by
negotiation-failures.e2e-live's 'deposit timeout' test (UCT
delta=-1000, USDU delta=+500 instead of restored to baseline).

Re-applies the block right after sphere_initialized log. Behavior
matches PR #12 verbatim:
  - sphere.accounting.setAutoReturn('*', true) gated on
    sphere.accounting !== null
  - RATE_LIMITED treated as success (in-process retry within 5s
    cooldown — flag already set)
  - All other errors fail fast (storage layer broken → downstream
    invoice ops would fail unpredictably anyway)

Verified: typecheck clean. Re-running negotiation-failures live
test will validate the refund path.
vrogojin added a commit that referenced this pull request May 4, 2026
Recovery merge consolidating PRs #9-#13 (real settlement work that was previously stranded) plus refund-leak fix, sphere trader withdraw CLI, and 7 rounds of steelman fixes on the WITHDRAW_TOKEN address validator. All 698 unit/integration tests pass. CI green.
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.

1 participant