mirror of
https://github.com/bytedance/deer-flow.git
synced 2026-11-20 11:20:35 +00:00
* feat(frontend): improve file attachment recovery and artifact presentation * feat(extensions): add Office artifact previews with search and navigation * fix: preserve public viewers and HTTP Office previews * fix: address file preview and upload review findings * fix: use typed upload errors and linear bookmark allocation * fix(office-preview): isolate parsing and bound preview resources * fix(ci): install Office preview fixture dependencies for E2E * fix(ci): provision matching Playwright browsers before E2E tests * fix(ci): isolate Python libraries and include Office documentation * test(frontend): wait for download status to finish rendering
257 lines
7.1 KiB
TypeScript
257 lines
7.1 KiB
TypeScript
import { afterEach, beforeEach, expect, rs, test } from "@rstest/core";
|
|
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
|
import { act, renderHook } from "@testing-library/react";
|
|
import { createElement, type ReactNode } from "react";
|
|
|
|
import type { PromptInputMessage } from "@/components/ai-elements/prompt-input";
|
|
import { I18nContext } from "@/core/i18n/context";
|
|
import { enUS } from "@/core/i18n/locales/en-US";
|
|
import { DEFAULT_LOCAL_SETTINGS } from "@/core/settings/local";
|
|
import type { UploadResponse } from "@/core/uploads/api";
|
|
import { AttachmentUploadSession } from "@/core/uploads/attachment-upload-session";
|
|
|
|
// onSent tells callers they may drop one-time composer state (quotes,
|
|
// references, staged files, the stored draft). It must not fire for a send
|
|
// whose attachments never upload, or a retry goes out without that context.
|
|
|
|
const mocks = rs.hoisted(() => ({
|
|
events: [] as string[],
|
|
submit: rs.fn(async () => undefined),
|
|
toastError: rs.fn(),
|
|
}));
|
|
|
|
rs.mock("sonner", () => ({ toast: { error: mocks.toastError } }));
|
|
|
|
rs.mock("@langchain/langgraph-sdk/react", () => ({
|
|
useStream: () => ({
|
|
isLoading: false,
|
|
messages: [],
|
|
stop: rs.fn(async () => undefined),
|
|
submit: mocks.submit,
|
|
values: { artifacts: [], messages: [], title: "", todos: [] },
|
|
}),
|
|
}));
|
|
|
|
const UPLOAD_RESPONSE: UploadResponse = {
|
|
success: true,
|
|
files: [
|
|
{
|
|
filename: "report.pdf",
|
|
size: 42,
|
|
path: "/data/uploads/report.pdf",
|
|
virtual_path: "/mnt/user-data/uploads/report.pdf",
|
|
artifact_url: "/api/threads/thread-1/artifacts/report.pdf",
|
|
},
|
|
],
|
|
message: "ok",
|
|
skipped_files: [],
|
|
};
|
|
|
|
const MESSAGE_WITH_FILE: PromptInputMessage = {
|
|
text: "Compare these",
|
|
files: [
|
|
{
|
|
type: "file",
|
|
url: "blob:report",
|
|
mediaType: "application/pdf",
|
|
filename: "report.pdf",
|
|
file: new File(["%PDF"], "report.pdf", { type: "application/pdf" }),
|
|
},
|
|
],
|
|
};
|
|
|
|
function stubUploadFetch(upload: () => Promise<Response>) {
|
|
rs.stubGlobal("fetch", async (input: unknown) => {
|
|
const url = String(input);
|
|
if (/\/api\/threads\/[^/]+\/uploads$/.test(url)) {
|
|
mocks.events.push("upload");
|
|
return upload();
|
|
}
|
|
if (/\/api\/threads\/[^/]+\/messages\/page/.test(url)) {
|
|
return new Response(
|
|
JSON.stringify({ data: [], has_more: false, next_before_seq: null }),
|
|
{ status: 200, headers: { "Content-Type": "application/json" } },
|
|
);
|
|
}
|
|
throw new Error(`Unexpected fetch: ${url}`);
|
|
});
|
|
}
|
|
|
|
function createWrapper(queryClient: QueryClient) {
|
|
return function SendMessageTestWrapper({
|
|
children,
|
|
}: {
|
|
children: ReactNode;
|
|
}) {
|
|
return createElement(
|
|
QueryClientProvider,
|
|
{ client: queryClient },
|
|
createElement(
|
|
I18nContext.Provider,
|
|
{
|
|
value: { locale: "en-US", setLocale: () => undefined, t: enUS },
|
|
},
|
|
children,
|
|
),
|
|
);
|
|
};
|
|
}
|
|
|
|
async function flushFrames() {
|
|
for (let index = 0; index < 6; index += 1) {
|
|
await act(async () => undefined);
|
|
}
|
|
}
|
|
|
|
async function renderThreadStream() {
|
|
const { useThreadStream } = await import("@/core/threads/hooks");
|
|
const rendered = renderHook(
|
|
() =>
|
|
useThreadStream({
|
|
context: DEFAULT_LOCAL_SETTINGS.context,
|
|
isMock: false,
|
|
threadId: "thread-1",
|
|
}),
|
|
{
|
|
wrapper: createWrapper(
|
|
new QueryClient({ defaultOptions: { queries: { retry: false } } }),
|
|
),
|
|
},
|
|
);
|
|
await flushFrames();
|
|
return rendered;
|
|
}
|
|
|
|
beforeEach(() => {
|
|
mocks.events = [];
|
|
mocks.toastError.mockReset();
|
|
mocks.submit.mockReset();
|
|
mocks.submit.mockImplementation(async () => {
|
|
mocks.events.push("submit");
|
|
});
|
|
});
|
|
|
|
test("does not replace a file failure whose message matches a session diagnostic", async () => {
|
|
stubUploadFetch(async () => new Response("{}"));
|
|
const { result } = await renderThreadStream();
|
|
const cause = new Error("Attachments are not ready");
|
|
const attachmentUploads = new AttachmentUploadSession(async () => {
|
|
throw cause;
|
|
});
|
|
const onSent = rs.fn();
|
|
await act(async () => {
|
|
await expect(
|
|
result.current.sendMessage("thread-1", MESSAGE_WITH_FILE, undefined, {
|
|
attachmentUploads,
|
|
onSent,
|
|
}),
|
|
).rejects.toMatchObject({ code: "failed", cause });
|
|
});
|
|
expect(mocks.toastError).toHaveBeenCalledWith(cause.message);
|
|
expect(mocks.submit).not.toHaveBeenCalled();
|
|
expect(onSent).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test("localizes a session state error using its type", async () => {
|
|
stubUploadFetch(async () => new Response("{}"));
|
|
const { result } = await renderThreadStream();
|
|
const attachmentUploads = new AttachmentUploadSession();
|
|
attachmentUploads.deactivate();
|
|
await act(async () => {
|
|
await expect(
|
|
result.current.sendMessage("thread-1", MESSAGE_WITH_FILE, undefined, {
|
|
attachmentUploads,
|
|
}),
|
|
).rejects.toMatchObject({ code: "inactive" });
|
|
});
|
|
expect(mocks.toastError).toHaveBeenCalledWith(enUS.uploads.notReady);
|
|
expect(mocks.submit).not.toHaveBeenCalled();
|
|
});
|
|
|
|
afterEach(() => {
|
|
rs.unstubAllGlobals();
|
|
});
|
|
|
|
test("does not report a send whose attachment upload fails", async () => {
|
|
stubUploadFetch(
|
|
async () =>
|
|
new Response(JSON.stringify({ detail: "disk full" }), { status: 500 }),
|
|
);
|
|
const { result } = await renderThreadStream();
|
|
const onSent = rs.fn();
|
|
|
|
let sendError: unknown;
|
|
await act(async () => {
|
|
await result.current
|
|
.sendMessage("thread-1", MESSAGE_WITH_FILE, undefined, { onSent })
|
|
.catch((error: unknown) => {
|
|
sendError = error;
|
|
});
|
|
});
|
|
|
|
expect(sendError).toBeInstanceOf(Error);
|
|
expect(mocks.events).toEqual(["upload"]);
|
|
expect(onSent).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test("reports the send after its attachments upload, right before the run is submitted", async () => {
|
|
let finishUpload!: () => void;
|
|
stubUploadFetch(
|
|
() =>
|
|
new Promise<Response>((resolve) => {
|
|
finishUpload = () =>
|
|
resolve(
|
|
new Response(JSON.stringify(UPLOAD_RESPONSE), {
|
|
status: 200,
|
|
headers: { "Content-Type": "application/json" },
|
|
}),
|
|
);
|
|
}),
|
|
);
|
|
const { result } = await renderThreadStream();
|
|
const onSent = rs.fn(() => {
|
|
mocks.events.push("onSent");
|
|
});
|
|
|
|
let sendPromise: Promise<unknown> | undefined;
|
|
act(() => {
|
|
sendPromise = result.current.sendMessage(
|
|
"thread-1",
|
|
MESSAGE_WITH_FILE,
|
|
undefined,
|
|
{ onSent },
|
|
);
|
|
});
|
|
await flushFrames();
|
|
expect(onSent).not.toHaveBeenCalled();
|
|
|
|
finishUpload();
|
|
await act(async () => {
|
|
await sendPromise;
|
|
});
|
|
|
|
expect(mocks.events).toEqual(["upload", "onSent", "submit"]);
|
|
expect(onSent).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
test("reports a text-only send before the run is submitted", async () => {
|
|
stubUploadFetch(async () => {
|
|
throw new Error("no upload expected");
|
|
});
|
|
const { result } = await renderThreadStream();
|
|
const onSent = rs.fn(() => {
|
|
mocks.events.push("onSent");
|
|
});
|
|
|
|
await act(async () => {
|
|
await result.current.sendMessage(
|
|
"thread-1",
|
|
{ text: "Hello", files: [] },
|
|
undefined,
|
|
{ onSent },
|
|
);
|
|
});
|
|
|
|
expect(mocks.events).toEqual(["onSent", "submit"]);
|
|
});
|