11import { spawnSync } from 'node:child_process' ;
2- import { mkdirSync , mkdtempSync , readFileSync , rmSync , symlinkSync , writeFileSync } from 'node:fs' ;
2+ import {
3+ cpSync ,
4+ mkdirSync ,
5+ mkdtempSync ,
6+ readFileSync ,
7+ rmSync ,
8+ symlinkSync ,
9+ writeFileSync ,
10+ } from 'node:fs' ;
311import { createRequire } from 'node:module' ;
412import { tmpdir } from 'node:os' ;
513import { dirname , join } from 'node:path' ;
14+ import { pathToFileURL } from 'node:url' ;
615import { Miniflare , convertV4MiniflareOptions } from 'miniflare' ;
716import { chromium } from 'playwright' ;
8- import { build } from 'vite' ;
17+ import { build , transformWithOxc } from 'vite' ;
918import { afterEach , describe , expect , it } from 'vite-plus/test' ;
1019import { analyzeApplication } from '../../src/application-routing.ts' ;
1120import { createProjectTsconfig } from '../../src/codegen/project-tsconfig.ts' ;
@@ -484,7 +493,7 @@ describe('standalone routeUrl utility', () => {
484493 async ( declarations ) => {
485494 const root = await createFixture ( {
486495 extraFiles : {
487- 'check-compatibility.ts' : `import { routeUrl, type RouteDestination, type RouteOptions } from 'void/routes';
496+ 'check-compatibility.ts' : `import { routeUrl, type RouteDestination, type RouteOptions, type RoutePath } from 'void/routes';
488497function userUrl<T extends string | number>(id: T) {
489498 return routeUrl('/users/[id]', { params: { id } });
490499}
@@ -500,6 +509,9 @@ function forwardDestination<T extends RouteDestination<'/users/[id]'>>(destinati
500509function forwardArgs(...args: Parameters<typeof routeUrl<'/users/[id]'>>) {
501510 return routeUrl(...args);
502511}
512+ function forwardGenericArgs<P extends RoutePath>(...args: Parameters<typeof routeUrl<P>>) {
513+ return routeUrl(...args);
514+ }
503515forwardArgs('/users/[id]', { params: { id: 42 } });
504516const user = { id: 42, name: 'Alice' };
505517routeUrl('/users/[id]', { params: user });
@@ -511,7 +523,7 @@ const destination: RouteDestination<'/users/[id]'> = {
511523routeUrl('/users/[id]', destination);
512524const staticDestination: RouteDestination<'/about'> = { pathname: '/about' };
513525routeUrl('/about', staticDestination);
514- void [userUrl, forwardOptions, forwardStaticOptions, forwardDestination, forwardArgs];
526+ void [userUrl, forwardOptions, forwardStaticOptions, forwardDestination, forwardArgs, forwardGenericArgs ];
515527` ,
516528 } ,
517529 } ) ;
@@ -522,6 +534,92 @@ void [userUrl, forwardOptions, forwardStaticOptions, forwardDestination, forward
522534 } ,
523535 ) ;
524536
537+ it . each ( [ 'generated' , 'package' ] as const ) (
538+ 'retains URL checks after an SDK upgrade without regenerating the %s declarations' ,
539+ async ( declarations ) => {
540+ const root = mkdtempSync ( join ( tmpdir ( ) , 'void-route-upgrade-' ) ) ;
541+ roots . push ( root ) ;
542+ const outDir = join ( root , '.void' ) ;
543+ const packageLink = join ( root , 'node_modules/void' ) ;
544+ mkdirSync ( outDir , { recursive : true } ) ;
545+ const manifest = JSON . parse ( readFileSync ( join ( packageRoot , 'package.json' ) , 'utf8' ) ) ;
546+ const generatorCode = (
547+ await transformWithOxc (
548+ readFileSync ( join ( packageRoot , 'src/codegen/route-types.ts' ) , 'utf8' ) ,
549+ 'route-types.ts' ,
550+ )
551+ ) . code ;
552+ function createSdkVersion ( version : string ) : string {
553+ const directory = join ( root , 'node_modules/.pnpm' , `void@${ version } /node_modules/void` ) ;
554+ mkdirSync ( join ( directory , 'src/codegen' ) , { recursive : true } ) ;
555+ cpSync ( join ( packageRoot , 'dist' ) , join ( directory , 'dist' ) , { recursive : true } ) ;
556+ symlinkSync ( join ( packageRoot , 'node_modules' ) , join ( directory , 'node_modules' ) , 'junction' ) ;
557+ writeFileSync ( join ( directory , 'src/codegen/route-types.mjs' ) , generatorCode ) ;
558+ writeFileSync ( join ( directory , 'package.json' ) , JSON . stringify ( { ...manifest , version } ) ) ;
559+ return directory ;
560+ }
561+ const oldSdk = createSdkVersion ( '1.9.1' ) ;
562+ const newSdk = createSdkVersion ( '1.9.2' ) ;
563+ symlinkSync ( oldSdk , packageLink , 'junction' ) ;
564+ writeFileSync ( join ( root , 'package.json' ) , '{"type":"module"}' ) ;
565+ const generator = pathToFileURL ( join ( oldSdk , 'src/codegen/route-types.mjs' ) ) . href ;
566+ const generation = spawnSync (
567+ process . execPath ,
568+ [
569+ '--input-type=module' ,
570+ '-e' ,
571+ `import { generateRouteTypes } from ${ JSON . stringify ( generator ) } ;
572+ const scan = { routes: [
573+ { pattern: '/about', params: [], methods: [] },
574+ { pattern: '/users/:id', params: ['id'], methods: [] },
575+ ], pages: { pages: [] } };
576+ process.stdout.write(generateRouteTypes(scan, ${ JSON . stringify ( join ( root , 'routes' ) ) } , ${ JSON . stringify ( outDir ) } , undefined, { typedRoutes: true }).dts);` ,
577+ ] ,
578+ { encoding : 'utf8' } ,
579+ ) ;
580+ expect ( { status : generation . status , errors : generation . stderr } ) . toEqual ( {
581+ status : 0 ,
582+ errors : '' ,
583+ } ) ;
584+ const routeTypesPath = join ( outDir , 'routes.d.ts' ) ;
585+ writeFileSync ( routeTypesPath , generation . stdout ) ;
586+ writeFileSync (
587+ join ( outDir , 'tsconfig.json' ) ,
588+ JSON . stringify ( createProjectTsconfig ( root , true ) ) ,
589+ ) ;
590+ writeFileSync (
591+ join ( root , 'check-upgrade.ts' ) ,
592+ `import { routeUrl, type Href } from 'void/routes';
593+ const href: Href = routeUrl('/users/[id]', { params: { id: 42 } });
594+ routeUrl('/about');
595+ // @ts-expect-error unknown route patterns remain checked
596+ routeUrl('/missing');
597+ // @ts-expect-error required params remain checked
598+ routeUrl('/users/[id]');
599+ // @ts-expect-error param values remain checked
600+ routeUrl('/users/[id]', { params: { id: true } });
601+ // @ts-expect-error raw URLs retain their existing route checks
602+ const unknown: Href = '/missing';
603+ void [href, unknown];
604+ ` ,
605+ ) ;
606+ expect ( typecheck ( root , declarations , [ 'check-upgrade.ts' ] ) ) . toEqual ( {
607+ output : '' ,
608+ status : 0 ,
609+ } ) ;
610+
611+ rmSync ( packageLink , { force : true , recursive : true } ) ;
612+ symlinkSync ( newSdk , packageLink , 'junction' ) ;
613+ rmSync ( oldSdk , { force : true , recursive : true } ) ;
614+
615+ expect ( readFileSync ( routeTypesPath , 'utf8' ) ) . toBe ( generation . stdout ) ;
616+ expect ( typecheck ( root , declarations , [ 'check-upgrade.ts' ] ) ) . toEqual ( {
617+ output : '' ,
618+ status : 0 ,
619+ } ) ;
620+ } ,
621+ ) ;
622+
525623 it . each ( [ 'generated' , 'package' ] as const ) (
526624 'rejects invalid unions and numeric keys through the %s declarations' ,
527625 async ( declarations ) => {
0 commit comments