Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
const hook = vi.hoisted(() => ({
subscribe: undefined as undefined | ((listener: () => void) => () => void),
snapshot: undefined as undefined | (() => boolean),
serverSnapshot: undefined as undefined | (() => boolean),
}));
vi.mock("react", () => ({
useSyncExternalStore: (
subscribe: (listener: () => void) => () => void,
snapshot: () => boolean,
serverSnapshot: () => boolean,
) => {
Object.assign(hook, { subscribe, snapshot, serverSnapshot });
return snapshot();
},
}));
function memoryStorage() {
const values = new Map<string, string>();
return {
getItem: vi.fn((key: string) => values.get(key) ?? null),
setItem: vi.fn((key: string, value: string) => values.set(key, value)),
removeItem: (key: string) => values.delete(key),
clear: () => values.clear(),
};
}
let local: ReturnType<typeof memoryStorage>;
let browser: EventTarget;
function storageEvent(key: string | null, storageArea: unknown = local) {
browser.dispatchEvent(Object.assign(new Event("storage"), { key, storageArea }));
}
beforeEach(() => {
vi.resetModules();
local = memoryStorage();
browser = new EventTarget();
vi.stubGlobal("localStorage", local);
vi.stubGlobal("window", browser);
});
afterEach(() => {
vi.unstubAllGlobals();
});
describe("notification sounds preference", () => {
it("defaults on and ignores malformed values without writing anything", async () => {
const preference = await import("./notification-preferences");
expect(preference.useNotificationSounds()).toBe(true);
expect(preference.notificationSoundsEnabled()).toBe(true);
expect(hook.serverSnapshot!()).toBe(true);
for (const value of ["", "false", "broken", "1"]) {
local.setItem(preference.NOTIFICATION_SOUNDS_KEY, value);
expect(preference.useNotificationSounds()).toBe(true);
}
local.setItem.mockClear();
preference.useNotificationSounds();
expect(local.setItem).not.toHaveBeenCalled();
});
it("persists both choices through a renderer reload", async () => {
let preference = await import("./notification-preferences");
preference.setNotificationSounds(false);
expect(local.getItem(preference.NOTIFICATION_SOUNDS_KEY)).toBe("0");
expect(preference.useNotificationSounds()).toBe(false);
expect(preference.notificationSoundsEnabled()).toBe(false);
vi.resetModules();
preference = await import("./notification-preferences");
expect(preference.notificationSoundsEnabled()).toBe(false);
preference.setNotificationSounds(true);
expect(local.getItem(preference.NOTIFICATION_SOUNDS_KEY)).toBe("1");
vi.resetModules();
preference = await import("./notification-preferences");
expect(preference.useNotificationSounds()).toBe(true);
});
it("notifies mounted consumers immediately and unsubscribes cleanly", async () => {
const preference = await import("./notification-preferences");
preference.useNotificationSounds();
const first = vi.fn();
const second = vi.fn();
const unsubscribeFirst = hook.subscribe!(first);
const unsubscribeSecond = hook.subscribe!(second);
preference.setNotificationSounds(false);
expect(first).toHaveBeenCalledOnce();
expect(second).toHaveBeenCalledOnce();
expect(hook.snapshot!()).toBe(false);
unsubscribeFirst();
preference.setNotificationSounds(true);
expect(first).toHaveBeenCalledOnce();
expect(second).toHaveBeenCalledTimes(2);
unsubscribeSecond();
preference.setNotificationSounds(false);
expect(second).toHaveBeenCalledTimes(2);
});
it("follows cross-window changes and clear, ignoring other storage", async () => {
const preference = await import("./notification-preferences");
preference.useNotificationSounds();
const listener = vi.fn();
const unsubscribe = hook.subscribe!(listener);
local.setItem(preference.NOTIFICATION_SOUNDS_KEY, "0");
storageEvent(preference.NOTIFICATION_SOUNDS_KEY);
expect(listener).toHaveBeenCalledOnce();
expect(hook.snapshot!()).toBe(false);
storageEvent("other-key");
storageEvent(preference.NOTIFICATION_SOUNDS_KEY, memoryStorage());
expect(listener).toHaveBeenCalledOnce();
local.clear();
storageEvent(null);
expect(listener).toHaveBeenCalledTimes(2);
expect(hook.snapshot!()).toBe(true);
unsubscribe();
storageEvent(null);
expect(listener).toHaveBeenCalledTimes(2);
});
it("follows another window's choice after Settings has unmounted", async () => {
const preference = await import("./notification-preferences");
preference.setNotificationSounds(false);
expect(preference.notificationSoundsEnabled()).toBe(false);
local.setItem(preference.NOTIFICATION_SOUNDS_KEY, "1");
expect(preference.notificationSoundsEnabled()).toBe(true);
});
it.each(["getter", "read", "write", "missing"])("keeps the choice usable when storage fails at %s", async (failure) => {
if (failure === "getter") {
Object.defineProperty(globalThis, "localStorage", { configurable: true, get() { throw new Error("blocked"); } });
} else if (failure === "missing") {
vi.stubGlobal("localStorage", undefined);
} else {
if (failure === "write") local.setItem.mockImplementation(() => { throw new Error("blocked"); });
if (failure === "read") local.getItem.mockImplementation(() => { throw new Error("blocked"); });
}
const preference = await import("./notification-preferences");
expect(preference.useNotificationSounds()).toBe(true);
preference.setNotificationSounds(false);
expect(preference.useNotificationSounds()).toBe(false);
expect(preference.notificationSoundsEnabled()).toBe(false);
preference.setNotificationSounds(true);
expect(preference.useNotificationSounds()).toBe(true);
});
});
describe("notification feed preference", () => {
it("defaults off, persists a choice, and leaves the sounds choice alone", async () => {
let preference = await import("./notification-preferences");
expect(preference.useNotificationFeed()).toBe(false);
expect(hook.serverSnapshot?.()).toBe(false);
preference.setNotificationFeed(true);
expect(local.setItem).toHaveBeenCalledWith("omb-notification-feed", "1");
expect(preference.notificationSoundsEnabled()).toBe(true);
vi.resetModules();
preference = await import("./notification-preferences");
expect(preference.notificationFeedEnabled()).toBe(true);
});
it("follows another window's choice", async () => {
const preference = await import("./notification-preferences");
const listener = vi.fn();
expect(preference.useNotificationFeed()).toBe(false);
const unsubscribe = hook.subscribe!(listener);
local.setItem("omb-notification-feed", "1");
storageEvent("omb-notification-feed");
expect(listener).toHaveBeenCalledTimes(1);
expect(preference.notificationFeedEnabled()).toBe(true);
unsubscribe();
});
});
describe("finished-work notifications preference", () => {
it("defaults on and persists changes", async () => {
let preference = await import("./notification-preferences");
expect(preference.finishedNotificationsEnabled()).toBe(true);
preference.setFinishedNotifications(false);
expect(local.getItem(preference.FINISHED_NOTIFICATIONS_KEY)).toBe("0");
expect(preference.useFinishedNotifications()).toBe(false);
vi.resetModules();
preference = await import("./notification-preferences");
expect(preference.finishedNotificationsEnabled()).toBe(false);
preference.setFinishedNotifications(true);
expect(local.getItem(preference.FINISHED_NOTIFICATIONS_KEY)).toBe("1");
expect(preference.finishedNotificationsEnabled()).toBe(true);
});
});
describe("notification tray pin (email 1a1a4fb0)", () => {
it("defaults off and survives a renderer reload", async () => {
let preference = await import("./notification-preferences");
expect(preference.notificationTrayPinned()).toBe(false);
preference.setNotificationTrayPinned(true);
expect(local.getItem(preference.NOTIFICATION_TRAY_PINNED_KEY)).toBe("1");
vi.resetModules();
preference = await import("./notification-preferences");
expect(preference.notificationTrayPinned()).toBe(true);
preference.setNotificationTrayPinned(false);
expect(local.getItem(preference.NOTIFICATION_TRAY_PINNED_KEY)).toBeNull();
expect(preference.notificationTrayPinned()).toBe(false);
});
});
describe("scheduled report notifications preference", () => {
it("follows the finished switch until set, then keeps its own choice", async () => {
let preference = await import("./notification-preferences");
expect(preference.reportNotificationsEnabled()).toBe(true);
preference.setFinishedNotifications(false);
expect(preference.reportNotificationsEnabled()).toBe(false);
preference.setReportNotifications(true);
expect(local.getItem(preference.REPORT_NOTIFICATIONS_KEY)).toBe("1");
expect(preference.useReportNotifications()).toBe(true);
vi.resetModules();
preference = await import("./notification-preferences");
expect(preference.finishedNotificationsEnabled()).toBe(false);
expect(preference.reportNotificationsEnabled()).toBe(true);
preference.setReportNotifications(false);
preference.setFinishedNotifications(true);
expect(preference.reportNotificationsEnabled()).toBe(false);
});
});