|
1 | 1 | import { describe, it, expect, vi, afterEach } from "vitest"; |
2 | 2 | import { PassThrough } from "node:stream"; |
3 | 3 | import type * as http from "node:http"; |
4 | | -import { writeSSEStream, calculateDelay } from "../sse-writer.js"; |
| 4 | +import { writeSSEStream, calculateDelay, calculateTotalDelay } from "../sse-writer.js"; |
5 | 5 | import type { SSEChunk, StreamingProfile, RecordedTimings } from "../types.js"; |
6 | 6 | import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; |
7 | 7 | import { tmpdir } from "node:os"; |
@@ -330,3 +330,72 @@ describe("fixture loader streamingProfile passthrough", () => { |
330 | 330 | expect(fixtures[0].streamingProfile).toEqual({ ttft: 300 }); |
331 | 331 | }); |
332 | 332 | }); |
| 333 | + |
| 334 | +// ─── calculateTotalDelay unit tests ────────────────────────────────────────── |
| 335 | +// The non-streaming equivalent of calculateDelay: sums it over the same chunk |
| 336 | +// count a streamed response at the same chunkSize would use, so a fixture's |
| 337 | +// timing is identical whether served streamed or not. |
| 338 | + |
| 339 | +describe("calculateTotalDelay", () => { |
| 340 | + it("returns fallback latency once, not per would-be chunk, when no profile is set", () => { |
| 341 | + // 25 chars at chunkSize 10 -> 3 chunks, but a flat fallback applies per |
| 342 | + // chunk the same way streaming's calculateDelay does, so the total is |
| 343 | + // fallback * chunkCount here too. |
| 344 | + expect(calculateTotalDelay(25, 10, undefined, 50)).toBe(150); |
| 345 | + }); |
| 346 | + |
| 347 | + it("returns 0 when there is no profile and no fallback", () => { |
| 348 | + expect(calculateTotalDelay(25, 10, undefined, undefined)).toBe(0); |
| 349 | + }); |
| 350 | + |
| 351 | + it("charges ttft once plus tps for every remaining would-be chunk", () => { |
| 352 | + const profile: StreamingProfile = { ttft: 200, tps: 50 }; |
| 353 | + // 25 chars at chunkSize 10 -> 3 chunks: ttft (200) + 2 * (1000/50 = 20) |
| 354 | + expect(calculateTotalDelay(25, 10, profile)).toBe(240); |
| 355 | + }); |
| 356 | + |
| 357 | + it("treats content shorter than chunkSize as a single chunk", () => { |
| 358 | + const profile: StreamingProfile = { ttft: 150, tps: 50 }; |
| 359 | + expect(calculateTotalDelay(5, 10, profile)).toBe(150); |
| 360 | + }); |
| 361 | + |
| 362 | + it("treats zero-length content as a single chunk (still pays ttft)", () => { |
| 363 | + const profile: StreamingProfile = { ttft: 120, tps: 50 }; |
| 364 | + expect(calculateTotalDelay(0, 10, profile)).toBe(120); |
| 365 | + }); |
| 366 | + |
| 367 | + it("matches summing calculateDelay over the same chunk range", () => { |
| 368 | + const profile: StreamingProfile = { ttft: 300, tps: 40 }; |
| 369 | + const contentLength = 47; |
| 370 | + const chunkSize = 10; |
| 371 | + const chunkCount = Math.ceil(contentLength / chunkSize); |
| 372 | + let expected = 0; |
| 373 | + for (let i = 0; i < chunkCount; i++) { |
| 374 | + expected += calculateDelay(i, profile); |
| 375 | + } |
| 376 | + expect(calculateTotalDelay(contentLength, chunkSize, profile)).toBe(expected); |
| 377 | + }); |
| 378 | + |
| 379 | + it("uses recordedTimings (ttft + inter-chunk delays) across the chunk range", () => { |
| 380 | + const recordedTimings: RecordedTimings = { |
| 381 | + ttftMs: 80, |
| 382 | + interChunkDelaysMs: [15, 25], |
| 383 | + totalDurationMs: 120, |
| 384 | + }; |
| 385 | + // 30 chars at chunkSize 10 -> 3 chunks: ttft (80) + recorded gaps (15, 25) |
| 386 | + expect(calculateTotalDelay(30, 10, undefined, undefined, recordedTimings)).toBe(120); |
| 387 | + }); |
| 388 | + |
| 389 | + it("divides by replaySpeed, same as calculateDelay", () => { |
| 390 | + const profile: StreamingProfile = { ttft: 200, tps: 50 }; |
| 391 | + // 25 chars at chunkSize 10 -> 240ms at 1x (see earlier test), 120ms at 2x |
| 392 | + expect(calculateTotalDelay(25, 10, profile, undefined, undefined, 2)).toBe(120); |
| 393 | + }); |
| 394 | + |
| 395 | + it("stays non-negative when jitter or replaySpeed could otherwise push it below zero", () => { |
| 396 | + const profile: StreamingProfile = { ttft: 10, tps: 500, jitter: 1 }; |
| 397 | + for (let i = 0; i < 50; i++) { |
| 398 | + expect(calculateTotalDelay(100, 10, profile)).toBeGreaterThanOrEqual(0); |
| 399 | + } |
| 400 | + }); |
| 401 | +}); |
0 commit comments