Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
// @vitest-environment happy-dom
import { act, createElement } from "react";
import { createRoot, type Root } from "react-dom/client";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { setLocale } from "@/lib/i18n";
import { WEB_PANELS_KEY, setWebPanels } from "@/lib/web-panel-preferences";
import { parseWebPanels } from "@/lib/web-panels";
import { PinnedPanelsSettings } from "./PinnedPanelsSettings";
(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
const page = { botId: "b1", threadId: "t1", messageId: "m1" };
let root: Root;
const byLabel = (label: string) => document.querySelector<HTMLButtonElement>(`button[aria-label="${label}"]`)!;
const dialog = () => document.querySelector<HTMLElement>('[role="dialog"]')!;
const click = async (button: HTMLButtonElement) => { expect(button).toBeTruthy(); await act(async () => button.click()); };
const inputName = async (value: string) => {
const input = dialog().querySelector<HTMLInputElement>("input")!;
await act(async () => {
Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value")!.set!.call(input, value);
input.dispatchEvent(new Event("input", { bubbles: true }));
});
};
const saved = () => parseWebPanels(localStorage.getItem(WEB_PANELS_KEY));
beforeEach(async () => {
setLocale("en");
localStorage.clear();
setWebPanels([{ id: "panel-1", name: "Report", page }, { id: "panel-2", name: "CI", url: "https://ci.example/" }]);
const host = document.createElement("div"); document.body.append(host);
root = createRoot(host);
await act(async () => root.render(createElement(PinnedPanelsSettings)));
});
afterEach(async () => { await act(async () => root.unmount()); document.body.innerHTML = ""; });
describe("renaming a pinned panel", () => {
it("keeps the existing add-page flow working", async () => {
await click([...document.querySelectorAll("button")].find((button) => button.textContent === "Add page")!);
const [name, url] = [...dialog().querySelectorAll("input")];
await act(async () => {
for (const [input, value] of [[name!, "Dashboard"], [url!, "https://dashboard.example/"]] as const) {
Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value")!.set!.call(input, value);
input.dispatchEvent(new Event("input", { bubbles: true }));
}
});
await click([...dialog().querySelectorAll("button")].find((button) => button.textContent === "Pin page")!);
expect(saved().at(-1)).toMatchObject({ name: "Dashboard", url: "https://dashboard.example/" });
});
it("saves a new name and retains the chat page reference and other panels", async () => {
await click(byLabel("Rename Report"));
expect(dialog().querySelector<HTMLInputElement>("input")!.value).toBe("Report");
await inputName(" Delivery ");
await click([...dialog().querySelectorAll("button")].find((button) => button.textContent === "Rename")!);
expect(saved()).toEqual([{ id: "panel-1", name: "Delivery", page }, { id: "panel-2", name: "CI", url: "https://ci.example/" }]);
expect(byLabel("Rename Delivery")).toBeTruthy();
await act(async () => { root.unmount(); });
const host = document.createElement("div"); document.body.append(host); root = createRoot(host);
await act(async () => root.render(createElement(PinnedPanelsSettings)));
expect(byLabel("Rename Delivery")).toBeTruthy();
});
it.each([
["a composing Enter", { isComposing: true }],
["an Enter with the IME key code", { keyCode: 229 }],
] as const)("keeps the draft after %s and saves on ordinary Enter", async (_label, init) => {
await click(byLabel("Rename Report"));
const input = dialog().querySelector<HTMLInputElement>("input")!;
input.focus();
input.dispatchEvent(new CompositionEvent("compositionstart", { bubbles: true }));
await inputName("配信");
const enter = (options: KeyboardEventInit = {}) => act(async () => {
input.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true, cancelable: true, ...options }));
});
await enter(init);
expect(dialog()).toContain(input);
expect(input.value).toBe("配信");
expect(saved()[0]).toEqual({ id: "panel-1", name: "Report", page });
input.dispatchEvent(new CompositionEvent("compositionend", { bubbles: true, data: "配信" }));
await enter();
expect(dialog()).toBeNull();
expect(saved()[0]).toEqual({ id: "panel-1", name: "配信", page });
});
it("renames a web URL without losing an edit made while the dialog was open", async () => {
await click(byLabel("Rename CI"));
await act(async () => setWebPanels([...saved(), { id: "panel-3", name: "Notes", url: "https://notes.example/" }]));
await inputName("Builds");
await click([...dialog().querySelectorAll("button")].find((button) => button.textContent === "Rename")!);
expect(saved()).toEqual([
{ id: "panel-1", name: "Report", page },
{ id: "panel-2", name: "Builds", url: "https://ci.example/" },
{ id: "panel-3", name: "Notes", url: "https://notes.example/" },
]);
});
it("keeps the old name on invalid input or cancel", async () => {
await click(byLabel("Rename Report"));
await inputName(" ");
await click([...dialog().querySelectorAll("button")].find((button) => button.textContent === "Rename")!);
expect(dialog().querySelector('[role="alert"]')).toBeTruthy();
expect(saved()[0]).toEqual({ id: "panel-1", name: "Report", page });
await inputName("Discard this");
await click([...dialog().querySelectorAll("button")].find((button) => button.textContent === "Cancel")!);
expect(saved()[0]).toEqual({ id: "panel-1", name: "Report", page });
});
});