|
1 | 1 | import { afterEach, describe, expect, it, vi } from 'vitest'; |
2 | | -import { encodeMultiChunks, MAX_CHUNKS_PER_REQUEST } from './streamer.js'; |
| 2 | +import { |
| 3 | + encodeMultiChunks, |
| 4 | + MAX_CHUNKS_PER_REQUEST, |
| 5 | + parseStreamControlFrame, |
| 6 | + STREAM_CONTROL_FRAME_SIZE, |
| 7 | +} from './streamer.js'; |
3 | 8 |
|
4 | 9 | describe('encodeMultiChunks', () => { |
5 | 10 | /** |
@@ -269,3 +274,198 @@ describe('writeToStreamMulti pagination', () => { |
269 | 274 | ]); |
270 | 275 | }); |
271 | 276 | }); |
| 277 | + |
| 278 | +/** |
| 279 | + * Build a control frame matching the workflow-server format. |
| 280 | + */ |
| 281 | +function buildControlFrame(done: boolean, nextIndex: number): Uint8Array { |
| 282 | + const frame = new Uint8Array(STREAM_CONTROL_FRAME_SIZE); |
| 283 | + // Bytes 0-3: zero-frame marker (already 0x00) |
| 284 | + frame[4] = done ? 1 : 0; |
| 285 | + new DataView(frame.buffer).setUint32(5, nextIndex, false); |
| 286 | + // Magic footer "WFCT" |
| 287 | + frame.set(new Uint8Array([0x57, 0x46, 0x43, 0x54]), 9); |
| 288 | + return frame; |
| 289 | +} |
| 290 | + |
| 291 | +describe('parseStreamControlFrame', () => { |
| 292 | + it('parses a valid done=true control frame', () => { |
| 293 | + const frame = buildControlFrame(true, 42); |
| 294 | + const result = parseStreamControlFrame(frame); |
| 295 | + expect(result).toEqual({ |
| 296 | + done: true, |
| 297 | + nextIndex: 42, |
| 298 | + totalLength: STREAM_CONTROL_FRAME_SIZE, |
| 299 | + }); |
| 300 | + }); |
| 301 | + |
| 302 | + it('parses a valid done=false (timeout) control frame', () => { |
| 303 | + const frame = buildControlFrame(false, 100); |
| 304 | + const result = parseStreamControlFrame(frame); |
| 305 | + expect(result).toEqual({ |
| 306 | + done: false, |
| 307 | + nextIndex: 100, |
| 308 | + totalLength: STREAM_CONTROL_FRAME_SIZE, |
| 309 | + }); |
| 310 | + }); |
| 311 | + |
| 312 | + it('parses control frame appended after data bytes', () => { |
| 313 | + const data = new Uint8Array([1, 2, 3, 4, 5]); |
| 314 | + const frame = buildControlFrame(false, 7); |
| 315 | + const combined = new Uint8Array(data.length + frame.length); |
| 316 | + combined.set(data, 0); |
| 317 | + combined.set(frame, data.length); |
| 318 | + |
| 319 | + const result = parseStreamControlFrame(combined); |
| 320 | + expect(result).toEqual({ |
| 321 | + done: false, |
| 322 | + nextIndex: 7, |
| 323 | + totalLength: STREAM_CONTROL_FRAME_SIZE, |
| 324 | + }); |
| 325 | + }); |
| 326 | + |
| 327 | + it('returns null for buffer shorter than control frame size', () => { |
| 328 | + expect(parseStreamControlFrame(new Uint8Array(12))).toBeNull(); |
| 329 | + expect(parseStreamControlFrame(new Uint8Array(0))).toBeNull(); |
| 330 | + }); |
| 331 | + |
| 332 | + it('returns null when magic footer does not match', () => { |
| 333 | + const frame = buildControlFrame(true, 0); |
| 334 | + frame[12] = 0xff; // corrupt magic footer |
| 335 | + expect(parseStreamControlFrame(frame)).toBeNull(); |
| 336 | + }); |
| 337 | + |
| 338 | + it('returns null when zero-frame marker is not all zeros', () => { |
| 339 | + const frame = buildControlFrame(true, 0); |
| 340 | + frame[0] = 1; // corrupt zero-frame marker |
| 341 | + expect(parseStreamControlFrame(frame)).toBeNull(); |
| 342 | + }); |
| 343 | + |
| 344 | + it('handles nextIndex=0', () => { |
| 345 | + const frame = buildControlFrame(false, 0); |
| 346 | + const result = parseStreamControlFrame(frame); |
| 347 | + expect(result).toEqual({ |
| 348 | + done: false, |
| 349 | + nextIndex: 0, |
| 350 | + totalLength: STREAM_CONTROL_FRAME_SIZE, |
| 351 | + }); |
| 352 | + }); |
| 353 | + |
| 354 | + it('handles large nextIndex values', () => { |
| 355 | + const frame = buildControlFrame(true, 0xffffffff); |
| 356 | + const result = parseStreamControlFrame(frame); |
| 357 | + expect(result?.nextIndex).toBe(0xffffffff); |
| 358 | + }); |
| 359 | +}); |
| 360 | + |
| 361 | +describe('readFromStream reconnection', () => { |
| 362 | + /** Collect every byte from a ReadableStream into one Uint8Array. */ |
| 363 | + async function drain( |
| 364 | + stream: ReadableStream<Uint8Array> |
| 365 | + ): Promise<Uint8Array> { |
| 366 | + const reader = stream.getReader(); |
| 367 | + const parts: Uint8Array[] = []; |
| 368 | + for (;;) { |
| 369 | + const { done, value } = await reader.read(); |
| 370 | + if (done) break; |
| 371 | + parts.push(value); |
| 372 | + } |
| 373 | + const len = parts.reduce((s, p) => s + p.length, 0); |
| 374 | + const out = new Uint8Array(len); |
| 375 | + let off = 0; |
| 376 | + for (const p of parts) { |
| 377 | + out.set(p, off); |
| 378 | + off += p.length; |
| 379 | + } |
| 380 | + return out; |
| 381 | + } |
| 382 | + |
| 383 | + function chunkedStream(chunks: Uint8Array[]): ReadableStream<Uint8Array> { |
| 384 | + let i = 0; |
| 385 | + return new ReadableStream({ |
| 386 | + pull(controller) { |
| 387 | + if (i < chunks.length) { |
| 388 | + controller.enqueue(chunks[i++]); |
| 389 | + } else { |
| 390 | + controller.close(); |
| 391 | + } |
| 392 | + }, |
| 393 | + }); |
| 394 | + } |
| 395 | + |
| 396 | + function streamResponse(...chunks: Uint8Array[]): Response { |
| 397 | + return new Response(chunkedStream(chunks), { |
| 398 | + status: 200, |
| 399 | + headers: { 'Content-Type': 'application/octet-stream' }, |
| 400 | + }); |
| 401 | + } |
| 402 | + |
| 403 | + async function getStreamer() { |
| 404 | + const { createStreamer } = await import('./streamer.js'); |
| 405 | + return createStreamer(); |
| 406 | + } |
| 407 | + |
| 408 | + afterEach(() => { |
| 409 | + vi.restoreAllMocks(); |
| 410 | + }); |
| 411 | + |
| 412 | + it('reconnects when server sends done=false and resumes from nextIndex', async () => { |
| 413 | + const chunk1 = new TextEncoder().encode('aaa'); |
| 414 | + const chunk2 = new TextEncoder().encode('bbb'); |
| 415 | + const timeout = buildControlFrame(false, 3); |
| 416 | + const done = buildControlFrame(true, 6); |
| 417 | + |
| 418 | + const fetchSpy = vi |
| 419 | + .spyOn(globalThis, 'fetch') |
| 420 | + .mockResolvedValueOnce(streamResponse(chunk1, timeout)) |
| 421 | + .mockResolvedValueOnce(streamResponse(chunk2, done)); |
| 422 | + |
| 423 | + const streamer = await getStreamer(); |
| 424 | + const result = await drain(await streamer.readFromStream('strm_test')); |
| 425 | + |
| 426 | + const expected = new Uint8Array([...chunk1, ...chunk2]); |
| 427 | + expect(result).toEqual(expected); |
| 428 | + expect(fetchSpy).toHaveBeenCalledTimes(2); |
| 429 | + |
| 430 | + const secondUrl = new URL(fetchSpy.mock.calls[1][0] as string); |
| 431 | + expect(secondUrl.searchParams.get('startIndex')).toBe('3'); |
| 432 | + }); |
| 433 | + |
| 434 | + it('falls through when no control frame is present (backward compat)', async () => { |
| 435 | + const data = new TextEncoder().encode('legacy server'); |
| 436 | + |
| 437 | + vi.spyOn(globalThis, 'fetch').mockResolvedValueOnce(streamResponse(data)); |
| 438 | + |
| 439 | + const streamer = await getStreamer(); |
| 440 | + const result = await drain(await streamer.readFromStream('strm_test')); |
| 441 | + |
| 442 | + expect(result).toEqual(data); |
| 443 | + }); |
| 444 | + |
| 445 | + it('propagates network error to consumer without retrying', async () => { |
| 446 | + const data = new TextEncoder().encode('partial'); |
| 447 | + |
| 448 | + let callCount = 0; |
| 449 | + const errorStream = new ReadableStream<Uint8Array>({ |
| 450 | + pull(controller) { |
| 451 | + if (callCount === 0) { |
| 452 | + callCount++; |
| 453 | + controller.enqueue(data); |
| 454 | + } else { |
| 455 | + controller.error(new Error('connection reset')); |
| 456 | + } |
| 457 | + }, |
| 458 | + }); |
| 459 | + |
| 460 | + vi.spyOn(globalThis, 'fetch').mockResolvedValueOnce( |
| 461 | + new Response(errorStream, { status: 200 }) |
| 462 | + ); |
| 463 | + |
| 464 | + const streamer = await getStreamer(); |
| 465 | + // readFromStream reads the full response via arrayBuffer(), so |
| 466 | + // a mid-stream error rejects the readFromStream promise itself. |
| 467 | + await expect(streamer.readFromStream('strm_test')).rejects.toThrow( |
| 468 | + 'connection reset' |
| 469 | + ); |
| 470 | + }); |
| 471 | +}); |
0 commit comments