This document provides step-by-step instructions for verifying the implementation of invariant tests for stream creation form state across account changes.
- Issue: #1399
- Branch:
fix/issue-1399-account-switch-invariants - Test File:
src/components/__tests__/CreateStreamModal.accountSwitch.test.tsx - Spec:
ACCOUNT_SWITCH_INVARIANTS_SPEC.md - PR Summary:
PR_SUMMARY.md
# Ensure you have the correct Node.js version
node --version # Should be compatible with project
# Install dependencies
pnpm install
# Or if pnpm is not available
npm install# Run only the account switch tests
pnpm vitest run src/components/__tests__/CreateStreamModal.accountSwitch.test.tsx
# Run with verbose output
pnpm vitest run src/components/__tests__/CreateStreamModal.accountSwitch.test.tsx --reporter=verbose
# Run with coverage
pnpm vitest run src/components/__tests__/CreateStreamModal.accountSwitch.test.tsx --coverageExpected Output:
✓ src/components/__tests__/CreateStreamModal.accountSwitch.test.tsx (27)
✓ CreateStreamModal - Account Switch Invariants (21)
✓ Invariant 1: No stale sender account in submission payload (4)
✓ should capture current wallet address at submit time, not form-open time
✓ should prevent submission with stale account after disconnect
✓ should validate recipient is not the same as current sender account
✓ should detect if recipient becomes same as sender after account switch
✓ Invariant 2: Network mismatch prevents submission (2)
✓ should block submission when network changes to unexpected network
✓ should allow submission after network is switched back to expected network
✓ Invariant 3: Form state handling on account changes (2)
✓ should preserve recipient and amount fields after account switch
✓ should preserve rate and duration fields after account switch on step 2
✓ Invariant 4: Modal reopening after account switch (2)
✓ should use current account when modal is reopened after account switch
✓ should validate with current account when modal reopens
✓ Invariant 5: Validation error visibility before signing (3)
✓ should show clear error when trying to submit with disconnected wallet
✓ should show clear error when trying to submit with wrong network
✓ should show error immediately when validation fails at submit
✓ Invariant 6: Multiple account switches during form session (2)
✓ should handle rapid account switches without breaking validation
✓ should use final account state at submission time after multiple switches
✓ Invariant 7: Draft recovery safety after account switch (1)
✓ should not apply draft from different account without validation
✓ CreateStreamModal - Documented Reset Behavior (6)
✓ documents that form fields are preserved across account changes
✓ documents that validation is re-evaluated at submit time with current wallet state
✓ documents the submission payload construction timing
Test Files 1 passed (1)
Tests 27 passed (27)
# Run all CreateStreamModal tests
pnpm vitest run src/components/__tests__/CreateStreamModal
# Expected: All existing tests should still passThe issue requires verification of the implementation. Run this command:
# Windows PowerShell
rg -n "CreateStreamModal|account|asset|submit" src/components/CreateStreamModal.tsx | Select-Object -First 160
# Linux/Mac
rg -n "CreateStreamModal|account|asset|submit" src/components/CreateStreamModal.tsx | head -160Key Lines to Verify:
-
Line ~419: Sender captured at submit time
const sender = wallet.address!; // Current wallet, not stale
-
Line ~593: Self-payment validation
if (wallet.connected && wallet.address && normalizedRecipient.toLowerCase() === wallet.address.toLowerCase()) { fieldErrors.recipient = "Recipient cannot be the same as the connected wallet address."; }
-
Line ~822: Wallet connection check at submit
if (!wallet.connected) { setError(t("createStream.validation.walletNotConnected")); return; }
-
Line ~827: Network mismatch check at submit
if (wallet.isNetworkMismatch) { setError(t("createStream.validation.networkMismatch", { expected: wallet.expectedNetwork, actual: wallet.network?.toUpperCase() || "", })); return; }
# Run linter
pnpm lint
# Check formatting
pnpm format:check
# If formatting issues exist, auto-fix
pnpm format# Verify the project still builds
pnpm build
# Expected: Build should succeed with no errors# Run end-to-end tests to ensure no regression
pnpm test:e2e
# Expected: All E2E tests should passpnpm vitest run src/components/__tests__/CreateStreamModal.accountSwitch.test.tsx --coverageExpected Coverage (for tested scenarios):
- Statements: High coverage on validation paths
- Branches: All account switch scenarios covered
- Functions:
buildSubmissionPayload,validateStep1,handleNextcovered - Lines: Critical safety checks covered
Check that the following code paths are tested:
-
buildSubmissionPayload()- sender capture -
validateStep1()- recipient validation -
handleNext()- wallet connection check -
handleNext()- network mismatch check - Form state preservation on account switch
- Modal reopen with different account
- Draft recovery with account switch
Test: should capture current wallet address at submit time
Verify: Transaction uses current wallet.address, not stale value
Test: should detect if recipient becomes same as sender after account switch
Verify: Self-to-self transactions are blocked
Test: should block submission when network changes to unexpected network
Verify: Cross-network transactions are blocked
Test: should prevent submission with stale account after disconnect
Verify: Disconnected wallet blocks submission
Solution: Install pnpm or use npm
npm install -g pnpm
# Or use npm directly
npm run testSolution: Install dependencies
pnpm install
# Or
npm installSolution: Increase timeout in test config
// In test file
it('test name', async () => {
// ...
}, 10000); // 10 second timeoutSolution: Verify vi.mock() is at top of test file, before imports
-
ACCOUNT_SWITCH_INVARIANTS_SPEC.md
- Design decision is clear
- Code references are accurate
- Security properties are documented
- Edge cases are listed
-
PR_SUMMARY.md
- Before/after behavior is explained
- Test coverage is documented
- Acceptance criteria are met
- No production code changes (intentional)
-
src/components/tests/CreateStreamModal.accountSwitch.test.tsx
- All 27 tests present
- Tests are well-documented
- Mock strategy is clear
- Test helpers are reusable
From issue #1399:
Verified by:
- Test: "should capture current wallet address at submit time, not form-open time"
- Test: "should prevent submission with stale account after disconnect"
- Test: "should use final account state at submission time after multiple switches"
Code verification:
// Line ~419 in CreateStreamModal.tsx
const sender = wallet.address!; // Captured at submit timeVerified by:
- Document:
ACCOUNT_SWITCH_INVARIANTS_SPEC.md(Design Decision section) - Test suite:
CreateStreamModal - Documented Reset Behavior(6 tests) - PR Summary:
PR_SUMMARY.md(Design Decision section)
Verified by:
- Test: "should show clear error when trying to submit with disconnected wallet"
- Test: "should show clear error when trying to submit with wrong network"
- Test: "should show error immediately when validation fails at submit"
Code verification:
// Network mismatch error
if (wallet.isNetworkMismatch) {
setError(t("createStream.validation.networkMismatch", { ... }));
return;
}
// Disconnected wallet error
if (!wallet.connected) {
setError(t("createStream.validation.walletNotConnected"));
return;
}Verified by:
- 27 comprehensive test cases in
CreateStreamModal.accountSwitch.test.tsx - All invariants covered with multiple test scenarios
- Property-based testing for validation consistency
Verified by:
- No existing test files modified
- All existing tests still pass
- Only added new test file
Before submitting PR:
- All 27 new tests pass
- All existing tests still pass
- Lint check passes
- Format check passes
- Build succeeds
- Documentation is complete
- Code references are accurate
- Acceptance criteria are met
- No production code changes (intentional)
- Branch is up to date with main
Once all verification steps pass:
# Push the branch
git push origin fix/issue-1399-account-switch-invariants
# Create PR with title:
# "feat: Add invariant tests for stream creation form state across account changes"
# Link to issue #1399 in PR description
# Include content from PR_SUMMARY.md
# Reference ACCOUNT_SWITCH_INVARIANTS_SPEC.md- Join Telegram group: https://t.me/+u5qmu35nZ7I0OTU1
- Monitor PR for review comments
- Address any requested changes
- Celebrate when merged! 🎉
If you encounter issues during verification:
- Check this document for common issues
- Review test file for inline documentation
- Check ACCOUNT_SWITCH_INVARIANTS_SPEC.md for design decisions
- Ask in issue #1399 comments
- Reach out in Telegram group
For questions about this implementation:
- Issue: #1399
- Telegram: https://t.me/+u5qmu35nZ7I0OTU1