Skip to content

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

15.3 KB
Raw Blame
// @vitest-environment happy-dom
import { act, createElement } from "react";
import { createRoot, type Root } from "react-dom/client";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import type { Bot } from "@/state/store";

const fixture = vi.hoisted(() => ({
  api: vi.fn(),
  advanced: true,
  config: { tts: { configured: true, provider: "elevenlabs", voice: "" } } as Record<string, unknown>,
}));
vi.mock("@/state/store", async (importOriginal) => ({
  ...await importOriginal<typeof import("@/state/store")>(),
  api: fixture.api,
  useStore: () => ({ state: { config: fixture.config }, dispatch: vi.fn() }),
}));

vi.mock("@/lib/interface-mode", async (importOriginal) => ({
  ...await importOriginal<typeof import("@/lib/interface-mode")>(),
  useAdvancedMode: () => fixture.advanced,
}));

const { VoiceSettings } = await import("./VoiceSettings");

(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;

const LIBRARY = { id: "lib-1", label: "Library Rachel", description: "irish", publicOwnerId: "owner2" };
const bot = (id: string): Bot => ({
  id, threadId: "t", name: id, title: "", description: "", color: "green",
  notifications: true, unread: false, messages: [], modelSelection: { instanceId: "codex", model: "m" },
});

let host: HTMLDivElement;
let root: Root;
beforeEach(() => {
  fixture.config = { tts: { configured: true, provider: "elevenlabs", voice: "" } };
  fixture.advanced = true;
  document.documentElement.dataset.reducedMotion = "true";
  host = document.createElement("div");
  document.body.append(host);
  root = createRoot(host);
});
afterEach(() => {
  act(() => root.unmount());
  host.remove();
  fixture.api.mockReset();
});

function addLater() {
  let resolve!: (value: { voiceId: string }) => void;
  const added = new Promise<{ voiceId: string }>((done) => (resolve = done));
  fixture.api.mockImplementation((path: string) =>
    path === "/api/tts/voices/add" ? added : Promise.resolve({ voices: [LIBRARY] }));
  return resolve;
}

const render = (forBot: Bot, onPatch: (patch: Record<string, unknown>) => void) =>
  act(async () => root.render(createElement(VoiceSettings, { bot: forBot, onPatch })));
const trigger = () => host.querySelector<HTMLButtonElement>("[data-voice-picker]")!;

async function pickLibraryVoice() {
  await act(async () => trigger().click());
  const row = [...document.body.querySelectorAll<HTMLElement>('[role="option"]')].find((option) => option.textContent?.includes("Library Rachel"))!;
  await act(async () => row.click());
}

describe("VoiceSettings", () => {
  it("sets a library voice once ElevenLabs has added it, and says it is adding meanwhile", async () => {
    const resolve = addLater();
    const onPatch = vi.fn();
    await render(bot("pepper"), onPatch);
    await pickLibraryVoice();

    expect(fixture.api).toHaveBeenCalledWith("/api/tts/voices/add", expect.objectContaining({ method: "POST" }));
    expect(trigger().textContent).toContain("Adding…");
    await act(async () => resolve({ voiceId: "added-1" }));
    expect(onPatch).toHaveBeenCalledWith({ voice: "added-1" });
    expect(trigger().disabled).toBe(false);
  });

  it("drops an add that settles after the bot changed, so it never lands on the wrong bot", async () => {
    const resolve = addLater();
    const onPatch = vi.fn();
    await render(bot("pepper"), onPatch);
    await pickLibraryVoice();

    await render(bot("salt"), onPatch);
    expect(trigger().textContent).not.toContain("Adding…");
    await act(async () => resolve({ voiceId: "added-1" }));
    expect(onPatch).not.toHaveBeenCalled();
  });

  it("lets a bot override and then follow the workspace playback speed", async () => {
    fixture.api.mockResolvedValue({ voices: [] });
    const onPatch = vi.fn();
    await render({ ...bot("pepper"), voicePlaybackRate: 1.5 }, onPatch);
    const select = host.querySelector<HTMLSelectElement>("[data-bot-voice-playback-rate] select")!;
    expect(select.value).toBe("1.5");
    await act(async () => {
      select.value = "";
      select.dispatchEvent(new Event("change", { bubbles: true }));
    });
    expect(onPatch).toHaveBeenCalledExactlyOnceWith({ voicePlaybackRate: null });
  });

  it("offers Cartesia beside the other voice providers", async () => {
    fixture.api.mockResolvedValue({ voices: [] });
    await render(bot("pepper"), vi.fn());
    const radios = [...host.querySelectorAll<HTMLElement>('[role="radiogroup"] [role="radio"]')];
    expect(radios.map((radio) => radio.textContent)).toEqual(["ElevenLabs", "Fish Audio", "OpenAI", "Grok (xAI)", "Gemini", "Built-in Mac voices", "Chatterbox (local)", "DeepSeek", "Cartesia"]);
    expect(radios.map((radio) => radio.className.match(/col-span-\d/)?.[0])).toEqual([
      "col-span-1", "col-span-1", "col-span-1", "col-span-1", "col-span-1", "col-span-1", "col-span-1", "col-span-1", "col-span-1",
    ]);
  });
});


it("offers Drama 3 in the Fish Audio model picker, with no synthesis knobs", async () => {
  fixture.config = { tts: { configured: true, provider: "fish", voice: "fish-1" } };
  fixture.api.mockResolvedValue({ voices: [] });
  await render(bot("pepper"), vi.fn());
  const model = host.querySelector<HTMLSelectElement>('select[aria-label="Fish Audio model"]')!;
  expect([...model.options].find((option) => option.value === "drama-3")?.textContent).toBe("Drama 3");
  model.value = "drama-3";
  await act(async () => model.dispatchEvent(new Event("change", { bubbles: true })));
  expect(fixture.api).toHaveBeenCalledWith("/api/config", { method: "PUT", body: JSON.stringify({ tts: { fishModel: "drama-3" } }) });
  expect(host.querySelector('input[name="topP"]')).toBeNull();
});


it("keeps a new per-bot speed in Advanced mode, showing Simple mode one already set", async () => {
  fixture.advanced = false;
  fixture.api.mockResolvedValue({ voices: [] });
  await render(bot("pepper"), vi.fn());
  expect(host.querySelector("[data-bot-voice-playback-rate]")).toBeNull();
  await render({ ...bot("pepper"), voicePlaybackRate: 1.5 }, vi.fn());
  expect(host.querySelector<HTMLSelectElement>("[data-bot-voice-playback-rate] select")?.value).toBe("1.5");
});


describe("Cartesia voice settings", () => {
  it("saves Cartesia's key in its own field and picks an owned voice", async () => {
    fixture.config = { tts: { provider: "cartesia", configured: false, voice: "" } };
    fixture.api.mockResolvedValue({ tts: { provider: "cartesia", configured: true } });
    const onPatch = vi.fn();
    await render(bot("pepper"), onPatch);
    const input = host.querySelector<HTMLInputElement>('input[aria-label="Cartesia key"]')!;
    await act(async () => {
      Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value")!.set!.call(input, "fixture-cartesia-key");
      input.dispatchEvent(new Event("input", { bubbles: true }));
    });
    await act(async () => host.querySelector<HTMLButtonElement>('input[aria-label="Cartesia key"] + button')!.click());
    expect(fixture.api).toHaveBeenCalledWith("/api/config", { method: "PUT", body: JSON.stringify({ tts: { cartesiaKey: "fixture-cartesia-key" } }) });
    expect(input.value).toBe("");
    fixture.config = { tts: { provider: "cartesia", configured: true, voice: "" } };
    fixture.api.mockResolvedValue({ voices: [{ id: "cartesia-voice", label: "My Cartesia clone" }] });
    await render(bot("pepper"), onPatch);
    await act(async () => trigger().click());
    const clone = [...document.body.querySelectorAll<HTMLElement>('[role="option"]')].find((option) => option.textContent?.includes("My Cartesia clone"))!;
    await act(async () => clone.click());
    expect(onPatch).toHaveBeenCalledExactlyOnceWith({ voice: "cartesia-voice" });
    expect(fixture.api).not.toHaveBeenCalledWith("/api/tts/voices/add", expect.anything());
  });
});

describe("OpenAI voice settings", () => {
  const openaiRadio = () => [...host.querySelectorAll<HTMLButtonElement>('[role="radio"]')].find((radio) => radio.textContent === "OpenAI")!;
  it("selects marin only after the person chooses OpenAI, without enabling readout", async () => {
    fixture.api.mockResolvedValue({ tts: { provider: "openai", configured: true }, voices: [] });
    const onPatch = vi.fn();
    await render({ ...bot("pepper"), voice: "elevenlabs-voice" }, onPatch);
    expect(onPatch).not.toHaveBeenCalled();
    await act(async () => openaiRadio().click());
    expect(fixture.api).toHaveBeenCalledWith("/api/config", { method: "PUT", body: JSON.stringify({ tts: { provider: "openai" } }) });
    expect(onPatch).toHaveBeenCalledExactlyOnceWith({ voice: "marin" });
  });
  it("preserves an already selected OpenAI voice", async () => {
    fixture.api.mockResolvedValue({ voices: [] });
    const onPatch = vi.fn();
    await render({ ...bot("pepper"), voice: "cedar" }, onPatch);
    await act(async () => openaiRadio().click());
    expect(onPatch).not.toHaveBeenCalled();
  });
  it("does not assign a voice to another bot if it changes while saving the provider", async () => {
    let resolve!: (v: unknown) => void;
    fixture.api.mockImplementation((path) => path === "/api/config" ? new Promise((done) => { resolve = done; }) : Promise.resolve({ voices: [] }));
    const onPatch = vi.fn();
    await render(bot("pepper"), onPatch);
    await act(async () => openaiRadio().click());
    await render(bot("salt"), onPatch);
    await act(async () => resolve({ tts: { provider: "openai" } }));
    expect(onPatch).not.toHaveBeenCalled();
  });
  it("links a missing key to the existing API key settings and discloses billing and AI voices", async () => {
    fixture.config = { tts: { provider: "openai", configured: false } };
    fixture.api.mockResolvedValue({ voices: [] });
    await render(bot("pepper"), vi.fn());
    expect(host.textContent).toContain("AI-generated");
    expect(host.textContent).toContain("billed by OpenAI");
    expect(host.textContent).toContain("Live call settings are separate");
    expect(host.textContent).toContain("Add an OpenAI API key");
    expect(host.querySelector('input[type="password"]')).toBeNull();
    expect(fixture.api).not.toHaveBeenCalled();
  });
});


describe("DeepSeek voice settings", () => {
  const deepseekRadio = () => [...host.querySelectorAll<HTMLButtonElement>('[role="radio"]')].find((radio) => radio.textContent === "DeepSeek")!;
  const STELLA_TIDE = { voices: [{ id: "stella", label: "Stella (female)" }, { id: "tide", label: "Tide (male)" }] };

  it("chooses Stella for a bot whose voice belongs to another service", async () => {
    fixture.api.mockResolvedValue({ tts: { provider: "deepseek", configured: true }, voices: [] });
    const onPatch = vi.fn();
    await render({ ...bot("pepper"), voice: "elevenlabs-voice" }, onPatch);
    await act(async () => deepseekRadio().click());
    expect(fixture.api).toHaveBeenCalledWith("/api/config", { method: "PUT", body: JSON.stringify({ tts: { provider: "deepseek" } }) });
    expect(onPatch).toHaveBeenCalledExactlyOnceWith({ voice: "stella" });
  });

  it("keeps a saved Tide choice", async () => {
    fixture.api.mockResolvedValue({ voices: [] });
    const onPatch = vi.fn();
    await render({ ...bot("pepper"), voice: "tide" }, onPatch);
    await act(async () => deepseekRadio().click());
    expect(onPatch).not.toHaveBeenCalled();
  });

  it("offers Stella and Tide and saves the pick", async () => {
    fixture.config = { tts: { provider: "deepseek", configured: true, ready: true, voice: "stella" } };
    fixture.api.mockResolvedValue(STELLA_TIDE);
    const onPatch = vi.fn();
    await render({ ...bot("pepper"), voice: "stella" }, onPatch);
    const picker = host.querySelector<HTMLSelectElement>("select[data-voice-picker]")!;
    expect([...picker.options].map((option) => option.textContent)).toEqual(expect.arrayContaining(["Stella (female)", "Tide (male)"]));
    expect(picker.value).toBe("stella");
    await act(async () => { picker.value = "tide"; picker.dispatchEvent(new Event("change", { bubbles: true })); });
    expect(onPatch).toHaveBeenCalledWith({ voice: "tide" });
  });

  it("points a missing key at the existing DeepSeek key, says it also listens, and asks for no second key", async () => {
    fixture.config = { tts: { provider: "deepseek", configured: false } };
    fixture.api.mockResolvedValue({ voices: [] });
    await render(bot("pepper"), vi.fn());
    expect(host.textContent).toContain("Add a DeepSeek API key");
    expect(host.textContent).toContain("speech-to-text");
    expect(host.querySelector("[data-keys-link=\"deepseek\"]")).not.toBeNull();
    expect(host.querySelector('input[type="password"]')).toBeNull();
  });
});


it("sets the default voice even when the provider broadcast arrives before the save response", async () => {
  let resolve!: (v: unknown) => void;
  fixture.api.mockImplementation((path) => path === "/api/config" ? new Promise((done) => { resolve = done; }) : Promise.resolve({ voices: [] }));
  const onPatch = vi.fn();
  const pepper = bot("pepper");
  await render(pepper, onPatch);
  const radio = [...host.querySelectorAll<HTMLButtonElement>('[role="radio"]')].find((item) => item.textContent === "OpenAI")!;
  await act(async () => radio.click());
  fixture.config = { tts: { provider: "openai", configured: true } };
  await render(pepper, onPatch);
  await act(async () => resolve({ tts: { provider: "openai", configured: true } }));
  expect(onPatch).toHaveBeenCalledExactlyOnceWith({ voice: "marin" });
});

it("keeps a voice selected before the pending provider save completes, even before bot props update", async () => {
  let resolve!: (v: unknown) => void;
  fixture.api.mockImplementation((path) => path === "/api/config"
    ? new Promise((done) => { resolve = done; })
    : Promise.resolve({ voices: [{ id: "cedar", label: "Cedar" }, { id: "marin", label: "Marin" }] }));
  const onPatch = vi.fn();
  const pepper = bot("pepper");
  await render(pepper, onPatch);
  const radio = [...host.querySelectorAll<HTMLButtonElement>('[role="radio"]')].find((item) => item.textContent === "OpenAI")!;
  await act(async () => radio.click());
  fixture.config = { tts: { provider: "openai", configured: true } };
  await render(pepper, onPatch);
  const picker = host.querySelector<HTMLSelectElement>('select[data-voice-picker]')!;
  await act(async () => { picker.value = "cedar"; picker.dispatchEvent(new Event("change", { bubbles: true })); });
  await act(async () => resolve({ tts: { provider: "openai", configured: true } }));
  expect(onPatch).toHaveBeenCalledExactlyOnceWith({ voice: "cedar" });
});

it("lets a saved Chatterbox server address be cleared", async () => {
  fixture.config = { tts: { provider: "chatterbox", configured: true, baseUrl: "http://127.0.0.1:4123", model: "" } };
  fixture.api.mockResolvedValue({ tts: { provider: "chatterbox", configured: false, baseUrl: "", model: "" } });
  await render(bot("pepper"), vi.fn());
  const input = host.querySelector<HTMLInputElement>('input[aria-label="Chatterbox server address"]')!;
  const save = host.querySelector<HTMLButtonElement>('input[aria-label="Chatterbox server address"] + button')!;
  expect(input.value).toBe("http://127.0.0.1:4123");
  expect(save.disabled).toBe(false);
  await act(async () => {
    Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value")!.set!.call(input, "");
    input.dispatchEvent(new Event("input", { bubbles: true }));
  });
  expect(save.disabled).toBe(false);
  await act(async () => { save.click(); });
  expect(fixture.api).toHaveBeenCalledWith("/api/config", { method: "PUT", body: JSON.stringify({ tts: { baseUrl: "", model: "" } }) });
});