Skip to content

Move the close and quit flow out of index.ts and test it - #150

Merged
A21-devs merged 3 commits into
mainfrom
refactor/quit-flow-tests
Dec 4, 2026
Merged

A21-devs merged 3 commits into
mainfrom
refactor/quit-flow-tests

Conversation

@A21-devs

@A21-devs A21-devs commented Dec 4, 2026

Copy link
Copy Markdown
Owner

Closes #147

The close and quit flow had no test at all, because index.ts can't be loaded without Electron. #138, #139 and #148 were checked by reading the code and a smoke run.

  • src/main/quitFlow.ts: QuitFlow holds the flow and its state (shutting down, which question is open, the pending work wait) and takes everything Electron-shaped as callbacks on a QuitHost: ask, show, hide, toTray, balloon, updateTray, quit, closeBehavior, rememberClose, guestBack, now, plus the runner and the work events. Same idea as runAutostart in autostart.ts. trayMenu() builds the tray template as plain objects.
  • index.ts keeps the Electron side only: it builds the host, maps the window's close event to onClose() (true means keep the window), and passes the template to Menu.buildFromTemplate. 446 -> 243 lines.
  • tests/quitFlow.test.ts: 25 cases against a fake host and a fake runner, answering questions by button label.

No behaviour change intended. The orderings that are hard to hit by hand are what the tests are mostly about: work finishing while its question is open (no wait, straight to the quit), a profile save starting while the running VMs question is open (the guest gets the power button only after the save), a paused guest resumed first, a guest that ignores the power button (another minute, Force off, Cancel), and the tray's items while waiting for work.

Testing

  • npm test: 178 pass (153 before, 25 new)
  • npm run typecheck, npm run build
  • npm run smoke: SMOKE OK, which exercises the real main process with the new wiring

index.ts can't be loaded without Electron, so none of this had a test:
the running VMs question, the still working question, the waits behind
them, the tray's enabled states and the guards against a question
opening twice. It is all decisions that don't need Electron.

quitFlow.ts takes the window, the tray, the dialogs and the backend as
callbacks (like runAutostart already does) and keeps the state the flow
needs. index.ts keeps the Electron side: it builds the host, maps the
window's close event to onClose, and hands the tray template to
Menu.buildFromTemplate. No behaviour change intended; index.ts goes from
446 to 243 lines.
25 cases through a fake host: the close behaviours, Don't ask again, the
work questions and both ways out of the wait, a paused guest resumed
before the power button, a profile save that starts while the question
is open, a guest that doesn't shut down, and the tray menu's items.

@A21-devs A21-devs left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Went through this as a behaviour-preserving move, comparing each method against what index.ts did before. The branch order in onClose is the same as the handler's (open question first, then nothing-running, then shutting down or waiting, then tray or quit), workFirst still re-checks hasWork() after the question so finished work doesn't start a wait, shutDownAllAndQuit still reads saving before awaiting workDone so the agent wait isn't skipped, and allWorkDone still clears the wait and rebuilds the tray before resolving. The fake-host tests read well and the two orderings I most wanted covered (work ending under its question, a save starting under the other one) are both there. Two small things.

Comment thread src/main/index.ts Outdated
...(endWorkWait ? [] : [{ label: 'Quit', click: () => void requestQuit() }])
])
)
tray.setContextMenu(Menu.buildFromTemplate(items as MenuItemConstructorOptions[]))

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

The cast isn't needed: TrayItem[] is assignable to MenuItemConstructorOptions[] as written, and I checked that Menu.buildFromTemplate(items) typechecks on its own (the only error is then the unused import). With the cast there, a later TrayItem that Electron would reject (a type or role that isn't in its union, say) compiles anyway and fails at runtime when the menu is built, which is exactly the breakage this refactor should keep catchable.

Comment thread src/main/quitFlow.ts Outdated
export class QuitFlow {
private shutting = false
/** a question is open on the window: no second copy, and closing the window leaves it up */
private asking: 'work' | 'vms' | null = null

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

asking carries which question is open, but every use is a truthiness check: onClose, requestQuit and askAndQuit all treat 'work' and 'vms' the same. The union reads like the two are told apart somewhere, so either a boolean would say what is actually meant, or the comment should say the kind is only there for reading in a debugger.

@A21-devs

A21-devs commented Dec 4, 2026

Copy link
Copy Markdown
Owner Author

Both taken:

  • Menu.buildFromTemplate(items) with no cast, and MenuItemConstructorOptions is no longer imported. Confirmed it typechecks, so a TrayItem Electron would reject is now a compile error instead of a runtime one.
  • asking is a plain boolean, and question() lost the kind parameter. Nothing read it.

npm run typecheck, npm test (178), npm run build, npm run smoke (SMOKE OK) after the change.

Menu.buildFromTemplate takes the TrayItem array as it is, so the cast
only hid a later mismatch with Electron's own type. The flag for an open
question is a boolean now: nothing told the two questions apart.

@A21-devs A21-devs left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Second pass over dbb7b14. Both are really fixed: Menu.buildFromTemplate(items) has no cast left and MenuItemConstructorOptions is gone from the imports, so the template type is checked against Electron's again, and asking is a boolean with the kind dropped from question().

One thing I checked while reading it, since a boolean set in a finally is only safe if questions never nest: question() is called from workFirst, askAndQuit and waitForShutdown, and each of those awaits its answer before the next one can start, so the inner call can never clear a flag an outer one set. The guards in onClose, requestQuit and askAndQuit make sure of that too.

Nothing else outstanding.

@A21-devs
A21-devs merged commit b2d1a54 into main Dec 4, 2026
1 check passed
@A21-devs
A21-devs deleted the refactor/quit-flow-tests branch December 4, 2026 19:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tests for the close and quit flow

1 participant