Skip to content

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

3.1 KB
Raw Blame
// @vitest-environment happy-dom
import { readFileSync } from "node:fs";
import { join } from "node:path";
import { createElement } from "react";
import { act } from "react";
import { createRoot } from "react-dom/client";
import { afterEach, describe, expect, it } from "vitest";
import { TurnNarrationRun } from "./TurnNarrationRun";
import { CopyFeedbackIcon } from "./CopyFeedbackIcon";

(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;

afterEach(() => {
  document.body.innerHTML = "";
});

function mount() {
  const host = document.createElement("div");
  document.body.appendChild(host);
  return { host, root: createRoot(host) };
}

describe("smooth micro-transitions", () => {
  it("draws the resting copy icon still, so code blocks and messages mount without motion", () => {
    const { host, root } = mount();
    act(() => root.render(createElement(CopyFeedbackIcon, { state: "idle", size: 17, strokeWidth: 1.5 })));
    const icon = host.querySelector("svg")!;
    expect(icon.getAttribute("class")).not.toMatch(/animate|check-in/);
    expect(icon.getAttribute("stroke-width")).toBe("1.5");

    act(() => root.render(createElement(CopyFeedbackIcon, { state: "copied", size: 17, strokeWidth: 1.5 })));
    expect(host.querySelector(".check-in")).not.toBeNull();

    act(() => root.render(createElement(CopyFeedbackIcon, { state: "failed", size: 17, strokeWidth: 1.5 })));
    expect(host.querySelector(".check-in")).toBeNull();
    expect(host.querySelector(".animate-icon-morph")).not.toBeNull();
    act(() => root.unmount());
  });

  it("turns the narration chevron and shows the steps at once, with no animated panel", () => {
    const { host, root } = mount();
    act(() => root.render(createElement(TurnNarrationRun, { label: "2 steps", children: createElement("div", { "data-testid": "step" }, "Read file") } as never)));
    const toggle = host.querySelector("button")!;
    expect(host.querySelector("svg.chevron-turn")).not.toBeNull();
    expect(host.querySelector('[data-testid="step"]')).toBeNull();

    act(() => toggle.click());
    expect(host.querySelector('[data-testid="step"]')).not.toBeNull();
    expect(host.querySelector("svg.chevron-turn")?.getAttribute("class")).toContain("rotate-90");

    act(() => toggle.click());
    expect(host.querySelector('[data-testid="step"]'), "closing is instant").toBeNull();
    act(() => root.unmount());
  });

  it("switches every new animation off under both reduced-motion settings", () => {
    const css = readFileSync(join(process.cwd(), "src", "styles.css"), "utf8");
    const media = css.slice(css.indexOf("@media (prefers-reduced-motion: reduce) {\n  .turn-now-card"));
    const block = media.slice(0, media.indexOf("\n}\n"));
    const flagged = media.slice(0, media.indexOf("animation: none !important;"));
    for (const name of ["animate-icon-morph", "chevron-turn", "turn-phrase-in"]) {
      expect(block, `${name} under prefers-reduced-motion`).toContain(`.${name}`);
      expect(flagged, `${name} under data-reduced-motion`).toContain(`:root[data-reduced-motion="true"] .${name}`);
    }
  });
});