@@ -5,6 +5,7 @@ import { describe, expect, it } from "@effect/vitest";
55import { Schema } from "effect" ;
66import { unstable_readConfig } from "wrangler" ;
77
8+ import { frontResponse } from "./front" ;
89import { parseV2Edge , v2EdgeResponse , type V2EdgeEnv , type V2Service } from "./marketing" ;
910
1011// The deployed edge reads its v2 settings from wrangler.jsonc. These tests read
@@ -18,6 +19,23 @@ const config = Schema.decodeUnknownSync(WranglerConfig)(
1819 unstable_readConfig ( { config : fileURLToPath ( new URL ( "../../wrangler.jsonc" , import . meta. url ) ) } ) ,
1920) ;
2021
22+ // The front Worker (wrangler.edge.jsonc) runs the same edge on executor.sh
23+ // before `executor-cloud`, with its own copy of the settings.
24+ const EdgeWranglerConfig = Schema . Struct ( {
25+ main : Schema . String ,
26+ vars : Schema . Record ( Schema . String , Schema . Unknown ) ,
27+ services : Schema . Array ( Schema . Struct ( { binding : Schema . String , service : Schema . String } ) ) ,
28+ routes : Schema . Array (
29+ Schema . Struct ( { pattern : Schema . String , zone_name : Schema . optional ( Schema . String ) } ) ,
30+ ) ,
31+ placement : Schema . optional ( Schema . Unknown ) ,
32+ } ) ;
33+ const edgeConfig = Schema . decodeUnknownSync ( EdgeWranglerConfig ) (
34+ unstable_readConfig ( {
35+ config : fileURLToPath ( new URL ( "../../wrangler.edge.jsonc" , import . meta. url ) ) ,
36+ } ) ,
37+ ) ;
38+
2139const stringVar = ( name : string ) : string | undefined => {
2240 const value = config . vars [ name ] ;
2341 return typeof value === "string" ? value : undefined ;
@@ -34,6 +52,20 @@ const shipped: V2EdgeEnv = {
3452 V2_ERROR_TUNNEL_PATH : stringVar ( "V2_ERROR_TUNNEL_PATH" ) ,
3553} ;
3654
55+ const edgeStringVar = ( name : string ) : string | undefined => {
56+ const value = edgeConfig . vars [ name ] ;
57+ return typeof value === "string" ? value : undefined ;
58+ } ;
59+
60+ /** The front Worker's shipped settings, with the same stand-in. */
61+ const frontShipped : V2EdgeEnv = {
62+ V2 : standIn ,
63+ V2_SIGN_UP_URL : edgeStringVar ( "V2_SIGN_UP_URL" ) ,
64+ V2_OAUTH_STATE_PREFIX : edgeStringVar ( "V2_OAUTH_STATE_PREFIX" ) ,
65+ V2_ANALYTICS_PROXY_PATH : edgeStringVar ( "V2_ANALYTICS_PROXY_PATH" ) ,
66+ V2_ERROR_TUNNEL_PATH : edgeStringVar ( "V2_ERROR_TUNNEL_PATH" ) ,
67+ } ;
68+
3769describe ( "production v2 edge settings" , ( ) => {
3870 it ( "binds V2 to the unplaced marketing gateway" , ( ) => {
3971 const bindings = config . services . filter ( ( service ) => service . binding === "V2" ) ;
@@ -66,6 +98,40 @@ describe("production v2 edge settings", () => {
6698 } ) ;
6799} ) ;
68100
101+ describe ( "the executor.sh front Worker's settings" , ( ) => {
102+ it ( "runs the front entry on a zone route for every executor.sh path" , ( ) => {
103+ expect ( edgeConfig . main ) . toMatch ( / s r c \/ e d g e \/ f r o n t \. t s $ / ) ;
104+ expect ( edgeConfig . routes ) . toEqual ( [ { pattern : "executor.sh/*" , zone_name : "executor.sh" } ] ) ;
105+ } ) ;
106+
107+ // Placement would send every request it answers to the placed region,
108+ // which is the trip this Worker exists to avoid.
109+ it ( "is not placed" , ( ) => {
110+ expect ( edgeConfig . placement ) . toBeUndefined ( ) ;
111+ } ) ;
112+
113+ it ( "ships the same v2 settings as executor-cloud" , ( ) => {
114+ const v2Vars = ( vars : Readonly < Record < string , unknown > > ) =>
115+ Object . fromEntries ( Object . entries ( vars ) . filter ( ( [ name ] ) => name . startsWith ( "V2_" ) ) ) ;
116+ expect ( Object . keys ( v2Vars ( edgeConfig . vars ) ) . toSorted ( ) ) . toEqual ( [
117+ "V2_ANALYTICS_PROXY_PATH" ,
118+ "V2_ERROR_TUNNEL_PATH" ,
119+ "V2_OAUTH_STATE_PREFIX" ,
120+ "V2_SIGN_UP_URL" ,
121+ ] ) ;
122+ expect ( v2Vars ( edgeConfig . vars ) ) . toEqual ( v2Vars ( config . vars ) ) ;
123+ } ) ;
124+
125+ it ( "binds the same v2 and marketing Workers as executor-cloud" , ( ) => {
126+ const bound = ( services : typeof config . services ) =>
127+ services
128+ . filter ( ( service ) => service . binding === "V2" || service . binding === "MARKETING" )
129+ . toSorted ( ( a , b ) => a . binding . localeCompare ( b . binding ) ) ;
130+ expect ( bound ( edgeConfig . services ) ) . toEqual ( bound ( config . services ) ) ;
131+ expect ( bound ( edgeConfig . services ) ) . toHaveLength ( 2 ) ;
132+ } ) ;
133+ } ) ;
134+
69135// v2 publishes the exact set of executor.sh requests v1's edge forwards to it.
70136// `v2-edge-contract.json` is a verbatim copy of v2's edge contract; update it
71137// only together with v2's contract, never by hand here. Every case runs
@@ -133,6 +199,38 @@ describe("v2's edge contract", () => {
133199 } ) ;
134200 }
135201
202+ // The front Worker answers the same requests the same way: everything the
203+ // contract forwards reaches v2 once, and nothing else reaches it.
204+ const runFrontCase = async ( method : string , target : string ) => {
205+ const calls : Request [ ] = [ ] ;
206+ const v2 : V2Service = {
207+ fetch : ( request ) => {
208+ calls . push ( request ) ;
209+ return Promise . resolve ( new Response ( "v2" ) ) ;
210+ } ,
211+ } ;
212+ const request = new Request ( `${ contract . origin } ${ target } ` , { method } ) ;
213+ const response = await frontResponse ( request , { ...frontShipped , V2 : v2 } ) ;
214+ return { request, calls, response } ;
215+ } ;
216+
217+ for ( const { method, target } of forwarded ) {
218+ it ( `front Worker forwards ${ method } ${ target } to v2` , async ( ) => {
219+ const { request, calls, response } = await runFrontCase ( method , target ) ;
220+ expect ( calls ) . toHaveLength ( 1 ) ;
221+ expect ( calls [ 0 ] ?. url ) . toBe ( request . url ) ;
222+ expect ( calls [ 0 ] ?. method ) . toBe ( method ) ;
223+ expect ( await response ?. text ( ) ) . toBe ( "v2" ) ;
224+ } ) ;
225+ }
226+
227+ for ( const { method, target } of contract . cases . filter ( ( c ) => ! c . forwards ) ) {
228+ it ( `front Worker does not forward ${ method } ${ target } to v2` , async ( ) => {
229+ const { calls } = await runFrontCase ( method , target ) ;
230+ expect ( calls ) . toHaveLength ( 0 ) ;
231+ } ) ;
232+ }
233+
136234 // Sign-up is not forwarded: the edge redirects it to v2's sign-up page.
137235 it ( "lists both sign-up paths as not forwarded" , ( ) => {
138236 expect ( signUps . map ( ( c ) => c . target ) . toSorted ( ) ) . toEqual ( [ "/sign-up" , "/signup" ] ) ;
0 commit comments