Skip to content

Commit 683ed85

Browse files
committed
Fix PPR test module mocks leaking into the rest of the suite
`mock.module` replaces a module for the whole bun test process, so mocking `@/lib/context` and `jwt-decode` wholesale in the PPR route params test broke every later test file that imported them. Spread the real context module and sign a real JWT instead. Also restore the `x-gitbook-route-site` debug header, commented out by mistake.
1 parent 5f2e3aa commit 683ed85

2 files changed

Lines changed: 21 additions & 15 deletions

File tree

packages/gitbook/src/app/utils.test.ts

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
import { describe, expect, it, mock } from 'bun:test';
2+
import jwt from 'jsonwebtoken';
23
import rison from 'rison';
34

5+
import * as realContext from '@/lib/context';
6+
47
mock.module('server-only', () => ({}));
5-
mock.module('@/lib/adaptive', () => ({
6-
getVisitorAuthClaims: () => ({}),
7-
getVisitorAuthClaimsFromToken: () => ({}),
8-
getPPRVisitorAuthClaimsFromToken: () => ({ scope: 'site-structure' }),
9-
}));
8+
// Only the lookup is stubbed: mocking the whole module would leak into the other test files,
9+
// as `mock.module` replaces it for the entire test process.
1010
mock.module('@/lib/context', () => ({
11+
...realContext,
1112
getBaseContext: (input: unknown) => input,
1213
fetchSiteContextByURLLookup: async (_baseContext: unknown, data: unknown) => data,
1314
}));
14-
mock.module('jwt-decode', () => ({
15-
jwtDecode: () => ({}),
16-
}));
1715

1816
const {
1917
getPPRHeaderRouteParams,
@@ -24,12 +22,20 @@ const {
2422
} = await import('./utils');
2523
type PPRRouteParams = import('./utils').PPRRouteParams;
2624

25+
const apiToken = jwt.sign(
26+
{
27+
exp: Math.floor(Date.now() / 1000) + 3600,
28+
siteStructureClaims: { scope: 'site-structure' },
29+
},
30+
'secret'
31+
);
32+
2733
const routeParams: PPRRouteParams = {
2834
mode: 'url',
2935
siteURL: 'docs.example.com',
3036
siteData: encodeURIComponent(
3137
rison.encode({
32-
apiToken: 'ppr-api-token',
38+
apiToken,
3339
site: 'site-id',
3440
siteSection: 'page-site-section-id',
3541
siteSpace: 'page-site-space-id',
@@ -65,7 +71,7 @@ describe('getPPRRouteParams', () => {
6571
expect(params).not.toHaveProperty('revalidationId');
6672
expect(params).not.toHaveProperty('pprDefaults');
6773
expect(getSiteURLDataFromParams(params)).toMatchObject({
68-
apiToken: 'ppr-api-token',
74+
apiToken,
6975
site: 'site-id',
7076
space: 'space-id',
7177
revision: 'ppr-revision-id',
@@ -79,7 +85,7 @@ describe('PPR cache region params', () => {
7985
...routeParams,
8086
siteData: encodeURIComponent(
8187
rison.encode({
82-
apiToken: 'new-ppr-api-token',
88+
apiToken: jwt.sign({ siteStructureClaims: {} }, 'other-secret'),
8389
site: 'site-id',
8490
siteSection: 'new-page-site-section-id',
8591
siteSpace: 'new-page-site-space-id',
@@ -99,7 +105,7 @@ describe('PPR cache region params', () => {
99105
);
100106

101107
expect(headerData).toMatchObject({
102-
apiToken: 'ppr-api-token',
108+
apiToken,
103109
siteSection: 'default-site-section-id',
104110
siteSpace: 'default-site-space-id',
105111
space: 'default-space-id',
@@ -140,7 +146,7 @@ describe('PPR cache region params', () => {
140146
);
141147

142148
expect(tocData).toMatchObject({
143-
apiToken: 'ppr-api-token',
149+
apiToken,
144150
siteSection: 'page-site-section-id',
145151
siteSpace: 'page-site-space-id',
146152
space: 'space-id',
@@ -172,7 +178,7 @@ describe('getPPRStaticSiteContext', () => {
172178
);
173179

174180
expect(context).toMatchObject({
175-
apiToken: 'ppr-api-token',
181+
apiToken,
176182
revision: 'ppr-revision-id',
177183
});
178184
expect(visitorAuthClaims).toEqual({ scope: 'site-structure' });

packages/gitbook/src/middleware.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ async function serveSiteRoutes(requestURL: URL, request: NextRequest) {
582582
response.headers.set('x-content-type-options', 'nosniff');
583583
// Debug header
584584
response.headers.set('x-gitbook-route-type', routeType);
585-
// response.headers.set('x-gitbook-route-site', siteURLWithoutProtocol);
585+
response.headers.set('x-gitbook-route-site', siteURLWithoutProtocol);
586586

587587
// noindex search/assistant deep links, kept crawlable so Google sees the directive.
588588
if (rewrittenURL.searchParams.has('ask') || rewrittenURL.searchParams.has('q')) {

0 commit comments

Comments
 (0)