| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- import { afterEach, describe, expect, it, vi } from "vitest";
- import { createWebhookBotProvider } from "../src/bots/providers/webhookProvider.js";
- afterEach(() => {
- vi.unstubAllGlobals();
- });
- describe("webhook bot provider", () => {
- it("parses webhook attachments with empty text", () => {
- const provider = createWebhookBotProvider({
- loadCredential: vi.fn(async () => "secret"),
- });
- expect(
- provider.parseCallback({
- botId: "webhook-1",
- userId: "user-1",
- text: "",
- attachments: [
- {
- id: "img-1",
- kind: "image",
- filename: "demo.png",
- mimeType: "image/png",
- dataBase64: "AQID",
- },
- ],
- }),
- ).toEqual([
- expect.objectContaining({
- text: "",
- attachments: [
- expect.objectContaining({
- id: "img-1",
- kind: "image",
- dataBase64: "AQID",
- }),
- ],
- }),
- ]);
- });
- it("parses structured elicitation responses without text", () => {
- const provider = createWebhookBotProvider({
- loadCredential: vi.fn(async () => "secret"),
- });
- expect(
- provider.parseCallback({
- type: "zcode.bot.elicitation_response",
- botId: "webhook-1",
- userId: "user-1",
- requestId: "elicit-1",
- action: "accept",
- content: { answer: "yes" },
- }),
- ).toEqual([
- expect.objectContaining({
- text: "",
- elicitationResponse: {
- requestId: "elicit-1",
- action: "accept",
- content: { answer: "yes" },
- },
- }),
- ]);
- });
- it("posts outbound replies to the configured webhook URL with the bot secret", async () => {
- const fetchMock = vi.fn(async () => new Response("ok", { status: 200 }));
- vi.stubGlobal("fetch", fetchMock);
- const provider = createWebhookBotProvider({
- loadCredential: vi.fn(async () => "secret"),
- });
- await provider.send(
- {
- id: "webhook-1",
- name: "Webhook",
- provider: "webhook",
- enabled: true,
- webhookUrl: "https://example.test/zcode",
- webhookSecretRef: "secret-ref",
- allowedWorkspaces: ["*"],
- allowedCommands: {
- status: true,
- new: true,
- workspace: true,
- model: true,
- thoughtLevel: true,
- sandboxMode: true,
- approvalPolicy: true,
- reply: true,
- },
- currentOptions: {},
- replyMode: "assistant_changes",
- },
- {
- botId: "webhook-1",
- provider: "webhook",
- providerUserId: "user-1",
- text: "hello",
- },
- );
- expect(fetchMock).toHaveBeenCalledWith(
- "https://example.test/zcode",
- expect.objectContaining({
- method: "POST",
- headers: expect.objectContaining({
- "content-type": "application/json",
- "x-zcode-bot-secret": "secret",
- }),
- }),
- );
- const body = JSON.parse(String(fetchMock.mock.calls[0]?.[1]?.body)) as { type?: string; text?: string };
- expect(body).toMatchObject({ type: "zcode.bot.message", text: "hello" });
- });
- it("posts structured elicitation requests to webhook URLs", async () => {
- const fetchMock = vi.fn(async () => new Response("ok", { status: 200 }));
- vi.stubGlobal("fetch", fetchMock);
- const provider = createWebhookBotProvider({
- loadCredential: vi.fn(async () => "secret"),
- });
- await provider.send(
- {
- id: "webhook-1",
- name: "Webhook",
- provider: "webhook",
- enabled: true,
- webhookUrl: "https://example.test/zcode",
- webhookSecretRef: "secret-ref",
- allowedWorkspaces: ["*"],
- allowedCommands: {
- status: true,
- new: true,
- workspace: true,
- model: true,
- thoughtLevel: true,
- sandboxMode: true,
- approvalPolicy: true,
- reply: true,
- },
- currentOptions: {},
- replyMode: "assistant_changes",
- },
- {
- botId: "webhook-1",
- provider: "webhook",
- providerUserId: "user-1",
- text: "Question",
- elicitation: {
- requestId: "elicit-1",
- taskId: "task-1",
- runId: "trace-1",
- currentQuestionIndex: 0,
- questions: [
- {
- question: "Pick?",
- header: "Pick",
- options: [{ value: "yes", label: "Yes" }],
- },
- ],
- },
- },
- );
- const body = JSON.parse(String(fetchMock.mock.calls[0]?.[1]?.body)) as {
- type?: string;
- elicitation?: unknown;
- };
- expect(body.type).toBe("zcode.bot.elicitation_request");
- expect(body.elicitation).toMatchObject({ requestId: "elicit-1" });
- });
- });
|