Skip to content

Commit 34d2e99

Browse files
committed
[AI] test(cron): await reconcileMaintenancePhaseTransition (now async)
Round 6 P1#1 made reconcileMaintenancePhaseTransition async so it can persist the store. Update all test call sites to await the result. Also fix the synchronous arrow function in maintenance-phase-transition.test.ts that used the now-async helper.
1 parent bdfd4fe commit 34d2e99

5 files changed

Lines changed: 42 additions & 40 deletions

‎src/cron/maintenance-deferral-mirroring.test.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ describe("maintenance deferral mirror has a single owner (reconcile, not applyJo
8585

8686
// Tick 1: inside the maintenance window. Job A is blocked, deferral
8787
// is recorded.
88-
reconcileMaintenancePhaseTransition(state, AT_UTC_03_30);
88+
await reconcileMaintenancePhaseTransition(state, AT_UTC_03_30);
8989
const blocked = isRunnableJob({
9090
state,
9191
job,
@@ -98,7 +98,7 @@ describe("maintenance deferral mirror has a single owner (reconcile, not applyJo
9898
expect(storeJob?.state.deferredMaintenanceCount ?? 0).toBe(0);
9999

100100
// Phase exits; reconcile mirrors the backlog and clears the queue.
101-
const phaseExit = reconcileMaintenancePhaseTransition(state, AT_UTC_05_00);
101+
const phaseExit = await reconcileMaintenancePhaseTransition(state, AT_UTC_05_00);
102102
expect(phaseExit.current).toBe("normal");
103103
expect(phaseExit.drainedCount).toBe(1);
104104
// After reconcile: mirror is set EXACTLY ONCE.
@@ -136,7 +136,7 @@ describe("maintenance deferral mirror has a single owner (reconcile, not applyJo
136136
const state = await makeState(job);
137137

138138
// Tick 1: inside the window. Deferral recorded.
139-
reconcileMaintenancePhaseTransition(state, AT_UTC_03_30);
139+
await reconcileMaintenancePhaseTransition(state, AT_UTC_03_30);
140140
isRunnableJob({
141141
state,
142142
job,
@@ -152,7 +152,7 @@ describe("maintenance deferral mirror has a single owner (reconcile, not applyJo
152152
// reconcile at the SAME tick — but since lastMaintenancePhase is
153153
// already "normal" from the previous tick, the second reconcile is
154154
// a no-op (no transition). The count is still 1, not 2.
155-
reconcileMaintenancePhaseTransition(state, AT_UTC_05_00); // exit
155+
await reconcileMaintenancePhaseTransition(state, AT_UTC_05_00); // exit
156156
expect(storeJob?.state.deferredMaintenanceCount).toBe(1);
157157
isRunnableJob({
158158
state,

‎src/cron/maintenance-fifo-replay.test.ts‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,15 @@ describe("phase-exit FIFO replay", () => {
9696
// Tick 1: inside the window. Phase is maintenance, all three are
9797
// deferred in A -> B -> C order via the public record path. The
9898
// store order does not matter — FIFO is bound to the deferral order.
99-
reconcileMaintenancePhaseTransition(state, AT_UTC_03_30);
99+
await reconcileMaintenancePhaseTransition(state, AT_UTC_03_30);
100100
beginMaintenancePhase(AT_UTC_03_30);
101101
recordMaintenanceDeferral({ jobId: "job-A", agentId: "main", nowMs: AT_UTC_03_30 });
102102
recordMaintenanceDeferral({ jobId: "job-B", agentId: "secondary", nowMs: AT_UTC_03_30 + 1 });
103103
recordMaintenanceDeferral({ jobId: "job-C", agentId: "main", nowMs: AT_UTC_03_30 + 2 });
104104
expect(getMaintenanceDeferralCount()).toBe(3);
105105

106106
// Phase exits. Replay happens here.
107-
const phaseExit = reconcileMaintenancePhaseTransition(state, AT_UTC_05_00);
107+
const phaseExit = await reconcileMaintenancePhaseTransition(state, AT_UTC_05_00);
108108
expect(phaseExit.current).toBe("normal");
109109
expect(phaseExit.drainedCount).toBe(3);
110110
expect(getMaintenanceDeferralCount()).toBe(0);
@@ -137,14 +137,14 @@ describe("phase-exit FIFO replay", () => {
137137
// replay must not artificially push a job into the future.
138138
const job = makeJob("job-overdue", "main", AT_UTC_03_30 - 60_000);
139139
const state = await makeState([job]);
140-
reconcileMaintenancePhaseTransition(state, AT_UTC_03_30);
140+
await reconcileMaintenancePhaseTransition(state, AT_UTC_03_30);
141141
beginMaintenancePhase(AT_UTC_03_30);
142142
recordMaintenanceDeferral({
143143
jobId: "job-overdue",
144144
agentId: "main",
145145
nowMs: AT_UTC_03_30,
146146
});
147-
const phaseExit = reconcileMaintenancePhaseTransition(state, AT_UTC_05_00);
147+
const phaseExit = await reconcileMaintenancePhaseTransition(state, AT_UTC_05_00);
148148
expect(phaseExit.drainedCount).toBe(1);
149149
const storeJob = state.store?.jobs.find((j) => j.id === "job-overdue");
150150
// Original nextRunAtMs was AT_UTC_03_30 - 60_000. The replay anchor
@@ -159,10 +159,10 @@ describe("phase-exit FIFO replay", () => {
159159
const deferred = makeJob("job-def", "main", AT_UTC_03_30 - 60_000);
160160
const fresh = makeJob("job-fresh", "main", AT_UTC_03_30 - 60_000);
161161
const state = await makeState([fresh, deferred]);
162-
reconcileMaintenancePhaseTransition(state, AT_UTC_03_30);
162+
await reconcileMaintenancePhaseTransition(state, AT_UTC_03_30);
163163
beginMaintenancePhase(AT_UTC_03_30);
164164
recordMaintenanceDeferral({ jobId: "job-def", agentId: "main", nowMs: AT_UTC_03_30 });
165-
reconcileMaintenancePhaseTransition(state, AT_UTC_05_00);
165+
await reconcileMaintenancePhaseTransition(state, AT_UTC_05_00);
166166
// job-fresh is still on the in-roster path (main is not in roster
167167
// so it would also defer). Force it to not be deferred by tweaking
168168
// the test: change its agent to ops so it never defers.
@@ -185,9 +185,9 @@ describe("phase-exit FIFO replay", () => {
185185
// empty across the cycle.
186186
const job = makeJob("job-A", "ops", AT_UTC_03_30 - 60_000); // in-roster
187187
const state = await makeState([job]);
188-
const tEnter = reconcileMaintenancePhaseTransition(state, AT_UTC_03_30);
188+
const tEnter = await reconcileMaintenancePhaseTransition(state, AT_UTC_03_30);
189189
expect(tEnter.current).toBe("maintenance");
190-
const tExit = reconcileMaintenancePhaseTransition(state, AT_UTC_05_00);
190+
const tExit = await reconcileMaintenancePhaseTransition(state, AT_UTC_05_00);
191191
expect(tExit.drainedCount).toBe(0);
192192
expect(getMaintenanceDeferralCount()).toBe(0);
193193
const storeJob = state.store?.jobs.find((j) => j.id === "job-A");

‎src/cron/maintenance-hot-reload.test.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ describe("cron.maintenance.* hot reload", () => {
6666
expect(result.allowed).toBe(true);
6767
// The scheduler-owned phase transition is a no-op when the maintenance
6868
// block is absent: undefined -> normal, no bump, no drain.
69-
const transition = reconcileMaintenancePhaseTransition(state, AT_UTC_03_30);
69+
const transition = await reconcileMaintenancePhaseTransition(state, AT_UTC_03_30);
7070
expect(transition.current).toBe("normal");
7171
expect(transition.phaseBegan).toBe(false);
7272
expect(transition.drainedCount).toBe(0);
@@ -98,7 +98,7 @@ describe("cron.maintenance.* hot reload", () => {
9898
});
9999
expect(result.phase).toBe("maintenance");
100100
expect(result.allowed).toBe(false);
101-
const transition = reconcileMaintenancePhaseTransition(state, AT_UTC_03_30);
101+
const transition = await reconcileMaintenancePhaseTransition(state, AT_UTC_03_30);
102102
expect(transition.phaseBegan).toBe(true);
103103
});
104104

@@ -113,7 +113,7 @@ describe("cron.maintenance.* hot reload", () => {
113113
maintenanceAgents: ["ops"],
114114
});
115115
expect(state.lastMaintenancePhase).toBeUndefined();
116-
const transition = reconcileMaintenancePhaseTransition(state, AT_UTC_03_30);
116+
const transition = await reconcileMaintenancePhaseTransition(state, AT_UTC_03_30);
117117
expect(transition.previous).toBeUndefined();
118118
expect(transition.current).toBe("maintenance");
119119
expect(transition.phaseBegan).toBe(true);
@@ -128,7 +128,7 @@ describe("cron.maintenance.* hot reload", () => {
128128
});
129129
// 01:30 UTC == 01:30 wall clock UTC, before the 02:00-04:00 window.
130130
const beforeWindowUtc = AT_UTC_03_30 - 2 * 60 * 60_000;
131-
const transition = reconcileMaintenancePhaseTransition(state, beforeWindowUtc);
131+
const transition = await reconcileMaintenancePhaseTransition(state, beforeWindowUtc);
132132
expect(transition.previous).toBeUndefined();
133133
expect(transition.current).toBe("normal");
134134
expect(transition.phaseBegan).toBe(false);
@@ -209,7 +209,7 @@ describe("cron.maintenance.* hot reload", () => {
209209
});
210210
expect(beforeResult.phase).toBe("maintenance");
211211
// The state-level transition also fires for the before state.
212-
const beforeTransition = reconcileMaintenancePhaseTransition(beforeState, AT_UTC_03_30);
212+
const beforeTransition = await reconcileMaintenancePhaseTransition(beforeState, AT_UTC_03_30);
213213
expect(beforeTransition.phaseBegan).toBe(true);
214214

215215
const afterState = await makeStateWithMaintenance({
@@ -233,7 +233,7 @@ describe("cron.maintenance.* hot reload", () => {
233233
});
234234
expect(afterResult.phase).toBe("normal");
235235
// The after state's first tick at 03:30 (outside the new window) is a no-op.
236-
const afterTransition = reconcileMaintenancePhaseTransition(afterState, AT_UTC_03_30);
236+
const afterTransition = await reconcileMaintenancePhaseTransition(afterState, AT_UTC_03_30);
237237
expect(afterTransition.phaseBegan).toBe(false);
238238
expect(afterTransition.drainedCount).toBe(0);
239239
});

‎src/cron/maintenance-lifecycle.integration.test.ts‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ describe("maintenance lifecycle: 6 jobs, 2 windows, 3 agents", () => {
142142

143143
// Tick 1 (before any window): no maintenance active. All 6 jobs are due
144144
// and runnable.
145-
reconcileMaintenancePhaseTransition(state, AT_NORMAL_BEFORE);
145+
await reconcileMaintenancePhaseTransition(state, AT_NORMAL_BEFORE);
146146
let runnable = collectRunnableJobs(state, AT_NORMAL_BEFORE, {
147147
allowCronMissedRunByLastRun: true,
148148
});
@@ -153,7 +153,7 @@ describe("maintenance lifecycle: 6 jobs, 2 windows, 3 agents", () => {
153153

154154
// Tick 2 (inside window 1, MAINT_1): only ops jobs should run; main and
155155
// secondary should be deferred.
156-
reconcileMaintenancePhaseTransition(state, AT_MAINT_1);
156+
await reconcileMaintenancePhaseTransition(state, AT_MAINT_1);
157157
runnable = collectRunnableJobs(state, AT_MAINT_1, {
158158
allowCronMissedRunByLastRun: true,
159159
});
@@ -172,7 +172,7 @@ describe("maintenance lifecycle: 6 jobs, 2 windows, 3 agents", () => {
172172

173173
// Tick 3 (after window 1, AT_NORMAL_BETWEEN): phase exits, backlog is
174174
// mirrored to job.state and the queue is drained.
175-
const tExit = reconcileMaintenancePhaseTransition(state, AT_NORMAL_BETWEEN);
175+
const tExit = await reconcileMaintenancePhaseTransition(state, AT_NORMAL_BETWEEN);
176176
expect(tExit.previous).toBe("maintenance");
177177
expect(tExit.current).toBe("normal");
178178
expect(tExit.drainedCount).toBe(3);
@@ -223,7 +223,7 @@ describe("maintenance lifecycle: 6 jobs, 2 windows, 3 agents", () => {
223223
const state = await makeStateWithJobs(jobs, twoWindowCfg);
224224

225225
// Window 1: phase=maintenance, queue one deferral.
226-
reconcileMaintenancePhaseTransition(state, AT_MAINT_1);
226+
await reconcileMaintenancePhaseTransition(state, AT_MAINT_1);
227227
expect(state.lastMaintenancePhase).toBe("maintenance");
228228
// Manually record a deferral for the test scenario (simulating that
229229
// isRunnableJob admitted-then-deferred the main job in window 1).
@@ -236,7 +236,7 @@ describe("maintenance lifecycle: 6 jobs, 2 windows, 3 agents", () => {
236236
expect(phaseId1).toMatch(/^phase-/);
237237

238238
// Exit window 1: drain.
239-
reconcileMaintenancePhaseTransition(state, AT_NORMAL_BETWEEN);
239+
await reconcileMaintenancePhaseTransition(state, AT_NORMAL_BETWEEN);
240240
expect(getMaintenanceDeferralCount()).toBe(0);
241241
const countAfterExit = getStoreJob(state, "main-A").state.deferredMaintenanceCount ?? 0;
242242
expect(countAfterExit).toBeGreaterThanOrEqual(1);
@@ -245,7 +245,7 @@ describe("maintenance lifecycle: 6 jobs, 2 windows, 3 agents", () => {
245245
// (05:30) is also outside the window. To simulate a "second window",
246246
// hot-swap the config and re-reconcile.
247247
state.deps = { ...state.deps, cronConfig: { maintenance: MAINT_CFG } };
248-
const tReentry = reconcileMaintenancePhaseTransition(state, AT_MAINT_2);
248+
const tReentry = await reconcileMaintenancePhaseTransition(state, AT_MAINT_2);
249249
expect(tReentry.phaseBegan).toBe(true);
250250
expect(state.lastMaintenancePhase).toBe("maintenance");
251251
isRunnableJob({
@@ -259,7 +259,7 @@ describe("maintenance lifecycle: 6 jobs, 2 windows, 3 agents", () => {
259259
expect(phaseId2).not.toBe(phaseId1);
260260

261261
// Exit window 2: the second deferral increments the per-job count.
262-
reconcileMaintenancePhaseTransition(state, AT_NORMAL_AFTER);
262+
await reconcileMaintenancePhaseTransition(state, AT_NORMAL_AFTER);
263263
const countAfterWindow2 = getStoreJob(state, "main-A").state.deferredMaintenanceCount ?? 0;
264264
expect(countAfterWindow2).toBeGreaterThan(countAfterExit);
265265
});
@@ -271,7 +271,7 @@ describe("maintenance lifecycle: 6 jobs, 2 windows, 3 agents", () => {
271271
];
272272
const state = await makeStateWithJobs(jobs);
273273

274-
reconcileMaintenancePhaseTransition(state, AT_MAINT_1);
274+
await reconcileMaintenancePhaseTransition(state, AT_MAINT_1);
275275
isRunnableJob({
276276
state,
277277
job: getStoreJob(state, "main-X"),
@@ -312,7 +312,7 @@ describe("maintenance lifecycle: 6 jobs, 2 windows, 3 agents", () => {
312312
updatedAtMs: 0,
313313
};
314314
const state = await makeStateWithJobs([jobWithoutAgent]);
315-
reconcileMaintenancePhaseTransition(state, AT_MAINT_1);
315+
await reconcileMaintenancePhaseTransition(state, AT_MAINT_1);
316316
const runnable = collectRunnableJobs(state, AT_MAINT_1, {
317317
allowCronMissedRunByLastRun: true,
318318
});

‎src/cron/maintenance-phase-transition.test.ts‎

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ describe("reconcileMaintenancePhaseTransition", () => {
5454
// phase id must be bumped so the first deferral binds to the current
5555
// window rather than a hypothetical phase-0.
5656
const state = await makeState();
57-
const t = reconcileMaintenancePhaseTransition(state, AT_UTC_03_30);
57+
const t = await reconcileMaintenancePhaseTransition(state, AT_UTC_03_30);
5858
expect(t.previous).toBeUndefined();
5959
expect(t.current).toBe("maintenance");
6060
expect(t.phaseBegan).toBe(true);
@@ -64,7 +64,7 @@ describe("reconcileMaintenancePhaseTransition", () => {
6464

6565
it("first call records the current phase without bumping if maintenance is not active", async () => {
6666
const state = await makeState();
67-
const t = reconcileMaintenancePhaseTransition(state, AT_UTC_05_00);
67+
const t = await reconcileMaintenancePhaseTransition(state, AT_UTC_05_00);
6868
expect(t.previous).toBeUndefined();
6969
expect(t.current).toBe("normal");
7070
expect(t.phaseBegan).toBe(false);
@@ -74,8 +74,8 @@ describe("reconcileMaintenancePhaseTransition", () => {
7474

7575
it("no-op when the phase does not change", async () => {
7676
const state = await makeState();
77-
reconcileMaintenancePhaseTransition(state, AT_UTC_03_30); // sets to maintenance
78-
const t = reconcileMaintenancePhaseTransition(state, AT_UTC_03_30 + 1_000);
77+
await reconcileMaintenancePhaseTransition(state, AT_UTC_03_30); // sets to maintenance
78+
const t = await reconcileMaintenancePhaseTransition(state, AT_UTC_03_30 + 1_000);
7979
expect(t.previous).toBe("maintenance");
8080
expect(t.current).toBe("maintenance");
8181
expect(t.phaseBegan).toBe(false);
@@ -84,9 +84,9 @@ describe("reconcileMaintenancePhaseTransition", () => {
8484

8585
it("bumps the phase id on normal -> maintenance transition", async () => {
8686
const state = await makeState();
87-
reconcileMaintenancePhaseTransition(state, AT_UTC_01_30); // normal
87+
await reconcileMaintenancePhaseTransition(state, AT_UTC_01_30); // normal
8888
expect(state.lastMaintenancePhase).toBe("normal");
89-
const t = reconcileMaintenancePhaseTransition(state, AT_UTC_03_30); // -> maintenance
89+
const t = await reconcileMaintenancePhaseTransition(state, AT_UTC_03_30); // -> maintenance
9090
expect(t.previous).toBe("normal");
9191
expect(t.current).toBe("maintenance");
9292
expect(t.phaseBegan).toBe(true);
@@ -95,12 +95,12 @@ describe("reconcileMaintenancePhaseTransition", () => {
9595

9696
it("drains the backlog on maintenance -> normal transition", async () => {
9797
const state = await makeState();
98-
reconcileMaintenancePhaseTransition(state, AT_UTC_03_30); // -> maintenance
98+
await reconcileMaintenancePhaseTransition(state, AT_UTC_03_30); // -> maintenance
9999
recordMaintenanceDeferral({ jobId: "job-A", agentId: "main", nowMs: AT_UTC_03_30 });
100100
recordMaintenanceDeferral({ jobId: "job-B", agentId: "main", nowMs: AT_UTC_03_30 + 1_000 });
101101
expect(getMaintenanceDeferralCount()).toBe(2);
102102

103-
const t = reconcileMaintenancePhaseTransition(state, AT_UTC_05_00); // -> normal
103+
const t = await reconcileMaintenancePhaseTransition(state, AT_UTC_05_00); // -> normal
104104
expect(t.previous).toBe("maintenance");
105105
expect(t.current).toBe("normal");
106106
expect(t.phaseBegan).toBe(false);
@@ -114,7 +114,9 @@ describe("reconcileMaintenancePhaseTransition", () => {
114114
enabled: false,
115115
window: { start: "02:00", end: "04:00", timezone: "UTC" },
116116
});
117-
expect(() => reconcileMaintenancePhaseTransition(state, AT_UTC_03_30)).not.toThrow();
117+
expect(
118+
async () => await reconcileMaintenancePhaseTransition(state, AT_UTC_03_30),
119+
).not.toThrow();
118120
expect(state.lastMaintenancePhase).toBe("normal");
119121
});
120122

@@ -128,23 +130,23 @@ describe("reconcileMaintenancePhaseTransition", () => {
128130

129131
// First tick: outside the window, so phase is normal -> nothing happens.
130132
// previous is undefined, current is normal: no transition (no bump, no drain).
131-
const t1 = reconcileMaintenancePhaseTransition(state, AT_UTC_05_00);
133+
const t1 = await reconcileMaintenancePhaseTransition(state, AT_UTC_05_00);
132134
expect(t1.drainedCount).toBe(0);
133135
// The stale entry is still there because the prior phase was undefined;
134136
// we only drain on an actual maintenance -> normal transition.
135137
expect(getMaintenanceDeferralCount()).toBe(1);
136138

137139
// Next tick: same normal, no-op.
138-
const t2 = reconcileMaintenancePhaseTransition(state, AT_UTC_05_00 + 1_000);
140+
const t2 = await reconcileMaintenancePhaseTransition(state, AT_UTC_05_00 + 1_000);
139141
expect(t2.drainedCount).toBe(0);
140142
});
141143

142144
it("logs the transition details for diagnostics", async () => {
143145
const state = await makeState();
144146
// no logger assertions here; just confirm the call returns the structured
145147
// details that the scheduler's log lines use.
146-
reconcileMaintenancePhaseTransition(state, AT_UTC_01_30);
147-
const t = reconcileMaintenancePhaseTransition(state, AT_UTC_03_30);
148+
await reconcileMaintenancePhaseTransition(state, AT_UTC_01_30);
149+
const t = await reconcileMaintenancePhaseTransition(state, AT_UTC_03_30);
148150
expect(t).toMatchObject({
149151
previous: "normal",
150152
current: "maintenance",

0 commit comments

Comments
 (0)