@@ -224,6 +224,129 @@ describe("handleChatEvent", () => {
224224 expect ( state . chatRunId ) . toBe ( "run-1" ) ;
225225 } ) ;
226226
227+ it ( "appends gateway deltaText when the cumulative snapshot matches the current prefix" , ( ) => {
228+ const state = createState ( {
229+ sessionKey : "main" ,
230+ chatRunId : "run-1" ,
231+ chatStream : "Live" ,
232+ } ) ;
233+ const payload : ChatEventPayload = {
234+ runId : "run-1" ,
235+ sessionKey : "main" ,
236+ state : "delta" ,
237+ deltaText : " reply" ,
238+ message : {
239+ role : "assistant" ,
240+ content : [ { type : "text" , text : "Live reply" } ] ,
241+ } ,
242+ } ;
243+
244+ expect ( handleChatEvent ( state , payload ) ) . toBe ( "delta" ) ;
245+ expect ( state . chatStream ) . toBe ( "Live reply" ) ;
246+ } ) ;
247+
248+ it ( "uses the cumulative snapshot when the first observed delta joins mid-stream" , ( ) => {
249+ const state = createState ( {
250+ sessionKey : "main" ,
251+ chatRunId : "run-1" ,
252+ chatStream : null ,
253+ } ) ;
254+ const payload : ChatEventPayload = {
255+ runId : "run-1" ,
256+ sessionKey : "main" ,
257+ state : "delta" ,
258+ deltaText : " reply" ,
259+ message : {
260+ role : "assistant" ,
261+ content : [ { type : "text" , text : "Live reply" } ] ,
262+ } ,
263+ } ;
264+
265+ expect ( handleChatEvent ( state , payload ) ) . toBe ( "delta" ) ;
266+ expect ( state . chatStream ) . toBe ( "Live reply" ) ;
267+ } ) ;
268+
269+ it ( "appends gateway deltaText when no full message snapshot is present" , ( ) => {
270+ const state = createState ( {
271+ sessionKey : "main" ,
272+ chatRunId : "run-1" ,
273+ chatStream : "Live" ,
274+ } ) ;
275+ const payload : ChatEventPayload = {
276+ runId : "run-1" ,
277+ sessionKey : "main" ,
278+ state : "delta" ,
279+ deltaText : " reply" ,
280+ } ;
281+
282+ expect ( handleChatEvent ( state , payload ) ) . toBe ( "delta" ) ;
283+ expect ( state . chatStream ) . toBe ( "Live reply" ) ;
284+ } ) ;
285+
286+ it ( "uses the cumulative snapshot when a missed delta would make append stale" , ( ) => {
287+ const state = createState ( {
288+ sessionKey : "main" ,
289+ chatRunId : "run-1" ,
290+ chatStream : "Hello" ,
291+ } ) ;
292+ const payload : ChatEventPayload = {
293+ runId : "run-1" ,
294+ sessionKey : "main" ,
295+ state : "delta" ,
296+ deltaText : "!" ,
297+ message : {
298+ role : "assistant" ,
299+ content : [ { type : "text" , text : "Hello world!" } ] ,
300+ } ,
301+ } ;
302+
303+ expect ( handleChatEvent ( state , payload ) ) . toBe ( "delta" ) ;
304+ expect ( state . chatStream ) . toBe ( "Hello world!" ) ;
305+ } ) ;
306+
307+ it ( "uses the cumulative snapshot when a same-length missed replacement changes the prefix" , ( ) => {
308+ const state = createState ( {
309+ sessionKey : "main" ,
310+ chatRunId : "run-1" ,
311+ chatStream : "AB" ,
312+ } ) ;
313+ const payload : ChatEventPayload = {
314+ runId : "run-1" ,
315+ sessionKey : "main" ,
316+ state : "delta" ,
317+ deltaText : "E" ,
318+ message : {
319+ role : "assistant" ,
320+ content : [ { type : "text" , text : "CDE" } ] ,
321+ } ,
322+ } ;
323+
324+ expect ( handleChatEvent ( state , payload ) ) . toBe ( "delta" ) ;
325+ expect ( state . chatStream ) . toBe ( "CDE" ) ;
326+ } ) ;
327+
328+ it ( "replaces the stream when gateway deltaText marks a replacement" , ( ) => {
329+ const state = createState ( {
330+ sessionKey : "main" ,
331+ chatRunId : "run-1" ,
332+ chatStream : "Alpha beta" ,
333+ } ) ;
334+ const payload : ChatEventPayload = {
335+ runId : "run-1" ,
336+ sessionKey : "main" ,
337+ state : "delta" ,
338+ deltaText : "Alpha" ,
339+ replace : true ,
340+ message : {
341+ role : "assistant" ,
342+ content : [ { type : "text" , text : "ignored snapshot" } ] ,
343+ } ,
344+ } ;
345+
346+ expect ( handleChatEvent ( state , payload ) ) . toBe ( "delta" ) ;
347+ expect ( state . chatStream ) . toBe ( "Alpha" ) ;
348+ } ) ;
349+
227350 it ( "adopts the run id for selected-session live deltas observed from another channel" , ( ) => {
228351 const state = createState ( {
229352 sessionKey : "agent:main:feishu:direct:peer-1" ,
0 commit comments