feishu-group-collaboration.test.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import type { BotGroupInputSource } from "@zcode/shared";
  2. import {
  3. createBotsServiceHarness,
  4. createFeishuConfig,
  5. createRecordingFeishuFetch,
  6. readBotSyntheticFixture,
  7. type RecordedRequest,
  8. } from "#e2e/bots/helpers/bots-service-harness";
  9. import { resolveE2ERuntimePath } from "#e2e/helpers/e2e-runtime-paths";
  10. describe("BOT-E2E-GR-01 group provider admission", () => {
  11. for (const provider of ["feishu", "lark"] as const) {
  12. it(`${provider}: isolates groups, checks roles and acknowledges canonical admission`, async () => {
  13. const fixture = await readBotSyntheticFixture("feishu-group-collaboration.json");
  14. expect(fixture.classification).toBe("synthetic");
  15. const requests: RecordedRequest[] = [];
  16. const originalFetch = globalThis.fetch;
  17. const recordingFetch = createRecordingFeishuFetch(requests);
  18. globalThis.fetch = async (input, init) => {
  19. const url = String(input);
  20. if (url.endsWith("/bot/v3/info"))
  21. return Response.json({ code: 0, bot: { open_id: "ou_current_bot" } });
  22. if (url.includes("/im/v1/chats/"))
  23. return Response.json({ code: 0, data: { name: "E2E Group", chat_mode: "group" } });
  24. return recordingFetch(input, init);
  25. };
  26. const botId = `${provider}-group-e2e`;
  27. const h = createBotsServiceHarness({ config: createFeishuConfig(provider, botId) });
  28. const admissions: Array<{ taskId: string; commandId: string; source: BotGroupInputSource }> =
  29. [];
  30. let created = 0;
  31. h.zcodeTaskService.createTask = async () => ({
  32. taskId: `group-task-${++created}`,
  33. title: "E2E Group",
  34. workspacePath: resolveE2ERuntimePath("bots", "e2e-bot-workspace"),
  35. provider: "glm",
  36. });
  37. h.zcodeTaskService.submitBotGroupInput = async (params: (typeof admissions)[number]) => {
  38. admissions.push(params);
  39. return {
  40. commandId: params.commandId,
  41. status: "accepted",
  42. revisionAtDecision: admissions.length,
  43. result: { type: "inputAccepted", inputId: params.commandId, delivery: "queue" },
  44. };
  45. };
  46. let messageSequence = 0;
  47. const callback = (
  48. text: string,
  49. chatId: string,
  50. sender = "ou_e2e_user",
  51. botMention = "ou_current_bot",
  52. ) => ({
  53. botId,
  54. event: {
  55. sender: { sender_type: "user", sender_id: { open_id: sender } },
  56. message: {
  57. chat_type: "group",
  58. chat_id: chatId,
  59. message_id: `om_group_${++messageSequence}`,
  60. message_type: "text",
  61. content: JSON.stringify({ text: `@_user_1 ${text}` }),
  62. mentions: [{ key: "@_user_1", id: { open_id: botMention }, name: "E2E Bot" }],
  63. },
  64. },
  65. });
  66. const send = (payload: ReturnType<typeof callback>) =>
  67. h.service.handleProviderCallbackResponse(provider, payload);
  68. try {
  69. await send(callback("/enable", "oc_a", "ou_member"));
  70. expect((await h.service.getBotStates()).filter((s) => s.group)).toHaveLength(0);
  71. const enableA = callback("/enable", "oc_a");
  72. const enableB = callback("/enable", "oc_b");
  73. await Promise.all([send(enableA), send(enableB)]);
  74. expect((await h.service.getBotStates()).filter((s) => s.group)).toHaveLength(2);
  75. const first = callback(fixture.promptMarker!, "oc_a", "ou_member");
  76. expect((await send(first)).ok).toBe(true);
  77. await send(first);
  78. await send(callback(fixture.promptMarker!, "oc_b", "ou_other"));
  79. await send(callback("ignored", "oc_a", "ou_member", "ou_different_bot"));
  80. await send(callback("/new", "oc_a", "ou_member"));
  81. expect(admissions).toHaveLength(2);
  82. expect(admissions[0]?.source).toMatchObject({
  83. provider,
  84. botId,
  85. chatId: "oc_a",
  86. senderId: "ou_member",
  87. });
  88. expect(admissions[1]?.source).toMatchObject({ chatId: "oc_b", senderId: "ou_other" });
  89. expect(admissions[0]?.taskId).not.toBe(admissions[1]?.taskId);
  90. expect(h.sendPromptCalls).toHaveLength(0);
  91. expect(
  92. h.subscriptions.every((s) => s.params.deliveryKind === "bot-channel-continuous"),
  93. ).toBe(true);
  94. expect(
  95. requests.filter((r) => r.method === "POST" && r.url.includes("/reactions")),
  96. ).toHaveLength(2);
  97. // 群命令按当前产品契约引用原消息;旧断言只接受无引用的 chat_id 发送。
  98. for (const enabled of [enableA, enableB]) {
  99. expect(
  100. requests.some(
  101. (r) =>
  102. r.method === "POST" &&
  103. r.url.endsWith(`/messages/${enabled.event.message.message_id}/reply`),
  104. ),
  105. ).toBe(true);
  106. }
  107. } finally {
  108. await h.service.disposeAllAndWait();
  109. globalThis.fetch = originalFetch;
  110. }
  111. });
  112. }
  113. });