Skip to content

test: add unit tests for node-agent heartbeat loop (WOP-2133)#782

Merged
TSavo merged 3 commits intomainfrom
agent/coder-2133/wop-2133-heartbeat-tests
Mar 14, 2026
Merged

test: add unit tests for node-agent heartbeat loop (WOP-2133)#782
TSavo merged 3 commits intomainfrom
agent/coder-2133/wop-2133-heartbeat-tests

Conversation

@TSavo
Copy link
Contributor

@TSavo TSavo commented Mar 14, 2026

Summary

Closes WOP-2133

  • Add src/node-agent/heartbeat.test.ts with 11 unit tests covering the NodeAgent heartbeat loop and WebSocket reconnect behavior
  • Tests use vi.hoisted() + vi.mock("ws") with a FakeWebSocket class (custom minimal event emitter + static OPEN constant) to control WebSocket lifecycle
  • Add server.deps.inline: ["@wopr-network/platform-core"] to vitest.config.ts so vi.mock("ws") intercepts inside the published package

Tests cover:

  • First heartbeat fires immediately on open event, then at configured interval
  • Error in collectHeartbeat (Docker daemon down) doesn't crash the loop — heartbeat continues
  • No heartbeat sent when readyState !== OPEN
  • stop() clears interval and closes WebSocket
  • stop() is idempotent (safe to call multiple times)
  • Heartbeat timer cleared on WebSocket close before reconnect
  • Exponential backoff: 1s → 2s → 4s on close events
  • Backoff capped at 30s
  • Delay resets to 1s after successful open
  • No reconnect after stop()
  • Error event handled gracefully (no crash)

Test plan

  • npm run check passes (biome + tsc + raw-sql)
  • npx vitest run src/node-agent/heartbeat.test.ts — 11/11 pass

Generated with Claude Code


Open with Devin

Summary by Sourcery

Add comprehensive unit tests for the NodeAgent heartbeat loop and WebSocket reconnect backoff behavior, and adjust Vitest config to support mocking WebSocket usage inside the platform-core package.

Build:

  • Configure Vitest to inline the @wopr-network/platform-core dependency so WebSocket mocks are applied within NodeAgent during testing.

Tests:

  • Add heartbeat loop tests validating immediate and periodic heartbeats, readiness checks, error resilience, and proper cleanup on stop and WebSocket close.
  • Add WebSocket reconnect backoff tests covering exponential delays, maximum backoff cap, delay reset on successful reconnect, and no reconnect after stop().

Note

Add unit tests for node-agent heartbeat loop and WebSocket reconnect backoff

  • Adds a Vitest test suite in heartbeat.test.ts covering heartbeat loop behavior (immediate send on open, periodic sends, error resilience, stop/clear timers) and exponential reconnect backoff (cap at 30s, reset on open, no reconnect after stop).
  • Uses vi.hoisted to define a FakeWebSocket and intercept the ws module, alongside mocks for node:os, node:fs/promises, and Docker via mockDockerode.
  • Adds vitest.config.heartbeat.ts which inlines @wopr-network/platform-core so that vi.mock('ws') correctly intercepts the WebSocket import within NodeAgent.
  • Excludes the heartbeat test from the default vitest.config.ts run and adds a second vitest invocation to npm test and npm run test:coverage using the heartbeat-specific config.

Macroscope summarized fc1556d.

Copilot AI review requested due to automatic review settings March 14, 2026 02:25
@linear
Copy link

linear bot commented Mar 14, 2026

@sourcery-ai
Copy link

sourcery-ai bot commented Mar 14, 2026

Reviewer's Guide

Adds a comprehensive vitest suite for the NodeAgent WebSocket heartbeat loop and reconnect/backoff behavior, along with a vitest config tweak to ensure the ws mock works when NodeAgent is imported from the published @wopr-network/platform-core package.

Sequence diagram for NodeAgent heartbeat loop behavior under test

sequenceDiagram
  actor Tester
  participant NodeAgent
  participant DockerManager
  participant WebSocket
  participant Timer

  Tester->>NodeAgent: new NodeAgent(config, dockerManager)
  Tester->>NodeAgent: start()
  NodeAgent->>DockerManager: initialize()
  NodeAgent->>WebSocket: new WebSocket(platformUrl)

  Note over WebSocket: open event triggers heartbeat
  Tester->>WebSocket: emit("open")
  WebSocket-->>NodeAgent: open
  NodeAgent->>Timer: startHeartbeatInterval(heartbeatIntervalMs)
  NodeAgent->>WebSocket: send(heartbeatPayload)

  loop every heartbeatIntervalMs
    Timer-->>NodeAgent: heartbeatTick
    NodeAgent->>WebSocket: check readyState
    alt readyState == OPEN
      NodeAgent->>DockerManager: collectHeartbeat()
      DockerManager-->>NodeAgent: heartbeatMetrics or error
      NodeAgent->>WebSocket: send(heartbeatPayload)
    else readyState != OPEN
      NodeAgent-->>NodeAgent: skip send
    end
  end

  Note over NodeAgent,WebSocket: collectHeartbeat error does not stop loop

  Tester->>NodeAgent: stop()
  NodeAgent->>Timer: clearHeartbeatInterval()
  NodeAgent->>WebSocket: close()
  Timer-->>NodeAgent: no further ticks
  NodeAgent-->>Tester: stop() is idempotent
Loading

Sequence diagram for NodeAgent WebSocket reconnect and backoff behavior under test

sequenceDiagram
  actor Tester
  participant NodeAgent
  participant WebSocket as WebSocket_n
  participant ReconnectTimer

  Tester->>NodeAgent: start()
  NodeAgent->>WebSocket: connect()
  Tester->>WebSocket: emit("open")
  WebSocket-->>NodeAgent: open
  NodeAgent-->>NodeAgent: resetReconnectDelayTo1s()

  loop repeated closes
    Note over WebSocket,NodeAgent: WebSocket closes unexpectedly
    Tester->>WebSocket: emit("close")
    WebSocket-->>NodeAgent: close
    NodeAgent->>ReconnectTimer: scheduleReconnect(currentDelayMs)

    alt stop() was called
      Tester->>NodeAgent: stop()
      NodeAgent->>ReconnectTimer: cancelReconnect()
      ReconnectTimer-->>NodeAgent: no reconnect
    else stop() not called
      ReconnectTimer-->>NodeAgent: reconnectDue
      NodeAgent->>WebSocket: new WebSocket(platformUrl)
      NodeAgent-->>NodeAgent: increaseDelayWithExponentialBackoff()
      alt delayExceedsMax
        NodeAgent-->>NodeAgent: capDelayAt30s()
      end
      Tester->>WebSocket: emit("open")
      WebSocket-->>NodeAgent: open
      NodeAgent-->>NodeAgent: resetReconnectDelayTo1s()
    end
  end

  Note over NodeAgent,WebSocket: error events are logged/handled without crash
Loading

Class diagram for test doubles and collaborators in NodeAgent heartbeat tests

classDiagram
  class SimpleEmitter {
    -_listeners: Record
    +on(event: string, fn: function) SimpleEmitter
    +emit(event: string, args: unknown) boolean
  }

  class FakeWebSocket {
    <<mock>>
    +static OPEN: number
    +static CLOSING: number
    +static CLOSED: number
    +static CONNECTING: number
    +readyState: number
    +send(): void
    +close(): void
    +constructor(url: string, opts: unknown)
  }

  class DockerManager {
    <<external>>
    +constructor(docker: object)
  }

  class NodeAgent {
    <<external>>
    +constructor(config: object, dockerManager: DockerManager)
    +start(): Promise~void~
    +stop(): void
  }

  class TestHelpers {
    <<utility>>
    +mockDockerode(): object
    +validConfig(overrides: object): object
    +getWsInstances(): Array
    +resetWsInstances(): void
  }

  SimpleEmitter <|-- FakeWebSocket
  TestHelpers ..> FakeWebSocket
  TestHelpers ..> DockerManager
  TestHelpers ..> NodeAgent
  NodeAgent --> DockerManager
  NodeAgent --> FakeWebSocket
Loading

File-Level Changes

Change Details Files
Introduce FakeWebSocket and supporting mocks to deterministically drive NodeAgent’s heartbeat and reconnect behavior in tests.
  • Define a hoisted FakeWebSocket class plus helpers to track WebSocket instances and reset state between tests.
  • Mock the ws module to use FakeWebSocket so NodeAgent’s internal WebSocket usage is fully controlled.
  • Stub node:os, node:fs/promises, and node:fs to remove host‑specific side effects and filesystem/network dependencies in unit tests.
  • Provide mockDockerode and validConfig helpers to construct a DockerManager and NodeAgent with stable behavior for all tests.
src/node-agent/heartbeat.test.ts
Add unit tests that cover the NodeAgent heartbeat loop, stop semantics, and WebSocket reconnect/backoff logic.
  • Verify immediate heartbeat send on WebSocket open and subsequent sends at the configured interval using fake timers.
  • Ensure errors in collectHeartbeat (e.g., Docker failures) don’t crash the loop and that heartbeats continue.
  • Assert that heartbeats are skipped when readyState is not OPEN and that stop() clears timers, closes the socket, and is idempotent.
  • Test that heartbeat timers are cleared on close, no reconnect occurs after stop(), and WebSocket error events are handled without throwing.
  • Validate exponential reconnect backoff (1s → 2s → 4s…), a 30s maximum delay, and reset of the backoff delay to 1s after a successful open.
src/node-agent/heartbeat.test.ts
Adjust Vitest configuration so ws mocking works when NodeAgent is imported from the published package.
  • Configure test.server.deps.inline to inline @wopr-network/platform-core, ensuring vi.mock("ws") is applied inside the package code under test.
vitest.config.ts

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@coderabbitai
Copy link

coderabbitai bot commented Mar 14, 2026

Warning

Rate limit exceeded

@TSavo has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 3 minutes and 50 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 536d1c54-acdc-456e-9cb3-fe02a9469dc5

📥 Commits

Reviewing files that changed from the base of the PR and between c5e0d6e and fc1556d.

📒 Files selected for processing (4)
  • package.json
  • src/node-agent/heartbeat.test.ts
  • vitest.config.heartbeat.ts
  • vitest.config.ts
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch agent/coder-2133/wop-2133-heartbeat-tests
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - I've left some high level feedback:

  • The beforeEach/afterEach blocks for the two describe suites are nearly identical (fake timers, system time, resetWsInstances, fetch stub); consider extracting a shared setup helper or higher-level describe to reduce duplication and keep test configuration consistent.
  • In FakeWebSocket, readyState is initialized as OPEN (1); if the real client code depends on the CONNECTING → OPEN transition, you might want to start at CONNECTING (0) and rely only on the explicit open event to more closely mirror real WebSocket behavior.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The `beforeEach`/`afterEach` blocks for the two `describe` suites are nearly identical (fake timers, system time, `resetWsInstances`, `fetch` stub); consider extracting a shared setup helper or higher-level `describe` to reduce duplication and keep test configuration consistent.
- In `FakeWebSocket`, `readyState` is initialized as `OPEN` (1); if the real client code depends on the `CONNECTING → OPEN` transition, you might want to start at `CONNECTING` (0) and rely only on the explicit `open` event to more closely mirror real WebSocket behavior.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

devin-ai-integration[bot]

This comment was marked as resolved.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds a focused test suite around the NodeAgent WebSocket heartbeat/reconnect behavior (from @wopr-network/platform-core) and adjusts Vitest configuration so vi.mock("ws") can reliably intercept that dependency during tests.

Changes:

  • Configure Vitest to inline @wopr-network/platform-core so deep dependency mocks (notably ws) apply inside NodeAgent.
  • Add src/node-agent/heartbeat.test.ts covering heartbeat scheduling, stop behavior, reconnect exponential backoff, and delay reset/capping.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
vitest.config.ts Inlines @wopr-network/platform-core to allow mocking ws within platform-core’s NodeAgent during tests.
src/node-agent/heartbeat.test.ts New unit tests validating NodeAgent heartbeat loop behavior and WebSocket reconnect backoff logic with extensive module mocks.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

vitest.config.ts Outdated
Comment on lines +8 to +9
// Inline platform-core so that vi.mock("ws") intercepts it inside NodeAgent.
inline: ["@wopr-network/platform-core"],
Comment on lines +251 to +262
await agent.start();
const ws = getWsInstances()[0];
ws.emit("open");
await vi.advanceTimersByTimeAsync(0);

expect(ws.send).toHaveBeenCalledTimes(1);

agent.stop();
expect(ws.close).toHaveBeenCalled();

await vi.advanceTimersByTimeAsync(15000);
expect(ws.send).toHaveBeenCalledTimes(1);
Comment on lines +265 to +281
it("stop() is safe to call multiple times without throwing", async () => {
const config = validConfig();
const { docker } = mockDockerode();
const dockerManager = new DockerManager(docker as never);
const agent = new NodeAgent(config, dockerManager);

await agent.start();
const ws = getWsInstances()[0];
ws.emit("open");
await vi.advanceTimersByTimeAsync(0);

expect(() => {
agent.stop();
agent.stop();
}).not.toThrow();
expect(ws.close).toHaveBeenCalledTimes(1);
});
Comment on lines +445 to +452
await agent.start();
expect(getWsInstances()).toHaveLength(1);

agent.stop();
getWsInstances()[0].emit("close");

await vi.advanceTimersByTimeAsync(60_000);
expect(getWsInstances()).toHaveLength(1);
@github-actions
Copy link

github-actions bot commented Mar 14, 2026

Coverage Report

Status Category Percentage Covered / Total
🔵 Lines 77.45% (🎯 70%) 3718 / 4800
🔵 Statements 77.1% (🎯 70%) 3920 / 5084
🔵 Functions 72.92% (🎯 70%) 598 / 820
🔵 Branches 65.91% (🎯 65%) 1586 / 2406
File CoverageNo changed files found.
Generated in workflow #2554 for commit fc1556d by the Vitest Coverage Report Action

@greptile-apps
Copy link

greptile-apps bot commented Mar 14, 2026

Greptile Summary

This PR adds 11 unit tests for the NodeAgent heartbeat loop and WebSocket reconnect backoff in @wopr-network/platform-core, and makes a global change to vitest.config.ts to enable vi.mock("ws") to intercept inside the published package. The test coverage is well-structured and exercises the key behaviors (immediate first heartbeat, error resilience, exponential backoff, cap at 30 s, backoff reset on reconnect, idempotent stop()).

Key issues found:

  • FakeWebSocket is missing EventEmitter methods (off, removeListener, once, removeAllListeners). The real ws.WebSocket inherits all of these from Node's EventEmitter. If NodeAgent uses any of them — for example ws.once("open", …) to reset the backoff counter, or ws.removeListener(…) in the close handler — every test in this file will throw TypeError at runtime rather than producing assertion failures, making the root cause very hard to diagnose. This should be addressed before merging.

  • server.deps.inline is a global vitest config change that applies to all 170+ test files in the suite, not just the new heartbeat tests. This increases Vite bundling overhead on every test run and creates a risk of unexpected mock interference and module instance identity changes in unrelated tests that also import from @wopr-network/platform-core. Scoping the setting to a per-file or workspace-isolated config would be safer.

  • Misleading comment on reconnect timing in the "heartbeat timer is cleared" test — the reconnect fires at t+1s from close, not at "total 6s", so the boundary assertion is weaker than it appears.

Confidence Score: 3/5

  • Safe to merge if the SimpleEmitter gap is confirmed not to be an issue in the current NodeAgent implementation, but the global vitest.config.ts change warrants extra caution before merging.
  • The test logic is generally sound and the coverage is comprehensive. The vitest.config.ts change is globally scoped and could silently affect unrelated tests across a large suite. The missing EventEmitter methods in FakeWebSocket are a latent brittleness risk — if NodeAgent already uses off/once, all 11 tests would currently be failing (and they're not, per the PR), but any future change to NodeAgent that starts using those methods would break all these tests with confusing errors. Neither issue is a hard blocker, but together they lower confidence that the test infrastructure is robust.
  • vitest.config.ts (global scope of server.deps.inline) and src/node-agent/heartbeat.test.ts (SimpleEmitter missing EventEmitter interface completeness)

Important Files Changed

Filename Overview
src/node-agent/heartbeat.test.ts New test file adding 11 unit tests for NodeAgent heartbeat loop and WebSocket reconnect backoff. Good overall coverage, but FakeWebSocket's SimpleEmitter is missing off/removeListener/once methods that the real ws.WebSocket inherits from EventEmitter, making tests brittle if NodeAgent uses those methods. One test also has a misleading comment on reconnect timing.
vitest.config.ts Adds server.deps.inline: ["@wopr-network/platform-core"] globally, affecting all 170+ test files that import from platform-core. This introduces bundle overhead across the full test suite and creates a risk of unexpected mock interference and module identity changes in unrelated tests.

Sequence Diagram

sequenceDiagram
    participant T as Test
    participant A as NodeAgent
    participant FWS as FakeWebSocket

    T->>A: new NodeAgent(config, dockerManager)
    T->>A: agent.start()
    A->>FWS: new WebSocket(url) [intercepted by vi.mock]
    FWS-->>A: ws instance stored
    T->>FWS: ws.emit("open")
    A->>A: startHeartbeat() → collectHeartbeat()
    A->>FWS: ws.send(JSON heartbeat) [immediate]
    loop every heartbeatIntervalMs
        A->>A: collectHeartbeat()
        A->>FWS: ws.send(JSON heartbeat)
    end
    T->>FWS: ws.emit("close")
    A->>A: stopHeartbeat() → clearInterval
    A->>A: scheduleReconnect(delay)
    Note over A: delay doubles on each close (1s→2s→4s…cap 30s)<br/>delay resets to 1s on successful open
    A->>FWS: new WebSocket(url) [after delay]
    T->>A: agent.stop()
    A->>FWS: ws.close()
    A->>A: stopped=true → no reconnect
Loading
Prompt To Fix All With AI
This is a comment left during a code review.
Path: src/node-agent/heartbeat.test.ts
Line: 13-27

Comment:
**`SimpleEmitter` missing `off`/`removeListener`/`once`**

`SimpleEmitter` only implements `on` and `emit`. The real `ws.WebSocket` extends Node's `EventEmitter`, which also exposes `removeListener`, `off`, `once`, and `removeAllListeners`. If `NodeAgent` calls any of these (e.g. to tear down listeners on close before reconnecting, or to register a one-shot `once("open", ...)` handler), the tests will throw a `TypeError` at runtime and the assertion results become unreliable.

For example, if `NodeAgent` does:
```ts
this.ws.once("open", () => { /* reset backoff */ });
```
that would immediately throw `TypeError: this.ws.once is not a function`, causing the tests to fail with an error rather than an assertion failure, making the root cause harder to diagnose.

Consider adding the missing `EventEmitter` methods to `SimpleEmitter`, or extending Node's built-in `EventEmitter` inside the `vi.hoisted` block:

```ts
class FakeWS {
  static OPEN = 1;
  static CLOSING = 2;
  static CLOSED = 3;
  static CONNECTING = 0;

  readyState = 1;
  send = vi.fn();
  close = vi.fn();

  private _emitter = new (require("node:events").EventEmitter)();

  on(event: string, fn: (...args: unknown[]) => void) { this._emitter.on(event, fn); return this; }
  once(event: string, fn: (...args: unknown[]) => void) { this._emitter.once(event, fn); return this; }
  off(event: string, fn: (...args: unknown[]) => void) { this._emitter.off(event, fn); return this; }
  removeListener(event: string, fn: (...args: unknown[]) => void) { this._emitter.removeListener(event, fn); return this; }
  emit(event: string, ...args: unknown[]) { return this._emitter.emit(event, ...args); }

  constructor(_url: string, _opts?: unknown) {
    instances.push(this);
  }
}
```

Note: `require()` is fine inside `vi.hoisted` because hoisted code runs in a CJS-compatible context before ESM module resolution.

How can I resolve this? If you propose a fix, please make it concise.

---

This is a comment left during a code review.
Path: vitest.config.ts
Line: 6-11

Comment:
**Global `server.deps.inline` affects all 170+ test files**

`server.deps.inline` is a global Vitest setting — every test in the suite now bundles `@wopr-network/platform-core` through Vite. There are 170+ other test files in `src/` and `tests/` that already import from `@wopr-network/platform-core` (including `src/api/routes/fleet.test.ts`, `src/trpc/routers/billing.test.ts`, `src/fleet/services.test.ts`, and many others). Consequences:

1. **Build-time overhead**: All tests now incur Vite bundling for platform-core on every run, adding startup latency to the full suite.
2. **Mock bleed risk**: Any test file in the suite that also calls `vi.mock("ws")` — even for unrelated reasons — will now intercept `ws` inside platform-core, which may or may not be the intent. Currently no other test file mocks `ws`, but this creates a hidden dependency that is easy to break silently.
3. **Module identity changes**: Inlining causes Vite to re-process the package, which can change the module instance identity (class instances, singletons). Tests that check `instanceof` against a platform-core class may start failing unexpectedly.

Consider scoping this to only the heartbeat tests by extracting a `vitest.config.heartbeat.ts` for this file, or by using Vitest's `project` workspace isolation, so the side-effects are contained to the tests that require it.

How can I resolve this? If you propose a fix, please make it concise.

---

This is a comment left during a code review.
Path: src/node-agent/heartbeat.test.ts
Line: 300-306

Comment:
**Misleading comment on reconnect timing**

The comment says "Reconnect fires 1s after close (total 6s from close)", but the reconnect timer starts immediately when `ws1.emit("close")` is called. With a 1 s initial backoff delay, the reconnect fires at **t+1000 ms from close** — which is already within the first `advanceTimersByTimeAsync(5000)` call. By the time the second advance runs, `getWsInstances()` already has length 2.

The assertion is correct, but the comment incorrectly implies the reconnect happens 6 s after close. This makes the test read as though it's verifying a 6 s delay, when it is actually verifying that the reconnect happened sometime during the preceding 5 s advance. Consider splitting the advance to precisely verify the 1 s boundary:

```ts
// At 999ms — reconnect not yet fired
await vi.advanceTimersByTimeAsync(999);
expect(ws1.send).toHaveBeenCalledTimes(1); // still cleared
expect(getWsInstances()).toHaveLength(1);

// At 1000ms — reconnect fires
await vi.advanceTimersByTimeAsync(1);
expect(getWsInstances()).toHaveLength(2);
```

This also makes the test self-documenting without needing the comment.

How can I resolve this? If you propose a fix, please make it concise.

Last reviewed commit: 6f23b4a

Comment on lines +13 to +27
class SimpleEmitter {
private _listeners: Record<string, Array<(...args: unknown[]) => void>> = {};

on(event: string, fn: (...args: unknown[]) => void) {
if (!this._listeners[event]) this._listeners[event] = [];
this._listeners[event].push(fn);
return this;
}

emit(event: string, ...args: unknown[]): boolean {
const fns = this._listeners[event] ?? [];
for (const fn of fns) fn(...args);
return fns.length > 0;
}
}
Copy link

Choose a reason for hiding this comment

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

SimpleEmitter missing off/removeListener/once

SimpleEmitter only implements on and emit. The real ws.WebSocket extends Node's EventEmitter, which also exposes removeListener, off, once, and removeAllListeners. If NodeAgent calls any of these (e.g. to tear down listeners on close before reconnecting, or to register a one-shot once("open", ...) handler), the tests will throw a TypeError at runtime and the assertion results become unreliable.

For example, if NodeAgent does:

this.ws.once("open", () => { /* reset backoff */ });

that would immediately throw TypeError: this.ws.once is not a function, causing the tests to fail with an error rather than an assertion failure, making the root cause harder to diagnose.

Consider adding the missing EventEmitter methods to SimpleEmitter, or extending Node's built-in EventEmitter inside the vi.hoisted block:

class FakeWS {
  static OPEN = 1;
  static CLOSING = 2;
  static CLOSED = 3;
  static CONNECTING = 0;

  readyState = 1;
  send = vi.fn();
  close = vi.fn();

  private _emitter = new (require("node:events").EventEmitter)();

  on(event: string, fn: (...args: unknown[]) => void) { this._emitter.on(event, fn); return this; }
  once(event: string, fn: (...args: unknown[]) => void) { this._emitter.once(event, fn); return this; }
  off(event: string, fn: (...args: unknown[]) => void) { this._emitter.off(event, fn); return this; }
  removeListener(event: string, fn: (...args: unknown[]) => void) { this._emitter.removeListener(event, fn); return this; }
  emit(event: string, ...args: unknown[]) { return this._emitter.emit(event, ...args); }

  constructor(_url: string, _opts?: unknown) {
    instances.push(this);
  }
}

Note: require() is fine inside vi.hoisted because hoisted code runs in a CJS-compatible context before ESM module resolution.

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/node-agent/heartbeat.test.ts
Line: 13-27

Comment:
**`SimpleEmitter` missing `off`/`removeListener`/`once`**

`SimpleEmitter` only implements `on` and `emit`. The real `ws.WebSocket` extends Node's `EventEmitter`, which also exposes `removeListener`, `off`, `once`, and `removeAllListeners`. If `NodeAgent` calls any of these (e.g. to tear down listeners on close before reconnecting, or to register a one-shot `once("open", ...)` handler), the tests will throw a `TypeError` at runtime and the assertion results become unreliable.

For example, if `NodeAgent` does:
```ts
this.ws.once("open", () => { /* reset backoff */ });
```
that would immediately throw `TypeError: this.ws.once is not a function`, causing the tests to fail with an error rather than an assertion failure, making the root cause harder to diagnose.

Consider adding the missing `EventEmitter` methods to `SimpleEmitter`, or extending Node's built-in `EventEmitter` inside the `vi.hoisted` block:

```ts
class FakeWS {
  static OPEN = 1;
  static CLOSING = 2;
  static CLOSED = 3;
  static CONNECTING = 0;

  readyState = 1;
  send = vi.fn();
  close = vi.fn();

  private _emitter = new (require("node:events").EventEmitter)();

  on(event: string, fn: (...args: unknown[]) => void) { this._emitter.on(event, fn); return this; }
  once(event: string, fn: (...args: unknown[]) => void) { this._emitter.once(event, fn); return this; }
  off(event: string, fn: (...args: unknown[]) => void) { this._emitter.off(event, fn); return this; }
  removeListener(event: string, fn: (...args: unknown[]) => void) { this._emitter.removeListener(event, fn); return this; }
  emit(event: string, ...args: unknown[]) { return this._emitter.emit(event, ...args); }

  constructor(_url: string, _opts?: unknown) {
    instances.push(this);
  }
}
```

Note: `require()` is fine inside `vi.hoisted` because hoisted code runs in a CJS-compatible context before ESM module resolution.

How can I resolve this? If you propose a fix, please make it concise.

vitest.config.ts Outdated
Comment on lines +6 to +11
server: {
deps: {
// Inline platform-core so that vi.mock("ws") intercepts it inside NodeAgent.
inline: ["@wopr-network/platform-core"],
},
},
Copy link

Choose a reason for hiding this comment

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

Global server.deps.inline affects all 170+ test files

server.deps.inline is a global Vitest setting — every test in the suite now bundles @wopr-network/platform-core through Vite. There are 170+ other test files in src/ and tests/ that already import from @wopr-network/platform-core (including src/api/routes/fleet.test.ts, src/trpc/routers/billing.test.ts, src/fleet/services.test.ts, and many others). Consequences:

  1. Build-time overhead: All tests now incur Vite bundling for platform-core on every run, adding startup latency to the full suite.
  2. Mock bleed risk: Any test file in the suite that also calls vi.mock("ws") — even for unrelated reasons — will now intercept ws inside platform-core, which may or may not be the intent. Currently no other test file mocks ws, but this creates a hidden dependency that is easy to break silently.
  3. Module identity changes: Inlining causes Vite to re-process the package, which can change the module instance identity (class instances, singletons). Tests that check instanceof against a platform-core class may start failing unexpectedly.

Consider scoping this to only the heartbeat tests by extracting a vitest.config.heartbeat.ts for this file, or by using Vitest's project workspace isolation, so the side-effects are contained to the tests that require it.

Prompt To Fix With AI
This is a comment left during a code review.
Path: vitest.config.ts
Line: 6-11

Comment:
**Global `server.deps.inline` affects all 170+ test files**

`server.deps.inline` is a global Vitest setting — every test in the suite now bundles `@wopr-network/platform-core` through Vite. There are 170+ other test files in `src/` and `tests/` that already import from `@wopr-network/platform-core` (including `src/api/routes/fleet.test.ts`, `src/trpc/routers/billing.test.ts`, `src/fleet/services.test.ts`, and many others). Consequences:

1. **Build-time overhead**: All tests now incur Vite bundling for platform-core on every run, adding startup latency to the full suite.
2. **Mock bleed risk**: Any test file in the suite that also calls `vi.mock("ws")` — even for unrelated reasons — will now intercept `ws` inside platform-core, which may or may not be the intent. Currently no other test file mocks `ws`, but this creates a hidden dependency that is easy to break silently.
3. **Module identity changes**: Inlining causes Vite to re-process the package, which can change the module instance identity (class instances, singletons). Tests that check `instanceof` against a platform-core class may start failing unexpectedly.

Consider scoping this to only the heartbeat tests by extracting a `vitest.config.heartbeat.ts` for this file, or by using Vitest's `project` workspace isolation, so the side-effects are contained to the tests that require it.

How can I resolve this? If you propose a fix, please make it concise.

Comment on lines +300 to +306
// Advance past heartbeat interval — old timer cleared, no sends on ws1
await vi.advanceTimersByTimeAsync(5000);
expect(ws1.send).toHaveBeenCalledTimes(1);

// Reconnect fires 1s after close (total 6s from close)
await vi.advanceTimersByTimeAsync(1000);
expect(getWsInstances()).toHaveLength(2);
Copy link

Choose a reason for hiding this comment

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

Misleading comment on reconnect timing

The comment says "Reconnect fires 1s after close (total 6s from close)", but the reconnect timer starts immediately when ws1.emit("close") is called. With a 1 s initial backoff delay, the reconnect fires at t+1000 ms from close — which is already within the first advanceTimersByTimeAsync(5000) call. By the time the second advance runs, getWsInstances() already has length 2.

The assertion is correct, but the comment incorrectly implies the reconnect happens 6 s after close. This makes the test read as though it's verifying a 6 s delay, when it is actually verifying that the reconnect happened sometime during the preceding 5 s advance. Consider splitting the advance to precisely verify the 1 s boundary:

// At 999ms — reconnect not yet fired
await vi.advanceTimersByTimeAsync(999);
expect(ws1.send).toHaveBeenCalledTimes(1); // still cleared
expect(getWsInstances()).toHaveLength(1);

// At 1000ms — reconnect fires
await vi.advanceTimersByTimeAsync(1);
expect(getWsInstances()).toHaveLength(2);

This also makes the test self-documenting without needing the comment.

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/node-agent/heartbeat.test.ts
Line: 300-306

Comment:
**Misleading comment on reconnect timing**

The comment says "Reconnect fires 1s after close (total 6s from close)", but the reconnect timer starts immediately when `ws1.emit("close")` is called. With a 1 s initial backoff delay, the reconnect fires at **t+1000 ms from close** — which is already within the first `advanceTimersByTimeAsync(5000)` call. By the time the second advance runs, `getWsInstances()` already has length 2.

The assertion is correct, but the comment incorrectly implies the reconnect happens 6 s after close. This makes the test read as though it's verifying a 6 s delay, when it is actually verifying that the reconnect happened sometime during the preceding 5 s advance. Consider splitting the advance to precisely verify the 1 s boundary:

```ts
// At 999ms — reconnect not yet fired
await vi.advanceTimersByTimeAsync(999);
expect(ws1.send).toHaveBeenCalledTimes(1); // still cleared
expect(getWsInstances()).toHaveLength(1);

// At 1000ms — reconnect fires
await vi.advanceTimersByTimeAsync(1);
expect(getWsInstances()).toHaveLength(2);
```

This also makes the test self-documenting without needing the comment.

How can I resolve this? If you propose a fix, please make it concise.

@TSavo
Copy link
Contributor Author

TSavo commented Mar 14, 2026

/improve

@TSavo
Copy link
Contributor Author

TSavo commented Mar 14, 2026

wopr-auto review: ISSUES FOUND.

Three issues to address before merge:

1. Missing try/finally in three tests (timer leak risk)

Tests at lines 245-263, 265-281, and 439-453 call agent.start() but do not wrap agent.stop() in a finally block. If any assertion throws before agent.stop() runs, heartbeat intervals and WebSocket reconnect timers leak into subsequent tests, causing flaky failures. All three need:

try {
  await agent.start();
  // assertions...
} finally {
  agent.stop();
}

2. SimpleEmitter missing once/off/removeListener

SimpleEmitter only implements on and emit. If NodeAgent calls this.ws.once("open", ...) (a common pattern to reset backoff after reconnect), tests will throw TypeError: this.ws.once is not a function at runtime. Add the missing EventEmitter methods or extend Node's built-in EventEmitter inside the vi.hoisted block.

3. server.deps.inline is global — affects all 170+ test files

server.deps.inline: ["@wopr-network/platform-core"] applies to every test in the suite. This adds Vite bundling overhead for all tests, risks instanceof breakage for tests checking platform-core class identity, and creates a hidden mock bleed dependency. Scope this to only the heartbeat tests via a vitest.config.heartbeat.ts or Vitest workspace project.

Awaiting human approval after fixes.

devin-ai-integration[bot]

This comment was marked as resolved.

@TSavo
Copy link
Contributor Author

TSavo commented Mar 14, 2026

wopr-auto re-review: ISSUES FOUND.

The three original issues (try/finally, SimpleEmitter once/off, global deps.inline) are all resolved in the current diff. One new issue remains:

Heartbeat tests excluded from CI coverage run

The CI unit-test job runs pnpm test:coverage which maps to vitest run --coverage. This uses vitest.config.ts, which now explicitly excludes src/node-agent/heartbeat.test.ts. The heartbeat-specific config (vitest.config.heartbeat.ts) is only chained in the test script, but CI never calls pnpm test — only pnpm test:coverage. Result: heartbeat tests never execute in CI and can silently regress.

Fix: add the heartbeat config to test:coverage in package.json:

"test:coverage": "vitest run --coverage && vitest run --config vitest.config.heartbeat.ts"

All other prior findings are resolved. Awaiting human approval after this fix.

WOPR and others added 3 commits March 13, 2026 19:53
Cover heartbeat interval firing, error resilience, WebSocket reconnect
with exponential backoff (capped at 30s), delay reset on successful
connection, and clean shutdown. Add server.deps.inline for platform-core
so vi.mock("ws") intercepts within the published package.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- Add try/finally to stop() tests so cleanup runs even if assertions throw
- Add once/off/removeListener to SimpleEmitter to prevent runtime errors
- Scope server.deps.inline to vitest.config.heartbeat.ts (not global)
- Exclude heartbeat test from vitest.config.ts; run via separate config
- Update test script to run both configs

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@TSavo TSavo force-pushed the agent/coder-2133/wop-2133-heartbeat-tests branch from a90d7d2 to fc1556d Compare March 14, 2026 02:54
@TSavo
Copy link
Contributor Author

TSavo commented Mar 14, 2026

wopr-auto review: LGTM. No issues found. Awaiting human approval to enter merge queue.

@TSavo TSavo added this pull request to the merge queue Mar 14, 2026
Merged via the queue into main with commit c5cfdbb Mar 14, 2026
19 checks passed
@TSavo TSavo deleted the agent/coder-2133/wop-2133-heartbeat-tests branch March 14, 2026 03:05
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.

2 participants