Skip to content

Commit 406d465

Browse files
committed
more tests
1 parent f60f59b commit 406d465

File tree

4 files changed

+207
-14
lines changed

4 files changed

+207
-14
lines changed

site/src/components/WorkspaceStats/WorkspaceScheduleForm.test.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ describe("validationSchema", () => {
5555
expect(validate).toThrowError(Language.errorNoDayOfWeek)
5656
})
5757

58+
it("allows startTime 16:20", () => {
59+
const values: WorkspaceScheduleFormValues = {
60+
...valid,
61+
startTime: "16:20",
62+
}
63+
const validate = () => validationSchema.validateSync(values)
64+
expect(validate).not.toThrow()
65+
})
66+
5867
it("disallows startTime to be H:mm", () => {
5968
const values: WorkspaceScheduleFormValues = {
6069
...valid,
@@ -73,10 +82,10 @@ describe("validationSchema", () => {
7382
expect(validate).toThrowError(Language.errorTime)
7483
})
7584

76-
it("disallows an invalid startTime 13:01", () => {
85+
it("disallows an invalid startTime 24:01", () => {
7786
const values: WorkspaceScheduleFormValues = {
7887
...valid,
79-
startTime: "13:01",
88+
startTime: "24:01",
8089
}
8190
const validate = () => validationSchema.validateSync(values)
8291
expect(validate).toThrowError(Language.errorTime)

site/src/components/WorkspaceStats/WorkspaceScheduleForm.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export const validationSchema = Yup.object({
8686
const parts = value.split(":")
8787
const HH = Number(parts[0])
8888
const mm = Number(parts[1])
89-
return HH >= 0 && HH <= 12 && mm >= 0 && mm <= 59
89+
return HH >= 0 && HH <= 23 && mm >= 0 && mm <= 59
9090
}
9191
}),
9292
ttl: Yup.number().min(0).integer(),
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
import * as TypesGen from "../../api/typesGenerated"
2+
import { WorkspaceScheduleFormValues } from "../../components/WorkspaceStats/WorkspaceScheduleForm"
3+
import { formValuesToAutoStartRequest, formValuesToTTLRequest } from "./WorkspaceSchedulePage"
4+
5+
const validValues: WorkspaceScheduleFormValues = {
6+
sunday: false,
7+
monday: true,
8+
tuesday: true,
9+
wednesday: true,
10+
thursday: true,
11+
friday: true,
12+
saturday: false,
13+
startTime: "09:30",
14+
ttl: 120,
15+
}
16+
17+
describe("WorkspaceSchedulePage", () => {
18+
describe("formValuesToAutoStartRequest", () => {
19+
it.each<[WorkspaceScheduleFormValues, TypesGen.UpdateWorkspaceAutostartRequest]>([
20+
[
21+
// Empty case
22+
{
23+
sunday: false,
24+
monday: false,
25+
tuesday: false,
26+
wednesday: false,
27+
thursday: false,
28+
friday: false,
29+
saturday: false,
30+
startTime: "",
31+
ttl: 0,
32+
},
33+
{
34+
schedule: "",
35+
},
36+
],
37+
[
38+
// Single day
39+
{
40+
sunday: true,
41+
monday: false,
42+
tuesday: false,
43+
wednesday: false,
44+
thursday: false,
45+
friday: false,
46+
saturday: false,
47+
startTime: "16:20",
48+
ttl: 120,
49+
},
50+
{
51+
schedule: "20 16 * * 0",
52+
},
53+
],
54+
[
55+
// Standard 1-5 case
56+
{
57+
sunday: false,
58+
monday: true,
59+
tuesday: true,
60+
wednesday: true,
61+
thursday: true,
62+
friday: true,
63+
saturday: false,
64+
startTime: "09:30",
65+
ttl: 120,
66+
},
67+
{
68+
schedule: "30 09 * * 1-5",
69+
},
70+
],
71+
[
72+
// Everyday
73+
{
74+
sunday: true,
75+
monday: true,
76+
tuesday: true,
77+
wednesday: true,
78+
thursday: true,
79+
friday: true,
80+
saturday: true,
81+
startTime: "09:00",
82+
ttl: 60 * 8,
83+
},
84+
{
85+
schedule: "00 09 * * 1-7",
86+
},
87+
],
88+
[
89+
// Mon, Wed, Fri Evenings
90+
{
91+
sunday: false,
92+
monday: true,
93+
tuesday: false,
94+
wednesday: true,
95+
thursday: false,
96+
friday: true,
97+
saturday: false,
98+
startTime: "16:20",
99+
ttl: 60 * 3,
100+
},
101+
{
102+
schedule: "20 16 * * 1,3,5",
103+
},
104+
],
105+
])(`formValuesToAutoStartRequest(%p) return %p`, (values, request) => {
106+
expect(formValuesToAutoStartRequest(values)).toEqual(request)
107+
})
108+
})
109+
110+
describe("formValuesToTTLRequest", () => {
111+
it.each<[WorkspaceScheduleFormValues, TypesGen.UpdateWorkspaceTTLRequest]>([
112+
[
113+
// 0 case
114+
{
115+
...validValues,
116+
ttl: 0,
117+
},
118+
{
119+
ttl: undefined,
120+
},
121+
],
122+
[
123+
// 2 Hours = 7.2e+12 case
124+
{
125+
...validValues,
126+
ttl: 120,
127+
},
128+
{
129+
ttl: 7_200_000_000_000,
130+
},
131+
],
132+
[
133+
// 8 hours = 2.88e+13 case
134+
{
135+
...validValues,
136+
ttl: 60 * 8,
137+
},
138+
{
139+
ttl: 28_800_000_000_000,
140+
},
141+
],
142+
])(`formValuesToTTLRequest(%p) returns %p`, (values, request) => {
143+
expect(formValuesToTTLRequest(values)).toEqual(request)
144+
})
145+
})
146+
})

site/src/pages/WorkspaceSchedulePage/WorkspaceSchedulePage.tsx

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
import { firstOrItem } from "../../util/array"
1212
import { workspaceSchedule } from "../../xServices/workspaceSchedule/workspaceScheduleXService"
1313

14-
// TODO(Grey): Test before opening PR from draft
1514
export const formValuesToAutoStartRequest = (
1615
values: WorkspaceScheduleFormValues,
1716
): TypesGen.UpdateWorkspaceAutostartRequest => {
@@ -21,22 +20,61 @@ export const formValuesToAutoStartRequest = (
2120
}
2221
}
2322

24-
// TODO(Grey): Fill in
25-
return {
26-
schedule: "9 30 * * 1-5",
27-
}
28-
}
23+
const [HH, mm] = values.startTime.split(":")
2924

30-
export const formValuesToTTLRequest = (values: WorkspaceScheduleFormValues): TypesGen.UpdateWorkspaceTTLRequest => {
31-
if (!values.ttl) {
25+
const makeCronString = (dow: string) => `${mm} ${HH} * * ${dow}`
26+
27+
const days = [
28+
values.sunday,
29+
values.monday,
30+
values.tuesday,
31+
values.wednesday,
32+
values.thursday,
33+
values.friday,
34+
values.saturday,
35+
]
36+
37+
const isEveryDay = days.every((day) => day)
38+
39+
const isMonThroughFri =
40+
!values.sunday &&
41+
values.monday &&
42+
values.tuesday &&
43+
values.wednesday &&
44+
values.thursday &&
45+
values.friday &&
46+
!values.saturday &&
47+
!values.sunday
48+
49+
// Handle special cases, falling throw to comma-separation
50+
if (isEveryDay) {
3251
return {
33-
ttl: 0, // TODO(Grey): Verify with Cian whether 0 or null is better to send
52+
schedule: makeCronString("1-7"),
53+
}
54+
} else if (isMonThroughFri) {
55+
return {
56+
schedule: makeCronString("1-5"),
57+
}
58+
} else {
59+
const dow = days.reduce((previous, current, idx) => {
60+
if (!current) {
61+
return previous
62+
} else {
63+
const prefix = previous ? "," : ""
64+
return previous + prefix + idx
65+
}
66+
}, "")
67+
68+
return {
69+
schedule: makeCronString(dow),
3470
}
3571
}
72+
}
3673

37-
// TODO(Grey): Fill in
74+
export const formValuesToTTLRequest = (values: WorkspaceScheduleFormValues): TypesGen.UpdateWorkspaceTTLRequest => {
3875
return {
39-
ttl: 0,
76+
// minutes to nanoseconds
77+
ttl: values.ttl ? values.ttl * 60 * 1000 * 1_000_000 : undefined,
4078
}
4179
}
4280

0 commit comments

Comments
 (0)