Skip to content

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

2.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";

const fixture = vi.hoisted(() => ({ hidden: [] as Array<{ instanceId: string; displayName: string }>, calls: [] as Array<{ path: string; body?: unknown }>, refresh: vi.fn(), dispatch: vi.fn(), answer: [{ instanceId: "codex" }] as unknown[] }));
vi.mock("@/state/store", async (original) => ({
  ...await original<typeof import("@/state/store")>(),
  useStore: () => ({ state: { instances: [] }, dispatch: fixture.dispatch, refreshInstances: fixture.refresh }),
  api: async (path: string, init?: RequestInit) => {
    fixture.calls.push({ path, ...(init?.body ? { body: JSON.parse(String(init.body)) } : {}) });
    if (path === "/api/instances/hidden") return { instances: fixture.hidden };
    fixture.hidden = [];
    return { instances: fixture.answer };
  },
}));

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

let root: Root;
let host: HTMLElement;
const settle = () => new Promise((done) => setTimeout(done, 0));
beforeEach(() => {
  fixture.calls = [];
  fixture.refresh.mockReset();
  fixture.dispatch.mockReset();
  host = document.createElement("div");
  document.body.append(host);
  root = createRoot(host);
});
afterEach(() => { flushSync(() => root.unmount()); host.remove(); });

describe("hidden providers in Settings", () => {
  it("shows nothing while no provider is hidden", async () => {
    fixture.hidden = [];
    flushSync(() => root.render(createElement(HiddenEngines)));
    await settle();
    expect(host.querySelector("[data-hidden-engines]")).toBeNull();
  });

  it("lists hidden providers, folded, and unhides one with a click", async () => {
    fixture.hidden = [{ instanceId: "hermes", displayName: "Hermes" }, { instanceId: "pi", displayName: "pi" }];
    flushSync(() => root.render(createElement(HiddenEngines)));
    await settle();
    const list = host.querySelector("[data-hidden-engines]")!;
    expect(list.querySelector("summary")!.textContent).toBe("Hidden (2)");
    expect(list.textContent).toContain("Hermes");
    flushSync(() => host.querySelector<HTMLButtonElement>('[data-unhide-engine="hermes"]')!.click());
    await settle();
    expect(fixture.calls).toContainEqual({ path: "/api/instances/hermes", body: { hidden: false } });
    expect(fixture.refresh).not.toHaveBeenCalled();
    expect(fixture.dispatch).toHaveBeenCalledWith({ type: "instances", instances: fixture.answer });
  });
});