Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

streaming-ssr-assertions.ts

143 lines4.8 KB

1import { expect } from '@playwright/test'
2import { test } from './e2e-utils/src/fixture'
3import type { Page } from '@playwright/test'
4​
5// Router transport markers, mirrored from router-core's hydration scripts.
6// Keep every spec's protocol knowledge in this one place.
7export const ROUTER_BOOTSTRAP_MARKER = '$_TSR.router='
8export const ROUTER_STREAM_END_MARKER = '$_TSR.e()'
9export const STREAM_BOUNDARY_MARKER = '$tsr-stream-boundary'
10export const STREAM_PART_SELECTOR = '[data-tsr-stream-part]'
11const LATE_RECORD_CLOSE = 'document.currentScript.remove()'
12​
13const immediateDataMarker = 'data-testid="immediate-data"'
14const renderedDeferredMarker = 'data-testid="deferred-data"'
15​
16const browserUserAgent =
17 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36'
18​
19export async function expectDeferredRouteResponseOrder(
20 baseURL: string | undefined,
21) {
22 if (!baseURL) {
23 throw new Error('Playwright baseURL is required')
24 }
25​
26 const response = await fetch(new URL('/deferred', baseURL), {
27 headers: {
28 accept: 'text/html',
29 'accept-encoding': 'identity',
30 'user-agent': browserUserAgent,
31 },
32 })
33​
34 expect(response.status).toBe(200)
35 expect(response.body).not.toBeNull()
36​
37 const html = await response.text()
38​
39 const responsePreview =
40 html.length > 2000
41 ? `${html.slice(0, 1000)}\n...\n${html.slice(-1000)}`
42 : html
43​
44 const immediateDataIndex = html.indexOf(immediateDataMarker)
45 const renderedDeferredIndex = html.indexOf(renderedDeferredMarker)
46 expect(immediateDataIndex, responsePreview).toBeGreaterThanOrEqual(0)
47 expect(renderedDeferredIndex, responsePreview).toBeGreaterThan(
48 immediateDataIndex,
49 )
50​
51 // SSR Query closes its request-local stream after renderer EOF. Its final
52 // record is the deterministic late router output for this test.
53 const boundaryIndex = html.indexOf(STREAM_BOUNDARY_MARKER)
54 const routerEndIndex = html.indexOf(ROUTER_STREAM_END_MARKER)
55​
56 expect(boundaryIndex).toBeGreaterThan(-1)
57 expect(boundaryIndex).toBeLessThan(routerEndIndex)
58 expect(renderedDeferredIndex).toBeLessThan(routerEndIndex)
59​
60 expectCompleteRouterStreamBeforeDocumentCloses(html, renderedDeferredMarker)
62​
63export async function expectQueryHeavyRouteStreamOrder(
64 baseURL: string | undefined,
65) {
66 if (!baseURL) {
67 throw new Error('Playwright baseURL is required')
68 }
69​
70 const response = await fetch(new URL('/query-heavy', baseURL), {
71 headers: {
72 accept: 'text/html',
73 'accept-encoding': 'identity',
74 'user-agent': browserUserAgent,
75 },
76 })
77​
78 expect(response.status).toBe(200)
79 const html = await response.text()
80​
81 expect(html).toContain('data-testid="fast-async-query-1"')
82 expect(html).toContain('data-testid="slow-async-query-3"')
83 expectCompleteRouterStreamBeforeDocumentCloses(html, '"slow-async-3"')
85​
86/** Asserts a route response carries the bootstrap, the boundary, and the end record. */
87export function expectRouterHydrationMarkers(html: string) {
88 expect(html).toContain(ROUTER_BOOTSTRAP_MARKER)
89 expect(html).toContain(ROUTER_STREAM_END_MARKER)
90 expect(html).toContain(STREAM_BOUNDARY_MARKER)
92​
93function expectCompleteRouterStreamBeforeDocumentCloses(
94 html: string,
95 payloadMarker: string,
96) {
97 const endIndex = html.indexOf(ROUTER_STREAM_END_MARKER)
98 const payloadIndex = html.indexOf(payloadMarker)
99 const scriptOpenIndex = html.lastIndexOf('<script', endIndex)
100 const previousScriptCloseIndex = html.lastIndexOf('</script>', endIndex)
101 const scriptCloseIndex = html.indexOf('</script>', endIndex)
102 const bodyCloseIndex = html.indexOf('</body>', scriptCloseIndex)
103 const htmlCloseIndex = html.indexOf('</html>', bodyCloseIndex)
104​
105 expect(endIndex).toBeGreaterThan(-1)
106 expect(scriptOpenIndex).toBeGreaterThan(previousScriptCloseIndex)
107 expect(scriptCloseIndex).toBeGreaterThan(endIndex)
108 expect(payloadIndex).toBeGreaterThan(-1)
109 expect(payloadIndex).toBeLessThan(endIndex)
110 expect(html.slice(endIndex, scriptCloseIndex)).toContain(LATE_RECORD_CLOSE)
111 expect(scriptCloseIndex).toBeLessThan(bodyCloseIndex)
112 expect(bodyCloseIndex).toBeLessThan(htmlCloseIndex)
114​
115async function verifyHydration(
116 page: Page,
117 { timeout = 10_000 }: { timeout?: number } = {},
118) {
119 const button = page.getByTestId('hydration-check-btn')
120 const status = page.getByTestId('hydration-status')
121​
122 await expect(button).toBeVisible()
123 await expect(async () => {
124 await button.click()
125 await expect(status).toHaveText('hydrated', { timeout: 100 })
126 }).toPass({ timeout })
127​
128 await expect(page.locator(STREAM_PART_SELECTOR)).toHaveCount(0)
129 await expect(
130 page.locator('script').filter({ hasText: STREAM_BOUNDARY_MARKER }),
131 ).toHaveCount(0)
133​
134export { test }
135​
136export const testWithHydration = test.extend({
137 page: async ({ page }, use) => {
138 await use(page)
139 await verifyHydration(page)
140 },
141})
142​
143export { expect }