Skip to content

Commit bef9d7f

Browse files
committed
test(feishu): add table-limit routing regression tests
Add focused tests for the parser-backed table counting and the guards at every static-card decision point. presentation-card.test.ts: - withinCardTableLimit covers piped tables, pipe-less GFM tables, alignment-colon delimiters, fenced code exclusion, and thematic breaks / plain pipes in prose at the 5-table boundary - feishuCardWithinTableLimit sums tables across card markdown elements and enforces the 5-table ceiling outbound.test.ts: - 5 tables with auto mode route to structured card - 6 tables fall back to post mode (auto, explicit card, and pipe-less GFM tables) - tables inside code blocks are excluded from the count - 6 tables with an action-bearing presentation refuse the generated card and deliver via the post path; 5 tables still build the card - a presentation text block embedding 6 tables refuses the card channel.test.ts: - message-action send with 6-table text (fallback and text-block forms) refuses the generated card and routes to the sendPayload fallback reply-dispatcher.test.ts: - 5 tables with streaming off route to static card - 6 tables fall back to post mode (auto and explicit card) - over-limit streaming content that was never accepted recovers through the post path instead of a rejected static card Refs: #43690
1 parent 063938e commit bef9d7f

4 files changed

Lines changed: 386 additions & 0 deletions

File tree

‎extensions/feishu/src/channel.test.ts‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,81 @@ describe("feishuPlugin actions", () => {
669669
expect(details.chatId).toBe("oc_group_1");
670670
});
671671

672+
it("falls back to text delivery when presentation text exceeds the card table limit", async () => {
673+
feishuOutboundSendPayloadMock.mockResolvedValueOnce({
674+
channel: "feishu",
675+
messageId: "om_fallback",
676+
chatId: "oc_group_1",
677+
});
678+
679+
const sixTables = Array.from(
680+
{ length: 6 },
681+
(_, i) => `| a${i} | b${i} |\n| - | - |\n| 1 | 2 |`,
682+
).join("\n\n");
683+
684+
const result = await feishuPlugin.actions?.handleAction?.({
685+
action: "send",
686+
params: {
687+
to: "chat:oc_group_1",
688+
message: sixTables,
689+
presentation: {
690+
title: "Status",
691+
blocks: [{ type: "text", text: "Build completed" }],
692+
},
693+
},
694+
cfg,
695+
accountId: undefined,
696+
toolContext: {},
697+
} as never);
698+
699+
// The generated card would embed 6 tables and hit Feishu ErrCode 11310, so it
700+
// must be rejected and routed to the sendPayload text fallback instead.
701+
expect(sendCardFeishuMock).not.toHaveBeenCalled();
702+
expect(feishuOutboundSendPayloadMock).toHaveBeenCalledTimes(1);
703+
const payloadArgs = requireRecord(
704+
mockCallArg(feishuOutboundSendPayloadMock, 0, 0, "feishuOutbound.sendPayload"),
705+
"sendPayload args",
706+
);
707+
expect(payloadArgs.to).toBe("chat:oc_group_1");
708+
expect(payloadArgs.text).toBe(sixTables);
709+
const details = resultDetails(result);
710+
expect(details.ok).toBe(true);
711+
expect(details.messageId).toBe("om_fallback");
712+
});
713+
714+
it("falls back when a presentation text block embeds 6 tables", async () => {
715+
feishuOutboundSendPayloadMock.mockResolvedValueOnce({
716+
channel: "feishu",
717+
messageId: "om_fallback",
718+
chatId: "oc_group_1",
719+
});
720+
721+
const sixTables = Array.from(
722+
{ length: 6 },
723+
(_, i) => `| a${i} | b${i} |\n| - | - |\n| 1 | 2 |`,
724+
).join("\n\n");
725+
726+
const result = await feishuPlugin.actions?.handleAction?.({
727+
action: "send",
728+
params: {
729+
to: "chat:oc_group_1",
730+
presentation: {
731+
title: "Report",
732+
blocks: [{ type: "text", text: sixTables }],
733+
},
734+
},
735+
cfg,
736+
accountId: undefined,
737+
toolContext: {},
738+
} as never);
739+
740+
expect(sendCardFeishuMock).not.toHaveBeenCalled();
741+
expect(feishuOutboundSendPayloadMock).toHaveBeenCalledTimes(1);
742+
const details = resultDetails(result);
743+
expect(details.ok).toBe(true);
744+
expect(details.messageId).toBe("om_fallback");
745+
});
746+
672747
it("hides prefixed native-card JSON in oversized presentation fallbacks", async () => {
673748
feishuOutboundSendPayloadMock.mockResolvedValueOnce({
674749
channel: "feishu",

‎extensions/feishu/src/outbound.test.ts‎

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3126,4 +3126,155 @@ describe("feishuOutbound.sendMedia renderMode", () => {
31263126
expect(sendMessageCall()?.accountId).toBe("main");
31273127
});
31283128
});
3129+
3130+
describe("feishuOutbound table-limit routing", () => {
3131+
beforeEach(() => {
3132+
resetOutboundMocks();
3133+
});
3134+
3135+
function makeTableText(count: number): string {
3136+
return Array.from({ length: count }, (_, i) => `| a${i} | b${i} |\n| - | - |\n| 1 | 2 |`).join(
3137+
"\n\n",
3138+
);
3139+
}
3140+
3141+
it("routes 5 markdown tables to structured card when renderMode=auto", async () => {
3142+
const text = makeTableText(5);
3143+
await sendText({
3144+
cfg: emptyConfig,
3145+
to: "chat_1",
3146+
text,
3147+
accountId: "main",
3148+
});
3149+
3150+
expect(sendStructuredCardFeishuMock).toHaveBeenCalledWith(expect.objectContaining({ text }));
3151+
expect(sendMessageFeishuMock).not.toHaveBeenCalled();
3152+
});
3153+
3154+
it("falls back to post mode for 6 markdown tables when renderMode=auto", async () => {
3155+
const text = makeTableText(6);
3156+
await sendText({
3157+
cfg: emptyConfig,
3158+
to: "chat_1",
3159+
text,
3160+
accountId: "main",
3161+
});
3162+
3163+
expect(sendMessageFeishuMock).toHaveBeenCalled();
3164+
expect(sendStructuredCardFeishuMock).not.toHaveBeenCalled();
3165+
});
3166+
3167+
it("falls back to post mode for 6 tables even with explicit renderMode=card", async () => {
3168+
const text = makeTableText(6);
3169+
await sendText({
3170+
cfg: cardRenderConfig,
3171+
to: "chat_1",
3172+
text,
3173+
accountId: "main",
3174+
});
3175+
3176+
expect(sendMessageFeishuMock).toHaveBeenCalled();
3177+
expect(sendStructuredCardFeishuMock).not.toHaveBeenCalled();
3178+
});
3179+
3180+
it("excludes tables inside code blocks from the table count", async () => {
3181+
const text = "```\n| a | b |\n| - | - |\n| 1 | 2 |\n```\n\n" + makeTableText(5);
3182+
await sendText({
3183+
cfg: emptyConfig,
3184+
to: "chat_1",
3185+
text,
3186+
accountId: "main",
3187+
});
3188+
3189+
expect(sendStructuredCardFeishuMock).toHaveBeenCalledWith(expect.objectContaining({ text }));
3190+
expect(sendMessageFeishuMock).not.toHaveBeenCalled();
3191+
});
3192+
3193+
it("falls back to post mode for 6 pipeless GFM tables", async () => {
3194+
const text = Array.from({ length: 6 }, (_, i) => `a${i} | b${i}\n--- | ---\n1 | 2`).join(
3195+
"\n\n",
3196+
);
3197+
await sendText({
3198+
cfg: emptyConfig,
3199+
to: "chat_1",
3200+
text,
3201+
accountId: "main",
3202+
});
3203+
3204+
expect(sendMessageFeishuMock).toHaveBeenCalled();
3205+
expect(sendStructuredCardFeishuMock).not.toHaveBeenCalled();
3206+
});
3207+
});
3208+
3209+
describe("feishuOutbound presentation card table-limit", () => {
3210+
beforeEach(() => {
3211+
resetOutboundMocks();
3212+
});
3213+
3214+
function makeTableText(count: number): string {
3215+
return Array.from({ length: count }, (_, i) => `| a${i} | b${i} |\n| - | - |\n| 1 | 2 |`).join(
3216+
"\n\n",
3217+
);
3218+
}
3219+
3220+
function makeActionPresentation(): MessagePresentation {
3221+
return {
3222+
title: "Confirm",
3223+
blocks: [
3224+
{
3225+
type: "buttons",
3226+
buttons: [{ label: "Confirm", action: { type: "command", command: "/ok" } }],
3227+
},
3228+
],
3229+
};
3230+
}
3231+
3232+
it("refuses the presentation card and falls back to post mode for 6 markdown tables", async () => {
3233+
const text = makeTableText(6);
3234+
await feishuOutbound.sendPayload?.({
3235+
cfg: emptyConfig,
3236+
to: "chat_1",
3237+
text,
3238+
accountId: "main",
3239+
payload: { text, presentation: makeActionPresentation() },
3240+
});
3241+
3242+
// The presentation card must not be built/sent (would hit ErrCode 11310).
3243+
expect(sendCardFeishuMock).not.toHaveBeenCalled();
3244+
// Delivery still happens through the post path with tables converted to code blocks.
3245+
expect(sendMessageFeishuMock).toHaveBeenCalled();
3246+
expect(sendMessageCall()?.text).toContain("```");
3247+
});
3248+
3249+
it("still builds the presentation card for 5 markdown tables", async () => {
3250+
const text = makeTableText(5);
3251+
await feishuOutbound.sendPayload?.({
3252+
cfg: emptyConfig,
3253+
to: "chat_1",
3254+
text,
3255+
accountId: "main",
3256+
payload: { text, presentation: makeActionPresentation() },
3257+
});
3258+
3259+
expect(sendCardFeishuMock).toHaveBeenCalled();
3260+
expect(sendMessageFeishuMock).not.toHaveBeenCalled();
3261+
});
3262+
3263+
it("refuses the card when a presentation text block embeds 6 tables", async () => {
3264+
const presentation: MessagePresentation = {
3265+
title: "Report",
3266+
blocks: [{ type: "text", text: makeTableText(6) }],
3267+
};
3268+
await feishuOutbound.sendPayload?.({
3269+
cfg: emptyConfig,
3270+
to: "chat_1",
3271+
text: "",
3272+
accountId: "main",
3273+
payload: { text: "", presentation },
3274+
});
3275+
3276+
expect(sendCardFeishuMock).not.toHaveBeenCalled();
3277+
expect(sendMessageFeishuMock).toHaveBeenCalled();
3278+
});
3279+
});
31293280
/* oxlint-disable max-lines -- TODO: split this grandfathered oversized file. */

‎extensions/feishu/src/presentation-card.test.ts‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import { normalizeMessagePresentation } from "openclaw/plugin-sdk/interactive-ru
22
import { describe, expect, it } from "vitest";
33
import {
44
buildFeishuPresentationCardElements,
5+
feishuCardWithinTableLimit,
56
isFeishuCardWithinEnvelope,
7+
withinCardTableLimit,
68
} from "./presentation-card.js";
79

810
describe("buildFeishuPresentationCardElements", () => {
@@ -50,3 +52,75 @@ describe("isFeishuCardWithinEnvelope", () => {
5052
expect(isFeishuCardWithinEnvelope(buildCard(201))).toBe(false);
5153
});
5254
});
55+
56+
describe("withinCardTableLimit (parser-backed table counting)", () => {
57+
const pipedTable = "| a | b |\n| - | - |\n| 1 | 2 |";
58+
const pipelessTable = "a | b\n--- | ---\n1 | 2";
59+
const repeat = (table: string, count: number) =>
60+
Array.from({ length: count }, () => table).join("\n\n");
61+
62+
it("accepts piped and pipe-less GFM tables at the 5-table boundary", () => {
63+
expect(withinCardTableLimit(repeat(pipedTable, 5))).toBe(true);
64+
expect(withinCardTableLimit(repeat(pipedTable, 6))).toBe(false);
65+
expect(withinCardTableLimit(repeat(pipelessTable, 5))).toBe(true);
66+
expect(withinCardTableLimit(repeat(pipelessTable, 6))).toBe(false);
67+
});
68+
69+
it("counts alignment-colon delimiters toward the limit", () => {
70+
const alignPiped = "| a | b |\n|:--|--:|\n| 1 | 2 |";
71+
const alignPipeless = "c | d\n:---: | ---\n3 | 4";
72+
expect(withinCardTableLimit(repeat(alignPiped, 6))).toBe(false);
73+
expect(withinCardTableLimit(repeat(alignPipeless, 6))).toBe(false);
74+
expect(withinCardTableLimit(repeat(alignPiped, 5))).toBe(true);
75+
});
76+
77+
it("does not count tables inside fenced code blocks", () => {
78+
expect(withinCardTableLimit("```\n" + repeat(pipedTable, 6) + "\n```")).toBe(true);
79+
expect(
80+
withinCardTableLimit("```\n" + repeat(pipedTable, 2) + "\n```\n\n" + repeat(pipedTable, 6)),
81+
).toBe(false);
82+
});
83+
84+
it("does not count thematic breaks or plain pipes in prose", () => {
85+
expect(withinCardTableLimit("---\n\nhello | world\n\n2024 | 2025")).toBe(true);
86+
});
87+
});
88+
89+
describe("feishuCardWithinTableLimit", () => {
90+
const table = "| a | b |\n| - | - |\n| 1 | 2 |";
91+
92+
it("sums tables across all markdown elements of the card", () => {
93+
const card = {
94+
schema: "2.0",
95+
body: {
96+
elements: [
97+
{ tag: "markdown", content: `${table}\n\n${table}\n\n${table}` },
98+
{ tag: "hr" },
99+
{ tag: "markdown", content: `${table}\n\n${table}\n\n${table}` },
100+
],
101+
},
102+
};
103+
expect(feishuCardWithinTableLimit(card)).toBe(false);
104+
});
105+
106+
it("accepts cards with at most 5 tables across elements", () => {
107+
const card = {
108+
schema: "2.0",
109+
body: {
110+
elements: [
111+
{ tag: "markdown", content: `${table}\n\n${table}\n\n${table}` },
112+
{ tag: "markdown", content: `${table}\n\n${table}` },
113+
],
114+
},
115+
};
116+
expect(feishuCardWithinTableLimit(card)).toBe(true);
117+
});
118+
119+
it("accepts cards without markdown tables", () => {
120+
const card = {
121+
schema: "2.0",
122+
body: { elements: [{ tag: "markdown", content: "plain | pipes but no table" }] },
123+
};
124+
expect(feishuCardWithinTableLimit(card)).toBe(true);
125+
});
126+
});

0 commit comments

Comments
 (0)