Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/gitpod-protocol/src/timeout-validation.spec.ts
2498 views
1
/**
2
* Copyright (c) 2025 Gitpod GmbH. All rights reserved.
3
* Licensed under the GNU Affero General Public License (AGPL).
4
* See License.AGPL.txt in the project root for license information.
5
*/
6
7
import { expect } from "chai";
8
import { WorkspaceTimeoutDuration } from "./gitpod-service";
9
10
describe("WorkspaceTimeoutDuration", () => {
11
describe("validate", () => {
12
it("should handle single unit durations correctly", () => {
13
expect(WorkspaceTimeoutDuration.validate("30m")).to.equal("30m");
14
expect(WorkspaceTimeoutDuration.validate("60m")).to.equal("60m");
15
expect(WorkspaceTimeoutDuration.validate("90m")).to.equal("90m");
16
expect(WorkspaceTimeoutDuration.validate("1h")).to.equal("1h");
17
expect(WorkspaceTimeoutDuration.validate("2h")).to.equal("2h");
18
});
19
20
it("should handle mixed unit durations correctly (bug fix)", () => {
21
// These were the bug cases that were incorrectly parsed
22
expect(WorkspaceTimeoutDuration.validate("1h30m")).to.equal("1h30m");
23
expect(WorkspaceTimeoutDuration.validate("2h15m")).to.equal("2h15m");
24
expect(WorkspaceTimeoutDuration.validate("3h45m")).to.equal("3h45m");
25
expect(WorkspaceTimeoutDuration.validate("1h1m")).to.equal("1h1m");
26
expect(WorkspaceTimeoutDuration.validate("2h59m")).to.equal("2h59m");
27
});
28
29
it("should handle complex Go duration formats", () => {
30
expect(WorkspaceTimeoutDuration.validate("1h30m45s")).to.equal("1h30m45s");
31
expect(WorkspaceTimeoutDuration.validate("1m30s")).to.equal("1m30s");
32
// Note: parse-duration doesn't support decimal durations like "1.5h"
33
});
34
35
it("should handle edge cases within limits", () => {
36
expect(WorkspaceTimeoutDuration.validate("24h")).to.equal("24h");
37
expect(WorkspaceTimeoutDuration.validate("23h59m")).to.equal("23h59m");
38
expect(WorkspaceTimeoutDuration.validate("1440m")).to.equal("1440m"); // 24 hours in minutes
39
});
40
41
it("should reject durations exceeding 24 hours", () => {
42
expect(() => WorkspaceTimeoutDuration.validate("25h")).to.throw(
43
"Workspace inactivity timeout cannot exceed 24h",
44
);
45
expect(() => WorkspaceTimeoutDuration.validate("1441m")).to.throw(
46
"Workspace inactivity timeout cannot exceed 24h",
47
);
48
expect(() => WorkspaceTimeoutDuration.validate("24h1m")).to.throw(
49
"Workspace inactivity timeout cannot exceed 24h",
50
);
51
});
52
53
it("should reject invalid formats", () => {
54
expect(() => WorkspaceTimeoutDuration.validate("invalid")).to.throw("Invalid timeout format");
55
expect(() => WorkspaceTimeoutDuration.validate("1x")).to.throw("Invalid timeout format");
56
expect(() => WorkspaceTimeoutDuration.validate("")).to.throw("Invalid timeout format");
57
expect(() => WorkspaceTimeoutDuration.validate("1")).to.throw("Invalid timeout format");
58
});
59
60
it("should handle case insensitivity", () => {
61
expect(WorkspaceTimeoutDuration.validate("1H30M")).to.equal("1h30m");
62
expect(WorkspaceTimeoutDuration.validate("90M")).to.equal("90m");
63
});
64
65
it("should reject zero or negative durations", () => {
66
// Note: Go duration format doesn't support negative durations in the format we accept
67
// Zero duration components are handled by the totalMinutes > 0 check
68
expect(() => WorkspaceTimeoutDuration.validate("0m")).to.throw("Invalid timeout value");
69
expect(() => WorkspaceTimeoutDuration.validate("0h")).to.throw("Invalid timeout value");
70
});
71
});
72
});
73
74