Skip to content

Commit f9832b1

Browse files
committed
extract [-+] disable logic to util/schedule, add unit tests
1 parent b8ada49 commit f9832b1

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

site/src/pages/WorkspacePage/WorkspacePage.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { FullScreenLoader } from "../../components/Loader/FullScreenLoader"
1111
import { Workspace, WorkspaceErrors } from "../../components/Workspace/Workspace"
1212
import { firstOrItem } from "../../util/array"
1313
import { pageTitle } from "../../util/page"
14-
import { maxDeadline, minDeadline } from "../../util/schedule"
14+
import { canExtendDeadline, canReduceDeadline, maxDeadline, minDeadline } from "../../util/schedule"
1515
import { getFaviconByStatus } from "../../util/workspace"
1616
import { selectUser } from "../../xServices/auth/authSelectors"
1717
import { XServiceContext } from "../../xServices/StateContext"
@@ -112,10 +112,10 @@ export const WorkspacePage: React.FC = () => {
112112
})
113113
},
114114
deadlineMinusEnabled: () => {
115-
return deadline > minDeadline()
115+
return canReduceDeadline(deadline)
116116
},
117117
deadlinePlusEnabled: () => {
118-
return deadline < maxDeadline(workspace, template)
118+
return canExtendDeadline(deadline, workspace, template)
119119
},
120120
}}
121121
workspace={workspace}

site/src/util/schedule.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import duration from "dayjs/plugin/duration"
33
import { Template, Workspace } from "../api/typesGenerated"
44
import * as Mocks from "../testHelpers/entities"
55
import {
6+
canExtendDeadline,
7+
canReduceDeadline,
68
deadlineExtensionMax,
79
deadlineExtensionMin,
810
extractTimezone,
@@ -78,3 +80,37 @@ describe("minDeadline", () => {
7880
expect(delta).toBeGreaterThanOrEqual(deadlineExtensionMin.asMilliseconds())
7981
})
8082
})
83+
84+
describe("canExtendDeadline", () => {
85+
it("should be falsy if the deadline is more than 24 hours in the future", () => {
86+
expect(
87+
canExtendDeadline(dayjs().add(25, "hours"), Mocks.MockWorkspace, Mocks.MockTemplate),
88+
).toBeFalsy()
89+
})
90+
91+
it("should be falsy if the deadline is more than the template max_ttl", () => {
92+
const tooFarAhead = dayjs().add(dayjs.duration(Mocks.MockTemplate.max_ttl_ms, "milliseconds"))
93+
expect(canExtendDeadline(tooFarAhead, Mocks.MockWorkspace, Mocks.MockTemplate)).toBeFalsy()
94+
})
95+
96+
it("should be truth if the deadline is within the template max_ttl", () => {
97+
const okDeadline = dayjs().add(
98+
dayjs.duration(Mocks.MockTemplate.max_ttl_ms / 2, "milliseconds"),
99+
)
100+
expect(canExtendDeadline(okDeadline, Mocks.MockWorkspace, Mocks.MockTemplate)).toBeFalsy()
101+
})
102+
})
103+
104+
describe("canReduceDeadline", () => {
105+
it("should be falsy if the deadline is 30 minutes or less in the future", () => {
106+
expect(canReduceDeadline(dayjs())).toBeFalsy()
107+
expect(canReduceDeadline(dayjs().add(1, "minutes"))).toBeFalsy()
108+
expect(canReduceDeadline(dayjs().add(29, "minutes"))).toBeFalsy()
109+
expect(canReduceDeadline(dayjs().add(30, "minutes"))).toBeFalsy()
110+
})
111+
112+
it("should be truthy if the deadline is 30 minutes or more in the future", () => {
113+
expect(canReduceDeadline(dayjs().add(31, "minutes"))).toBeTruthy()
114+
expect(canReduceDeadline(dayjs().add(100, "years"))).toBeTruthy()
115+
})
116+
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy