Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
modulePathIgnorePatterns: ['<rootDir>/dist/']
modulePathIgnorePatterns: ['<rootDir>/dist/'],
testPathIgnorePatterns: ['__tests__/__mocks__/']
}
11 changes: 7 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"@aws-sdk/client-sts": "3.590.0",
"@aws-sdk/credential-providers": "3.590.0",
"@aws-sdk/util-endpoints": "3.587.0",
"@dbbs/next-cache-handler-core": "1.3.0",
"@dbbs/next-cache-handler-core": "1.4.0",
"aws-cdk-lib": "2.144.0",
"aws-sdk": "2.1635.0",
"body-parser": "^1.20.3",
Expand Down
56 changes: 56 additions & 0 deletions src/__tests__/__mocks__/cacheEntries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import {
CachedRouteKind,
CachedRedirectValue,
IncrementalCachedPageValue,
IncrementalCachedAppPageValue,
CachedFetchValue,
CachedRouteValue
} from '@dbbs/next-cache-handler-core'

export const mockRedirectCacheEntry: CachedRedirectValue = {
kind: CachedRouteKind.REDIRECT,
props: {}
}

export const mockPageCacheEntry: IncrementalCachedPageValue = {
kind: CachedRouteKind.PAGE,
html: '<p>My Page</p>',
pageData: {},
headers: {},
status: 200
}

export const mockAppPageCacheEntry: IncrementalCachedAppPageValue = {
kind: CachedRouteKind.APP_PAGE,
html: '<p>My Page</p>',
rscData: Buffer.from('123'),
headers: {},
postponed: undefined,
status: 200,
segmentData: undefined
}

export const mockFetchCacheEntry: CachedFetchValue = {
kind: CachedRouteKind.FETCH,
data: {
url: 'https://example.com',
headers: {},
body: '123'
},
tags: [],
revalidate: 1000
}

export const mockRouteCacheEntry: CachedRouteValue = {
kind: CachedRouteKind.ROUTE,
body: Buffer.from('123'),
status: 200,
headers: {}
}

export const mockAppRouteCacheEntry: CachedRouteValue = {
kind: CachedRouteKind.APP_ROUTE,
body: Buffer.from('123'),
status: 200,
headers: {}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
import { CacheEntry, CacheContext } from '@dbbs/next-cache-handler-core'
import { S3Cache, TAG_PREFIX } from './s3'
import { CacheEntry, CacheContext, CachedRouteKind } from '@dbbs/next-cache-handler-core'
import { S3Cache, TAG_PREFIX, CACHE_ONE_YEAR } from '../../../cacheHandler/strategy/s3'
import {
mockRedirectCacheEntry,
mockPageCacheEntry,
mockAppPageCacheEntry,
mockFetchCacheEntry,
mockRouteCacheEntry,
mockAppRouteCacheEntry
} from '../../__mocks__/cacheEntries'

const mockHtmlPage = '<p>My Page</p>'

export const mockCacheEntry = {
value: {
pageData: {},
html: mockHtmlPage,
kind: 'PAGE',
postponed: undefined,
kind: CachedRouteKind.PAGE,
headers: undefined,
status: 200
},
lastModified: 100000
} satisfies CacheEntry

const mockRevalidate = 100
const mockCacheControlRevalidateHeader = `s-maxage=${mockRevalidate}, stale-while-revalidate=${CACHE_ONE_YEAR - mockRevalidate}`

const mockCacheContext: CacheContext = {
isAppRouter: false,
serverCacheDirPath: ''
Expand Down Expand Up @@ -87,23 +97,119 @@ describe('S3Cache', () => {
expect(result).toBeNull()
})

it('should set cache for page router', async () => {
await s3Cache.set(cacheKey, cacheKey, mockCacheEntry, mockCacheContext)
it('should not write cache for redirect entry', async () => {
await s3Cache.set(cacheKey, cacheKey, { value: mockRedirectCacheEntry, lastModified: 0 }, mockCacheContext)

expect(s3Cache.client.putObject).not.toHaveBeenCalled()
expect(mockDynamoPutItem).not.toHaveBeenCalled()
})

it('should not write cache if revalidate is 0', async () => {
await s3Cache.set(
cacheKey,
cacheKey,
{ value: mockPageCacheEntry, lastModified: 0, revalidate: 0 },
mockCacheContext
)

expect(s3Cache.client.putObject).not.toHaveBeenCalled()
expect(mockDynamoPutItem).not.toHaveBeenCalled()
})

it('should not write cache if value is null', async () => {
await s3Cache.set(cacheKey, cacheKey, { value: null, lastModified: 0 }, mockCacheContext)

expect(s3Cache.client.putObject).not.toHaveBeenCalled()
expect(mockDynamoPutItem).not.toHaveBeenCalled()
})

it(`should write value for ${CachedRouteKind.APP_PAGE}`, async () => {
await s3Cache.set(
cacheKey,
cacheKey,
{ value: mockAppPageCacheEntry, lastModified: 0, revalidate: mockRevalidate },
mockCacheContext
)

expect(s3Cache.client.putObject).toHaveBeenCalledTimes(2)
expect(s3Cache.client.putObject).toHaveBeenNthCalledWith(1, {
Bucket: mockBucketName,
Key: `${cacheKey}/${cacheKey}.html`,
Body: mockHtmlPage,
ContentType: 'text/html',
CacheControl: mockCacheControlRevalidateHeader,
Metadata: {
'Cache-Fragment-Key': cacheKey
}
})
expect(s3Cache.client.putObject).toHaveBeenNthCalledWith(2, {
Bucket: mockBucketName,
Key: `${cacheKey}/${cacheKey}.rsc`,
Body: mockAppPageCacheEntry.rscData?.toString(),
ContentType: 'text/x-component',
CacheControl: mockCacheControlRevalidateHeader,
Metadata: {
'Cache-Fragment-Key': cacheKey
}
})
expect(mockDynamoPutItem).toHaveBeenCalledWith({
TableName: process.env.DYNAMODB_CACHE_TABLE,
Item: {
pageKey: { S: cacheKey },
cacheKey: { S: cacheKey },
s3Key: { S: `${cacheKey}/${cacheKey}` },
tags: { S: '' },
createdAt: { S: expect.any(String) }
}
})
})

it(`should write value for ${CachedRouteKind.FETCH}`, async () => {
await s3Cache.set(
cacheKey,
cacheKey,
{ value: mockFetchCacheEntry, lastModified: 0, revalidate: mockRevalidate },
mockCacheContext
)

expect(s3Cache.client.putObject).toHaveBeenCalledTimes(1)
expect(s3Cache.client.putObject).toHaveBeenNthCalledWith(1, {
Bucket: mockBucketName,
Key: `${cacheKey}/${cacheKey}.json`,
Body: mockFetchCacheEntry.data.body,
ContentType: 'application/json',
CacheControl: mockCacheControlRevalidateHeader,
Metadata: {
'Cache-Fragment-Key': cacheKey
}
})
expect(mockDynamoPutItem).toHaveBeenCalledWith({
TableName: process.env.DYNAMODB_CACHE_TABLE,
Item: {
pageKey: { S: cacheKey },
cacheKey: { S: cacheKey },
s3Key: { S: `${cacheKey}/${cacheKey}` },
tags: { S: '' },
createdAt: { S: expect.any(String) }
}
})
})

it(`should write value for ${CachedRouteKind.APP_ROUTE}`, async () => {
await s3Cache.set(
cacheKey,
cacheKey,
{ value: mockAppRouteCacheEntry, lastModified: 0, revalidate: mockRevalidate },
mockCacheContext
)

expect(s3Cache.client.putObject).toHaveBeenCalledTimes(1)
expect(s3Cache.client.putObject).toHaveBeenNthCalledWith(1, {
Bucket: mockBucketName,
Key: `${cacheKey}/${cacheKey}.json`,
Body: JSON.stringify(mockCacheEntry),
Body: mockAppRouteCacheEntry.body.toString(),
ContentType: 'application/json',
CacheControl: mockCacheControlRevalidateHeader,
Metadata: {
'Cache-Fragment-Key': cacheKey
}
Expand All @@ -120,32 +226,99 @@ describe('S3Cache', () => {
})
})

it('should set cache for app router', async () => {
await s3Cache.set(cacheKey, cacheKey, mockCacheEntry, { ...mockCacheContext, isAppRouter: true })
expect(s3Cache.client.putObject).toHaveBeenCalledTimes(3)
it(`should write value for ${CachedRouteKind.ROUTE}`, async () => {
await s3Cache.set(
cacheKey,
cacheKey,
{ value: mockRouteCacheEntry, lastModified: 0, revalidate: mockRevalidate },
mockCacheContext
)

expect(s3Cache.client.putObject).toHaveBeenCalledTimes(1)
expect(s3Cache.client.putObject).toHaveBeenNthCalledWith(1, {
Bucket: mockBucketName,
Key: `${cacheKey}/${cacheKey}.json`,
Body: mockRouteCacheEntry.body.toString(),
ContentType: 'application/json',
CacheControl: mockCacheControlRevalidateHeader,
Metadata: {
'Cache-Fragment-Key': cacheKey
}
})
expect(mockDynamoPutItem).toHaveBeenCalledWith({
TableName: process.env.DYNAMODB_CACHE_TABLE,
Item: {
pageKey: { S: cacheKey },
cacheKey: { S: cacheKey },
s3Key: { S: `${cacheKey}/${cacheKey}` },
tags: { S: '' },
createdAt: { S: expect.any(String) }
}
})
})

it(`should write value for ${CachedRouteKind.PAGE} with page router`, async () => {
const pageData = { value: mockPageCacheEntry, lastModified: 0, revalidate: mockRevalidate }
await s3Cache.set(cacheKey, cacheKey, pageData, mockCacheContext)

expect(s3Cache.client.putObject).toHaveBeenCalledTimes(2)
expect(s3Cache.client.putObject).toHaveBeenNthCalledWith(1, {
Bucket: mockBucketName,
Key: `${cacheKey}/${cacheKey}.html`,
Body: mockHtmlPage,
ContentType: 'text/html',
CacheControl: mockCacheControlRevalidateHeader,
Metadata: {
'Cache-Fragment-Key': cacheKey
}
})
expect(s3Cache.client.putObject).toHaveBeenNthCalledWith(2, {
Bucket: mockBucketName,
Key: `${cacheKey}/${cacheKey}.json`,
Body: JSON.stringify(mockCacheEntry),
Body: JSON.stringify(pageData),
ContentType: 'application/json',
CacheControl: mockCacheControlRevalidateHeader,
Metadata: {
'Cache-Fragment-Key': cacheKey
}
})
expect(s3Cache.client.putObject).toHaveBeenNthCalledWith(3, {
expect(mockDynamoPutItem).toHaveBeenCalledWith({
TableName: process.env.DYNAMODB_CACHE_TABLE,
Item: {
pageKey: { S: cacheKey },
cacheKey: { S: cacheKey },
s3Key: { S: `${cacheKey}/${cacheKey}` },
tags: { S: '' },
createdAt: { S: expect.any(String) }
}
})
})

it(`should write value for ${CachedRouteKind.PAGE} with app router`, async () => {
await s3Cache.set(
cacheKey,
cacheKey,
{ value: mockPageCacheEntry, lastModified: 0, revalidate: mockRevalidate },
{ ...mockCacheContext, isAppRouter: true }
)

expect(s3Cache.client.putObject).toHaveBeenCalledTimes(2)
expect(s3Cache.client.putObject).toHaveBeenNthCalledWith(1, {
Bucket: mockBucketName,
Key: `${cacheKey}/${cacheKey}.html`,
Body: mockHtmlPage,
ContentType: 'text/html',
CacheControl: mockCacheControlRevalidateHeader,
Metadata: {
'Cache-Fragment-Key': cacheKey
}
})
expect(s3Cache.client.putObject).toHaveBeenNthCalledWith(2, {
Bucket: mockBucketName,
Key: `${cacheKey}/${cacheKey}.rsc`,
Body: mockCacheEntry.value.pageData,
Body: mockPageCacheEntry.pageData,
ContentType: 'text/x-component',
CacheControl: mockCacheControlRevalidateHeader,
Metadata: {
'Cache-Fragment-Key': cacheKey
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { chunkArray } from './array'
import { chunkArray } from '../../common/array'

describe('chunkArray', () => {
interface TestCase<T> {
Expand Down
Loading