@@ -8,6 +8,7 @@ use serde_json::{json, Value};
88use std:: fs;
99use std:: net:: TcpListener ;
1010use std:: path:: PathBuf ;
11+ use std:: process:: Stdio ;
1112use std:: time:: Duration ;
1213use tempfile:: TempDir ;
1314use tokio:: process:: { Child , Command } ;
@@ -31,6 +32,7 @@ async fn durable_object_person_updates_apply() -> Result<(), Box<dyn std::error:
3132 & pipeline_endpoint. to_string ( ) ,
3233 debug_token,
3334 ) ?;
35+ patch_worker_bundle ( ) ?;
3436
3537 let mut wrangler = spawn_wrangler_dev ( & config_path, port) ?;
3638 wait_for_health ( port) . await ?;
@@ -43,8 +45,10 @@ async fn durable_object_person_updates_apply() -> Result<(), Box<dyn std::error:
4345 . post ( format ! ( "{base_url}/identify" ) )
4446 . json ( & json ! ( {
4547 "distinct_id" : "person-1" ,
46- "properties" : { "email" : "person1@example.com" } ,
47- "$set_once" : { "created_at" : "2024-01-01" }
48+ "properties" : {
49+ "$set" : { "email" : "person1@example.com" } ,
50+ "$set_once" : { "created_at" : "2024-01-01" }
51+ }
4852 } ) )
4953 . send ( )
5054 . await ?
@@ -78,8 +82,8 @@ async fn durable_object_person_updates_apply() -> Result<(), Box<dyn std::error:
7882 client
7983 . post ( format ! ( "{base_url}/alias" ) )
8084 . json ( & json ! ( {
81- "distinct_id" : "anon -1" ,
82- "alias" : "person -1"
85+ "distinct_id" : "person -1" ,
86+ "alias" : "anon -1"
8387 } ) )
8488 . send ( )
8589 . await ?
@@ -105,7 +109,7 @@ fn write_wrangler_config(
105109 pipeline_endpoint : & str ,
106110 debug_token : & str ,
107111) -> Result < PathBuf , Box < dyn std:: error:: Error > > {
108- let main_path = std:: env:: current_dir ( ) ?. join ( "build/index.js " ) ;
112+ let main_path = std:: env:: current_dir ( ) ?. join ( "build/worker.mjs " ) ;
109113 let config = format ! (
110114 r#"
111115name = "hogflare-test"
@@ -117,13 +121,20 @@ CLOUDFLARE_PIPELINE_ENDPOINT = "{pipeline}"
117121CLOUDFLARE_PIPELINE_TIMEOUT_SECS = "5"
118122PERSON_DEBUG_TOKEN = "{debug_token}"
119123
124+ [build.upload]
125+ format = "modules"
126+
120127[[durable_objects.bindings]]
121128name = "PERSONS"
122129class_name = "PersonDurableObject"
123130
131+ [[durable_objects.bindings]]
132+ name = "PERSON_ID_COUNTER"
133+ class_name = "PersonIdCounterDurableObject"
134+
124135[[migrations]]
125136tag = "v1"
126- new_classes = ["PersonDurableObject"]
137+ new_classes = ["PersonDurableObject", "PersonIdCounterDurableObject" ]
127138"# ,
128139 main = main_path. display( ) ,
129140 pipeline = pipeline_endpoint,
@@ -135,12 +146,38 @@ new_classes = ["PersonDurableObject"]
135146 Ok ( path)
136147}
137148
149+ fn patch_worker_bundle ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
150+ let build_dir = std:: env:: current_dir ( ) ?. join ( "build" ) ;
151+ let bundle_path = build_dir. join ( "index.js" ) ;
152+ if !bundle_path. exists ( ) {
153+ return Err ( "missing build/index.js; run worker-build before tests" . into ( ) ) ;
154+ }
155+ let contents = fs:: read_to_string ( & bundle_path) ?;
156+ let patched = if contents. starts_with ( "import source wasmModule" ) {
157+ contents. replacen (
158+ "import source wasmModule from" ,
159+ "import wasmModule from" ,
160+ 1 ,
161+ )
162+ } else {
163+ contents
164+ } ;
165+
166+ fs:: write ( & bundle_path, & patched) ?;
167+ fs:: write ( build_dir. join ( "index.mjs" ) , & patched) ?;
168+
169+ let worker_shim = r#"export { default } from "./index.mjs";
170+ export * from "./index.mjs";
171+ "# ;
172+ fs:: write ( build_dir. join ( "worker.mjs" ) , worker_shim) ?;
173+ Ok ( ( ) )
174+ }
175+
138176fn spawn_wrangler_dev ( config_path : & PathBuf , port : u16 ) -> Result < Child , Box < dyn std:: error:: Error > > {
139177 let child = Command :: new ( "bunx" )
140178 . arg ( "wrangler" )
141179 . arg ( "dev" )
142180 . arg ( "--local" )
143- . arg ( "--no-bundle" )
144181 . arg ( "--config" )
145182 . arg ( config_path)
146183 . arg ( "--ip" )
@@ -150,6 +187,8 @@ fn spawn_wrangler_dev(config_path: &PathBuf, port: u16) -> Result<Child, Box<dyn
150187 . arg ( "--log-level" )
151188 . arg ( "error" )
152189 . env ( "WRANGLER_SEND_METRICS" , "false" )
190+ . stdout ( Stdio :: null ( ) )
191+ . stderr ( Stdio :: null ( ) )
153192 . spawn ( ) ?;
154193
155194 Ok ( child)
0 commit comments