test: share feishu card assertions

This commit is contained in:
Peter Steinberger
2026-04-20 21:54:53 +01:00
parent cb869c823e
commit 972d01965c
4 changed files with 61 additions and 88 deletions

View File

@@ -8,6 +8,10 @@ import {
type FeishuCardActionEvent,
} from "./card-action.js";
import { createFeishuCardInteractionEnvelope } from "./card-interaction.js";
import {
expectFirstSentCardUsesFillWidthOnly,
expectSentCardHasP2pAction,
} from "./card-test-helpers.js";
import {
FEISHU_APPROVAL_CANCEL_ACTION,
FEISHU_APPROVAL_CONFIRM_ACTION,
@@ -239,21 +243,7 @@ describe("Feishu Card Action Handler", () => {
}),
}),
);
const firstSendArg = (sendCardFeishuMock.mock.calls as unknown[][]).at(0)?.[0] as
| {
card?: {
config?: {
width_mode?: string;
wide_screen_mode?: boolean;
enable_forward?: boolean;
};
};
}
| undefined;
const sentCard = firstSendArg?.card;
expect(sentCard).toBeDefined();
expect(sentCard?.config?.wide_screen_mode).toBeUndefined();
expect(sentCard?.config?.enable_forward).toBeUndefined();
expectFirstSentCardUsesFillWidthOnly(sendCardFeishuMock);
expect(handleFeishuMessage).not.toHaveBeenCalled();
});
@@ -423,28 +413,7 @@ describe("Feishu Card Action Handler", () => {
await handleFeishuCardAction({ cfg, event, runtime, accountId: "main" });
expect(sendCardFeishuMock).toHaveBeenCalledWith(
expect.objectContaining({
card: expect.objectContaining({
body: expect.objectContaining({
elements: expect.arrayContaining([
expect.objectContaining({
tag: "action",
actions: expect.arrayContaining([
expect.objectContaining({
value: expect.objectContaining({
c: expect.objectContaining({
t: "p2p",
}),
}),
}),
]),
}),
]),
}),
}),
}),
);
expectSentCardHasP2pAction(sendCardFeishuMock);
expect(createFeishuClientMock).toHaveBeenCalledTimes(1);
});

View File

@@ -0,0 +1,47 @@
import { expect } from "vitest";
export function expectFirstSentCardUsesFillWidthOnly(sendCardMock: {
mock: { calls: unknown[][] };
}) {
const firstSendArg = sendCardMock.mock.calls.at(0)?.[0] as
| {
card?: {
config?: {
width_mode?: string;
wide_screen_mode?: boolean;
enable_forward?: boolean;
};
};
}
| undefined;
const sentCard = firstSendArg?.card;
expect(sentCard).toBeDefined();
expect(sentCard?.config?.width_mode).toBe("fill");
expect(sentCard?.config?.wide_screen_mode).toBeUndefined();
expect(sentCard?.config?.enable_forward).toBeUndefined();
}
export function expectSentCardHasP2pAction(sendCardMock: unknown) {
expect(sendCardMock).toHaveBeenCalledWith(
expect.objectContaining({
card: expect.objectContaining({
body: expect.objectContaining({
elements: expect.arrayContaining([
expect.objectContaining({
tag: "action",
actions: expect.arrayContaining([
expect.objectContaining({
value: expect.objectContaining({
c: expect.objectContaining({
t: "p2p",
}),
}),
}),
]),
}),
]),
}),
}),
}),
);
}

View File

@@ -1,6 +1,10 @@
import { describe, expect, it, vi, beforeEach } from "vitest";
import { createRuntimeEnv } from "../../../test/helpers/plugins/runtime-env.js";
import type { ClawdbotConfig, RuntimeEnv } from "../runtime-api.js";
import {
expectFirstSentCardUsesFillWidthOnly,
expectSentCardHasP2pAction,
} from "./card-test-helpers.js";
import {
createQuickActionLauncherCard,
isFeishuQuickActionMenuEventKey,
@@ -71,44 +75,10 @@ describe("feishu quick-action launcher", () => {
expect.objectContaining({
to: "user:u123",
accountId: "main",
card: expect.objectContaining({
config: expect.objectContaining({
width_mode: "fill",
}),
body: expect.objectContaining({
elements: expect.arrayContaining([
expect.objectContaining({
tag: "action",
actions: expect.arrayContaining([
expect.objectContaining({
value: expect.objectContaining({
c: expect.objectContaining({
t: "p2p",
}),
}),
}),
]),
}),
]),
}),
}),
}),
);
const firstSendArg = (sendCardFeishuMock.mock.calls as unknown[][]).at(0)?.[0] as
| {
card?: {
config?: {
width_mode?: string;
wide_screen_mode?: boolean;
enable_forward?: boolean;
};
};
}
| undefined;
const sentCard = firstSendArg?.card;
expect(sentCard).toBeDefined();
expect(sentCard?.config?.wide_screen_mode).toBeUndefined();
expect(sentCard?.config?.enable_forward).toBeUndefined();
expectSentCardHasP2pAction(sendCardFeishuMock);
expectFirstSentCardUsesFillWidthOnly(sendCardFeishuMock);
});
it("falls back to legacy menu handling when launcher send fails", async () => {

View File

@@ -1,5 +1,6 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import type { ClawdbotConfig, RuntimeEnv } from "../runtime-api.js";
import { expectFirstSentCardUsesFillWidthOnly } from "./card-test-helpers.js";
import { monitorSingleAccount } from "./monitor.account.js";
import { setFeishuRuntime } from "./runtime.js";
import type { ResolvedFeishuAccount } from "./types.js";
@@ -215,21 +216,7 @@ describe("Feishu bot menu handler", () => {
}),
);
});
const firstSendArg = (sendCardFeishuMock.mock.calls as unknown[][]).at(0)?.[0] as
| {
card?: {
config?: {
width_mode?: string;
wide_screen_mode?: boolean;
enable_forward?: boolean;
};
};
}
| undefined;
const sentCard = firstSendArg?.card;
expect(sentCard).toBeDefined();
expect(sentCard?.config?.wide_screen_mode).toBeUndefined();
expect(sentCard?.config?.enable_forward).toBeUndefined();
expectFirstSentCardUsesFillWidthOnly(sendCardFeishuMock);
});
it("reopens replay for explicit retryable fallback failures", async () => {