forked from Vatix-Protocol/vatix-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimeout-utils.test.ts
More file actions
147 lines (120 loc) · 3.96 KB
/
Copy pathtimeout-utils.test.ts
File metadata and controls
147 lines (120 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/**
* Unit tests for Timeout Utilities
*
* Covers timeout validation, signal creation, and withTimeout behavior.
*/
import { describe, it, expect, vi } from "vitest";
import {
validateTimeout,
createTimeoutSignal,
withTimeout,
DEFAULT_TIMEOUT_MS,
MIN_TIMEOUT_MS,
MAX_TIMEOUT_MS,
formatDuration,
} from "./timeout-utils.js";
describe("validateTimeout", () => {
it("should return valid timeout as-is", () => {
expect(validateTimeout(10_000)).toBe(10_000);
});
it("should throw TimeoutValidationError for NaN", () => {
expect(() => validateTimeout(NaN)).toThrowError(
expect.objectContaining({ name: "TimeoutValidationError", statusCode: 400 })
);
});
it("should throw TimeoutValidationError for non-number", () => {
expect(() => validateTimeout("abc" as unknown as number)).toThrowError(
expect.objectContaining({ name: "TimeoutValidationError", statusCode: 400 })
);
});
it("should clamp values below minimum", () => {
expect(validateTimeout(100)).toBe(MIN_TIMEOUT_MS);
});
it("should clamp values above maximum", () => {
expect(validateTimeout(600_000)).toBe(MAX_TIMEOUT_MS);
});
});
describe("createTimeoutSignal", () => {
it("should create a signal that aborts after timeout", async () => {
const { signal, clear } = createTimeoutSignal(100);
await new Promise((resolve) => setTimeout(resolve, 1500));
expect(signal.aborted).toBe(true);
clear();
});
it("should combine with existing signal", async () => {
const existingController = new AbortController();
const { signal, clear } = createTimeoutSignal(
1000,
existingController.signal
);
existingController.abort(new Error("Cancelled"));
expect(signal.aborted).toBe(true);
clear();
});
it("should clean up timeout on clear", async () => {
const { signal, clear } = createTimeoutSignal(1000);
clear();
expect(signal.aborted).toBe(false);
});
});
describe("withTimeout", () => {
it("should return result when operation completes in time", async () => {
const result = await withTimeout(async () => "success", {
timeoutMs: 1000,
});
expect(result.timedOut).toBe(false);
expect(result.value).toBe("success");
expect(result.error).toBeUndefined();
});
it("should time out when operation takes too long", async () => {
const result = await withTimeout(
async () => {
await new Promise((resolve) => setTimeout(resolve, 5000));
return "too late";
},
{ timeoutMs: 100 }
);
expect(result.timedOut).toBe(true);
expect(result.value).toBeUndefined();
expect(result.error).toBeDefined();
expect(result.error!.message).toContain("timed out");
});
it("should capture operation errors", async () => {
const result = await withTimeout(
async () => {
throw new Error("Provider error");
},
{ timeoutMs: 1000 }
);
expect(result.timedOut).toBe(false);
expect(result.value).toBeUndefined();
expect(result.error).toBeDefined();
expect(result.error!.message).toBe("Provider error");
});
it("should report duration", async () => {
const result = await withTimeout(async () => "done", { timeoutMs: 1000 });
expect(result.durationMs).toBeGreaterThanOrEqual(0);
});
it("should use custom error message", async () => {
const result = await withTimeout(
async () => {
await new Promise((resolve) => setTimeout(resolve, 5000));
return "too late";
},
{ timeoutMs: 100, errorMessage: "Custom timeout message" }
);
expect(result.timedOut).toBe(true);
expect(result.error!.message).toBe("Custom timeout message");
});
});
describe("formatDuration", () => {
it("should format milliseconds", () => {
expect(formatDuration(500)).toBe("500ms");
});
it("should format seconds", () => {
expect(formatDuration(1500)).toBe("1.50s");
});
it("should format exact seconds", () => {
expect(formatDuration(2000)).toBe("2.00s");
});
});