Skip to content

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

10.2 KB
Raw Blame
// @vitest-environment happy-dom
import { act, createElement } from "react";
import { createRoot, type Root } from "react-dom/client";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";

import { botButtonTone } from "@/lib/bot-button";
import { setAdvancedMode } from "@/lib/interface-mode";
import type { UpdaterState } from "@/lib/updater";
import { UpdateIndicator, updateIndicatorLabel, updateIndicatorShown } from "./UpdateIndicator";
import { updateHasButton } from "./SidebarProfileMenu";

const NOTES = "- **Faster start.** Opens at once. (#1)\n- **Rooms keep actions.** Same as chats. (#2)\n- **Mentions say who they reach.** (#3)";

let push: (state: UpdaterState) => void = () => {};
const install = vi.fn(async () => {});
const check = vi.fn(async () => {});
let host: HTMLDivElement;
let root: Root;

const button = () => host.querySelector<HTMLButtonElement>("[data-update-indicator]");
const card = () => document.querySelector<HTMLElement>("[data-update-card]");
const emit = (state: UpdaterState) => act(async () => push(state));

beforeEach(async () => {
  (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
  install.mockClear();
  check.mockClear();
  (window as unknown as { ogb: unknown }).ogb = {
    updater: {
      onState: (callback: (state: UpdaterState) => void) => {
        push = callback;
        callback({ status: "idle" });
        return () => {};
      },
      install,
      check,
      getState: async () => ({ status: "idle" }),
    },
  };
  setAdvancedMode(true);
  host = document.createElement("div");
  document.body.appendChild(host);
  root = createRoot(host);
  await act(async () => root.render(createElement(UpdateIndicator)));
});

afterEach(async () => {
  await act(async () => root.unmount());
  host.remove();
  delete (window as unknown as { ogb?: unknown }).ogb;
});

describe("the update button", () => {
  it("is shown only while an update downloads, prepares, waits or installs", () => {
    for (const status of ["idle", "checking", "error", "handed-off"] as const) expect(updateIndicatorShown({ status })).toBe(false);
    for (const status of ["downloading", "preparing", "downloaded", "installing"] as const) expect(updateIndicatorShown({ status })).toBe(true);
    expect(updateIndicatorShown(null)).toBe(false);
  });

  it("says what it holds", () => {
    expect(updateIndicatorLabel({ status: "downloaded", version: "0.2.0" })).toBe("Update ready: MausBot 0.2.0");
    expect(updateIndicatorLabel({ status: "downloading", percent: 41.6 })).toBe("Downloading an update, 42%");
    expect(updateIndicatorLabel({ status: "downloading" })).toBe("Downloading an update");
  });

  it("takes over the profile row's own update badge for those phases", () => {
    expect(updateHasButton("downloaded")).toBe(true);
    expect(updateHasButton("downloading")).toBe(true);
    expect(updateHasButton("error")).toBe(false);
    expect(updateHasButton("handed-off")).toBe(false);
  });

  it("in Simple is a round blue button the height of the Connect apps pill, its white arrow pulsing while it downloads", async () => {
    expect(botButtonTone("blue").ink, "white on a blue bot's own send-button fill").toBe("#ffffff");
    await act(async () => setAdvancedMode(false));
    expect(button(), "nothing while there is no update").toBeNull();
    await emit({ status: "downloading", version: "0.2.0", percent: 10 });
    const classes = () => button()!.className.split(" ");
    expect(classes()).toEqual(expect.arrayContaining(["bot-button", "size-9", "rounded-full", "animate-pulse", "motion-reduce:animate-none"]));
    expect(classes()).not.toContain("bg-white");
    expect(button()!.style.getPropertyValue("--bot-button")).toBe(botButtonTone("blue").fill);
    expect(button()!.style.getPropertyValue("--bot-button-ink")).toBe("#ffffff");
    await emit({ status: "downloaded", version: "0.2.0" });
    expect(classes(), "still once it is ready").not.toContain("animate-pulse");
    await act(async () => button()!.click());
    expect(card(), "it opens the update card").not.toBeNull();
    await emit({ status: "idle" });
    expect(button(), "gone once the app has updated").toBeNull();
    expect(card()).toBeNull();
  });

  it("appears when an update arrives and goes when there is none", async () => {
    expect(button()).toBeNull();
    await emit({ status: "downloading", version: "0.2.0", percent: 10 });
    expect(button()).not.toBeNull();
    expect(button()!.className).toContain("rounded-full");
    expect(button()!.className).toContain("bg-white");
    expect(button()!.className).toContain("size-7");
    await emit({ status: "idle" });
    expect(button()).toBeNull();
  });
});

describe("the update card", () => {
  it("opens centred with the version, highlights and Restart to update, which installs", async () => {
    await emit({ status: "downloaded", version: "0.2.0", releaseNotes: NOTES, installMode: "restart" });
    await act(async () => button()!.click());
    const dialog = card()!;
    expect(dialog.getAttribute("role")).toBe("dialog");
    expect(dialog.getAttribute("aria-modal")).toBe("true");
    expect(dialog.parentElement!.className).toContain("items-center");
    expect(dialog.className).toContain("max-w-[420px]");
    expect(dialog.textContent).toContain("MausBot 0.2.0");
    expect([...dialog.querySelectorAll("li")].map((item) => item.textContent)).toEqual([
      "Faster start.",
      "Rooms keep actions.",
      "Mentions say who they reach.",
    ]);
    const restart = [...dialog.querySelectorAll("button")].find((node) => node.textContent?.includes("Restart to update"))!;
    expect(document.activeElement).toBe(restart);
    await act(async () => restart.click());
    expect(install).toHaveBeenCalledOnce();
  });

  it("names the desktop app's version, not the version of a server page it shows", async () => {
    (window as unknown as { ogb: { app: unknown } }).ogb.app = { version: "9.8.7", ownPage: false };
    await emit({ status: "downloaded", version: "0.2.0", releaseNotes: NOTES, installMode: "restart" });
    await act(async () => button()!.click());
    expect(card()!.textContent).toContain("you have 9.8.7");
  });

  it("shows the download's progress and holds the action until it is done", async () => {
    await emit({ status: "downloading", version: "0.2.0", percent: 45 });
    await act(async () => button()!.click());
    const bar = card()!.querySelector('[role="progressbar"]')!;
    expect(bar.getAttribute("aria-valuenow")).toBe("45");
    const primary = [...card()!.querySelectorAll("button")].find((node) => node.textContent?.includes("Downloading 45%"))!;
    expect(primary.disabled).toBe(true);
    await emit({ status: "downloaded", version: "0.2.0", percent: 100 });
    expect(card()!.textContent).toContain("Restart to update");
    expect(card()!.querySelector('[role="progressbar"]')).toBeNull();
  });

  it("says the update installs by itself the next time the person is away, and still offers Restart", async () => {
    await emit({ status: "downloaded", version: "0.2.0", installMode: "restart", installsByItself: true });
    await act(async () => button()!.click());
    expect(card()!.textContent).toContain("Restart now, or leave it: MausBot installs it by itself the next time you're away and nothing is running.");
    expect(card()!.textContent).toContain("Restart to update");
    await emit({ status: "downloaded", version: "0.2.0", installMode: "restart", installsByItself: false });
    expect(card()!.textContent).toContain("MausBot closes and reopens in a moment.");
    expect(card()!.textContent).not.toContain("by itself");
  });

  it("offers Install where a terminal finishes the update", async () => {
    await emit({ status: "downloaded", version: "0.2.0", installMode: "handoff" });
    await act(async () => button()!.click());
    expect(card()!.textContent).toContain("Install");
    expect(card()!.textContent).not.toContain("Restart to update");
  });

  it("keeps Tab inside, closes on Escape and Later, and gives focus back to the button", async () => {
    await emit({ status: "downloaded", version: "0.2.0" });
    button()!.focus();
    await act(async () => button()!.click());
    const controls = [...card()!.querySelectorAll<HTMLButtonElement>("button:not([disabled])")];
    controls.at(-1)!.focus();
    await act(async () => window.dispatchEvent(new KeyboardEvent("keydown", { key: "Tab", cancelable: true })));
    expect(document.activeElement).toBe(controls[0]);
    await act(async () => window.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape", cancelable: true })));
    expect(card()).toBeNull();
    expect(document.activeElement).toBe(button());

    await act(async () => button()!.click());
    const later = [...card()!.querySelectorAll("button")].find((node) => node.textContent === "Later")!;
    await act(async () => later.click());
    expect(card()).toBeNull();
    expect(install).not.toHaveBeenCalled();
  });

  it("closes once there is nothing left to act on, and stays for a failure's Try again", async () => {
    await emit({ status: "downloaded", version: "0.2.0", installMode: "handoff" });
    await act(async () => button()!.click());
    await emit({ status: "handed-off", version: "0.2.0", installMode: "handoff", command: "sudo apt install ./x.deb" });
    expect(card()).toBeNull();
    await emit({ status: "downloaded", version: "0.2.1" });
    expect(card()).toBeNull();

    await act(async () => button()!.click());
    await emit({ status: "error", message: "boom" });
    expect(card()!.textContent).toContain("Try again");
  });

  it("links to all release notes even when none read as highlights", async () => {
    await emit({ status: "downloaded", version: "0.2.0", releaseNotes: "A steadier build with small fixes." });
    await act(async () => button()!.click());
    expect(card()!.querySelectorAll("li")).toHaveLength(0);
    expect(card()!.textContent).toContain("All release notes");
  });

  it("uses one md button scale in the footer", async () => {
    await emit({ status: "downloaded", version: "0.2.0" });
    await act(async () => button()!.click());
    const footer = [...card()!.querySelectorAll("button")].slice(-2);
    for (const node of footer) {
      for (const token of ["h-7", "text-[13px]", "font-medium", "leading-4", "rounded-full", "gap-2"]) expect(node.className).toContain(token);
    }
  });
});