@@ -2,7 +2,7 @@ import * as fs from "node:fs/promises";
22import  *  as  os  from  "node:os" ; 
33import  *  as  path  from  "node:path" ; 
44import  {  resolve  }  from  "node:path" ; 
5- import  {  describe ,  expect ,  test ,  vi  }  from  "vitest" ; 
5+ import  {  describe ,  expect ,  onTestFinished ,   test ,  vi  }  from  "vitest" ; 
66import  { 
77	runLongLived , 
88	waitForReady , 
@@ -44,9 +44,29 @@ async function runWranglerDev(
4444		{  WRANGLER_REGISTRY_PATH : devRegistryPath  } 
4545	) ; 
4646
47+ 	onTestFinished ( ( )  =>  session . stop ( ) ) ; 
48+ 
4749	return  `http://${ session . ip } ${ session . port }  ; 
4850} 
4951
52+ async  function  setupPlatformProxy ( config : string ,  devRegistryPath ?: string )  { 
53+ 	vi . stubEnv ( "WRANGLER_REGISTRY_PATH" ,  devRegistryPath ) ; 
54+ 	try  { 
55+ 		// @ts -expect-error It works 
56+ 		const  wrangler  =  await  import ( "wrangler" ) ; 
57+ 		const  proxy  =  await  wrangler . getPlatformProxy < Record < string ,  any > > ( { 
58+ 			configPath : config , 
59+ 		} ) ; 
60+ 
61+ 		onTestFinished ( ( )  =>  proxy . dispose ( ) ) ; 
62+ 
63+ 		return  proxy ; 
64+ 	}  finally  { 
65+ 		// Unset the environment variable to avoid affecting other tests 
66+ 		vi . stubEnv ( "WRANGLER_REGISTRY_PATH" ,  undefined ) ; 
67+ 	} 
68+ } 
69+ 
5070describe ( "Dev Registry: vite dev <-> vite dev" ,  ( )  =>  { 
5171	it ( "supports module worker fetch over service binding" ,  async  ( { 
5272		devRegistryPath, 
@@ -432,3 +452,150 @@ describe("Dev Registry: vite dev <-> wrangler dev", () => {
432452		} ) ; 
433453	} ) ; 
434454} ) ; 
455+ 
456+ describe ( "Dev Registry: getPlatformProxy -> wrangler / vite dev" ,  ( )  =>  { 
457+ 	it ( "supports fetch over service binding" ,  async  ( {  devRegistryPath } )  =>  { 
458+ 		const  {  env }  =  await  setupPlatformProxy ( 
459+ 			"wrangler.worker-entrypoint-a.jsonc" , 
460+ 			devRegistryPath 
461+ 		) ; 
462+ 
463+ 		await  vi . waitFor ( async  ( )  =>  { 
464+ 			const  response  =  await  env . WORKER_ENTRYPOINT_B . fetch ( "http://localhost" ) ; 
465+ 
466+ 			expect ( response . status ) . toBe ( 503 ) ; 
467+ 			expect ( await  response . text ( ) ) . toEqual ( 
468+ 				`Couldn't find a local dev session for the "default" entrypoint of service "worker-entrypoint-b" to proxy to` 
469+ 			) ; 
470+ 		} ) ; 
471+ 
472+ 		await  vi . waitFor ( async  ( )  =>  { 
473+ 			const  response  =  await  env . MODULE_WORKER . fetch ( "http://localhost" ) ; 
474+ 
475+ 			expect ( response . status ) . toBe ( 503 ) ; 
476+ 			expect ( await  response . text ( ) ) . toEqual ( 
477+ 				`Couldn't find a local dev session for the "default" entrypoint of service "module-worker" to proxy to` 
478+ 			) ; 
479+ 		} ) ; 
480+ 
481+ 		await  runViteDev ( "vite.worker-entrypoint-b.config.ts" ,  devRegistryPath ) ; 
482+ 
483+ 		await  vi . waitFor ( async  ( )  =>  { 
484+ 			const  response  =  await  env . WORKER_ENTRYPOINT_B . fetch ( "http://localhost" ) ; 
485+ 
486+ 			expect ( await  response . text ( ) ) . toEqual ( "Hello from Worker Entrypoint!" ) ; 
487+ 			expect ( response . status ) . toBe ( 200 ) ; 
488+ 		} ) ; 
489+ 
490+ 		await  vi . waitFor ( async  ( )  =>  { 
491+ 			const  response  =  await  env . MODULE_WORKER . fetch ( "http://localhost" ) ; 
492+ 
493+ 			expect ( response . status ) . toBe ( 503 ) ; 
494+ 			expect ( await  response . text ( ) ) . toEqual ( 
495+ 				`Couldn't find a local dev session for the "default" entrypoint of service "module-worker" to proxy to` 
496+ 			) ; 
497+ 		} ) ; 
498+ 
499+ 		await  runWranglerDev ( "wrangler.module-worker.jsonc" ,  devRegistryPath ) ; 
500+ 
501+ 		await  vi . waitFor ( async  ( )  =>  { 
502+ 			const  response  =  await  env . MODULE_WORKER . fetch ( "http://localhost" ) ; 
503+ 
504+ 			expect ( await  response . text ( ) ) . toEqual ( "Hello from Module Worker!" ) ; 
505+ 			expect ( response . status ) . toBe ( 200 ) ; 
506+ 		} ) ; 
507+ 
508+ 		await  vi . waitFor ( async  ( )  =>  { 
509+ 			const  response  =  await  env . WORKER_ENTRYPOINT_B . fetch ( "http://localhost" ) ; 
510+ 
511+ 			expect ( await  response . text ( ) ) . toEqual ( "Hello from Worker Entrypoint!" ) ; 
512+ 			expect ( response . status ) . toBe ( 200 ) ; 
513+ 		} ) ; 
514+ 	} ) ; 
515+ 
516+ 	it ( "supports RPC over service binding" ,  async  ( {  devRegistryPath } )  =>  { 
517+ 		const  {  env }  =  await  setupPlatformProxy ( 
518+ 			"wrangler.module-worker.jsonc" , 
519+ 			devRegistryPath 
520+ 		) ; 
521+ 
522+ 		expect ( ( )  => 
523+ 			env . WORKER_ENTRYPOINT_A . ping ( ) 
524+ 		) . toThrowErrorMatchingInlineSnapshot ( 
525+ 			`[Error: Cannot access "ping" as we couldn't find a local dev session for the "default" entrypoint of service "worker-entrypoint-a" to proxy to.]` 
526+ 		) ; 
527+ 
528+ 		expect ( ( )  => 
529+ 			env . WORKER_ENTRYPOINT_B . ping ( ) 
530+ 		) . toThrowErrorMatchingInlineSnapshot ( 
531+ 			`[Error: Cannot access "ping" as we couldn't find a local dev session for the "default" entrypoint of service "worker-entrypoint-b" to proxy to.]` 
532+ 		) ; 
533+ 
534+ 		await  runViteDev ( "vite.worker-entrypoint-a.config.ts" ,  devRegistryPath ) ; 
535+ 
536+ 		await  vi . waitFor ( async  ( )  =>  { 
537+ 			const  result  =  await  env . WORKER_ENTRYPOINT_A . ping ( ) ; 
538+ 			expect ( result ) . toBe ( "Pong" ) ; 
539+ 		} ) ; 
540+ 
541+ 		await  runWranglerDev ( "wrangler.worker-entrypoint-b.jsonc" ,  devRegistryPath ) ; 
542+ 
543+ 		await  vi . waitFor ( async  ( )  =>  { 
544+ 			const  result  =  await  env . WORKER_ENTRYPOINT_B . ping ( ) ; 
545+ 
546+ 			expect ( result ) . toBe ( "Pong" ) ; 
547+ 		} ) ; 
548+ 	} ) ; 
549+ 
550+ 	it ( "supports fetch over durable object binding" ,  async  ( { 
551+ 		devRegistryPath, 
552+ 	} )  =>  { 
553+ 		const  {  env }  =  await  setupPlatformProxy ( 
554+ 			"wrangler.external-durable-object.jsonc" , 
555+ 			devRegistryPath 
556+ 		) ; 
557+ 		const  id  =  env . DURABLE_OBJECT . newUniqueId ( ) ; 
558+ 		const  stub  =  env . DURABLE_OBJECT . get ( id ) ; 
559+ 
560+ 		await  vi . waitFor ( async  ( )  =>  { 
561+ 			const  response  =  await  stub . fetch ( "http://localhost" ) ; 
562+ 			expect ( response . status ) . toBe ( 503 ) ; 
563+ 			expect ( await  response . text ( ) ) . toEqual ( "Service Unavailable" ) ; 
564+ 		} ) ; 
565+ 
566+ 		await  runWranglerDev ( 
567+ 			"wrangler.internal-durable-object.jsonc" , 
568+ 			devRegistryPath 
569+ 		) ; 
570+ 
571+ 		await  vi . waitFor ( async  ( )  =>  { 
572+ 			const  response  =  await  stub . fetch ( "http://localhost" ) ; 
573+ 
574+ 			expect ( response . status ) . toBe ( 200 ) ; 
575+ 			expect ( await  response . text ( ) ) . toEqual ( "Hello from Durable Object!" ) ; 
576+ 		} ) ; 
577+ 	} ) ; 
578+ 
579+ 	it ( "supports RPC over durable object binding" ,  async  ( { 
580+ 		devRegistryPath, 
581+ 	} )  =>  { 
582+ 		const  {  env }  =  await  setupPlatformProxy ( 
583+ 			"wrangler.external-durable-object.jsonc" , 
584+ 			devRegistryPath 
585+ 		) ; 
586+ 		const  id  =  env . DURABLE_OBJECT . newUniqueId ( ) ; 
587+ 		const  stub  =  env . DURABLE_OBJECT . get ( id ) ; 
588+ 
589+ 		expect ( ( )  =>  stub . ping ( ) ) . toThrowErrorMatchingInlineSnapshot ( 
590+ 			`[Error: Cannot access "ping" as Durable Object RPC is not yet supported between multiple dev sessions.]` 
591+ 		) ; 
592+ 		await  runWranglerDev ( 
593+ 			"wrangler.internal-durable-object.jsonc" , 
594+ 			devRegistryPath 
595+ 		) ; 
596+ 
597+ 		expect ( ( )  =>  stub . ping ( ) ) . toThrowErrorMatchingInlineSnapshot ( 
598+ 			`[Error: Cannot access "ping" as Durable Object RPC is not yet supported between multiple dev sessions.]` 
599+ 		) ; 
600+ 	} ) ; 
601+ } ) ; 
0 commit comments