@@ -22,6 +22,7 @@ import * as DateTime from "effect/DateTime";
2222import * as Effect from "effect/Effect" ;
2323import * as Layer from "effect/Layer" ;
2424import * as Result from "effect/Result" ;
25+ import * as Stream from "effect/Stream" ;
2526import * as TestClock from "effect/testing/TestClock" ;
2627import * as SqlClient from "effect/sql/SqlClient" ;
2728import * as SqlitePersistence from "../persistence/Sqlite.ts" ;
@@ -1100,3 +1101,80 @@ it.effect("replays a thread.create receipt without changing the thread", () =>
11001101 assert . equal ( yield * orchestrator . getThreadEventSequence ( threadId ) , original . sequence ) ;
11011102 } ) . pipe ( Effect . provide ( layerTest ) ) ,
11021103) ;
1104+
1105+ it . effect ( "explicit settle clears snooze atomically and stays idempotent after re-snoozing" , ( ) =>
1106+ Effect . gen ( function * ( ) {
1107+ const orchestrator = yield * Orchestrator . OrchestratorV2 ;
1108+ const projections = yield * ProjectionStore . ProjectionStoreV2 ;
1109+ const events = yield * EventSink . EventSinkV2 ;
1110+ const threadId = ThreadId . make ( "thread:settle-snoozed" ) ;
1111+ yield * orchestrator . dispatch ( {
1112+ type : "thread.create" ,
1113+ commandId : CommandId . make ( "create-settle-snoozed" ) ,
1114+ threadId,
1115+ projectId : ProjectId . make ( "project:settle-snoozed" ) ,
1116+ title : "Snoozed thread" ,
1117+ modelSelection,
1118+ runtimeMode : "full-access" ,
1119+ interactionMode : "default" ,
1120+ branch : null ,
1121+ worktreePath : null ,
1122+ createdBy : "user" ,
1123+ creationSource : "web" ,
1124+ } ) ;
1125+ const now = yield * DateTime . now ;
1126+ const settledAt = DateTime . add ( now , { seconds : 1 } ) ;
1127+ const snoozedUntil = DateTime . add ( now , { hours : 1 } ) ;
1128+ for ( const round of [ 1 , 2 ] ) {
1129+ yield * orchestrator . dispatch ( {
1130+ type : "thread.snooze" ,
1131+ commandId : CommandId . make ( `snooze-before-settle-${ round } ` ) ,
1132+ threadId,
1133+ snoozedUntil : DateTime . formatIso ( snoozedUntil ) ,
1134+ } ) ;
1135+ assert . deepInclude ( yield * projections . getThread ( threadId ) , {
1136+ snoozedUntil,
1137+ snoozedAt : yield * DateTime . now ,
1138+ } ) ;
1139+ yield * TestClock . adjust ( "1 second" ) ;
1140+ const commandId = CommandId . make ( `settle-snoozed-${ round } ` ) ;
1141+ yield * orchestrator . dispatch ( { type : "thread.settle" , commandId, threadId } ) ;
1142+ const expected = {
1143+ settledOverride : "settled" ,
1144+ settledAt,
1145+ snoozedUntil : null ,
1146+ snoozedAt : null ,
1147+ updatedAt : yield * DateTime . now ,
1148+ } as const ;
1149+ const committed = yield * events . readByCommandId ( { commandId } ) . pipe ( Stream . runCollect ) ;
1150+ assert . lengthOf ( committed , 1 ) ;
1151+ assert . equal ( committed [ 0 ] ?. event . type , "thread.settled" ) ;
1152+ assert . deepInclude ( committed [ 0 ] ?. event . payload , expected ) ;
1153+ const settled = yield * projections . getThreadShell ( threadId ) ;
1154+ assert . deepInclude ( settled , expected ) ;
1155+ assert . deepInclude ( ( yield * projections . getThreadProjection ( threadId ) ) . thread , expected ) ;
1156+
1157+ yield * TestClock . adjust ( "1 second" ) ;
1158+ yield * orchestrator . dispatch ( {
1159+ type : "thread.settle" ,
1160+ commandId : CommandId . make ( `settle-again-${ round } ` ) ,
1161+ threadId,
1162+ } ) ;
1163+ assert . deepEqual ( yield * projections . getThreadShell ( threadId ) , settled ) ;
1164+ }
1165+ yield * orchestrator . dispatch ( {
1166+ type : "thread.unsettle" ,
1167+ commandId : CommandId . make ( "unsettle-after-snooze" ) ,
1168+ threadId,
1169+ reason : "user" ,
1170+ } ) ;
1171+ const active = {
1172+ settledOverride : "active" ,
1173+ settledAt : null ,
1174+ snoozedUntil : null ,
1175+ snoozedAt : null ,
1176+ } as const ;
1177+ assert . deepInclude ( yield * projections . getThreadShell ( threadId ) , active ) ;
1178+ assert . deepInclude ( ( yield * projections . getThreadProjection ( threadId ) ) . thread , active ) ;
1179+ } ) . pipe ( Effect . provide ( layerTest ) ) ,
1180+ ) ;
0 commit comments