bot-remote-disconnected.test.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import type { BotsConfigFile, BotsStateFile } from "@zcode/shared";
  2. import {
  3. createBotsServiceHarness,
  4. readBotSyntheticFixture,
  5. } from "./helpers/bots-service-harness.js";
  6. describe("Bot remote workspace delivery E2E", () => {
  7. it("BOT-E2E-RW-01 rejects a disconnected remote workspace without reconnect or local fallback", async () => {
  8. const fixture = await readBotSyntheticFixture("remote-workspace-disconnected.json");
  9. expect(fixture).toMatchObject({ caseId: "BOT-E2E-RW-01", classification: "synthetic" });
  10. expect(fixture.syntheticReason.length).toBeGreaterThan(20);
  11. const config: BotsConfigFile = {
  12. version: 3,
  13. bots: [
  14. {
  15. id: "webhook-e2e",
  16. name: "Webhook E2E",
  17. provider: "webhook",
  18. enabled: true,
  19. webhookSecretRef: "webhook-secret-e2e",
  20. providerUserId: "user-e2e",
  21. allowedWorkspaces: ["*"],
  22. allowedCommands: {
  23. status: true,
  24. new: true,
  25. workspace: true,
  26. model: true,
  27. thoughtLevel: true,
  28. sandboxMode: true,
  29. approvalPolicy: true,
  30. reply: true,
  31. },
  32. currentOptions: {},
  33. replyMode: "assistant_changes",
  34. },
  35. ],
  36. };
  37. const state: BotsStateFile = {
  38. version: 3,
  39. bots: {
  40. "webhook-e2e": {
  41. botId: "webhook-e2e",
  42. workspacePath: "/workspace",
  43. workspaceIdentity: "ssh://e2e-host/workspace",
  44. workspaceId: "ssh://e2e-host/workspace",
  45. mode: "task",
  46. activeTaskId: "task-remote-e2e",
  47. updatedAt: Date.now(),
  48. },
  49. },
  50. };
  51. let isConnectedCalls = 0;
  52. let ensureConnectedCalls = 0;
  53. let remoteTaskServiceCalls = 0;
  54. const harness = createBotsServiceHarness({
  55. config,
  56. state,
  57. lastWorkspaceSession: [
  58. {
  59. kind: "remote",
  60. workspacePath: "/workspace",
  61. workspaceIdentity: "ssh://e2e-host/workspace",
  62. remoteSessionId: "remote-session-e2e",
  63. },
  64. ],
  65. remoteWorkspaceService: {
  66. isConnected: async (target: unknown) => {
  67. isConnectedCalls += 1;
  68. expect(target).toEqual({
  69. workspacePath: "/workspace",
  70. workspaceIdentity: "ssh://e2e-host/workspace",
  71. });
  72. return false;
  73. },
  74. ensureConnected: async () => {
  75. ensureConnectedCalls += 1;
  76. return { ok: true };
  77. },
  78. getZCodeTaskService: async () => {
  79. remoteTaskServiceCalls += 1;
  80. return harness.zcodeTaskService;
  81. },
  82. },
  83. });
  84. try {
  85. const replies = await harness.service.handleInboundMessage({
  86. botId: "webhook-e2e",
  87. text: "E2E_BOT_REMOTE_DISCONNECTED 请检查当前项目",
  88. actor: {
  89. provider: "webhook",
  90. botId: "webhook-e2e",
  91. providerUserId: "user-e2e",
  92. chatType: "private",
  93. },
  94. });
  95. expect(replies.map((reply) => reply.text)).toEqual([
  96. "当前远端项目 /workspace 未连接。请先发送 **/重连**,连接恢复后再重试。上一条请求未执行。",
  97. ]);
  98. expect(isConnectedCalls).toBe(1);
  99. expect(ensureConnectedCalls).toBe(0);
  100. expect(remoteTaskServiceCalls).toBe(0);
  101. expect(harness.createTaskCalls).toEqual([]);
  102. expect(harness.sendPromptCalls).toEqual([]);
  103. } finally {
  104. harness.service.disposeAll();
  105. }
  106. });
  107. });