Skip to content

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

3.5 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, describe, expect, it, vi } from "vitest";
import type { Bot, Task } from "@/state/store";
import { setThreadsUnfolded } from "@/lib/thread-fold-preferences";
import { setShowThreads } from "@/lib/thread-preferences";
import { SidebarThreadLabels, ThreadFoldChevron } from "./SidebarThreadLabels";

const now = Date.now();
const task = (threadId: string, title: string, hoursAgo: number, extra: Partial<Task> = {}): Task =>
  ({ threadId, title, createdAt: now - hoursAgo * 3_600_000, updatedAt: now - hoursAgo * 3_600_000, ...extra });
const research = (extra: Partial<Bot> = {}): Bot => ({
  id: "research", threadId: "pricing", name: "Research", title: "", description: "", notifications: true, color: "green",
  unread: false, modelSelection: { instanceId: "fake", model: "fake" }, messages: [],
  tasks: [task("pricing", "Competitor pricing", 1), task("home", "A long title for the conversation this bot always comes back to", 2), task("deck", "Board deck", 3)],
  ...extra,
});

let host: HTMLDivElement;
let root: Root;
const render = (bot: Bot) => flushSync(() => root.render(createElement(SidebarThreadLabels, { bot, selected: true, rowThreadId: bot.threadId, pendingQueued: {}, dispatch: vi.fn() })));
const marked = () => [...host.querySelectorAll("[data-main-thread-mark]")].map((mark) => mark.closest("[data-thread-label]")?.getAttribute("data-thread-label"));

beforeEach(() => {
  localStorage.clear();
  setThreadsUnfolded("research", true);
  host = document.createElement("div");
  document.body.append(host);
  root = createRoot(host);
});
afterEach(() => {
  flushSync(() => root.unmount());
  host.remove();
});

describe("a bot's threads unfolded under its row in Simple mode", () => {
  it("marks the bot's main thread, and no other row, with a mark a long title cannot push out", () => {
    render(research({ mainThreadId: "home" }));
    expect(marked()).toEqual(["home"]);
    const mark = host.querySelector("[data-main-thread-mark]")!;
    expect([mark.getAttribute("role"), mark.getAttribute("aria-label"), mark.textContent]).toEqual(["img", "Main thread", "Main"]);
    expect(mark.className).toContain("shrink-0");
    render(research({ mainThreadId: "deck" }));
    expect(marked()).toEqual(["deck"]);
    render(research());
    expect(marked()).toEqual([]);
  });
});

describe("threads in the sidebar, turned on while the app is on Simple", () => {
  const labels = () => [...host.querySelectorAll("[data-thread-label]")].map((row) => row.getAttribute("data-thread-label"));
  const chevron = (bot: Bot) => flushSync(() => root.render(createElement(ThreadFoldChevron, { bot, dispatch: vi.fn() })));

  it("lists every bot's threads under its row, with New thread where the fold was, and folds back only when turned off", () => {
    setThreadsUnfolded("research", false);
    render(research());
    expect(labels()).toEqual([]);
    chevron(research());
    expect(host.querySelector("[data-bot-threads-fold]")).not.toBeNull();

    flushSync(() => setShowThreads(true));
    render(research());
    expect(labels()).toEqual(["pricing", "home", "deck"]);
    chevron(research());
    expect([host.querySelector("[data-bot-threads-fold]"), host.querySelector("[data-bot-new-thread]")?.getAttribute("data-bot-new-thread")]).toEqual([null, "research"]);

    flushSync(() => setShowThreads(false));
    render(research());
    expect(labels()).toEqual([]);
  });
});