Skip to content

Commit 4479095

Browse files
test(web): attachment preview tests no longer boot the Word engine or count background fetches
The remaining Word renders (60/100-page report, remount reload) still compiled the engine's WebAssembly per test and took 11-15 s on CI. The file now stands in a viewer that shows the prepared body text, so these tests cover fetching, preparation and remounts without the engine. The renewed-URL HTML test counted every call to the stubbed global fetch, including the connection runtime's background environment discovery poll. It now counts only the attachment's own requests. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 6e01cfe commit 4479095

1 file changed

Lines changed: 35 additions & 12 deletions

File tree

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

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,26 @@ vi.mock(import("./ReadOnlySourcePreview"), () => ({
2121
default: ({ text }: { text: string }) => <pre>{text}</pre>,
2222
}));
2323

24+
// The Word engine compiles about 30 MB of WebAssembly in a fresh worker per document, which took
25+
// 10-17 s per test on loaded CI runners. These tests cover fetching, preparation and remounts, so
26+
// the stand-in shows the prepared body's text instead of laying it out.
27+
vi.mock(import("./office/WordDocument"), async () => {
28+
const { use } = await import("react");
29+
const { default: Zip } = await import("jszip");
30+
const texts = new WeakMap<ArrayBuffer, Promise<string>>();
31+
const read = (bytes: ArrayBuffer) =>
32+
Zip.loadAsync(bytes)
33+
.then((zip) => zip.file("word/document.xml")!.async("string"))
34+
.then((xml) => xml.replace(/<[^>]+>/g, " "));
35+
return {
36+
default: ({ bytes }: { bytes: ArrayBuffer }) => {
37+
let text = texts.get(bytes);
38+
if (!text) texts.set(bytes, (text = read(bytes)));
39+
return <pre>{use(text)}</pre>;
40+
},
41+
};
42+
});
43+
2444
import { AttachmentFilePreview } from "./AttachmentFilePreview";
2545
import LargeTextViewer from "./LargeTextViewer";
2646
import VirtualTablePreview from "./VirtualTablePreview";
@@ -1807,7 +1827,7 @@ it("keeps spreadsheet dates on their calendar day west of UTC", async () => {
18071827
}
18081828
});
18091829

1810-
it.each([60, 100])("renders all %i pages of a table-heavy Word report", async (pages) => {
1830+
it.each([60, 100])("prepares all %i pages of a table-heavy Word report", async (pages) => {
18111831
const { default: JSZip } = await import("jszip");
18121832
const archive = new JSZip();
18131833
const paragraph = (text: string) =>
@@ -1835,16 +1855,12 @@ it.each([60, 100])("renders all %i pages of a table-heavy Word report", async (p
18351855
sizeBytes={file.size}
18361856
/>,
18371857
);
1838-
await vi.waitFor(() => expect(screen.container.textContent).toMatch(/\d+ pages/), {
1839-
timeout: 30_000,
1858+
// The first Office preview in the file loads the lazy viewer chain and the preparation worker.
1859+
await vi.waitFor(() => expect(screen.container.textContent).toContain("page " + pages), {
1860+
timeout: 10_000,
18401861
});
18411862
expect(screen.container.textContent).not.toContain("Preview limited");
18421863
expect(screen.container.querySelector('[role="alert"]')).toBeNull();
1843-
const scroll = screen.container.querySelector<HTMLElement>("[data-office-pages]")!;
1844-
await act(() => scroll.scrollTo({ top: scroll.scrollHeight }));
1845-
await vi.waitFor(() => expect(screen.container.textContent).toContain("page " + pages), {
1846-
timeout: 30_000,
1847-
});
18481864
});
18491865

18501866
describe("standalone Mermaid previews", () => {
@@ -1978,6 +1994,13 @@ describe("attachment HTML preview recovery", () => {
19781994
await act(async () => button!.click());
19791995
};
19801996
const alert = (screen: Screen) => screen.container.querySelector('[role="alert"]');
1997+
// The app's connection runtime keeps polling environment discovery through the same global
1998+
// fetch while these tests run, so only the attachment's own requests are counted.
1999+
const attachmentFetches = () =>
2000+
vi
2001+
.mocked(fetch)
2002+
.mock.calls.map(([url]) => url)
2003+
.filter((url) => typeof url === "string" && url.startsWith("data:text/html"));
19812004

19822005
it("keeps a renewed URL for subsequent rendered and source views", async () => {
19832006
setup();
@@ -1994,7 +2017,7 @@ describe("attachment HTML preview recovery", () => {
19942017
await act(async () => {
19952018
await import("./ReadOnlySourcePreview");
19962019
});
1997-
expect(fetch).toHaveBeenCalledExactlyOnceWith(renewedUrl, expect.any(Object));
2020+
expect(attachmentFetches()).toEqual([renewedUrl]);
19982021
await vi.waitFor(() =>
19992022
expect(screen.container.querySelector("pre")?.textContent).toBe("<p>Captured HTML</p>"),
20002023
);
@@ -2003,7 +2026,7 @@ describe("attachment HTML preview recovery", () => {
20032026
expect(screen.container.querySelector("iframe")?.getAttribute("src")).toBe(renewedUrl);
20042027
await toggleMode(screen, "Show HTML source");
20052028
expect(refresh).toHaveBeenCalledTimes(2);
2006-
expect(vi.mocked(fetch).mock.calls.map(([url]) => url)).toEqual([renewedUrl, renewedUrl]);
2029+
expect(attachmentFetches()).toEqual([renewedUrl, renewedUrl]);
20072030
});
20082031

20092032
it("does not fetch or mark an expired URL fresh when reauthorization is unavailable", async () => {
@@ -2018,13 +2041,13 @@ describe("attachment HTML preview recovery", () => {
20182041
now = 61 * 60_000;
20192042
refresh.mockResolvedValue(null);
20202043
await toggleMode(screen, "Show HTML source");
2021-
expect(fetch).not.toHaveBeenCalled();
2044+
expect(attachmentFetches()).toEqual([]);
20222045
expect(alert(screen)?.textContent).toContain("Reconnect to the environment and try again.");
20232046

20242047
await toggleMode(screen, "Show rendered page");
20252048
await toggleMode(screen, "Show HTML source");
20262049
expect(refresh).toHaveBeenCalledTimes(3);
2027-
expect(fetch).not.toHaveBeenCalled();
2050+
expect(attachmentFetches()).toEqual([]);
20282051
});
20292052

20302053
it("can return to rendered HTML after local source decoding fails", async () => {

0 commit comments

Comments
 (0)