Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

4.4 KB
Raw Blame
import { cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react";
import { afterEach, describe, expect, it, vi } from "vitest";

import { PatConnectDialog } from "@/views/PluginsView";

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function makePlugin(id: string): any {
  return {
    id,
    name: id,
    description: "d",
    category: "Communication",
    logoSlug: id,
    authMode: "pat_paste",
    authConfig: {
      mode: "pat_paste",
      token_creation_url: "https://example.test",
      token_prefix: "",
      instruction_md: "md",
    },
    status: "not_connected",
    featured: false,
    liveCallable: false,
  };
}

afterEach(cleanup);

describe("PatConnectDialog owner lock", () => {
  it("shows a numeric owner-id field for a channel plugin and forwards it", () => {
    const onSubmit = vi.fn();
    render(
      <PatConnectDialog
        plugin={makePlugin("discord")}
        onClose={() => {}}
        onSubmit={onSubmit}
        isPending={false}
        errorMessage={null}
      />,
    );

    const idField = screen.getByLabelText(/user id/i);
    fireEvent.change(screen.getByPlaceholderText(/^token$/i), {
      target: { value: "bot-token" },
    });
    fireEvent.change(idField, { target: { value: "4242" } });
    fireEvent.click(screen.getByRole("button", { name: /^Connect/i }));

    // The third argument is the self-hosted server address, null for a plugin
    // whose auth config declares no instance_url field.
    expect(onSubmit).toHaveBeenCalledWith("bot-token", 4242, null);
  });

  it("omits the owner-id field for non-channel plugins and submits null", () => {
    const onSubmit = vi.fn();
    render(
      <PatConnectDialog
        plugin={makePlugin("github")}
        onClose={() => {}}
        onSubmit={onSubmit}
        isPending={false}
        errorMessage={null}
      />,
    );

    expect(screen.queryByLabelText(/user id/i)).toBeNull();
    fireEvent.change(screen.getByPlaceholderText(/^token$/i), {
      target: { value: "ghp_x" },
    });
    fireEvent.click(screen.getByRole("button", { name: /^Connect/i }));

    expect(onSubmit).toHaveBeenCalledWith("ghp_x", null, null);
  });

  it("forwards null when the owner-id field is left blank", () => {
    const onSubmit = vi.fn();
    render(
      <PatConnectDialog
        plugin={makePlugin("telegram")}
        onClose={() => {}}
        onSubmit={onSubmit}
        isPending={false}
        errorMessage={null}
      />,
    );

    fireEvent.change(screen.getByPlaceholderText(/^token$/i), {
      target: { value: "123:ABC" },
    });
    fireEvent.click(screen.getByRole("button", { name: /^Connect/i }));

    expect(onSubmit).toHaveBeenCalledWith("123:ABC", null, null);
  });

  it("shows Discord browser helpers only for discord", () => {
    const onSubmit = vi.fn();
    render(
      <PatConnectDialog
        plugin={makePlugin("discord")}
        onClose={() => {}}
        onSubmit={onSubmit}
        isPending={false}
        errorMessage={null}
      />,
    );

    expect(
      screen.getByRole("button", { name: /Fill in my Discord ID/i }),
    ).toBeTruthy();
    expect(
      screen.getByRole("button", { name: /Add the bot to my server/i }),
    ).toBeTruthy();
    cleanup();

    render(
      <PatConnectDialog
        plugin={makePlugin("telegram")}
        onClose={() => {}}
        onSubmit={onSubmit}
        isPending={false}
        errorMessage={null}
      />,
    );

    expect(
      screen.queryByRole("button", { name: /Fill in my Discord ID/i }),
    ).toBeNull();
  });

  it("fills the owner id from the verified browser login", async () => {
    const fetchMock = vi.fn().mockResolvedValue({
      ok: true,
      json: () => Promise.resolve({ user_id: "4242", username: "alice" }),
    });
    vi.stubGlobal("fetch", fetchMock);
    try {
      render(
        <PatConnectDialog
          plugin={makePlugin("discord")}
          onClose={() => {}}
          onSubmit={() => {}}
          isPending={false}
          errorMessage={null}
        />,
      );

      fireEvent.click(screen.getByRole("button", { name: /Fill in my Discord ID/i }));

      await waitFor(() => {
        expect(
          (screen.getByLabelText(/user id/i) as HTMLInputElement).value,
        ).toBe("4242");
      });
      expect(fetchMock).toHaveBeenCalledWith(
        "/api/marketplace/plugins/discord/identity",
      );
    } finally {
      vi.unstubAllGlobals();
    }
  });
});