feat: archive bindings on Feishu lifecycle events

This commit is contained in:
2026-07-13 15:53:28 +08:00
parent d94cc787b2
commit 82f57317df
7 changed files with 402 additions and 7 deletions
+61 -1
View File
@@ -1,6 +1,6 @@
import { describe, expect, it, vi, beforeEach } from "vitest";
import type { FastifyBaseLogger } from "fastify";
import type { CardActionEvent, FeishuRuntime } from "../../src/feishu/client.js";
import type { CardActionEvent, FeishuBindingLifecycleEvent, FeishuRuntime } from "../../src/feishu/client.js";
import { startFeishuListenerWithClient } from "../../src/feishu/client.js";
const larkMock = vi.hoisted(() => {
@@ -80,6 +80,57 @@ describe("Feishu card action callbacks", () => {
expect(logger.error).toHaveBeenCalledWith({ err }, "feishu card action handler threw");
});
});
it("dispatches chat dissolution and bot-removal lifecycle events", async () => {
const onLifecycle = vi.fn(async (_event: FeishuBindingLifecycleEvent) => {});
await startFeishuListenerWithClient(
{ appId: "app-id", appSecret: "app-secret", botOpenId: "bot-open-id" },
fakeClient(),
silentLogger(),
async () => {},
undefined,
undefined,
onLifecycle,
);
await lifecycleHandler("im.chat.disbanded_v1")({
event_id: "event-dissolved",
chat_id: "chat-dissolved",
});
await lifecycleHandler("im.chat.member.bot.deleted_v1")({
header: { event_id: "event-bot-removed" },
chat_id: "chat-bot-removed",
});
expect(onLifecycle).toHaveBeenNthCalledWith(1, {
eventId: "event-dissolved",
chatId: "chat-dissolved",
reason: "chat_dissolved",
});
expect(onLifecycle).toHaveBeenNthCalledWith(2, {
eventId: "event-bot-removed",
chatId: "chat-bot-removed",
reason: "bot_removed",
});
});
it("propagates lifecycle archival failures to the WS dispatcher", async () => {
const failure = new Error("archive failed");
await startFeishuListenerWithClient(
{ appId: "app-id", appSecret: "app-secret", botOpenId: "bot-open-id" },
fakeClient(),
silentLogger(),
async () => {},
undefined,
undefined,
async () => { throw failure; },
);
await expect(lifecycleHandler("im.chat.disbanded_v1")({
event_id: "event-failure",
chat_id: "chat-failure",
})).rejects.toThrow(failure);
});
});
function cardActionHandler(): (data: unknown) => Promise<void> {
@@ -91,6 +142,15 @@ function cardActionHandler(): (data: unknown) => Promise<void> {
return handler;
}
function lifecycleHandler(eventType: string): (data: unknown) => Promise<void> {
const start = larkMock.starts[0];
if (start === undefined) throw new Error("WSClient.start was not called");
const dispatcher = start.eventDispatcher as { readonly handlers?: Record<string, (data: unknown) => Promise<void>> };
const handler = dispatcher.handlers?.[eventType];
if (handler === undefined) throw new Error(`${eventType} handler was not registered`);
return handler;
}
function makeCardActionEvent(): CardActionEvent {
return {
operator: { open_id: "ou_user" },