|
| 1 | +import { expect, test } from "@playwright/test"; |
| 2 | + |
| 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`; |
| 13 | + |
| 14 | +test("streaming should work in api route", async ({ page }) => { |
| 15 | + await page.goto("/sse"); |
| 16 | + |
| 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); |
| 23 | + |
| 24 | + const seenLines: Array<{ line: string; time: number }> = []; |
| 25 | + const startTime = Date.now(); |
| 26 | + |
| 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(), |
| 35 | + time: Date.now() - startTime, |
| 36 | + }); |
| 37 | + } |
| 38 | + // wait for a bit before checking again |
| 39 | + await page.waitForTimeout(200); |
| 40 | + } |
| 41 | + |
| 42 | + expect(seenLines.map((n) => n.line)).toEqual( |
| 43 | + SADE_SMOOTH_OPERATOR_LYRIC.split("\n"), |
| 44 | + ); |
| 45 | + for (let i = 1; i < seenLines.length; i++) { |
| 46 | + expect(seenLines[i].time - seenLines[i - 1].time).toBeGreaterThan(500); |
| 47 | + } |
| 48 | + |
| 49 | + await expect(page.getByTestId("video")).toBeVisible(); |
| 50 | +}); |
0 commit comments