Skip to content

test(electron): drive IPC handlers with bad input instead of checking registration - #4338

Merged
kovtcharov-amd merged 1 commit into
amd:mainfrom
kovtcharov:claude/electron-ipc-behaviour-tests
Sep 25, 2026
Merged

kovtcharov-amd merged 1 commit into
amd:mainfrom
kovtcharov:claude/electron-ipc-behaviour-tests

Conversation

@kovtcharov

Copy link
Copy Markdown
Contributor

Several Electron IPC tests passed without ever calling the handler with bad input. They only checked that a channel was registered, or they mocked out the method under test. A broken check would still pass: the updater could accept a path-traversal release tag, or the agent manager could spawn a process for an unknown id, with no test failing. These tests now call each handler the way the renderer does, pass malformed input, and assert that the call is rejected and nothing else happens. The install progress window had no tests at all and now has coverage. This PR changes tests only. Temporarily loosening the updater's tag regex fails 7 of the new tests, and removing the agent-id and URL-encoding guards fails 11.

Tray-manager tray:set-config validation is left to #4299 and #4330, which already add those tests.

Test plan

  • cd tests/electron && npm ci && npx jest (26 suites, 770 tests pass on Node 20)
  • Weaken TAG_RE in auto-updater.cjs to /^v\d+\.\d+\.\d+/: 7 new tag tests fail
  • Remove the agent-id guard and encodeURIComponent in agent-process-manager.cjs: 11 new IPC tests fail

Part of #4258

… registration

Several Electron IPC tests only proved a channel was registered or spied the
method they were meant to exercise, so a broken input check would still pass.
They now invoke each handler the way the renderer does, with malformed input,
and assert it is rejected with no side effect.

- Updater: malformed release tags (traversal, query strings, trailing
  newline, non-strings) are refused without pinning or touching the feed.
- Agent manager: unknown, empty and non-string ids never spawn a process or
  reach the backend, and uninstall ids stay URL-encoded.
- Notifications: a late or repeated renderer response cannot overturn an
  answer already sent to the agent.
- Install progress window: first test coverage for its IPC handlers,
  progress buffering and failure-dialog routing.

Part of amd#4258
@github-actions github-actions Bot added the tests Test changes label Sep 25, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Verdict: Approve with suggestions

This PR replaces Electron IPC tests that only checked "is this channel registered" with tests that actually call each handler the way the renderer does — including with malformed input — and adds first-time coverage for the install progress window. The tests are real: I reproduced both mutation claims from the test plan exactly, so these tests would catch the regressions they're written to catch.

Nothing blocking. Three small things worth a follow-up:

  • One leftover test still passes when a channel is never registered at all. With the three registration tests deleted, the "handlers are removed on shutdown" test only checks that channels are absent afterwards — which is trivially true if they were never added. Adding a check before the removal closes that hole.
  • One updater channel (the release list) is now the only one with no behavioural test; its only remaining coverage is a test that greps the source file for a string.
  • A few of the new tests install a fake network function and clean it up at the end of the test body rather than in a finally, so one failing assertion leaks the fake into later tests and turns a single failure into a confusing cascade.

Real-world evidence

No evidence-bundle.md was produced for this PR. It changes tests only and touches no user-visible surface, so there is nothing to screenshot or curl. I ran the affected suites directly during this review:

$ cd tests/electron && npm ci && npx jest auto-updater.test.cjs \
    backend-installer-progress-dialog.test.cjs \
    test_agent_process_manager.js test_notification_service.js
Test Suites: 4 passed, 4 total
Tests:       286 passed, 286 total

Both mutation claims in the test plan reproduce exactly:

# TAG_RE weakened to /^v\d+\.\d+\.\d+/
Tests:       7 failed, 32 passed, 39 total

# encodeURIComponent + non-empty-agent-id guards removed
Tests:       11 failed, 128 passed, 139 total

Three consecutive randomized-order runs (npx jest --randomize) all passed 286/286, so the new tests are order-independent today despite the cleanup nit below.

🔍 Technical details

Issues

🟢 removes all 5 IPC handlers on destroy no longer proves registration (tests/electron/auto-updater.test.cjs:469)

The three deleted tests were the only thing asserting that list-releases, install-version, and resume get registered on init(). The surviving destroy test asserts only has(channel) === false after destroy(), which passes vacuously if init() never registered the channel. test_agent_process_manager.js:253 keeps exactly this guard (a registration-list test) — the updater file now has no equivalent.

  test("removes all 5 IPC handlers on destroy", () => {
    const mod = loadModule();
    const win = makeMockWindow();
    mod.init(win);

    const channels = [
      "gaia:update:get-status",
      "gaia:update:check",
      "gaia:update:list-releases",
      "gaia:update:install-version",
      "gaia:update:resume",
    ];
    for (const c of channels) {
      expect(electronMock.ipcMain._handlers.has(c)).toBe(true);
    }

    mod.destroy();

    for (const c of channels) {
      expect(electronMock.ipcMain._handlers.has(c)).toBe(false);
    }
  });

🟢 gaia:update:list-releases has no behavioural coverage (tests/electron/auto-updater.test.cjs)

install-version and check both got invoke-based tests; list-releases did not. Its only remaining coverage is auto-updater-ipc.test.cjs:71, which asserts the channel name appears in the source text — the exact pattern this PR is removing elsewhere. Worth an invoke test (e.g. feed unreachable → rejects/returns an error state) in this PR or the follow-up.

🟢 global.fetch cleanup isn't in a finally (tests/electron/test_agent_process_manager.js:1520, :1535, :1551, :1571, :1587)

Each of these ends with a bare delete global.fetch;. If the await expect(...) above it throws, the stub survives into the next test. Randomized runs pass today, but on a genuine failure this turns one red test into several. Pattern to apply across the five:

        global.fetch = jest.fn();
        try {
          await expect(
            ipcMain.simulateInvoke("agent:install", agentId)
          ).rejects.toThrow(/requires a non-empty agent id/);
          expect(global.fetch).not.toHaveBeenCalled();
        } finally {
          delete global.fetch;
        }

🟢 agent:send-rpc is still half-mocked (tests/electron/test_agent_process_manager.js:1620)

The new block adds a real not-running rejection test, but the adjacent happy-path test still spies on sendJsonRpc and asserts it was called — the pattern this PR is retiring. Fine to leave for the follow-up; noting it so #4258 doesn't consider this channel done.

Strengths

  • The mutation checks in the test plan are real and reproducible. Stating "weaken TAG_RE → 7 fail, drop the guards → 11 fail" and having both land exactly is the strongest possible argument that these tests have teeth. Both reproduced here.
  • The malformed-input tables target the right properties. install-version asserts not just the throw but that the feed URL, allowDowngrade, the on-disk pin file, and getStatus().pinnedVersion are all untouched — a rejected tag can't half-apply. Same for agent:uninstall, which pins the encoded URL (..%2Fsessions%2Fx) rather than just "fetch was called".
  • The new progress-dialog suite covers the parts most likely to break silently: progress buffered until did-finish-load then replayed once, sends dropped after the window is destroyed, double close() tolerated, and both log-file handlers ignoring renderer-supplied arguments.
  • Tests are order-independent (verified with --randomize ×3) and the file follows the existing *.test.cjs conventions.

@kovtcharov-amd
kovtcharov-amd added this pull request to the merge queue Sep 25, 2026
Merged via the queue into amd:main with commit e9364d0 Sep 25, 2026
32 of 34 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tests Test changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants