| 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 stays open for the whole scheduled period", () => |
| 35 | { |
| 36 | const scheduledAt = new Date(2026, 7, 28, 0, 0, 0, 0).getTime(); |
| 37 | assert.equal(restartWarningStartsAt(scheduledAt), 0); |
| 38 | assert.equal(isInRestartWarningWindow(scheduledAt - RESTART_WARN_BEFORE_MS, scheduledAt), true); |
| 39 | assert.equal(isInRestartWarningWindow(scheduledAt - RESTART_WARN_BEFORE_MS - 1, scheduledAt), true); |
| 40 | assert.equal(isInRestartWarningWindow(scheduledAt - 12 * 60 * 60 * 1000, scheduledAt), true); |
| 41 | assert.equal(isInRestartWarningWindow(scheduledAt, scheduledAt), false); |
| 42 | }); |
| 43 | |
| 44 | test("countdown formatting", () => |
| 45 | { |
| 46 | assert.equal(formatRestartCountdown(45_000), "45s"); |
| 47 | assert.equal(formatRestartCountdown(125_000), "2m 05s"); |
| 48 | assert.match(restartCountdownMessage(3_600_000), /1h 00m 00s/); |
| 49 | const scheduledAt = Date.now() + 90_000; |
| 50 | assert.match( |
| 51 | restartCountdownMessage(Math.max(0, scheduledAt - Date.now())), |
| 52 | /1m \d{2}s/ |
| 53 | ); |
| 54 | }); |
| 55 | |
| 56 | test("restart manifest write read consume is atomic-ish", () => |
| 57 | { |
| 58 | const dir = mkdtempSync(join(tmpdir(), "mmo-restart-")); |
| 59 | const path = restartManifestPath(dir); |
| 60 | writeRestartManifest(path, { |
| 61 | reasons: ["code-update"], |
| 62 | deployCode: true, |
| 63 | targetCommit: "abc", |
| 64 | writtenAt: 123 |
| 65 | }); |
| 66 | assert.equal(existsSync(path), true); |
| 67 | const read = readRestartManifest(path); |
| 68 | assert.equal(read.deployCode, true); |
| 69 | assert.equal(read.targetCommit, "abc"); |
| 70 | const consumed = consumeRestartManifest(path); |
| 71 | assert.equal(consumed.deployCode, true); |
| 72 | assert.equal(existsSync(path), false); |
| 73 | assert.equal(consumeRestartManifest(path), null); |
| 74 | }); |
| 75 | |
| 76 | test("DeployScheduler schedules only when remote is ahead", async () => |
| 77 | { |
| 78 | let now = new Date(2026, 7, 27, 12, 0, 0, 0).getTime(); |
| 79 | const statuses = []; |
| 80 | let ahead = true; |
| 81 | const scheduler = new DeployScheduler({ |
| 82 | repoRoot: ".", |
| 83 | enabled: true, |
| 84 | now: () => now, |
| 85 | checkIntervalMs: 60_000, |
| 86 | checkRemote: async () => ({ |
| 87 | ok: true, |
| 88 | ahead, |
| 89 | local: "aaa", |
| 90 | remote: ahead ? "bbb" : "aaa" |
| 91 | }), |
| 92 | onStatusChange (status) |
| 93 | { statuses.push(status); } |
| 94 | }); |
| 95 | await scheduler.poll(); |
| 96 | assert.equal(scheduler.status().scheduled, true); |
| 97 | assert.equal(scheduler.status().scheduledAt, nextLocalMidnightMs(now)); |
| 98 | assert.equal(scheduler.status().warning, true); |
| 99 | |
| 100 | now = scheduler.status().scheduledAt - RESTART_WARN_BEFORE_MS; |
| 101 | scheduler.tick(); |
| 102 | assert.equal(scheduler.status().warning, true); |
| 103 | |
| 104 | ahead = false; |
| 105 | await scheduler.poll(); |
| 106 | assert.equal(scheduler.status().scheduled, false); |
| 107 | scheduler.stop(); |
| 108 | }); |
| 109 | |
| 110 | test("DeployScheduler fires onRestartDue once at midnight", async () => |
| 111 | { |
| 112 | let now = new Date(2026, 7, 27, 23, 0, 0, 0).getTime(); |
| 113 | let restarts = 0; |
| 114 | const scheduler = new DeployScheduler({ |
| 115 | repoRoot: ".", |
| 116 | enabled: true, |
| 117 | now: () => now, |
| 118 | checkRemote: async () => ({ ok: true, ahead: true, local: "a", remote: "b" }), |
| 119 | onRestartDue () |
| 120 | { restarts += 1; } |
| 121 | }); |
| 122 | await scheduler.poll(); |
| 123 | const at = scheduler.status().scheduledAt; |
| 124 | now = at; |
| 125 | scheduler.tick(); |
| 126 | scheduler.tick(); |
| 127 | assert.equal(restarts, 1); |
| 128 | scheduler.stop(); |
| 129 | }); |