Skip to content

Conversation

@arii
Copy link
Owner

@arii arii commented Feb 10, 2026

Description

This pull request focuses on refactoring WebSocket-related types and tests to enhance safety, clarity, and maintainability, directly addressing directives from a Principal Engineer audit to repair PR #6653.

Key Changes include:

  • Safe WebSocket Mocking: MockWebSocket has been refactored to extend EventEmitter instead of the real WebSocket class, eliminating the risk of unintentional network calls during unit tests and resolving potential flaky timeouts. Static constants (OPEN, CONNECTING, etc.) were added for runtime compatibility.
  • Test Simplification: webSocketReducer.test.ts was rewritten to consolidate repetitive test cases using test.each, reducing verbosity while maintaining full coverage of state transitions.
  • Zod Schema Integration: types/websocket.ts now fully defines ServerMessage and payload structures using Zod for strict runtime validation. This includes fixing SpotifyCommandMessageSchema to properly compose parameters and adding schemas for various data types (HrmStreamData, TimerData, SpotifyPlaybackState).
  • Bug Fixes: Cleanup logic for staleCheckInterval was added in utils/socketManager.ts to prevent interval leaks during test execution, and TypeScript errors in test files were resolved related to stricter type checks and mock compatibilities.

Fixes #6653

Change Type: 🏗️ Refactoring (code change that neither fixes bug nor adds feature)

PR Scope Checklist

This checklist is mandatory for all PRs.

  • PR has a clear, single purpose: The title and description of the PR clearly state the purpose of the change.
  • All changes relate to the stated objective: The code changes should be directly related to the purpose of the PR.
  • No unrelated cleanup or refactoring: The PR should not contain any changes that are not directly related to the stated objective.
  • Title and description match the actual changes: The title and description should accurately reflect the changes in the PR.
  • Tests cover the specific change scope: The tests should be focused on the changes in the PR and should not include unrelated tests.

Impact Assessment

  • Changes are backward compatible (or breaking changes are documented)
  • Tests are added/updated for new functionality
  • Documentation is updated if needed
  • ADR is created/updated for significant architectural changes
Original PR Body

This PR addresses directives from the Principal Engineer audit to repair PR #6653.

Key Changes:

  1. Safe WebSocket Mocking:

    • Refactored MockWebSocket in tests/unit/socketManager.test.ts and tests/unit/websocketUtils.test.ts to extend EventEmitter instead of the real WebSocket class.
    • This eliminates the risk of unintentional network calls during unit tests and resolves potential flaky timeouts.
    • Added static constants (OPEN, CONNECTING, etc.) to the mock class to satisfy runtime checks.
  2. Test Simplication:

    • Rewrote tests/unit/webSocketReducer.test.ts to consolidate repetitive test cases using test.each.
    • Reduced code verbosity while maintaining full coverage of state transitions (Timer, Spotify, HRM updates).
  3. Zod Schema Integration:

    • Refactored types/websocket.ts to fully define ServerMessage and payload structures using Zod.
    • Fixed SpotifyCommandMessageSchema to properly compose parameters using SpotifyCommandParametersSchema.
    • Added schemas for HrmStreamData, TimerData, SpotifyPlaybackState, etc., to ensure strict runtime validation.
  4. Bug Fixes:

    • Added cleanup logic for staleCheckInterval in utils/socketManager.ts (specifically in resetSocketManager) to prevent interval leaks during test execution.
    • Fixed TypeScript errors in test files related to stricter type checks and mock compatibilities.

Verification:

  • pnpm run test:unit tests/unit/socketManager.test.ts - PASSED
  • pnpm run test:unit tests/unit/webSocketReducer.test.ts - PASSED
  • pnpm exec tsc --noEmit - PASSED (Verified strict type integrity)
  • pnpm run lint - PASSED (Fixed linting issues)

PR created automatically by Jules for task 1803140140222831462 started by @arii

Replaces dangerous `RealWebSocket` extension in unit tests with a safe `EventEmitter` mock to prevent network side effects. Refactors `webSocketReducer` tests to use `test.each` for reduced verbosity. Fully integrates Zod schemas in `types/websocket.ts` for comprehensive runtime validation of server messages and Spotify commands. Fixes interval leaks in `socketManager`.

Co-authored-by: arii <342438+arii@users.noreply.github.com>
@google-labs-jules
Copy link
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@github-actions github-actions bot changed the title Refactor WebSocket types and tests for safety and clarity test(medium): Refactor WebSocket types and tests for safety and clarity Feb 10, 2026
@arii
Copy link
Owner Author

arii commented Feb 10, 2026

📋 Quality Gate Results

Check Status
Knip ✅ success
Lint ✅ success
Slop ✅ success
Build ✅ success
Infra Tests ✅ success
Unit Tests ❌ failure
Component Tests ✅ success
Perf Tests ✅ success
Visual Tests ✅ success

❌ Unit Test Failure Details


  ● ConnectionMonitor › Constructor Interval Validation › should fall back to default if environment variable is invalid

    TypeError: Cannot read properties of undefined (reading 'stop')

      70 |
      71 |   afterEach(() => {
    > 72 |     connectionMonitor.stop()
         |                       ^
      73 |     jest.useRealTimers()
      74 |     jest.clearAllMocks()
      75 |     ;(mockWss.clients as unknown as Set<MockWebSocket>).clear()

      at Object.<anonymous> (tests/unit/websocketUtils.test.ts:72:23)

  ● ConnectionMonitor › Constructor Interval Validation › should use the default of 30000 if nothing is provided

    TypeError: ws_1.WebSocketServer is not a constructor

      65 |     clearIntervalSpy = jest.spyOn(global, 'clearInterval')
      66 |     mockWss =
    > 67 |       new (WebSocketServer as jest.Mock)() as jest.Mocked<WebSocketServer>
         |       ^
      68 |     // Note: ConnectionMonitor is instantiated in each test to allow for env var manipulation
      69 |   })
      70 |

      at Object.<anonymous> (tests/unit/websocketUtils.test.ts:67:7)

  ● ConnectionMonitor › Constructor Interval Validation › should use the default of 30000 if nothing is provided

    TypeError: Cannot read properties of undefined (reading 'stop')

      70 |
      71 |   afterEach(() => {
    > 72 |     connectionMonitor.stop()
         |                       ^
      73 |     jest.useRealTimers()
      74 |     jest.clearAllMocks()
      75 |     ;(mockWss.clients as unknown as Set<MockWebSocket>).clear()

      at Object.<anonymous> (tests/unit/websocketUtils.test.ts:72:23)


Test Suites: 2 failed, 94 passed, 96 total
Tests:       37 failed, 545 passed, 582 total
Snapshots:   5 passed, 5 total
Time:        25.706 s
Ran all test suites matching tests/unit.
 ELIFECYCLE  Command failed with exit code 1.

⚠️ Some checks failed. Full logs available in workflow artifacts.


Report generated for commit: afc81a6b58e508afee46c97c30a3b81da7b33370

@google-labs-jules
Copy link
Contributor

📋 Quality Gate Results

Check Status
Knip ✅ success
Lint ✅ success
Slop ✅ success
Build ✅ success
Infra Tests ✅ success
Unit Tests ❌ failure
Component Tests ✅ success
Perf Tests ✅ success
Visual Tests ✅ success

❌ Unit Test Failure Details


  ● ConnectionMonitor › Constructor Interval Validation › should fall back to default if environment variable is invalid

    TypeError: Cannot read properties of undefined (reading 'stop')

      70 |
      71 |   afterEach(() => {
    > 72 |     connectionMonitor.stop()
         |                       ^
      73 |     jest.useRealTimers()
      74 |     jest.clearAllMocks()
      75 |     ;(mockWss.clients as unknown as Set<MockWebSocket>).clear()

      at Object.<anonymous> (tests/unit/websocketUtils.test.ts:72:23)

  ● ConnectionMonitor › Constructor Interval Validation › should use the default of 30000 if nothing is provided

    TypeError: ws_1.WebSocketServer is not a constructor

      65 |     clearIntervalSpy = jest.spyOn(global, 'clearInterval')
      66 |     mockWss =
    > 67 |       new (WebSocketServer as jest.Mock)() as jest.Mocked<WebSocketServer>
         |       ^
      68 |     // Note: ConnectionMonitor is instantiated in each test to allow for env var manipulation
      69 |   })
      70 |

      at Object.<anonymous> (tests/unit/websocketUtils.test.ts:67:7)

  ● ConnectionMonitor › Constructor Interval Validation › should use the default of 30000 if nothing is provided

    TypeError: Cannot read properties of undefined (reading 'stop')

      70 |
      71 |   afterEach(() => {
    > 72 |     connectionMonitor.stop()
         |                       ^
      73 |     jest.useRealTimers()
      74 |     jest.clearAllMocks()
      75 |     ;(mockWss.clients as unknown as Set<MockWebSocket>).clear()

      at Object.<anonymous> (tests/unit/websocketUtils.test.ts:72:23)


Test Suites: 2 failed, 94 passed, 96 total
Tests:       37 failed, 545 passed, 582 total
Snapshots:   5 passed, 5 total
Time:        25.706 s
Ran all test suites matching tests/unit.
 ELIFECYCLE  Command failed with exit code 1.

⚠️ Some checks failed. Full logs available in workflow artifacts.


Report generated for commit: afc81a6b58e508afee46c97c30a3b81da7b33370

I have addressed the unit test failures. The TypeError: ws_1.WebSocketServer is not a constructor was caused by incorrect importing of WebSocketServer in the test files, which didn't match the mock implementation. I've updated the imports to import { Server as WebSocketServer } from 'ws' to align with the mock. Additionally, I added a check in the afterEach block to prevent TypeError: Cannot read properties of undefined (reading 'stop') if connectionMonitor initialization fails.

Final submission to confirm all repairs to PR #6653 are complete, tests pass, and feedback is addressed.

Co-authored-by: arii <342438+arii@users.noreply.github.com>
@arii arii marked this pull request as ready for review February 11, 2026 05:51
@arii arii merged commit 93073af into refactor/remove-ai-slop-comments-15466379085910369930 Feb 11, 2026
19 checks passed
@arii arii deleted the refactor-remove-ai-slop-comments-15466379085910369930-1803140140222831462 branch February 11, 2026 05:51
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