@@ -3,6 +3,7 @@ import fs from "node:fs/promises";
33import path from "node:path" ;
44import { beforeEach , describe , expect , it , vi } from "vitest" ;
55import * as modelThinkingDefault from "../agents/model-thinking-default.js" ;
6+ import type { SessionEntry } from "../config/sessions.js" ;
67import { runCronIsolatedAgentTurn } from "./isolated-agent.js" ;
78import {
89 makeCfg ,
@@ -21,10 +22,13 @@ import {
2122} from "./isolated-agent.turn-test-helpers.js" ;
2223import { setupRunCronIsolatedAgentTurnSuite } from "./isolated-agent/run.suite-helpers.js" ;
2324import {
25+ dispatchCronDeliveryMock ,
2426 mockRunCronFallbackPassthrough ,
2527 runEmbeddedPiAgentMock ,
2628 updateSessionStoreMock ,
2729} from "./isolated-agent/run.test-harness.js" ;
30+ import { normalizeCronJobCreate } from "./normalize.js" ;
31+ import type { CronJob } from "./types.js" ;
2832
2933setupRunCronIsolatedAgentTurnSuite ( ) ;
3034
@@ -148,7 +152,7 @@ describe("runCronIsolatedAgentTurn session identity", () => {
148152 } ) ;
149153 } ) ;
150154
151- it ( "persists rotated transcript identity for session -bound cron runs" , async ( ) => {
155+ it ( "persists rotated transcript identity for current -bound cron runs" , async ( ) => {
152156 await withTempHome ( async ( home ) => {
153157 const deps = makeDeps ( ) ;
154158 const boundSessionKey = "agent:main:telegram:direct:42" ;
@@ -177,34 +181,35 @@ describe("runCronIsolatedAgentTurn session identity", () => {
177181 } ,
178182 } ,
179183 } ) ;
180- updateSessionStoreMock . mockImplementation ( async ( _storePath , update ) => {
181- const store = {
182- [ boundSessionKey ] : {
183- sessionId : "bound-session" ,
184- sessionFile : originalSessionFile ,
185- updatedAt : Date . now ( ) ,
186- lastInteractionAt : Date . now ( ) - 1_000 ,
187- systemSent : true ,
188- } ,
189- } ;
184+ updateSessionStoreMock . mockImplementation ( async ( targetStorePath , update ) => {
185+ const raw = await fs . readFile ( targetStorePath , "utf-8" ) ;
186+ const store = JSON . parse ( raw ) as Record < string , SessionEntry > ;
190187 update ( store ) ;
188+ await fs . writeFile ( targetStorePath , JSON . stringify ( store , null , 2 ) , "utf-8" ) ;
191189 } ) ;
190+ const currentBoundJob = normalizeCronJobCreate (
191+ {
192+ ...makeJob ( DEFAULT_AGENT_TURN_PAYLOAD ) ,
193+ sessionTarget : "current" ,
194+ delivery : { mode : "none" } ,
195+ } ,
196+ { sessionContext : { sessionKey : boundSessionKey } } ,
197+ ) as CronJob ;
192198
193199 const res = await runCronIsolatedAgentTurn ( {
194200 cfg : makeCfg ( home , storePath ) ,
195201 deps,
196- job : {
197- ...makeJob ( DEFAULT_AGENT_TURN_PAYLOAD ) ,
198- sessionTarget : `session:${ boundSessionKey } ` ,
199- delivery : { mode : "none" } ,
200- } ,
202+ job : currentBoundJob ,
201203 message : DEFAULT_MESSAGE ,
202204 sessionKey : boundSessionKey ,
203205 lane : "cron" ,
204206 } ) ;
205207
206208 expect ( res . status ) . toBe ( "ok" ) ;
207209 expect ( res . sessionId ) . toBe ( "bound-session-rotated" ) ;
210+ expect ( dispatchCronDeliveryMock . mock . calls . at ( - 1 ) ?. [ 0 ] ) . toEqual (
211+ expect . objectContaining ( { sessionId : "bound-session-rotated" } ) ,
212+ ) ;
208213
209214 const finalPersist = updateSessionStoreMock . mock . calls . at ( - 1 ) ;
210215 expect ( finalPersist ?. [ 0 ] ) . toBe ( storePath ) ;
@@ -218,6 +223,13 @@ describe("runCronIsolatedAgentTurn session identity", () => {
218223 usageFamilySessionIds : [ "bound-session" , "bound-session-rotated" ] ,
219224 } ) ,
220225 ) ;
226+
227+ await expect ( readSessionEntry ( storePath , boundSessionKey ) ) . resolves . toEqual (
228+ expect . objectContaining ( {
229+ sessionId : "bound-session-rotated" ,
230+ sessionFile : rotatedSessionFile ,
231+ } ) ,
232+ ) ;
221233 } ) ;
222234 } ) ;
223235
0 commit comments