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
// "Sign in with Boat" above the Boat key field: each state it shows, what its buttons ask the server, and where it is
// not offered at all. The server and the browser are stand-ins: nothing here opens a page or reaches Boat.
import { act, createElement } from "react";
import { createRoot, type Root } from "react-dom/client";
import { afterEach, beforeEach, expect, it, vi } from "vitest";
import type { BoatSignInStatus } from "../../../shared/boat-sign-in";
import { setLocale } from "@/lib/i18n";
const api = vi.fn(async (path: string, _init?: { method?: string }): Promise<unknown> => (path.endsWith("/start") ? { url: "https://boat.dev/api/box/oauth2/authorize?client_id=x&state=y" } : { ok: true }));
vi.mock("@/state/api", () => ({ api }));
const openExternalLink = vi.fn(async (_url: string) => {});
vi.mock("@/lib/app-links", () => ({ openExternalLink }));
const { BoatSignIn } = await import("./BoatSignIn");
const { boatSignInShown, isBoatSignInUrl } = await import("@/lib/boat-sign-in");
const { keyEntry, savedState } = await import("./key-catalog");
(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
let host: HTMLDivElement, root: Root | null = null;
const NONE: BoatSignInStatus = { available: true, state: "none", keySaved: false };
const CONNECTED: BoatSignInStatus = { available: true, state: "connected", account: "[email protected]", missingScopes: [], used: true, keySaved: false };
beforeEach(() => { setLocale("en"); vi.clearAllMocks(); host = document.createElement("div"); document.body.append(host); });
afterEach(async () => { await act(async () => root?.unmount()); root = null; host.remove(); });
async function show(status: BoatSignInStatus, more: { fieldShown?: boolean; onConnected?: () => void } = {}) {
root ??= createRoot(host);
await act(async () => { root!.render(createElement(BoatSignIn, { status, fieldShown: more.fieldShown ?? true, onConnected: more.onConnected })); });
}
const click = async (element: Element | null) => { expect(element).toBeTruthy(); await act(async () => { (element as HTMLElement).click(); await Promise.resolve(); await Promise.resolve(); }); };
const text = () => host.textContent ?? "";
it("not connected: the button with Boat's mark first, then \"or paste an API key\"", async () => {
await show(NONE);
const button = host.querySelector("[data-boat-sign-in]")!;
expect(button.textContent).toBe("Sign in with Boat");
expect(button.querySelector("svg path")?.getAttribute("d")).toMatch(/^M490\.123 361\.461/);
expect(host.querySelector("[data-boat-or]")?.textContent).toBe("or paste an API key");
expect(host.querySelector("[data-boat-disconnect]")).toBeNull();
await click(button);
expect(api).toHaveBeenCalledWith("/api/boat/sign-in/start", { method: "POST" });
expect(openExternalLink).toHaveBeenCalledWith("https://boat.dev/api/box/oauth2/authorize?client_id=x&state=y");
});
it("opens nothing but Boat's own sign-in page, whatever the server answers", async () => {
api.mockResolvedValueOnce({ url: "https://boat.dev.evil.example/api/box/oauth2/authorize" });
await show(NONE);
await click(host.querySelector("[data-boat-sign-in]"));
expect(openExternalLink).not.toHaveBeenCalled();
expect(host.querySelector("[role=alert]")?.textContent).toBe("Couldn't open the sign-in page in your browser. Try again.");
for (const bad of ["https://boat.dev/other", "http://boat.dev/api/box/oauth2/authorize", "https://[email protected]/api/box/oauth2/authorize", "javascript:alert(1)", "https://boat.dev:444/api/box/oauth2/authorize", 7, null]) {
expect(isBoatSignInUrl(bad), String(bad)).toBe(false);
}
expect(isBoatSignInUrl("https://boat.dev/api/box/oauth2/authorize?state=a")).toBe(true);
});
it("waiting for Boat: says so, and Cancel spends the link", async () => {
await show({ ...NONE, pending: true });
expect(host.querySelector("[data-boat-sign-in-state]")?.getAttribute("data-boat-sign-in-state")).toBe("waiting");
expect(text()).toContain("Finish signing in in your browser");
expect(host.querySelector("[data-boat-sign-in]")).toBeNull();
await click(host.querySelector("[data-boat-cancel]"));
expect(api).toHaveBeenCalledWith("/api/boat/sign-in/cancel", { method: "POST" });
});
it("connected: the account and Disconnect, and the caller hears it once", async () => {
const onConnected = vi.fn();
await show({ ...NONE, pending: true }, { onConnected });
await show(CONNECTED, { onConnected });
await show(CONNECTED, { onConnected });
expect(onConnected).toHaveBeenCalledTimes(1);
expect(host.querySelector("[data-boat-account]")?.textContent).toBe("Signed in to Boat as [email protected]");
expect(host.querySelector("[data-boat-sign-in]")).toBeNull();
expect(host.querySelector("[data-boat-used]")).toBeNull();
await click(host.querySelector("[data-boat-disconnect]"));
expect(api).toHaveBeenCalledWith("/api/boat/sign-in", { method: "DELETE" });
});
it("a refused Disconnect shows the server's reason", async () => {
api.mockRejectedValueOnce(new Error("remove this installation's cloud computers before changing or clearing the Boat account"));
await show(CONNECTED);
await click(host.querySelector("[data-boat-disconnect]"));
expect(host.querySelector("[role=alert]")?.textContent).toContain("remove this installation's cloud computers");
});
it("no account name when the person did not allow it, and the scopes they left out are named", async () => {
await show({ ...CONNECTED, account: undefined, missingScopes: ["account.read", "sandbox.create"] });
expect(host.querySelector("[data-boat-account]")?.textContent).toBe("Signed in to Boat");
expect(host.querySelector("[data-boat-missing]")?.textContent).toBe("Not allowed when you signed in: account.read, sandbox.create. Anything that needs one of these will say so. Sign in again to allow them.");
expect(host.querySelector("[data-boat-sign-in]")?.textContent).toBe("Sign in again");
});
it("with a key saved too, it says which of the two is used", async () => {
await show({ ...CONNECTED, keySaved: true, used: true }, { fieldShown: false });
expect(host.querySelector("[data-boat-used]")?.textContent).toContain("use this sign-in");
expect(host.querySelector("[data-boat-or]")).toBeNull();
await show({ ...CONNECTED, keySaved: true, used: false }, { fieldShown: false });
expect(host.querySelector("[data-boat-used]")?.textContent).toContain("use the Boat API key below");
});
it("ended, cancelled and failed each say what happened and offer the button again", async () => {
await show({ available: true, state: "needs-sign-in", missingScopes: [], used: true, keySaved: false });
expect(text()).toContain("Your Boat account sign-in has ended. Sign in again.");
expect(host.querySelector("[data-boat-sign-in]")?.textContent).toBe("Sign in again");
expect(host.querySelector("[data-boat-disconnect]")).toBeTruthy();
for (const [outcome, words] of [["cancelled", "Sign-in cancelled. Nothing was connected."], ["failed", "did not complete"], ["refused", "Not signed in."], ["unreachable", "Couldn't reach boat.dev"], ["not-saved", "could not be saved"]] as const) {
await show({ ...NONE, last: { outcome } });
expect(host.querySelector(`[data-boat-outcome='${outcome}']`)?.textContent).toContain(words);
expect(host.querySelector("[data-boat-sign-in]")?.textContent).toBe("Sign in with Boat");
}
await show({ ...CONNECTED, last: { outcome: "connected" } });
expect(host.querySelector("[data-boat-outcome]")).toBeNull();
});
it("is offered only on the desktop app's own local page, and never where cloud desktops are included", () => {
const config = { box: { configured: false, keySaved: false, signIn: NONE } };
for (const port of ["8799", "18799", "28799"]) expect(boatSignInShown(config, { hostname: "127.0.0.1", port })).toEqual(NONE);
expect(boatSignInShown(config, { hostname: "localhost", port: "8799" })).toEqual(NONE);
for (const page of [{ hostname: "127.0.0.1", port: "5199" }, { hostname: "127.0.0.1", port: "" }, { hostname: "my-server.example", port: "8799" }, { hostname: "192.168.1.20", port: "8799" }]) {
expect(boatSignInShown(config, page), JSON.stringify(page)).toBeNull();
}
const local = { hostname: "127.0.0.1", port: "8799" };
expect(boatSignInShown({ box: { configured: false } }, local)).toBeNull();
expect(boatSignInShown({ box: { configured: false, signIn: { ...NONE, available: false } } }, local)).toBeNull();
expect(boatSignInShown({ box: { configured: true, included: true, signIn: NONE } }, local)).toBeNull();
expect(boatSignInShown(null, local)).toBeNull();
});
it("the key field under it is the pasted key's alone: a sign-in is not shown as a saved key", () => {
const entry = keyEntry("box");
const saved = (box: Record<string, unknown>) => savedState(entry, { box } as never);
expect(saved({ configured: true, keySaved: false, signIn: CONNECTED })).toEqual({ configured: false });
expect(saved({ configured: true, keySaved: true, last4: "c3d4", signIn: CONNECTED })).toEqual({ configured: true, last4: "c3d4" });
// A server that says nothing about sign-in: exactly as before.
expect(saved({ configured: true, last4: "c3d4" })).toEqual({ configured: true, last4: "c3d4" });
expect(saved({ configured: true, included: true })).toEqual({ configured: false, included: true });
});