mirror of
https://github.com/bytedance/deer-flow.git
synced 2026-10-28 14:10:37 +00:00
`ArtifactFileDetail` and `loadArtifactContentFromToolCall` re-parsed `write-file:` URLs with `new URL` + `decodeURIComponent` after `parseWriteFileArtifact` had already rejected malformed values. The panel restores its selection from session storage and only type-checks it as a string, so a stale or hand-edited `write-file:%E0%A4%A` (or `write-file://[`) threw `URIError`/`TypeError` during render. Export the guarded parser and read `path` / `tool_call_id` from one parsed result in both places; a rejected URL degrades to the raw string or `undefined` instead of taking the panel down.
237 lines
6.6 KiB
TypeScript
237 lines
6.6 KiB
TypeScript
import { afterEach, describe, expect, it, rs } from "@rstest/core";
|
|
|
|
import {
|
|
ARTIFACT_PREVIEW_MAX_BYTES,
|
|
loadArtifactContent,
|
|
loadArtifactContentFromToolCall,
|
|
} from "@/core/artifacts/loader";
|
|
|
|
describe("loadArtifactContent", () => {
|
|
afterEach(() => {
|
|
rs.restoreAllMocks();
|
|
rs.unstubAllGlobals();
|
|
});
|
|
|
|
it("uses the server content revision when available", async () => {
|
|
rs.spyOn(globalThis, "fetch").mockResolvedValue(
|
|
new Response("content", {
|
|
status: 200,
|
|
headers: { ETag: `"${"a".repeat(64)}"` },
|
|
}),
|
|
);
|
|
|
|
const loaded = await loadArtifactContent({
|
|
filepath: "/mnt/user-data/outputs/report.md",
|
|
threadId: "thread-1",
|
|
});
|
|
|
|
expect(loaded.sha256).toBe("a".repeat(64));
|
|
});
|
|
|
|
it("computes a revision for a complete response without a SHA-256 ETag", async () => {
|
|
rs.spyOn(globalThis, "fetch").mockResolvedValue(
|
|
new Response("content", {
|
|
status: 200,
|
|
headers: { ETag: '"starlette-file-etag"' },
|
|
}),
|
|
);
|
|
|
|
const loaded = await loadArtifactContent({
|
|
filepath: "/mnt/user-data/outputs/page.html",
|
|
threadId: "thread-1",
|
|
});
|
|
|
|
expect(loaded.sha256).toBe(
|
|
"ed7002b439e9ac845f22357d822bac1444730fbdb6016d3ec9432297b9ec9f73",
|
|
);
|
|
});
|
|
|
|
it("requests only the preview byte budget and reports truncation", async () => {
|
|
const bytes = new TextEncoder().encode("preview");
|
|
const fetchMock = rs.fn(async (_url: string, init?: RequestInit) => {
|
|
expect(new Headers(init?.headers).get("Range")).toBe(
|
|
`bytes=0-${ARTIFACT_PREVIEW_MAX_BYTES - 1}`,
|
|
);
|
|
expect(init?.credentials).toBe("include");
|
|
return new Response(bytes, {
|
|
status: 206,
|
|
headers: {
|
|
"Content-Range": `bytes 0-${bytes.length - 1}/2000000`,
|
|
},
|
|
});
|
|
});
|
|
rs.stubGlobal("fetch", fetchMock);
|
|
|
|
const result = await loadArtifactContent({
|
|
filepath: "/mnt/user-data/outputs/large.txt",
|
|
threadId: "thread-1",
|
|
});
|
|
|
|
expect(result.content).toBe("preview");
|
|
expect(result.truncated).toBe(true);
|
|
expect(result.totalBytes).toBe(2_000_000);
|
|
expect(result.sha256).toBeUndefined();
|
|
});
|
|
|
|
it("loads and revisions the full file only when explicitly requested", async () => {
|
|
const fetchMock = rs.fn(async (_url: string, init?: RequestInit) => {
|
|
expect(new Headers(init?.headers).has("Range")).toBe(false);
|
|
return new Response("complete", {
|
|
status: 200,
|
|
headers: { "Content-Length": "8" },
|
|
});
|
|
});
|
|
rs.stubGlobal("fetch", fetchMock);
|
|
|
|
const result = await loadArtifactContent({
|
|
filepath: "/mnt/user-data/outputs/large.txt",
|
|
threadId: "thread-1",
|
|
full: true,
|
|
});
|
|
|
|
expect(result).toMatchObject({
|
|
content: "complete",
|
|
truncated: false,
|
|
totalBytes: 8,
|
|
sha256:
|
|
"eebbf6457e46a7f63acdf9b97390f790ba443d60cfa44b607da7e5c40aa1cc1d",
|
|
});
|
|
});
|
|
|
|
it("does not render a replacement character for a split UTF-8 code point", async () => {
|
|
const emojiBytes = new TextEncoder().encode("abc😀");
|
|
const partial = emojiBytes.slice(0, -2);
|
|
rs.stubGlobal(
|
|
"fetch",
|
|
rs.fn(
|
|
async () =>
|
|
new Response(partial, {
|
|
status: 206,
|
|
headers: {
|
|
"Content-Range": `bytes 0-${partial.length - 1}/${emojiBytes.length + 10}`,
|
|
},
|
|
}),
|
|
),
|
|
);
|
|
|
|
const result = await loadArtifactContent({
|
|
filepath: "/mnt/user-data/outputs/unicode.txt",
|
|
threadId: "thread-1",
|
|
});
|
|
|
|
expect(result.content).toBe("abc");
|
|
});
|
|
|
|
it("treats an unsatisfied range on an empty file as empty content", async () => {
|
|
rs.stubGlobal(
|
|
"fetch",
|
|
rs.fn(
|
|
async () =>
|
|
new Response(null, {
|
|
status: 416,
|
|
headers: { "Content-Range": "bytes */0" },
|
|
}),
|
|
),
|
|
);
|
|
|
|
const result = await loadArtifactContent({
|
|
filepath: "/mnt/user-data/outputs/empty.txt",
|
|
threadId: "thread-1",
|
|
});
|
|
|
|
expect(result).toMatchObject({
|
|
content: "",
|
|
truncated: false,
|
|
totalBytes: 0,
|
|
sha256:
|
|
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
|
});
|
|
});
|
|
|
|
it("parses a weak (W/) SHA-256 ETag returned by a gzipped response", async () => {
|
|
rs.spyOn(globalThis, "fetch").mockResolvedValue(
|
|
new Response("content", {
|
|
status: 200,
|
|
headers: { ETag: `W/"${"b".repeat(64)}"` },
|
|
}),
|
|
);
|
|
|
|
const loaded = await loadArtifactContent({
|
|
filepath: "/mnt/user-data/outputs/report.md",
|
|
threadId: "thread-1",
|
|
});
|
|
|
|
expect(loaded.sha256).toBe("b".repeat(64));
|
|
});
|
|
|
|
it("resolves without throwing when crypto.subtle is unavailable (non-secure context)", async () => {
|
|
rs.stubGlobal("crypto", { subtle: undefined } as unknown as Crypto);
|
|
|
|
const bytes = new TextEncoder().encode("complete");
|
|
rs.stubGlobal(
|
|
"fetch",
|
|
rs.fn(async (_url: string, init?: RequestInit) => {
|
|
expect(new Headers(init?.headers).has("Range")).toBe(false);
|
|
return new Response(bytes, {
|
|
status: 200,
|
|
headers: { "Content-Length": String(bytes.length) },
|
|
});
|
|
}),
|
|
);
|
|
|
|
const loaded = await loadArtifactContent({
|
|
filepath: "/mnt/user-data/outputs/non-secure.html",
|
|
threadId: "thread-1",
|
|
full: true,
|
|
});
|
|
|
|
// FNV-1a fallback keeps preview working and returns a string; because it
|
|
// is not a 64-hex digest the UI treats it as non-editable (no 422 on save).
|
|
expect(typeof loaded.sha256).toBe("string");
|
|
expect(loaded.sha256).toHaveLength(8);
|
|
});
|
|
});
|
|
|
|
describe("loadArtifactContentFromToolCall", () => {
|
|
type ToolCallThread = Parameters<
|
|
typeof loadArtifactContentFromToolCall
|
|
>[0]["thread"];
|
|
|
|
const thread = {
|
|
messages: [
|
|
{
|
|
type: "ai",
|
|
id: "ai-1",
|
|
tool_calls: [
|
|
{
|
|
id: "call-1",
|
|
name: "str_replace",
|
|
args: {
|
|
path: "/mnt/user-data/outputs/report.md",
|
|
content: "patched",
|
|
},
|
|
},
|
|
],
|
|
},
|
|
],
|
|
} as unknown as ToolCallThread;
|
|
|
|
it("returns undefined instead of throwing for a malformed write-file URL", () => {
|
|
expect(
|
|
loadArtifactContentFromToolCall({ url: "write-file://[", thread }),
|
|
).toBeUndefined();
|
|
expect(
|
|
loadArtifactContentFromToolCall({ url: "write-file:%E0%A4%A", thread }),
|
|
).toBeUndefined();
|
|
});
|
|
|
|
it("reads non-write_file tool output through the parsed coordinates", () => {
|
|
expect(
|
|
loadArtifactContentFromToolCall({
|
|
url: "write-file:/mnt/user-data/outputs/report.md?message_id=ai-1&tool_call_id=call-1",
|
|
thread,
|
|
}),
|
|
).toBe("patched");
|
|
});
|
|
});
|