botsWebhookProvider.test.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import { afterEach, describe, expect, it, vi } from "vitest";
  2. import { createWebhookBotProvider } from "../src/bots/providers/webhookProvider.js";
  3. afterEach(() => {
  4. vi.unstubAllGlobals();
  5. });
  6. describe("webhook bot provider", () => {
  7. it("parses webhook attachments with empty text", () => {
  8. const provider = createWebhookBotProvider({
  9. loadCredential: vi.fn(async () => "secret"),
  10. });
  11. expect(
  12. provider.parseCallback({
  13. botId: "webhook-1",
  14. userId: "user-1",
  15. text: "",
  16. attachments: [
  17. {
  18. id: "img-1",
  19. kind: "image",
  20. filename: "demo.png",
  21. mimeType: "image/png",
  22. dataBase64: "AQID",
  23. },
  24. ],
  25. }),
  26. ).toEqual([
  27. expect.objectContaining({
  28. text: "",
  29. attachments: [
  30. expect.objectContaining({
  31. id: "img-1",
  32. kind: "image",
  33. dataBase64: "AQID",
  34. }),
  35. ],
  36. }),
  37. ]);
  38. });
  39. it("parses structured elicitation responses without text", () => {
  40. const provider = createWebhookBotProvider({
  41. loadCredential: vi.fn(async () => "secret"),
  42. });
  43. expect(
  44. provider.parseCallback({
  45. type: "zcode.bot.elicitation_response",
  46. botId: "webhook-1",
  47. userId: "user-1",
  48. requestId: "elicit-1",
  49. action: "accept",
  50. content: { answer: "yes" },
  51. }),
  52. ).toEqual([
  53. expect.objectContaining({
  54. text: "",
  55. elicitationResponse: {
  56. requestId: "elicit-1",
  57. action: "accept",
  58. content: { answer: "yes" },
  59. },
  60. }),
  61. ]);
  62. });
  63. it("posts outbound replies to the configured webhook URL with the bot secret", async () => {
  64. const fetchMock = vi.fn(async () => new Response("ok", { status: 200 }));
  65. vi.stubGlobal("fetch", fetchMock);
  66. const provider = createWebhookBotProvider({
  67. loadCredential: vi.fn(async () => "secret"),
  68. });
  69. await provider.send(
  70. {
  71. id: "webhook-1",
  72. name: "Webhook",
  73. provider: "webhook",
  74. enabled: true,
  75. webhookUrl: "https://example.test/zcode",
  76. webhookSecretRef: "secret-ref",
  77. allowedWorkspaces: ["*"],
  78. allowedCommands: {
  79. status: true,
  80. new: true,
  81. workspace: true,
  82. model: true,
  83. thoughtLevel: true,
  84. sandboxMode: true,
  85. approvalPolicy: true,
  86. reply: true,
  87. },
  88. currentOptions: {},
  89. replyMode: "assistant_changes",
  90. },
  91. {
  92. botId: "webhook-1",
  93. provider: "webhook",
  94. providerUserId: "user-1",
  95. text: "hello",
  96. },
  97. );
  98. expect(fetchMock).toHaveBeenCalledWith(
  99. "https://example.test/zcode",
  100. expect.objectContaining({
  101. method: "POST",
  102. headers: expect.objectContaining({
  103. "content-type": "application/json",
  104. "x-zcode-bot-secret": "secret",
  105. }),
  106. }),
  107. );
  108. const body = JSON.parse(String(fetchMock.mock.calls[0]?.[1]?.body)) as { type?: string; text?: string };
  109. expect(body).toMatchObject({ type: "zcode.bot.message", text: "hello" });
  110. });
  111. it("posts structured elicitation requests to webhook URLs", async () => {
  112. const fetchMock = vi.fn(async () => new Response("ok", { status: 200 }));
  113. vi.stubGlobal("fetch", fetchMock);
  114. const provider = createWebhookBotProvider({
  115. loadCredential: vi.fn(async () => "secret"),
  116. });
  117. await provider.send(
  118. {
  119. id: "webhook-1",
  120. name: "Webhook",
  121. provider: "webhook",
  122. enabled: true,
  123. webhookUrl: "https://example.test/zcode",
  124. webhookSecretRef: "secret-ref",
  125. allowedWorkspaces: ["*"],
  126. allowedCommands: {
  127. status: true,
  128. new: true,
  129. workspace: true,
  130. model: true,
  131. thoughtLevel: true,
  132. sandboxMode: true,
  133. approvalPolicy: true,
  134. reply: true,
  135. },
  136. currentOptions: {},
  137. replyMode: "assistant_changes",
  138. },
  139. {
  140. botId: "webhook-1",
  141. provider: "webhook",
  142. providerUserId: "user-1",
  143. text: "Question",
  144. elicitation: {
  145. requestId: "elicit-1",
  146. taskId: "task-1",
  147. runId: "trace-1",
  148. currentQuestionIndex: 0,
  149. questions: [
  150. {
  151. question: "Pick?",
  152. header: "Pick",
  153. options: [{ value: "yes", label: "Yes" }],
  154. },
  155. ],
  156. },
  157. },
  158. );
  159. const body = JSON.parse(String(fetchMock.mock.calls[0]?.[1]?.body)) as {
  160. type?: string;
  161. elicitation?: unknown;
  162. };
  163. expect(body.type).toBe("zcode.bot.elicitation_request");
  164. expect(body.elicitation).toMatchObject({ requestId: "elicit-1" });
  165. });
  166. });