Skip to content

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

2.8 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 { setLocale } from "@/lib/i18n";

const fixture = vi.hoisted(() => ({ config: {} as Record<string, unknown> }));
vi.mock("@/state/store-select", async (original) => ({
  ...await original<typeof import("@/state/store-select")>(),
  useStoreSelector: (selector: (state: unknown) => unknown) => selector({ config: fixture.config }),
  useStoreDispatch: () => () => {},
}));
import { AvatarImageGenerator } from "./AvatarImageGenerator";

const GEMINI_KEY_URL = "https://aistudio.google.com/apikey";
let container: HTMLDivElement, root: Root;
beforeEach(() => {
  vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
  setLocale("en");
  container = document.createElement("div"); document.body.append(container); root = createRoot(container);
});
afterEach(async () => { await act(async () => root.unmount()); container.remove(); vi.unstubAllGlobals(); });

const render = async (imageGen: unknown) => {
  fixture.config = imageGen === undefined ? {} : { imageGen };
  await act(async () => {
    root.render(createElement(AvatarImageGenerator, { botLabel: "Researcher", disabled: false, generating: false, onGenerate: async () => {} }));
  });
};
const keyLink = () => [...container.querySelectorAll("a")].find((a) => a.getAttribute("href") === GEMINI_KEY_URL);

describe("Generate with AI: no image provider yet", () => {
  it("says a subscription can't make pictures and links to a Gemini API key", async () => {
    await render({ ready: false, primary: { provider: "gemini", configured: false } });
    expect(container.textContent).toContain("No image provider is set up yet.");
    expect(container.textContent).toContain("Pictures need an image API key. AI subscriptions such as Antigravity or Gemini can't make them here.");
    const link = keyLink();
    expect(link?.textContent).toBe("Get a Gemini API key");
    expect(link?.getAttribute("target")).toBe("_blank");
    expect(link?.getAttribute("rel")).toBe("noreferrer");
    expect(container.querySelector("textarea")?.disabled).toBe(true);
  });

  it("shows the same help before the image settings were ever loaded", async () => {
    await render(undefined);
    expect(container.textContent).toContain("can't make them here");
    expect(keyLink()).toBeTruthy();
  });

  it("shows only the provider in use once a key is set", async () => {
    await render({ ready: true, primary: { provider: "gemini", configured: true } });
    expect(container.textContent).toContain("Uses Gemini, as set in Image generation.");
    expect(container.textContent).not.toContain("can't make them here");
    expect(keyLink()).toBeUndefined();
    expect(container.querySelector("textarea")?.disabled).toBe(false);
  });
});