|
| 1 | +import { describe, it, expect, vi, afterEach } from "vitest"; |
| 2 | +import { ApiClient } from "./index.js"; |
| 3 | +import { BatchNotSealedError } from "./errors.js"; |
| 4 | + |
| 5 | +vi.setConfig({ testTimeout: 10_000 }); |
| 6 | + |
| 7 | +describe("streamBatchItems unsealed handling", () => { |
| 8 | + const originalFetch = globalThis.fetch; |
| 9 | + |
| 10 | + afterEach(() => { |
| 11 | + globalThis.fetch = originalFetch; |
| 12 | + vi.restoreAllMocks(); |
| 13 | + }); |
| 14 | + |
| 15 | + /** |
| 16 | + * Helper to create a mock fetch that properly consumes the request body stream. |
| 17 | + * This is necessary because streamBatchItems sends a ReadableStream body. |
| 18 | + */ |
| 19 | + function createMockFetch( |
| 20 | + responses: Array<{ |
| 21 | + id: string; |
| 22 | + itemsAccepted: number; |
| 23 | + itemsDeduplicated: number; |
| 24 | + sealed: boolean; |
| 25 | + enqueuedCount?: number; |
| 26 | + expectedCount?: number; |
| 27 | + }> |
| 28 | + ) { |
| 29 | + let callIndex = 0; |
| 30 | + return vi.fn().mockImplementation(async (_url: string, init?: RequestInit) => { |
| 31 | + // Consume the request body stream to prevent hanging |
| 32 | + if (init?.body && init.body instanceof ReadableStream) { |
| 33 | + const reader = init.body.getReader(); |
| 34 | + // Drain the stream |
| 35 | + while (true) { |
| 36 | + const { done } = await reader.read(); |
| 37 | + if (done) break; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + const responseData = responses[Math.min(callIndex, responses.length - 1)]; |
| 42 | + callIndex++; |
| 43 | + |
| 44 | + return { |
| 45 | + ok: true, |
| 46 | + json: () => Promise.resolve(responseData), |
| 47 | + }; |
| 48 | + }); |
| 49 | + } |
| 50 | + |
| 51 | + it("throws BatchNotSealedError when sealed=false after retries exhausted", async () => { |
| 52 | + const mockFetch = createMockFetch([ |
| 53 | + { |
| 54 | + id: "batch_test123", |
| 55 | + itemsAccepted: 5, |
| 56 | + itemsDeduplicated: 0, |
| 57 | + sealed: false, |
| 58 | + enqueuedCount: 5, |
| 59 | + expectedCount: 10, |
| 60 | + }, |
| 61 | + ]); |
| 62 | + globalThis.fetch = mockFetch; |
| 63 | + |
| 64 | + const client = new ApiClient("http://localhost:3030", "tr_test_key"); |
| 65 | + |
| 66 | + const error = await client |
| 67 | + .streamBatchItems( |
| 68 | + "batch_test123", |
| 69 | + [{ index: 0, task: "test-task", payload: "{}" }], |
| 70 | + { retry: { maxAttempts: 2, minDelay: 10, maxDelay: 50 } } |
| 71 | + ) |
| 72 | + .catch((e) => e); |
| 73 | + |
| 74 | + expect(error).toBeInstanceOf(BatchNotSealedError); |
| 75 | + expect((error as BatchNotSealedError).batchId).toBe("batch_test123"); |
| 76 | + expect((error as BatchNotSealedError).enqueuedCount).toBe(5); |
| 77 | + expect((error as BatchNotSealedError).expectedCount).toBe(10); |
| 78 | + expect((error as BatchNotSealedError).itemsAccepted).toBe(5); |
| 79 | + expect((error as BatchNotSealedError).itemsDeduplicated).toBe(0); |
| 80 | + |
| 81 | + // Should have retried (2 attempts total based on maxAttempts) |
| 82 | + expect(mockFetch).toHaveBeenCalledTimes(2); |
| 83 | + }); |
| 84 | + |
| 85 | + it("retries when sealed=false and succeeds when sealed=true on retry", async () => { |
| 86 | + const mockFetch = createMockFetch([ |
| 87 | + // First response: not sealed |
| 88 | + { |
| 89 | + id: "batch_test123", |
| 90 | + itemsAccepted: 5, |
| 91 | + itemsDeduplicated: 0, |
| 92 | + sealed: false, |
| 93 | + enqueuedCount: 5, |
| 94 | + expectedCount: 10, |
| 95 | + }, |
| 96 | + // Second response: sealed |
| 97 | + { |
| 98 | + id: "batch_test123", |
| 99 | + itemsAccepted: 5, |
| 100 | + itemsDeduplicated: 0, |
| 101 | + sealed: true, |
| 102 | + }, |
| 103 | + ]); |
| 104 | + globalThis.fetch = mockFetch; |
| 105 | + |
| 106 | + const client = new ApiClient("http://localhost:3030", "tr_test_key"); |
| 107 | + |
| 108 | + const result = await client.streamBatchItems( |
| 109 | + "batch_test123", |
| 110 | + [{ index: 0, task: "test-task", payload: "{}" }], |
| 111 | + { retry: { maxAttempts: 3, minDelay: 10, maxDelay: 50 } } |
| 112 | + ); |
| 113 | + |
| 114 | + expect(result.sealed).toBe(true); |
| 115 | + // Should have been called twice (first unsealed, second sealed) |
| 116 | + expect(mockFetch).toHaveBeenCalledTimes(2); |
| 117 | + }); |
| 118 | + |
| 119 | + it("succeeds immediately when sealed=true on first attempt", async () => { |
| 120 | + const mockFetch = createMockFetch([ |
| 121 | + { |
| 122 | + id: "batch_test123", |
| 123 | + itemsAccepted: 10, |
| 124 | + itemsDeduplicated: 0, |
| 125 | + sealed: true, |
| 126 | + }, |
| 127 | + ]); |
| 128 | + globalThis.fetch = mockFetch; |
| 129 | + |
| 130 | + const client = new ApiClient("http://localhost:3030", "tr_test_key"); |
| 131 | + |
| 132 | + const result = await client.streamBatchItems("batch_test123", [ |
| 133 | + { index: 0, task: "test-task", payload: "{}" }, |
| 134 | + ]); |
| 135 | + |
| 136 | + expect(result.sealed).toBe(true); |
| 137 | + expect(result.itemsAccepted).toBe(10); |
| 138 | + // Should only be called once |
| 139 | + expect(mockFetch).toHaveBeenCalledTimes(1); |
| 140 | + }); |
| 141 | + |
| 142 | + it("BatchNotSealedError has descriptive message", async () => { |
| 143 | + const mockFetch = createMockFetch([ |
| 144 | + { |
| 145 | + id: "batch_abc123", |
| 146 | + itemsAccepted: 7, |
| 147 | + itemsDeduplicated: 2, |
| 148 | + sealed: false, |
| 149 | + enqueuedCount: 9, |
| 150 | + expectedCount: 15, |
| 151 | + }, |
| 152 | + ]); |
| 153 | + globalThis.fetch = mockFetch; |
| 154 | + |
| 155 | + const client = new ApiClient("http://localhost:3030", "tr_test_key"); |
| 156 | + |
| 157 | + const error = await client |
| 158 | + .streamBatchItems( |
| 159 | + "batch_abc123", |
| 160 | + [{ index: 0, task: "test-task", payload: "{}" }], |
| 161 | + { retry: { maxAttempts: 1, minDelay: 10, maxDelay: 50 } } |
| 162 | + ) |
| 163 | + .catch((e) => e); |
| 164 | + |
| 165 | + expect(error).toBeInstanceOf(BatchNotSealedError); |
| 166 | + expect(error.message).toContain("batch_abc123"); |
| 167 | + expect(error.message).toContain("9 of 15"); |
| 168 | + expect(error.message).toContain("accepted: 7"); |
| 169 | + expect(error.message).toContain("deduplicated: 2"); |
| 170 | + }); |
| 171 | + |
| 172 | + it("handles missing enqueuedCount and expectedCount gracefully", async () => { |
| 173 | + // Simulate older server response that might not include these fields |
| 174 | + const mockFetch = createMockFetch([ |
| 175 | + { |
| 176 | + id: "batch_test123", |
| 177 | + itemsAccepted: 5, |
| 178 | + itemsDeduplicated: 0, |
| 179 | + sealed: false, |
| 180 | + // No enqueuedCount or expectedCount |
| 181 | + }, |
| 182 | + ]); |
| 183 | + globalThis.fetch = mockFetch; |
| 184 | + |
| 185 | + const client = new ApiClient("http://localhost:3030", "tr_test_key"); |
| 186 | + |
| 187 | + const error = await client |
| 188 | + .streamBatchItems( |
| 189 | + "batch_test123", |
| 190 | + [{ index: 0, task: "test-task", payload: "{}" }], |
| 191 | + { retry: { maxAttempts: 1, minDelay: 10, maxDelay: 50 } } |
| 192 | + ) |
| 193 | + .catch((e) => e); |
| 194 | + |
| 195 | + expect(error).toBeInstanceOf(BatchNotSealedError); |
| 196 | + // Should default to 0 when not provided |
| 197 | + expect((error as BatchNotSealedError).enqueuedCount).toBe(0); |
| 198 | + expect((error as BatchNotSealedError).expectedCount).toBe(0); |
| 199 | + }); |
| 200 | +}); |
0 commit comments