11import assert from "assert" ;
22import fs from "fs/promises" ;
3+
34import path from "path" ;
45import { CorePlugin , Request , Response , Scheduler } from "@miniflare/core" ;
56import {
67 Compatibility ,
78 NoOpLog ,
89 PluginContext ,
10+ RequestContext ,
911 STRING_SCRIPT_PATH ,
1012} from "@miniflare/shared" ;
1113import {
@@ -43,6 +45,8 @@ test("CorePlugin: parses options from argv", (t) => {
4345 "fetch_refuses_unknown_protocols" ,
4446 "--compat-flag" ,
4547 "durable_object_fetch_allows_relative_url" ,
48+ "--usage-model" ,
49+ "unbound" ,
4650 "--upstream" ,
4751 "https://github.com/mrbbot" ,
4852 "--watch" ,
@@ -63,6 +67,7 @@ test("CorePlugin: parses options from argv", (t) => {
6367 "--global-async-io" ,
6468 "--global-timers" ,
6569 "--global-random" ,
70+ "--actual-time" ,
6671 ] ) ;
6772 t . deepEqual ( options , {
6873 scriptPath : "script.js" ,
@@ -79,6 +84,7 @@ test("CorePlugin: parses options from argv", (t) => {
7984 "fetch_refuses_unknown_protocols" ,
8085 "durable_object_fetch_allows_relative_url" ,
8186 ] ,
87+ usageModel : "unbound" ,
8288 upstream : "https://github.com/mrbbot" ,
8389 watch : true ,
8490 debug : true ,
@@ -105,6 +111,7 @@ test("CorePlugin: parses options from argv", (t) => {
105111 globalAsyncIO : true ,
106112 globalTimers : true ,
107113 globalRandom : true ,
114+ actualTime : true ,
108115 } ) ;
109116 options = parsePluginArgv ( CorePlugin , [
110117 "-c" ,
@@ -158,6 +165,7 @@ test("CorePlugin: parses options from wrangler config", async (t) => {
158165 global_async_io : true ,
159166 global_timers : true ,
160167 global_random : true ,
168+ actual_time : true ,
161169 } ,
162170 } ,
163171 configDir
@@ -212,6 +220,7 @@ test("CorePlugin: parses options from wrangler config", async (t) => {
212220 globalAsyncIO : true ,
213221 globalTimers : true ,
214222 globalRandom : true ,
223+ actualTime : true ,
215224 } ) ;
216225 // Check build upload dir defaults to dist
217226 options = parsePluginWranglerConfig (
@@ -246,6 +255,7 @@ test("CorePlugin: logs options", (t) => {
246255 globalAsyncIO : true ,
247256 globalTimers : true ,
248257 globalRandom : true ,
258+ actualTime : true ,
249259 } ) ;
250260 t . deepEqual ( logs , [
251261 // script is OptionType.NONE so omitted
@@ -267,6 +277,7 @@ test("CorePlugin: logs options", (t) => {
267277 "Allow Global Async I/O: true" ,
268278 "Allow Global Timers: true" ,
269279 "Allow Global Secure Random: true" ,
280+ "Actual Time: true" ,
270281 ] ) ;
271282 // Check logs default wrangler config/package paths
272283 logs = logPluginOptions ( CorePlugin , {
@@ -511,6 +522,23 @@ test("CorePlugin: setup: includes navigator only if compatibility flag enabled",
511522 globals = ( await plugin . setup ( ) ) . globals ;
512523 t . is ( globals ?. navigator . userAgent , "Cloudflare-Workers" ) ;
513524} ) ;
525+ test ( "CorePlugin: setup: uses actual time if option enabled" , async ( t ) => {
526+ let plugin = new CorePlugin ( ctx ) ;
527+ let DateImpl : typeof Date = ( await plugin . setup ( ) ) . globals ?. Date ;
528+ await new RequestContext ( ) . runWith ( async ( ) => {
529+ const previous = DateImpl . now ( ) ;
530+ await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) ) ;
531+ t . is ( DateImpl . now ( ) , previous ) ;
532+ } ) ;
533+
534+ plugin = new CorePlugin ( ctx , { actualTime : true } ) ;
535+ DateImpl = ( await plugin . setup ( ) ) . globals ?. Date ;
536+ await new RequestContext ( ) . runWith ( async ( ) => {
537+ const previous = DateImpl . now ( ) ;
538+ await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) ) ;
539+ t . not ( DateImpl . now ( ) , previous ) ;
540+ } ) ;
541+ } ) ;
514542
515543test ( "CorePlugin: setup: structuredClone: creates deep-copy of value" , async ( t ) => {
516544 const plugin = new CorePlugin ( ctx ) ;
0 commit comments