diff --git a/src/main/quitFlow.ts b/src/main/quitFlow.ts index d43cc04..5c1229d 100644 --- a/src/main/quitFlow.ts +++ b/src/main/quitFlow.ts @@ -74,6 +74,8 @@ const KEEP = 'Keep them running in the tray' const SHUT = 'Shut down VMs and quit' const FORCE = 'Force off and quit' const MORE = 'Wait another minute' +// not Cancel: the guests already got the power button, and nothing here can take that back +const STAY = "Don't quit" const MINUTE = 60_000 /** VMs whose work the user chose to leave unfinished. Quitting doesn't wait for theirs */ @@ -282,19 +284,24 @@ export class QuitFlow { // profile save or delete gets the power button press once that's done, not while the guest // hibernates. Unless the user already said to leave that work unfinished: then waiting for it // only makes the quit look stuck, and a hibernate the power button turns into a shutdown is fine + // VMs the power button press didn't reach (no QMP connection): they won't turn off on their own + const unreached = new Set() + const press = async (n: string): Promise => { + if (!(await this.pressPower(n))) unreached.add(n) + } await Promise.all( names.map(async (n) => { - if (abandoned.has(n)) return this.pressPower(n) + if (abandoned.has(n)) return press(n) const saving = host.vmWork()[n] === 'saving' await this.workDone(n) if (!runner.isRunning(n)) return // a session save boots the VM again right before it ends, and the guest misses a power button // press while it's still resuming from hibernate. It has the agent, so wait for that to answer if (saving) await host.guestBack(n, 120_000) - await this.pressPower(n) + await press(n) }) ) - if (!(await this.waitForShutdown(names))) return this.release(abandoned) + if (!(await this.waitForShutdown(names, unreached))) return this.release(abandoned) host.quit() } @@ -303,7 +310,7 @@ export class QuitFlow { * false if the user cancelled. A Windows guest installing updates on shutdown regularly needs * longer than a minute, so the question offers another minute as well as Force off. */ - private async waitForShutdown(names: string[]): Promise { + private async waitForShutdown(names: string[], unreached: ReadonlySet): Promise { const host = this.host let waiting = names // real time, not rounds of the question: it counts a question left unanswered for a while too @@ -313,14 +320,30 @@ export class QuitFlow { waiting = waiting.filter((_n, i) => !done[i]) if (!waiting.length) return true const minutes = Math.max(1, Math.round((host.now() - since) / MINUTE)) + const off = names.filter((n) => !waiting.includes(n)) + const asked = waiting.filter((n) => !unreached.has(n)) + const notAsked = waiting.filter((n) => unreached.has(n)) host.show() const { choice } = await this.question({ type: 'warning', title: 'Still running', message: `${waiting.join(', ')} ${waiting.length === 1 ? 'has' : 'have'} not shut down ${minutes === 1 ? 'within a minute' : `in ${minutes} minutes`}.`, - detail: `A guest installing updates on shutdown can take a few minutes. Force off loses anything unsaved inside ${waiting.length === 1 ? 'it' : 'them'}.`, - buttons: [MORE, FORCE, 'Cancel'], - cancelId: 2, + detail: [ + // so Don't quit doesn't leave the user wondering where the other VMs went + off.length ? `${off.join(', ')} ${off.length === 1 ? 'is' : 'are'} already off.` : '', + `A guest installing updates on shutdown can take a few minutes. Force off loses anything unsaved inside ${waiting.length === 1 ? 'it' : 'them'}.`, + asked.length + ? `Don't quit keeps the app open, but ${asked.length === waiting.length ? (asked.length === 1 ? 'the guest was' : 'the guests were') : asked.join(', ') + (asked.length === 1 ? ' was' : ' were')} already asked to shut down and may still turn off.` + : '', + notAsked.length + ? `The app couldn't reach ${notAsked.join(', ')} to ask ${notAsked.length === 1 ? 'it' : 'them'} to shut down, so ${notAsked.length === 1 ? 'it' : 'they'} won't turn off by ${notAsked.length === 1 ? 'itself' : 'themselves'}. Force off is the way to turn ${notAsked.length === 1 ? 'it' : 'them'} off.` + : '' + ] + .filter(Boolean) + .join('\n\n'), + // waiting longer only helps a guest that got the power button + buttons: asked.length ? [MORE, FORCE, STAY] : [FORCE, STAY], + cancelId: asked.length ? 2 : 1, defaultId: 0, noLink: true }) @@ -335,14 +358,17 @@ export class QuitFlow { } } - private async pressPower(name: string): Promise { + /** false if the press didn't reach a running guest */ + private async pressPower(name: string): Promise { const runner = this.host.runner - if (!runner.isRunning(name)) return + if (!runner.isRunning(name)) return true try { if (runner.state(name).status === 'paused') await runner.resume(name) await runner.shutdown(name) + return true } catch { - // no QMP connection: it ends up in the force off question + // no QMP connection: it ends up in the force off question, which says so + return false } } diff --git a/tests/quitFlow.test.ts b/tests/quitFlow.test.ts index a6dc7bf..6b92334 100644 --- a/tests/quitFlow.test.ts +++ b/tests/quitFlow.test.ts @@ -8,12 +8,15 @@ const WAIT = "Quit when it's done" const NOW = 'Quit anyway' const MORE = 'Wait another minute' const FORCE = 'Force off and quit' +const STAY = "Don't quit" class FakeRunner { running = new Set() statuses: Record = {} /** guests that ignore the power button, so waitForExit times out */ stuck = new Set() + /** guests whose QMP connection is gone, so the power button can't be pressed */ + unreachable = new Set() calls: string[] = [] /** VMs the flow has said must not be started again */ held = new Set() @@ -32,6 +35,7 @@ class FakeRunner { this.statuses[name] = 'running' } async shutdown(name: string): Promise { + if (this.unreachable.has(name)) throw new Error('QMP is not connected') this.calls.push(`shutdown:${name}`) if (!this.stuck.has(name)) this.running.delete(name) } @@ -347,8 +351,8 @@ describe('QuitFlow after Quit anyway', () => { expect([...t.runner.held]).toEqual([]) }) - it('lets go of the hold on Cancel in the Force off question', async () => { - const t = setup({ running: ['win11'], work: { win11: 'saving' }, answer: (q) => (q.title === 'Still working' ? NOW : q.title === 'Still running' ? 'Cancel' : SHUT) }) + it("lets go of the hold on Don't quit in the Force off question", async () => { + const t = setup({ running: ['win11'], work: { win11: 'saving' }, answer: (q) => (q.title === 'Still working' ? NOW : q.title === 'Still running' ? STAY : SHUT) }) t.runner.stuck.add('win11') await t.flow.requestQuit() expect(t.titles()).toContain('Still running') @@ -428,8 +432,8 @@ describe('QuitFlow guests that do not shut down', () => { expect(t.log).toContain('quit') }) - it('stops the quit on Cancel and puts the tray back', async () => { - const t = setup({ running: ['win11'], closeBehavior: 'quit', answer: () => 'Cancel' }) + it("stops the quit on Don't quit and puts the tray back", async () => { + const t = setup({ running: ['win11'], closeBehavior: 'quit', answer: () => STAY }) t.runner.stuck.add('win11') t.flow.onClose() await settle() @@ -437,6 +441,53 @@ describe('QuitFlow guests that do not shut down', () => { expect(t.log).not.toContain('quit') expect(t.log.filter((l) => l === 'updateTray').length).toBeGreaterThan(1) }) + + it("says the shut down can't be taken back, and which VMs are already off", async () => { + const t = setup({ running: ['win11', 'kali', 'debian'], closeBehavior: 'quit', answer: () => STAY }) + t.runner.stuck.add('win11') + t.flow.onClose() + await settle() + const q = t.asked[0] + expect(q.buttons).toEqual([MORE, FORCE, STAY]) + expect(q.buttons[q.cancelId]).toBe(STAY) + expect(q.detail).toMatch(/^kali, debian are already off\./) + expect(q.detail).toContain('the guest was already asked to shut down') + }) + + it("doesn't say a guest it couldn't reach was asked to shut down", async () => { + const t = setup({ running: ['win11', 'kali'], closeBehavior: 'quit', answer: () => STAY }) + t.runner.stuck.add('win11') + t.runner.stuck.add('kali') + t.runner.unreachable.add('kali') + t.flow.onClose() + await settle() + const detail = t.asked[0].detail ?? '' + expect(detail).toContain('but win11 was already asked to shut down') + expect(detail).toContain("The app couldn't reach kali to ask it to shut down, so it won't turn off by itself.") + }) + + it('leaves out the asked sentence when no guest could be reached', async () => { + const t = setup({ running: ['win11'], closeBehavior: 'quit', answer: () => STAY }) + t.runner.stuck.add('win11') + t.runner.unreachable.add('win11') + t.flow.onClose() + await settle() + expect(t.asked[0].detail).not.toContain('already asked') + expect(t.asked[0].detail).toContain("couldn't reach win11") + // another minute can't help, so it isn't offered + expect(t.asked[0].buttons).toEqual([FORCE, STAY]) + expect(t.asked[0].buttons[t.asked[0].cancelId]).toBe(STAY) + }) + + it('names nothing as off when every guest is still up', async () => { + const t = setup({ running: ['win11', 'kali'], closeBehavior: 'quit', answer: () => STAY }) + t.runner.stuck.add('win11') + t.runner.stuck.add('kali') + t.flow.onClose() + await settle() + expect(t.asked[0].detail).not.toContain('already off') + expect(t.asked[0].detail).toContain('the guests were already asked') + }) }) describe('trayMenu', () => {