|
1 | 1 | import { beforeEach, describe, expect, it, vi } from "vitest"; |
2 | | -import { createGlobalCommandRunner } from "./shared.js"; |
| 2 | +import { defaultRuntime } from "../../runtime.js"; |
| 3 | +import { createGlobalCommandRunner, parseTimeoutMsOrExit } from "./shared.js"; |
3 | 4 |
|
4 | 5 | const runCommandWithTimeout = vi.hoisted(() => vi.fn()); |
5 | 6 |
|
@@ -48,4 +49,40 @@ describe("createGlobalCommandRunner", () => { |
48 | 49 | code: 17, |
49 | 50 | }); |
50 | 51 | }); |
| 52 | + |
| 53 | + it("requires timeout values to be complete positive integer seconds", () => { |
| 54 | + const error = vi.spyOn(defaultRuntime, "error").mockImplementation(() => undefined); |
| 55 | + const exit = vi.spyOn(defaultRuntime, "exit").mockImplementation(() => undefined as never); |
| 56 | + |
| 57 | + try { |
| 58 | + expect(parseTimeoutMsOrExit("1.5")).toBeNull(); |
| 59 | + expect(parseTimeoutMsOrExit("10abc")).toBeNull(); |
| 60 | + expect(parseTimeoutMsOrExit("0")).toBeNull(); |
| 61 | + expect(parseTimeoutMsOrExit("-1")).toBeNull(); |
| 62 | + |
| 63 | + expect(error).toHaveBeenCalledTimes(4); |
| 64 | + expect(error).toHaveBeenCalledWith("--timeout must be a positive integer (seconds)"); |
| 65 | + expect(exit).toHaveBeenCalledTimes(4); |
| 66 | + expect(exit).toHaveBeenCalledWith(1); |
| 67 | + } finally { |
| 68 | + error.mockRestore(); |
| 69 | + exit.mockRestore(); |
| 70 | + } |
| 71 | + }); |
| 72 | + |
| 73 | + it("parses complete positive integer timeout values as milliseconds", () => { |
| 74 | + const error = vi.spyOn(defaultRuntime, "error").mockImplementation(() => undefined); |
| 75 | + const exit = vi.spyOn(defaultRuntime, "exit").mockImplementation(() => undefined as never); |
| 76 | + |
| 77 | + try { |
| 78 | + expect(parseTimeoutMsOrExit(" 10 ")).toBe(10_000); |
| 79 | + expect(parseTimeoutMsOrExit("001")).toBe(1_000); |
| 80 | + expect(parseTimeoutMsOrExit()).toBeUndefined(); |
| 81 | + expect(error).not.toHaveBeenCalled(); |
| 82 | + expect(exit).not.toHaveBeenCalled(); |
| 83 | + } finally { |
| 84 | + error.mockRestore(); |
| 85 | + exit.mockRestore(); |
| 86 | + } |
| 87 | + }); |
51 | 88 | }); |
0 commit comments