BlueGit

timfa questward

Files

questward / server / tests / deploy.test.js

deploy.test.js

deploy.test.js 3.7 KiB Download Raw
1import test from "node:test";
2import assert from "node:assert/strict";
3import { mkdtempSync, existsSync } from "node:fs";
4import { tmpdir } from "node:os";
5import { join } from "node:path";
6import {
7 nextLocalMidnightMs,
8 isInRestartWarningWindow,
9 restartWarningStartsAt,
10 formatRestartCountdown,
11 restartCountdownMessage,
12 RESTART_WARN_BEFORE_MS
13} from "../../shared/deploy/schedule.js";
14import { DeployScheduler } from "../src/deploy/scheduler.js";
15import {
16 writeRestartManifest,
17 readRestartManifest,
18 consumeRestartManifest,
19 restartManifestPath
20} from "../src/deploy/manifest.js";
21
22test("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
34test("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
44test("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});
50
51test("restart manifest write read consume is atomic-ish", () =>
52{
53 const dir = mkdtempSync(join(tmpdir(), "mmo-restart-"));
54 const path = restartManifestPath(dir);
55 writeRestartManifest(path, {
56 reasons: ["code-update"],
57 deployCode: true,
58 targetCommit: "abc",
59 writtenAt: 123
60 });
61 assert.equal(existsSync(path), true);
62 const read = readRestartManifest(path);
63 assert.equal(read.deployCode, true);
64 assert.equal(read.targetCommit, "abc");
65 const consumed = consumeRestartManifest(path);
66 assert.equal(consumed.deployCode, true);
67 assert.equal(existsSync(path), false);
68 assert.equal(consumeRestartManifest(path), null);
69});
70
71test("DeployScheduler schedules only when remote is ahead", async () =>
72{
73 let now = new Date(2026, 7, 27, 12, 0, 0, 0).getTime();
74 const statuses = [];
75 let ahead = true;
76 const scheduler = new DeployScheduler({
77 repoRoot: ".",
78 enabled: true,
79 now: () => now,
80 checkIntervalMs: 60_000,
81 checkRemote: async () => ({
82 ok: true,
83 ahead,
84 local: "aaa",
85 remote: ahead ? "bbb" : "aaa"
86 }),
87 onStatusChange (status)
88 { statuses.push(status); }
89 });
90 await scheduler.poll();
91 assert.equal(scheduler.status().scheduled, true);
92 assert.equal(scheduler.status().scheduledAt, nextLocalMidnightMs(now));
93 assert.equal(scheduler.status().warning, true);
94
95 now = scheduler.status().scheduledAt - RESTART_WARN_BEFORE_MS;
96 scheduler.tick();
97 assert.equal(scheduler.status().warning, true);
98
99 ahead = false;
100 await scheduler.poll();
101 assert.equal(scheduler.status().scheduled, false);
102 scheduler.stop();
103});
104
105test("DeployScheduler fires onRestartDue once at midnight", async () =>
106{
107 let now = new Date(2026, 7, 27, 23, 0, 0, 0).getTime();
108 let restarts = 0;
109 const scheduler = new DeployScheduler({
110 repoRoot: ".",
111 enabled: true,
112 now: () => now,
113 checkRemote: async () => ({ ok: true, ahead: true, local: "a", remote: "b" }),
114 onRestartDue ()
115 { restarts += 1; }
116 });
117 await scheduler.poll();
118 const at = scheduler.status().scheduledAt;
119 now = at;
120 scheduler.tick();
121 scheduler.tick();
122 assert.equal(restarts, 1);
123 scheduler.stop();
124});