@@ -4,6 +4,8 @@ import { expect, it } from "@effect/vitest";
44import * as Effect from "effect/Effect" ;
55import * as Layer from "effect/Layer" ;
66
7+ import type { DeepPartial } from "@t3tools/shared/Struct" ;
8+
79import {
810 OrchestrationEngineService ,
911 type OrchestrationEngineShape ,
@@ -13,6 +15,8 @@ import {
1315 type ProjectionSnapshotQueryShape ,
1416} from "../Services/ProjectionSnapshotQuery.ts" ;
1517import { TaskScheduler } from "../Services/TaskScheduler.ts" ;
18+ import { ServerSettingsService } from "../../serverSettings.ts" ;
19+ import type { ServerSettings } from "@t3tools/contracts" ;
1620import { makeTaskSchedulerLive } from "./TaskScheduler.ts" ;
1721
1822function makeDueTask ( input : {
@@ -40,53 +44,79 @@ const state = {
4044 dispatched : [ ] as Array < { type : string ; commandId : CommandId ; dueAt : string } > ,
4145} ;
4246
43- const schedulerLayer = makeTaskSchedulerLive ( { tickIntervalMs : 60_000 } ) . pipe (
44- Layer . provideMerge (
45- Layer . succeed ( ProjectionSnapshotQuery , {
46- listDueTasks : ( ) => Effect . succeed ( state . dueTasks ) ,
47- } as unknown as ProjectionSnapshotQueryShape ) ,
48- ) ,
49- Layer . provideMerge (
50- Layer . succeed ( OrchestrationEngineService , {
51- dispatch : ( command : { type : string ; commandId : CommandId ; dueAt ?: string } ) =>
52- Effect . sync ( ( ) => {
53- state . dispatched . push ( command as { type : string ; commandId : CommandId ; dueAt : string } ) ;
54- return { sequence : 1 } ;
55- } ) ,
56- } as unknown as OrchestrationEngineShape ) ,
57- ) ,
58- ) ;
59-
60- it . layer ( schedulerLayer . pipe ( Layer . provideMerge ( NodeServices . layer ) ) ) ( "task scheduler" , ( it ) => {
61- it . effect ( "dispatches a deterministic task.fire per due slot" , ( ) =>
62- Effect . gen ( function * ( ) {
63- state . dispatched = [ ] ;
64- state . dueTasks = [
65- makeDueTask ( { taskId : "task-a" , nextFireAt : "2026-01-01T00:00:00.000Z" } ) ,
66- makeDueTask ( { taskId : "task-b" , nextFireAt : "2026-01-01T00:05:00.000Z" } ) ,
67- ] ;
68-
69- const scheduler = yield * TaskScheduler ;
70- const firedCount = yield * scheduler . tick ( ) ;
47+ const fakeProjectionLayer = Layer . succeed ( ProjectionSnapshotQuery , {
48+ listDueTasks : ( ) => Effect . succeed ( state . dueTasks ) ,
49+ } as unknown as ProjectionSnapshotQueryShape ) ;
7150
72- expect ( firedCount ) . toBe ( 2 ) ;
73- expect ( state . dispatched ) . toHaveLength ( 2 ) ;
74- // Deterministic ids keyed by the due slot so crash-retries collapse
75- // into the engine's idempotent command receipts.
76- expect ( state . dispatched [ 0 ] ?. type ) . toBe ( "task.fire" ) ;
77- expect ( state . dispatched [ 0 ] ?. commandId ) . toBe (
78- CommandId . make ( "server:task-fire:task-a:2026-01-01T00:00:00.000Z" ) ,
79- ) ;
80- expect ( state . dispatched [ 1 ] ?. commandId ) . toBe (
81- CommandId . make ( "server:task-fire:task-b:2026-01-01T00:05:00.000Z" ) ,
82- ) ;
51+ const fakeEngineLayer = Layer . succeed ( OrchestrationEngineService , {
52+ dispatch : ( command : { type : string ; commandId : CommandId ; dueAt ?: string } ) =>
53+ Effect . sync ( ( ) => {
54+ state . dispatched . push ( command as { type : string ; commandId : CommandId ; dueAt : string } ) ;
55+ return { sequence : 1 } ;
8356 } ) ,
57+ } as unknown as OrchestrationEngineShape ) ;
58+
59+ const schedulerLayerWithSettings = ( settingsOverrides ?: DeepPartial < ServerSettings > ) =>
60+ makeTaskSchedulerLive ( { tickIntervalMs : 60_000 } ) . pipe (
61+ Layer . provideMerge ( ServerSettingsService . layerTest ( settingsOverrides ) ) ,
62+ Layer . provideMerge ( fakeProjectionLayer ) ,
63+ Layer . provideMerge ( fakeEngineLayer ) ,
8464 ) ;
8565
86- it . effect ( "returns zero and dispatches nothing when no task is due" , ( ) =>
66+ it . layer ( schedulerLayerWithSettings ( ) . pipe ( Layer . provideMerge ( NodeServices . layer ) ) ) (
67+ "task scheduler" ,
68+ ( it ) => {
69+ it . effect ( "dispatches a deterministic task.fire per due slot" , ( ) =>
70+ Effect . gen ( function * ( ) {
71+ state . dispatched = [ ] ;
72+ state . dueTasks = [
73+ makeDueTask ( { taskId : "task-a" , nextFireAt : "2026-01-01T00:00:00.000Z" } ) ,
74+ makeDueTask ( { taskId : "task-b" , nextFireAt : "2026-01-01T00:05:00.000Z" } ) ,
75+ ] ;
76+
77+ const scheduler = yield * TaskScheduler ;
78+ const firedCount = yield * scheduler . tick ( ) ;
79+
80+ expect ( firedCount ) . toBe ( 2 ) ;
81+ expect ( state . dispatched ) . toHaveLength ( 2 ) ;
82+ // Deterministic ids keyed by the due slot so crash-retries collapse
83+ // into the engine's idempotent command receipts.
84+ expect ( state . dispatched [ 0 ] ?. type ) . toBe ( "task.fire" ) ;
85+ expect ( state . dispatched [ 0 ] ?. commandId ) . toBe (
86+ CommandId . make ( "server:task-fire:task-a:2026-01-01T00:00:00.000Z" ) ,
87+ ) ;
88+ expect ( state . dispatched [ 1 ] ?. commandId ) . toBe (
89+ CommandId . make ( "server:task-fire:task-b:2026-01-01T00:05:00.000Z" ) ,
90+ ) ;
91+ } ) ,
92+ ) ;
93+
94+ it . effect ( "returns zero and dispatches nothing when no task is due" , ( ) =>
95+ Effect . gen ( function * ( ) {
96+ state . dispatched = [ ] ;
97+ state . dueTasks = [ ] ;
98+
99+ const scheduler = yield * TaskScheduler ;
100+ const firedCount = yield * scheduler . tick ( ) ;
101+
102+ expect ( firedCount ) . toBe ( 0 ) ;
103+ expect ( state . dispatched ) . toHaveLength ( 0 ) ;
104+ } ) ,
105+ ) ;
106+ } ,
107+ ) ;
108+
109+ it . layer (
110+ schedulerLayerWithSettings ( { enableScheduledTasks : false } ) . pipe (
111+ Layer . provideMerge ( NodeServices . layer ) ,
112+ ) ,
113+ ) ( "task scheduler with the feature switch off" , ( it ) => {
114+ it . effect ( "skips the due scan and dispatches nothing" , ( ) =>
87115 Effect . gen ( function * ( ) {
88116 state . dispatched = [ ] ;
89- state . dueTasks = [ ] ;
117+ state . dueTasks = [
118+ makeDueTask ( { taskId : "task-off" , nextFireAt : "2026-01-01T00:00:00.000Z" } ) ,
119+ ] ;
90120
91121 const scheduler = yield * TaskScheduler ;
92122 const firedCount = yield * scheduler . tick ( ) ;
0 commit comments