Skip to content

test: add unit tests for env validation, middleware CSP, and wallet connectors - #979

Merged
nanaf6203-bit merged 2 commits into
MettaChain:mainfrom
eulami:test/eulami-issue-coverage
Aug 31, 2026
Merged

test: add unit tests for env validation, middleware CSP, and wallet connectors#979
nanaf6203-bit merged 2 commits into
MettaChain:mainfrom
eulami:test/eulami-issue-coverage

Conversation

@eulami

@eulami eulami commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds comprehensive unit test coverage for three untested modules assigned to eulami, closing all three issues in a single PR. Also fixes a Jest configuration issue that prevented tests from running.


Closes #935requireEnv (env validation) tests

New file: src/config/env/__tests__/schema.test.ts

Tests validateEnv and validateEnvRequirements covering:

  • Default values for optional fields (NEXT_PUBLIC_APP_NAME, NODE_ENV, etc.)
  • String-to-boolean transformations (CSP_ENFORCE, NEXT_PUBLIC_DEBUG_MODE)
  • Rate limit numeric string parsing
  • Invalid NODE_ENV rejection
  • Environment-specific requirements (dev requires nothing, staging requires RPC + WC ID, production warns on missing)
  • Production graceful degradation for missing optional fields

Closes #936 — Middleware CSP tests

New file: src/__tests__/middleware.test.ts

Tests CSP header generation covering:

  • CSP disabled when CSP_ENFORCE is not true
  • Correct CSP directives (default-src 'self', script-src with nonce)
  • API routes (/api/*) bypass CSP entirely
  • Non-HTML Accept headers bypass CSP
  • Nonce uniqueness across 20 sequential requests
  • upgrade-insecure-requests directive present in production only
  • unsafe-eval present in development, excluded in production
  • x-nonce header injected into outgoing requests

Closes #937 — Wallet connector adapter tests

New file: src/lib/walletConnectors/__tests__/walletConnectors.test.ts

23 tests covering all three adapter modules:

Adapter Tests Coverage
MetaMask 8 success, missing ethereum, wrong provider, empty accounts, user rejection (4001), pending request (-32002), availability checks
Coinbase 8 same as MetaMask
WalletConnect 7 configured check, success, empty accounts, user rejection, project ID error, generic error

Infrastructure

  • Renamed jest.config.jsjest.config.cjs — fixes require() in ESM context ("type": "module" in package.json)
  • Removed global @walletconnect/web3-provider mock from jest.setup.js — now controlled per-test for WalletConnect-specific behavior

Test results

eulami and others added 2 commits August 31, 2026 11:04
…onnectors

Add comprehensive test coverage for three untested modules assigned to
eulami (issues MettaChain#935, MettaChain#936, MettaChain#937):

- src/config/env/__tests__/schema.test.ts: Tests for validateEnv and
  validateEnvRequirements covering defaults, type coercion, enum validation,
  and environment-specific requirements (dev/staging/prod).
- src/__tests__/middleware.test.ts: Tests for CSP header generation, nonce
  uniqueness, dev-vs-prod directive differences, API path skipping, and
  x-nonce request header injection.
- src/lib/walletConnectors/__tests__/walletConnectors.test.ts: Tests for
  MetaMask, Coinbase, and WalletConnect adapter mappings covering success
  paths, missing wallet, wrong provider, user rejection, pending request
  errors, and availability checks.

Also renames jest.config.js → jest.config.cjs to fix ESM/CJS conflict
with "type": "module" in package.json, and removes the global
@walletconnect/web3-provider mock from jest.setup.js (now per-test).

Closes MettaChain#935, closes MettaChain#936, closes MettaChain#937

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
@nanaf6203-bit
nanaf6203-bit merged commit 7224855 into MettaChain:main Aug 31, 2026
3 of 5 checks passed
@github-actions

Copy link
Copy Markdown

🔒 Preview Environment Destroyed

The preview environment for this PR has been torn down.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants