|
| 1 | +import{typeAxiosInstance,typeAxiosResponse}from"axios"; |
| 2 | +import{typeReaderLike}from"eventsource"; |
| 3 | +import{EventEmitter}from"node:events"; |
| 4 | +import{typeIncomingMessage}from"node:http"; |
| 5 | +import{describe,it,expect,vi}from"vitest"; |
| 6 | + |
| 7 | +import{createStreamingFetchAdapter}from"@/api/streamingFetchAdapter"; |
| 8 | + |
| 9 | +constTEST_URL="https://example.com/api"; |
| 10 | + |
| 11 | +describe("createStreamingFetchAdapter",()=>{ |
| 12 | +describe("Request Handling",()=>{ |
| 13 | +it("passes URL, signal, and responseType to axios",async()=>{ |
| 14 | +constmockAxios=createAxiosMock(); |
| 15 | +constmockStream=createMockStream(); |
| 16 | +setupAxiosResponse(mockAxios,200,{},mockStream); |
| 17 | + |
| 18 | +constadapter=createStreamingFetchAdapter(mockAxios); |
| 19 | +constsignal=newAbortController().signal; |
| 20 | + |
| 21 | +awaitadapter(TEST_URL,{ signal}); |
| 22 | + |
| 23 | +expect(mockAxios.request).toHaveBeenCalledWith({ |
| 24 | +url:TEST_URL, |
| 25 | +signal,// correctly passes signal |
| 26 | +headers:{}, |
| 27 | +responseType:"stream", |
| 28 | +validateStatus:expect.any(Function), |
| 29 | +}); |
| 30 | +}); |
| 31 | + |
| 32 | +it("applies headers in correct precedence order (config overrides init)",async()=>{ |
| 33 | +constmockAxios=createAxiosMock(); |
| 34 | +constmockStream=createMockStream(); |
| 35 | +setupAxiosResponse(mockAxios,200,{},mockStream); |
| 36 | + |
| 37 | +// Test 1: No config headers, only init headers |
| 38 | +constadapter1=createStreamingFetchAdapter(mockAxios); |
| 39 | +awaitadapter1(TEST_URL,{ |
| 40 | +headers:{"X-Init":"init-value"}, |
| 41 | +}); |
| 42 | + |
| 43 | +expect(mockAxios.request).toHaveBeenCalledWith( |
| 44 | +expect.objectContaining({ |
| 45 | +headers:{"X-Init":"init-value"}, |
| 46 | +}), |
| 47 | +); |
| 48 | + |
| 49 | +// Test 2: Config headers merge with init headers |
| 50 | +constadapter2=createStreamingFetchAdapter(mockAxios,{ |
| 51 | +"X-Config":"config-value", |
| 52 | +}); |
| 53 | +awaitadapter2(TEST_URL,{ |
| 54 | +headers:{"X-Init":"init-value"}, |
| 55 | +}); |
| 56 | + |
| 57 | +expect(mockAxios.request).toHaveBeenCalledWith( |
| 58 | +expect.objectContaining({ |
| 59 | +headers:{ |
| 60 | +"X-Init":"init-value", |
| 61 | +"X-Config":"config-value", |
| 62 | +}, |
| 63 | +}), |
| 64 | +); |
| 65 | + |
| 66 | +// Test 3: Config headers override init headers |
| 67 | +constadapter3=createStreamingFetchAdapter(mockAxios,{ |
| 68 | +"X-Header":"config-value", |
| 69 | +}); |
| 70 | +awaitadapter3(TEST_URL,{ |
| 71 | +headers:{"X-Header":"init-value"}, |
| 72 | +}); |
| 73 | + |
| 74 | +expect(mockAxios.request).toHaveBeenCalledWith( |
| 75 | +expect.objectContaining({ |
| 76 | +headers:{"X-Header":"config-value"}, |
| 77 | +}), |
| 78 | +); |
| 79 | +}); |
| 80 | +}); |
| 81 | + |
| 82 | +describe("Response Properties",()=>{ |
| 83 | +it("returns response with correct properties",async()=>{ |
| 84 | +constmockAxios=createAxiosMock(); |
| 85 | +constmockStream=createMockStream(); |
| 86 | +setupAxiosResponse( |
| 87 | +mockAxios, |
| 88 | +200, |
| 89 | +{"content-type":"text/event-stream"}, |
| 90 | +mockStream, |
| 91 | +); |
| 92 | + |
| 93 | +constadapter=createStreamingFetchAdapter(mockAxios); |
| 94 | +constresponse=awaitadapter(TEST_URL); |
| 95 | + |
| 96 | +expect(response.url).toBe(TEST_URL); |
| 97 | +expect(response.status).toBe(200); |
| 98 | +expect(response.headers.get("content-type")).toBe("text/event-stream"); |
| 99 | +// Headers are lowercased when we retrieve them |
| 100 | +expect(response.headers.get("CoNtEnT-TyPe")).toBe("text/event-stream"); |
| 101 | +expect(response.body?.getReader).toBeDefined(); |
| 102 | +}); |
| 103 | + |
| 104 | +it("detects redirected requests",async()=>{ |
| 105 | +constmockAxios=createAxiosMock(); |
| 106 | +constmockStream=createMockStream(); |
| 107 | +constmockResponse={ |
| 108 | +status:200, |
| 109 | +headers:{}, |
| 110 | +data:mockStream, |
| 111 | +request:{ |
| 112 | +res:{ |
| 113 | +responseUrl:"https://redirect.com/api", |
| 114 | +}, |
| 115 | +}, |
| 116 | +}asAxiosResponse<IncomingMessage>; |
| 117 | +vi.mocked(mockAxios.request).mockResolvedValue(mockResponse); |
| 118 | + |
| 119 | +constadapter=createStreamingFetchAdapter(mockAxios); |
| 120 | +constresponse=awaitadapter(TEST_URL); |
| 121 | + |
| 122 | +expect(response.redirected).toBe(true); |
| 123 | +}); |
| 124 | +}); |
| 125 | + |
| 126 | +describe("Stream Handling",()=>{ |
| 127 | +it("enqueues data chunks from stream",async()=>{ |
| 128 | +const{ mockStream, reader}=awaitsetupReaderTest(); |
| 129 | + |
| 130 | +constchunk1=Buffer.from("data1"); |
| 131 | +constchunk2=Buffer.from("data2"); |
| 132 | +mockStream.emit("data",chunk1); |
| 133 | +mockStream.emit("data",chunk2); |
| 134 | +mockStream.emit("end"); |
| 135 | + |
| 136 | +constresult1=awaitreader.read(); |
| 137 | +expect(result1.value).toEqual(chunk1); |
| 138 | +expect(result1.done).toBe(false); |
| 139 | + |
| 140 | +constresult2=awaitreader.read(); |
| 141 | +expect(result2.value).toEqual(chunk2); |
| 142 | +expect(result2.done).toBe(false); |
| 143 | + |
| 144 | +constresult3=awaitreader.read(); |
| 145 | +// Closed after end |
| 146 | +expect(result3.done).toBe(true); |
| 147 | +}); |
| 148 | + |
| 149 | +it("propagates stream errors",async()=>{ |
| 150 | +const{ mockStream, reader}=awaitsetupReaderTest(); |
| 151 | + |
| 152 | +consterror=newError("Stream error"); |
| 153 | +mockStream.emit("error",error); |
| 154 | + |
| 155 | +awaitexpect(reader.read()).rejects.toThrow("Stream error"); |
| 156 | +}); |
| 157 | + |
| 158 | +it("handles errors after stream is closed",async()=>{ |
| 159 | +const{ mockStream, reader}=awaitsetupReaderTest(); |
| 160 | + |
| 161 | +mockStream.emit("end"); |
| 162 | +awaitreader.read(); |
| 163 | + |
| 164 | +// Emit events after stream is closed - should not throw |
| 165 | +expect(()=>mockStream.emit("data",Buffer.from("late"))).not.toThrow(); |
| 166 | +expect(()=>mockStream.emit("end")).not.toThrow(); |
| 167 | +}); |
| 168 | + |
| 169 | +it("destroys stream on cancel",async()=>{ |
| 170 | +const{ mockStream, reader}=awaitsetupReaderTest(); |
| 171 | + |
| 172 | +awaitreader.cancel(); |
| 173 | + |
| 174 | +expect(mockStream.destroy).toHaveBeenCalled(); |
| 175 | +}); |
| 176 | +}); |
| 177 | +}); |
| 178 | + |
| 179 | +functioncreateAxiosMock():AxiosInstance{ |
| 180 | +return{ |
| 181 | +request:vi.fn(), |
| 182 | +}asunknownasAxiosInstance; |
| 183 | +} |
| 184 | + |
| 185 | +functioncreateMockStream():IncomingMessage{ |
| 186 | +conststream=newEventEmitter()asIncomingMessage; |
| 187 | +stream.destroy=vi.fn(); |
| 188 | +returnstream; |
| 189 | +} |
| 190 | + |
| 191 | +functionsetupAxiosResponse( |
| 192 | +axios:AxiosInstance, |
| 193 | +status:number, |
| 194 | +headers:Record<string,string>, |
| 195 | +stream:IncomingMessage, |
| 196 | +):void{ |
| 197 | +vi.mocked(axios.request).mockResolvedValue({ |
| 198 | +status, |
| 199 | +headers, |
| 200 | +data:stream, |
| 201 | +}); |
| 202 | +} |
| 203 | + |
| 204 | +asyncfunctionsetupReaderTest():Promise<{ |
| 205 | +mockStream:IncomingMessage; |
| 206 | +reader:ReaderLike|ReadableStreamDefaultReader<Uint8Array<ArrayBuffer>>; |
| 207 | +}>{ |
| 208 | +constmockAxios=createAxiosMock(); |
| 209 | +constmockStream=createMockStream(); |
| 210 | +setupAxiosResponse(mockAxios,200,{},mockStream); |
| 211 | + |
| 212 | +constadapter=createStreamingFetchAdapter(mockAxios); |
| 213 | +constresponse=awaitadapter(TEST_URL); |
| 214 | +constreader=response.body?.getReader(); |
| 215 | +if(reader===undefined){ |
| 216 | +thrownewError("Reader is undefined"); |
| 217 | +} |
| 218 | + |
| 219 | +return{ mockStream, reader}; |
| 220 | +} |