File size: 3,183 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
import { AsyncLocalStorage } from 'node:async_hooks'
import type { WorkUnitStore } from '../app-render/work-unit-async-storage.external'
import type { WorkStore } from '../app-render/work-async-storage.external'
import type { IncrementalCache } from './incremental-cache'
import { createPatchedFetcher } from './patch-fetch'
describe('createPatchedFetcher', () => {
it('should not buffer a streamed response', async () => {
const mockFetch: jest.MockedFunction<typeof fetch> = jest.fn()
let streamChunk: () => void
const readableStream = new ReadableStream({
start(controller) {
controller.enqueue(new TextEncoder().encode('stream start'))
streamChunk = () => {
controller.enqueue(new TextEncoder().encode('stream end'))
controller.close()
}
},
})
mockFetch.mockResolvedValue(new Response(readableStream))
const workAsyncStorage = new AsyncLocalStorage<WorkStore>()
const workUnitAsyncStorage = new AsyncLocalStorage<WorkUnitStore>()
const patchedFetch = createPatchedFetcher(mockFetch, {
// workUnitAsyncStorage does not need to provide a store for this test.
workAsyncStorage,
workUnitAsyncStorage,
})
let resolveIncrementalCacheSet: () => void
const incrementalCacheSetPromise = new Promise<void>((resolve) => {
resolveIncrementalCacheSet = resolve
})
const incrementalCache = {
get: jest.fn(),
set: jest.fn(() => resolveIncrementalCacheSet()),
generateCacheKey: jest.fn(() => 'test-cache-key'),
lock: jest.fn(() => () => {}),
} as unknown as IncrementalCache
// We only need to provide a few of the WorkStore properties.
const workStore: Partial<WorkStore> = {
page: '/',
route: '/',
incrementalCache,
}
await workAsyncStorage.run(workStore as WorkStore, async () => {
const response = await patchedFetch('https://example.com', {
cache: 'force-cache',
})
if (!response.body) {
throw new Error(`Response body is ${JSON.stringify(response.body)}.`)
}
const reader = response.body.getReader()
let result = await reader.read()
const textDecoder = new TextDecoder()
expect(textDecoder.decode(result.value)).toBe('stream start')
streamChunk()
result = await reader.read()
expect(textDecoder.decode(result.value)).toBe('stream end')
await incrementalCacheSetPromise
expect(incrementalCache.set).toHaveBeenCalledWith(
'test-cache-key',
{
data: {
body: btoa('stream startstream end'),
headers: {},
status: 200,
url: '', // the mocked response does not have a URL
},
kind: 'FETCH',
revalidate: 31536000, // default of one year
},
{
fetchCache: true,
fetchIdx: 1,
fetchUrl: 'https://example.com/',
tags: [],
isImplicitBuildTimeCache: false,
}
)
})
// Setting a lower timeout than default, because the test will fail with a
// timeout when we regress and buffer the response.
}, 1000)
})
|