Skip to content

Commit 4744536

Browse files
committed
test: update interfaces in getBlobServerGets
1 parent cdf9e9a commit 4744536

File tree

7 files changed

+41
-41
lines changed

7 files changed

+41
-41
lines changed

tests/integration/cache-handler.test.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ describe('page router', () => {
8282
countOfBlobServerGetsForKey(ctx, '/static/revalidate-automatic'),
8383
'should only try to get value once from blob store (date calculation should not trigger additional blobs.get)',
8484
).toBe(1)
85-
ctx.blobServerGetSpy.mockClear()
85+
ctx.blobServerOnRequestSpy.mockClear()
8686

8787
// wait to have page regenerated in the background
8888
await new Promise<void>((resolve) => setTimeout(resolve, 1000))
@@ -116,7 +116,7 @@ describe('page router', () => {
116116
countOfBlobServerGetsForKey(ctx, '/static/revalidate-automatic'),
117117
'should only try to get value once from blob store (date calculation should not trigger additional blobs.get)',
118118
).toBe(1)
119-
ctx.blobServerGetSpy.mockClear()
119+
ctx.blobServerOnRequestSpy.mockClear()
120120

121121
// ping that should serve the stale page for static/revalidate-slow, while revalidating in background
122122
await invokeFunction(ctx, { url: 'static/revalidate-slow' })
@@ -125,7 +125,7 @@ describe('page router', () => {
125125
countOfBlobServerGetsForKey(ctx, '/static/revalidate-slow'),
126126
'should only try to get value once from blob store (date calculation should not trigger additional blobs.get)',
127127
).toBe(1)
128-
ctx.blobServerGetSpy.mockClear()
128+
ctx.blobServerOnRequestSpy.mockClear()
129129

130130
// wait to have a stale page
131131
await new Promise<void>((resolve) => setTimeout(resolve, 6_000))
@@ -136,7 +136,7 @@ describe('page router', () => {
136136
countOfBlobServerGetsForKey(ctx, '/static/revalidate-slow'),
137137
'should only try to get value once from blob store (date calculation should not trigger additional blobs.get)',
138138
).toBe(1)
139-
ctx.blobServerGetSpy.mockClear()
139+
ctx.blobServerOnRequestSpy.mockClear()
140140

141141
// over 5 seconds since it was regenerated, so we should get stale response,
142142
// while fresh is generated in the background
@@ -160,7 +160,7 @@ describe('page router', () => {
160160
countOfBlobServerGetsForKey(ctx, '/static/revalidate-automatic'),
161161
'should only try to get value once from blob store (date calculation should not trigger additional blobs.get)',
162162
).toBe(1)
163-
ctx.blobServerGetSpy.mockClear()
163+
ctx.blobServerOnRequestSpy.mockClear()
164164

165165
// Slow revalidate should still be a hit, but the maxage should be updated
166166
const callLater2 = await invokeFunction(ctx, { url: 'static/revalidate-slow' })
@@ -177,7 +177,7 @@ describe('page router', () => {
177177
countOfBlobServerGetsForKey(ctx, '/static/revalidate-slow'),
178178
'should only try to get value once from blob store (date calculation should not trigger additional blobs.get)',
179179
).toBe(1)
180-
ctx.blobServerGetSpy.mockClear()
180+
ctx.blobServerOnRequestSpy.mockClear()
181181

182182
// it does not wait for the cache.set so we have to manually wait here until the blob storage got populated
183183
await new Promise<void>((resolve) => setTimeout(resolve, 1000))
@@ -204,7 +204,7 @@ describe('page router', () => {
204204
countOfBlobServerGetsForKey(ctx, '/static/revalidate-automatic'),
205205
'should only try to get value once from blob store (date calculation should not trigger additional blobs.get)',
206206
).toBe(1)
207-
ctx.blobServerGetSpy.mockClear()
207+
ctx.blobServerOnRequestSpy.mockClear()
208208
})
209209
})
210210

@@ -245,7 +245,7 @@ describe('app router', () => {
245245
countOfBlobServerGetsForKey(ctx, '/posts/1'),
246246
'should only try to get value once from blob store (date calculation should not trigger additional blobs.get)',
247247
).toBe(1)
248-
ctx.blobServerGetSpy.mockClear()
248+
ctx.blobServerOnRequestSpy.mockClear()
249249

250250
// test a prerendered page without TTL
251251
const post2 = await invokeFunction(ctx, { url: '/' })
@@ -263,7 +263,7 @@ describe('app router', () => {
263263
countOfBlobServerGetsForKey(ctx, '/index'),
264264
'should only try to get value once from blob store (date calculation should not trigger additional blobs.get)',
265265
).toBe(1)
266-
ctx.blobServerGetSpy.mockClear()
266+
ctx.blobServerOnRequestSpy.mockClear()
267267

268268
expect(await ctx.blobStore.get(encodeBlobKey('/posts/3'))).toBeNull()
269269
// this page is not pre-rendered and should result in a cache miss
@@ -293,7 +293,7 @@ describe('app router', () => {
293293
countOfBlobServerGetsForKey(ctx, '/posts/1'),
294294
'should only try to get value once from blob store (date calculation should not trigger additional blobs.get)',
295295
).toBe(1)
296-
ctx.blobServerGetSpy.mockClear()
296+
ctx.blobServerOnRequestSpy.mockClear()
297297
// it should've been regenerated in the background after the first call
298298
// so the date should be different
299299
expect(staleDate, 'the date was cached and is matching the initial one').not.toBe(post1Date)
@@ -320,7 +320,7 @@ describe('app router', () => {
320320
countOfBlobServerGetsForKey(ctx, '/posts/1'),
321321
'should only try to get value once from blob store (date calculation should not trigger additional blobs.get)',
322322
).toBe(1)
323-
ctx.blobServerGetSpy.mockClear()
323+
ctx.blobServerOnRequestSpy.mockClear()
324324
})
325325

326326
test<FixtureTestContext>("not-prerendered pages should be permanently cached when produced by sandboxed invocations that don't share memory", async (ctx) => {
@@ -405,7 +405,7 @@ describe('route', () => {
405405
})
406406
expect(blobEntry).not.toBeNull()
407407

408-
ctx.blobServerGetSpy.mockClear()
408+
ctx.blobServerOnRequestSpy.mockClear()
409409

410410
// test the first invocation of the route - we should get stale response while fresh is generated in the background
411411
const call1 = await invokeFunction(ctx, { url: '/api/revalidate-handler' })
@@ -428,7 +428,7 @@ describe('route', () => {
428428
countOfBlobServerGetsForKey(ctx, '/api/revalidate-handler'),
429429
'should only try to get value once from blob store (date calculation should not trigger additional blobs.get)',
430430
).toBe(1)
431-
ctx.blobServerGetSpy.mockClear()
431+
ctx.blobServerOnRequestSpy.mockClear()
432432

433433
// it does not wait for the cache.set so we have to manually wait here until the blob storage got populated
434434
await new Promise<void>((resolve) => setTimeout(resolve, 1000))
@@ -458,7 +458,7 @@ describe('route', () => {
458458
countOfBlobServerGetsForKey(ctx, '/api/revalidate-handler'),
459459
'should only try to get value once from blob store (date calculation should not trigger additional blobs.get)',
460460
).toBe(1)
461-
ctx.blobServerGetSpy.mockClear()
461+
ctx.blobServerOnRequestSpy.mockClear()
462462

463463
// wait to have a stale route again
464464
await new Promise<void>((resolve) => setTimeout(resolve, 8_000))
@@ -485,7 +485,7 @@ describe('route', () => {
485485
countOfBlobServerGetsForKey(ctx, '/api/revalidate-handler'),
486486
'should only try to get value once from blob store (date calculation should not trigger additional blobs.get)',
487487
).toBe(1)
488-
ctx.blobServerGetSpy.mockClear()
488+
ctx.blobServerOnRequestSpy.mockClear()
489489

490490
// it does not wait for the cache.set so we have to manually wait here until the blob storage got populated
491491
await new Promise<void>((resolve) => setTimeout(resolve, 1000))
@@ -510,7 +510,7 @@ describe('route', () => {
510510
countOfBlobServerGetsForKey(ctx, '/api/revalidate-handler'),
511511
'should only try to get value once from blob store (date calculation should not trigger additional blobs.get)',
512512
).toBe(1)
513-
ctx.blobServerGetSpy.mockClear()
513+
ctx.blobServerOnRequestSpy.mockClear()
514514
})
515515

516516
test<FixtureTestContext>('cacheable route handler response not produced at build is served correctly', async (ctx) => {

tests/integration/fetch-handler.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ test<FixtureTestContext>('does not fetch same cached fetch data from blobs twice
363363
'expected blobs for all types of values to be retrieved at most once per key (including fetch data, tag manifests, static files)',
364364
).toBeDistinct()
365365

366-
ctx.blobServerGetSpy.mockClear()
366+
ctx.blobServerOnRequestSpy.mockClear()
367367
handlerCalled = 0
368368
const request2 = await invokeFunction(ctx, {
369369
url: 'same-fetch-multiple-times/99',

tests/integration/request-context.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ describe('request-context does NOT leak between concurrent requests', () => {
106106
lastModified: new Date(mockedDateForRevalidateSlow).getTime(),
107107
})
108108

109-
ctx.blobServerGetSpy.mockClear()
109+
ctx.blobServerOnRequestSpy.mockClear()
110110

111111
const waitForCacheHandlerGetAndPausePromise =
112112
waitForCacheHandlerGetAndPause('/static/revalidate-slow')
@@ -182,7 +182,7 @@ describe('request-context does NOT leak between concurrent requests', () => {
182182
lastModified: new Date(mockedDateForStaticFetch2).getTime(),
183183
})
184184

185-
ctx.blobServerGetSpy.mockClear()
185+
ctx.blobServerOnRequestSpy.mockClear()
186186

187187
const waitForCacheHandlerGetAndPausePromise = waitForCacheHandlerGetAndPause('/static-fetch/2')
188188
const slowCallPromise = invokeFunction(ctx, {

tests/integration/revalidate-path.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ test<FixtureTestContext>('should revalidate a route by path', async (ctx) => {
5858
expect(await ctx.blobStore.get(encodeBlobKey('/static-fetch/1'))).not.toBeNull()
5959
expect(await ctx.blobStore.get(encodeBlobKey('_N_T_/static-fetch/[id]/page'))).toBeNull()
6060

61-
ctx.blobServerGetSpy.mockClear()
61+
ctx.blobServerOnRequestSpy.mockClear()
6262

6363
// test the function call
6464
const post1 = await invokeFunction(ctx, { url: '/static-fetch/1' })
@@ -78,7 +78,7 @@ test<FixtureTestContext>('should revalidate a route by path', async (ctx) => {
7878
getBlobServerGets(ctx, isTagManifest),
7979
`expected tag manifests to be retrieved at most once per tag`,
8080
).toBeDistinct()
81-
ctx.blobServerGetSpy.mockClear()
81+
ctx.blobServerOnRequestSpy.mockClear()
8282

8383
const post1Route2 = await invokeFunction(ctx, { url: '/static-fetch/2' })
8484
expect(post1Route2.statusCode).toBe(200)
@@ -95,7 +95,7 @@ test<FixtureTestContext>('should revalidate a route by path', async (ctx) => {
9595
getBlobServerGets(ctx, isTagManifest),
9696
`expected tag manifests to be retrieved at most once per tag`,
9797
).toBeDistinct()
98-
ctx.blobServerGetSpy.mockClear()
98+
ctx.blobServerOnRequestSpy.mockClear()
9999

100100
const revalidate = await invokeFunction(ctx, { url: '/api/on-demand-revalidate/path' })
101101
expect(revalidate.statusCode).toBe(200)
@@ -106,7 +106,7 @@ test<FixtureTestContext>('should revalidate a route by path', async (ctx) => {
106106

107107
expect(await ctx.blobStore.get(encodeBlobKey('_N_T_/static-fetch/[id]/page'))).not.toBeNull()
108108

109-
ctx.blobServerGetSpy.mockClear()
109+
ctx.blobServerOnRequestSpy.mockClear()
110110

111111
const post2 = await invokeFunction(ctx, { url: '/static-fetch/1' })
112112
const post2Date = load(post2.body)('[data-testid="date-now"]').text()
@@ -126,7 +126,7 @@ test<FixtureTestContext>('should revalidate a route by path', async (ctx) => {
126126
getBlobServerGets(ctx, isTagManifest),
127127
`expected tag manifests to be retrieved at most once per tag`,
128128
).toBeDistinct()
129-
ctx.blobServerGetSpy.mockClear()
129+
ctx.blobServerOnRequestSpy.mockClear()
130130

131131
const post2Route2 = await invokeFunction(ctx, { url: '/static-fetch/2' })
132132

@@ -144,5 +144,5 @@ test<FixtureTestContext>('should revalidate a route by path', async (ctx) => {
144144
getBlobServerGets(ctx, isTagManifest),
145145
`expected tag manifests to be retrieved at most once per tag`,
146146
).toBeDistinct()
147-
ctx.blobServerGetSpy.mockClear()
147+
ctx.blobServerOnRequestSpy.mockClear()
148148
})

tests/integration/revalidate-tags.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ test<FixtureTestContext>('should revalidate a route by tag', async (ctx) => {
5757

5858
expect(await ctx.blobStore.get(encodeBlobKey('/static-fetch-1'))).not.toBeNull()
5959

60-
ctx.blobServerGetSpy.mockClear()
60+
ctx.blobServerOnRequestSpy.mockClear()
6161

6262
// test the function call
6363
const post1 = await invokeFunction(ctx, { url: '/static-fetch-1' })
@@ -78,7 +78,7 @@ test<FixtureTestContext>('should revalidate a route by tag', async (ctx) => {
7878
getBlobServerGets(ctx, isTagManifest),
7979
`expected tag manifests to be retrieved at most once per tag`,
8080
).toBeDistinct()
81-
ctx.blobServerGetSpy.mockClear()
81+
ctx.blobServerOnRequestSpy.mockClear()
8282

8383
const revalidate = await invokeFunction(ctx, { url: '/api/on-demand-revalidate/tag' })
8484
expect(revalidate.statusCode).toBe(200)
@@ -87,7 +87,7 @@ test<FixtureTestContext>('should revalidate a route by tag', async (ctx) => {
8787
// it does not wait for the revalidation
8888
await new Promise<void>((resolve) => setTimeout(resolve, 100))
8989

90-
ctx.blobServerGetSpy.mockClear()
90+
ctx.blobServerOnRequestSpy.mockClear()
9191

9292
const post2 = await invokeFunction(ctx, { url: '/static-fetch-1' })
9393
const post2Date = load(post2.body)('[data-testid="date-now"]').text()
@@ -109,7 +109,7 @@ test<FixtureTestContext>('should revalidate a route by tag', async (ctx) => {
109109
getBlobServerGets(ctx, isTagManifest),
110110
`expected tag manifests to be retrieved at most once per tag`,
111111
).toBeDistinct()
112-
ctx.blobServerGetSpy.mockClear()
112+
ctx.blobServerOnRequestSpy.mockClear()
113113

114114
// it does not wait for the cache.set so we have to manually wait here until the blob storage got populated
115115
await new Promise<void>((resolve) => setTimeout(resolve, 100))
@@ -134,7 +134,7 @@ test<FixtureTestContext>('should revalidate a route by tag', async (ctx) => {
134134
getBlobServerGets(ctx, isTagManifest),
135135
`expected tag manifests to be retrieved at most once per tag`,
136136
).toBeDistinct()
137-
ctx.blobServerGetSpy.mockClear()
137+
ctx.blobServerOnRequestSpy.mockClear()
138138

139139
const revalidate2 = await invokeFunction(ctx, { url: '/api/on-demand-revalidate/tag' })
140140
expect(revalidate2.statusCode).toBe(200)
@@ -143,7 +143,7 @@ test<FixtureTestContext>('should revalidate a route by tag', async (ctx) => {
143143
// it does not wait for the revalidation
144144
await new Promise<void>((resolve) => setTimeout(resolve, 100))
145145

146-
ctx.blobServerGetSpy.mockClear()
146+
ctx.blobServerOnRequestSpy.mockClear()
147147

148148
const post4 = await invokeFunction(ctx, { url: '/static-fetch-1' })
149149
const post4Date = load(post4.body)('[data-testid="date-now"]').text()
@@ -165,5 +165,5 @@ test<FixtureTestContext>('should revalidate a route by tag', async (ctx) => {
165165
getBlobServerGets(ctx, isTagManifest),
166166
`expected tag manifests to be retrieved at most once per tag`,
167167
).toBeDistinct()
168-
ctx.blobServerGetSpy.mockClear()
168+
ctx.blobServerOnRequestSpy.mockClear()
169169
})

tests/utils/contexts.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { type getStore } from '@netlify/blobs'
1+
import type { getStore } from '@netlify/blobs'
22
import { BlobsServer } from '@netlify/blobs/server'
33
import { type WriteStream } from 'node:fs'
4-
import { MockInstance, TestContext } from 'vitest'
4+
import { TestContext } from 'vitest'
55

66
export interface FixtureTestContext extends TestContext {
77
cwd: string
@@ -10,7 +10,7 @@ export interface FixtureTestContext extends TestContext {
1010
blobStoreHost: string
1111
blobStorePort: number
1212
blobServer: BlobsServer
13-
blobServerGetSpy: MockInstance<Parameters<BlobsServer['get']>, ReturnType<BlobsServer['get']>>
13+
blobServerOnRequestSpy: BlobsServer['onRequest']
1414
blobStore: ReturnType<typeof getStore>
1515
functionDist: string
1616
edgeFunctionPort: number

tests/utils/helpers.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import getPort from 'get-port'
22

33
import { getDeployStore } from '@netlify/blobs'
4-
import { BlobsServer } from '@netlify/blobs/server'
4+
import { BlobsServer, Operation } from '@netlify/blobs/server'
55
import type { NetlifyPluginUtils } from '@netlify/build'
66
import { Buffer } from 'node:buffer'
77
import { mkdtemp } from 'node:fs/promises'
@@ -34,13 +34,14 @@ export const generateRandomObjectID = () => {
3434
export const startMockBlobStore = async (ctx: FixtureTestContext) => {
3535
const port = await getPort()
3636
// create new blob store server
37+
ctx.blobServerOnRequestSpy = vi.fn()
3738
ctx.blobServer = new BlobsServer({
3839
port,
3940
token: BLOB_TOKEN,
41+
onRequest: ctx.blobServerOnRequestSpy,
4042
directory: await mkdtemp(join(tmpdir(), 'opennextjs-netlify-blob-')),
4143
})
4244
await ctx.blobServer.start()
43-
ctx.blobServerGetSpy = vi.spyOn(ctx.blobServer, 'get')
4445
ctx.blobStoreHost = `localhost:${port}`
4546
ctx.blobStorePort = port
4647
vi.stubEnv('NETLIFY_BLOBS_CONTEXT', createBlobContext(ctx))
@@ -72,11 +73,10 @@ export const getBlobEntries = async (ctx: FixtureTestContext) => {
7273

7374
export function getBlobServerGets(ctx: FixtureTestContext, predicate?: (key: string) => boolean) {
7475
const isString = (arg: unknown): arg is string => typeof arg === 'string'
75-
return ctx.blobServerGetSpy.mock.calls
76+
return ctx.blobServerOnRequestSpy.mock.calls
7677
.map(([request]) => {
77-
if (typeof request.url !== 'string') {
78-
return undefined
79-
}
78+
if (request.type !== Operation.GET) return undefined
79+
if (!isString(request.url)) return undefined
8080

8181
let urlSegments = request.url.split('/').slice(1)
8282

0 commit comments

Comments
 (0)