| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { stopExitState, stopAndExit } from "./quit.js"; |
| 4 | test("automatic exit waits for its own successful stop and confirmed VM shutdown", () => { |
| 5 | const status = { |
| 6 | vm_running: true, |
| 7 | job: { id: "old-stop", kind: "stop", running: false, error: "" }, |
| 8 | }; |
| 9 | assert.equal(stopExitState(status, null), "waiting"); |
| 10 | assert.equal(stopExitState(status, "new-stop"), "waiting"); |
| 11 | status.job.id = "new-stop"; |
| 12 | status.job.running = true; |
| 13 | assert.equal(stopExitState(status, "new-stop"), "waiting"); |
| 14 | status.job.running = false; |
| 15 | assert.equal(stopExitState(status, "new-stop"), "waiting"); |
| 16 | status.vm_running = false; |
| 17 | assert.equal(stopExitState(status, "new-stop"), "exit"); |
| 18 | status.job.error = "VM stop failed"; |
| 19 | assert.equal(stopExitState(status, "new-stop"), "failed"); |
| 20 | }); |
| 21 | |
| 22 | test("exit completes without the page polling status, and ignores an old stop", async () => { |
| 23 | let exits = 0, |
| 24 | reads = 0; |
| 25 | const statuses = [ |
| 26 | { vm_running: false, job: { id: "old", kind: "stop", running: false } }, |
| 27 | { vm_running: true, job: { id: "wanted", kind: "stop", running: true } }, |
| 28 | { vm_running: true, job: { id: "wanted", kind: "stop", running: false } }, |
| 29 | { vm_running: false, job: { id: "wanted", kind: "stop", running: false } }, |
| 30 | ]; |
| 31 | assert.equal( |
| 32 | await stopAndExit({ |
| 33 | stop: async () => ({ id: "wanted" }), |
| 34 | readStatus: async () => { |
| 35 | reads++; |
| 36 | return statuses.shift(); |
| 37 | }, |
| 38 | exit: async () => { |
| 39 | exits++; |
| 40 | }, |
| 41 | isCancelled: () => false, |
| 42 | onStatus: () => {}, |
| 43 | delay: async () => {}, |
| 44 | }), |
| 45 | true, |
| 46 | ); |
| 47 | assert.equal(reads, 4); |
| 48 | assert.equal(exits, 1); |
| 49 | }); |
| 50 | test("a failed stop leaves the app open and surfaces the error", async () => { |
| 51 | await assert.rejects( |
| 52 | stopAndExit({ |
| 53 | stop: async () => ({ id: "wanted" }), |
| 54 | readStatus: async () => ({ |
| 55 | vm_running: true, |
| 56 | job: { |
| 57 | id: "wanted", |
| 58 | kind: "stop", |
| 59 | running: false, |
| 60 | error: "stop failed", |
| 61 | }, |
| 62 | }), |
| 63 | exit: () => assert.fail("must remain open"), |
| 64 | isCancelled: () => false, |
| 65 | onStatus: () => {}, |
| 66 | }), |
| 67 | /stop failed/, |
| 68 | ); |
| 69 | }); |
| 70 | test("cancelling automatic exit during a status request prevents app termination", async () => { |
| 71 | let cancelled = false; |
| 72 | const result = await stopAndExit({ |
| 73 | stop: async () => ({ id: "wanted" }), |
| 74 | readStatus: async () => { |
| 75 | cancelled = true; |
| 76 | return { |
| 77 | vm_running: false, |
| 78 | job: { id: "wanted", kind: "stop", running: false }, |
| 79 | }; |
| 80 | }, |
| 81 | exit: () => assert.fail("cancelled exit"), |
| 82 | isCancelled: () => cancelled, |
| 83 | onStatus: () => assert.fail("cancelled update"), |
| 84 | }); |
| 85 | assert.equal(result, false); |
| 86 | }); |