Path: blob/main/components/gitpod-protocol/src/timeout-validation.spec.ts
2498 views
/**1* Copyright (c) 2025 Gitpod GmbH. All rights reserved.2* Licensed under the GNU Affero General Public License (AGPL).3* See License.AGPL.txt in the project root for license information.4*/56import { expect } from "chai";7import { WorkspaceTimeoutDuration } from "./gitpod-service";89describe("WorkspaceTimeoutDuration", () => {10describe("validate", () => {11it("should handle single unit durations correctly", () => {12expect(WorkspaceTimeoutDuration.validate("30m")).to.equal("30m");13expect(WorkspaceTimeoutDuration.validate("60m")).to.equal("60m");14expect(WorkspaceTimeoutDuration.validate("90m")).to.equal("90m");15expect(WorkspaceTimeoutDuration.validate("1h")).to.equal("1h");16expect(WorkspaceTimeoutDuration.validate("2h")).to.equal("2h");17});1819it("should handle mixed unit durations correctly (bug fix)", () => {20// These were the bug cases that were incorrectly parsed21expect(WorkspaceTimeoutDuration.validate("1h30m")).to.equal("1h30m");22expect(WorkspaceTimeoutDuration.validate("2h15m")).to.equal("2h15m");23expect(WorkspaceTimeoutDuration.validate("3h45m")).to.equal("3h45m");24expect(WorkspaceTimeoutDuration.validate("1h1m")).to.equal("1h1m");25expect(WorkspaceTimeoutDuration.validate("2h59m")).to.equal("2h59m");26});2728it("should handle complex Go duration formats", () => {29expect(WorkspaceTimeoutDuration.validate("1h30m45s")).to.equal("1h30m45s");30expect(WorkspaceTimeoutDuration.validate("1m30s")).to.equal("1m30s");31// Note: parse-duration doesn't support decimal durations like "1.5h"32});3334it("should handle edge cases within limits", () => {35expect(WorkspaceTimeoutDuration.validate("24h")).to.equal("24h");36expect(WorkspaceTimeoutDuration.validate("23h59m")).to.equal("23h59m");37expect(WorkspaceTimeoutDuration.validate("1440m")).to.equal("1440m"); // 24 hours in minutes38});3940it("should reject durations exceeding 24 hours", () => {41expect(() => WorkspaceTimeoutDuration.validate("25h")).to.throw(42"Workspace inactivity timeout cannot exceed 24h",43);44expect(() => WorkspaceTimeoutDuration.validate("1441m")).to.throw(45"Workspace inactivity timeout cannot exceed 24h",46);47expect(() => WorkspaceTimeoutDuration.validate("24h1m")).to.throw(48"Workspace inactivity timeout cannot exceed 24h",49);50});5152it("should reject invalid formats", () => {53expect(() => WorkspaceTimeoutDuration.validate("invalid")).to.throw("Invalid timeout format");54expect(() => WorkspaceTimeoutDuration.validate("1x")).to.throw("Invalid timeout format");55expect(() => WorkspaceTimeoutDuration.validate("")).to.throw("Invalid timeout format");56expect(() => WorkspaceTimeoutDuration.validate("1")).to.throw("Invalid timeout format");57});5859it("should handle case insensitivity", () => {60expect(WorkspaceTimeoutDuration.validate("1H30M")).to.equal("1h30m");61expect(WorkspaceTimeoutDuration.validate("90M")).to.equal("90m");62});6364it("should reject zero or negative durations", () => {65// Note: Go duration format doesn't support negative durations in the format we accept66// Zero duration components are handled by the totalMinutes > 0 check67expect(() => WorkspaceTimeoutDuration.validate("0m")).to.throw("Invalid timeout value");68expect(() => WorkspaceTimeoutDuration.validate("0h")).to.throw("Invalid timeout value");69});70});71});727374