@@ -57,6 +57,17 @@ describe("CronService restart catch-up after a schedule change", () => {
5757 await editor . update ( jobId , {
5858 schedule : { kind : "cron" , expr : "0 21 * * *" , tz : "Europe/Istanbul" } ,
5959 } ) ;
60+ const activatedAtMs = Date . parse ( "2026-07-28T07:18:00.000Z" ) ;
61+ expect ( editor . getJob ( jobId ) ?. state . scheduleActivatedAtMs ) . toBe ( activatedAtMs ) ;
62+
63+ vi . setSystemTime ( new Date ( "2026-07-28T08:18:00.000Z" ) ) ;
64+ await editor . update ( jobId , { name : "renamed daily briefing" } ) ;
65+ await editor . update ( jobId , {
66+ schedule : { kind : "cron" , expr : "0 21 * * *" , tz : "Europe/Istanbul" } ,
67+ } ) ;
68+ const idempotentlyUpdated = editor . getJob ( jobId ) ;
69+ expect ( idempotentlyUpdated ?. state . scheduleActivatedAtMs ) . toBe ( activatedAtMs ) ;
70+ expect ( idempotentlyUpdated ?. updatedAtMs ) . toBe ( Date . parse ( "2026-07-28T08:18:00.000Z" ) ) ;
6071 } finally {
6172 editor . stop ( ) ;
6273 }
@@ -79,4 +90,108 @@ describe("CronService restart catch-up after a schedule change", () => {
7990 await store . cleanup ( ) ;
8091 }
8192 } ) ;
93+
94+ it ( "runs a genuine missed slot that occurred after schedule activation" , async ( ) => {
95+ const store = await makeStorePath ( ) ;
96+ const runCommandJob = vi . fn ( async ( ) => ( { status : "ok" as const , summary : "done" } ) ) ;
97+ const now = Date . parse ( "2026-07-30T13:18:00.000Z" ) ;
98+ await writeCronStoreSnapshot ( {
99+ storePath : store . storePath ,
100+ jobs : [
101+ {
102+ id : "restart-post-activation-slot" ,
103+ name : "daily briefing" ,
104+ enabled : true ,
105+ createdAtMs : Date . parse ( "2026-07-01T12:00:00.000Z" ) ,
106+ updatedAtMs : Date . parse ( "2026-07-29T08:00:00.000Z" ) ,
107+ schedule : { kind : "cron" , expr : "0 12 * * *" , tz : "UTC" } ,
108+ sessionTarget : "isolated" ,
109+ wakeMode : "next-heartbeat" ,
110+ payload : { kind : "command" , argv : [ "echo" , "FIRED" ] } ,
111+ state : {
112+ nextRunAtMs : Date . parse ( "2026-07-31T12:00:00.000Z" ) ,
113+ lastRunAtMs : Date . parse ( "2026-07-28T12:00:00.000Z" ) ,
114+ lastStatus : "ok" ,
115+ scheduleActivatedAtMs : Date . parse ( "2026-07-29T08:00:00.000Z" ) ,
116+ } ,
117+ } ,
118+ ] ,
119+ } ) ;
120+ const service = new CronService ( {
121+ storePath : store . storePath ,
122+ cronEnabled : true ,
123+ log : noopLogger ,
124+ nowMs : ( ) => now ,
125+ enqueueSystemEvent : vi . fn ( ) ,
126+ requestHeartbeat : vi . fn ( ) ,
127+ runIsolatedAgentJob : vi . fn ( async ( ) => ( { status : "ok" as const } ) ) ,
128+ runCommandJob : runCommandJob as never ,
129+ } ) ;
130+
131+ try {
132+ await service . start ( ) ;
133+
134+ expect ( runCommandJob ) . toHaveBeenCalledTimes ( 1 ) ;
135+ expect ( service . getJob ( "restart-post-activation-slot" ) ?. state . lastRunAtMs ) . toBe ( now ) ;
136+ } finally {
137+ service . stop ( ) ;
138+ await store . cleanup ( ) ;
139+ }
140+ } ) ;
141+
142+ it ( "preserves one legacy catch-up for an unstamped pre-upgrade job" , async ( ) => {
143+ const store = await makeStorePath ( ) ;
144+ const runCommandJob = vi . fn ( async ( ) => ( { status : "ok" as const , summary : "done" } ) ) ;
145+ const now = Date . parse ( "2026-07-30T13:18:00.000Z" ) ;
146+ await writeCronStoreSnapshot ( {
147+ storePath : store . storePath ,
148+ jobs : [
149+ {
150+ id : "restart-legacy-unstamped-slot" ,
151+ name : "legacy daily briefing" ,
152+ enabled : true ,
153+ createdAtMs : Date . parse ( "2026-07-01T12:00:00.000Z" ) ,
154+ updatedAtMs : Date . parse ( "2026-07-29T08:00:00.000Z" ) ,
155+ schedule : { kind : "cron" , expr : "0 12 * * *" , tz : "UTC" } ,
156+ sessionTarget : "isolated" ,
157+ wakeMode : "next-heartbeat" ,
158+ payload : { kind : "command" , argv : [ "echo" , "FIRED" ] } ,
159+ state : {
160+ nextRunAtMs : Date . parse ( "2026-07-31T12:00:00.000Z" ) ,
161+ lastRunAtMs : Date . parse ( "2026-07-28T12:00:00.000Z" ) ,
162+ lastStatus : "ok" ,
163+ } ,
164+ } ,
165+ ] ,
166+ } ) ;
167+ const createService = ( ) =>
168+ new CronService ( {
169+ storePath : store . storePath ,
170+ cronEnabled : true ,
171+ log : noopLogger ,
172+ nowMs : ( ) => now ,
173+ enqueueSystemEvent : vi . fn ( ) ,
174+ requestHeartbeat : vi . fn ( ) ,
175+ runIsolatedAgentJob : vi . fn ( async ( ) => ( { status : "ok" as const } ) ) ,
176+ runCommandJob : runCommandJob as never ,
177+ } ) ;
178+
179+ const firstRestart = createService ( ) ;
180+ try {
181+ await firstRestart . start ( ) ;
182+ expect ( runCommandJob ) . toHaveBeenCalledTimes ( 1 ) ;
183+ expect ( firstRestart . getJob ( "restart-legacy-unstamped-slot" ) ?. state . lastRunAtMs ) . toBe ( now ) ;
184+ } finally {
185+ firstRestart . stop ( ) ;
186+ }
187+
188+ const secondRestart = createService ( ) ;
189+ try {
190+ await secondRestart . start ( ) ;
191+ expect ( runCommandJob ) . toHaveBeenCalledTimes ( 1 ) ;
192+ } finally {
193+ secondRestart . stop ( ) ;
194+ await store . cleanup ( ) ;
195+ }
196+ } ) ;
82197} ) ;
0 commit comments