@@ -25,6 +25,12 @@ func TestCalculateAutoStop(t *testing.T) {
25
25
26
26
now := time .Now ()
27
27
28
+ chicago , err := time .LoadLocation ("America/Chicago" )
29
+ require .NoError (t , err , "loading chicago time location" )
30
+
31
+ // pastDateNight is 9:45pm on a wednesday
32
+ pastDateNight := time .Date (2024 , 2 , 14 , 21 , 45 , 0 , 0 , chicago )
33
+
28
34
// Wednesday the 8th of February 2023 at midnight. This date was
29
35
// specifically chosen as it doesn't fall on a applicable week for both
30
36
// fortnightly and triweekly autostop requirements.
@@ -70,8 +76,12 @@ func TestCalculateAutoStop(t *testing.T) {
70
76
t .Log ("saturdayMidnightAfterDstOut" , saturdayMidnightAfterDstOut )
71
77
72
78
cases := []struct {
73
- name string
74
- now time.Time
79
+ name string
80
+ now time.Time
81
+
82
+ wsAutostart string
83
+ templateAutoStart schedule.TemplateAutostartRequirement
84
+
75
85
templateAllowAutostop bool
76
86
templateDefaultTTL time.Duration
77
87
templateAutostopRequirement schedule.TemplateAutostopRequirement
@@ -364,6 +374,33 @@ func TestCalculateAutoStop(t *testing.T) {
364
374
// expectedDeadline is copied from expectedMaxDeadline.
365
375
expectedMaxDeadline : dstOutQuietHoursExpectedTime ,
366
376
},
377
+ {
378
+ // A user expects this workspace to be online from 9am -> 9pm.
379
+ // So if a deadline is going to land in the middle of this range,
380
+ // we should bump it to the end.
381
+ // This is already done on `ActivityBumpWorkspace`, but that requires
382
+ // activity on the workspace.
383
+ name : "AutostopCrossAutostartBorder" ,
384
+ // Starting at 9:45pm, with the autostart at 9am.
385
+ now : pastDateNight ,
386
+ templateAllowAutostop : false ,
387
+ templateDefaultTTL : time .Hour * 12 ,
388
+ workspaceTTL : time .Hour * 12 ,
389
+ // At 9am every morning
390
+ wsAutostart : "CRON_TZ=America/Chicago 0 9 * * *" ,
391
+
392
+ // No quiet hours
393
+ templateAutoStart : schedule.TemplateAutostartRequirement {
394
+ // Just allow all days of the week
395
+ DaysOfWeek : 0b01111111 ,
396
+ },
397
+ templateAutostopRequirement : schedule.TemplateAutostopRequirement {},
398
+ userQuietHoursSchedule : "" ,
399
+
400
+ expectedDeadline : time .Date (pastDateNight .Year (), pastDateNight .Month (), pastDateNight .Day ()+ 1 , 21 , 0 , 0 , 0 , chicago ),
401
+ expectedMaxDeadline : time.Time {},
402
+ errContains : "" ,
403
+ },
367
404
}
368
405
369
406
for _ , c := range cases {
@@ -382,6 +419,7 @@ func TestCalculateAutoStop(t *testing.T) {
382
419
UserAutostopEnabled : c .templateAllowAutostop ,
383
420
DefaultTTL : c .templateDefaultTTL ,
384
421
AutostopRequirement : c .templateAutostopRequirement ,
422
+ AutostartRequirement : c .templateAutoStart ,
385
423
}, nil
386
424
},
387
425
}
@@ -433,11 +471,20 @@ func TestCalculateAutoStop(t *testing.T) {
433
471
Valid : true ,
434
472
}
435
473
}
474
+
475
+ autostart := sql.NullString {}
476
+ if c .wsAutostart != "" {
477
+ autostart = sql.NullString {
478
+ String : c .wsAutostart ,
479
+ Valid : true ,
480
+ }
481
+ }
436
482
workspace := dbgen .Workspace (t , db , database.Workspace {
437
- TemplateID : template .ID ,
438
- OrganizationID : org .ID ,
439
- OwnerID : user .ID ,
440
- Ttl : workspaceTTL ,
483
+ TemplateID : template .ID ,
484
+ OrganizationID : org .ID ,
485
+ OwnerID : user .ID ,
486
+ Ttl : workspaceTTL ,
487
+ AutostartSchedule : autostart ,
441
488
})
442
489
443
490
autostop , err := schedule .CalculateAutostop (ctx , schedule.CalculateAutostopParams {
@@ -446,6 +493,7 @@ func TestCalculateAutoStop(t *testing.T) {
446
493
UserQuietHoursScheduleStore : userQuietHoursScheduleStore ,
447
494
Now : c .now ,
448
495
Workspace : workspace ,
496
+ WorkspaceAutostart : c .wsAutostart ,
449
497
})
450
498
if c .errContains != "" {
451
499
require .Error (t , err )
0 commit comments