@@ -190,6 +190,48 @@ describe("handleQaInbound", () => {
190190 } ) ;
191191 } ) ;
192192
193+ it ( "escapes control characters in dispatch error logs" , async ( ) => {
194+ const runtime = createPluginRuntimeMock ( ) ;
195+ const warn = vi . spyOn ( console , "warn" ) . mockImplementation ( ( ) => undefined ) ;
196+ const c1Control = String . fromCharCode ( 0x9b ) ;
197+ const lineSeparator = String . fromCodePoint ( 0x2028 ) ;
198+ const paragraphSeparator = String . fromCodePoint ( 0x2029 ) ;
199+ vi . mocked ( deleteQaBusMessage ) . mockRejectedValueOnce (
200+ new Error ( `cleanup\nforged\u001b[31m${ c1Control } 32m${ lineSeparator } next` ) ,
201+ ) ;
202+ setQaChannelRuntime ( runtime ) ;
203+
204+ try {
205+ await handleQaInbound ( createQaInboundParams ( ) ) ;
206+
207+ const assembled = firstRunAssembledParams ( runtime ) ;
208+ await assembled . replyOptions ?. onPartialReply ?.( { text : "unfinished preview" } ) ;
209+ assembled . delivery . onError ?.( new Error ( `dispatch\r\nforged${ paragraphSeparator } next` ) , {
210+ kind : "final" ,
211+ } ) ;
212+
213+ await vi . waitFor ( ( ) => {
214+ expect ( warn ) . toHaveBeenCalledTimes ( 2 ) ;
215+ } ) ;
216+ assembled . delivery . onError ?.( undefined , { kind : "final" } ) ;
217+ await vi . waitFor ( ( ) => {
218+ expect ( warn ) . toHaveBeenCalledTimes ( 3 ) ;
219+ } ) ;
220+ const output = warn . mock . calls . flat ( ) . join ( " " ) ;
221+ expect ( output ) . not . toContain ( "\r" ) ;
222+ expect ( output ) . not . toContain ( "\n" ) ;
223+ expect ( output ) . not . toContain ( String . fromCharCode ( 0x1b ) ) ;
224+ expect ( output ) . not . toContain ( c1Control ) ;
225+ expect ( output ) . not . toContain ( lineSeparator ) ;
226+ expect ( output ) . not . toContain ( paragraphSeparator ) ;
227+ expect ( output ) . toContain ( "dispatch\\u000d\\u000aforged\\u2029next" ) ;
228+ expect ( output ) . toContain ( "cleanup\\u000aforged\\u001b[31m\\u009b32m\\u2028next" ) ;
229+ expect ( output ) . toContain ( "[object Undefined]" ) ;
230+ } finally {
231+ warn . mockRestore ( ) ;
232+ }
233+ } ) ;
234+
193235 it ( "marks group messages that match configured mention patterns" , async ( ) => {
194236 const runtime = createPluginRuntimeMock ( ) ;
195237 vi . mocked ( runtime . channel . mentions . buildMentionRegexes ) . mockReturnValue ( [ / \b @ ? o p e n c l a w \b / i] ) ;
0 commit comments