// @vitest-environment node
import { describe, expect, it } from 'vitest'
import {
createStreamableHead,
renderSSRHeadShell,
renderSSRHeadSuspenseChunk,
} from '../src/stream/server'
const STREAM_TEMPLATE = '
'
const encoder = new TextEncoder()
const decoder = new TextDecoder()
function decodeChunk(chunk: Uint8Array | undefined): string {
return chunk ? decoder.decode(chunk) : ''
}
function withTimeout(promise: Promise, message: string, ms = 1000): Promise {
let timeout: ReturnType | undefined
const timeoutPromise = new Promise((_, reject) => {
timeout = setTimeout(() => reject(new Error(message)), ms)
})
return Promise.race([promise, timeoutPromise]).finally(() => {
if (timeout)
clearTimeout(timeout)
})
}
describe('solid-js streaming SSR', () => {
describe('createStreamableHead', () => {
it('uses custom stream key', async () => {
const { head } = createStreamableHead({ streamKey: '__solid__' })
head.push({ title: 'Test' })
const shell = await renderSSRHeadShell(head, '')
expect(shell).toContain('window.__solid__')
})
it('uses default stream key', async () => {
const { head } = createStreamableHead()
head.push({ title: 'Test' })
const shell = await renderSSRHeadShell(head, '')
expect(shell).toContain('window.__unhead__')
})
})
describe('wrapStream', () => {
it('rejects when the inner stream errors before shell is ready', async () => {
const { wrapStream } = createStreamableHead()
const error = new Error('pre-shell stream failure')
const appStream = new ReadableStream({
start(controller) {
controller.error(error)
},
})
const reader = wrapStream(appStream, STREAM_TEMPLATE).getReader()
await expect(withTimeout(reader.read(), 'pre-shell stream error did not reject')).rejects.toBe(error)
expect(appStream.locked).toBe(false)
})
it('rejects when the inner stream errors after shell is ready', async () => {
const { head, onCompleteShell, wrapStream } = createStreamableHead()
const error = new Error('post-shell stream failure')
let appController!: ReadableStreamDefaultController
const appStream = new ReadableStream({
start(controller) {
appController = controller
},
})
head.push({ title: 'Shell Ready' })
const reader = wrapStream(appStream, STREAM_TEMPLATE).getReader()
onCompleteShell()
const shellChunk = await withTimeout(reader.read(), 'shell chunk was not emitted')
expect(shellChunk.done).toBe(false)
expect(decodeChunk(shellChunk.value)).toContain('Shell Ready')
appController.enqueue(encoder.encode('app'))
const appChunk = await withTimeout(reader.read(), 'app chunk was not emitted')
expect(appChunk.done).toBe(false)
expect(decodeChunk(appChunk.value)).toBe('app')
appController.error(error)
await expect(withTimeout(reader.read(), 'post-shell stream error did not reject')).rejects.toBe(error)
expect(appStream.locked).toBe(false)
})
it('drains queued chunks before rejecting after an inner stream error', async () => {
const { head, onCompleteShell, wrapStream } = createStreamableHead()
const error = new Error('queued stream failure')
let appController!: ReadableStreamDefaultController
const appStream = new ReadableStream({
start(controller) {
appController = controller
},
})
head.push({ title: 'Queued Shell' })
const reader = wrapStream(appStream, STREAM_TEMPLATE).getReader()
onCompleteShell()
await Promise.resolve()
await Promise.resolve()
appController.enqueue(encoder.encode('queued app'))
appController.error(error)
const shellChunk = await withTimeout(reader.read(), 'queued shell chunk was not emitted')
expect(shellChunk.done).toBe(false)
expect(decodeChunk(shellChunk.value)).toContain('Queued Shell')
const appChunk = await withTimeout(reader.read(), 'queued app chunk was not emitted')
expect(appChunk.done).toBe(false)
expect(decodeChunk(appChunk.value)).toBe('queued app')
await expect(withTimeout(reader.read(), 'queued stream error did not reject after chunks drained')).rejects.toBe(error)
expect(appStream.locked).toBe(false)
})
it('cancels the inner reader when the outer reader is cancelled', async () => {
const { wrapStream } = createStreamableHead()
const reason = new Error('client aborted')
let cancelReason: unknown
let cancelCount = 0
const appStream = new ReadableStream({
pull() {
return Promise.resolve()
},
cancel(cancelledReason) {
cancelCount++
cancelReason = cancelledReason
},
})
const reader = wrapStream(appStream, STREAM_TEMPLATE).getReader()
await withTimeout(reader.cancel(reason), 'outer stream cancel did not resolve')
expect(cancelCount).toBe(1)
expect(cancelReason).toBe(reason)
expect(appStream.locked).toBe(false)
})
it('flushes buffered chunks and releases the inner reader after normal completion', async () => {
const { head, onCompleteShell, wrapStream } = createStreamableHead()
const appStream = new ReadableStream({
start(controller) {
controller.enqueue(encoder.encode('buffered app'))
controller.close()
},
})
head.push({ title: 'Buffered Shell' })
const textPromise = new Response(wrapStream(appStream, STREAM_TEMPLATE)).text()
await Promise.resolve()
onCompleteShell()
const text = await withTimeout(textPromise, 'normal stream completion did not resolve')
expect(text).toContain('Buffered Shell')
expect(text).toContain('buffered app')
expect(text).toContain('')
expect(appStream.locked).toBe(false)
})
})
describe('renderSSRHeadShell', () => {
it('renders initial head tags into shell', async () => {
const { head } = createStreamableHead()
head.push({
title: 'Solid Streaming Test',
meta: [{ name: 'description', content: 'Test description' }],
})
const template = ''
const result = await renderSSRHeadShell(head, template)
expect(result).toContain('Solid Streaming Test')
expect(result).toContain('')
expect(result).toContain('window.__unhead__')
})
it('applies html and body attrs', async () => {
const { head } = createStreamableHead()
head.push({
htmlAttrs: { lang: 'en', dir: 'ltr' },
bodyAttrs: { class: 'dark' },
})
const template = ''
const result = await renderSSRHeadShell(head, template)
expect(result).toContain('lang="en"')
expect(result).toContain('dir="ltr"')
expect(result).toContain('class="dark"')
})
it('clears entries after rendering shell', async () => {
const { head } = createStreamableHead()
head.push({ title: 'Test' })
await renderSSRHeadShell(head, '')
const chunk = renderSSRHeadSuspenseChunk(head)
expect(chunk).toBe('')
})
})
describe('renderSSRHeadSuspenseChunk', () => {
it('returns empty string when no new tags', async () => {
const { head } = createStreamableHead()
head.push({ title: 'Test' })
await renderSSRHeadShell(head, '')
const result = renderSSRHeadSuspenseChunk(head)
expect(result).toBe('')
})
it('returns push script for new tags', async () => {
const { head } = createStreamableHead()
head.push({ title: 'Initial' })
await renderSSRHeadShell(head, '')
head.push({
title: 'Updated Title',
meta: [{ name: 'description', content: 'New description' }],
})
const result = renderSSRHeadSuspenseChunk(head)
expect(result).toContain('window.__unhead__.push')
expect(result).toContain('Updated Title')
expect(result).toContain('New description')
})
it('clears entries after rendering chunk', async () => {
const { head } = createStreamableHead()
await renderSSRHeadShell(head, '')
head.push({ title: 'First' })
renderSSRHeadSuspenseChunk(head)
const result = renderSSRHeadSuspenseChunk(head)
expect(result).toBe('')
})
})
describe('xSS prevention', () => {
it('escapes script tags in content', async () => {
const { head } = createStreamableHead()
await renderSSRHeadShell(head, '')
head.push({
title: '',
})
const result = renderSSRHeadSuspenseChunk(head)
expect(result).not.toContain('' }],
})
const result = renderSSRHeadSuspenseChunk(head)
expect(result).not.toContain('