bot-deleted-task-recovery.test.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import { readFile } from "node:fs/promises";
  2. import type { BotsStateFile } from "@zcode/shared";
  3. import {
  4. createBotsServiceHarness,
  5. createFeishuCallback,
  6. createFeishuConfig,
  7. createRecordingFeishuFetch,
  8. type RecordedRequest,
  9. } from "#e2e/bots/helpers/bots-service-harness";
  10. import { resolveE2ERuntimePath } from "#e2e/helpers/e2e-runtime-paths";
  11. const workspacePath = resolveE2ERuntimePath("bots", "e2e-bot-workspace");
  12. describe("BOT-E2E-DT-01 deleted task recovery", () => {
  13. it("replaces the hidden task and notifies once even if Feishu redelivers the message", async () => {
  14. const fixture = JSON.parse(
  15. await readFile(
  16. // 数据文件位置按当前 spec 计算,fixture 仍在 E2E 根目录下。
  17. new URL("../../fixtures/bots/bot-deleted-task-recovery.json", import.meta.url),
  18. "utf8",
  19. ),
  20. );
  21. expect(fixture.classification).toBe("synthetic");
  22. const requests: RecordedRequest[] = [];
  23. const broadcasts: unknown[] = [];
  24. const originalFetch = globalThis.fetch;
  25. globalThis.fetch = createRecordingFeishuFetch(requests);
  26. const botId = "feishu-deleted-e2e";
  27. const state: BotsStateFile = {
  28. version: 3,
  29. bots: {
  30. [botId]: {
  31. botId,
  32. workspacePath,
  33. workspaceId: workspacePath,
  34. mode: "task",
  35. activeTaskId: "task-deleted",
  36. pendingPermissionOptions: [],
  37. updatedAt: Date.now(),
  38. },
  39. },
  40. };
  41. const h = createBotsServiceHarness({
  42. config: createFeishuConfig("feishu", botId),
  43. state,
  44. broadcastService: {
  45. send: async (message) => {
  46. broadcasts.push(message);
  47. },
  48. },
  49. });
  50. let deletionReads = 0;
  51. h.zcodeTaskService.listDeletedTaskIds = async (params: unknown) => {
  52. expect(params).toEqual({ workspacePath, workspaceIdentity: undefined });
  53. deletionReads += 1;
  54. return ["task-deleted"];
  55. };
  56. let resumeCalls = 0;
  57. h.zcodeTaskService.resumeTask = async () => {
  58. resumeCalls += 1;
  59. throw new Error("deleted task must not resume");
  60. };
  61. try {
  62. const callback = createFeishuCallback(botId, fixture.promptMarker);
  63. const result = await h.service.handleProviderCallbackResponse("feishu", callback);
  64. expect(result.ok).toBe(true);
  65. const duplicate = await h.service.handleProviderCallbackResponse("feishu", callback);
  66. expect(duplicate.ok).toBe(true);
  67. // 订阅恢复与消息准入都会读删除索引;去重契约是下方的新建、派发与通知各一次。
  68. expect(deletionReads).toBeGreaterThan(0);
  69. expect(resumeCalls).toBe(0);
  70. expect(h.createTaskCalls).toHaveLength(1);
  71. expect(h.sendPromptCalls).toHaveLength(1);
  72. expect(h.sendPromptCalls[0]).toMatchObject({
  73. taskId: "task-e2e-bot",
  74. content: fixture.promptMarker,
  75. });
  76. expect(await h.service.getBotStates()).toEqual([
  77. expect.objectContaining({
  78. activeTaskId: "task-e2e-bot",
  79. workspacePath,
  80. pendingPermissionOptions: undefined,
  81. }),
  82. ]);
  83. expect(broadcasts).toContainEqual(
  84. expect.objectContaining({
  85. channel: "bots:task",
  86. payload: expect.objectContaining({
  87. event: "created",
  88. taskId: "task-e2e-bot",
  89. workspacePath,
  90. }),
  91. }),
  92. );
  93. expect(h.subscriptions[0]?.params).toMatchObject({
  94. taskId: "task-e2e-bot",
  95. deliveryKind: "bot-channel-continuous",
  96. });
  97. const notices = requests.filter(
  98. (request) => request.method === "POST" && request.body?.includes("原任务已删除"),
  99. );
  100. expect(notices).toHaveLength(1);
  101. expect(notices[0]?.body).toContain("不会继承原任务的对话上下文");
  102. } finally {
  103. h.service.disposeAll();
  104. globalThis.fetch = originalFetch;
  105. }
  106. });
  107. });