Skip to content

armgit / mso

Public Imported from GitHub
0

Browser-based graphical shell and control plane for a Linux server you own — real terminal, file manager, live metrics and BYOK AI in one mobile-first workspace. Next.js 16 · React 19 · node-pty · Tailwind v4 · shadcn/ui. Public alpha, self-hosted.

2.3 KB
Raw Blame
import { beforeEach, describe, expect, it, vi } from "vitest";
import { NextRequest } from "next/server";

const mocks = vi.hoisted(() => ({ receive: vi.fn(), dispatch: vi.fn(), rate: vi.fn() }));
vi.mock("@/lib/host/rate-limit", () => ({ rateLimitedUntrusted: mocks.rate }));
vi.mock("../../workflow-runtime", () => ({ channelWorkflowRuntime: () => ({ scope: "exec", resolveTool: () => undefined }) }));
vi.mock("@/lib/channels", () => {
  class ChannelError extends Error {
    constructor(public readonly code: string, public readonly status = 400) { super(code); }
  }
  return { ChannelError, receiveTelegram: mocks.receive, dispatchChannelInbound: mocks.dispatch };
});

const { POST } = await import("./route");
const request = () => new NextRequest("https://mso.example.test/api/v1/channels/telegram/11111111-1111-4111-8111-111111111111", {
  method: "POST",
  headers: { "content-type": "application/json", "x-telegram-bot-api-secret-token": "secret_123" },
  body: JSON.stringify({ update_id: 7 }),
});

beforeEach(() => {
  vi.clearAllMocks();
  mocks.rate.mockReturnValue(false);
  mocks.receive.mockResolvedValue({ provider: "telegram", eventId: "7", kind: "message", receivedAt: new Date().toISOString(), raw: {} });
  mocks.dispatch.mockResolvedValue({ workflow: { runId: "run-1", state: "running", graphId: "flow-1" } });
});

describe("Telegram channel webhook route", () => {
  it("dispatches only after provider verification returns an event", async () => {
    const response = await POST(request(), { params: Promise.resolve({ id: "11111111-1111-4111-8111-111111111111" }) });
    expect(response.status).toBe(200);
    expect(mocks.receive).toHaveBeenCalledWith("11111111-1111-4111-8111-111111111111", { update_id: 7 }, "secret_123");
    expect(mocks.dispatch).toHaveBeenCalledTimes(1);
    await expect(response.json()).resolves.toMatchObject({ ok: true, eventId: "7", workflow: { runId: "run-1" } });
  });

  it("preserves provider-native verification failures", async () => {
    const { ChannelError } = await import("@/lib/channels");
    mocks.receive.mockRejectedValue(new ChannelError("invalid_webhook_signature", 401));
    const response = await POST(request(), { params: Promise.resolve({ id: "11111111-1111-4111-8111-111111111111" }) });
    expect(response.status).toBe(401);
    expect(mocks.dispatch).not.toHaveBeenCalled();
  });
});