Skip to content

Tests for the window pin/restore lifecycle (pinWindow, sweepStalePins, restoreWindow) #397

Backlog

Description

@TMHSDigital

Summary

The pin/restore code added in 0.55.0 has tests only for its pure helpers. pinWindow, sweepStalePins and restoreWindow never run in the suite (windowControl.ts line coverage is 42%). The server test for minimized windows also runs a real PowerShell restore against a fake hwnd, which is what broke CI on Ubuntu for the 0.55.0 release commit.

Where

  • src/utils/windowControl.ts:170-173
  • src/utils/windowControl.ts:196-235
  • src/utils/windowControl.ts:240-260
  • src/utils/resolveTarget.ts:110-121
  • src/utils/resolveTarget.ts:142-145
  • src/index.ts:90-96
  • src/__tests__/windowControl.test.ts:26-110
  • src/__tests__/server.test.ts:22-28
  • src/__tests__/server.test.ts:35-50
  • src/__tests__/server.test.ts:155-159
  • src/utils/resolveTarget.ts:110-117

Details

windowControl.test.ts tests only isOccluded, chooseBackend, the script builders, parsePinFile and MinimizedWindowError. The parts with side effects never run in the suite: pinWindow (writes a flag file, spawns a detached PowerShell watcher, resolves on "pinned"/"already"/error/exit/timeout, release() is meant to be idempotent), sweepStalePins (the boot sweep in index.ts:92 that deletes other servers' flag files and unpins their windows), and restoreWindow. In the v8 per-line run, windowControl.ts line coverage is 41.9%, and every statement from 197-234 and 242-259 is uncovered. In prepareCapture, the path that restores a minimized window and then reselects it by hwnd (resolveTarget.ts:118-121) is also uncovered, along with holdPin (144). The only handler test that mocks prepareCapture (waitForChangeHandler.test.ts:35) hard-codes pin: false, restored: false, so no tool result with alwaysOnTop: true or restoredWindow: true is asserted anywhere.

if (/already/.test(out)) { release(); done(); }   // windowControl.ts:223-226
else if (/pinned/.test(out)) done();
...
if (!pin || pin.serverPid === process.pid || isAlive(pin.serverPid)) continue;  // :250
rmSync(join(pinDir(), name), { force: true });

Also: server.test "refuses a minimized window" runs a real PowerShell restore against a fake hwnd

server.test.ts fakes getRawWindows (a "Mail" window with minimized: true, hwnd: 303) but mocks neither restoreWindow nor runPowerShell. Since 74a3ce6, prepareCapture catches the MinimizedWindowError and calls restoreWindow(303), which spawns a real powershell running ShowWindow/SetWindowPos(HWND_BOTTOM) on handle 303. The test passes by a different route on each OS. On Linux, spawning PowerShell throws and resolveTarget.ts:116 rethrows (the 35f4446 fix). On Windows, IsIconic(303) is false for a handle that does not exist, so the script prints "normal", the faked list still says minimized, and selectWindow throws again. In the local Windows run, line 116 is uncovered. Each of the 4 it.each cases starts a cold PowerShell. Cold PowerShell starts already caused 5 s timeouts in #370/#372/#373.

Steps: On Windows, run npx vitest run src/__tests__/server.test.ts --coverage and look at src/utils/resolveTarget.ts:116, which shows 0 hits. The handler spawned powershell for the restore.

Impact

A regression in the release, "already topmost" or error handling leaves the user's window pinned HWND_TOPMOST after the capture, or leaves flag files that keep the watcher alive. A regression in the sweep's pid check could unpin windows that a live sibling server is still holding. The restore-then-reselect path is the headline behaviour of 74a3ce6, and an outside contributor wrote it.

Steps to reproduce

npx vitest run --coverage, then look at src/utils/windowControl.ts in coverage-final.json. Statements 197-259 have 0 hits.

Suggested fix

Add windowControl lifecycle tests that mock node:child_process.spawn (a fake child with a stdout EventEmitter), plus runPowerShell and isAlive. Cases: (1) stdout "pinned": resolves, flag file exists, and release() removes it, twice without error. (2) stdout "already": the flag is removed before resolve. (3) child "error": the flag is removed. (4) no output: resolves after timeoutMs (use fake timers). (5) sweepStalePins with flags for a dead pid, a live pid, this pid and a malformed name: only the dead one is removed and unpinned, and the count counts only successful unpins. (6) prepareCapture with a mocked restoreWindow returning true and a second getRawWindows where the restored window is no longer the first title match: it reselects hwnd=<n> and captureReport contains restoredWindow: true. (7) A holdPin call when pin is true calls pinWindow(hwnd).

For "server.test "refuses a minimized window" runs a real PowerShell restore against a fake hwnd": In server.test.ts, add vi.mock("../utils/windowControl.js", ...) that keeps the real pure functions and replaces restoreWindow (and pinWindow) with fakes. Then split the case in two: restoreWindow rejects, so the error says "minimized" (resolveTarget.ts:116), and restoreWindow resolves false while the window is still minimized, so the error still says "minimized".

Acceptance criteria

  • pinWindow: resolves on pinned, already, error, exit and timeout; release() is idempotent
  • sweepStalePins: removes only stale flags and unpins those windows
  • prepareCapture: restore-then-reselect-by-hwnd path
  • server.test's minimized-window tests stub the restore instead of spawning PowerShell

Getting started

  • Relevant files: src/utils/windowControl.ts, src/utils/resolveTarget.ts
  • Run tests: npm test (also npm run typecheck and npm run lint); see CONTRIBUTING.md
  • If you change a tool's schema or description, run npm run docs

Filed by repo-audit/full-audit at f0e5ebd. Severity: medium. Effort: M. audit-key: testing|src/utils/windowControl.ts|pinWindow

Activity

  1. added
    help wanted
    testing
    effort: medium
    on Nov 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    effort: mediumhelp wantedpriority: mediumtesting

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions