|
1 | 1 | // DashScope-compatible download regressions: body idle after headers. |
2 | | -import { once } from "node:events"; |
3 | | -import http from "node:http"; |
4 | | -import { afterEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { describe, expect, it, vi } from "vitest"; |
5 | 3 | import { downloadDashscopeGeneratedVideos } from "./dashscope-compatible.js"; |
6 | 4 |
|
7 | 5 | function neverChunkingVideoResponse(): Response { |
@@ -100,56 +98,46 @@ describe("downloadDashscopeGeneratedVideos", () => { |
100 | 98 | // Exhausted deadline is checked before fetch — no network I/O is initiated. |
101 | 99 | expect(fetchFn).not.toHaveBeenCalled(); |
102 | 100 | }); |
103 | | -}); |
104 | | - |
105 | | -describe("downloadDashscopeGeneratedVideos loopback HTTP", () => { |
106 | | - let server: http.Server | undefined; |
107 | 101 |
|
108 | | - afterEach(async () => { |
109 | | - if (!server) { |
110 | | - return; |
| 102 | + it("releases the guarded fetch when the remaining-budget resolver throws", async () => { |
| 103 | + vi.useFakeTimers(); |
| 104 | + try { |
| 105 | + let requestSignal: AbortSignal | undefined; |
| 106 | + const cancelBody = vi.fn(); |
| 107 | + const timeoutMs = vi |
| 108 | + .fn<() => number>() |
| 109 | + .mockReturnValueOnce(100) |
| 110 | + .mockImplementationOnce(() => { |
| 111 | + throw new Error("remaining-budget resolver failed"); |
| 112 | + }); |
| 113 | + const fetchFn = vi.fn(async (_url: string | URL | Request, init?: RequestInit) => { |
| 114 | + requestSignal = init?.signal ?? undefined; |
| 115 | + return new Response(new ReadableStream({ cancel: cancelBody }), { |
| 116 | + status: 200, |
| 117 | + headers: { "content-type": "video/mp4" }, |
| 118 | + }); |
| 119 | + }); |
| 120 | + |
| 121 | + await expect( |
| 122 | + downloadDashscopeGeneratedVideos({ |
| 123 | + providerLabel: "Alibaba Wan", |
| 124 | + urls: ["https://example.com/out.mp4"], |
| 125 | + timeoutMs, |
| 126 | + fetchFn: fetchFn as typeof fetch, |
| 127 | + maxBytes: 10 * 1024 * 1024, |
| 128 | + }), |
| 129 | + ).rejects.toThrow("remaining-budget resolver failed"); |
| 130 | + |
| 131 | + expect(timeoutMs).toHaveBeenCalledTimes(2); |
| 132 | + expect(cancelBody).toHaveBeenCalledOnce(); |
| 133 | + expect(cancelBody.mock.calls[0]?.[0]).toMatchObject({ |
| 134 | + message: "remaining-budget resolver failed", |
| 135 | + }); |
| 136 | + expect(requestSignal?.aborted).toBe(false); |
| 137 | + await vi.advanceTimersByTimeAsync(100); |
| 138 | + expect(requestSignal?.aborted).toBe(false); |
| 139 | + } finally { |
| 140 | + vi.useRealTimers(); |
111 | 141 | } |
112 | | - server.closeAllConnections?.(); |
113 | | - server.close(); |
114 | | - await once(server, "close").catch(() => undefined); |
115 | | - server = undefined; |
116 | | - }); |
117 | | - |
118 | | - it("bounds a stalled loopback HTTP body with a real server", async () => { |
119 | | - server = http.createServer((_req, res) => { |
120 | | - res.writeHead(200, { "content-type": "video/mp4" }); |
121 | | - // Write one chunk so fetch resolves (Node fetch hangs until first body byte). |
122 | | - // Then never write more — simulates stalled CDN. |
123 | | - res.write("first-chunk"); |
124 | | - }); |
125 | | - server.on("clientError", (_err, socket) => socket.destroy()); |
126 | | - server.listen(0, "127.0.0.1"); |
127 | | - await once(server, "listening"); |
128 | | - const address = server.address(); |
129 | | - if (!address || typeof address === "string") { |
130 | | - throw new Error("expected loopback server address"); |
131 | | - } |
132 | | - const port = address.port; |
133 | | - |
134 | | - const timeoutMs = 200; |
135 | | - const startedAt = Date.now(); |
136 | | - |
137 | | - // Accept either error because the fetch-level AbortSignal timeout and |
138 | | - // the post-header chunk-idle timeout race; both are bounded within the |
139 | | - // configured budget. |
140 | | - await expect( |
141 | | - downloadDashscopeGeneratedVideos({ |
142 | | - providerLabel: "Alibaba Wan", |
143 | | - urls: [`http://127.0.0.1:${port}/out.mp4`], |
144 | | - timeoutMs, |
145 | | - fetchFn: fetch, |
146 | | - allowPrivateNetwork: true, |
147 | | - maxBytes: 10 * 1024 * 1024, |
148 | | - }), |
149 | | - ).rejects.toThrow(); |
150 | | - |
151 | | - const elapsedMs = Date.now() - startedAt; |
152 | | - // Bounded proof: the download rejects within 5s, not an indefinite hang. |
153 | | - expect(elapsedMs).toBeLessThan(5_000); |
154 | 142 | }); |
155 | 143 | }); |
0 commit comments