@@ -22,6 +22,31 @@ import {
2222
2323setupRunCronIsolatedAgentTurnSuite ( ) ;
2424
25+ function lastEmbeddedAgentCall ( ) : {
26+ agentDir ?: string ;
27+ prompt ?: string ;
28+ sessionKey ?: string ;
29+ workspaceDir ?: string ;
30+ sessionFile ?: string ;
31+ } {
32+ const calls = runEmbeddedPiAgentMock . mock . calls ;
33+ const call = calls [ calls . length - 1 ] ;
34+ if ( ! call ) {
35+ throw new Error ( "expected runEmbeddedPiAgent call" ) ;
36+ }
37+ const value = call [ 0 ] ;
38+ if ( ! value || typeof value !== "object" || Array . isArray ( value ) ) {
39+ throw new Error ( "expected runEmbeddedPiAgent call payload" ) ;
40+ }
41+ return value as {
42+ agentDir ?: string ;
43+ prompt ?: string ;
44+ sessionKey ?: string ;
45+ workspaceDir ?: string ;
46+ sessionFile ?: string ;
47+ } ;
48+ }
49+
2550describe ( "runCronIsolatedAgentTurn session identity" , ( ) => {
2651 beforeEach ( ( ) => {
2752 vi . spyOn ( modelThinkingDefault , "resolveThinkingDefault" ) . mockReturnValue ( "off" ) ;
@@ -36,10 +61,8 @@ describe("runCronIsolatedAgentTurn session identity", () => {
3661 } ) ;
3762
3863 expect ( res . status ) . toBe ( "ok" ) ;
39- const call = runEmbeddedPiAgentMock . mock . calls . at ( - 1 ) ?. [ 0 ] as {
40- agentDir ?: string ;
41- } ;
42- expect ( call ?. agentDir ) . toBe ( path . join ( home , ".openclaw" , "agents" , "main" , "agent" ) ) ;
64+ const call = lastEmbeddedAgentCall ( ) ;
65+ expect ( call . agentDir ) . toBe ( path . join ( home , ".openclaw" , "agents" , "main" , "agent" ) ) ;
4366 } ) ;
4467 } ) ;
4568
@@ -49,10 +72,8 @@ describe("runCronIsolatedAgentTurn session identity", () => {
4972 jobPayload : DEFAULT_AGENT_TURN_PAYLOAD ,
5073 } ) ;
5174
52- const call = runEmbeddedPiAgentMock . mock . calls . at ( - 1 ) ?. [ 0 ] as {
53- prompt ?: string ;
54- } ;
55- const lines = call ?. prompt ?. split ( "\n" ) ?? [ ] ;
75+ const call = lastEmbeddedAgentCall ( ) ;
76+ const lines = ( call . prompt ?? "" ) . split ( "\n" ) ;
5677 expect ( lines [ 0 ] ) . toContain ( "[cron:job-1" ) ;
5778 expect ( lines [ 0 ] ) . toContain ( "do it" ) ;
5879 expect ( lines [ 1 ] ) . toMatch ( / ^ C u r r e n t t i m e : .+ \( .+ \) $ / ) ;
@@ -98,14 +119,10 @@ describe("runCronIsolatedAgentTurn session identity", () => {
98119 } ) ;
99120
100121 expect ( res . status ) . toBe ( "ok" ) ;
101- const call = runEmbeddedPiAgentMock . mock . calls . at ( - 1 ) ?. [ 0 ] as {
102- sessionKey ?: string ;
103- workspaceDir ?: string ;
104- sessionFile ?: string ;
105- } ;
106- expect ( call ?. sessionKey ) . toMatch ( / ^ a g e n t : o p s : c r o n : j o b - o p s : r u n : / ) ;
107- expect ( call ?. workspaceDir ) . toBe ( opsWorkspace ) ;
108- expect ( call ?. sessionFile ) . toContain ( path . join ( "agents" , "ops" ) ) ;
122+ const call = lastEmbeddedAgentCall ( ) ;
123+ expect ( call . sessionKey ) . toMatch ( / ^ a g e n t : o p s : c r o n : j o b - o p s : r u n : / ) ;
124+ expect ( call . workspaceDir ) . toBe ( opsWorkspace ) ;
125+ expect ( call . sessionFile ) . toContain ( path . join ( "agents" , "ops" ) ) ;
109126 } ) ;
110127 } ) ;
111128
@@ -114,14 +131,12 @@ describe("runCronIsolatedAgentTurn session identity", () => {
114131 await runCronTurn ( home , {
115132 jobPayload : DEFAULT_AGENT_TURN_PAYLOAD ,
116133 } ) ;
117- const call = runEmbeddedPiAgentMock . mock . calls . at ( - 1 ) ?. [ 0 ] as {
118- sessionFile ?: string ;
119- } ;
134+ const call = lastEmbeddedAgentCall ( ) ;
120135
121- expect ( call ? .sessionFile ) . toContain (
136+ expect ( call . sessionFile ) . toContain (
122137 path . join ( home , ".openclaw" , "agents" , "main" , "sessions" ) ,
123138 ) ;
124- expect ( call ? .sessionFile ? .endsWith ( ".jsonl" ) ) . toBe ( true ) ;
139+ expect ( String ( call . sessionFile ) . endsWith ( ".jsonl" ) ) . toBe ( true ) ;
125140 } ) ;
126141 } ) ;
127142
0 commit comments