Ryker_Feng 6a4e5a3bb2
feat(frontend): add side conversations for quoted follow-ups (#3934)
* feat(frontend): add side conversations for quoted follow-ups

* style(frontend): apply prettier formatting to sidecar-chat files

* fix(frontend): surface sidecar cascade cleanup failures via console.warn

Previously deleteSidecarThreadsForParent silently swallowed both
lookup errors and per-thread deletion failures, so parent thread
deletions could succeed while orphaning sidecar threads with no
signal to the caller. Log a warning that includes the parent id
and the failed thread ids/reasons so the leak is discoverable in
telemetry, matching the existing console.warn/error pattern in
this file.

* fix(frontend): address all sidecar review feedback

Resolve every reviewer comment on PR #3934:

- input-box/hooks/sidecar-panel: clear quoted references only via an
  `onSent` callback that fires after the in-flight guard, so a dropped
  send no longer silently discards quotes (willem-bd #3550).
- message-list: flip the selection toolbar below the selection when it
  would clip above the viewport (willem-bd #3551).
- reference-metadata/thread/input-box: keep referenced ids, roles, and
  count arrays 1:1 parallel instead of deduping ids (willem-bd #3552).
- message-list: widen selection containment to the shared assistant-turn
  container and hint when a selection crosses messages (willem-bd #3553).
- sidecar/api: coalesce concurrent sidecar creates for one parent behind
  a single in-flight promise to prevent duplicates (willem-bd #3554).
- sidecar-trigger/context: force-restore on trigger click so a sidecar
  deleted elsewhere self-heals instead of opening a dead thread
  (willem-bd #3555).
- threads/hooks: surface sidecar cascade cleanup failures via
  console.warn for both lookup and per-thread deletes (Copilot).

Add unit + e2e coverage for parallel metadata, atomic create, and
trigger self-healing.
2026-07-05 00:12:16 +08:00

151 lines
4.0 KiB
TypeScript

import { beforeEach, expect, rs, test } from "@rstest/core";
rs.mock("@/core/api/fetcher", () => ({
fetch: rs.fn(),
}));
rs.mock("@/core/config", () => ({
getBackendBaseURL: () => "http://localhost",
}));
import { fetch as fetcher } from "@/core/api/fetcher";
import type { SidecarContext } from "@/core/sidecar";
import {
createSidecarThread,
findLatestSidecarThread,
} from "@/core/sidecar/api";
import type { AgentThread } from "@/core/threads";
const fetchWithAuth = rs.mocked(fetcher);
beforeEach(() => {
fetchWithAuth.mockReset();
});
function makeThread(
threadId: string,
metadata: Record<string, unknown> = {},
): AgentThread {
return {
thread_id: threadId,
created_at: "2025-01-01T00:00:00Z",
updated_at: "2025-01-01T00:00:00Z",
metadata,
status: "idle",
values: { title: threadId, messages: [] },
} as unknown as AgentThread;
}
function threadResponse(threadId: string): Response {
return new Response(JSON.stringify(makeThread(threadId)), {
status: 200,
headers: { "Content-Type": "application/json" },
});
}
const context: SidecarContext = {
type: "referenced_message",
label: "Assistant message",
messageId: "msg-1",
role: "assistant",
content: "Answer",
};
test("finds the latest sidecar thread for a parent thread", async () => {
const sidecar = makeThread("sidecar-1", {
deerflow_sidecar: true,
parent_thread_id: "parent-1",
});
const search = rs.fn().mockResolvedValue([sidecar]);
await expect(
findLatestSidecarThread({
parentThreadId: "parent-1",
apiClient: { threads: { search } },
}),
).resolves.toBe(sidecar);
expect(search).toHaveBeenCalledWith({
metadata: {
deerflow_sidecar: true,
parent_thread_id: "parent-1",
},
limit: 1,
offset: 0,
sortBy: "updated_at",
sortOrder: "desc",
});
});
test("ignores malformed sidecar search results", async () => {
const search = rs.fn().mockResolvedValue([makeThread("primary-1")]);
await expect(
findLatestSidecarThread({
parentThreadId: "parent-1",
apiClient: { threads: { search } },
}),
).resolves.toBeNull();
});
test("ignores sidecar search results from another parent thread", async () => {
const search = rs.fn().mockResolvedValue([
makeThread("sidecar-1", {
deerflow_sidecar: true,
parent_thread_id: "parent-2",
}),
]);
await expect(
findLatestSidecarThread({
parentThreadId: "parent-1",
apiClient: { threads: { search } },
}),
).resolves.toBeNull();
});
test("coalesces concurrent creates for the same parent into one request", async () => {
let resolveFetch: ((value: Response) => void) | undefined;
fetchWithAuth.mockReturnValueOnce(
new Promise<Response>((resolve) => {
resolveFetch = resolve;
}),
);
const first = createSidecarThread({ parentThreadId: "parent-1", context });
const second = createSidecarThread({ parentThreadId: "parent-1", context });
resolveFetch?.(threadResponse("sidecar-1"));
const [firstThread, secondThread] = await Promise.all([first, second]);
expect(fetchWithAuth).toHaveBeenCalledTimes(1);
expect(firstThread).toEqual(secondThread);
});
test("allows a new create after the in-flight request settles", async () => {
fetchWithAuth
.mockResolvedValueOnce(threadResponse("s-1"))
.mockResolvedValueOnce(threadResponse("s-2"));
await createSidecarThread({ parentThreadId: "parent-1", context });
await createSidecarThread({ parentThreadId: "parent-1", context });
expect(fetchWithAuth).toHaveBeenCalledTimes(2);
});
test("clears the in-flight entry when a create fails", async () => {
fetchWithAuth
.mockResolvedValueOnce(new Response(null, { status: 500 }))
.mockResolvedValueOnce(threadResponse("s-1"));
await expect(
createSidecarThread({ parentThreadId: "parent-1", context }),
).rejects.toThrow("Failed to create side conversation.");
await expect(
createSidecarThread({ parentThreadId: "parent-1", context }),
).resolves.toMatchObject({ thread_id: "s-1" });
expect(fetchWithAuth).toHaveBeenCalledTimes(2);
});