Skip to content

"Vibe-Trading: Your Personal Trading Agent"

6.7 KB
Raw Blame
import { fireEvent, render, screen, waitFor } from "@testing-library/react";
import { Settings } from "../Settings";

const apiMock = vi.hoisted(() => ({
  getLLMSettings: vi.fn(),
  getDataSourceSettings: vi.fn(),
  getChannelStatus: vi.fn(),
  getChannelsConfig: vi.fn(),
  listLLMModels: vi.fn(),
  startChannels: vi.fn(),
  stopChannels: vi.fn(),
  putChannelConfig: vi.fn(),
  testChannel: vi.fn(),
  updateLLMSettings: vi.fn(),
  updateDataSourceSettings: vi.fn(),
  listCustomProviders: vi.fn().mockResolvedValue({status: "ok", providers: []}),
}));

vi.mock("@/lib/api", async () => {
  const actual = await vi.importActual<typeof import("@/lib/api")>("@/lib/api");
  return {
    ...actual,
    api: apiMock,
    isAuthRequiredError: vi.fn(() => false),
  };
});

vi.mock("@/lib/apiAuth", () => ({
  getApiAuthKey: vi.fn(() => ""),
  setApiAuthKey: vi.fn(),
}));

function llmSettings() {
  return {
    provider: "openrouter",
    model_name: "deepseek/deepseek-v3.2",
    base_url: "https://openrouter.ai/api/v1",
    api_key_env: "OPENROUTER_API_KEY",
    api_key_configured: false,
    api_key_required: true,
    temperature: 0.1,
    timeout_seconds: 120,
    max_retries: 2,
    reasoning_effort: "",
    sse_timeout_seconds: 300,
    env_path: "agent/.env",
    providers: [
      {
        name: "openrouter",
        label: "OpenRouter",
        api_key_env: "OPENROUTER_API_KEY",
        base_url_env: "OPENROUTER_BASE_URL",
        default_model: "deepseek/deepseek-v3.2",
        default_base_url: "https://openrouter.ai/api/v1",
        api_key_required: true,
        auth_type: "api_key",
      },
    ],
  };
}

function dataSourceSettings() {
  return {
    tushare_token_configured: false,
    baostock_supported: true,
    baostock_installed: true,
    baostock_message: "BaoStock available",
    env_path: "agent/.env",
  };
}

function channelStatus(overrides = {}) {
  return {
    running: false,
    inbound_queue: 0,
    outbound_queue: 0,
    session_count: 0,
    channels: {
      websocket: {
        name: "websocket",
        display_name: "WebSocket",
        configured: true,
        enabled: true,
        available: true,
        loaded: true,
        running: false,
        error: "",
        install_hint: "",
      },
      telegram: {
        name: "telegram",
        display_name: "Telegram",
        configured: true,
        enabled: false,
        available: false,
        loaded: false,
        running: false,
        error: "ModuleNotFoundError",
        install_hint: "pip install 'vibe-trading-ai[telegram]'",
      },
    },
    ...overrides,
  };
}

function channelsConfig() {
  return {
    config_path: "agent/agent.json",
    writable: true,
    runtime_running: false,
    channels: {
      dingtalk: {
        display_name: "DingTalk",
        available: true,
        loaded: true,
        install_hint: "",
        error: "",
        supports_test: true,
        sdk_available: true,
        fields: [],
        values: { enabled: false },
        secrets: {},
      },
    },
  };
}

describe("Settings IM channels panel", () => {
  beforeEach(() => {
    apiMock.getLLMSettings.mockResolvedValue(llmSettings());
    apiMock.getDataSourceSettings.mockResolvedValue(dataSourceSettings());
    apiMock.getChannelStatus.mockResolvedValue(channelStatus());
    apiMock.getChannelsConfig.mockResolvedValue(channelsConfig());
    apiMock.listLLMModels.mockResolvedValue({
      provider: "openrouter",
      models: ["deepseek/deepseek-v3.2"],
      source: "default",
      warning_code: "api_key_required",
    });
    apiMock.startChannels.mockResolvedValue(channelStatus({ running: true }));
    apiMock.stopChannels.mockResolvedValue(channelStatus());
  });

  it("renders channel runtime status and refreshes it", async () => {
    render(<Settings />);

    expect(await screen.findByText("IM Channels")).toBeInTheDocument();
    expect(await screen.findByText("websocket")).toBeInTheDocument();
    expect(screen.getByText("telegram")).toBeInTheDocument();
    expect(screen.getByText("pip install 'vibe-trading-ai[telegram]'")).toBeInTheDocument();

    fireEvent.click(screen.getByRole("button", { name: "Refresh" }));

    await waitFor(() => expect(apiMock.getChannelStatus).toHaveBeenCalledTimes(2));
  });

  it("starts channels from the settings control surface", async () => {
    render(<Settings />);
    // Start stays disabled until the runtime status lands, so wait for the row
    // the status request renders before clicking.
    await screen.findByText("websocket");

    fireEvent.click(screen.getByRole("button", { name: "Start channels" }));

    await waitFor(() => expect(apiMock.startChannels).toHaveBeenCalledTimes(1));
  });

  it("still renders LLM and data source settings when channel status fails", async () => {
    apiMock.getChannelStatus.mockRejectedValue(
      new Error('Expected JSON from /channels/status, got text/html: <!doctype html>'),
    );

    render(<Settings />);

    expect(await screen.findByText("LLM Settings")).toBeInTheDocument();
    expect(screen.getByText("Data Source Settings")).toBeInTheDocument();
    expect(screen.getByText("IM Channels")).toBeInTheDocument();
    expect(screen.getByRole("button", { name: "Refresh" })).toBeInTheDocument();
    expect(screen.getByRole("button", { name: "Start channels" })).toBeDisabled();
  });

  it("renders SDK-managed provider authentication without editable transport fields", async () => {
    apiMock.getLLMSettings.mockResolvedValue({
      ...llmSettings(),
      provider: "copilot",
      model_name: "claude-sonnet-5",
      base_url: "https://api.githubcopilot.com",
      api_key_env: "COPILOT_GITHUB_TOKEN",
      api_key_required: false,
      providers: [
        {
          name: "copilot",
          label: "GitHub Copilot SDK",
          api_key_env: "COPILOT_GITHUB_TOKEN",
          base_url_env: "COPILOT_BASE_URL",
          default_model: "claude-sonnet-5",
          default_base_url: "https://api.githubcopilot.com",
          api_key_required: false,
          auth_type: "gh_cli",
          login_command: "gh auth login",
        },
      ],
    });

    render(<Settings />);

    expect(
      await screen.findByDisplayValue("https://api.githubcopilot.com"),
    ).toBeDisabled();
    const authStatus = screen.getByText(
      "This provider manages authentication. Run: gh auth login",
    );
    expect(authStatus.closest("label")?.querySelector("input")).toBeDisabled();
  });

  it("translates stable model-discovery warning codes in the frontend", async () => {
    render(<Settings />);
    await screen.findByText("LLM Settings");

    fireEvent.click(screen.getByRole("button", { name: "Load models" }));

    expect(await screen.findByText(
      "Enter or save this provider's API key to load its available models.",
    )).toBeInTheDocument();
  });
});