iterate.com / ui

git clone https://git.iterate.com/repos/ui

ui / src / components / agent-chat / agent-live-activity.test.tsxmain at d68d7e1

// @vitest-environment jsdom
// The live row between a model's answer that holds a script and the script's run: the agent is
// still working, so the row keeps its spinner.
import { act } from "react";
import { createRoot } from "react-dom/client";
import { expect, test } from "vitest";
import { AgentLiveActivity } from "./agent-feed.tsx";
import { initialAgentUiState, reduceAgentUi } from "./agent-ui-reducer.ts";
import { frame, request, settled, succeeded } from "#/test-support/agent-log.ts";

Object.assign(globalThis, { IS_REACT_ACT_ENVIRONMENT: true });

test("a finished answer whose script has not started yet still shows the agent working", async () => {
  const script = '<codemode status="Multiplying">\nreturn 6 * 7;\n</codemode>';
  const events = [
    request(1, { model: "gpt-test" }),
    frame(2, 1, { textDelta: script }),
    settled(3, 1, succeeded(script)),
  ];
  let state = initialAgentUiState();
  for (const event of events) state = reduceAgentUi(state, event).endState;

  const container = document.createElement("div");
  const root = createRoot(container);
  await act(async () =>
    root.render(
      <AgentLiveActivity
        state={state}
        toggledIds={new Set()}
        onToggle={() => {}}
        inspect={{ llmRequest: () => {}, scriptRun: () => {} }}
      />,
    ),
  );
  expect(container.querySelector('[aria-label="Loading"]')).not.toBeNull();
  expect(container.textContent).toContain("Working…");
  await act(async () => root.unmount());
});

test("in-flight codemode responseText never streams into the collapsed chat feed, while expanded rail shows it", async () => {
  let state = initialAgentUiState();
  state = reduceAgentUi(state, request(1, { model: "gpt-test" })).endState;

  const container = document.createElement("div");
  const root = createRoot(container);
  const renderWith = async (toggledIds: ReadonlySet<string>) => {
    await act(async () =>
      root.render(
        <AgentLiveActivity
          state={state}
          toggledIds={toggledIds}
          onToggle={() => {}}
          inspect={{ llmRequest: () => {}, scriptRun: () => {} }}
        />,
      ),
    );
  };

  await renderWith(new Set());
  expect(container.textContent).toContain("Waiting for a response");

  // Reasoning tokens show "Thinking" and stream in italics, still zero code
  state = reduceAgentUi(
    state,
    frame(2, 1, { thinkingDelta: "Planning the script", thinking: true }),
  ).endState;
  await renderWith(new Set());
  expect(container.textContent).toContain("Thinking");
  expect(container.textContent).toContain("Planning the script");

  // Even a partial `<cod` prefix (before `<codemode>` completes) and full `<codemode>` body stay
  // hidden from the collapsed chat feed, while status flips to "Writing code"
  state = reduceAgentUi(state, frame(3, 1, { textDelta: "<cod" })).endState;
  await renderWith(new Set());
  expect(container.textContent).toContain("Writing code");
  expect(container.textContent).not.toContain("<cod");

  state = reduceAgentUi(
    state,
    frame(4, 1, { textDelta: "emode>\nawait sendMessage('hello');\n</codemode>" }),
  ).endState;
  await renderWith(new Set());
  expect(container.textContent).toContain("Writing code");
  expect(container.textContent).not.toContain("sendMessage");
  expect(container.textContent).not.toContain("<codemode>");

  // Expanding the activity rail DOES show the in-flight `<codemode>` stream
  const toggleId = `live-activity:${state.live!.id}`;
  await renderWith(new Set([toggleId]));
  expect(container.textContent).toContain("sendMessage('hello')");

  await act(async () => root.unmount());
});