@@ -37,6 +37,7 @@ vi.mock("../../system-agent/greeting.js", () => ({
3737
3838type FakeEngine = {
3939 answerWizard : ReturnType < typeof vi . fn > ;
40+ cancelWizard : ReturnType < typeof vi . fn > ;
4041 handle : ReturnType < typeof vi . fn > ;
4142 seedHistory : ReturnType < typeof vi . fn > ;
4243 historyLength : ReturnType < typeof vi . fn > ;
@@ -53,6 +54,9 @@ function makeEngine(): FakeEngine {
5354 answerWizard : vi . fn ( async ( ) => {
5455 throw new SystemAgentWizardAnswerError ( "No hosted wizard is awaiting an answer." ) ;
5556 } ) ,
57+ cancelWizard : vi . fn ( async ( ) => {
58+ throw new SystemAgentWizardAnswerError ( "No hosted wizard is awaiting cancellation." ) ;
59+ } ) ,
5660 handle : vi . fn ( async ( ) => ( { text : "did the thing" , action : "none" } ) ) ,
5761 seedHistory : vi . fn ( ) ,
5862 historyLength : vi . fn ( ( ) => 0 ) ,
@@ -181,6 +185,11 @@ describe("openclaw.chat session ownership", () => {
181185 attacker ,
182186 ) ;
183187 const reset = await callChat ( context , { sessionId : "owned-session" , reset : true } , attacker ) ;
188+ const cancel = await callChat (
189+ context ,
190+ { sessionId : "owned-session" , wizardCancel : { stepId : "channel" } } ,
191+ attacker ,
192+ ) ;
184193
185194 expect ( turn ) . toMatchObject ( {
186195 ok : false ,
@@ -197,7 +206,15 @@ describe("openclaw.chat session ownership", () => {
197206 payload : undefined ,
198207 error : { code : "INVALID_REQUEST" } ,
199208 } ) ;
209+ expect ( cancel ) . toMatchObject ( {
210+ ok : false ,
211+ payload : undefined ,
212+ error : { code : "INVALID_REQUEST" } ,
213+ } ) ;
200214 expect ( handle ) . not . toHaveBeenCalled ( ) ;
215+ expect (
216+ expectDefined ( createdEngines [ 0 ] , "created system-agent engine" ) . cancelWizard ,
217+ ) . not . toHaveBeenCalled ( ) ;
201218 expect (
202219 expectDefined ( createdEngines [ 0 ] , "created system-agent engine" ) . dispose ,
203220 ) . not . toHaveBeenCalled ( ) ;
@@ -370,6 +387,36 @@ describe("openclaw.chat session responses", () => {
370387 expect ( inferenceFallbackMocks . verifySystemAgentInferenceWithFallback ) . not . toHaveBeenCalled ( ) ;
371388 } ) ;
372389
390+ it ( "rejects a structured cancel without an active chat session" , async ( ) => {
391+ const call = await callChat ( makeContext ( new Map ( ) ) , {
392+ sessionId : "missing" ,
393+ wizardCancel : { stepId : "channel" } ,
394+ } ) ;
395+
396+ expect ( call ) . toMatchObject ( {
397+ ok : false ,
398+ error : {
399+ code : "INVALID_REQUEST" ,
400+ details : { code : "system_agent_session_invalidated" } ,
401+ } ,
402+ } ) ;
403+ expect ( setupInferenceMocks . verifySetupInference ) . not . toHaveBeenCalled ( ) ;
404+ } ) ;
405+
406+ it ( "routes a structured cancel through its bound session" , async ( ) => {
407+ const engine = makeEngine ( ) ;
408+ engine . cancelWizard . mockResolvedValue ( { text : "Setup cancelled." , action : "none" } ) ;
409+ const sessions = new Map < string , SystemAgentChatSession > ( [ [ "s1" , seededSession ( { engine } ) ] ] ) ;
410+
411+ const call = await callChat ( makeContext ( sessions ) , {
412+ sessionId : "s1" ,
413+ wizardCancel : { stepId : "channel" } ,
414+ } ) ;
415+
416+ expect ( engine . cancelWizard ) . toHaveBeenCalledWith ( { stepId : "channel" } ) ;
417+ expect ( call . payload ) . toMatchObject ( { reply : "Setup cancelled." , action : "none" } ) ;
418+ } ) ;
419+
373420 it ( "rejects a structured answer when the active session has no hosted wizard" , async ( ) => {
374421 const engine = makeEngine ( ) ;
375422 const sessions = new Map < string , SystemAgentChatSession > ( [ [ "s1" , seededSession ( { engine } ) ] ] ) ;
0 commit comments