Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

2.4 KB
Raw Blame
// @vitest-environment happy-dom
// The real controller runs against a fake preload bridge.
import { createElement } from "react";
import { flushSync } from "react-dom";
import { createRoot, type Root } from "react-dom/client";
import { afterEach, beforeEach, expect, it, vi } from "vitest";

import { setLocale } from "@/lib/i18n";
import { usePhoneSetupController, type CompanionState } from "./PhoneSetupFlow";

vi.mock("@/state/store", () => ({ useStore: () => ({ state: { config: {} } }) }));
const { PairPhoneSteps } = await import("./PhoneDevicesSection");

const off = { on: false, since: null };
const closed: CompanionState = { enabled: false, keepAwake: false, port: 8810, devices: [], pairing: null,
  routes: { wifi: off, relay: off, tailscale: off, https: off } };

function Page() {
  const c = usePhoneSetupController();
  return c.state ? createElement("div", { "data-steps": "" }, createElement(PairPhoneSteps, { c, state: c.state, codeView: createElement("div", { "data-code": "" }) })) : null;
}

let host: HTMLDivElement;
let root: Root;
const steps = () => document.querySelector<HTMLElement>("[data-steps]");
const turnOn = () => document.querySelector<HTMLButtonElement>("[data-pair-turn-on]");

beforeEach(() => {
  setLocale("en");
  host = document.createElement("div");
  document.body.append(host);
  root = createRoot(host);
});
afterEach(() => {
  flushSync(() => root.unmount());
  host.remove();
  delete (globalThis as { ogb?: unknown }).ogb;
});

it("stays on the choice and says why when the chosen way in could not be turned on", async () => {
  const companion = {
    state: vi.fn(async () => closed),
    setRoute: vi.fn(async () => { throw new Error("settings not writable"); }),
    start: vi.fn(async () => ({ ...closed, enabled: true })),
    pairing: vi.fn(async () => closed),
    refreshTailscale: vi.fn(async () => closed),
  };
  (globalThis as { ogb?: unknown }).ogb = { companion };
  flushSync(() => root.render(createElement(Page)));
  await vi.waitFor(() => expect(turnOn()?.disabled).toBe(false));
  turnOn()!.click();
  await vi.waitFor(() => expect(companion.setRoute).toHaveBeenCalledWith("wifi", true));
  await vi.waitFor(() => expect(steps()?.querySelector('[role="alert"]')?.textContent).toBe(
    "Remote access could not be updated. Open Advanced & troubleshooting and try again.",
  ));
  expect(steps()?.querySelector("[data-code]")).toBeNull();
  expect(turnOn()).not.toBeNull();
  expect(companion.pairing).not.toHaveBeenCalled();
});