Skip to content

Job application tracker and AI-powered job search assistant. Helps job seekers manage their search journey with AI resume review, job matching, task logging, and application analytics, all while keeping your data private.

11.8 KB
Raw Blame
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { AGENT_PASTE_PART_TYPE } from "@/models/agent.model";

const chat = {
  messages: [] as any[],
  status: "ready" as string,
  regenerate: vi.fn(),
  resumeStream: vi.fn(),
  addToolApprovalResponse: vi.fn(),
  interruptedTurn: false,
  dismissInterrupted: vi.fn(),
  toolStreams: {} as Record<string, string>,
};
vi.mock("@/components/agent/AgentChatProvider", () => ({
  useAgentChat: () => chat,
}));
vi.mock("@/components/TipTapContentViewer", () => ({
  TipTapContentViewer: ({ content }: { content: string }) => (
    <div data-testid="tiptap-content" dangerouslySetInnerHTML={{ __html: content }} />
  ),
}));

import { AgentChatMessages } from "@/components/agent/AgentChatMessages";

const toolPart = (state: string, extra: Record<string, unknown> = {}) => ({
  type: "tool-add_job",
  toolCallId: "c1",
  state,
  input: { company: "Acme", jobTitle: "Platform Engineer" },
  ...extra,
});

const assistant = (parts: any[]) => ({ id: "a1", role: "assistant", parts });
const user = (parts: any[]) => ({ id: "u1", role: "user", parts });

const posting = "Senior Platform Engineer at Acme. We run payments reliability.";

describe("AgentChatMessages", () => {
  beforeEach(() => {
    vi.clearAllMocks();
    chat.messages = [];
    chat.status = "ready";
    chat.interruptedTurn = false;
    chat.toolStreams = {};
  });

  it("renders the running card while the tool input is still arriving", () => {
    chat.messages = [assistant([toolPart("input-available")])];
    render(<AgentChatMessages />);
    expect(screen.getByText(/preparing to add a job/i)).toBeInTheDocument();
  });

  it("renders the approval card for approval-requested", () => {
    chat.messages = [
      assistant([toolPart("approval-requested", { approval: { id: "ap1" } })]),
    ];
    render(<AgentChatMessages />);
    expect(screen.getByText("Needs approval")).toBeInTheDocument();
    expect(screen.getByRole("button", { name: "Confirm" })).toBeInTheDocument();
  });

  it("renders the result card for output-available", () => {
    chat.messages = [
      assistant([
        toolPart("output-available", {
          output: { created: true, jobId: "j1", resolutions: [] },
        }),
      ]),
    ];
    render(<AgentChatMessages />);
    expect(screen.getByText(/added/i)).toBeInTheDocument();
    expect(screen.queryByText("Needs approval")).not.toBeInTheDocument();
  });

  it("feeds the approval card the pasted text from the same thread", () => {
    chat.messages = [
      user([
        { type: "text", text: "add this" },
        {
          type: AGENT_PASTE_PART_TYPE,
          id: "p1",
          data: {
            id: "p1",
            text: posting,
            chars: posting.length,
            truncated: false,
          },
        },
      ]),
      assistant([toolPart("approval-requested", { approval: { id: "ap1" } })]),
    ];
    render(<AgentChatMessages />);
    expect(screen.getByText(/pasted verbatim/i)).toBeInTheDocument();
    expect(screen.getByText(new RegExp(posting.slice(0, 30)))).toBeInTheDocument();
  });

  it("offers Continue on an interrupted turn and calls regenerate only on click", async () => {
    chat.interruptedTurn = true;
    chat.messages = [assistant([{ type: "text", text: "half a s" }])];
    render(<AgentChatMessages />);

    expect(chat.regenerate).not.toHaveBeenCalled();
    expect(chat.resumeStream).not.toHaveBeenCalled();

    await userEvent.click(screen.getByRole("button", { name: /continue/i }));
    expect(chat.regenerate).toHaveBeenCalled();
    expect(chat.resumeStream).not.toHaveBeenCalled();
  });

  it("lets the interrupted notice be dismissed", async () => {
    chat.interruptedTurn = true;
    chat.messages = [assistant([{ type: "text", text: "half a s" }])];
    render(<AgentChatMessages />);
    await userEvent.click(screen.getByRole("button", { name: /dismiss/i }));
    expect(chat.dismissInterrupted).toHaveBeenCalled();
  });

  it("offers Continue when a seeded transcript ends with a user message", () => {
    chat.messages = [user([{ type: "text", text: "add a job" }])];
    render(<AgentChatMessages />);
    expect(screen.getByRole("button", { name: /continue/i })).toBeInTheDocument();
  });

  it("does not offer Continue while the reply is still in flight", () => {
    chat.status = "submitted";
    chat.messages = [user([{ type: "text", text: "add a job" }])];
    render(<AgentChatMessages />);
    expect(
      screen.queryByRole("button", { name: /continue/i }),
    ).not.toBeInTheDocument();
  });

  it("does not offer Continue after a completed assistant turn", () => {
    chat.messages = [
      user([{ type: "text", text: "add a job" }]),
      assistant([{ type: "text", text: "Done." }]),
    ];
    render(<AgentChatMessages />);
    expect(
      screen.queryByRole("button", { name: /continue/i }),
    ).not.toBeInTheDocument();
  });

  it("shows the thinking row while the first assistant part is still pending", () => {
    chat.status = "submitted";
    chat.messages = [user([{ type: "text", text: "add a job" }])];
    render(<AgentChatMessages />);
    expect(screen.getByText(/thinking/i)).toBeInTheDocument();
  });

  it("shows the thinking row after a tool result while the reply is composed", () => {
    chat.status = "streaming";
    chat.messages = [
      assistant([
        toolPart("output-available", {
          output: { created: true, jobId: "j1", resolutions: [] },
        }),
      ]),
    ];
    render(<AgentChatMessages />);
    expect(screen.getByText(/thinking/i)).toBeInTheDocument();
  });

  it("does not double up on the running card, which spins already", () => {
    chat.status = "streaming";
    chat.messages = [assistant([toolPart("input-available")])];
    render(<AgentChatMessages />);
    expect(screen.getByText(/preparing to add a job/i)).toBeInTheDocument();
    expect(screen.queryByText(/thinking/i)).not.toBeInTheDocument();
  });

  it("does not show the thinking row while an approval is on screen", () => {
    chat.status = "streaming";
    chat.messages = [
      assistant([toolPart("approval-requested", { approval: { id: "ap1" } })]),
    ];
    render(<AgentChatMessages />);
    expect(screen.queryByText(/thinking/i)).not.toBeInTheDocument();
  });

  it("does not show the thinking row once text is arriving", () => {
    chat.status = "streaming";
    chat.messages = [assistant([{ type: "text", text: "I found the" }])];
    render(<AgentChatMessages />);
    expect(screen.queryByText(/thinking/i)).not.toBeInTheDocument();
  });

  it("does not show the thinking row on a settled transcript", () => {
    chat.messages = [
      user([{ type: "text", text: "add a job" }]),
      assistant([{ type: "text", text: "Done." }]),
    ];
    render(<AgentChatMessages />);
    expect(screen.queryByText(/thinking/i)).not.toBeInTheDocument();
  });

  it("renders assistant text as plain text, never as markdown media", () => {
    chat.messages = [
      assistant([{ type: "text", text: "![x](http://evil/a.png)" }]),
    ];
    const { container } = render(<AgentChatMessages />);
    expect(container.innerHTML).not.toContain("<img");
    expect(screen.getByTestId("tiptap-content")).toHaveTextContent(
      "![x](http://evil/a.png)",
    );
  });

  it("renders assistant markdown as formatted HTML", () => {
    chat.messages = [
      assistant([{ type: "text", text: "## Summary\n\n- one\n- two" }]),
    ];
    render(<AgentChatMessages />);
    const rendered = screen.getByTestId("tiptap-content");
    expect(rendered.innerHTML).toContain("<h2>Summary</h2>");
    expect(rendered.innerHTML).toContain("<li>one</li>");
  });

  it("leaves user text unformatted", () => {
    chat.messages = [user([{ type: "text", text: "## not a heading" }])];
    render(<AgentChatMessages />);
    expect(screen.queryByTestId("tiptap-content")).not.toBeInTheDocument();
    expect(screen.getByText("## not a heading")).toBeInTheDocument();
  });

  it("does not render a link the model emitted", () => {
    chat.messages = [
      assistant([{ type: "text", text: "[click](https://evil.example/?d=x)" }]),
    ];
    render(<AgentChatMessages />);
    expect(screen.queryByRole("link")).not.toBeInTheDocument();
  });

  // think:true means reasoning parts now reach the client. They are the
  // model's scratchpad, not its answer, and must never render as prose.
  it("renders nothing for a reasoning part", () => {
    chat.messages = [
      assistant([
        { type: "reasoning", text: "The user pasted a posting, so I omit jobDescription." },
        { type: "text", text: "Added." },
      ]),
    ];
    render(<AgentChatMessages />);
    expect(screen.getByText("Added.")).toBeInTheDocument();
    expect(screen.queryByText(/omit jobDescription/)).not.toBeInTheDocument();
  });

  // The transcript no longer parses assistant prose for a SCORES line at all
  // — the card comes from the tool part. A model that emits one anyway is
  // rendered as the plain text it is.
  it("renders no score card for a scores line in assistant prose", () => {
    chat.messages = [
      assistant([
        {
          type: "text",
          text: "SCORES: overall=78 impact=72 clarity=81 ats=69\n\nSolid resume.",
        },
      ]),
    ];
    render(<AgentChatMessages />);
    expect(screen.queryByText("Impact")).not.toBeInTheDocument();
  });

  it("renders a follow-up answer with no score card", () => {
    chat.messages = [
      assistant([
        { type: "text", text: "Your ATS score is held back by the tables." },
      ]),
    ];
    render(<AgentChatMessages />);
    expect(screen.queryByText("Impact")).not.toBeInTheDocument();
  });

  const reviewRunning = {
    type: "tool-review_resume",
    toolCallId: "rv1",
    state: "input-available",
    input: {},
  } as any;

  it("renders the review as it streams, before the tool result exists", () => {
    chat.status = "streaming";
    chat.toolStreams = {
      rv1: "SCORES: overall=78 impact=72 clarity=81 ats=69\n\n## Summary\n\nSolid.",
    };
    chat.messages = [assistant([reviewRunning])];
    render(<AgentChatMessages />);
    expect(screen.getByText("Impact")).toBeInTheDocument();
    expect(screen.queryByText(/SCORES:/)).not.toBeInTheDocument();
    expect(screen.getByTestId("tiptap-content").innerHTML).toContain(
      "<h2>Summary</h2>",
    );
  });

  it("shows the running card until the first review token arrives", () => {
    chat.status = "streaming";
    chat.toolStreams = {};
    chat.messages = [assistant([reviewRunning])];
    render(<AgentChatMessages />);
    expect(screen.getByText(/reviewing your resume/i)).toBeInTheDocument();
  });

  it("renders a running match_job from its streamed text", () => {
    chat.toolStreams = {
      m1: "SCORES: match=72 recommendation=good\n\n## Summary\n\nStreaming.",
    };
    chat.messages = [
      {
        id: "a1",
        role: "assistant",
        parts: [
          {
            type: "tool-match_job",
            toolCallId: "m1",
            state: "input-available",
            input: {},
          },
        ],
      },
    ] as any;
    render(<AgentChatMessages />);
    expect(screen.getByText(/Streaming\./i)).toBeInTheDocument();
    expect(screen.queryByText(/SCORES:/)).not.toBeInTheDocument();
  });

  it("renders a running generate_cover_letter from its streamed text", () => {
    chat.toolStreams = {
      c1: "<think>planning</think>Dear Hiring Manager,\n\nI am writing.",
    };
    chat.messages = [
      {
        id: "a1",
        role: "assistant",
        parts: [
          {
            type: "tool-generate_cover_letter",
            toolCallId: "c1",
            state: "input-available",
            input: {},
          },
        ],
      },
    ] as any;
    render(<AgentChatMessages />);
    expect(screen.getByText(/Dear Hiring Manager/i)).toBeInTheDocument();
    expect(screen.queryByText(/planning/)).not.toBeInTheDocument();
  });
});