bot-runtime-authority.test.ts 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import { mkdtemp, rm } from "node:fs/promises";
  2. import { tmpdir } from "node:os";
  3. import { join } from "node:path";
  4. import { setDataBaseDir } from "@zcode/services/node";
  5. import { acquireFeishuWebSocketLock } from "../../../../../services/src/bots/channelRuntime.js";
  6. import {
  7. createBotsServiceHarness,
  8. createFeishuConfig,
  9. readBotSyntheticFixture,
  10. } from "#e2e/bots/helpers/bots-service-harness";
  11. describe("Bot runtime authority E2E", () => {
  12. let dataDir: string | undefined;
  13. afterEach(async () => {
  14. setDataBaseDir(null);
  15. if (dataDir) await rm(dataDir, { recursive: true, force: true });
  16. dataDir = undefined;
  17. });
  18. it("BOT-E2E-RT-01 grants the Feishu runtime lock to only one local host", async () => {
  19. const fixture = await readBotSyntheticFixture("feishu-runtime-lock.json");
  20. expect(fixture).toMatchObject({ caseId: "BOT-E2E-RT-01", classification: "synthetic" });
  21. dataDir = await mkdtemp(join(tmpdir(), "zcode-e2e-bot-runtime-"));
  22. setDataBaseDir(dataDir);
  23. const bot = createFeishuConfig().bots[0]!;
  24. const firstHostLock = await acquireFeishuWebSocketLock(bot);
  25. expect(firstHostLock).not.toBeNull();
  26. try {
  27. const competingBot = {
  28. ...bot,
  29. id: "feishu-same-app-other-record",
  30. credentialRef: "feishu-other-secret-ref",
  31. };
  32. const competingHostLock = await acquireFeishuWebSocketLock(competingBot);
  33. expect(competingHostLock).toBeNull();
  34. } finally {
  35. await firstHostLock?.release();
  36. }
  37. const takeoverLock = await acquireFeishuWebSocketLock(bot);
  38. expect(takeoverLock).not.toBeNull();
  39. await takeoverLock?.release();
  40. });
  41. it("BOT-E2E-RT-02 keeps provider runtimes disabled for an attached remote host", async () => {
  42. const fixture = await readBotSyntheticFixture("attached-remote-runtime.json");
  43. expect(fixture).toMatchObject({ caseId: "BOT-E2E-RT-02", classification: "synthetic" });
  44. const originalFetch = globalThis.fetch;
  45. const requests: string[] = [];
  46. globalThis.fetch = async (input) => {
  47. requests.push(String(input));
  48. return new Response(JSON.stringify({ code: 0 }));
  49. };
  50. const harness = createBotsServiceHarness({
  51. config: createFeishuConfig(),
  52. // 与 services/node.ts 的 !isDesktopAttachedRemote 装配一致;这是 owner 权限,
  53. // 不是跳过被测流程。下方保存配置还会触发 scheduleRefresh,验证 guard 仍生效。
  54. runStartupBackgroundTasks: false,
  55. projectWorkspaces: [
  56. {
  57. kind: "remote",
  58. workspacePath: "/workspace",
  59. workspaceIdentity: "remote:server:e2e-host:/workspace",
  60. remoteSessionId: "remote-session-e2e",
  61. },
  62. ],
  63. });
  64. try {
  65. await harness.service.saveConfig(createFeishuConfig());
  66. await new Promise((resolve) => setTimeout(resolve, 100));
  67. const status = await harness.service.getStatus();
  68. expect(status.botRuntime).toEqual([
  69. expect.objectContaining({
  70. botId: "feishu-e2e",
  71. provider: "feishu",
  72. status: "idle",
  73. message: "Bot is configured.",
  74. }),
  75. ]);
  76. expect(requests).toEqual([]);
  77. } finally {
  78. await harness.service.disposeAllAndWait();
  79. globalThis.fetch = originalFetch;
  80. }
  81. });
  82. });