@@ -85,7 +85,8 @@ vi.mock("./streaming-card.js", () => {
8585 update = vi . fn ( async ( ) => { } ) ;
8686 close = vi . fn ( async ( text ?: string ) => {
8787 this . active = false ;
88- return Boolean ( text ?. trim ( ) ) ;
88+ const visible = Boolean ( text ?. trim ( ) ) ;
89+ return { contentVisible : visible , requestedContentVisible : visible } ;
8990 } ) ;
9091 discard = vi . fn ( async ( ) => {
9192 this . active = false ;
@@ -118,6 +119,10 @@ afterAll(() => {
118119
119120describe ( "createFeishuReplyDispatcher streaming behavior" , ( ) => {
120121 type ReplyDispatcherArgs = Parameters < typeof createFeishuReplyDispatcher > [ 0 ] ;
122+ type RuntimeLoggerStub = ReplyDispatcherArgs [ "runtime" ] & {
123+ log : ReturnType < typeof vi . fn > ;
124+ error : ReturnType < typeof vi . fn > ;
125+ } ;
121126 type TypingDispatcherOptions = {
122127 onReplyStart ?: ( ) => Promise < void > | void ;
123128 onIdle ?: ( ) => Promise < void > | void ;
@@ -220,8 +225,8 @@ describe("createFeishuReplyDispatcher streaming behavior", () => {
220225 return firstMockArg ( createReplyDispatcherWithTypingMock , "reply dispatcher options" ) ;
221226 }
222227
223- function createRuntimeLogger ( ) {
224- return { log : vi . fn ( ) , error : vi . fn ( ) } as never ;
228+ function createRuntimeLogger ( ) : RuntimeLoggerStub {
229+ return { log : vi . fn ( ) , error : vi . fn ( ) } as unknown as RuntimeLoggerStub ;
225230 }
226231
227232 function createDispatcherHarness ( overrides : Partial < ReplyDispatcherArgs > = { } ) {
@@ -1340,6 +1345,9 @@ describe("createFeishuReplyDispatcher streaming behavior", () => {
13401345 expect ( streamingInstances [ 0 ] . close ) . toHaveBeenCalledWith ( "caption from stream" , {
13411346 note : "Agent: agent" ,
13421347 } ) ;
1348+ expect ( streamingInstances [ 0 ] . close . mock . invocationCallOrder [ 0 ] ) . toBeLessThan (
1349+ sendMediaFeishuMock . mock . invocationCallOrder [ 0 ] ,
1350+ ) ;
13431351 expect ( sendMessageFeishuMock ) . not . toHaveBeenCalled ( ) ;
13441352 expect ( sendStructuredCardFeishuMock ) . not . toHaveBeenCalled ( ) ;
13451353 expect ( sendMediaFeishuMock ) . toHaveBeenCalledTimes ( 1 ) ;
@@ -1448,86 +1456,97 @@ describe("createFeishuReplyDispatcher streaming behavior", () => {
14481456 } ) ;
14491457
14501458 it ( "closes streaming final text before sending regular media attachments" , async ( ) => {
1451- const { result, options } = createDispatcherHarness ( {
1452- runtime : createRuntimeLogger ( ) ,
1453- } ) ;
1459+ const { options } = createDispatcherHarness ( ) ;
14541460
14551461 await options . deliver (
1456- {
1457- text : "final report\n\nSee the attached image." ,
1458- mediaUrls : [ "https://example.com/report.png" ] ,
1459- } ,
1462+ { text : "final report" , mediaUrl : "https://example.com/report.png" } ,
14601463 { kind : "final" } ,
14611464 ) ;
14621465
1463- expect ( streamingInstances ) . toHaveLength ( 1 ) ;
1464- expect ( streamingInstances [ 0 ] . close ) . toHaveBeenCalledTimes ( 1 ) ;
1465- expect ( streamingInstances [ 0 ] . close ) . toHaveBeenCalledWith (
1466- "final report\n\nSee the attached image." ,
1467- { note : "Agent: agent" } ,
1466+ expect ( streamingInstances [ 0 ] . close ) . toHaveBeenCalledWith ( "final report" , {
1467+ note : "Agent: agent" ,
1468+ } ) ;
1469+ expect ( streamingInstances [ 0 ] . close . mock . invocationCallOrder [ 0 ] ) . toBeLessThan (
1470+ sendMediaFeishuMock . mock . invocationCallOrder [ 0 ] ,
14681471 ) ;
14691472 expect ( sendMediaFeishuMock ) . toHaveBeenCalledTimes ( 1 ) ;
1470- expectMockArgFields ( sendMediaFeishuMock , "media send params" , {
1471- mediaUrl : "https://example.com/report.png" ,
1472- } ) ;
1473-
1474- await expect ( result . ensureNoVisibleReplyFallback ( "zero-final-count" ) ) . resolves . toBe ( false ) ;
14751473 expect ( sendMessageFeishuMock ) . not . toHaveBeenCalled ( ) ;
1476- expect ( result . getVisibleReplyState ( ) ) . toEqual ( {
1477- visibleReplySent : true ,
1478- skippedFinalReason : null ,
1479- } ) ;
14801474 } ) ;
14811475
1482- it ( "falls back to plain final text when streaming close accepts no content before media" , async ( ) => {
1483- resolveFeishuAccountMock . mockReturnValue ( {
1484- accountId : "main" ,
1485- appId : "app_id" ,
1486- appSecret : "app_secret" ,
1487- domain : "feishu" ,
1488- config : {
1489- renderMode : "card" ,
1490- streaming : true ,
1491- } ,
1492- } ) ;
1493- const { result, options } = createDispatcherHarness ( {
1494- runtime : createRuntimeLogger ( ) ,
1495- } ) ;
1476+ it ( "falls back to plain final text when streaming close misses the requested content" , async ( ) => {
1477+ const { result, options } = createDispatcherHarness ( ) ;
14961478
1497- await options . onReplyStart ?.( ) ;
1479+ result . replyOptions . onPartialReply ?.( { text : "prior preview" } ) ;
14981480 streamingInstances [ 0 ] . close = vi . fn ( async ( ) => {
14991481 streamingInstances [ 0 ] . active = false ;
1500- return false ;
1482+ return { contentVisible : true , requestedContentVisible : false } ;
15011483 } ) ;
15021484
15031485 await options . deliver (
1504- {
1505- text : "final report\n\nSee the attached image." ,
1506- mediaUrls : [ "https://example.com/report.png" ] ,
1507- } ,
1486+ { text : "final report" , mediaUrl : "https://example.com/report.png" } ,
15081487 { kind : "final" } ,
15091488 ) ;
15101489
1511- expect ( streamingInstances [ 0 ] . close ) . toHaveBeenCalledWith (
1512- "final report\n\nSee the attached image." ,
1513- { note : "Agent: agent" } ,
1490+ expectMockArgFields ( sendMessageFeishuMock , "message send params" , { text : "final report" } ) ;
1491+ expect ( streamingInstances [ 0 ] . close . mock . invocationCallOrder [ 0 ] ) . toBeLessThan (
1492+ sendMessageFeishuMock . mock . invocationCallOrder [ 0 ] ,
15141493 ) ;
1515- expectMockArgFields ( sendMessageFeishuMock , "message send params" , {
1516- text : "final report\n\nSee the attached image." ,
1517- } ) ;
1518- expectMockArgFields ( sendMediaFeishuMock , "media send params" , {
1519- mediaUrl : "https://example.com/report.png" ,
1520- } ) ;
15211494 expect ( sendMessageFeishuMock . mock . invocationCallOrder [ 0 ] ) . toBeLessThan (
15221495 sendMediaFeishuMock . mock . invocationCallOrder [ 0 ] ,
15231496 ) ;
15241497
1525- await expect ( result . ensureNoVisibleReplyFallback ( "zero-final-count" ) ) . resolves . toBe ( false ) ;
15261498 expect ( sendMessageFeishuMock ) . toHaveBeenCalledTimes ( 1 ) ;
1527- expect ( result . getVisibleReplyState ( ) ) . toEqual ( {
1528- visibleReplySent : true ,
1529- skippedFinalReason : null ,
1499+ } ) ;
1500+
1501+ it ( "falls back to text and media when streaming close rejects" , async ( ) => {
1502+ const runtime = createRuntimeLogger ( ) ;
1503+ const { result, options } = createDispatcherHarness ( { runtime } ) ;
1504+
1505+ result . replyOptions . onPartialReply ?.( { text : "prior preview" } ) ;
1506+ streamingInstances [ 0 ] . close = vi . fn ( async ( ) => {
1507+ streamingInstances [ 0 ] . active = false ;
1508+ throw new Error ( "close failed" ) ;
1509+ } ) ;
1510+
1511+ await options . deliver (
1512+ { text : "final report" , mediaUrl : "https://example.com/report.png" } ,
1513+ { kind : "final" } ,
1514+ ) ;
1515+
1516+ expect ( streamingInstances [ 0 ] . close . mock . invocationCallOrder [ 0 ] ) . toBeLessThan (
1517+ sendMessageFeishuMock . mock . invocationCallOrder [ 0 ] ,
1518+ ) ;
1519+ expect ( sendMessageFeishuMock . mock . invocationCallOrder [ 0 ] ) . toBeLessThan (
1520+ sendMediaFeishuMock . mock . invocationCallOrder [ 0 ] ,
1521+ ) ;
1522+ expectMockArgFields ( sendMessageFeishuMock , "message send params" , { text : "final report" } ) ;
1523+ expect ( runtime . error ) . toHaveBeenCalledWith ( expect . stringContaining ( "close failed" ) ) ;
1524+ } ) ;
1525+
1526+ it ( "still sends media when the close-false text fallback rejects" , async ( ) => {
1527+ const { result, options } = createDispatcherHarness ( ) ;
1528+
1529+ result . replyOptions . onPartialReply ?.( { text : "prior preview" } ) ;
1530+ streamingInstances [ 0 ] . close = vi . fn ( async ( ) => {
1531+ streamingInstances [ 0 ] . active = false ;
1532+ return { contentVisible : false , requestedContentVisible : false } ;
15301533 } ) ;
1534+ sendMessageFeishuMock . mockRejectedValueOnce ( new Error ( "text failed" ) ) ;
1535+
1536+ await expect (
1537+ options . deliver (
1538+ { text : "final report" , mediaUrl : "https://example.com/report.png" } ,
1539+ { kind : "final" } ,
1540+ ) ,
1541+ ) . rejects . toThrow ( "text failed" ) ;
1542+
1543+ expect ( streamingInstances [ 0 ] . close . mock . invocationCallOrder [ 0 ] ) . toBeLessThan (
1544+ sendMessageFeishuMock . mock . invocationCallOrder [ 0 ] ,
1545+ ) ;
1546+ expect ( sendMessageFeishuMock . mock . invocationCallOrder [ 0 ] ) . toBeLessThan (
1547+ sendMediaFeishuMock . mock . invocationCallOrder [ 0 ] ,
1548+ ) ;
1549+ expect ( sendMediaFeishuMock ) . toHaveBeenCalledTimes ( 1 ) ;
15311550 } ) ;
15321551
15331552 it ( "sends attachments after streaming final markdown replies" , async ( ) => {
@@ -2079,7 +2098,7 @@ describe("createFeishuReplyDispatcher streaming behavior", () => {
20792098 await options . deliver ( { text : "```md\nvisible answer\n```" } , { kind : "final" } ) ;
20802099 streamingInstances [ 0 ] . close = vi . fn ( async ( ) => {
20812100 streamingInstances [ 0 ] . active = false ;
2082- return false ;
2101+ return { contentVisible : false , requestedContentVisible : false } ;
20832102 } ) ;
20842103
20852104 await options . onIdle ?.( ) ;
@@ -2111,7 +2130,7 @@ describe("createFeishuReplyDispatcher streaming behavior", () => {
21112130 releaseClose = resolve ;
21122131 } ) ;
21132132 streamingSession . active = false ;
2114- return true ;
2133+ return { contentVisible : true , requestedContentVisible : true } ;
21152134 } ) ;
21162135 streamingSession . close = closeMock ;
21172136
0 commit comments