Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

24.1 KB
Raw Blame
import { Children, createElement, isValidElement, type ReactElement, type ReactNode } from "react";
import { renderToStaticMarkup } from "react-dom/server";
import { afterEach, describe, expect, it, vi } from "vitest";
import * as store from "@/state/store";
import { BuiltInReplyWait, modelIds, originChanged, OtherOpenAiCompatServers, LmStudioServer, lmStudioBaseUrl } from "./OpenAiCompatServers";

const fixture = vi.hoisted(() => ({ counting: false, values: [] as unknown[], index: 0, api: vi.fn(), track: vi.fn(), advanced: false }));
vi.mock("react", async (original) => {
  const react = await original<typeof import("react")>();
  return {
    ...react,
    useState: (initial: unknown) => {
      if (!fixture.counting) return react.useState(initial);
      const index = fixture.index++;
      if (!(index in fixture.values)) fixture.values[index] = initial;
      return [fixture.values[index], (next: unknown) => { fixture.values[index] = next; }];
    },
  };
});
vi.mock("@/state/store", async (original) => ({ ...await original<typeof import("@/state/store")>(), api: fixture.api }));
vi.mock("@/lib/analytics", () => ({ track: fixture.track }));
vi.mock("@/lib/interface-mode", () => ({ useAdvancedMode: () => fixture.advanced }));

afterEach(() => { vi.unstubAllGlobals(); vi.restoreAllMocks(); fixture.advanced = false; });

const instance = (instanceId: string, displayName: string, extra: Record<string, unknown> = {}) => ({
  instanceId, driverKind: "openai-compat", displayName,
  snapshot: { state: "available" }, models: { default: "m", options: [] }, ...extra,
}) as unknown as store.InstanceInfo;

function render(instances: store.InstanceInfo[]) {
  vi.stubGlobal("window", {});
  vi.spyOn(store, "useStore").mockReturnValue({
    state: { ...store.initialState, instances },
    dispatch: vi.fn(),
    flushBotPatches: vi.fn(),
    refreshInstances: vi.fn(),
    refreshModels: vi.fn(),
  });
  return renderToStaticMarkup(createElement(OtherOpenAiCompatServers));
}

describe("other OpenAI-compatible servers", () => {
  it("lists only servers added here, by name and base URL", () => {
    const html = render([
      instance("openaiCompat", "Other (OpenAI-compatible)"),
      instance("openai", "OpenAI", { url: "https://api.openai.com/v1" }),
      instance("openai-compat-1", "Zhipu", { url: "https://open.bigmodel.cn/api/paas/v4" }),
    ]);
    expect(html).toContain("Zhipu");
    expect(html).toContain("https://open.bigmodel.cn/api/paas/v4");
    expect(html).toContain('aria-label="Remove Zhipu"');
    expect(html).not.toContain("api.openai.com");
    expect(html).not.toContain('aria-label="Remove OpenAI"');
  });

  it("says so when none are added", () => {
    const html = render([instance("openaiCompat", "Other (OpenAI-compatible)")]);
    expect(html).toContain("No other servers yet.");
  });

  it("says a bot on this engine has no terminal and no files of its own", () => {
    const html = render([instance("openaiCompat", "Other (OpenAI-compatible)")]);
    expect(html).toContain("no terminal and no files of their own");
    expect(html).toContain("Claude or Codex custom endpoint");
  });

  it("reads model IDs separated by commas, CJK commas or spaces", () => {
    expect(modelIds("glm-4.6, glm-5,glm-4.6、qwen3 ")).toEqual(["glm-4.6", "glm-5", "qwen3"]);
    expect(modelIds("  ")).toEqual([]);
  });
});

describe("adding another OpenAI-compatible server", () => {
  const KEY = "sk-ZZtestSECRET0123456789";
  type Props = { children?: ReactNode; servers?: unknown; type?: string; onChange?: (event: { target: { value?: string; checked?: boolean } }) => void; onClick?: () => void; "aria-label"?: string };
  const nodes = (tree: ReactNode): Array<ReactElement<Props>> =>
    Children.toArray(tree).flatMap((child) => isValidElement<Props>(child) ? [child, ...nodes(child.props.children)] : []);
  const flush = async () => { for (let i = 0; i < 20; i++) await Promise.resolve(); };
  function form() {
    vi.stubGlobal("window", {});
    vi.spyOn(store, "useStore").mockReturnValue({
      state: { ...store.initialState, instances: [] }, dispatch: vi.fn(), flushBotPatches: vi.fn(), refreshInstances: vi.fn(), refreshModels: vi.fn(),
    });
    const added = nodes(OtherOpenAiCompatServers()).find((node) => node.props.servers !== undefined)!;
    fixture.counting = true;
    fixture.index = 0;
    try {
      return nodes((added.type as (props: Props) => ReactNode)(added.props));
    } finally {
      fixture.counting = false;
    }
  }
  async function add() {
    for (const [label, value] of [["Display name", "Ada's lab box"], ["Base URL", "https://ada.example.com/v1"], ["API key", KEY]] as const) {
      form().find((node) => node.props["aria-label"] === label)!.props.onChange!({ target: { value } });
    }
    form().find((node) => node.type === "button" && node.props.children && JSON.stringify(node.props.children).includes("Add server"))!.props.onClick!();
    await flush();
  }

  it.each([
    ["unverified", () => fixture.api.mockResolvedValueOnce({ instances: [] })],
    ["error", () => fixture.api.mockRejectedValueOnce(new store.ApiError(`unreachable https://ada.example.com/v1 ${KEY}`, 502, {}))],
  ])("reports one custom endpoint key save (%s), with no name, address or key", async (result, answer) => {
    fixture.values = [];
    fixture.track.mockReset();
    fixture.api.mockReset();
    answer();
    await add();
    expect(fixture.api).toHaveBeenCalledExactlyOnceWith("/api/instances/openai-compat", expect.objectContaining({ method: "POST" }));
    expect(fixture.track.mock.calls).toEqual([["api_key_saved", { provider: "custom_endpoint", via: "settings", result }]]);
    expect(JSON.stringify(fixture.track.mock.calls)).not.toMatch(/ada|example|ZZtest|SECRET/);
  });

  it("marks a server behind a tunnel as local only when the box is ticked, a box Advanced shows", async () => {
    const box = () => form().find((node) => node.type === "input" && node.props.type === "checkbox")!;
    expect(form().some((node) => node.type === "input" && node.props.type === "checkbox")).toBe(false);
    fixture.advanced = true;
    expect(JSON.stringify(form().map((node) => node.props.children ?? ""))).toContain("tunnel");
    fixture.values = [];
    fixture.api.mockReset();
    fixture.api.mockResolvedValue({ instances: [] });
    await add();
    expect(JSON.parse(String(fixture.api.mock.calls.at(-1)![1].body))).not.toHaveProperty("local");
    box().props.onChange!({ target: { checked: true } });
    await add();
    expect(JSON.parse(String(fixture.api.mock.calls.at(-1)![1].body))).toMatchObject({ local: true });
  });

  it("fills in Google Gemini, so only the AI Studio key is left to paste", async () => {
    fixture.values = [];
    fixture.api.mockReset();
    fixture.api.mockResolvedValueOnce({ instances: [] });
    const button = (text: string) => form().find((node) => node.type === "button" && JSON.stringify(node.props.children ?? "").includes(text))!;
    expect(JSON.stringify(form().map((node) => node.props.children ?? ""))).not.toContain("aistudio.google.com");
    button("Fill in Google Gemini").props.onClick!();
    expect(JSON.stringify(form().map((node) => node.props.children ?? ""))).toContain("aistudio.google.com/apikey");
    form().find((node) => node.props["aria-label"] === "API key")!.props.onChange!({ target: { value: "gemini-fixture" } });
    button("Add server").props.onClick!();
    await flush();
    expect(fixture.api).toHaveBeenCalledExactlyOnceWith("/api/instances/openai-compat", expect.objectContaining({ method: "POST" }));
    expect(JSON.parse(fixture.api.mock.calls[0][1].body)).toEqual({ displayName: "Google Gemini", url: "https://generativelanguage.googleapis.com/v1beta/openai", key: "gemini-fixture" });
  });
});

describe("how long a server may take to reply", () => {
  type Props = { children?: ReactNode; servers?: unknown; value?: unknown; onChange?: (event: { target: { value: string } }) => void; onPick?: unknown; "aria-label"?: string };
  const nodes = (tree: ReactNode): Array<ReactElement<Props>> =>
    Children.toArray(tree).flatMap((child) => isValidElement<Props>(child) ? [child, ...nodes(child.props.children)] : []);
  const waitPicker = (tree: ReactNode) => {
    const picker = nodes(tree).find((node) => node.props.onPick !== undefined)!;
    return (picker.type as (props: Props) => ReactElement<Props>)(picker.props);
  };
  function waitSelect(replyWaitMinutes?: number) {
    vi.stubGlobal("window", {});
    const servers = [instance("openai-compat-1", "Ollama", { url: "http://127.0.0.1:11434/v1", ...(replyWaitMinutes ? { replyWaitMinutes } : {}) })];
    vi.spyOn(store, "useStore").mockReturnValue({
      state: { ...store.initialState, instances: servers }, dispatch: vi.fn(), flushBotPatches: vi.fn(), refreshInstances: vi.fn(), refreshModels: vi.fn(),
    });
    const added = nodes(OtherOpenAiCompatServers()).find((node) => node.props.servers !== undefined)!;
    fixture.counting = true;
    fixture.index = 0;
    try {
      return waitPicker((added.type as (props: Props) => ReactNode)(added.props));
    } finally {
      fixture.counting = false;
    }
  }

  function builtInSelect(url: string) {
    vi.stubGlobal("window", {});
    vi.spyOn(store, "useStore").mockReturnValue({
      state: { ...store.initialState, config: { openaiCompat: { url } } as unknown as store.ConfigStatus, instances: [instance("openaiCompat", "Other (OpenAI-compatible)")] },
      dispatch: vi.fn(), flushBotPatches: vi.fn(), refreshInstances: vi.fn(), refreshModels: vi.fn(),
    });
    fixture.counting = true;
    fixture.index = 0;
    try {
      const tree = BuiltInReplyWait();
      return tree && waitPicker(tree);
    } finally {
      fixture.counting = false;
    }
  }

  it("saves the chosen minutes for that server, and Default clears them", async () => {
    fixture.values = [];
    fixture.api.mockReset();
    fixture.api.mockResolvedValue({ instances: [] });
    const select = waitSelect();
    expect(select.props.value).toBe("");
    select.props.onChange!({ target: { value: "20" } });
    await Promise.resolve();
    expect(fixture.api).toHaveBeenLastCalledWith("/api/instances/openai-compat-1", { method: "PATCH", body: JSON.stringify({ replyWaitMinutes: 20 }) });
    fixture.values = [];
    waitSelect(20).props.onChange!({ target: { value: "" } });
    await Promise.resolve();
    expect(fixture.api).toHaveBeenLastCalledWith("/api/instances/openai-compat-1", { method: "PATCH", body: JSON.stringify({ replyWaitMinutes: null }) });
  });

  it("offers the same wait for the built-in server once its base URL is set, such as a local Ollama", async () => {
    fixture.values = [];
    fixture.api.mockReset();
    fixture.api.mockResolvedValue({ instances: [] });
    const select = builtInSelect("http://localhost:11434/v1")!;
    expect(select.props["aria-label"]).toBe("How long to wait for Other (OpenAI-compatible) to reply");
    expect(JSON.stringify(select.props.children)).toContain("Wait 30 min");
    select.props.onChange!({ target: { value: "30" } });
    await Promise.resolve();
    expect(fixture.api).toHaveBeenLastCalledWith("/api/instances/openaiCompat", { method: "PATCH", body: JSON.stringify({ replyWaitMinutes: 30 }) });
    fixture.values = [];
    expect(builtInSelect("")).toBeNull();
  });

  it("shows a saved wait that is not one of the usual choices", () => {
    fixture.values = [];
    const html = JSON.stringify(waitSelect(45).props.children);
    expect(html).toContain("Wait 45 min");
  });
});

describe("LM Studio and Bionic setup", () => {
  type Props = { children?: ReactNode; onChange?: (event: { target: { value: string } }) => void; onClick?: () => void; "aria-label"?: string };
  const elementNodes = (tree: ReactNode): Array<ReactElement<Props>> =>
    Children.toArray(tree).flatMap((child) => isValidElement<Props>(child) ? [child, ...elementNodes(child.props.children)] : []);
  it("builds the loopback address from the port alone", () => {
    expect(lmStudioBaseUrl("1234")).toBe("http://127.0.0.1:1234/v1");
    expect(lmStudioBaseUrl(" 8080 ")).toBe("http://127.0.0.1:8080/v1");
    expect(lmStudioBaseUrl("0")).toBeNull();
    expect(lmStudioBaseUrl("65536")).toBeNull();
    expect(lmStudioBaseUrl("12a")).toBeNull();
  });

  it("asks only for the port and an optional token", () => {
    vi.stubGlobal("window", {});
    vi.spyOn(store, "useStore").mockReturnValue({
      state: { ...store.initialState, instances: [] }, dispatch: vi.fn(), flushBotPatches: vi.fn(), refreshInstances: vi.fn(), refreshModels: vi.fn(),
    });
    const html = renderToStaticMarkup(createElement(LmStudioServer));
    expect(html).toContain("LM Studio (Bionic)");
    expect(html).toContain("Bionic is the current LM Studio app");
    expect(html).toContain('aria-label="Port"');
    expect(html).toContain('aria-label="API token (optional)"');
    expect(html).toContain("http://127.0.0.1:1234/v1");
    expect(html).not.toContain("Display name");
    expect(html).not.toContain("Model IDs");
    expect(html).not.toContain('aria-label="Base URL"');
  });

  it("adds LM Studio with the placeholder key when the token is empty", async () => {
    fixture.values = [];
    fixture.api.mockReset();
    fixture.track.mockReset();
    fixture.api.mockResolvedValueOnce({ instances: [] });
    vi.stubGlobal("window", {});
    vi.spyOn(store, "useStore").mockReturnValue({
      state: { ...store.initialState, instances: [] }, dispatch: vi.fn(), flushBotPatches: vi.fn(), refreshInstances: vi.fn(), refreshModels: vi.fn(),
    });
    const button = () => {
      fixture.counting = true;
      fixture.index = 0;
      try {
        return elementNodes(LmStudioServer()).find((node) => node.type === "button")!;
      } finally {
        fixture.counting = false;
      }
    };
    button().props.onClick!();
    for (let i = 0; i < 20; i++) await Promise.resolve();
    expect(fixture.api).toHaveBeenCalledExactlyOnceWith("/api/instances/openai-compat", {
      method: "POST",
      body: JSON.stringify({ displayName: "LM Studio", url: "http://127.0.0.1:1234/v1", key: "lm-studio", catalog: "lmstudio" }),
    });
    expect(JSON.stringify(fixture.track.mock.calls)).not.toContain("lm-studio");
  });

  it("sends a token the person typed", async () => {
    fixture.values = [];
    fixture.api.mockReset();
    fixture.api.mockResolvedValueOnce({ instances: [] });
    vi.stubGlobal("window", {});
    vi.spyOn(store, "useStore").mockReturnValue({
      state: { ...store.initialState, instances: [] }, dispatch: vi.fn(), flushBotPatches: vi.fn(), refreshInstances: vi.fn(), refreshModels: vi.fn(),
    });
    const fields = () => {
      fixture.counting = true;
      fixture.index = 0;
      try {
        return elementNodes(LmStudioServer());
      } finally {
        fixture.counting = false;
      }
    };
    fields().find((node) => node.props["aria-label"] === "API token (optional)")!.props.onChange!({ target: { value: "studio-token" } });
    fields().find((node) => node.type === "button")!.props.onClick!();
    for (let i = 0; i < 20; i++) await Promise.resolve();
    const body = JSON.parse(String(fixture.api.mock.calls[0][1].body)) as { key: string };
    expect(body.key).toBe("studio-token");
  });

  it("stays quiet when that port is already added", () => {
    vi.stubGlobal("window", {});
    vi.spyOn(store, "useStore").mockReturnValue({
      state: {
        ...store.initialState,
        instances: [instance("openai-compat-1", "LM Studio", { url: "http://127.0.0.1:1234/v1" })],
      },
      dispatch: vi.fn(), flushBotPatches: vi.fn(), refreshInstances: vi.fn(), refreshModels: vi.fn(),
    });
    const html = renderToStaticMarkup(createElement(LmStudioServer));
    expect(html).toContain("This server is already added.");
    expect(html).toContain("disabled");
  });
});

describe("changing an added server's model IDs", () => {
  type Props = { children?: ReactNode; servers?: unknown; value?: unknown; onChange?: (event: { target: { value: string } }) => void; onClick?: () => void; "aria-label"?: string };
  const nodes = (tree: ReactNode): Array<ReactElement<Props>> =>
    Children.toArray(tree).flatMap((child) => isValidElement<Props>(child) ? [child, ...nodes(child.props.children)] : []);
  const flush = async () => { for (let i = 0; i < 20; i++) await Promise.resolve(); };
  function row(managedModels?: string[], dispatch = vi.fn(), extra: Record<string, unknown> = {}) {
    vi.stubGlobal("window", {});
    const servers = [instance("openai-compat-1", "Zhipu", { url: "https://open.bigmodel.cn/api/paas/v4", ...(managedModels ? { managedModels } : {}), ...extra })];
    vi.spyOn(store, "useStore").mockReturnValue({
      state: { ...store.initialState, instances: servers }, dispatch, flushBotPatches: vi.fn(), refreshInstances: vi.fn(), refreshModels: vi.fn(),
    });
    const added = nodes(OtherOpenAiCompatServers()).find((node) => node.props.servers !== undefined)!;
    fixture.counting = true;
    fixture.index = 0;
    try {
      return nodes((added.type as (props: Props) => ReactNode)(added.props));
    } finally {
      fixture.counting = false;
    }
  }
  const labelled = (tree: Array<ReactElement<Props>>, label: string) => tree.find((node) => node.props["aria-label"] === label);
  const save = (tree: Array<ReactElement<Props>>) => tree.find((node) => node.type === "button" && JSON.stringify(node.props.children ?? "") === JSON.stringify("Save"))!;

  it("opens the saved IDs for editing and saves the new list", async () => {
    fixture.values = [];
    fixture.api.mockReset();
    const dispatch = vi.fn();
    const updated = [instance("openai-compat-1", "Zhipu", { managedModels: ["glm-5", "qwen3"] })];
    fixture.api.mockResolvedValue({ instances: updated });
    expect(labelled(row(["glm-4.6", "glm-5"]), "Model IDs for Zhipu")).toBeUndefined();
    labelled(row(["glm-4.6", "glm-5"]), "Edit Zhipu")!.props.onClick!();
    const field = labelled(row(["glm-4.6", "glm-5"]), "Model IDs for Zhipu")!;
    expect(field.props.value).toBe("glm-4.6, glm-5");
    field.props.onChange!({ target: { value: "glm-5,qwen3 glm-5" } });
    save(row(["glm-4.6", "glm-5"], dispatch)).props.onClick!();
    await flush();
    expect(fixture.api).toHaveBeenCalledExactlyOnceWith("/api/instances/openai-compat-1", { method: "PATCH", body: JSON.stringify({ models: ["glm-5", "qwen3"] }) });
    expect(dispatch).toHaveBeenCalledExactlyOnceWith({ type: "instances", instances: updated });
    expect(labelled(row(["glm-5", "qwen3"]), "Model IDs for Zhipu")).toBeUndefined();
  });

  it("starts empty when the server's models are detected, and an empty list goes back to detecting them", async () => {
    fixture.values = [];
    fixture.api.mockReset();
    fixture.api.mockResolvedValue({ instances: [] });
    labelled(row(), "Edit Zhipu")!.props.onClick!();
    expect(labelled(row(), "Model IDs for Zhipu")!.props.value).toBe("");
    save(row()).props.onClick!();
    await flush();
    expect(fixture.api).toHaveBeenCalledExactlyOnceWith("/api/instances/openai-compat-1", { method: "PATCH", body: JSON.stringify({ models: [] }) });
  });

  it("shows the model-ID limits next to the editor instead of sending a list the server refuses", async () => {
    fixture.values = [];
    fixture.api.mockReset();
    labelled(row(["glm-4.6"]), "Edit Zhipu")!.props.onClick!();
    labelled(row(["glm-4.6"]), "Model IDs for Zhipu")!.props.onChange!({ target: { value: Array.from({ length: 65 }, (_, i) => `m${i}`).join(" ") } });
    save(row(["glm-4.6"])).props.onClick!();
    await flush();
    expect(fixture.api).not.toHaveBeenCalled();
    const alert = row(["glm-4.6"]).find((node) => (node.props as { role?: string }).role === "alert");
    expect(alert?.props.children).toBe("Use at most 64 model IDs, each up to 200 characters.");
  });

  it("opens with the saved base URL and an empty key, and sends a new path on the same host without a key", async () => {
    fixture.values = [];
    fixture.api.mockReset();
    fixture.api.mockResolvedValue({ instances: [] });
    labelled(row(["glm-5"]), "Edit Zhipu")!.props.onClick!();
    const address = labelled(row(["glm-5"]), "Base URL for Zhipu")!;
    expect(address.props.value).toBe("https://open.bigmodel.cn/api/paas/v4");
    expect(labelled(row(["glm-5"]), "New API key for Zhipu")!.props.value).toBe("");
    address.props.onChange!({ target: { value: "https://open.bigmodel.cn/api/coding/paas/v4/" } });
    save(row(["glm-5"])).props.onClick!();
    await flush();
    expect(fixture.api).toHaveBeenCalledExactlyOnceWith("/api/instances/openai-compat-1", {
      method: "PATCH", body: JSON.stringify({ models: ["glm-5"], url: "https://open.bigmodel.cn/api/coding/paas/v4" }),
    });
  });

  it("asks for the key again before saving an address on another host", async () => {
    fixture.values = [];
    fixture.api.mockReset();
    fixture.api.mockResolvedValue({ instances: [] });
    labelled(row(["glm-5"]), "Edit Zhipu")!.props.onClick!();
    labelled(row(["glm-5"]), "Base URL for Zhipu")!.props.onChange!({ target: { value: "https://api.z.ai/api/paas/v4" } });
    expect((save(row(["glm-5"])).props as { disabled?: boolean }).disabled).toBe(true);
    expect(JSON.stringify(row(["glm-5"]).map((node) => node.props.children))).toContain("This address is on another server or protocol.");
    labelled(row(["glm-5"]), "New API key for Zhipu")!.props.onChange!({ target: { value: " sk-new " } });
    expect((save(row(["glm-5"])).props as { disabled?: boolean }).disabled).toBe(false);
    save(row(["glm-5"])).props.onClick!();
    await flush();
    expect(fixture.api).toHaveBeenCalledExactlyOnceWith("/api/instances/openai-compat-1", {
      method: "PATCH", body: JSON.stringify({ models: ["glm-5"], url: "https://api.z.ai/api/paas/v4", apiKey: "sk-new" }),
    });
  });

  it("asks for the key again when https becomes http on the same host", () => {
    fixture.values = [];
    fixture.api.mockReset();
    labelled(row(["glm-5"]), "Edit Zhipu")!.props.onClick!();
    labelled(row(["glm-5"]), "Base URL for Zhipu")!.props.onChange!({ target: { value: "http://open.bigmodel.cn/api/paas/v4" } });
    expect((save(row(["glm-5"])).props as { disabled?: boolean }).disabled).toBe(true);
    expect(JSON.stringify(row(["glm-5"]).map((node) => node.props.children))).toContain("This address is on another server or protocol.");
  });

  it("moves a server with Ollama's placeholder key to another host without asking for a key", async () => {
    fixture.values = [];
    fixture.api.mockReset();
    fixture.api.mockResolvedValue({ instances: [] });
    const ollama = { url: "http://127.0.0.1:11434", placeholderKey: true };
    labelled(row(undefined, vi.fn(), ollama), "Edit Zhipu")!.props.onClick!();
    labelled(row(undefined, vi.fn(), ollama), "Base URL for Zhipu")!.props.onChange!({ target: { value: "http://192.168.1.20:11434" } });
    expect((save(row(undefined, vi.fn(), ollama)).props as { disabled?: boolean }).disabled).toBe(false);
    save(row(undefined, vi.fn(), ollama)).props.onClick!();
    await flush();
    expect(fixture.api).toHaveBeenCalledExactlyOnceWith("/api/instances/openai-compat-1", {
      method: "PATCH", body: JSON.stringify({ models: [], url: "http://192.168.1.20:11434" }),
    });
  });

  it("keeps the editor open with one line when the new address's model list could not be read", async () => {
    fixture.values = [];
    fixture.api.mockReset();
    fixture.api.mockResolvedValue({ instances: [], warning: "Saved. The model list could not be read" });
    labelled(row(), "Edit Zhipu")!.props.onClick!();
    labelled(row(), "Base URL for Zhipu")!.props.onChange!({ target: { value: "https://open.bigmodel.cn/api/v5" } });
    save(row()).props.onClick!();
    await flush();
    const status = row().find((node) => (node.props as { role?: string }).role === "status");
    expect(status?.props.children).toMatch(/^Saved\. The model list could not be read at this address/);
    expect(labelled(row(), "Base URL for Zhipu")).toBeDefined();
  });
});

describe("originChanged", () => {
  it("compares scheme, host and port", () => {
    expect(originChanged("https://a.example/v1", "https://a.example/v2")).toBe(false);
    expect(originChanged("https://a.example/v1", "https://b.example/v1")).toBe(true);
    expect(originChanged("https://a.example/v1", "https://a.example:8443/v1")).toBe(true);
    expect(originChanged("https://a.example/v1", "http://a.example/v1")).toBe(true);
    expect(originChanged(undefined, "https://a.example/v1")).toBe(true);
    expect(originChanged("https://a.example/v1", "not a url")).toBe(false);
  });
});