Skip to content

Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.

7.1 KB
Raw Blame
import * as NodeFS from "node:fs";

import { describe, expect, it } from "vite-plus/test";

function readSibling(file: string) {
  return NodeFS.readFileSync(new URL(`./${file}`, import.meta.url), "utf8");
}

/** Returns the source of one exported memo row component. */
function rowComponent(source: string, name: string) {
  const start = source.indexOf(`export const ${name} = memo(`);
  const end = source.indexOf("\nexport ", start + 1);

  return start < 0 ? "" : source.slice(start, end < 0 ? source.length : end);
}

describe("BotThreadLanding message formatting", () => {
  it("renders assistant messages with the shared rich markdown component", () => {
    const entries = [
      ["BotThreadLanding.tsx", "bot-provider-message", "bot-user-message"],
      ["GroupThreadLanding.tsx", "group-provider-message", "group-user-message"],
    ] as const;

    for (const [file, assistantTestId, userTestId] of entries) {
      const source = readSibling(file);
      const assistantStart = source.indexOf(`testId="${assistantTestId}"`);
      const userStart = source.indexOf(`testId="${userTestId}"`, assistantStart);
      const assistantSource = source.slice(assistantStart, userStart);

      expect(assistantStart).toBeGreaterThan(-1);
      expect(userStart).toBeGreaterThan(assistantStart);
      expect(source.lastIndexOf("<AssistantMessageRow", assistantStart)).toBeGreaterThan(-1);
      expect(assistantSource).toContain("cwd={runtime.defaultProject?.workspaceRoot}");
      expect(assistantSource).toContain("threadRef={runtime.linkedThreadRef ?? undefined}");
      expect(source.lastIndexOf("<UserMessageRow", userStart)).toBeGreaterThan(assistantStart);
    }

    const assistantRow = rowComponent(readSibling("BotChatMessageRows.tsx"), "AssistantMessageRow");
    expect(assistantRow).toContain("<ChatMarkdown");
    expect(assistantRow).toContain('className="min-w-0 flex-1"');
    expect(assistantRow).not.toContain("onTaskListChange");
    const userRow = rowComponent(readSibling("BotChatMessageRows.tsx"), "UserMessageRow");
    // Sent text goes through SentMessageText, which keeps a reply's backlink compact.
    expect(userRow).toContain("<SentMessageText");
  });

  it("renders step meters for bot and group replies", () => {
    for (const file of ["BotThreadLanding.tsx", "GroupThreadLanding.tsx"]) {
      expect(readSibling(file)).toContain("stepMeters.get(message.turnId)");
    }

    const assistantRow = rowComponent(readSibling("BotChatMessageRows.tsx"), "AssistantMessageRow");
    expect(assistantRow).toContain("<BotStepMeter meter={stepMeter} />");
  });

  it("uses the free-scrolling conversation area instead of end-justified overflow", () => {
    const botSource = NodeFS.readFileSync(
      new URL("./BotThreadLanding.tsx", import.meta.url),
      "utf8",
    );

    const groupSource = NodeFS.readFileSync(
      new URL("./GroupThreadLanding.tsx", import.meta.url),
      "utf8",
    );

    expect(botSource).toContain("<BotConversationScrollArea");
    expect(groupSource).toContain("<BotConversationScrollArea");
    expect(botSource).not.toContain("justify-end gap-4");
    expect(groupSource).not.toContain("justify-end gap-4");
  });

  it("renders the shared approval card in bot and group threads", () => {
    const sources = ["BotThreadLanding.tsx", "GroupThreadLanding.tsx"].map((file) =>
      NodeFS.readFileSync(new URL(`./${file}`, import.meta.url), "utf8"),
    );

    for (const source of sources) {
      expect(source).toContain("<BotApprovalPrompt");
      expect(source).toContain("useRosterPendingApproval(runtime.linkedThreadRef)");
      expect(source).toContain("pendingApproval !== null ||");
    }
  });

  it("keeps message actions visible for coarse pointers without dropping reply controls", () => {
    const source = readSibling("BotChatMessageRows.tsx");
    expect(source).toContain("pointer-coarse:opacity-100");

    for (const name of ["AssistantMessageRow", "UserMessageRow"]) {
      const row = rowComponent(source, name);
      const controls = row.split("<MessageControls").slice(1);
      expect(controls.length).toBeGreaterThan(0);
      expect(row).toContain("HOVER_CONTROLS_CLASS");

      for (const block of controls) {
        const props = block.slice(0, block.indexOf("/>"));
        expect(props).toContain("onReply=");

        if (name === "AssistantMessageRow") expect(props).toContain("readAloud");
      }
    }

    // Only the "Unavailable bot" layout omits reactions.
    const assistantControls = rowComponent(source, "AssistantMessageRow").split("<MessageControls");
    expect(assistantControls[2]).toContain("{...reactions.controls}");
    expect(rowComponent(source, "UserMessageRow")).toContain("{...reactions.controls}");
  });

  it("mounts the voice action in the live bot chat header", () => {
    const source = NodeFS.readFileSync(new URL("./BotThreadLanding.tsx", import.meta.url), "utf8");

    expect(source).toContain(
      'import { BotVoiceCallButton, useVoiceCall } from "../voice/VoiceCall"',
    );
    expect(source).toContain("data-chat-header-actions");
    expect(source).toContain('runtime.latestTurn?.state === "running"');
    expect(source).toContain("voiceCall.activeCall?.botId === bot.id");
    expect(source).toContain("voiceCall.startingBotId === bot.id");
  });

  it("disables reactions with an accessible reason while the linked thread is unavailable", () => {
    const source = NodeFS.readFileSync(new URL("./BotThreadLanding.tsx", import.meta.url), "utf8");
    const rows = readSibling("BotChatMessageRows.tsx");

    expect(source).toContain(
      "const reactionHandler = runtime.linkedThreadRef !== null ? updateReaction : null;",
    );
    expect(rows).toContain("<UnavailableReactionControl");
    expect(rows).toContain("aria-label={unavailableReason}");
    expect(rows).toContain("disabled:pointer-events-auto");
    expect(rows).toContain("if (!onReactionChange) return { controls: {}, chips: {} };");
    expect(source).not.toContain(
      'if (!threadRef) {\n      toastManager.add({ type: "error", title: t("Could not update reaction") });',
    );
  });

  it("renders routine receipts as readable actions with an explicit failed tone", () => {
    const source = NodeFS.readFileSync(new URL("./BotThreadLanding.tsx", import.meta.url), "utf8");

    const receiptRow = NodeFS.readFileSync(
      new URL("./RoutineReceiptRow.tsx", import.meta.url),
      "utf8",
    );

    expect(receiptRow).toContain('const Row = opensRoutines ? "button" : "div"');
    expect(receiptRow).toContain("onClick={opensRoutines ? onOpenRoutines : undefined}");
    expect(source).toContain("{...(onOpenRoutines ? { onOpenRoutines } : {})}");
    // The foreground token keeps failed text at WCAG AA on its tinted row in
    // every built-in theme; the saturated destructive color does not in dark mode.
    expect(receiptRow).toContain('error && "bg-destructive/8 text-destructive-foreground"');
    expect(receiptRow).toContain("hover:bg-destructive/12 hover:text-destructive-foreground");
    expect(receiptRow).not.toMatch(/text-destructive(?!-foreground)/);
    expect(receiptRow).toContain("whitespace-normal break-words");
    expect(receiptRow).not.toContain("truncate");
  });
});