11import { setHeadstartWPConfig } from '@headstartwp/core/utils' ;
2+ import { Rewrite } from 'next/dist/lib/load-custom-routes' ;
23import { withHeadstartWPConfig } from '../withHeadstartWPConfig' ;
34
45// Mock fs module
@@ -61,13 +62,13 @@ describe('withHeadstartWPConfig - Host Check', () => {
6162 } ;
6263
6364 const nextConfig = withHeadstartWPConfig ( { } , headlessConfig ) ;
64- const rewrites = await nextConfig . rewrites ?.( ) ;
65+ const rewrites = ( await nextConfig . rewrites ?.( ) ) ?? [ ] ;
6566
6667 expect ( Array . isArray ( rewrites ) ) . toBe ( true ) ;
6768 expect ( rewrites ) . toHaveLength ( 8 ) ;
6869
6970 // Check that all rewrites have the has check
70- rewrites . forEach ( ( rewrite ) => {
71+ ( rewrites as Rewrite [ ] ) . forEach ( ( rewrite ) => {
7172 expect ( rewrite ) . toHaveProperty ( 'has' ) ;
7273 expect ( rewrite . has ) . toEqual ( [
7374 { type : 'header' , key : 'host' , value : 'example.com' } ,
@@ -82,13 +83,13 @@ describe('withHeadstartWPConfig - Host Check', () => {
8283 } ;
8384
8485 const nextConfig = withHeadstartWPConfig ( { } , headlessConfig ) ;
85- const rewrites = await nextConfig . rewrites ?.( ) ;
86+ const rewrites = ( await nextConfig . rewrites ?.( ) ) ?? [ ] ;
8687
8788 expect ( Array . isArray ( rewrites ) ) . toBe ( true ) ;
8889 expect ( rewrites ) . toHaveLength ( 8 ) ;
8990
9091 // Check that all rewrites have the has check with inferred host
91- rewrites . forEach ( ( rewrite ) => {
92+ ( rewrites as Rewrite [ ] ) . forEach ( ( rewrite ) => {
9293 expect ( rewrite ) . toHaveProperty ( 'has' ) ;
9394 expect ( rewrite . has ) . toEqual ( [
9495 { type : 'header' , key : 'host' , value : 'example.com' } ,
@@ -102,13 +103,13 @@ describe('withHeadstartWPConfig - Host Check', () => {
102103 } ;
103104
104105 const nextConfig = withHeadstartWPConfig ( { } , headlessConfig ) ;
105- const rewrites = await nextConfig . rewrites ?.( ) ;
106+ const rewrites = ( await nextConfig . rewrites ?.( ) ) ?? [ ] ;
106107
107108 expect ( Array . isArray ( rewrites ) ) . toBe ( true ) ;
108109 expect ( rewrites ) . toHaveLength ( 8 ) ;
109110
110111 // Check that no rewrites have the has check
111- rewrites . forEach ( ( rewrite ) => {
112+ ( rewrites as Rewrite [ ] ) . forEach ( ( rewrite ) => {
112113 expect ( rewrite ) . not . toHaveProperty ( 'has' ) ;
113114 } ) ;
114115 } ) ;
@@ -120,13 +121,13 @@ describe('withHeadstartWPConfig - Host Check', () => {
120121 } ;
121122
122123 const nextConfig = withHeadstartWPConfig ( { } , headlessConfig ) ;
123- const rewrites = await nextConfig . rewrites ?.( ) ;
124+ const rewrites = ( await nextConfig . rewrites ?.( ) ) ?? [ ] ;
124125
125126 expect ( Array . isArray ( rewrites ) ) . toBe ( true ) ;
126127 expect ( rewrites ) . toHaveLength ( 8 ) ;
127128
128129 // Check that no rewrites have the has check due to invalid URL
129- rewrites . forEach ( ( rewrite ) => {
130+ ( rewrites as Rewrite [ ] ) . forEach ( ( rewrite ) => {
130131 expect ( rewrite ) . not . toHaveProperty ( 'has' ) ;
131132 } ) ;
132133 } ) ;
@@ -139,13 +140,13 @@ describe('withHeadstartWPConfig - Host Check', () => {
139140 } ;
140141
141142 const nextConfig = withHeadstartWPConfig ( { } , headlessConfig ) ;
142- const rewrites = await nextConfig . rewrites ?.( ) ;
143+ const rewrites = ( await nextConfig . rewrites ?.( ) ) ?? [ ] ;
143144
144145 expect ( Array . isArray ( rewrites ) ) . toBe ( true ) ;
145146 expect ( rewrites ) . toHaveLength ( 8 ) ;
146147
147148 // Check that all rewrites use the explicitly defined host
148- rewrites . forEach ( ( rewrite ) => {
149+ ( rewrites as Rewrite [ ] ) . forEach ( ( rewrite ) => {
149150 expect ( rewrite ) . toHaveProperty ( 'has' ) ;
150151 expect ( rewrite . has ) . toEqual ( [
151152 { type : 'header' , key : 'host' , value : 'explicit.example.com' } ,
@@ -174,7 +175,7 @@ describe('withHeadstartWPConfig - Host Check', () => {
174175 } ;
175176
176177 const nextConfig = withHeadstartWPConfig ( { } , headlessConfig ) ;
177- const rewrites = await nextConfig . rewrites ?.( ) ;
178+ const rewrites = ( await nextConfig . rewrites ?.( ) ) ?? [ ] ;
178179
179180 expect ( Array . isArray ( rewrites ) ) . toBe ( true ) ;
180181 expect ( rewrites ) . toHaveLength ( 24 ) ; // 8 rewrites per site
@@ -217,18 +218,18 @@ describe('withHeadstartWPConfig - Host Check', () => {
217218 } ;
218219
219220 const nextConfig = withHeadstartWPConfig ( { } , headlessConfig ) ;
220- const rewrites = await nextConfig . rewrites ?.( ) ;
221+ const rewrites = ( await nextConfig . rewrites ?.( ) ) ?? [ ] ;
221222
222223 expect ( Array . isArray ( rewrites ) ) . toBe ( true ) ;
223224 expect ( rewrites ) . toHaveLength ( 16 ) ; // 8 rewrites per site
224225
225226 // All rewrites should have has checks
226- rewrites . forEach ( ( rewrite ) => {
227+ ( rewrites as Rewrite [ ] ) . forEach ( ( rewrite ) => {
227228 expect ( rewrite ) . toHaveProperty ( 'has' ) ;
228229 expect ( rewrite . has ) . toHaveLength ( 1 ) ;
229- expect ( rewrite . has [ 0 ] ) . toHaveProperty ( 'type' , 'header' ) ;
230- expect ( rewrite . has [ 0 ] ) . toHaveProperty ( 'key' , 'host' ) ;
231- expect ( [ 'example.com' , 'test.com' ] ) . toContain ( rewrite . has [ 0 ] . value ) ;
230+ expect ( rewrite . has ?. [ 0 ] ) . toHaveProperty ( 'type' , 'header' ) ;
231+ expect ( rewrite . has ?. [ 0 ] ) . toHaveProperty ( 'key' , 'host' ) ;
232+ expect ( [ 'example.com' , 'test.com' ] ) . toContain ( rewrite . has ?. [ 0 ] . value ) ;
232233 } ) ;
233234 } ) ;
234235 } ) ;
@@ -241,20 +242,22 @@ describe('withHeadstartWPConfig - Host Check', () => {
241242 } ;
242243
243244 const nextConfig = withHeadstartWPConfig ( { } , headlessConfig ) ;
244- const rewrites = await nextConfig . rewrites ?.( ) ;
245+ const rewrites = ( await nextConfig . rewrites ?.( ) ) ?? [ ] ;
245246
246247 expect ( Array . isArray ( rewrites ) ) . toBe ( true ) ;
247248
248249 // Check specific rewrite patterns
249- const cacheHealthcheckRewrite = rewrites . find ( ( r ) => r . source === '/cache-healthcheck' ) ;
250+ const cacheHealthcheckRewrite = ( rewrites as Rewrite [ ] ) . find (
251+ ( r ) => r . source === '/cache-healthcheck' ,
252+ ) ;
250253 expect ( cacheHealthcheckRewrite ) . toBeDefined ( ) ;
251254 expect ( cacheHealthcheckRewrite ) . toMatchObject ( {
252255 source : '/cache-healthcheck' ,
253256 destination : '/api/cache-healthcheck' ,
254257 has : [ { type : 'header' , key : 'host' , value : 'example.com' } ] ,
255258 } ) ;
256259
257- const feedRewrite = rewrites . find ( ( r ) => r . source === '/feed' ) ;
260+ const feedRewrite = ( rewrites as Rewrite [ ] ) . find ( ( r ) => r . source === '/feed' ) ;
258261 expect ( feedRewrite ) . toBeDefined ( ) ;
259262 expect ( feedRewrite ) . toMatchObject ( {
260263 source : '/feed' ,
@@ -277,10 +280,10 @@ describe('withHeadstartWPConfig - Host Check', () => {
277280 } ;
278281
279282 const nextConfig = withHeadstartWPConfig (
280- { rewrites : ( ) => existingRewrites } ,
283+ { rewrites : ( ) => Promise . resolve ( existingRewrites ) } ,
281284 headlessConfig ,
282285 ) ;
283- const rewrites = await nextConfig . rewrites ?.( ) ;
286+ const rewrites = ( await nextConfig . rewrites ?.( ) ) ?? [ ] ;
284287
285288 expect ( Array . isArray ( rewrites ) ) . toBe ( true ) ;
286289 expect ( rewrites ) . toHaveLength ( 9 ) ; // 1 existing + 8 default
@@ -315,13 +318,13 @@ describe('withHeadstartWPConfig - Host Check', () => {
315318 } ;
316319
317320 const nextConfig = withHeadstartWPConfig ( { } , headlessConfig ) ;
318- const rewrites = await nextConfig . rewrites ?.( ) ;
321+ const rewrites = ( await nextConfig . rewrites ?.( ) ) ?? [ ] ;
319322
320323 expect ( Array . isArray ( rewrites ) ) . toBe ( true ) ;
321324 expect ( rewrites ) . toHaveLength ( 8 ) ;
322325
323326 // Check that host includes port
324- rewrites . forEach ( ( rewrite ) => {
327+ ( rewrites as Rewrite [ ] ) . forEach ( ( rewrite ) => {
325328 expect ( rewrite ) . toHaveProperty ( 'has' ) ;
326329 expect ( rewrite . has ) . toEqual ( [
327330 { type : 'header' , key : 'host' , value : 'example.com:3000' } ,
@@ -336,13 +339,13 @@ describe('withHeadstartWPConfig - Host Check', () => {
336339 } ;
337340
338341 const nextConfig = withHeadstartWPConfig ( { } , headlessConfig ) ;
339- const rewrites = await nextConfig . rewrites ?.( ) ;
342+ const rewrites = ( await nextConfig . rewrites ?.( ) ) ?? [ ] ;
340343
341344 expect ( Array . isArray ( rewrites ) ) . toBe ( true ) ;
342345 expect ( rewrites ) . toHaveLength ( 8 ) ;
343346
344347 // Check that subdomain is preserved
345- rewrites . forEach ( ( rewrite ) => {
348+ ( rewrites as Rewrite [ ] ) . forEach ( ( rewrite ) => {
346349 expect ( rewrite ) . toHaveProperty ( 'has' ) ;
347350 expect ( rewrite . has ) . toEqual ( [
348351 { type : 'header' , key : 'host' , value : 'subdomain.example.com' } ,
0 commit comments