@@ -70,6 +70,38 @@ describe("commitments command", () => {
7070 beforeEach ( ( ) => {
7171 vi . clearAllMocks ( ) ;
7272 mocks . listCommitments . mockResolvedValue ( [ commitment ( ) ] ) ;
73+ mocks . markCommitmentsStatus . mockResolvedValue ( [ "cm_escape" ] ) ;
74+ } ) ;
75+
76+ it ( "sanitizes invalid status values before rendering errors" , async ( ) => {
77+ const { runtime } = createRuntime ( ) ;
78+
79+ await commitmentsListCommand (
80+ { status : "bad\u001b]52;c;Zm9yZ2Vk\u0007\nforged: yes\u001b[31m" } ,
81+ runtime ,
82+ ) ;
83+
84+ expect ( runtime . error ) . toHaveBeenCalledWith (
85+ "Unknown commitment status: bad\\nforged: yes. Use one of: pending, sent, dismissed, snoozed, expired." ,
86+ ) ;
87+ expect ( runtime . exit ) . toHaveBeenCalledWith ( 1 ) ;
88+ expect ( mocks . listCommitments ) . not . toHaveBeenCalled ( ) ;
89+ } ) ;
90+
91+ it ( "sanitizes agent filters and database paths in human-readable output" , async ( ) => {
92+ const unsafeText = "\u001b]52;c;Zm9yZ2Vk\u0007\nforged: yes\u001b[31m" ;
93+ mocks . resolveCommitmentDatabasePath . mockReturnValueOnce ( `/tmp/${ unsafeText } .sqlite` ) ;
94+ mocks . listCommitments . mockResolvedValueOnce ( [ ] ) ;
95+ const { runtime, logs } = createRuntime ( ) ;
96+
97+ await commitmentsListCommand ( { agent : `main${ unsafeText } ` } , runtime ) ;
98+
99+ expect ( logs . map ( stripAnsi ) ) . toContain ( "Store: /tmp/\\nforged: yes.sqlite" ) ;
100+ expect ( logs . map ( stripAnsi ) ) . toContain ( "Agent filter: main\\nforged: yes" ) ;
101+ for ( const line of logs ) {
102+ expect ( line ) . not . toContain ( "\u0007" ) ;
103+ expect ( line ) . not . toContain ( "\n" ) ;
104+ }
73105 } ) ;
74106
75107 it ( "sanitizes untrusted commitment fields in table output" , async ( ) => {
@@ -241,4 +273,80 @@ describe("commitments command", () => {
241273 nowMs : expect . any ( Number ) ,
242274 } ) ;
243275 } ) ;
276+
277+ it ( "reports only actually dismissed ids and explains skipped ids" , async ( ) => {
278+ mocks . markCommitmentsStatus . mockResolvedValueOnce ( [ "cm_valid" ] ) ;
279+ const { runtime, logs } = createRuntime ( ) ;
280+
281+ await commitmentsDismissCommand (
282+ { ids : [ " cm_valid " , "cm_missing" , "cm_valid" , "cm_terminal" ] } ,
283+ runtime ,
284+ ) ;
285+
286+ expect ( mocks . markCommitmentsStatus ) . toHaveBeenCalledWith ( {
287+ cfg : { } ,
288+ ids : [ "cm_valid" , "cm_missing" , "cm_terminal" ] ,
289+ status : "dismissed" ,
290+ nowMs : expect . any ( Number ) ,
291+ } ) ;
292+ expect ( logs . map ( stripAnsi ) ) . toEqual ( [ "Dismissed commitments: cm_valid" ] ) ;
293+ expect ( runtime . error ) . toHaveBeenCalledWith (
294+ "Commitments not found or no longer active: cm_missing, cm_terminal. Run openclaw commitments --all to inspect current state." ,
295+ ) ;
296+ expect ( runtime . exit ) . toHaveBeenCalledWith ( 1 ) ;
297+ } ) ;
298+
299+ it ( "rejects dismissals when no requested commitment is active" , async ( ) => {
300+ mocks . markCommitmentsStatus . mockResolvedValueOnce ( [ ] ) ;
301+ const { runtime, logs } = createRuntime ( ) ;
302+
303+ await commitmentsDismissCommand ( { ids : [ "cm_missing" , "cm_terminal" ] } , runtime ) ;
304+
305+ expect ( logs ) . toEqual ( [ ] ) ;
306+ expect ( runtime . error ) . toHaveBeenCalledWith (
307+ "Commitments not found or no longer active: cm_missing, cm_terminal. Run openclaw commitments --all to inspect current state." ,
308+ ) ;
309+ expect ( runtime . exit ) . toHaveBeenCalledWith ( 1 ) ;
310+ } ) ;
311+
312+ it ( "keeps unsuccessful dismissals truthful and machine-readable" , async ( ) => {
313+ mocks . markCommitmentsStatus . mockResolvedValueOnce ( [ ] ) ;
314+ const { runtime, logs, stdout } = createRuntime ( ) ;
315+
316+ await commitmentsDismissCommand ( { ids : [ "cm_missing" ] , json : true } , runtime ) ;
317+
318+ expect ( logs ) . toEqual ( [ ] ) ;
319+ expect ( stdout ) . toEqual ( [
320+ JSON . stringify ( { dismissed : [ ] , notDismissed : [ "cm_missing" ] } , null , 2 ) ,
321+ ] ) ;
322+ expect ( runtime . exit ) . toHaveBeenCalledWith ( 1 ) ;
323+ } ) ;
324+
325+ it ( "sanitizes dismissed ids in human output while preserving raw JSON" , async ( ) => {
326+ const unsafeId = "cm_\u001b]52;c;Zm9yZ2Vk\u0007\nforged: yes\u001b[31m" ;
327+ mocks . markCommitmentsStatus . mockResolvedValue ( [ unsafeId ] ) ;
328+ const human = createRuntime ( ) ;
329+ const json = createRuntime ( ) ;
330+
331+ await commitmentsDismissCommand ( { ids : [ unsafeId ] } , human . runtime ) ;
332+ await commitmentsDismissCommand ( { ids : [ unsafeId ] , json : true } , json . runtime ) ;
333+
334+ expect ( human . logs . map ( stripAnsi ) ) . toEqual ( [ "Dismissed commitments: cm_\\nforged: yes" ] ) ;
335+ expect ( human . logs [ 0 ] ) . not . toContain ( "\u0007" ) ;
336+ expect ( human . logs [ 0 ] ) . not . toContain ( "\n" ) ;
337+ expect ( JSON . parse ( json . stdout [ 0 ] ?? "{}" ) ) . toStrictEqual ( { dismissed : [ unsafeId ] } ) ;
338+ } ) ;
339+
340+ it ( "sanitizes skipped dismiss ids in human-readable failure output" , async ( ) => {
341+ const unsafeId = "cm_\u001b]52;c;Zm9yZ2Vk\u0007\nforged: yes\u001b[31m" ;
342+ mocks . markCommitmentsStatus . mockResolvedValueOnce ( [ ] ) ;
343+ const { runtime } = createRuntime ( ) ;
344+
345+ await commitmentsDismissCommand ( { ids : [ unsafeId ] } , runtime ) ;
346+
347+ expect ( runtime . error ) . toHaveBeenCalledWith (
348+ "Commitments not found or no longer active: cm_\\nforged: yes. Run openclaw commitments --all to inspect current state." ,
349+ ) ;
350+ expect ( runtime . exit ) . toHaveBeenCalledWith ( 1 ) ;
351+ } ) ;
244352} ) ;
0 commit comments