Skip to content

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

5.6 KB
Raw Blame
// @vitest-environment happy-dom
// A bot's own Live call voice, in a real DOM, on the bot's voice settings beside the read-aloud voice.
import { createElement } from "react";
import { flushSync } from "react-dom";
import { createRoot, type Root } from "react-dom/client";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { setLocale } from "@/lib/i18n";
import { BotEditorStore, initialState, type AppState, type Bot } from "@/state/store";
import type { LiveSettings } from "../../../shared/wire";
import { BotEditorContext } from "./BotEditorContext";
import { VoiceSection } from "./VoiceSection";
import type { useBotSettingsDerived } from "./useBotSettingsDerived";

let admin = false;
vi.mock("@/lib/use-owner-or-admin", () => ({ useOwnerOrAdmin: () => admin }));
let advanced = true;
vi.mock("@/lib/interface-mode", async (importOriginal) => ({ ...await importOriginal<typeof import("@/lib/interface-mode")>(), useAdvancedMode: () => advanced }));

const atlas: Bot = {
  id: "atlas", threadId: "thread-atlas", name: "Atlas", title: "", description: "", notifications: true, color: "green",
  unread: false, modelSelection: { instanceId: "claude", model: "test" }, messages: [],
};
const openAiLive: LiveSettings = { configured: true, voice: "marin", readTypedReplies: true, idleMinutes: 5 };

let host: HTMLDivElement;
let root: Root;
const patch = vi.fn();
const render = (live: LiveSettings | undefined, bot: Bot = atlas, draft = false) => {
  const value = { state: { ...initialState, config: { live } as AppState["config"] }, dispatch: vi.fn(), flushBotPatches: async () => null, refreshInstances: async () => {}, refreshModels: async () => {} };
  const derived = { patch } as unknown as ReturnType<typeof useBotSettingsDerived>;
  const section = createElement(VoiceSection, { bot, derived });
  const editor = createElement(BotEditorContext.Provider, { value: { request: vi.fn(), draft }, children: section });
  flushSync(() => root.render(createElement(BotEditorStore, { value, children: editor })));
};
const card = () => host.querySelector("[data-live-call-voice]");
const picker = () => card()?.querySelector("select") ?? null;

beforeEach(() => {
  setLocale("en");
  patch.mockReset();
  admin = false;
  advanced = true;
  host = document.createElement("div");
  document.body.append(host);
  root = createRoot(host);
});

afterEach(() => {
  flushSync(() => root.unmount());
  host.remove();
});

describe("a bot's Live call voice in its voice settings", () => {
  it("shows the bot's own call voice and saves a new pick to the same setting the call uses", () => {
    render(openAiLive, { ...atlas, liveVoice: "cedar" });
    expect(card()?.textContent).toContain("Live call voice");
    expect(card()?.textContent).toContain("Voice for Atlas");
    expect(picker()?.value).toBe("cedar");
    picker()!.value = "marin";
    flushSync(() => picker()!.dispatchEvent(new Event("change", { bubbles: true })));
    expect(patch).toHaveBeenCalledWith({ liveVoice: "marin" });
  });

  it("follows the default call voice until the bot has one of the provider's own", () => {
    render(openAiLive);
    expect(picker()?.value).toBe("");
    render({ ...openAiLive, provider: "gemini" }, { ...atlas, liveVoice: "cedar" });
    expect(picker()?.value).toBe("");
    expect([...picker()!.options].map((option) => option.value)).toContain("Kore");
  });

  it("is not there when Live calls are not set up, or the call speaks in its agent's own voice", () => {
    render(undefined);
    expect(card()).toBeNull();
    render({ ...openAiLive, configured: false });
    expect(card()).toBeNull();
    render({ ...openAiLive, provider: "fish" });
    expect(card()).toBeNull();
    render({ ...openAiLive, provider: "elevenlabs" });
    expect(card()).toBeNull();
  });

  it("lets an admin choose a Fish agent, a GPT-Live voice, or the workspace service", () => {
    admin = true;
    render(openAiLive);
    expect(picker()?.value).toBe("");
    picker()!.value = "fish";
    flushSync(() => picker()!.dispatchEvent(new Event("change", { bubbles: true })));
    expect(patch).toHaveBeenLastCalledWith({ liveOverride: { provider: "fish" } });

    render(openAiLive, { ...atlas, liveOverride: { provider: "fish" } });
    const agent = card()?.querySelector("input");
    expect(agent).toBeDefined();
    agent!.value = " fish-atlas ";
    flushSync(() => agent!.dispatchEvent(new Event("focusout", { bubbles: true })));
    expect(patch).toHaveBeenLastCalledWith({ liveOverride: { provider: "fish", fishAgentId: "fish-atlas" } });

    render({ ...openAiLive, provider: "fish" }, { ...atlas, liveOverride: { provider: "openai" }, liveVoice: "cedar" });
    expect(picker()?.value).toBe("openai");
    expect(card()?.querySelectorAll("select")[1]?.value).toBe("cedar");
    picker()!.value = "";
    flushSync(() => picker()!.dispatchEvent(new Event("change", { bubbles: true })));
    expect(patch).toHaveBeenLastCalledWith({ liveOverride: null });
  });

  it("keeps the call service choice in Advanced mode, showing Simple mode only a choice already made", () => {
    admin = true;
    advanced = false;
    render(openAiLive);
    expect(card()?.querySelectorAll("select")).toHaveLength(1);
    expect(card()?.textContent).not.toContain("Use the default call service");
    render(openAiLive, { ...atlas, liveOverride: { provider: "fish", fishAgentId: "fish-atlas" } });
    expect(picker()?.value).toBe("fish");
    expect(card()?.querySelector("input")?.value).toBe("fish-atlas");
  });

  it("is not on a bot being created, whose defaults keep no call voice", () => {
    render(openAiLive, atlas, true);
    expect(card()).toBeNull();
  });
});