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, useState } from "react";
import { createRoot, type Root } from "react-dom/client";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { opensOnPress, returnMenuFocus, useMenuPress, type MenuPress } from "./menu-press";
(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
const toggles: MenuPress[] = [];
function Trigger() {
const [open, setOpen] = useState(false);
const press = useMenuPress((how) => { toggles.push(how); setOpen((value) => !value); });
return createElement("button", { type: "button", "aria-expanded": open, ...press.triggerProps }, "Add");
}
let host: HTMLDivElement;
let root: Root;
const button = () => host.querySelector("button")!;
const down = (pointerType = "mouse", extra: PointerEventInit = {}) =>
act(async () => { button().dispatchEvent(new PointerEvent("pointerdown", { bubbles: true, pointerType, button: 0, ...extra })); });
const click = (detail = 1) => act(async () => { button().dispatchEvent(new MouseEvent("click", { bubbles: true, detail })); });
beforeEach(async () => {
toggles.length = 0;
host = document.createElement("div");
document.body.append(host);
root = createRoot(host);
await act(async () => root.render(createElement(Trigger)));
});
afterEach(async () => {
await act(async () => root.unmount());
host.remove();
});
describe("a menu trigger's press", () => {
it("toggles on a mouse press and spends that press's click", async () => {
await down();
expect(button().getAttribute("aria-expanded")).toBe("true");
await click();
expect(button().getAttribute("aria-expanded")).toBe("true");
expect(toggles).toEqual(["pointer"]);
});
it("toggles on the click for a tap, a pen, a modified press, or a click with no press", async () => {
for (const init of [{ pointerType: "touch" }, { pointerType: "pen" }, { pointerType: "mouse", ctrlKey: true }]) {
await down(init.pointerType, init);
await click();
}
await click();
expect(toggles).toEqual(["click", "click", "click", "click"]);
});
it("reports Enter and Space (a click with detail 0) as the keyboard, even after a press that never clicked", async () => {
await down();
await click(0);
expect(toggles).toEqual(["pointer", "keyboard"]);
});
it("keeps the mouse's focus change off the trigger only for the press that opens a menu which focuses itself", async () => {
function Focusing() {
const [open, setOpen] = useState(false);
const press = useMenuPress(() => setOpen((value) => !value), { focusesMenu: true, open });
return createElement("button", { type: "button", "aria-expanded": open, ...press.triggerProps }, "Model");
}
await act(async () => root.render(createElement(Focusing)));
const mousedown = () => {
const event = new MouseEvent("mousedown", { bubbles: true, cancelable: true });
button().dispatchEvent(event);
return event.defaultPrevented;
};
await down();
expect(mousedown()).toBe(true);
await click();
await down();
expect(button().getAttribute("aria-expanded")).toBe("false");
expect(mousedown()).toBe(false);
});
it("only takes a plain primary mouse press", () => {
const base = { pointerType: "mouse", button: 0, ctrlKey: false, metaKey: false, shiftKey: false, altKey: false };
expect(opensOnPress(base)).toBe(true);
expect(opensOnPress({ ...base, button: 2 })).toBe(false);
expect(opensOnPress({ ...base, metaKey: true })).toBe(false);
expect(opensOnPress({ ...base, pointerType: "touch" })).toBe(false);
});
});
describe("focus back on the trigger", () => {
it("keeps the ring off after the mouse, until the next key or blur", () => {
returnMenuFocus(button(), "pointer");
expect(document.activeElement).toBe(button());
expect(button().hasAttribute("data-quiet-focus")).toBe(true);
button().dispatchEvent(new KeyboardEvent("keydown", { key: "Tab", bubbles: true }));
expect(button().hasAttribute("data-quiet-focus")).toBe(false);
returnMenuFocus(button(), "click");
button().blur();
expect(button().hasAttribute("data-quiet-focus")).toBe(false);
});
it("leaves the ring after the keyboard, or when focus had gone into the menu", () => {
returnMenuFocus(button(), "keyboard");
expect(button().hasAttribute("data-quiet-focus")).toBe(false);
const menu = document.createElement("div");
const row = document.createElement("button");
menu.append(row);
document.body.append(menu);
row.focus();
returnMenuFocus(button(), "pointer", menu);
expect(document.activeElement).toBe(button());
expect(button().hasAttribute("data-quiet-focus")).toBe(false);
menu.remove();
});
});