bot-runtime-authority.test.ts 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 "./helpers/bots-service-harness.js";
  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. runStartupBackgroundTasks: false,
  53. lastWorkspaceSession: [
  54. {
  55. kind: "remote",
  56. workspacePath: "/workspace",
  57. workspaceIdentity: "ssh://e2e-host/workspace",
  58. remoteSessionId: "remote-session-e2e",
  59. },
  60. ],
  61. });
  62. try {
  63. await new Promise((resolve) => setTimeout(resolve, 100));
  64. const status = await harness.service.getStatus();
  65. expect(status.botRuntime).toEqual([
  66. expect.objectContaining({
  67. botId: "feishu-e2e",
  68. provider: "feishu",
  69. status: "idle",
  70. message: "Bot is configured.",
  71. }),
  72. ]);
  73. expect(requests).toEqual([]);
  74. } finally {
  75. harness.service.disposeAll();
  76. globalThis.fetch = originalFetch;
  77. }
  78. });
  79. });