@@ -21,7 +21,7 @@ function createCronSystemEventJob(now: number, overrides: Partial<CronJob> = {})
2121}
2222
2323describe ( "issue #13992 regression - cron jobs skip execution" , ( ) => {
24- it ( "should NOT recompute nextRunAtMs for past-due jobs during maintenance " , ( ) => {
24+ it ( "should NOT recompute nextRunAtMs for past-due jobs by default " , ( ) => {
2525 const now = Date . now ( ) ;
2626 const pastDue = now - 60_000 ; // 1 minute ago
2727
@@ -40,6 +40,61 @@ describe("issue #13992 regression - cron jobs skip execution", () => {
4040 expect ( job . state . nextRunAtMs ) . toBe ( pastDue ) ;
4141 } ) ;
4242
43+ it ( "should recompute past-due nextRunAtMs with recomputeExpired when slot already executed" , ( ) => {
44+ // NOTE: in onTimer this recovery branch is used only when due scan found no
45+ // runnable jobs; this unit test validates the maintenance helper contract.
46+ const now = Date . now ( ) ;
47+ const pastDue = now - 60_000 ;
48+
49+ const job : CronJob = {
50+ id : "test-job" ,
51+ name : "test job" ,
52+ enabled : true ,
53+ schedule : { kind : "cron" , expr : "0 8 * * *" , tz : "UTC" } ,
54+ payload : { kind : "systemEvent" , text : "test" } ,
55+ sessionTarget : "main" ,
56+ wakeMode : "next-heartbeat" ,
57+ createdAtMs : now - 3600_000 ,
58+ updatedAtMs : now - 3600_000 ,
59+ state : {
60+ nextRunAtMs : pastDue ,
61+ lastRunAtMs : pastDue + 1000 ,
62+ } ,
63+ } ;
64+
65+ const state = createMockCronStateForJobs ( { jobs : [ job ] , nowMs : now } ) ;
66+ recomputeNextRunsForMaintenance ( state , { recomputeExpired : true } ) ;
67+
68+ expect ( typeof job . state . nextRunAtMs ) . toBe ( "number" ) ;
69+ expect ( ( job . state . nextRunAtMs ?? 0 ) > now ) . toBe ( true ) ;
70+ } ) ;
71+
72+ it ( "should NOT recompute past-due nextRunAtMs for running jobs even with recomputeExpired" , ( ) => {
73+ const now = Date . now ( ) ;
74+ const pastDue = now - 60_000 ;
75+
76+ const job : CronJob = {
77+ id : "test-job" ,
78+ name : "test job" ,
79+ enabled : true ,
80+ schedule : { kind : "cron" , expr : "0 8 * * *" , tz : "UTC" } ,
81+ payload : { kind : "systemEvent" , text : "test" } ,
82+ sessionTarget : "main" ,
83+ wakeMode : "next-heartbeat" ,
84+ createdAtMs : now - 3600_000 ,
85+ updatedAtMs : now - 3600_000 ,
86+ state : {
87+ nextRunAtMs : pastDue ,
88+ runningAtMs : now - 500 ,
89+ } ,
90+ } ;
91+
92+ const state = createMockCronStateForJobs ( { jobs : [ job ] , nowMs : now } ) ;
93+ recomputeNextRunsForMaintenance ( state , { recomputeExpired : true } ) ;
94+
95+ expect ( job . state . nextRunAtMs ) . toBe ( pastDue ) ;
96+ } ) ;
97+
4398 it ( "should compute missing nextRunAtMs during maintenance" , ( ) => {
4499 const now = Date . now ( ) ;
45100
@@ -138,4 +193,78 @@ describe("issue #13992 regression - cron jobs skip execution", () => {
138193 expect ( malformedJob . state . scheduleErrorCount ) . toBe ( 1 ) ;
139194 expect ( malformedJob . state . lastError ) . toMatch ( / ^ s c h e d u l e e r r o r : / ) ;
140195 } ) ;
196+
197+ it ( "recomputes expired slots already executed but keeps never-executed stale slots" , ( ) => {
198+ const now = Date . now ( ) ;
199+ const pastDue = now - 60_000 ;
200+ const alreadyExecuted : CronJob = {
201+ id : "already-executed" ,
202+ name : "already executed" ,
203+ enabled : true ,
204+ schedule : { kind : "cron" , expr : "0 8 * * *" , tz : "UTC" } ,
205+ payload : { kind : "systemEvent" , text : "done" } ,
206+ sessionTarget : "main" ,
207+ wakeMode : "next-heartbeat" ,
208+ createdAtMs : now - 86400_000 ,
209+ updatedAtMs : now - 86400_000 ,
210+ state : {
211+ nextRunAtMs : pastDue ,
212+ lastRunAtMs : pastDue + 1000 ,
213+ } ,
214+ } ;
215+
216+ const neverExecuted : CronJob = {
217+ id : "never-executed" ,
218+ name : "never executed" ,
219+ enabled : true ,
220+ schedule : { kind : "cron" , expr : "0 8 * * *" , tz : "UTC" } ,
221+ payload : { kind : "systemEvent" , text : "pending" } ,
222+ sessionTarget : "main" ,
223+ wakeMode : "next-heartbeat" ,
224+ createdAtMs : now - 86400_000 * 2 ,
225+ updatedAtMs : now - 86400_000 * 2 ,
226+ state : {
227+ nextRunAtMs : pastDue ,
228+ lastRunAtMs : pastDue - 86400_000 ,
229+ } ,
230+ } ;
231+
232+ const state = createMockCronStateForJobs ( {
233+ jobs : [ alreadyExecuted , neverExecuted ] ,
234+ nowMs : now ,
235+ } ) ;
236+ recomputeNextRunsForMaintenance ( state , { recomputeExpired : true } ) ;
237+
238+ expect ( ( alreadyExecuted . state . nextRunAtMs ?? 0 ) > now ) . toBe ( true ) ;
239+ expect ( neverExecuted . state . nextRunAtMs ) . toBe ( pastDue ) ;
240+ } ) ;
241+
242+ it ( "does not advance overdue never-executed jobs when stale running marker is cleared" , ( ) => {
243+ const now = Date . now ( ) ;
244+ const pastDue = now - 60_000 ;
245+ const staleRunningAt = now - 3 * 60 * 60_000 ;
246+
247+ const job : CronJob = {
248+ id : "stale-running-overdue" ,
249+ name : "stale running overdue" ,
250+ enabled : true ,
251+ schedule : { kind : "cron" , expr : "0 8 * * *" , tz : "UTC" } ,
252+ payload : { kind : "systemEvent" , text : "test" } ,
253+ sessionTarget : "main" ,
254+ wakeMode : "next-heartbeat" ,
255+ createdAtMs : now - 86400_000 ,
256+ updatedAtMs : now - 86400_000 ,
257+ state : {
258+ nextRunAtMs : pastDue ,
259+ runningAtMs : staleRunningAt ,
260+ lastRunAtMs : pastDue - 3600_000 ,
261+ } ,
262+ } ;
263+
264+ const state = createMockCronStateForJobs ( { jobs : [ job ] , nowMs : now } ) ;
265+ recomputeNextRunsForMaintenance ( state , { recomputeExpired : true , nowMs : now } ) ;
266+
267+ expect ( job . state . runningAtMs ) . toBeUndefined ( ) ;
268+ expect ( job . state . nextRunAtMs ) . toBe ( pastDue ) ;
269+ } ) ;
141270} ) ;
0 commit comments