Skip to content

Commit 6e01cfe

Browse files
test(web): attachment preview Word and large-text tests no longer flake on CI
Nine Word tests rendered a full document through the Word engine only to check prepareOffice's retain/omit decision. Each render boots a fresh document worker that compiles about 30 MB of WebAssembly; on CI those tests took 10-17 s each against the 15 s timeout. The retain cases now check the prepared package, as the omission cases already do, and the two omission renders are removed. The large text recovery test asserted the worker count right after the new text appeared, but that text paints from a main-thread prefetch two frames before the worker starts. It now waits for the worker. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 0790e38 commit 6e01cfe

1 file changed

Lines changed: 26 additions & 60 deletions

File tree

‎apps/web/src/components/files/AttachmentFilePreview.browser.test.tsx‎

Lines changed: 26 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -495,15 +495,16 @@ describe("large text preview recovery", () => {
495495
expect(workers).toHaveLength(1);
496496
state(`${changed}#updated`, "two");
497497
await screen.rerender(preview("file-edit"));
498+
// The prefetched first screen paints before the worker starts, so wait for the worker itself.
499+
await vi.waitFor(() => expect(workers).toHaveLength(2));
498500
await vi.waitFor(() => expect(screen.container.textContent).toContain("changed"));
499-
expect(workers).toHaveLength(2);
500501
await screen.rerender(preview("file-edit", 1));
501502
await vi.waitFor(() => expect(workers).toHaveLength(3));
502503
await vi.waitFor(() => expect(screen.container.textContent).toContain("changed"));
503504
state(`${original}#legacy`);
504505
await screen.rerender(preview("legacy-edit", 1));
506+
await vi.waitFor(() => expect(workers).toHaveLength(4));
505507
await vi.waitFor(() => expect(screen.container.textContent).toContain("original"));
506-
expect(workers).toHaveLength(4);
507508
assetUrlState.mockReturnValue({ _tag: "Failure" });
508509
await screen.rerender(preview("legacy-edit", 1));
509510
expect(screen.container.textContent).toContain("Unable to load file preview.");
@@ -1323,8 +1324,9 @@ async function expandedWordFixture(role: (typeof EXPANDED_WORD_ROLES)[number]) {
13231324
return archive.generateAsync({ type: "blob", compression: "DEFLATE" });
13241325
}
13251326

1326-
// Rendering starts a layout engine per case, which dominated this file on loaded CI
1327-
// runners. The omission decision lives in prepareOffice, so every role is checked there.
1327+
// Rendering starts a Word engine worker per case, which compiles about 30 MB of WebAssembly and
1328+
// took 10-17 s per test on loaded CI runners. Omission and retention are prepareOffice decisions,
1329+
// so they are checked there.
13281330
it.each(EXPANDED_WORD_ROLES)(
13291331
"keeps the body and omits expanded %s from the prepared document",
13301332
async (role) => {
@@ -1356,38 +1358,6 @@ it.each(EXPANDED_WORD_ROLES)(
13561358
},
13571359
);
13581360

1359-
// Two full renders prove the engine accepts a prepared body whose parts were removed
1360-
// (body references for notes, section references for headers and footers) and show the warning.
1361-
it.each(["footnotes", "footer"] as const)(
1362-
"renders the body and warns when omitting expanded %s",
1363-
async (role) => {
1364-
const file = await expandedWordFixture(role);
1365-
const src = URL.createObjectURL(file);
1366-
try {
1367-
const container = document.createElement("div");
1368-
container.style.cssText = "display:flex;flex-direction:column;width:640px;height:700px";
1369-
await using screen = await render(
1370-
<OfficeFilePreview src={src} name="expanded.docx" kind="docx" sizeBytes={file.size} />,
1371-
{ container },
1372-
);
1373-
await vi.waitFor(
1374-
() => {
1375-
expect(screen.container.querySelector('[role="alert"]')?.textContent ?? "").toBe("");
1376-
expect(screen.container.textContent).toContain("body retained");
1377-
},
1378-
{ timeout: 30_000 },
1379-
);
1380-
expect(screen.container.textContent).toContain("Preview limited");
1381-
expect(screen.container.textContent).not.toContain("omitted payload");
1382-
await vi.waitFor(() =>
1383-
expect(screen.container.querySelector("canvas")!.width).toBeGreaterThan(0),
1384-
);
1385-
} finally {
1386-
URL.revokeObjectURL(src);
1387-
}
1388-
},
1389-
);
1390-
13911361
it.each([
13921362
"ordinary parts",
13931363
"unused parts",
@@ -1506,30 +1476,26 @@ it.each([
15061476
),
15071477
);
15081478
}
1509-
const file = await archive.generateAsync({ type: "blob", compression: "DEFLATE" });
1510-
const src = URL.createObjectURL(file);
1511-
try {
1512-
const container = document.createElement("div");
1513-
container.style.cssText = "display:flex;flex-direction:column;width:640px;height:700px";
1514-
await using screen = await render(
1515-
<OfficeFilePreview src={src} name="ordinary.docx" kind="docx" />,
1516-
{ container },
1517-
);
1518-
await vi.waitFor(() => expect(screen.container.textContent).toContain("ordinary body"), {
1519-
timeout: 30_000,
1520-
});
1521-
expect(screen.container.textContent).not.toContain("Preview limited");
1522-
await vi.waitFor(() =>
1523-
expect(screen.container.querySelector("canvas")!.width).toBeGreaterThan(0),
1524-
);
1525-
if (scenario === "default template styles")
1526-
expect(screen.container.textContent).toContain("styled cell");
1527-
if (ordinary) {
1528-
expect(screen.container.textContent).toContain("header retained");
1529-
expect(screen.container.textContent).toContain("footer retained");
1530-
}
1531-
} finally {
1532-
URL.revokeObjectURL(src);
1479+
const { prepareOffice } = await import("./officeArchive");
1480+
const prepared = await prepareOffice(
1481+
await archive.generateAsync({ type: "arraybuffer", compression: "DEFLATE" }),
1482+
"docx",
1483+
);
1484+
expect(prepared.truncated).toBe(false);
1485+
const bounded = await JSZip.loadAsync(prepared.bytes);
1486+
const markup = await bounded.file("word/document.xml")!.async("string");
1487+
expect(markup).toContain("ordinary body");
1488+
expect(markup).toContain("styled body");
1489+
expect(bounded.file("word/styles.xml")).not.toBeNull();
1490+
if (scenario === "default template styles") {
1491+
expect(markup).toContain("styled cell");
1492+
expect(await bounded.file("word/styles.xml")!.async("string")).toContain('w:styleId="Table99"');
1493+
}
1494+
if (ordinary) {
1495+
expect(markup).toMatch(/<w:headerReference/);
1496+
expect(markup).toMatch(/<w:footerReference/);
1497+
for (const role of ["header", "footer", "footnotes", "endnotes", "comments"])
1498+
expect(await bounded.file(`word/${role}.xml`)!.async("string")).toContain(`${role} retained`);
15331499
}
15341500
});
15351501

0 commit comments

Comments
 (0)