Skip to content

Add Transaction Builder Tests & tRPC Integration Tests #255

@Andre-Diamond

Description

@Andre-Diamond

Add Transaction Builder Tests & tRPC Integration Tests

Background

Two gaps exist in the current test coverage:

  1. Gap 1 — Transaction building is not covered in Jest. The DRep certificate wiring in registerDrep.tsx, updateDrep.tsx, and retire.tsx should be exercised outside the browser. The staking certificate helpers in stakingCertificates.ts and the proxy contract methods in offchain.ts should also have direct coverage.

  2. Gap 2 — The tRPC persistence path is untested. The browser persists transactions via transaction.createTransaction. A schema mismatch, DB constraint violation, wrong state value, or authorization regression in the tRPC layer is not caught today.

What We're Adding

Option B — Transaction Builder Unit Tests

Extract shared transaction-building logic into testable functions where needed and cover it with Jest tests that run in Node.js. Tests should assert which Mesh builder methods are called with which arguments, without requiring a browser wallet adapter.

Test strategy: use lightweight mock builders that record method calls. Plain JS mocks are enough for targeted helpers; proxy transaction tests can use a Proxy-based mesh mock to record arbitrary builder calls while still exposing the provider fields required by the contract.

Phase 1 — Test infrastructure

  • src/__tests__/tx-builders/fixtures.ts — synthetic UTxO, address, DRep, staking, script CBOR, and stable PARAM_UTXO fixtures.
  • src/__tests__/tx-builders/mockProvider.ts — mock IFetcher / IEvaluator provider; evaluateTx() returns [].
  • src/__tests__/tx-builders/testTxBuilder.tsMeshTxBuilder factory backed by the mock provider.
  • src/__tests__/tx-builders/cborUtils.ts — transaction body and certificate constants retained for possible future structural assertions.
  • src/__tests__/tx-builders/infrastructure.test.ts — smoke coverage for constructing MeshTxBuilder with the mock provider.

Phase 2 — Staking certificate tests

  • src/__tests__/tx-builders/stakingCert.test.ts.
  • Use createCertBuilderMock() to record calls to registerStakeCertificate, deregisterStakeCertificate, delegateStakeCertificate, and withdrawal.
  • Cover buildStakingCertificateActions() for register, deregister, delegate, and register_and_delegate.
  • Cover buildStakingActionConfigs() for withdrawal, registerAndDelegate, and required success messaging.
  • Confirm empty poolHex does not throw because pool validation is left to chain rules.

Phase 3 — DRep certificate extraction and tests

  • src/lib/tx-builders/buildDRepCertTx.ts — add applyDRepCert(txBuilder, params).
  • registerDrep.tsx, updateDrep.tsx, and retire.tsx — call applyDRepCert(...) instead of carrying inline certificate wiring.
  • src/__tests__/tx-builders/drepCert.test.ts — use createDRepBuilderMock() to record builder method calls.
  • Cover register/update/retire certificate method calls and anchor validation.
  • Cover legacy wallet behavior where drepCbor === scriptCbor skips certificateScript.
  • Cover SDK wallet behavior where distinct drepCbor adds certificateScript.
  • Confirm txIn, txInScript, and changeAddress are called as expected.

Phase 4 — Proxy transaction tests

  • src/__tests__/tx-builders/proxy.test.ts.
  • Use createMeshMock(provider), a Proxy-based universal mock that records every builder method call, exposes fetcher / evaluator, and avoids thenable behavior.
  • Mock getWalletInfoForTx with jest.spyOn to inject wallet UTxOs, collateral, and an auth-token UTxO derived from contract.getAuthTokenPolicyId().
  • Cover pure computations: getAuthTokenPolicyId(), deterministic policy ID generation, getDrepId(), and setProxyAddress().
  • Cover setupProxy: minting 10 auth tokens, output to proxy address, selected paramUtxo, and insufficient-balance failure.
  • Cover manageProxyDrep: register/deregister/update certificate calls, missing-anchor failures, missing-auth-token failure, certificateScript, and changeAddress.
  • Cover voteProxyDrep: empty votes, single vote, multiple votes, DRep voter identity, missing auth token, and malformed proposal ID.

Phase 5 — CI gate

  • Add .github/workflows/unit-tests.yml for PRs to main / preprod, pushes to main, and manual dispatch.
  • Run npm run test:ci -- --testPathPatterns="src/__tests__/tx-builders" and upload coverage/.
  • Add 90% line coverage thresholds for src/utils/stakingCertificates.ts and src/lib/tx-builders/buildDRepCertTx.ts in jest.config.mjs.
  • Verify the workflow command passes locally and in CI.

Option B Stabilization

  • Run npm run test:ci -- --testPathPatterns="src/__tests__/tx-builders" locally and in CI.

Option C — tRPC createCaller Integration Tests

Test tRPC procedures directly via createCaller: no HTTP server, no browser, no Playwright. Production code should not need changes for the first pass.

Phase 1 — Test infrastructure

  • src/__tests__/trpc/helpers.tsmakeWalletCtx, makeSessionCtx, and makeAnonymousCtx factories.
  • src/__tests__/trpc/fixtures.tsseedWallet, seedUser, and cleanupFixtures helpers.
  • Real DB tests skip when DATABASE_URL / TEST_DATABASE_URL is absent via an itWithDb guard.

Phase 2 — Authorization tests, mock DB, always runs

  • src/__tests__/trpc/transactionAuth.test.ts.
  • Cover no auth, wallet not found, non-signer forbidden, owner allowed, and signer via sessionWallets allowed.
  • src/__tests__/trpc/proxyAuth.test.ts.
  • Cover no auth, non-signer walletId, user not found, wrong user, valid signer, and missing both walletId and userId.

Phase 3 — Transaction procedure tests, real DB

  • src/__tests__/trpc/createTransaction.test.ts.
  • Cover happy-path row persistence, optional description / txHash, Zod validation for empty txCbor / txJson, forbidden non-signer access, and returned row shape.
  • src/__tests__/trpc/pendingTransactions.test.ts.
  • Cover empty results, one pending transaction, completed state exclusion, createdAt descending order, and cross-wallet isolation.

Phase 4 — Proxy procedure tests, real DB

  • src/__tests__/trpc/createProxy.test.ts.
  • Cover walletId and userId variants, isActive defaulting true, optional description, Zod validation, forbidden cases, and read-back via getProxiesByWallet.

Phase 5 — CI integration

  • Add focused scripts such as test:unit and test:trpc to package.json.
  • Add CI steps for no-DB unit tests and DB-backed tRPC tests using TEST_DATABASE_URL.

Updated Sequencing

PR Covers Source changes? Risk
1 Option B test infrastructure + staking tests None None
2 Option B DRep extraction + tests Yes, small extraction Low
3 Option B proxy tests None None
4 Option B CI gate Workflow + Jest config None
5 Option C infrastructure + auth tests None expected None
6 Option C transaction + proxy procedure tests None expected Low
7 Option C CI gate Scripts + workflow/CI updates None

Option B and Option C remain independently mergeable. Progress should be tracked by ticking the checkboxes above as each task lands.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

Status

No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions