2026-05-16 01:14:11 +08:00
|
|
|
import { render, screen } from "@testing-library/react";
|
2026-05-13 08:05:34 +00:00
|
|
|
import { describe, expect, it } from "vitest";
|
|
|
|
|
|
2026-05-17 17:04:57 +08:00
|
|
|
import {
|
|
|
|
|
assistantCopyFlags,
|
|
|
|
|
buildDisplayUnits,
|
|
|
|
|
ThreadMessages,
|
|
|
|
|
} from "@/components/thread/ThreadMessages";
|
2026-05-13 08:05:34 +00:00
|
|
|
import type { UIMessage } from "@/lib/types";
|
|
|
|
|
|
|
|
|
|
describe("ThreadMessages", () => {
|
2026-05-30 23:45:26 +08:00
|
|
|
it("groups consecutive reasoning and tool rows into one timeline before the answer", () => {
|
2026-05-13 08:05:34 +00:00
|
|
|
const messages: UIMessage[] = [
|
|
|
|
|
{
|
|
|
|
|
id: "r1",
|
|
|
|
|
role: "assistant",
|
|
|
|
|
content: "",
|
|
|
|
|
reasoning: "thinking",
|
|
|
|
|
reasoningStreaming: false,
|
|
|
|
|
isStreaming: true,
|
|
|
|
|
createdAt: Date.now(),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: "t1",
|
|
|
|
|
role: "tool",
|
|
|
|
|
kind: "trace",
|
|
|
|
|
content: "search()",
|
|
|
|
|
traces: ["search()"],
|
|
|
|
|
createdAt: Date.now(),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: "r2",
|
|
|
|
|
role: "assistant",
|
|
|
|
|
content: "",
|
|
|
|
|
reasoning: "more thinking",
|
|
|
|
|
reasoningStreaming: false,
|
|
|
|
|
isStreaming: true,
|
|
|
|
|
createdAt: Date.now(),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: "a1",
|
|
|
|
|
role: "assistant",
|
|
|
|
|
content: "final answer",
|
|
|
|
|
createdAt: Date.now(),
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
2026-05-16 01:14:11 +08:00
|
|
|
const { container } = render(
|
|
|
|
|
<ThreadMessages messages={messages} isStreaming={false} />,
|
|
|
|
|
);
|
2026-05-13 08:05:34 +00:00
|
|
|
const rows = Array.from(container.firstElementChild?.children ?? []);
|
|
|
|
|
|
2026-05-16 01:14:11 +08:00
|
|
|
expect(rows).toHaveLength(2);
|
|
|
|
|
expect(rows[0]).not.toHaveClass("mt-2", "mt-4", "mt-5");
|
|
|
|
|
expect(rows[1]).toHaveClass("mt-4");
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-30 23:45:26 +08:00
|
|
|
it("keeps file edits as their own activity row inside a turn", () => {
|
2026-05-17 23:52:14 +08:00
|
|
|
const messages: UIMessage[] = [
|
|
|
|
|
{
|
|
|
|
|
id: "r1",
|
|
|
|
|
role: "assistant",
|
|
|
|
|
content: "",
|
|
|
|
|
reasoning: "first pass",
|
|
|
|
|
activitySegmentId: "seg-1",
|
|
|
|
|
createdAt: 1,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: "t1",
|
|
|
|
|
role: "tool",
|
|
|
|
|
kind: "trace",
|
|
|
|
|
content: "edit_file()",
|
|
|
|
|
traces: ["edit_file()"],
|
|
|
|
|
fileEdits: [{
|
|
|
|
|
call_id: "call-edit",
|
|
|
|
|
tool: "edit_file",
|
|
|
|
|
path: "foo.txt",
|
|
|
|
|
phase: "end",
|
|
|
|
|
added: 2,
|
|
|
|
|
deleted: 1,
|
|
|
|
|
status: "done",
|
|
|
|
|
}],
|
|
|
|
|
activitySegmentId: "seg-1",
|
|
|
|
|
createdAt: 2,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: "r2",
|
|
|
|
|
role: "assistant",
|
|
|
|
|
content: "",
|
|
|
|
|
reasoning: "second pass",
|
|
|
|
|
activitySegmentId: "seg-2",
|
|
|
|
|
createdAt: 3,
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
const units = buildDisplayUnits(messages);
|
|
|
|
|
|
2026-05-30 23:45:26 +08:00
|
|
|
expect(units).toHaveLength(3);
|
|
|
|
|
expect(units.map((unit) => unit.type)).toEqual(["activity", "activity", "activity"]);
|
|
|
|
|
expect(units[0].type === "activity" ? units[0].messages.map((m) => m.id) : []).toEqual(["r1"]);
|
|
|
|
|
expect(units[1].type === "activity" ? units[1].messages.map((m) => m.id) : []).toEqual(["t1"]);
|
|
|
|
|
expect(units[2].type === "activity" ? units[2].messages.map((m) => m.id) : []).toEqual(["r2"]);
|
2026-05-17 23:52:14 +08:00
|
|
|
});
|
|
|
|
|
|
2026-06-01 15:00:59 +08:00
|
|
|
it("splits ordinary tool activity when segment ids changed", () => {
|
2026-05-17 23:52:14 +08:00
|
|
|
const messages: UIMessage[] = [
|
|
|
|
|
{
|
|
|
|
|
id: "r1",
|
|
|
|
|
role: "assistant",
|
|
|
|
|
content: "",
|
|
|
|
|
reasoning: "first pass",
|
|
|
|
|
activitySegmentId: "seg-1",
|
|
|
|
|
createdAt: 1,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: "t1",
|
|
|
|
|
role: "tool",
|
|
|
|
|
kind: "trace",
|
|
|
|
|
content: "read_file()",
|
|
|
|
|
traces: ["read_file()"],
|
|
|
|
|
activitySegmentId: "seg-1",
|
|
|
|
|
createdAt: 2,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: "r2",
|
|
|
|
|
role: "assistant",
|
|
|
|
|
content: "",
|
|
|
|
|
reasoning: "second pass",
|
|
|
|
|
activitySegmentId: "seg-2",
|
|
|
|
|
createdAt: 3,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: "t2",
|
|
|
|
|
role: "tool",
|
|
|
|
|
kind: "trace",
|
|
|
|
|
content: "grep()",
|
|
|
|
|
traces: ["grep()"],
|
|
|
|
|
activitySegmentId: "seg-2",
|
|
|
|
|
createdAt: 4,
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
const units = buildDisplayUnits(messages);
|
|
|
|
|
|
2026-06-01 15:00:59 +08:00
|
|
|
expect(units).toHaveLength(2);
|
2026-05-30 23:45:26 +08:00
|
|
|
expect(units[0].type === "activity" ? units[0].messages.map((m) => m.id) : []).toEqual([
|
2026-05-17 23:52:14 +08:00
|
|
|
"r1",
|
|
|
|
|
"t1",
|
2026-06-01 15:00:59 +08:00
|
|
|
]);
|
|
|
|
|
expect(units[1].type === "activity" ? units[1].messages.map((m) => m.id) : []).toEqual([
|
2026-05-17 23:52:14 +08:00
|
|
|
"r2",
|
|
|
|
|
"t2",
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
|
2026-06-01 15:00:59 +08:00
|
|
|
it("renders a later tool segment after the visible answer that preceded it", () => {
|
|
|
|
|
const messages: UIMessage[] = [
|
|
|
|
|
{
|
|
|
|
|
id: "r1",
|
|
|
|
|
role: "assistant",
|
|
|
|
|
content: "",
|
|
|
|
|
reasoning: "I should do a fresh search.",
|
|
|
|
|
activitySegmentId: "seg-1",
|
|
|
|
|
createdAt: 1,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: "a1",
|
|
|
|
|
role: "assistant",
|
|
|
|
|
content: "Let me search the latest data.",
|
|
|
|
|
createdAt: 2,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: "t1",
|
|
|
|
|
role: "tool",
|
|
|
|
|
kind: "trace",
|
|
|
|
|
content: "Searching query: HKUDS/nanobot GitHub stars",
|
|
|
|
|
traces: ["Searching query: HKUDS/nanobot GitHub stars"],
|
|
|
|
|
activitySegmentId: "seg-2",
|
|
|
|
|
createdAt: 3,
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
const units = buildDisplayUnits(messages);
|
|
|
|
|
|
|
|
|
|
expect(units).toHaveLength(3);
|
|
|
|
|
expect(units[0].type === "activity" ? units[0].messages.map((m) => m.id) : []).toEqual(["r1"]);
|
|
|
|
|
expect(units[1]).toMatchObject({
|
|
|
|
|
type: "message",
|
|
|
|
|
message: {
|
|
|
|
|
id: "a1",
|
|
|
|
|
content: "Let me search the latest data.",
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
expect(units[2].type === "activity" ? units[2].messages.map((m) => m.id) : []).toEqual(["t1"]);
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-30 23:45:26 +08:00
|
|
|
it("only marks the current activity timeline as live while streaming", () => {
|
2026-05-17 23:52:14 +08:00
|
|
|
const messages: UIMessage[] = [
|
|
|
|
|
{
|
|
|
|
|
id: "r1",
|
|
|
|
|
role: "assistant",
|
|
|
|
|
content: "",
|
|
|
|
|
reasoning: "first pass",
|
|
|
|
|
reasoningStreaming: true,
|
|
|
|
|
activitySegmentId: "seg-1",
|
|
|
|
|
createdAt: 1,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: "t1",
|
|
|
|
|
role: "tool",
|
|
|
|
|
kind: "trace",
|
|
|
|
|
content: "edit_file()",
|
|
|
|
|
traces: ["edit_file()"],
|
|
|
|
|
fileEdits: [{
|
|
|
|
|
call_id: "call-edit",
|
|
|
|
|
tool: "edit_file",
|
|
|
|
|
path: "foo.txt",
|
|
|
|
|
phase: "start",
|
|
|
|
|
added: 4,
|
|
|
|
|
deleted: 1,
|
|
|
|
|
approximate: true,
|
|
|
|
|
status: "editing",
|
|
|
|
|
}],
|
|
|
|
|
activitySegmentId: "seg-1",
|
|
|
|
|
createdAt: 2,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: "r2",
|
|
|
|
|
role: "assistant",
|
|
|
|
|
content: "",
|
|
|
|
|
reasoning: "second pass",
|
|
|
|
|
reasoningStreaming: true,
|
|
|
|
|
activitySegmentId: "seg-2",
|
|
|
|
|
createdAt: 3,
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
render(<ThreadMessages messages={messages} isStreaming />);
|
|
|
|
|
|
2026-05-31 20:52:56 +08:00
|
|
|
expect(screen.getByLabelText(/edited foo\.txt/i)).toBeInTheDocument();
|
|
|
|
|
expect(screen.queryByLabelText(/editing foo\.txt/i)).not.toBeInTheDocument();
|
2026-05-17 23:52:14 +08:00
|
|
|
});
|
|
|
|
|
|
2026-05-30 23:45:26 +08:00
|
|
|
it("folds final answer reasoning into the preceding activity timeline", () => {
|
2026-05-17 17:11:38 +08:00
|
|
|
const messages: UIMessage[] = [
|
|
|
|
|
{
|
|
|
|
|
id: "r1",
|
|
|
|
|
role: "assistant",
|
|
|
|
|
content: "",
|
|
|
|
|
reasoning: "search plan",
|
|
|
|
|
reasoningStreaming: false,
|
|
|
|
|
createdAt: 1,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: "t1",
|
|
|
|
|
role: "tool",
|
|
|
|
|
kind: "trace",
|
|
|
|
|
content: "web_search()",
|
|
|
|
|
traces: ["web_search()"],
|
|
|
|
|
createdAt: 2,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: "a1",
|
|
|
|
|
role: "assistant",
|
|
|
|
|
content: "final answer",
|
|
|
|
|
reasoning: "summarize results",
|
|
|
|
|
reasoningStreaming: false,
|
2026-05-24 18:38:08 +08:00
|
|
|
latencyMs: 9_200,
|
2026-05-17 17:11:38 +08:00
|
|
|
createdAt: 3,
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
const units = buildDisplayUnits(messages);
|
|
|
|
|
|
|
|
|
|
expect(units).toHaveLength(2);
|
2026-05-30 23:45:26 +08:00
|
|
|
expect(units[0]).toMatchObject({ type: "activity" });
|
|
|
|
|
expect(units[0].type === "activity" ? units[0].messages.map((m) => m.id) : []).toEqual([
|
2026-05-17 17:11:38 +08:00
|
|
|
"r1",
|
|
|
|
|
"t1",
|
|
|
|
|
"a1-reasoning",
|
|
|
|
|
]);
|
2026-05-30 23:45:26 +08:00
|
|
|
expect(units[0].type === "activity" ? units[0].messages.at(-1)?.latencyMs : undefined).toBe(9_200);
|
2026-05-17 17:11:38 +08:00
|
|
|
expect(units[1]).toMatchObject({
|
2026-05-30 23:45:26 +08:00
|
|
|
type: "message",
|
2026-05-17 17:11:38 +08:00
|
|
|
message: {
|
|
|
|
|
id: "a1",
|
|
|
|
|
content: "final answer",
|
|
|
|
|
},
|
|
|
|
|
});
|
2026-05-30 23:45:26 +08:00
|
|
|
if (units[1].type === "message") {
|
2026-05-17 17:11:38 +08:00
|
|
|
expect(units[1].message).not.toHaveProperty("reasoning");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render(<ThreadMessages messages={messages} isStreaming={false} />);
|
|
|
|
|
expect(screen.queryByRole("button", { name: /^thinking$/i })).not.toBeInTheDocument();
|
2026-05-24 18:38:08 +08:00
|
|
|
expect(screen.getByText("Thought for 9s")).toBeInTheDocument();
|
2026-05-17 17:11:38 +08:00
|
|
|
expect(screen.getByText("final answer")).toBeInTheDocument();
|
|
|
|
|
});
|
|
|
|
|
|
2026-06-01 15:00:59 +08:00
|
|
|
it("keeps late activity after the live assistant answer while streaming", () => {
|
2026-05-29 03:42:53 +08:00
|
|
|
const messages: UIMessage[] = [
|
|
|
|
|
{
|
|
|
|
|
id: "t0",
|
|
|
|
|
role: "tool",
|
|
|
|
|
kind: "trace",
|
|
|
|
|
content: "Thinking",
|
|
|
|
|
traces: ["Thinking"],
|
|
|
|
|
activitySegmentId: "seg-live",
|
|
|
|
|
createdAt: 1,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: "a1",
|
|
|
|
|
role: "assistant",
|
|
|
|
|
content: "partial answer",
|
|
|
|
|
isStreaming: true,
|
|
|
|
|
createdAt: 2,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: "t1",
|
|
|
|
|
role: "tool",
|
|
|
|
|
kind: "trace",
|
|
|
|
|
content: "Reading api.github.com/repos/NousResearch/hermes-agent",
|
|
|
|
|
traces: ["Reading api.github.com/repos/NousResearch/hermes-agent"],
|
|
|
|
|
activitySegmentId: "seg-live",
|
|
|
|
|
createdAt: 3,
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
const units = buildDisplayUnits(messages);
|
|
|
|
|
|
2026-06-01 15:00:59 +08:00
|
|
|
expect(units).toHaveLength(3);
|
|
|
|
|
expect(units[0].type === "activity" ? units[0].messages.map((m) => m.id) : []).toEqual(["t0"]);
|
2026-05-29 03:42:53 +08:00
|
|
|
expect(units[1]).toMatchObject({
|
2026-05-30 23:45:26 +08:00
|
|
|
type: "message",
|
2026-05-29 03:42:53 +08:00
|
|
|
message: {
|
|
|
|
|
id: "a1",
|
|
|
|
|
content: "partial answer",
|
|
|
|
|
},
|
|
|
|
|
});
|
2026-06-01 15:00:59 +08:00
|
|
|
expect(units[2].type === "activity" ? units[2].messages.map((m) => m.id) : []).toEqual(["t1"]);
|
2026-05-29 03:42:53 +08:00
|
|
|
|
|
|
|
|
render(<ThreadMessages messages={messages} isStreaming />);
|
|
|
|
|
|
|
|
|
|
const answer = screen.getByText("partial answer");
|
2026-06-01 15:00:59 +08:00
|
|
|
const liveActivity = screen.getByRole("button", { name: /working/i });
|
|
|
|
|
expect(answer.compareDocumentPosition(liveActivity) & Node.DOCUMENT_POSITION_FOLLOWING).toBeTruthy();
|
2026-05-29 03:42:53 +08:00
|
|
|
});
|
|
|
|
|
|
2026-06-01 15:00:59 +08:00
|
|
|
it("keeps late activity after a completed assistant answer", () => {
|
2026-05-29 03:42:53 +08:00
|
|
|
const messages: UIMessage[] = [
|
|
|
|
|
{
|
|
|
|
|
id: "r1",
|
|
|
|
|
role: "assistant",
|
|
|
|
|
content: "",
|
|
|
|
|
reasoning: "checking weather",
|
|
|
|
|
activitySegmentId: "seg-late",
|
|
|
|
|
createdAt: 1,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: "a1",
|
|
|
|
|
role: "assistant",
|
|
|
|
|
content: "Hong Kong is hot today.",
|
|
|
|
|
latencyMs: 161_000,
|
|
|
|
|
createdAt: 2,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: "t1",
|
|
|
|
|
role: "tool",
|
|
|
|
|
kind: "trace",
|
|
|
|
|
content: "Reading hko.gov.hk/en/wxinfo/currwx/current.htm",
|
|
|
|
|
traces: ["Reading hko.gov.hk/en/wxinfo/currwx/current.htm"],
|
|
|
|
|
activitySegmentId: "seg-late",
|
|
|
|
|
createdAt: 3,
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
const units = buildDisplayUnits(messages);
|
|
|
|
|
|
2026-06-01 15:00:59 +08:00
|
|
|
expect(units).toHaveLength(3);
|
|
|
|
|
expect(units[0].type === "activity" ? units[0].messages.map((m) => m.id) : []).toEqual(["r1"]);
|
2026-05-29 03:42:53 +08:00
|
|
|
expect(units[1]).toMatchObject({
|
2026-05-30 23:45:26 +08:00
|
|
|
type: "message",
|
2026-05-29 03:42:53 +08:00
|
|
|
message: {
|
|
|
|
|
id: "a1",
|
|
|
|
|
content: "Hong Kong is hot today.",
|
|
|
|
|
},
|
|
|
|
|
});
|
2026-06-01 15:00:59 +08:00
|
|
|
expect(units[2].type === "activity" ? units[2].messages.map((m) => m.id) : []).toEqual(["t1"]);
|
2026-05-29 03:42:53 +08:00
|
|
|
|
|
|
|
|
render(<ThreadMessages messages={messages} isStreaming={false} />);
|
|
|
|
|
|
|
|
|
|
const answer = screen.getByText("Hong Kong is hot today.");
|
2026-06-01 15:00:59 +08:00
|
|
|
const laterActivity = screen.getAllByText(/thought/i).at(-1);
|
|
|
|
|
expect(laterActivity).toBeTruthy();
|
|
|
|
|
expect(answer.compareDocumentPosition(laterActivity!) & Node.DOCUMENT_POSITION_FOLLOWING).toBeTruthy();
|
2026-05-29 03:42:53 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("renders interrupted pre-tool text as activity before the final answer", () => {
|
|
|
|
|
const messages: UIMessage[] = [
|
|
|
|
|
{
|
|
|
|
|
id: "prelude",
|
|
|
|
|
role: "assistant",
|
|
|
|
|
content: "",
|
|
|
|
|
reasoning: "I will inspect first.",
|
|
|
|
|
isStreaming: false,
|
|
|
|
|
activitySegmentId: "seg-1",
|
|
|
|
|
createdAt: 1,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: "tool",
|
|
|
|
|
role: "tool",
|
|
|
|
|
kind: "trace",
|
|
|
|
|
content: 'exec({"cmd":"ls"})',
|
|
|
|
|
traces: ['exec({"cmd":"ls"})'],
|
|
|
|
|
activitySegmentId: "seg-1",
|
|
|
|
|
createdAt: 2,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: "final",
|
|
|
|
|
role: "assistant",
|
|
|
|
|
content: "Done. Open index.html to play.",
|
|
|
|
|
createdAt: 3,
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
const units = buildDisplayUnits(messages);
|
|
|
|
|
|
|
|
|
|
expect(units).toHaveLength(2);
|
2026-05-30 23:45:26 +08:00
|
|
|
expect(units[0].type === "activity" ? units[0].messages.map((m) => m.id) : []).toEqual([
|
2026-05-29 03:42:53 +08:00
|
|
|
"prelude",
|
|
|
|
|
"tool",
|
|
|
|
|
]);
|
|
|
|
|
expect(units[1]).toMatchObject({
|
2026-05-30 23:45:26 +08:00
|
|
|
type: "message",
|
2026-05-29 03:42:53 +08:00
|
|
|
message: {
|
|
|
|
|
id: "final",
|
|
|
|
|
content: "Done. Open index.html to play.",
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-30 23:45:26 +08:00
|
|
|
it("passes assistant turn latency to the preceding completed activity timeline", () => {
|
2026-05-24 18:38:08 +08:00
|
|
|
const messages: UIMessage[] = [
|
|
|
|
|
{
|
|
|
|
|
id: "r1",
|
|
|
|
|
role: "assistant",
|
|
|
|
|
content: "",
|
|
|
|
|
reasoning: "search plan",
|
|
|
|
|
reasoningStreaming: false,
|
|
|
|
|
createdAt: 1,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: "t1",
|
|
|
|
|
role: "tool",
|
|
|
|
|
kind: "trace",
|
|
|
|
|
content: "web_search()",
|
|
|
|
|
traces: ["web_search()"],
|
|
|
|
|
createdAt: 1,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: "a1",
|
|
|
|
|
role: "assistant",
|
|
|
|
|
content: "final answer",
|
|
|
|
|
latencyMs: 14_800,
|
|
|
|
|
createdAt: 1,
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
render(<ThreadMessages messages={messages} isStreaming={false} />);
|
|
|
|
|
|
|
|
|
|
expect(screen.getByText("Thought for 15s")).toBeInTheDocument();
|
|
|
|
|
expect(screen.queryByText("Thought for 0s")).not.toBeInTheDocument();
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-16 01:14:11 +08:00
|
|
|
it("shows copy only on the last assistant slice before the next user turn", () => {
|
|
|
|
|
const messages: UIMessage[] = [
|
|
|
|
|
{
|
|
|
|
|
id: "early",
|
|
|
|
|
role: "assistant",
|
|
|
|
|
content: "starting…",
|
|
|
|
|
createdAt: 1,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: "t1",
|
|
|
|
|
role: "tool",
|
|
|
|
|
kind: "trace",
|
|
|
|
|
content: "search()",
|
|
|
|
|
traces: ["search()"],
|
|
|
|
|
createdAt: 2,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: "late",
|
|
|
|
|
role: "assistant",
|
|
|
|
|
content: "final reply",
|
|
|
|
|
createdAt: 3,
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
render(<ThreadMessages messages={messages} isStreaming={false} />);
|
|
|
|
|
|
|
|
|
|
expect(screen.getAllByRole("button", { name: "Copy reply" })).toHaveLength(1);
|
|
|
|
|
expect(screen.getByText("final reply")).toBeInTheDocument();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("shows copy only on the second assistant when two text slices appear before user", () => {
|
|
|
|
|
const messages: UIMessage[] = [
|
|
|
|
|
{ id: "a1", role: "assistant", content: "part one", createdAt: 1 },
|
|
|
|
|
{ id: "a2", role: "assistant", content: "part two", createdAt: 2 },
|
|
|
|
|
];
|
|
|
|
|
render(<ThreadMessages messages={messages} isStreaming={false} />);
|
|
|
|
|
expect(screen.getAllByRole("button", { name: "Copy reply" })).toHaveLength(1);
|
2026-05-13 08:05:34 +00:00
|
|
|
});
|
2026-05-17 17:04:57 +08:00
|
|
|
|
|
|
|
|
it("computes final assistant copy flags with user-boundary semantics", () => {
|
|
|
|
|
const units = buildDisplayUnits([
|
|
|
|
|
{ id: "u1", role: "user", content: "one", createdAt: 1 },
|
|
|
|
|
{ id: "a1", role: "assistant", content: "draft", createdAt: 2 },
|
|
|
|
|
{
|
|
|
|
|
id: "t1",
|
|
|
|
|
role: "tool",
|
|
|
|
|
kind: "trace",
|
|
|
|
|
content: "tool()",
|
|
|
|
|
traces: ["tool()"],
|
|
|
|
|
createdAt: 3,
|
|
|
|
|
},
|
|
|
|
|
{ id: "a2", role: "assistant", content: "final", createdAt: 4 },
|
|
|
|
|
{ id: "u2", role: "user", content: "two", createdAt: 5 },
|
|
|
|
|
{ id: "a3", role: "assistant", content: "next", createdAt: 6 },
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
const flags = assistantCopyFlags(units);
|
|
|
|
|
const assistantFlags = units
|
|
|
|
|
.map((unit, index) =>
|
2026-05-30 23:45:26 +08:00
|
|
|
unit.type === "message" && unit.message.role === "assistant"
|
2026-05-17 17:04:57 +08:00
|
|
|
? [unit.message.id, flags[index]]
|
|
|
|
|
: null,
|
|
|
|
|
)
|
|
|
|
|
.filter(Boolean);
|
|
|
|
|
|
|
|
|
|
expect(assistantFlags).toEqual([
|
|
|
|
|
["a1", false],
|
|
|
|
|
["a2", true],
|
|
|
|
|
["a3", true],
|
|
|
|
|
]);
|
|
|
|
|
});
|
2026-05-13 08:05:34 +00:00
|
|
|
});
|