| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { mkdtempSync, existsSync } from "node:fs"; |
| 4 | import { tmpdir } from "node:os"; |
| 5 | import { join } from "node:path"; |
| 6 | import { |
| 7 | nextLocalMidnightMs, |
| 8 | isInRestartWarningWindow, |
| 9 | restartWarningStartsAt, |
| 10 | formatRestartCountdown, |
| 11 | restartCountdownMessage, |
| 12 | RESTART_WARN_BEFORE_MS |
| 13 | } from "../../shared/deploy/schedule.js"; |
| 14 | import { DeployScheduler } from "../src/deploy/scheduler.js"; |
| 15 | import { |
| 16 | writeRestartManifest, |
| 17 | readRestartManifest, |
| 18 | consumeRestartManifest, |
| 19 | restartManifestPath |
| 20 | } from "../src/deploy/manifest.js"; |
| 21 | |
| 22 | test("nextLocalMidnightMs is the following calendar midnight", () => |
| 23 | { |
| 24 | const noon = new Date(2026, 7, 27, 12, 0, 0, 0).getTime(); |
| 25 | const midnight = nextLocalMidnightMs(noon); |
| 26 | const d = new Date(midnight); |
| 27 | assert.equal(d.getFullYear(), 2026); |
| 28 | assert.equal(d.getMonth(), 7); |
| 29 | assert.equal(d.getDate(), 28); |
| 30 | assert.equal(d.getHours(), 0); |
| 31 | assert.equal(d.getMinutes(), 0); |
| 32 | }); |
| 33 | |
| 34 | test("warning window opens three hours before midnight", () => |
| 35 | { |
| 36 | const scheduledAt = new Date(2026, 7, 28, 0, 0, 0, 0).getTime(); |
| 37 | assert.equal(restartWarningStartsAt(scheduledAt), scheduledAt - RESTART_WARN_BEFORE_MS); |
| 38 | assert.equal(isInRestartWarningWindow(scheduledAt - RESTART_WARN_BEFORE_MS, scheduledAt), true); |
| 39 | assert.equal(isInRestartWarningWindow(scheduledAt - RESTART_WARN_BEFORE_MS - 1, scheduledAt), false); |
| 40 | assert.equal(isInRestartWarningWindow(scheduledAt, scheduledAt), false); |
| 41 | }); |
| 42 | |
| 43 | test("countdown formatting", () => |
| 44 | { |
| 45 | assert.equal(formatRestartCountdown(45_000), "45s"); |
| 46 | assert.equal(formatRestartCountdown(125_000), "2m 05s"); |
| 47 | assert.match(restartCountdownMessage(3_600_000), /1h 00m 00s/); |
| 48 | }); |
| 49 | |
| 50 | test("restart manifest write read consume is atomic-ish", () => |
| 51 | { |
| 52 | const dir = mkdtempSync(join(tmpdir(), "mmo-restart-")); |
| 53 | const path = restartManifestPath(dir); |
| 54 | writeRestartManifest(path, { |
| 55 | reasons: ["code-update"], |
| 56 | deployCode: true, |
| 57 | targetCommit: "abc", |
| 58 | writtenAt: 123 |
| 59 | }); |
| 60 | assert.equal(existsSync(path), true); |
| 61 | const read = readRestartManifest(path); |
| 62 | assert.equal(read.deployCode, true); |
| 63 | assert.equal(read.targetCommit, "abc"); |
| 64 | const consumed = consumeRestartManifest(path); |
| 65 | assert.equal(consumed.deployCode, true); |
| 66 | assert.equal(existsSync(path), false); |
| 67 | assert.equal(consumeRestartManifest(path), null); |
| 68 | }); |
| 69 | |
| 70 | test("DeployScheduler schedules only when remote is ahead", async () => |
| 71 | { |
| 72 | let now = new Date(2026, 7, 27, 12, 0, 0, 0).getTime(); |
| 73 | const statuses = []; |
| 74 | let ahead = true; |
| 75 | const scheduler = new DeployScheduler({ |
| 76 | repoRoot: ".", |
| 77 | enabled: true, |
| 78 | now: () => now, |
| 79 | checkIntervalMs: 60_000, |
| 80 | checkRemote: async () => ({ |
| 81 | ok: true, |
| 82 | ahead, |
| 83 | local: "aaa", |
| 84 | remote: ahead ? "bbb" : "aaa" |
| 85 | }), |
| 86 | onStatusChange (status) |
| 87 | { statuses.push(status); } |
| 88 | }); |
| 89 | await scheduler.poll(); |
| 90 | assert.equal(scheduler.status().scheduled, true); |
| 91 | assert.equal(scheduler.status().scheduledAt, nextLocalMidnightMs(now)); |
| 92 | assert.equal(scheduler.status().warning, false); |
| 93 | |
| 94 | now = scheduler.status().scheduledAt - RESTART_WARN_BEFORE_MS; |
| 95 | scheduler.tick(); |
| 96 | assert.equal(scheduler.status().warning, true); |
| 97 | |
| 98 | ahead = false; |
| 99 | await scheduler.poll(); |
| 100 | assert.equal(scheduler.status().scheduled, false); |
| 101 | scheduler.stop(); |
| 102 | }); |
| 103 | |
| 104 | test("DeployScheduler fires onRestartDue once at midnight", async () => |
| 105 | { |
| 106 | let now = new Date(2026, 7, 27, 23, 0, 0, 0).getTime(); |
| 107 | let restarts = 0; |
| 108 | const scheduler = new DeployScheduler({ |
| 109 | repoRoot: ".", |
| 110 | enabled: true, |
| 111 | now: () => now, |
| 112 | checkRemote: async () => ({ ok: true, ahead: true, local: "a", remote: "b" }), |
| 113 | onRestartDue () |
| 114 | { restarts += 1; } |
| 115 | }); |
| 116 | await scheduler.poll(); |
| 117 | const at = scheduler.status().scheduledAt; |
| 118 | now = at; |
| 119 | scheduler.tick(); |
| 120 | scheduler.tick(); |
| 121 | assert.equal(restarts, 1); |
| 122 | scheduler.stop(); |
| 123 | }); |