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 { 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 { setLocale } from "@/lib/i18n";
import type { ConfigStatus } from "@/state/store";
const fx = vi.hoisted(() => ({
saveKey: vi.fn(),
dispatch: vi.fn(),
}));
vi.mock("@/state/store", async (original) => {
const actual = await original<typeof import("@/state/store")>();
return {
...actual,
useStore: () => ({
state: { ...actual.initialState, config: {} },
dispatch: fx.dispatch,
refreshInstances: vi.fn(),
}),
};
});
vi.mock("./key-save", async (original) => {
const actual = await original<typeof import("./key-save")>();
return { ...actual, saveKey: fx.saveKey };
});
vi.mock("@/lib/analytics", () => ({ track: vi.fn() }));
const { KeyRow } = await import("./KeyRow");
let host: HTMLDivElement;
let root: Root;
beforeEach(() => {
setLocale("en");
fx.saveKey.mockReset();
fx.dispatch.mockReset();
fx.saveKey.mockResolvedValue({ anthropic: { configured: true, last4: "alf1" } } as ConfigStatus);
host = document.createElement("div");
document.body.append(host);
root = createRoot(host);
});
afterEach(() => {
flushSync(() => root.unmount());
host.remove();
vi.clearAllMocks();
});
function mount() {
flushSync(() => root.render(createElement(KeyRow, { id: "anthropic" })));
const input = host.querySelector<HTMLInputElement>("[data-key-input]")!;
flushSync(() => input.focus());
return input;
}
function type(input: HTMLInputElement, value: string) {
const setter = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value")!.set!;
flushSync(() => {
setter.call(input, value);
input.dispatchEvent(new Event("input", { bubbles: true }));
});
}
function press(input: HTMLInputElement, init: KeyboardEventInit) {
flushSync(() => input.dispatchEvent(new KeyboardEvent("keydown", { bubbles: true, cancelable: true, ...init })));
}
const IME = [
{ name: "composition", init: { isComposing: true } },
{ name: "IME key code", init: { keyCode: 229 } },
];
describe("saving a key with an input method", () => {
it.each(IME)("does not save on the Enter that belongs to the $name", async ({ init }) => {
const input = mount();
type(input, "sk-ant-half");
press(input, { key: "Enter", ...init });
await Promise.resolve();
await Promise.resolve();
expect(fx.saveKey).not.toHaveBeenCalled();
expect(input.value).toBe("sk-ant-half");
});
it("still saves on a plain Enter", async () => {
const input = mount();
type(input, "sk-ant-half");
press(input, { key: "Enter" });
await vi.waitFor(() => expect(fx.saveKey).toHaveBeenCalledOnce());
expect(fx.saveKey).toHaveBeenCalledWith(expect.anything(), "sk-ant-half", true);
});
});