mirror of https://github.com/openclaw/openclaw.git
refactor: dedupe feishu send reply fallback helpers
This commit is contained in:
parent
0159269a51
commit
de9ea76b6c
|
|
@ -25,6 +25,16 @@ describe("Feishu reply fallback for withdrawn/deleted targets", () => {
|
||||||
const replyMock = vi.fn();
|
const replyMock = vi.fn();
|
||||||
const createMock = vi.fn();
|
const createMock = vi.fn();
|
||||||
|
|
||||||
|
async function expectFallbackResult(
|
||||||
|
send: () => Promise<{ messageId?: string }>,
|
||||||
|
expectedMessageId: string,
|
||||||
|
) {
|
||||||
|
const result = await send();
|
||||||
|
expect(replyMock).toHaveBeenCalledTimes(1);
|
||||||
|
expect(createMock).toHaveBeenCalledTimes(1);
|
||||||
|
expect(result.messageId).toBe(expectedMessageId);
|
||||||
|
}
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
vi.clearAllMocks();
|
vi.clearAllMocks();
|
||||||
resolveFeishuSendTargetMock.mockReturnValue({
|
resolveFeishuSendTargetMock.mockReturnValue({
|
||||||
|
|
@ -51,16 +61,16 @@ describe("Feishu reply fallback for withdrawn/deleted targets", () => {
|
||||||
data: { message_id: "om_new" },
|
data: { message_id: "om_new" },
|
||||||
});
|
});
|
||||||
|
|
||||||
const result = await sendMessageFeishu({
|
await expectFallbackResult(
|
||||||
|
() =>
|
||||||
|
sendMessageFeishu({
|
||||||
cfg: {} as never,
|
cfg: {} as never,
|
||||||
to: "user:ou_target",
|
to: "user:ou_target",
|
||||||
text: "hello",
|
text: "hello",
|
||||||
replyToMessageId: "om_parent",
|
replyToMessageId: "om_parent",
|
||||||
});
|
}),
|
||||||
|
"om_new",
|
||||||
expect(replyMock).toHaveBeenCalledTimes(1);
|
);
|
||||||
expect(createMock).toHaveBeenCalledTimes(1);
|
|
||||||
expect(result.messageId).toBe("om_new");
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("falls back to create for withdrawn card replies", async () => {
|
it("falls back to create for withdrawn card replies", async () => {
|
||||||
|
|
@ -73,16 +83,16 @@ describe("Feishu reply fallback for withdrawn/deleted targets", () => {
|
||||||
data: { message_id: "om_card_new" },
|
data: { message_id: "om_card_new" },
|
||||||
});
|
});
|
||||||
|
|
||||||
const result = await sendCardFeishu({
|
await expectFallbackResult(
|
||||||
|
() =>
|
||||||
|
sendCardFeishu({
|
||||||
cfg: {} as never,
|
cfg: {} as never,
|
||||||
to: "user:ou_target",
|
to: "user:ou_target",
|
||||||
card: { schema: "2.0" },
|
card: { schema: "2.0" },
|
||||||
replyToMessageId: "om_parent",
|
replyToMessageId: "om_parent",
|
||||||
});
|
}),
|
||||||
|
"om_card_new",
|
||||||
expect(replyMock).toHaveBeenCalledTimes(1);
|
);
|
||||||
expect(createMock).toHaveBeenCalledTimes(1);
|
|
||||||
expect(result.messageId).toBe("om_card_new");
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("still throws for non-withdrawn reply failures", async () => {
|
it("still throws for non-withdrawn reply failures", async () => {
|
||||||
|
|
@ -111,16 +121,16 @@ describe("Feishu reply fallback for withdrawn/deleted targets", () => {
|
||||||
data: { message_id: "om_thrown_fallback" },
|
data: { message_id: "om_thrown_fallback" },
|
||||||
});
|
});
|
||||||
|
|
||||||
const result = await sendMessageFeishu({
|
await expectFallbackResult(
|
||||||
|
() =>
|
||||||
|
sendMessageFeishu({
|
||||||
cfg: {} as never,
|
cfg: {} as never,
|
||||||
to: "user:ou_target",
|
to: "user:ou_target",
|
||||||
text: "hello",
|
text: "hello",
|
||||||
replyToMessageId: "om_parent",
|
replyToMessageId: "om_parent",
|
||||||
});
|
}),
|
||||||
|
"om_thrown_fallback",
|
||||||
expect(replyMock).toHaveBeenCalledTimes(1);
|
);
|
||||||
expect(createMock).toHaveBeenCalledTimes(1);
|
|
||||||
expect(result.messageId).toBe("om_thrown_fallback");
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("falls back to create when card reply throws a not-found AxiosError", async () => {
|
it("falls back to create when card reply throws a not-found AxiosError", async () => {
|
||||||
|
|
@ -133,16 +143,16 @@ describe("Feishu reply fallback for withdrawn/deleted targets", () => {
|
||||||
data: { message_id: "om_axios_fallback" },
|
data: { message_id: "om_axios_fallback" },
|
||||||
});
|
});
|
||||||
|
|
||||||
const result = await sendCardFeishu({
|
await expectFallbackResult(
|
||||||
|
() =>
|
||||||
|
sendCardFeishu({
|
||||||
cfg: {} as never,
|
cfg: {} as never,
|
||||||
to: "user:ou_target",
|
to: "user:ou_target",
|
||||||
card: { schema: "2.0" },
|
card: { schema: "2.0" },
|
||||||
replyToMessageId: "om_parent",
|
replyToMessageId: "om_parent",
|
||||||
});
|
}),
|
||||||
|
"om_axios_fallback",
|
||||||
expect(replyMock).toHaveBeenCalledTimes(1);
|
);
|
||||||
expect(createMock).toHaveBeenCalledTimes(1);
|
|
||||||
expect(result.messageId).toBe("om_axios_fallback");
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("re-throws non-withdrawn thrown errors for text messages", async () => {
|
it("re-throws non-withdrawn thrown errors for text messages", async () => {
|
||||||
|
|
|
||||||
|
|
@ -55,6 +55,30 @@ type FeishuCreateMessageClient = {
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
type FeishuMessageSender = {
|
||||||
|
id?: string;
|
||||||
|
id_type?: string;
|
||||||
|
sender_type?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
type FeishuMessageGetItem = {
|
||||||
|
message_id?: string;
|
||||||
|
chat_id?: string;
|
||||||
|
chat_type?: FeishuChatType;
|
||||||
|
msg_type?: string;
|
||||||
|
body?: { content?: string };
|
||||||
|
sender?: FeishuMessageSender;
|
||||||
|
create_time?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
type FeishuGetMessageResponse = {
|
||||||
|
code?: number;
|
||||||
|
msg?: string;
|
||||||
|
data?: FeishuMessageGetItem & {
|
||||||
|
items?: FeishuMessageGetItem[];
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
/** Send a direct message as a fallback when a reply target is unavailable. */
|
/** Send a direct message as a fallback when a reply target is unavailable. */
|
||||||
async function sendFallbackDirect(
|
async function sendFallbackDirect(
|
||||||
client: FeishuCreateMessageClient,
|
client: FeishuCreateMessageClient,
|
||||||
|
|
@ -214,36 +238,7 @@ export async function getMessageFeishu(params: {
|
||||||
try {
|
try {
|
||||||
const response = (await client.im.message.get({
|
const response = (await client.im.message.get({
|
||||||
path: { message_id: messageId },
|
path: { message_id: messageId },
|
||||||
})) as {
|
})) as FeishuGetMessageResponse;
|
||||||
code?: number;
|
|
||||||
msg?: string;
|
|
||||||
data?: {
|
|
||||||
items?: Array<{
|
|
||||||
message_id?: string;
|
|
||||||
chat_id?: string;
|
|
||||||
chat_type?: FeishuChatType;
|
|
||||||
msg_type?: string;
|
|
||||||
body?: { content?: string };
|
|
||||||
sender?: {
|
|
||||||
id?: string;
|
|
||||||
id_type?: string;
|
|
||||||
sender_type?: string;
|
|
||||||
};
|
|
||||||
create_time?: string;
|
|
||||||
}>;
|
|
||||||
message_id?: string;
|
|
||||||
chat_id?: string;
|
|
||||||
chat_type?: FeishuChatType;
|
|
||||||
msg_type?: string;
|
|
||||||
body?: { content?: string };
|
|
||||||
sender?: {
|
|
||||||
id?: string;
|
|
||||||
id_type?: string;
|
|
||||||
sender_type?: string;
|
|
||||||
};
|
|
||||||
create_time?: string;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
if (response.code !== 0) {
|
if (response.code !== 0) {
|
||||||
return null;
|
return null;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue