package dingtalk import ( "context" "strings" "testing" ) // smoothAnswer mirrors a live answer shape: prose, a compact table, more prose, // a second table, then a long tail. With the historical modes the streaming // card froze at the first table and the rest arrived in one final frame. func smoothAnswer() string { return "Nova 是新一代工业协作机器人平台。\n\n" + "| Model | Payload | Reach |\n| :--- | :--- | :--- |\n| Nova 3 | 3 kg | 626 mm |\n| Nova 5 | 5 kg | 900 mm |\n\n" + "选型时先看负载与臂展,再看控制柜与接口。\n\n" + "| Interface | Count |\n| :--- | :--- |\n| DI | 16 |\n| DO | 16 |\n\n" + strings.Repeat("最后一段补充说明。", 30) } func streamedFrames(t *testing.T, mode, answer string) (frames, lastPartial int) { t.Helper() h, err := NewHandlerWithOptions(TenantConfig{TenantKey: "t", Channel: "dingtalk"}, &fakeQA{}, &fakeReply{}, HandlerOptions{WideTableGrouping: mode}) if err != nil { t.Fatalf("handler: %v", err) } prev := "" for n := 1; n <= len(answer); n += 7 { // ~model chunk granularity p := h.sanitizeOutboundPartial(context.Background(), answer[:n]) if p != prev { frames++ prev = p } } return frames, len(prev) } // TestSmoothModeKeepsStreamingThroughFormingTable guards the regression where a // half-formed table truncated every streaming frame (stableMarkdownPrefix), so // the card appeared to stop and dumped the remaining bytes at finalize. func TestSmoothModeKeepsStreamingThroughFormingTable(t *testing.T) { answer := smoothAnswer() smoothFrames, smoothLast := streamedFrames(t, "smooth", answer) streamFrames, streamLast := streamedFrames(t, "stream", answer) if smoothFrames <= streamFrames { t.Fatalf("smooth mode must deliver more streaming frames: smooth=%d stream=%d", smoothFrames, streamFrames) } if smoothLast < len(answer)*9/10 { t.Fatalf("smooth streaming must deliver the growing answer: last=%d answer=%d", smoothLast, len(answer)) } if streamLast >= len(answer)*9/10 { t.Fatalf("stream mode is expected to hold the table tail back, got last=%d", streamLast) } } // TestSmoothModeKeepsRealCompactTableOnFinalFrame pins that the fix only // changes streaming frames: a compact table still lands as a real table. func TestSmoothModeKeepsRealCompactTableOnFinalFrame(t *testing.T) { h, err := NewHandlerWithOptions(TenantConfig{TenantKey: "t", Channel: "dingtalk"}, &fakeQA{}, &fakeReply{}, HandlerOptions{WideTableGrouping: "smooth"}) if err != nil { t.Fatalf("handler: %v", err) } final := h.rewriteOutboundContent(context.Background(), smoothAnswer()) if !strings.Contains(final, "| Model | Payload | Reach |") { t.Fatalf("final frame must keep the real compact table, got %q", final) } }