Skip to content

A local-first multi-agent app development platform, built on PenguinHarness

test(ui-gallery): 画廊的 mock 也认得文件面板的 Word 预览

lmlihengb162ea6a4d69
diff --git a/packages/ui-gallery/src/app/mock/fixtures.ts b/packages/ui-gallery/src/app/mock/fixtures.ts
index e8a00765..863cc959 100644
--- a/packages/ui-gallery/src/app/mock/fixtures.ts
+++ b/packages/ui-gallery/src/app/mock/fixtures.ts
@@ -1230,6 +1230,7 @@ export function buildFixtures(lang: Lang, now: number): DemoFixtures {
entries: {
"": [
dir("corpus", 1),
+ dir("docs", 2),
dir("scripts", 3),
dir("src", 0),
dir("test", 0),
@@ -1244,6 +1245,9 @@ export function buildFixtures(lang: Lang, now: number): DemoFixtures {
file("memory.md", 5_118, 1),
],
"corpus/penguin-docs": [file("server-api.md", 31_004, 1), file("omni-message.md", 22_870, 1)],
+ // Two Word documents, because the Files panel shows them two different ways: a .docx is
+ // previewed as Markdown the server extracts, a legacy .doc only says to save it as .docx.
+ docs: [file("quarterly-report.docx", 24_576, 2), file("contract.doc", 39_936, 6)],
scripts: [file("check-links.mjs", 1_984, 3)],
src: [file("bm25.ts", 3_102, 5), file("rag.ts", 1_410, 0)],
test: [file("rag.test.ts", 2_206, 0)],
@@ -1268,6 +1272,14 @@ export function buildFixtures(lang: Lang, now: number): DemoFixtures {
"# Memory\n\nMemory files are Markdown with a frontmatter name and description.\n",
"corpus/penguin-docs/server-api.md": "# Server API\n\nEvery route is listed here.\n",
"corpus/penguin-docs/omni-message.md": "# OmniMessage\n\nThe message protocol.\n",
+ // What the server's extractor would hand the panel for this .docx (the mock holds the
+ // Workspace as text, so this one "binary" file is kept as its extracted Markdown).
+ "docs/quarterly-report.docx": `# ${L("季度报告", "Quarterly report")}\n\n${L(
+ "## 摘要\n\n语料库补齐到 4 个上游仓库,检索命中率 0.81。\n\n- 新增 MCP 文档 3 篇\n- 每周链接检查已接入 CI\n",
+ "## Summary\n\nThe corpus now covers four upstream repositories; hit rate 0.81.\n\n- Three new MCP documents\n- The weekly link check runs in CI\n",
+ )}`,
+ // A legacy .doc only ever reaches the "save it as .docx" message, so its bytes never matter.
+ "docs/contract.doc": "",
},
};
diff --git a/packages/ui-gallery/src/app/mock/routes.ts b/packages/ui-gallery/src/app/mock/routes.ts
index f4d772c6..d14a9c1c 100644
--- a/packages/ui-gallery/src/app/mock/routes.ts
+++ b/packages/ui-gallery/src/app/mock/routes.ts
@@ -2120,6 +2120,31 @@ const workspaceFiles = {
});
return empty();
},
+ /**
+ * A .docx's text as Markdown, which the server extracts for the Files panel's Word preview
+ * (see `routes/workspace-files.ts`). The demo Workspace is text, so the "extraction" is simply
+ * what the fixtures hold at that path — the panel routes on the file's name and never reads
+ * these bytes as a document. The legacy-.doc and no-extraction answers are the server's own
+ * codes, so the panel's two refusal messages are exercised here too.
+ */
+ previewText: ({ store, query }: Ctx) => {
+ const path = normalizePath(query.get("path") ?? "");
+ const content = store.f.workspace.content[path];
+ if (content === undefined) notFound("File");
+ const name = path.slice(path.lastIndexOf("/") + 1).toLowerCase();
+ if (name.endsWith(".doc"))
+ fail(
+ 415,
+ "doc_legacy",
+ "Legacy .doc files cannot be previewed; save the file as .docx or download it.",
+ );
+ if (!name.endsWith(".docx"))
+ fail(415, "preview_unavailable", "No text preview for this file type.");
+ return raw(content, {
+ "content-type": "text/markdown; charset=utf-8",
+ etag: `"${content.length}-${path.length}"`,
+ });
+ },
search: ({ store, query }: Ctx): WorkspaceSearchResponse => {
const q = (query.get("q") ?? "").toLowerCase();
if (q === "") fail(400, "empty_query", "Nothing to search for.");
@@ -2144,6 +2169,7 @@ for (const base of ["/api/sessions/:sessionId/files", "/api/projects/:projectId/
router
.get(base, workspaceFiles.list)
.get(`${base}/content`, workspaceFiles.read)
+ .get(`${base}/preview-text`, workspaceFiles.previewText)
.put(`${base}/content`, workspaceFiles.write)
.delete(`${base}/content`, workspaceFiles.remove)
.post(`${base}/reveal`, workspaceFiles.reveal)
diff --git a/packages/ui-gallery/test/mock-api.test.ts b/packages/ui-gallery/test/mock-api.test.ts
index b1135bd9..ae47bb2a 100644
--- a/packages/ui-gallery/test/mock-api.test.ts
+++ b/packages/ui-gallery/test/mock-api.test.ts
@@ -10,6 +10,7 @@ import { describe, expect, it } from "vitest";
import * as api from "../../web/src/api/endpoints";
import { ApiError, UNMOCKED } from "../src/app/mock/errors";
import { answerAsResponse } from "../src/app/mock/fetch-shim";
+import { IDS } from "../src/app/mock/ids";
import { router } from "../src/app/mock/routes";
import { resetStore } from "../src/app/mock/store";
@@ -309,6 +310,46 @@ describe("the mocked API", () => {
expect(missing.status).toBe(404);
});
+ it("extracts a Word document for the Files panel, and answers what it cannot with the server's codes", async () => {
+ const store = resetStore({ lang: "en", signedIn: true });
+ const session = store.f.sessions[0]!.sessionId;
+ const dir = { projectId: store.f.project.projectId, workspace: IDS.workspace, machineId: null };
+
+ // Both ways the panel names a Workspace: by Session and by directory.
+ for (const url of [
+ api.workspaceFileTextUrl(session, "docs/quarterly-report.docx"),
+ api.workspaceDirFileTextUrl(dir, "docs/quarterly-report.docx"),
+ ]) {
+ const extracted = await answerAsResponse("GET", url, undefined);
+ expect(extracted.status).toBe(200);
+ expect(extracted.headers.get("etag")).toMatch(/^".+"$/);
+ expect(await extracted.text()).toContain("Quarterly report");
+ }
+
+ const legacy = await answerAsResponse(
+ "GET",
+ api.workspaceFileTextUrl(session, "docs/contract.doc"),
+ undefined,
+ );
+ expect(legacy.status).toBe(415);
+ expect((await legacy.json()).error.code).toBe("doc_legacy");
+
+ const plain = await answerAsResponse(
+ "GET",
+ api.workspaceFileTextUrl(session, "README.md"),
+ undefined,
+ );
+ expect(plain.status).toBe(415);
+ expect((await plain.json()).error.code).toBe("preview_unavailable");
+
+ const gone = await answerAsResponse(
+ "GET",
+ api.workspaceFileTextUrl(session, "docs/nope.docx"),
+ undefined,
+ );
+ expect(gone.status).toBe(404);
+ });
+
it("refuses what needs a server behind it with one code the app turns into a toast", async () => {
resetStore({ lang: "en", signedIn: true });
await expect(api.createProject({ projectId: "x" })).rejects.toMatchObject({