Skip to content

Phase 2: Complete Migration of Existing Tape Tests to Vitest #470

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jun 14, 2025

Conversation

theturtle32
Copy link
Owner

@theturtle32 theturtle32 commented Jun 14, 2025

Phase 2: Test Migration and Helper Infrastructure

This PR implements Phase 2 of the comprehensive WebSocket-Node test suite modernization, migrating from tape to Vitest and establishing robust testing infrastructure.

✅ Completed Work

Phase 2.1: Existing Test Migration

  • All tape tests migrated to Vitest with modern ES module syntax
  • Converted 5 legacy test files to use .mjs extensions and Vitest assertions
  • Enhanced test reliability and maintainability
  • Preserved all existing test functionality while improving structure

Phase 2.2: Comprehensive Test Helper Infrastructure

  • Enhanced TestServerManager: Advanced WebSocket server management with lifecycle control, connection tracking, and configurable test scenarios
  • Mock Infrastructure: Complete set of mock classes (MockWebSocketServer, MockWebSocketClient, MockWebSocketConnection) for isolated testing
  • Test Data Generators: Comprehensive generators for WebSocket frames, payloads, malformed data, and protocol violations
  • Custom Assertion Library: WebSocket-specific assertions for frame validation, connection states, and protocol compliance
  • Improved Test Configuration: Enhanced Vitest config with proper coverage settings and test isolation

🔧 Technical Improvements

  • ES Module Support: All test files use .mjs extensions for proper ES module handling
  • Test Stability: Single-threaded execution prevents port conflicts and ensures reliable test execution
  • Legacy Compatibility: Maintained backward compatibility with existing test patterns
  • Enhanced Coverage: Improved test infrastructure supports comprehensive coverage expansion

📊 Project Status

Phase 2 Status: ✅ COMPLETED

  • ✅ All existing tests migrated to Vitest
  • ✅ Complete test helper infrastructure implemented
  • ⚠️ Parallel test execution deferred for stability (documented decision)

Next Phase: Ready for Phase 3 (Core Component Test Expansion)

🧪 Test Infrastructure Features

TestServerManager

  • Advanced server lifecycle management
  • Connection tracking and message history
  • Configurable test scenarios (echo, broadcast, protocol testing)
  • SSL/TLS support for secure connection testing

Mock Classes

  • Isolated testing without real network dependencies
  • Simulation of various connection states and scenarios
  • Error condition testing capabilities

Test Generators

  • WebSocket frame generation for all frame types
  • Random payload generation (text, binary, JSON)
  • Malformed data generation for edge case testing
  • Protocol violation scenarios for compliance testing

Custom Assertions

  • WebSocket frame validation
  • Connection state verification
  • Protocol compliance checking
  • Performance and memory leak detection

🎯 Impact

This foundation enables comprehensive test coverage expansion in subsequent phases, providing:

  • Reliable Test Infrastructure: Stable, maintainable test utilities
  • Modern Testing Practices: Vitest integration with coverage reporting
  • Scalable Architecture: Ready for extensive test suite expansion
  • Developer Experience: Better debugging, watch mode, and IDE integration

The modernized test suite provides a solid foundation for achieving 85%+ code coverage across all WebSocket-Node components.

🤖 Generated with Claude Code

theturtle32 and others added 2 commits June 13, 2025 17:41
- Migrate websocketFrame.js tests to frame.test.mjs with ES module syntax
- Migrate request.js tests to request.test.mjs with async/await patterns
- Migrate w3cwebsocket.js tests to w3c-websocket.test.mjs with proper event handling
- Migrate regressions.js tests to historical.test.mjs maintaining regression coverage
- Migrate dropBeforeAccept.js tests to connection-lifecycle.test.mjs
- Create enhanced test server helpers with dynamic port allocation
- Create ES module compatible echo server helper
- Configure Vitest for single-threaded execution to avoid port conflicts
- Update TEST_SUITE_MODERNIZATION_PLAN.md to reflect completion status
- All 13 migrated tests passing with 55% code coverage

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
@theturtle32
Copy link
Owner Author

/gemini review

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary of Changes

Hello @theturtle32, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request represents the completion of Phase 2 of the test suite modernization effort. I've migrated all existing tape tests to the Vitest framework, adopting modern ES module syntax and setting up foundational test helper infrastructure for server management and external processes. This establishes a solid base for the comprehensive test expansion planned in Phase 3.

Highlights

  • Test Migration Complete (Phase 2): I've completed Phase 2 of the test suite modernization, successfully migrating all 5 remaining tape-based test files (websocketFrame.js, request.js, w3cwebsocket.js, regressions.js, dropBeforeAccept.js) to new Vitest-based .mjs files.
  • New Test Helper Infrastructure: I've introduced two new helper modules in test/helpers/: test-server.mjs for managing the core WebSocketServer lifecycle within tests, and start-echo-server.mjs for spawning and managing an external echo server process used in some tests.
  • Vitest Configuration Enhancements: I've updated the vitest.config.mjs to configure thread pooling (single thread for potential isolation benefits), and increased the testTimeout and hookTimeout to 15 seconds to accommodate tests involving server startup and network interactions.
  • DevContainer and Dependency Updates: I've added volume mounts to the .devcontainer/devcontainer.json for better persistence of configuration, pnpm state, and node_modules. I also added an onlyBuiltDependencies section to pnpm-workspace.yaml.
  • Modernization Plan Document Update: I've updated the TEST_SUITE_MODERNIZATION_PLAN.md to mark all specific test migration tasks (2.1.1 through 2.1.6) within Phase 2 as completed.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configureGemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request successfully migrates existing tape tests to Vitest, introduces modern ES module syntax for tests, and enhances the testing infrastructure with new helpers and configurations. The changes are well-structured and significantly improve the test suite. My review focuses on minor refinements in the new test files, such as simplifying async test patterns and suggesting ways to make assertions more robust or clearer. These are generally aimed at improving long-term maintainability and readability of the new test code. The core logic of the tests and the migration itself appear solid.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request successfully migrates existing tape-based tests to Vitest, introducing modern ES module syntax and significant infrastructure improvements. My review includes suggestions for enhancing documentation consistency, refining a pnpm configuration, and improving the clarity, robustness, and maintainability of some of the new test cases.

Repository owner deleted a comment from gemini-code-assist bot Jun 14, 2025
theturtle32 and others added 2 commits June 13, 2025 18:26
- Enhanced TestServerManager with advanced server management capabilities
- Added comprehensive mock infrastructure for testing isolation
- Implemented test data generators for various WebSocket scenarios
- Created custom assertion library for WebSocket-specific validations
- Updated project plan to reflect completed Phase 2.2 tasks
- Improved test configuration and setup for better stability

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
- Fix syntax error in generators.mjs: invalidUtf8 variable typo
- Simplify typeof checks in start-echo-server.mjs parameter handling
- Remove redundant async keywords from tests that manually return Promise
- Improve error handling in w3c-websocket.test.mjs event error reporting
- Replace expect(true).toBe(true) with more meaningful assertions

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
@theturtle32
Copy link
Owner Author

/gemini review

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request successfully completes Phase 2 of the test suite modernization, migrating existing tests to Vitest and establishing a comprehensive set of test helper infrastructure including server managers, mocks, generators, and assertions. The new helper utilities are well-designed and provide a strong foundation for future test expansion. Review comments highlight opportunities to improve test reliability and consistency by leveraging the new infrastructure more fully and addressing observed test failures/timeouts.

theturtle32 and others added 2 commits June 13, 2025 18:45
High Priority Fixes:
- Refactor connection-lifecycle test to use async/await with waitForEvent utilities
- Fix timeout issues in request test by simplifying async handling with test utilities
- Both tests now use modern async patterns instead of manual Promise management

Medium Priority Improvements:
- Replace bufferAllocUnsafe/bufferFromString with standard Node.js Buffer methods
- Migrate w3c-websocket tests from startEchoServer to TestServerManager for better isolation
- Migrate historical regression test to use TestServerManager instead of external process
- All tests now use consistent, managed test infrastructure

Low Priority Enhancement:
- Improve error handling in test-server.mjs stop method with environment-aware cleanup

These changes improve test reliability, reduce external dependencies, and provide
better error reporting while maintaining backward compatibility.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
The test was waiting for both 'request' and 'connectFailed' events simultaneously,
but the connectFailed event only occurs after we attempt to accept with the wrong
protocol. Fixed by:

1. Wait for the request event first
2. Verify the requested protocol
3. Set up connectFailed listener
4. Attempt accept with wrong protocol (which throws and triggers connectFailed)
5. Wait for and verify the connectFailed event

All tests now pass consistently.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
@theturtle32
Copy link
Owner Author

/gemini review

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request modernizes the test suite by migrating to Vitest and adding test helpers, mocks, and assertions. A failing test (Connection Lifecycle should handle TCP connection drop before server accepts request) must be addressed. Minor refinements are suggested for some helper utilities to enhance robustness.

Repository owner deleted a comment from gemini-code-assist bot Jun 14, 2025
Repository owner deleted a comment from gemini-code-assist bot Jun 14, 2025
theturtle32 and others added 3 commits June 13, 2025 18:58
The stop() method was trying to call reject(e) but the Promise constructor
only had resolve parameter defined. Added reject parameter to enable proper
error propagation in non-test environments while maintaining test stability.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
- Remove es5-ext from pnpm onlyBuiltDependencies (pure JS library doesn't need building)
- Add clarifying comment for WebSocket PING opcode (0x09) in frame test
- Error message handling for event objects already improved in earlier commits
- Manual counter tracking already eliminated through async/await refactoring

All review feedback has been addressed while maintaining test reliability.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
1. Fix 64-bit payload length handling in expectValidWebSocketFrame assertion
   - Properly handle full 64-bit range by reading high and low 32-bit parts
   - Skip exact length validation for payloads > 4GB due to JS number limitations
   - Maintain validation for normal-sized payloads

2. Fix async server.stop() call in synchronous 'exit' event handler
   - Replace async stop() with synchronous cleanup in 'exit' handler
   - Add 'beforeExit' handler for proper async cleanup when possible
   - Ensure emergency cleanup works for process termination

3. Remove redundant enforceTestTimeout function
   - Vitest provides built-in testTimeout and hookTimeout configurations
   - These are already properly configured in vitest.config.mjs
   - Custom timeout enforcement is unnecessary and less reliable

All review feedback now fully addressed with robust solutions.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
@theturtle32
Copy link
Owner Author

/gemini review

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request represents a significant and well-executed step in modernizing the WebSocket-Node test suite. The migration from tape to Vitest and the establishment of a comprehensive test helper infrastructure are commendable.

Key observations:

  • Test Helper Infrastructure: The new helper files (test/helpers/assertions.mjs, generators.mjs, mocks.mjs, test-server.mjs, test-utils.mjs) are thoughtfully designed and provide a robust foundation for writing effective tests. The TestServerManager is particularly well-implemented, including crucial features like SSL support, connection tracking, and robust cleanup mechanisms using beforeExit and exit process events.
  • Test Migration: The existing tests appear to be correctly migrated to Vitest syntax, preserving their original intent while leveraging modern ES module syntax.
  • Vitest Configuration: The vitest.config.mjs is well-configured, with appropriate settings for single-threaded execution (important for WebSocket test stability), timeouts, and detailed coverage reporting (including include and exclude patterns for both tests and source files).
  • Documentation: The TEST_SUITE_MODERNIZATION_PLAN.md has been updated accurately to reflect the completed work and the decision to defer parallel test execution, which is a sensible choice for initial stability.
  • Code Quality: The newly added JavaScript code is clean, well-organized, and adheres to common best practices. The mocks are comprehensive, and the data generators cover a good range of valid, invalid, and edge-case scenarios for WebSocket frames and protocol interactions.
  • Developer Experience: Changes to .devcontainer/devcontainer.json (adding volume mounts for pnpm and node_modules) and .gitignore (adding test-results.json) are practical improvements.
  • PNPM Configuration: The new pnpm-workspace.yaml with onlyBuiltDependencies is a good touch for optimizing builds if native modules are involved.

Overall, this PR is of high quality and lays excellent groundwork for future test expansion. The attention to detail in the helper utilities, mock implementations, and server management will greatly benefit the project's maintainability and reliability.

@theturtle32 theturtle32 merged commit 3f6694f into v2 Jun 14, 2025
1 check passed
@theturtle32 theturtle32 deleted the phase-2-test-migration branch June 14, 2025 02:09
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