Skip to content

test(bitcoin-wallet-snap): repair the integration suite and cover template output order - #158

Open
jeremytsng wants to merge 1 commit into
fix/btc-output-reordering-fx-failfrom
chore/repair-bitcoin-snap-integration-tests
Open

test(bitcoin-wallet-snap): repair the integration suite and cover template output order#158
jeremytsng wants to merge 1 commit into
fix/btc-output-reordering-fx-failfrom
chore/repair-bitcoin-snap-integration-tests

Conversation

@jeremytsng

@jeremytsng jeremytsng commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

Explanation

Stacked on #157, so the base branch is fix/btc-output-reordering-fx-fail — the new test here fails without that fix. Retarget to main once #157 merges.

The problem

The integration suite hasn't run since the migration into this monorepo. Two things stop it before a single test runs: run-integration.sh points at jest.integration.config.js, but the file is jest.integration.config.mjs; and the config has no transform, so it falls back to babel-jest and needs @babel/preset-env, which isn't installed anywhere.

Once it did run, 53 of 60 tests failed. The assertions still expect the old v1 keyring envelope, { pending: false, result }, which the snap stopped returning in 2.0.1. And two tests in keyring.test.ts assume nothing else touches their account, but all four suites share one regtest chain and client-request.test.ts sends 0.001 BTC straight to the address those tests assert on. Which of the two fails depends on the order Jest runs the files in.

Separately, fillPsbt had one assertion: psbt: expect.any(String) // non deterministic. That's why an output-ordering bug was invisible here. Only the change amount varies — the order doesn't.

The fix

Point the script at the real filename, and give the config the same ts-jest transform the unit config already uses.

Update the assertions to the v2 shape. Assert the funding transaction is present rather than alone, and the balance is at least the funded amount rather than exactly it.

Add a test that a template of [deposit, ours(1000), OP_RETURN] comes back with all three in place, ours still worth 1000, and the wallet's change appended after. The existing fillPsbt test now checks output order too — scripts only, since that template's last output belongs to the wallet and legitimately becomes the drain output.

psbt-utils.ts builds and reads PSBTs with no dependencies. Importing @metamask/bitcoindevkit isn't an option here: it wraps a .wasm file that Jest won't resolve.

Verification

Green across repeated runs from a fresh chain: 4 suites, 54 passed, 7 skipped.

The new test was confirmed to fail without #157, coming back as [deposit, OP_RETURN, ours(999778558)] — moved to the end and given the excess.

References

Run it locally with Docker:

yarn workspace @metamask/bitcoin-wallet-snap run build
yarn workspace @metamask/bitcoin-wallet-snap run test:integration

Not addressed here: no CI workflow runs this suite, which is why the rot went unnoticed. Wiring it in needs Docker on the runners, so that's a call for the owning team.

Checklist

  • I've updated the test suite for new or updated code as appropriate
  • I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate
  • I've communicated my changes to consumers by updating changelogs for packages I've changed
  • I've introduced breaking changes in this PR and have prepared draft pull requests for clients and consumer packages to resolve them

Test-only changes, so there's no changelog entry — the no-changelog label is applied for that reason.

@jeremytsng
jeremytsng force-pushed the fix/btc-output-reordering-fx-fail branch 2 times, most recently from cd9168d to 0712819 Compare August 19, 2026 13:17
@jeremytsng
jeremytsng force-pushed the chore/repair-bitcoin-snap-integration-tests branch from cf4d066 to f938a3c Compare August 19, 2026 13:19
@jeremytsng
jeremytsng force-pushed the chore/repair-bitcoin-snap-integration-tests branch from f938a3c to 1c8f601 Compare August 19, 2026 18:26
@jeremytsng
jeremytsng marked this pull request as ready for review August 19, 2026 18:38
@jeremytsng
jeremytsng requested a review from a team as a code owner August 19, 2026 18:38
@jeremytsng
jeremytsng force-pushed the fix/btc-output-reordering-fx-fail branch from 0712819 to 303e410 Compare August 20, 2026 19:04
…plate output order

The integration suite has not run since the migration into this monorepo.
run-integration.sh passed jest.integration.config.js while the file is
jest.integration.config.mjs, and the config declared no transform, so it fell
back to the preset's babel-jest and required @babel/preset-env, which is not
installed. Point the script at the real filename and use the same ts-jest
transform the unit config uses.

With the suite running for the first time, 53 of 60 tests failed. The
assertions still expected the v1 keyring envelope, { pending: false, result },
which the snap stopped returning in 2.0.1, and one assertion required the
funding transaction to be the only transaction on a regtest chain that the
other suites broadcast to.

Add a regression test for template output order: a template whose middle
output belongs to the wallet must keep both its position and its value.
Before the ordering fix that output was moved to the end of the transaction
and given the excess. Assert output order on the existing fillPsbt test too,
which only checked that a string came back.
@jeremytsng
jeremytsng force-pushed the chore/repair-bitcoin-snap-integration-tests branch from 1c8f601 to c834891 Compare August 20, 2026 19:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant