11import { describe , expect , it , mock } from 'bun:test' ;
2+ import jwt from 'jsonwebtoken' ;
23import rison from 'rison' ;
34
5+ import * as realContext from '@/lib/context' ;
6+
47mock . 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.
1010mock . 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
1816const {
1917 getPPRHeaderRouteParams,
@@ -24,12 +22,20 @@ const {
2422} = await import ( './utils' ) ;
2523type 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+
2733const 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' } ) ;
0 commit comments