Repository navigation
Move the close and quit flow out of index.ts and test it - #150
Conversation
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
left a comment
There was a problem hiding this comment.
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.
| ...(endWorkWait ? [] : [{ label: 'Quit', click: () => void requestQuit() }]) | ||
| ]) | ||
| ) | ||
| tray.setContextMenu(Menu.buildFromTemplate(items as MenuItemConstructorOptions[])) |
There was a problem hiding this comment.
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.
| 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 |
There was a problem hiding this comment.
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.
|
Both taken:
|
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
left a comment
There was a problem hiding this comment.
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.
Closes #147
The close and quit flow had no test at all, because
index.tscan't be loaded without Electron. #138, #139 and #148 were checked by reading the code and a smoke run.src/main/quitFlow.ts:QuitFlowholds the flow and its state (shutting down, which question is open, the pending work wait) and takes everything Electron-shaped as callbacks on aQuitHost:ask,show,hide,toTray,balloon,updateTray,quit,closeBehavior,rememberClose,guestBack,now, plus the runner and the work events. Same idea asrunAutostartin autostart.ts.trayMenu()builds the tray template as plain objects.index.tskeeps the Electron side only: it builds the host, maps the window'scloseevent toonClose()(true means keep the window), and passes the template toMenu.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 buildnpm run smoke: SMOKE OK, which exercises the real main process with the new wiring