|
1 | 1 | import { expect, test } from "@playwright/test";
|
2 | 2 |
|
3 |
| -test("streaming should work in api route", async ({ page }) => { |
4 |
| - const ITERATOR_LENGTH = 10; |
5 |
| - |
6 |
| - const res = await page.goto("/api/streaming", { |
7 |
| - // we set waitUntil: "commit" to ensure that the response is streamed |
8 |
| - // without this option, the response would be buffered and sent all at once |
9 |
| - // we could also drop the `await` aswell, but then we can't see the headers first. |
10 |
| - waitUntil: "commit", |
11 |
| - }); |
| 3 | +const SADE_SMOOTH_OPERATOR_LYRIC = `Diamond life, lover boy |
| 4 | +He move in space with minimum waste and maximum joy |
| 5 | +City lights and business nights |
| 6 | +When you require streetcar desire for higher heights |
| 7 | +No place for beginners or sensitive hearts |
| 8 | +When sentiment is left to chance |
| 9 | +No place to be ending but somewhere to start |
| 10 | +No need to ask, he's a smooth operator |
| 11 | +Smooth operator, smooth operator |
| 12 | +Smooth operator`; |
12 | 13 |
|
13 |
| - expect(res?.headers()["content-type"]).toBe("text/html; charset=utf-8"); |
14 |
| - expect(res?.headers()["cache-control"]).toBe("no-cache, no-transform"); |
15 |
| - // AWS API Gateway remaps the connection header to `x-amzn-remapped-connection` |
16 |
| - expect(res?.headers()["x-amzn-remapped-connection"]).toBe("keep-alive"); |
| 14 | +test("streaming should work in api route", async ({ page }) => { |
| 15 | + await page.goto("/sse"); |
17 | 16 |
|
18 |
| - // wait for first number to be present |
19 |
| - await page.getByTestId("iteratorCount").first().waitFor(); |
| 17 | + // wait for first line to be present |
| 18 | + await page.getByTestId("line").first().waitFor(); |
| 19 | + const initialLines = await page.getByTestId("line").count(); |
| 20 | + // fail if all lines appear at once |
| 21 | + // this is a safeguard to ensure that the response is streamed and not buffered all at once |
| 22 | + expect(initialLines).toBe(1); |
20 | 23 |
|
21 |
| - const seenNumbers: Array<{ number: string; time: number }> = []; |
| 24 | + const seenLines: Array<{ line: string; time: number }> = []; |
22 | 25 | const startTime = Date.now();
|
23 | 26 |
|
24 |
| - const initialParagraphs = await page.getByTestId("iteratorCount").count(); |
25 |
| - // fail if all paragraphs appear at once |
26 |
| - // this is a safeguard to ensure that the response is streamed and not buffered all at once |
27 |
| - expect(initialParagraphs).toBe(1); |
28 |
| - |
29 |
| - while ( |
30 |
| - seenNumbers.length < ITERATOR_LENGTH && |
31 |
| - Date.now() - startTime < 11000 |
32 |
| - ) { |
33 |
| - const elements = await page.getByTestId("iteratorCount").all(); |
34 |
| - if (elements.length > seenNumbers.length) { |
35 |
| - expect(elements.length).toBe(seenNumbers.length + 1); |
36 |
| - const newElement = elements[elements.length - 1]; |
37 |
| - seenNumbers.push({ |
38 |
| - number: await newElement.innerText(), |
| 27 | + // we loop until we see all lines |
| 28 | + while (seenLines.length < SADE_SMOOTH_OPERATOR_LYRIC.split("\n").length) { |
| 29 | + const lines = await page.getByTestId("line").all(); |
| 30 | + if (lines.length > seenLines.length) { |
| 31 | + expect(lines.length).toBe(seenLines.length + 1); |
| 32 | + const newLine = lines[lines.length - 1]; |
| 33 | + seenLines.push({ |
| 34 | + line: await newLine.innerText(), |
39 | 35 | time: Date.now() - startTime,
|
40 | 36 | });
|
41 | 37 | }
|
42 |
| - await page.waitForTimeout(100); |
| 38 | + // wait for a bit before checking again |
| 39 | + await page.waitForTimeout(200); |
43 | 40 | }
|
44 | 41 |
|
45 |
| - expect(seenNumbers.map((n) => n.number)).toEqual( |
46 |
| - [...Array(ITERATOR_LENGTH)].map((_, i) => String(i + 1)), |
| 42 | + expect(seenLines.map((n) => n.line)).toEqual( |
| 43 | + SADE_SMOOTH_OPERATOR_LYRIC.split("\n"), |
47 | 44 | );
|
48 |
| - |
49 |
| - // verify streaming timing |
50 |
| - for (let i = 1; i < seenNumbers.length; i++) { |
51 |
| - const timeDiff = seenNumbers[i].time - seenNumbers[i - 1].time; |
52 |
| - expect(timeDiff).toBeGreaterThanOrEqual(100); |
| 45 | + for (let i = 1; i < seenLines.length; i++) { |
| 46 | + expect(seenLines[i].time - seenLines[i - 1].time).toBeGreaterThan(500); |
53 | 47 | }
|
| 48 | + |
| 49 | + await expect(page.getByTestId("video")).toBeVisible(); |
54 | 50 | });
|
0 commit comments