Skip to content

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

5.8 KB
Raw Blame
// @vitest-environment happy-dom
import { createElement } from "react";
import { flushSync } from "react-dom";
import { createRoot, type Root } from "react-dom/client";
import { afterEach, beforeEach, expect, it, vi } from "vitest";
import type { AppState, Bot, InstanceInfo, Message } from "@/state/store";

const viewer = vi.hoisted(() => ({ reactor: "p_ada" }));
vi.mock("./DesktopCapabilities", async (importOriginal) => ({
  ...await importOriginal<typeof import("./DesktopCapabilities")>(),
  useDesktopCapabilities: () => ({ capabilities: { dictation: { available: false }, host: { packaged: true, platform: "other" }, localComputer: { available: false } }, ready: true }),
}));
vi.mock("@/lib/analytics", () => ({ track: vi.fn() }));
vi.mock("@/lib/cloud-guest", () => ({ useCanWriteIn: () => true, useReactor: () => viewer.reactor }));
vi.mock("./ModelPicker", () => ({ ModelPicker: () => null }));

const { ChatView } = await import("./ChatView");
const { setColorUserBubbles } = await import("@/lib/user-bubble-preference");
const { BotEditorStore, initialState } = await import("@/state/store");
const { t } = await import("@/lib/i18n");
const { setAdvancedMode } = await import("@/lib/interface-mode");

let root: Root;
let container: HTMLDivElement;
beforeEach(() => {
  vi.stubGlobal("fetch", () => new Promise(() => {}));
  container = document.createElement("div");
  document.body.append(container);
  root = createRoot(container);
});
afterEach(() => {
  root.unmount();
  container.remove();
  vi.unstubAllGlobals();
});

const instances = [{ instanceId: "test", driverKind: "codex", displayName: "Test", snapshot: { state: "available" } } as InstanceInfo];
const team = { id: "team-acme", name: "Acme" };
const line = (id: string, sender: { name: string; id: string }, text: string): Message => ({ id, role: "user", kind: "text", at: 1, text, sender });
const luna = (messages: Message[], extra: Partial<Bot> = {}): Bot => ({
  id: "luna", threadId: "main", name: "Luna", title: "", description: "", color: "green", notifications: true, unread: false,
  busy: false, messages, modelSelection: { instanceId: "test", model: "m" },
  tasks: [{ threadId: "main", title: "Main", createdAt: 1, busy: false, activity: "idle", modelSelection: { instanceId: "test", model: "m" }, approvalMode: "ask" }],
  ...extra,
});
const draw = (bot: Bot, config: Partial<NonNullable<AppState["config"]>> = { team }) => {
  const state: AppState = { ...initialState, connected: true, selectedId: bot.id, bots: [bot], instances, config: config as AppState["config"] };
  const value = { state, dispatch: () => {}, flushBotPatches: async () => null, refreshInstances: async () => {}, refreshModels: async () => {} };
  flushSync(() => root.render(createElement(BotEditorStore, { value, children: createElement(ChatView, { bot }) })));
};
const row = (text: string) => [...container.querySelectorAll<HTMLElement>("[data-message-row]")].find((element) => element.textContent?.includes(text))!;

it("shows a teammate's line in a shared chat as theirs, with their name, and only the viewer's own in their bubble", () => {
  setColorUserBubbles(true);
  draw(luna([line("o1", { name: "Olive", id: "p_olive" }, "plant is memory-Fernando"), line("a1", { name: "Ada", id: "p_ada" }, "noted")]));
  const olive = row("plant is memory-Fernando");
  expect(olive.textContent).toContain("Olive");
  expect(olive.querySelector("[data-chat-bubble]")!.className).not.toContain("user-bubble-colored");
  const own = row("noted");
  expect(own.textContent).not.toContain("Ada");
  expect(own.querySelector("[data-chat-bubble]")!.className).toContain("user-bubble-colored");
});

it("names a teammate's line on a shared server without a team, and not the viewer's own", () => {
  draw(luna([line("o1", { name: "Olive", id: "p_olive" }, "plant is memory-Fernando"), line("a1", { name: "Ada", id: "p_ada" }, "noted")]), {});
  expect(row("plant is memory-Fernando").textContent).toContain("Olive");
  expect(row("noted").textContent).not.toContain("Ada");
});

it("sets a person's name in the same type as a bot's name", () => {
  draw(luna([line("o1", { name: "Olive", id: "p_olive" }, "plant is memory-Fernando")]), {});
  const name = [...row("plant is memory-Fernando").querySelectorAll("div")].find((element) => element.textContent === "Olive")!;
  expect(name.className).toContain("text-[13px]");
  expect(name.className).toContain("font-semibold");
  expect(name.className).toContain("text-ink-secondary");
});

it("says a member's own MausBot is theirs, not everyone's", () => {
  draw(luna([], { defaultBot: { person: "[email protected]" } } as Partial<Bot>));
  expect(container.textContent).not.toContain(t("team.sharedHeader", { team: "Acme" }));
  expect(container.textContent).toContain(t("team.ownHelperHeader"));
});

it.each([[false, "[data-chathead-privacy='shared']"], [true, "[data-chathead-shared]"]])("names who can read a bot's shared chat from the bot's own sharing (Advanced: %s)", (advanced, shown) => {
  setAdvancedMode(advanced);
  const said = () => container.querySelector(shown)?.getAttribute("title");
  draw(luna([]));
  expect(said()).toBe(t("team.sharedHeader", { team: "Acme" }));
  draw(luna([], { onlyPeopleAdded: true }));
  expect(said()).toBe(t("team.sharedHeaderPeople"));
  setAdvancedMode(false);
});

it("labels a tagged teammate's answer with its name, and leaves the chat's own bot unlabelled", () => {
  draw(luna([
    { id: "u1", role: "user", kind: "text", at: 1, text: "@Chief how much did we spend today?" },
    { id: "c1", role: "bot", kind: "text", at: 2, text: "$4.20 today.", from: { botId: "chief", name: "Chief", color: "red" }, roomRequest: { id: "h1", phase: "result" } },
    { id: "l1", role: "bot", kind: "text", at: 3, text: "Anything else?" },
  ]));
  const block = (id: string) => container.querySelector(`[data-mid="${id}"]`)!.textContent;
  expect(block("c1")).toContain("Chief");
  expect(block("l1")).not.toContain("Luna");
});