@@ -5,9 +5,11 @@ import {
55 readdir ,
66 readFile ,
77 rm ,
8+ symlink ,
89 writeFile ,
910} from "node:fs/promises" ;
1011import { join } from "node:path" ;
12+ import { pathToFileURL } from "node:url" ;
1113import { tmpdir } from "node:os" ;
1214import { bundleVersion } from "@webstudio-is/protocol" ;
1315import { generateRedirectsModule , prebuild } from "./prebuild" ;
@@ -20,6 +22,35 @@ const rootFolderId = "root";
2022const elementComponent = "ws:element" ;
2123const slowPrebuildTestTimeout = 15_000 ;
2224type Redirects = Array < { old : string ; new : string ; status ?: "301" | "302" } > ;
25+ type GeneratedRouteModule = {
26+ loader : ( args : { request : Request } ) => Response | Promise < Response > ;
27+ } ;
28+
29+ const importGeneratedRoute = async ( path : string ) => {
30+ await symlink ( join ( originalCwd , "node_modules" ) , "node_modules" , "dir" ) ;
31+ return ( await import (
32+ `${ pathToFileURL ( join ( tempDir , path ) ) . href } ?test=${ crypto . randomUUID ( ) } `
33+ ) ) as GeneratedRouteModule ;
34+ } ;
35+
36+ const expectGeneratedRedirectFallback = async ( path : string ) => {
37+ const routeModule = await importGeneratedRoute ( path ) ;
38+ const redirectResponse = await routeModule . loader ( {
39+ request : new Request ( "https://example.com/dl.php?filename=file.pdf" ) ,
40+ } ) ;
41+ expect ( redirectResponse . status ) . toBe ( 301 ) ;
42+ expect ( redirectResponse . headers . get ( "Location" ) ) . toBe ( "/downloads/file.pdf" ) ;
43+
44+ try {
45+ await routeModule . loader ( {
46+ request : new Request ( "https://example.com/not-a-redirect" ) ,
47+ } ) ;
48+ throw new Error ( "Expected unmatched request to throw a 404 response." ) ;
49+ } catch ( error ) {
50+ expect ( error ) . toBeInstanceOf ( Response ) ;
51+ expect ( ( error as Response ) . status ) . toBe ( 404 ) ;
52+ }
53+ } ;
2354
2455const getFilePaths = async ( dir : string ) : Promise < string [ ] > => {
2556 const entries = await readdir ( dir , { withFileTypes : true } ) ;
@@ -302,6 +333,7 @@ describe("prebuild", () => {
302333 expect ( routeTemplate ) . toContain ( "../__generated__/_index.server" ) ;
303334 expect ( routeTemplate ) . not . toContain ( "__CLIENT__" ) ;
304335 expect ( routeTemplate ) . not . toContain ( "__SERVER__" ) ;
336+ await expectGeneratedRedirectFallback ( "app/routes/$.tsx" ) ;
305337
306338 await expect (
307339 readFile ( "app/__generated__/stale.ts" , "utf8" )
0 commit comments