@zapier/zapier-sdk 0.33.0 → 0.33.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (128) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/index.cjs +2 -1
  3. package/dist/index.d.mts +9 -1
  4. package/dist/index.d.ts +1 -1
  5. package/dist/index.d.ts.map +1 -1
  6. package/dist/index.mjs +2 -1
  7. package/dist/plugins/registry/index.d.ts.map +1 -1
  8. package/dist/plugins/registry/index.js +1 -0
  9. package/dist/types/sdk.d.ts +8 -0
  10. package/dist/types/sdk.d.ts.map +1 -1
  11. package/package.json +2 -2
  12. package/dist/api/auth.test.d.ts +0 -2
  13. package/dist/api/auth.test.d.ts.map +0 -1
  14. package/dist/api/auth.test.js +0 -220
  15. package/dist/api/client.test.d.ts +0 -2
  16. package/dist/api/client.test.d.ts.map +0 -1
  17. package/dist/api/client.test.js +0 -611
  18. package/dist/api/debug.test.d.ts +0 -2
  19. package/dist/api/debug.test.d.ts.map +0 -1
  20. package/dist/api/debug.test.js +0 -59
  21. package/dist/api/polling.test.d.ts +0 -2
  22. package/dist/api/polling.test.d.ts.map +0 -1
  23. package/dist/api/polling.test.js +0 -360
  24. package/dist/auth.test.d.ts +0 -2
  25. package/dist/auth.test.d.ts.map +0 -1
  26. package/dist/auth.test.js +0 -480
  27. package/dist/plugins/eventEmission/builders.test.d.ts +0 -2
  28. package/dist/plugins/eventEmission/builders.test.d.ts.map +0 -1
  29. package/dist/plugins/eventEmission/builders.test.js +0 -138
  30. package/dist/plugins/eventEmission/index.test.d.ts +0 -5
  31. package/dist/plugins/eventEmission/index.test.d.ts.map +0 -1
  32. package/dist/plugins/eventEmission/index.test.js +0 -712
  33. package/dist/plugins/eventEmission/transport.test.d.ts +0 -5
  34. package/dist/plugins/eventEmission/transport.test.d.ts.map +0 -1
  35. package/dist/plugins/eventEmission/transport.test.js +0 -164
  36. package/dist/plugins/fetch/index.test.d.ts +0 -2
  37. package/dist/plugins/fetch/index.test.d.ts.map +0 -1
  38. package/dist/plugins/fetch/index.test.js +0 -428
  39. package/dist/plugins/findFirstConnection/index.test.d.ts +0 -2
  40. package/dist/plugins/findFirstConnection/index.test.d.ts.map +0 -1
  41. package/dist/plugins/findFirstConnection/index.test.js +0 -177
  42. package/dist/plugins/findUniqueConnection/index.test.d.ts +0 -2
  43. package/dist/plugins/findUniqueConnection/index.test.d.ts.map +0 -1
  44. package/dist/plugins/findUniqueConnection/index.test.js +0 -159
  45. package/dist/plugins/getAction/index.test.d.ts +0 -2
  46. package/dist/plugins/getAction/index.test.d.ts.map +0 -1
  47. package/dist/plugins/getAction/index.test.js +0 -211
  48. package/dist/plugins/getApp/index.test.d.ts +0 -2
  49. package/dist/plugins/getApp/index.test.d.ts.map +0 -1
  50. package/dist/plugins/getApp/index.test.js +0 -157
  51. package/dist/plugins/getConnection/index.test.d.ts +0 -2
  52. package/dist/plugins/getConnection/index.test.d.ts.map +0 -1
  53. package/dist/plugins/getConnection/index.test.js +0 -124
  54. package/dist/plugins/getInputFieldsSchema/index.test.d.ts +0 -2
  55. package/dist/plugins/getInputFieldsSchema/index.test.d.ts.map +0 -1
  56. package/dist/plugins/getInputFieldsSchema/index.test.js +0 -291
  57. package/dist/plugins/listActions/index.test.d.ts +0 -2
  58. package/dist/plugins/listActions/index.test.d.ts.map +0 -1
  59. package/dist/plugins/listActions/index.test.js +0 -454
  60. package/dist/plugins/listApps/index.test.d.ts +0 -2
  61. package/dist/plugins/listApps/index.test.d.ts.map +0 -1
  62. package/dist/plugins/listApps/index.test.js +0 -124
  63. package/dist/plugins/listConnections/index.test.d.ts +0 -2
  64. package/dist/plugins/listConnections/index.test.d.ts.map +0 -1
  65. package/dist/plugins/listConnections/index.test.js +0 -920
  66. package/dist/plugins/listInputFieldChoices/index.test.d.ts +0 -2
  67. package/dist/plugins/listInputFieldChoices/index.test.d.ts.map +0 -1
  68. package/dist/plugins/listInputFieldChoices/index.test.js +0 -717
  69. package/dist/plugins/listInputFields/index.test.d.ts +0 -2
  70. package/dist/plugins/listInputFields/index.test.d.ts.map +0 -1
  71. package/dist/plugins/listInputFields/index.test.js +0 -359
  72. package/dist/plugins/manifest/index.test.d.ts +0 -2
  73. package/dist/plugins/manifest/index.test.d.ts.map +0 -1
  74. package/dist/plugins/manifest/index.test.js +0 -1179
  75. package/dist/plugins/request/index.test.d.ts +0 -2
  76. package/dist/plugins/request/index.test.d.ts.map +0 -1
  77. package/dist/plugins/request/index.test.js +0 -458
  78. package/dist/plugins/runAction/index.test.d.ts +0 -2
  79. package/dist/plugins/runAction/index.test.d.ts.map +0 -1
  80. package/dist/plugins/runAction/index.test.js +0 -350
  81. package/dist/resolvers/connectionId.test.d.ts +0 -2
  82. package/dist/resolvers/connectionId.test.d.ts.map +0 -1
  83. package/dist/resolvers/connectionId.test.js +0 -61
  84. package/dist/sdk.test.d.ts +0 -2
  85. package/dist/sdk.test.d.ts.map +0 -1
  86. package/dist/sdk.test.js +0 -260
  87. package/dist/types/domain.test.d.ts +0 -2
  88. package/dist/types/domain.test.d.ts.map +0 -1
  89. package/dist/types/domain.test.js +0 -39
  90. package/dist/utils/array-utils.test.d.ts +0 -2
  91. package/dist/utils/array-utils.test.d.ts.map +0 -1
  92. package/dist/utils/array-utils.test.js +0 -107
  93. package/dist/utils/batch-utils.test.d.ts +0 -2
  94. package/dist/utils/batch-utils.test.d.ts.map +0 -1
  95. package/dist/utils/batch-utils.test.js +0 -476
  96. package/dist/utils/domain-utils.test.d.ts +0 -2
  97. package/dist/utils/domain-utils.test.d.ts.map +0 -1
  98. package/dist/utils/domain-utils.test.js +0 -346
  99. package/dist/utils/file-utils.test.d.ts +0 -2
  100. package/dist/utils/file-utils.test.d.ts.map +0 -1
  101. package/dist/utils/file-utils.test.js +0 -51
  102. package/dist/utils/function-utils.test.d.ts +0 -2
  103. package/dist/utils/function-utils.test.d.ts.map +0 -1
  104. package/dist/utils/function-utils.test.js +0 -188
  105. package/dist/utils/id-utils.test.d.ts +0 -2
  106. package/dist/utils/id-utils.test.d.ts.map +0 -1
  107. package/dist/utils/id-utils.test.js +0 -22
  108. package/dist/utils/pagination-utils.test.d.ts +0 -17
  109. package/dist/utils/pagination-utils.test.d.ts.map +0 -1
  110. package/dist/utils/pagination-utils.test.js +0 -461
  111. package/dist/utils/retry-utils.test.d.ts +0 -2
  112. package/dist/utils/retry-utils.test.d.ts.map +0 -1
  113. package/dist/utils/retry-utils.test.js +0 -90
  114. package/dist/utils/string-utils.test.d.ts +0 -2
  115. package/dist/utils/string-utils.test.d.ts.map +0 -1
  116. package/dist/utils/string-utils.test.js +0 -59
  117. package/dist/utils/telemetry-context.test.d.ts +0 -2
  118. package/dist/utils/telemetry-context.test.d.ts.map +0 -1
  119. package/dist/utils/telemetry-context.test.js +0 -154
  120. package/dist/utils/telemetry-utils.test.d.ts +0 -2
  121. package/dist/utils/telemetry-utils.test.d.ts.map +0 -1
  122. package/dist/utils/telemetry-utils.test.js +0 -155
  123. package/dist/utils/url-utils.test.d.ts +0 -2
  124. package/dist/utils/url-utils.test.d.ts.map +0 -1
  125. package/dist/utils/url-utils.test.js +0 -103
  126. package/dist/utils/validation.test.d.ts +0 -2
  127. package/dist/utils/validation.test.d.ts.map +0 -1
  128. package/dist/utils/validation.test.js +0 -44
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=domain.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"domain.test.d.ts","sourceRoot":"","sources":["../../src/types/domain.test.ts"],"names":[],"mappings":""}
@@ -1,39 +0,0 @@
1
- import { describe, it, expect } from "vitest";
2
- import { splitVersionedKey } from "../utils/domain-utils";
3
- describe("splitVersionedKey", () => {
4
- it("should split versioned key with version", () => {
5
- const [baseKey, version] = splitVersionedKey("SlackCLIAPI@1.21.1");
6
- expect(baseKey).toBe("SlackCLIAPI");
7
- expect(version).toBe("1.21.1");
8
- });
9
- it("should split versioned key with complex version", () => {
10
- const [baseKey, version] = splitVersionedKey("GoogleSheetsAPI@2.5.0-beta.1");
11
- expect(baseKey).toBe("GoogleSheetsAPI");
12
- expect(version).toBe("2.5.0-beta.1");
13
- });
14
- it("should handle versioned key without version", () => {
15
- const [baseKey, version] = splitVersionedKey("SomeAPI");
16
- expect(baseKey).toBe("SomeAPI");
17
- expect(version).toBeUndefined();
18
- });
19
- it("should handle versioned key with multiple @ symbols", () => {
20
- const [baseKey, version] = splitVersionedKey("ComplexAPI@1.0.0@beta");
21
- expect(baseKey).toBe("ComplexAPI");
22
- expect(version).toBe("1.0.0@beta");
23
- });
24
- it("should handle edge case with many @ symbols", () => {
25
- const [baseKey, version] = splitVersionedKey("API@with@multiple@at@symbols@1.2.3");
26
- expect(baseKey).toBe("API");
27
- expect(version).toBe("with@multiple@at@symbols@1.2.3");
28
- });
29
- it("should handle empty string", () => {
30
- const [baseKey, version] = splitVersionedKey("");
31
- expect(baseKey).toBe("");
32
- expect(version).toBeUndefined();
33
- });
34
- it("should handle string that starts with @", () => {
35
- const [baseKey, version] = splitVersionedKey("@version");
36
- expect(baseKey).toBe("");
37
- expect(version).toBe("version");
38
- });
39
- });
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=array-utils.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"array-utils.test.d.ts","sourceRoot":"","sources":["../../src/utils/array-utils.test.ts"],"names":[],"mappings":""}
@@ -1,107 +0,0 @@
1
- import { describe, it, expect } from "vitest";
2
- import { toArrayFromAsync } from "./array-utils";
3
- describe("array-utils", () => {
4
- describe("fromAsync", () => {
5
- it("should convert async generator to array", async () => {
6
- async function* generateNumbers() {
7
- yield 1;
8
- yield 2;
9
- yield 3;
10
- }
11
- const result = await toArrayFromAsync(generateNumbers());
12
- expect(result).toEqual([1, 2, 3]);
13
- });
14
- it("should handle empty async generator", async () => {
15
- async function* generateNothing() {
16
- // yield nothing
17
- }
18
- const result = await toArrayFromAsync(generateNothing());
19
- expect(result).toEqual([]);
20
- });
21
- it("should handle async generator with async operations", async () => {
22
- async function* generateWithDelay() {
23
- for (let i = 1; i <= 3; i++) {
24
- await new Promise((resolve) => setTimeout(resolve, 1)); // Small delay
25
- yield i * 10;
26
- }
27
- }
28
- const result = await toArrayFromAsync(generateWithDelay());
29
- expect(result).toEqual([10, 20, 30]);
30
- });
31
- it("should handle async generator that yields different types", async () => {
32
- async function* generateMixed() {
33
- yield "hello";
34
- yield 42;
35
- yield { name: "test" };
36
- yield [1, 2, 3];
37
- }
38
- const result = await toArrayFromAsync(generateMixed());
39
- expect(result).toEqual(["hello", 42, { name: "test" }, [1, 2, 3]]);
40
- });
41
- it("should handle async generator with error", async () => {
42
- async function* generateWithError() {
43
- yield 1;
44
- yield 2;
45
- throw new Error("Test error");
46
- }
47
- await expect(toArrayFromAsync(generateWithError())).rejects.toThrow("Test error");
48
- });
49
- it("should handle async iterable from array with Symbol.asyncIterator", async () => {
50
- const asyncIterable = {
51
- async *[Symbol.asyncIterator]() {
52
- for (const item of ["a", "b", "c"]) {
53
- yield item;
54
- }
55
- },
56
- };
57
- const result = await toArrayFromAsync(asyncIterable);
58
- expect(result).toEqual(["a", "b", "c"]);
59
- });
60
- it("should handle large async generator efficiently", async () => {
61
- async function* generateLargeSequence() {
62
- for (let i = 0; i < 1000; i++) {
63
- yield i;
64
- }
65
- }
66
- const result = await toArrayFromAsync(generateLargeSequence());
67
- expect(result).toHaveLength(1000);
68
- expect(result[0]).toBe(0);
69
- expect(result[999]).toBe(999);
70
- });
71
- it("should handle async generator that yields promises (promises are auto-awaited)", async () => {
72
- async function* generatePromises() {
73
- yield Promise.resolve("first");
74
- yield Promise.resolve("second");
75
- }
76
- const result = await toArrayFromAsync(generatePromises());
77
- // for await automatically awaits promises, so we get resolved values
78
- expect(result).toEqual(["first", "second"]);
79
- });
80
- it("should handle async generator that yields already resolved values", async () => {
81
- async function* generateResolvedValues() {
82
- yield await Promise.resolve("first");
83
- yield await Promise.resolve("second");
84
- }
85
- const result = await toArrayFromAsync(generateResolvedValues());
86
- // The resolved values are yielded directly
87
- expect(result).toEqual(["first", "second"]);
88
- });
89
- it("should maintain order of async generator items", async () => {
90
- async function* generateInOrder() {
91
- for (let i = 1; i <= 5; i++) {
92
- // Simulate varying delays
93
- await new Promise((resolve) => setTimeout(resolve, Math.random() * 10));
94
- yield `item-${i}`;
95
- }
96
- }
97
- const result = await toArrayFromAsync(generateInOrder());
98
- expect(result).toEqual([
99
- "item-1",
100
- "item-2",
101
- "item-3",
102
- "item-4",
103
- "item-5",
104
- ]);
105
- });
106
- });
107
- });
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=batch-utils.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"batch-utils.test.d.ts","sourceRoot":"","sources":["../../src/utils/batch-utils.test.ts"],"names":[],"mappings":""}
@@ -1,476 +0,0 @@
1
- import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
2
- import { batch } from "./batch-utils";
3
- // Mock the timers/promises module
4
- vi.mock("timers/promises", () => ({
5
- setTimeout: vi.fn(() => Promise.resolve()),
6
- }));
7
- // Mock retry-utils to control backoff behavior in tests
8
- vi.mock("./retry-utils", () => ({
9
- calculateErrorBackoffMs: vi.fn(() => 0), // No delay in tests
10
- MAX_CONSECUTIVE_ERRORS: 3,
11
- }));
12
- describe("batch", () => {
13
- beforeEach(() => {
14
- vi.clearAllMocks();
15
- });
16
- afterEach(() => {
17
- vi.clearAllMocks();
18
- });
19
- describe("basic functionality", () => {
20
- it("should execute all tasks and return settled results", async () => {
21
- const tasks = [
22
- () => Promise.resolve("result1"),
23
- () => Promise.resolve("result2"),
24
- () => Promise.resolve("result3"),
25
- ];
26
- const results = await batch(tasks);
27
- expect(results).toEqual([
28
- { status: "fulfilled", value: "result1" },
29
- { status: "fulfilled", value: "result2" },
30
- { status: "fulfilled", value: "result3" },
31
- ]);
32
- });
33
- it("should handle empty task array", async () => {
34
- const results = await batch([]);
35
- expect(results).toEqual([]);
36
- });
37
- it("should handle single task", async () => {
38
- const tasks = [() => Promise.resolve("single")];
39
- const results = await batch(tasks);
40
- expect(results).toEqual([{ status: "fulfilled", value: "single" }]);
41
- });
42
- it("should maintain result order matching input order", async () => {
43
- // Create tasks that resolve in reverse order but should maintain input order
44
- const tasks = [
45
- () => new Promise((resolve) => setTimeout(() => resolve("first"), 30)),
46
- () => new Promise((resolve) => setTimeout(() => resolve("second"), 20)),
47
- () => new Promise((resolve) => setTimeout(() => resolve("third"), 10)),
48
- ];
49
- const results = await batch(tasks);
50
- expect(results[0]).toEqual({ status: "fulfilled", value: "first" });
51
- expect(results[1]).toEqual({ status: "fulfilled", value: "second" });
52
- expect(results[2]).toEqual({ status: "fulfilled", value: "third" });
53
- });
54
- });
55
- describe("concurrency limiting", () => {
56
- it("should limit concurrent execution", async () => {
57
- let concurrentCount = 0;
58
- let maxConcurrent = 0;
59
- const createTask = () => () => {
60
- concurrentCount++;
61
- maxConcurrent = Math.max(maxConcurrent, concurrentCount);
62
- return new Promise((resolve) => {
63
- setTimeout(() => {
64
- concurrentCount--;
65
- resolve("done");
66
- }, 10);
67
- });
68
- };
69
- const tasks = Array.from({ length: 20 }, createTask);
70
- await batch(tasks, { concurrency: 5 });
71
- // Max concurrent should not exceed limit
72
- expect(maxConcurrent).toBeLessThanOrEqual(5);
73
- });
74
- it("should respect custom concurrency limit", async () => {
75
- let concurrentCount = 0;
76
- let maxConcurrent = 0;
77
- const createTask = () => () => {
78
- concurrentCount++;
79
- maxConcurrent = Math.max(maxConcurrent, concurrentCount);
80
- return new Promise((resolve) => {
81
- setTimeout(() => {
82
- concurrentCount--;
83
- resolve("done");
84
- }, 10);
85
- });
86
- };
87
- const tasks = Array.from({ length: 20 }, createTask);
88
- await batch(tasks, { concurrency: 3 });
89
- expect(maxConcurrent).toBeLessThanOrEqual(3);
90
- });
91
- it("should handle concurrency limit greater than task count", async () => {
92
- const tasks = [() => Promise.resolve(1), () => Promise.resolve(2)];
93
- const results = await batch(tasks, { concurrency: 10 });
94
- expect(results).toHaveLength(2);
95
- });
96
- });
97
- describe("error handling", () => {
98
- it("should handle failing tasks without stopping batch", async () => {
99
- const tasks = [
100
- () => Promise.resolve("success1"),
101
- () => Promise.reject(new Error("failed")),
102
- () => Promise.resolve("success2"),
103
- ];
104
- const results = await batch(tasks, { retry: false });
105
- expect(results[0]).toEqual({ status: "fulfilled", value: "success1" });
106
- expect(results[1]).toEqual({
107
- status: "rejected",
108
- reason: expect.any(Error),
109
- });
110
- expect(results[2]).toEqual({ status: "fulfilled", value: "success2" });
111
- });
112
- it("should retry failed tasks when retry is enabled", async () => {
113
- let attemptCount = 0;
114
- const failTwiceThenSucceed = () => {
115
- attemptCount++;
116
- if (attemptCount <= 2) {
117
- return Promise.reject(new Error("temporary failure"));
118
- }
119
- return Promise.resolve("success");
120
- };
121
- const tasks = [failTwiceThenSucceed];
122
- const results = await batch(tasks, { retry: true });
123
- expect(results[0]).toEqual({ status: "fulfilled", value: "success" });
124
- expect(attemptCount).toBe(3); // Initial + 2 retries
125
- });
126
- it("should give up after MAX_CONSECUTIVE_ERRORS retries", async () => {
127
- let attemptCount = 0;
128
- const alwaysFail = () => {
129
- attemptCount++;
130
- return Promise.reject(new Error("always fails"));
131
- };
132
- const tasks = [alwaysFail];
133
- const results = await batch(tasks, { retry: true });
134
- expect(results[0]).toEqual({
135
- status: "rejected",
136
- reason: expect.any(Error),
137
- });
138
- expect(attemptCount).toBe(3); // MAX_CONSECUTIVE_ERRORS
139
- });
140
- it("should not retry when retry is disabled", async () => {
141
- let attemptCount = 0;
142
- const failTask = () => {
143
- attemptCount++;
144
- return Promise.reject(new Error("failed"));
145
- };
146
- const tasks = [failTask];
147
- const results = await batch(tasks, { retry: false });
148
- expect(results[0]).toEqual({
149
- status: "rejected",
150
- reason: expect.any(Error),
151
- });
152
- expect(attemptCount).toBe(1); // No retries
153
- });
154
- });
155
- describe("options validation", () => {
156
- it("should throw error for invalid concurrency", async () => {
157
- const tasks = [() => Promise.resolve("test")];
158
- await expect(batch(tasks, { concurrency: 0 })).rejects.toThrow("Concurrency must be greater than 0");
159
- await expect(batch(tasks, { concurrency: -1 })).rejects.toThrow("Concurrency must be greater than 0");
160
- });
161
- it("should use default options when not provided", async () => {
162
- const tasks = [() => Promise.resolve("test")];
163
- const results = await batch(tasks);
164
- expect(results).toEqual([{ status: "fulfilled", value: "test" }]);
165
- });
166
- });
167
- describe("mixed success and failure scenarios", () => {
168
- it("should handle alternating success and failure", async () => {
169
- const tasks = [
170
- () => Promise.resolve("success1"),
171
- () => Promise.reject(new Error("error1")),
172
- () => Promise.resolve("success2"),
173
- () => Promise.reject(new Error("error2")),
174
- () => Promise.resolve("success3"),
175
- ];
176
- const results = await batch(tasks, { retry: false });
177
- expect(results[0].status).toBe("fulfilled");
178
- expect(results[1].status).toBe("rejected");
179
- expect(results[2].status).toBe("fulfilled");
180
- expect(results[3].status).toBe("rejected");
181
- expect(results[4].status).toBe("fulfilled");
182
- });
183
- it("should handle some tasks succeeding after retry", async () => {
184
- let task1Attempts = 0;
185
- let task2Attempts = 0;
186
- const task1 = () => {
187
- task1Attempts++;
188
- if (task1Attempts === 1) {
189
- return Promise.reject(new Error("task1 first attempt"));
190
- }
191
- return Promise.resolve("task1 success");
192
- };
193
- const task2 = () => {
194
- task2Attempts++;
195
- return Promise.resolve("task2 success");
196
- };
197
- const tasks = [task1, task2];
198
- const results = await batch(tasks, { retry: true });
199
- expect(results[0]).toEqual({
200
- status: "fulfilled",
201
- value: "task1 success",
202
- });
203
- expect(results[1]).toEqual({
204
- status: "fulfilled",
205
- value: "task2 success",
206
- });
207
- expect(task1Attempts).toBe(2); // Initial + 1 retry
208
- expect(task2Attempts).toBe(1); // No retry needed
209
- });
210
- });
211
- describe("performance characteristics", () => {
212
- it("should complete faster with higher concurrency", async () => {
213
- const delay = 50; // ms
214
- const taskCount = 10;
215
- const tasks = Array.from({ length: taskCount }, () => () => new Promise((resolve) => setTimeout(() => resolve("done"), delay)));
216
- // Low concurrency (sequential-ish)
217
- const start1 = Date.now();
218
- await batch([...tasks], { concurrency: 1 });
219
- const duration1 = Date.now() - start1;
220
- // High concurrency (parallel)
221
- const start2 = Date.now();
222
- await batch([...tasks], { concurrency: 10 });
223
- const duration2 = Date.now() - start2;
224
- // High concurrency should be significantly faster
225
- // Allow some margin for timing variability
226
- expect(duration2).toBeLessThan(duration1 * 0.8);
227
- });
228
- });
229
- describe("result ordering guarantees", () => {
230
- it("should maintain exact order with out-of-order completion and mixed results", async () => {
231
- // This is the ultimate ordering test - combining:
232
- // 1. Tasks that complete in reverse order (via delays)
233
- // 2. Mixed success/failure
234
- // 3. High concurrency (multiple workers racing)
235
- const executionLog = [];
236
- const tasks = [
237
- // Task 0: Slow success
238
- () => new Promise((resolve) => {
239
- setTimeout(() => {
240
- executionLog.push("task0-complete");
241
- resolve("result0");
242
- }, 100);
243
- }),
244
- // Task 1: Fast failure
245
- () => new Promise((_, reject) => {
246
- setTimeout(() => {
247
- executionLog.push("task1-complete");
248
- reject(new Error("error1"));
249
- }, 10);
250
- }),
251
- // Task 2: Medium success
252
- () => new Promise((resolve) => {
253
- setTimeout(() => {
254
- executionLog.push("task2-complete");
255
- resolve("result2");
256
- }, 50);
257
- }),
258
- // Task 3: Instant failure
259
- () => new Promise((_, reject) => {
260
- executionLog.push("task3-complete");
261
- reject(new Error("error3"));
262
- }),
263
- // Task 4: Medium-slow success
264
- () => new Promise((resolve) => {
265
- setTimeout(() => {
266
- executionLog.push("task4-complete");
267
- resolve("result4");
268
- }, 75);
269
- }),
270
- ];
271
- const results = await batch(tasks, { concurrency: 5, retry: false });
272
- // Verify all tasks completed
273
- expect(executionLog).toHaveLength(5);
274
- expect(executionLog).toContain("task0-complete");
275
- expect(executionLog).toContain("task1-complete");
276
- expect(executionLog).toContain("task2-complete");
277
- expect(executionLog).toContain("task3-complete");
278
- expect(executionLog).toContain("task4-complete");
279
- // BUT results MUST maintain input order despite out-of-order completion
280
- expect(results).toHaveLength(5);
281
- // Task 0: position 0, slow success
282
- expect(results[0]).toEqual({
283
- status: "fulfilled",
284
- value: "result0",
285
- });
286
- // Task 1: position 1, fast failure
287
- expect(results[1]).toEqual({
288
- status: "rejected",
289
- reason: expect.objectContaining({
290
- message: "error1",
291
- }),
292
- });
293
- // Task 2: position 2, medium success
294
- expect(results[2]).toEqual({
295
- status: "fulfilled",
296
- value: "result2",
297
- });
298
- // Task 3: position 3, instant failure
299
- expect(results[3]).toEqual({
300
- status: "rejected",
301
- reason: expect.objectContaining({
302
- message: "error3",
303
- }),
304
- });
305
- // Task 4: position 4, medium-slow success
306
- expect(results[4]).toEqual({
307
- status: "fulfilled",
308
- value: "result4",
309
- });
310
- });
311
- it("should match Promise.allSettled ordering behavior exactly", async () => {
312
- // This test ensures our batch function has identical ordering to Promise.allSettled
313
- const tasks = [
314
- () => new Promise((resolve) => setTimeout(() => resolve("a"), 30)),
315
- () => new Promise((_, reject) => setTimeout(() => reject(new Error("b")), 10)),
316
- () => new Promise((resolve) => setTimeout(() => resolve("c"), 20)),
317
- ];
318
- // Run both in parallel
319
- const [batchResults, settledResults] = await Promise.all([
320
- batch(tasks.map((t) => t), { concurrency: 3, retry: false }),
321
- Promise.allSettled(tasks.map((t) => t())),
322
- ]);
323
- // Results should have same structure
324
- expect(batchResults).toHaveLength(settledResults.length);
325
- // Compare each result
326
- for (let i = 0; i < batchResults.length; i++) {
327
- const batchResult = batchResults[i];
328
- const settledResult = settledResults[i];
329
- expect(batchResult.status).toBe(settledResult.status);
330
- if (batchResult.status === "fulfilled" &&
331
- settledResult.status === "fulfilled") {
332
- expect(batchResult.value).toBe(settledResult.value);
333
- }
334
- if (batchResult.status === "rejected" &&
335
- settledResult.status === "rejected") {
336
- expect(batchResult.reason).toBeInstanceOf(Error);
337
- expect(settledResult.reason).toBeInstanceOf(Error);
338
- }
339
- }
340
- });
341
- });
342
- describe("timeout handling", () => {
343
- beforeEach(async () => {
344
- // Unmock setTimeout for timeout tests to allow real timing
345
- vi.unmock("timers/promises");
346
- // Re-import batch to get the non-mocked version
347
- vi.resetModules();
348
- });
349
- afterEach(() => {
350
- // Restore mocks after timeout tests
351
- vi.doMock("timers/promises", () => ({
352
- setTimeout: vi.fn(() => Promise.resolve()),
353
- }));
354
- });
355
- it("should throw error when overall batch timeout is exceeded", async () => {
356
- const { batch } = await import("./batch-utils");
357
- const tasks = Array.from({ length: 10 }, () => () => new Promise((resolve) => setTimeout(() => resolve("done"), 100)));
358
- await expect(batch(tasks, { concurrency: 2, timeoutMs: 50 })).rejects.toThrow("Batch operation timed out");
359
- });
360
- it("should timeout individual tasks when taskTimeoutMs is set", async () => {
361
- const { batch } = await import("./batch-utils");
362
- const tasks = [
363
- () => Promise.resolve("instant"), // Instant task - should not timeout
364
- () => new Promise((resolve) => setTimeout(() => resolve("slow"), 500)), // Slow task - should timeout
365
- () => Promise.resolve("instant2"), // Another instant task - should not timeout
366
- ];
367
- const results = await batch(tasks, {
368
- taskTimeoutMs: 100, // 100ms timeout
369
- retry: false,
370
- batchDelay: 0, // Disable batch delay for timing reliability
371
- });
372
- expect(results[0].status).toBe("fulfilled");
373
- expect(results[1].status).toBe("rejected");
374
- if (results[1].status === "rejected") {
375
- expect(results[1].reason).toBeInstanceOf(Error);
376
- expect(results[1].reason.message).toContain("timed out");
377
- }
378
- expect(results[2].status).toBe("fulfilled");
379
- });
380
- it("should not retry tasks that timeout", async () => {
381
- const { batch } = await import("./batch-utils");
382
- let attemptCount = 0;
383
- const slowTask = () => {
384
- attemptCount++;
385
- return new Promise((resolve) => setTimeout(() => resolve("done"), 200));
386
- };
387
- const results = await batch([slowTask], {
388
- taskTimeoutMs: 50,
389
- retry: true,
390
- });
391
- expect(results[0].status).toBe("rejected");
392
- expect(attemptCount).toBe(1); // Should not retry timeout errors
393
- });
394
- it("should validate timeout parameters", async () => {
395
- const { batch } = await import("./batch-utils");
396
- const tasks = [() => Promise.resolve("test")];
397
- await expect(batch(tasks, { timeoutMs: 0 })).rejects.toThrow("Timeout must be greater than 0");
398
- await expect(batch(tasks, { timeoutMs: -100 })).rejects.toThrow("Timeout must be greater than 0");
399
- await expect(batch(tasks, { taskTimeoutMs: 0 })).rejects.toThrow("Task timeout must be greater than 0");
400
- await expect(batch(tasks, { taskTimeoutMs: -100 })).rejects.toThrow("Task timeout must be greater than 0");
401
- });
402
- it("should complete tasks that finish before overall timeout", async () => {
403
- const { batch } = await import("./batch-utils");
404
- const tasks = [
405
- () => new Promise((resolve) => setTimeout(() => resolve("task1"), 10)),
406
- () => new Promise((resolve) => setTimeout(() => resolve("task2"), 20)),
407
- () => new Promise((resolve) => setTimeout(() => resolve("task3"), 30)),
408
- ];
409
- const results = await batch(tasks, {
410
- timeoutMs: 100,
411
- concurrency: 3,
412
- });
413
- expect(results).toHaveLength(3);
414
- expect(results[0].status).toBe("fulfilled");
415
- expect(results[1].status).toBe("fulfilled");
416
- expect(results[2].status).toBe("fulfilled");
417
- });
418
- it("should report tasks remaining when batch timeout occurs", async () => {
419
- const { batch } = await import("./batch-utils");
420
- const tasks = Array.from({ length: 20 }, () => () => new Promise((resolve) => setTimeout(() => resolve("done"), 100)));
421
- try {
422
- await batch(tasks, { concurrency: 2, timeoutMs: 50 });
423
- expect.fail("Should have thrown timeout error");
424
- }
425
- catch (error) {
426
- expect(error).toBeInstanceOf(Error);
427
- expect(error.message).toContain("task(s) not completed");
428
- }
429
- });
430
- it("should allow taskTimeoutMs without overall timeout affecting quick tasks", async () => {
431
- const { batch } = await import("./batch-utils");
432
- const tasks = [
433
- () => Promise.resolve("instant1"),
434
- () => Promise.resolve("instant2"),
435
- () => Promise.resolve("instant3"),
436
- ];
437
- const results = await batch(tasks, {
438
- taskTimeoutMs: 1000,
439
- timeoutMs: 5000,
440
- });
441
- expect(results).toHaveLength(3);
442
- expect(results.every((r) => r.status === "fulfilled")).toBe(true);
443
- });
444
- it("should handle mix of timed out and successful tasks", async () => {
445
- const { batch } = await import("./batch-utils");
446
- const tasks = [
447
- () => Promise.resolve("fast1"), // Instant - should not timeout
448
- () => new Promise((resolve) => setTimeout(() => resolve("slow1"), 500)), // Slow - should timeout
449
- () => Promise.resolve("fast2"), // Instant - should not timeout
450
- () => new Promise((resolve) => setTimeout(() => resolve("slow2"), 500)), // Slow - should timeout
451
- ];
452
- const results = await batch(tasks, {
453
- taskTimeoutMs: 100,
454
- retry: false,
455
- concurrency: 4,
456
- batchDelay: 0, // Disable batch delay for timing reliability
457
- });
458
- expect(results[0].status).toBe("fulfilled");
459
- expect(results[1].status).toBe("rejected");
460
- expect(results[2].status).toBe("fulfilled");
461
- expect(results[3].status).toBe("rejected");
462
- });
463
- it("should use default timeout when not specified", async () => {
464
- const { batch } = await import("./batch-utils");
465
- const tasks = [
466
- () => Promise.resolve("test1"),
467
- () => Promise.resolve("test2"),
468
- ];
469
- // Should not throw with default 3-minute timeout
470
- const results = await batch(tasks);
471
- expect(results).toHaveLength(2);
472
- expect(results[0].status).toBe("fulfilled");
473
- expect(results[1].status).toBe("fulfilled");
474
- });
475
- });
476
- });
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=domain-utils.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"domain-utils.test.d.ts","sourceRoot":"","sources":["../../src/utils/domain-utils.test.ts"],"names":[],"mappings":""}