@bevel-software/platform-core-frontend 0.11.2 → 0.12.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/THIRD-PARTY-NOTICES.md +670 -395
- package/package.json +6 -2
- package/src/lib/__tests__/api.test.ts +95 -0
- package/src/lib/api.ts +30 -2
- package/src/lib/clipboard.ts +25 -0
- package/src/modules/access/__tests__/useFileAccess.test.tsx +47 -0
- package/src/modules/access/hooks/useFileAccess.ts +23 -5
- package/src/modules/git/__tests__/GitSyncFailedBanner.test.tsx +181 -0
- package/src/modules/git/components/GitSyncFailedBanner.tsx +106 -0
- package/src/modules/library/utils/clipboard.ts +4 -20
- package/src/modules/secrets-vault/components/SecretsPage.tsx +313 -313
- package/src/modules/secrets-vault/components/__tests__/SecretsPage.test.tsx +82 -82
- package/src/modules/workspace/components/FileExplorer.tsx +6 -14
- package/src/modules/workspace/components/FileViewer.tsx +20 -4
- package/src/modules/workspace/components/KbPageHeader.tsx +1 -1
- package/src/modules/workspace/components/__tests__/FileExplorer.test.tsx +3 -1
- package/src/modules/workspace/components/__tests__/FileViewer.test.tsx +17 -0
- package/src/modules/workspace/components/__tests__/KbPageHeader.test.tsx +15 -0
- package/src/modules/workspace/components/renderers/DocxRenderer.tsx +11 -2
- package/src/modules/workspace/components/renderers/DownloadFileButton.tsx +88 -0
- package/src/modules/workspace/components/renderers/EmailRenderer.tsx +309 -0
- package/src/modules/workspace/components/renderers/LegacyOfficeRenderer.tsx +60 -0
- package/src/modules/workspace/components/renderers/PdfRenderer.tsx +261 -33
- package/src/modules/workspace/components/renderers/PptxRenderer.tsx +184 -0
- package/src/modules/workspace/components/renderers/XlsxRenderer.tsx +195 -45
- package/src/modules/workspace/components/renderers/__tests__/DownloadFileButton.test.tsx +48 -0
- package/src/modules/workspace/components/renderers/__tests__/EmailRenderer.test.tsx +176 -0
- package/src/modules/workspace/components/renderers/__tests__/PdfRenderer.test.tsx +214 -76
- package/src/modules/workspace/components/renderers/__tests__/PptxRenderer.test.tsx +125 -0
- package/src/modules/workspace/components/renderers/__tests__/XlsxRenderer.test.tsx +245 -0
- package/src/modules/workspace/components/renderers/__tests__/emailBody.test.ts +117 -0
- package/src/modules/workspace/components/renderers/__tests__/emailMessage.test.ts +404 -0
- package/src/modules/workspace/components/renderers/__tests__/pptxOutline.test.ts +448 -0
- package/src/modules/workspace/components/renderers/__tests__/readBodyCapped.test.ts +117 -0
- package/src/modules/workspace/components/renderers/__tests__/rendererLayout.test.ts +73 -7
- package/src/modules/workspace/components/renderers/__tests__/rendererSelection.test.tsx +107 -0
- package/src/modules/workspace/components/renderers/downloadFile.ts +33 -0
- package/src/modules/workspace/components/renderers/emailBody.ts +168 -0
- package/src/modules/workspace/components/renderers/emailMessage.ts +223 -0
- package/src/modules/workspace/components/renderers/emlMessage.ts +91 -0
- package/src/modules/workspace/components/renderers/htmlSandbox.ts +9 -2
- package/src/modules/workspace/components/renderers/index.ts +82 -6
- package/src/modules/workspace/components/renderers/lazyRenderer.tsx +9 -2
- package/src/modules/workspace/components/renderers/msgMessage.ts +219 -0
- package/src/modules/workspace/components/renderers/pptxOutline.ts +317 -0
- package/src/modules/workspace/components/renderers/readBodyCapped.ts +97 -0
- package/src/modules/workspace/components/renderers/xmlReading.ts +0 -0
- package/src/url-suffix-imports.d.ts +12 -0
|
@@ -1,17 +1,57 @@
|
|
|
1
1
|
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
2
2
|
import { render, screen, waitFor } from '@testing-library/react';
|
|
3
|
+
import userEvent from '@testing-library/user-event';
|
|
3
4
|
import {
|
|
4
5
|
WorkspaceContext,
|
|
5
6
|
type WorkspaceContextValue,
|
|
6
7
|
} from '../../../state/workspace.context';
|
|
7
|
-
import { PdfRenderer } from '../PdfRenderer';
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
9
|
+
/**
|
|
10
|
+
* pdf.js is mocked at the module boundary: the real library needs a worker
|
|
11
|
+
* thread and a 2D canvas context, neither of which happy-dom has. What is
|
|
12
|
+
* under test is everything the renderer OWNS — the fetch, the pager, the
|
|
13
|
+
* fit-to-width scale handed to `getViewport`, parse-failure copy, document
|
|
14
|
+
* teardown — against pdf.js's v5 surface (`getDocument().promise`,
|
|
15
|
+
* `doc.getPage`, `page.render({ canvas, viewport })`).
|
|
16
|
+
*/
|
|
17
|
+
const pdfjsMock = vi.hoisted(() => {
|
|
18
|
+
const renderTask = { promise: Promise.resolve(), cancel: vi.fn() };
|
|
19
|
+
const page = {
|
|
20
|
+
getViewport: vi.fn(({ scale }: { scale: number }) => ({
|
|
21
|
+
width: 600 * scale,
|
|
22
|
+
height: 800 * scale,
|
|
23
|
+
})),
|
|
24
|
+
render: vi.fn(() => renderTask),
|
|
25
|
+
cleanup: vi.fn(),
|
|
26
|
+
};
|
|
27
|
+
const doc = {
|
|
28
|
+
numPages: 3,
|
|
29
|
+
getPage: vi.fn(async (_n: number) => page),
|
|
30
|
+
destroy: vi.fn(async () => {}),
|
|
31
|
+
};
|
|
32
|
+
// The renderer retains the LOADING TASK and destroys that (pdf.js's
|
|
33
|
+
// canonical teardown — it frees the doc in every state); the mock task's
|
|
34
|
+
// destroy forwards to doc.destroy the way the real one does.
|
|
35
|
+
const makeTask = (promise: Promise<unknown>) => ({
|
|
36
|
+
promise,
|
|
37
|
+
destroy: vi.fn(async () => {
|
|
38
|
+
await doc.destroy();
|
|
39
|
+
}),
|
|
40
|
+
});
|
|
41
|
+
return {
|
|
42
|
+
doc,
|
|
43
|
+
page,
|
|
44
|
+
renderTask,
|
|
45
|
+
makeTask,
|
|
46
|
+
getDocument: vi.fn(() => makeTask(Promise.resolve(doc))),
|
|
47
|
+
};
|
|
48
|
+
});
|
|
49
|
+
vi.mock('pdfjs-dist', () => ({
|
|
50
|
+
GlobalWorkerOptions: {},
|
|
51
|
+
getDocument: pdfjsMock.getDocument,
|
|
52
|
+
}));
|
|
53
|
+
|
|
54
|
+
import { PdfRenderer } from '../PdfRenderer';
|
|
15
55
|
|
|
16
56
|
const baseProps = {
|
|
17
57
|
filePath: 'Inbox/brief.pdf',
|
|
@@ -21,62 +61,105 @@ const baseProps = {
|
|
|
21
61
|
|
|
22
62
|
function renderWithWorkspace(workspaceId: string | null = 'ws-1') {
|
|
23
63
|
return render(
|
|
24
|
-
<WorkspaceContext.Provider
|
|
64
|
+
<WorkspaceContext.Provider
|
|
65
|
+
value={{ workspaceId } as unknown as WorkspaceContextValue}
|
|
66
|
+
>
|
|
25
67
|
<PdfRenderer {...baseProps} />
|
|
26
68
|
</WorkspaceContext.Provider>,
|
|
27
69
|
);
|
|
28
70
|
}
|
|
29
71
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
72
|
+
function stubPdfFetch() {
|
|
73
|
+
const fetchSpy = vi.fn<typeof fetch>(async () =>
|
|
74
|
+
new Response(new Blob([new Uint8Array([0x25, 0x50, 0x44, 0x46])]), { status: 200 }),
|
|
75
|
+
);
|
|
76
|
+
vi.stubGlobal('fetch', fetchSpy);
|
|
77
|
+
return fetchSpy;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// happy-dom lays nothing out, so every element measures 0 wide — and the
|
|
81
|
+
// renderer deliberately waits for a real measurement before drawing. Give
|
|
82
|
+
// the page column a width so fit-to-width has something to fit.
|
|
83
|
+
const CONTAINER_WIDTH = 900;
|
|
33
84
|
|
|
34
|
-
// jsdom does not implement URL.createObjectURL / revokeObjectURL — install
|
|
35
|
-
// them as own-properties on the global URL class. We must NOT replace the
|
|
36
|
-
// URL global itself (vi.stubGlobal('URL', …)) because Response/Blob/fetch
|
|
37
|
-
// internals construct `new URL(…)` and would break.
|
|
38
85
|
beforeEach(() => {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
objectUrls.push(url);
|
|
43
|
-
return url;
|
|
86
|
+
Object.defineProperty(HTMLElement.prototype, 'clientWidth', {
|
|
87
|
+
configurable: true,
|
|
88
|
+
get: () => CONTAINER_WIDTH,
|
|
44
89
|
});
|
|
45
|
-
|
|
46
|
-
(
|
|
47
|
-
|
|
48
|
-
(
|
|
49
|
-
|
|
90
|
+
pdfjsMock.getDocument.mockClear();
|
|
91
|
+
pdfjsMock.getDocument.mockImplementation(() => pdfjsMock.makeTask(Promise.resolve(pdfjsMock.doc)));
|
|
92
|
+
pdfjsMock.doc.getPage.mockClear();
|
|
93
|
+
pdfjsMock.doc.getPage.mockImplementation(async (_n: number) => pdfjsMock.page);
|
|
94
|
+
pdfjsMock.doc.destroy.mockClear();
|
|
95
|
+
pdfjsMock.page.getViewport.mockClear();
|
|
96
|
+
pdfjsMock.page.render.mockClear();
|
|
97
|
+
pdfjsMock.page.cleanup.mockClear();
|
|
50
98
|
});
|
|
51
99
|
|
|
52
100
|
afterEach(() => {
|
|
53
|
-
delete (
|
|
54
|
-
delete (URL as unknown as { revokeObjectURL?: unknown }).revokeObjectURL;
|
|
101
|
+
delete (HTMLElement.prototype as { clientWidth?: unknown }).clientWidth;
|
|
55
102
|
vi.unstubAllGlobals();
|
|
56
103
|
vi.restoreAllMocks();
|
|
57
104
|
});
|
|
58
105
|
|
|
59
106
|
describe('PdfRenderer', () => {
|
|
60
|
-
it('fetches the
|
|
61
|
-
const
|
|
62
|
-
type: 'application/pdf',
|
|
63
|
-
});
|
|
64
|
-
const fetchSpy = vi.fn<typeof fetch>(async () => new Response(pdfBytes, { status: 200 }));
|
|
65
|
-
vi.stubGlobal('fetch', fetchSpy);
|
|
66
|
-
|
|
107
|
+
it('fetches the bytes, opens the document, and renders page 1 fit to the column width', async () => {
|
|
108
|
+
const fetchSpy = stubPdfFetch();
|
|
67
109
|
renderWithWorkspace('ws-1');
|
|
68
110
|
|
|
69
|
-
|
|
70
|
-
expect(iframe.tagName).toBe('IFRAME');
|
|
71
|
-
expect((iframe as HTMLIFrameElement).src).toBe(objectUrls[0]);
|
|
72
|
-
|
|
111
|
+
await screen.findByText('Page 1 of 3');
|
|
73
112
|
expect(fetchSpy).toHaveBeenCalledTimes(1);
|
|
74
113
|
const calledUrl = String(fetchSpy.mock.calls[0]![0]);
|
|
75
114
|
expect(calledUrl).toContain('/api/workspace/ws-1/file/raw');
|
|
76
115
|
expect(calledUrl).toContain('path=Inbox%2Fbrief.pdf');
|
|
116
|
+
|
|
117
|
+
await waitFor(() => expect(pdfjsMock.page.render).toHaveBeenCalledTimes(1));
|
|
118
|
+
expect(pdfjsMock.doc.getPage).toHaveBeenCalledWith(1);
|
|
119
|
+
// Fit-to-width: base viewport is 600 wide at scale 1, the column is 900,
|
|
120
|
+
// so the render viewport is requested at scale 900/600 (dpr 1 in tests).
|
|
121
|
+
expect(pdfjsMock.page.getViewport).toHaveBeenLastCalledWith({ scale: 900 / 600 });
|
|
122
|
+
const renderArgs = (pdfjsMock.page.render.mock.calls[0] as unknown[])[0] as {
|
|
123
|
+
canvas: HTMLCanvasElement;
|
|
124
|
+
viewport: { width: number };
|
|
125
|
+
};
|
|
126
|
+
expect(renderArgs.canvas).toBeInstanceOf(HTMLCanvasElement);
|
|
127
|
+
expect(renderArgs.canvas.width).toBe(900);
|
|
77
128
|
});
|
|
78
129
|
|
|
79
|
-
it('
|
|
130
|
+
it('pages forward and back, and pins the pager at both ends', async () => {
|
|
131
|
+
stubPdfFetch();
|
|
132
|
+
const user = userEvent.setup();
|
|
133
|
+
renderWithWorkspace('ws-1');
|
|
134
|
+
|
|
135
|
+
await screen.findByText('Page 1 of 3');
|
|
136
|
+
const prev = screen.getByRole('button', { name: 'Prev' });
|
|
137
|
+
const next = screen.getByRole('button', { name: 'Next' });
|
|
138
|
+
// First page: nowhere back to go.
|
|
139
|
+
expect(prev).toBeDisabled();
|
|
140
|
+
|
|
141
|
+
await user.click(next);
|
|
142
|
+
await screen.findByText('Page 2 of 3');
|
|
143
|
+
await waitFor(() => expect(pdfjsMock.doc.getPage).toHaveBeenCalledWith(2));
|
|
144
|
+
expect(prev).toBeEnabled();
|
|
145
|
+
|
|
146
|
+
await user.click(next);
|
|
147
|
+
await screen.findByText('Page 3 of 3');
|
|
148
|
+
// Last page: the pager stops, it does not wrap.
|
|
149
|
+
expect(next).toBeDisabled();
|
|
150
|
+
|
|
151
|
+
await user.click(prev);
|
|
152
|
+
await screen.findByText('Page 2 of 3');
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it('offers Download in the toolbar', async () => {
|
|
156
|
+
stubPdfFetch();
|
|
157
|
+
renderWithWorkspace('ws-1');
|
|
158
|
+
await screen.findByText('Page 1 of 3');
|
|
159
|
+
expect(screen.getByRole('button', { name: /Download/ })).toBeInTheDocument();
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it('shows the loading state until the document resolves', async () => {
|
|
80
163
|
let resolveFetch!: (res: Response) => void;
|
|
81
164
|
vi.stubGlobal(
|
|
82
165
|
'fetch',
|
|
@@ -89,14 +172,43 @@ describe('PdfRenderer', () => {
|
|
|
89
172
|
);
|
|
90
173
|
|
|
91
174
|
renderWithWorkspace('ws-1');
|
|
92
|
-
expect(screen.getByText('Loading PDF
|
|
175
|
+
expect(screen.getByText('Loading PDF…')).toBeInTheDocument();
|
|
93
176
|
|
|
94
|
-
resolveFetch(
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
177
|
+
resolveFetch(new Response(new Blob([new Uint8Array([0x25])]), { status: 200 }));
|
|
178
|
+
await screen.findByText('Page 1 of 3');
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it('says the file could not be parsed as a PDF when pdf.js rejects it, and still offers Download', async () => {
|
|
182
|
+
stubPdfFetch();
|
|
183
|
+
// A renamed .txt, a truncated upload — the fetch succeeded, the parse did not.
|
|
184
|
+
pdfjsMock.getDocument.mockImplementation(() =>
|
|
185
|
+
pdfjsMock.makeTask(Promise.reject(new Error('Invalid PDF structure'))),
|
|
98
186
|
);
|
|
99
|
-
|
|
187
|
+
vi.spyOn(console, 'warn').mockImplementation(() => {});
|
|
188
|
+
|
|
189
|
+
renderWithWorkspace('ws-1');
|
|
190
|
+
await screen.findByText('This file could not be parsed as a PDF.');
|
|
191
|
+
// The bytes may still be fine for a desktop viewer.
|
|
192
|
+
expect(screen.getByRole('button', { name: /Download/ })).toBeInTheDocument();
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it('destroys the rejected loading task on parse failure — the error view must not keep it alive — without double-destroying on unmount', async () => {
|
|
196
|
+
stubPdfFetch();
|
|
197
|
+
let task!: ReturnType<typeof pdfjsMock.makeTask>;
|
|
198
|
+
pdfjsMock.getDocument.mockImplementation(() => {
|
|
199
|
+
task = pdfjsMock.makeTask(Promise.reject(new Error('Invalid PDF structure')));
|
|
200
|
+
return task;
|
|
201
|
+
});
|
|
202
|
+
vi.spyOn(console, 'warn').mockImplementation(() => {});
|
|
203
|
+
|
|
204
|
+
const { unmount } = renderWithWorkspace('ws-1');
|
|
205
|
+
await screen.findByText('This file could not be parsed as a PDF.');
|
|
206
|
+
// Torn down on the rejection path, while the error view is mounted…
|
|
207
|
+
expect(task.destroy).toHaveBeenCalledTimes(1);
|
|
208
|
+
|
|
209
|
+
// …and the effect cleanup does not destroy it a second time.
|
|
210
|
+
unmount();
|
|
211
|
+
expect(task.destroy).toHaveBeenCalledTimes(1);
|
|
100
212
|
});
|
|
101
213
|
|
|
102
214
|
it('renders an error message when the fetch returns a non-OK response', async () => {
|
|
@@ -104,11 +216,8 @@ describe('PdfRenderer', () => {
|
|
|
104
216
|
'fetch',
|
|
105
217
|
vi.fn(async () => new Response('not found', { status: 404 })),
|
|
106
218
|
);
|
|
107
|
-
|
|
108
219
|
renderWithWorkspace('ws-1');
|
|
109
|
-
await
|
|
110
|
-
expect(screen.getByText(/Failed to load PDF \(HTTP 404\)/)).toBeInTheDocument(),
|
|
111
|
-
);
|
|
220
|
+
await screen.findByText(/Failed to load PDF \(HTTP 404\)/);
|
|
112
221
|
});
|
|
113
222
|
|
|
114
223
|
it('renders an error message when fetch throws (network failure)', async () => {
|
|
@@ -118,23 +227,68 @@ describe('PdfRenderer', () => {
|
|
|
118
227
|
throw new Error('network down');
|
|
119
228
|
}),
|
|
120
229
|
);
|
|
121
|
-
|
|
122
230
|
renderWithWorkspace('ws-1');
|
|
123
|
-
await
|
|
231
|
+
await screen.findByText('network down');
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
it('destroys the worker-side document on unmount', async () => {
|
|
235
|
+
stubPdfFetch();
|
|
236
|
+
const { unmount } = renderWithWorkspace('ws-1');
|
|
237
|
+
await screen.findByText('Page 1 of 3');
|
|
238
|
+
|
|
239
|
+
unmount();
|
|
240
|
+
expect(pdfjsMock.doc.destroy).toHaveBeenCalled();
|
|
124
241
|
});
|
|
125
242
|
|
|
126
|
-
it('
|
|
127
|
-
|
|
128
|
-
|
|
243
|
+
it('destroys the LOADING TASK on unmount even while the document is still resolving — a doc that resolves after cancellation is torn down, not leaked', async () => {
|
|
244
|
+
stubPdfFetch();
|
|
245
|
+
let resolveDoc!: (d: unknown) => void;
|
|
246
|
+
let task!: ReturnType<typeof pdfjsMock.makeTask>;
|
|
247
|
+
pdfjsMock.getDocument.mockImplementation(() => {
|
|
248
|
+
task = pdfjsMock.makeTask(
|
|
249
|
+
new Promise((resolve) => {
|
|
250
|
+
resolveDoc = resolve;
|
|
251
|
+
}),
|
|
252
|
+
);
|
|
253
|
+
return task;
|
|
129
254
|
});
|
|
130
|
-
vi.stubGlobal('fetch', vi.fn(async () => new Response(pdfBytes, { status: 200 })));
|
|
131
255
|
|
|
132
256
|
const { unmount } = renderWithWorkspace('ws-1');
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
expect(createObjectURLSpy).toHaveBeenCalledTimes(1);
|
|
257
|
+
// Let the fetch finish and getDocument fire.
|
|
258
|
+
await waitFor(() => expect(pdfjsMock.getDocument).toHaveBeenCalled());
|
|
136
259
|
unmount();
|
|
137
|
-
|
|
260
|
+
// The task is destroyed although its promise never resolved…
|
|
261
|
+
expect(task.destroy).toHaveBeenCalled();
|
|
262
|
+
// …and a late resolution changes nothing (destroy already covered it).
|
|
263
|
+
resolveDoc(pdfjsMock.doc);
|
|
264
|
+
await new Promise((r) => setTimeout(r, 0));
|
|
265
|
+
expect(pdfjsMock.doc.destroy).toHaveBeenCalled();
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
it('releases the PRIOR page (page.cleanup) only after the next page has rendered — never mid-render', async () => {
|
|
269
|
+
stubPdfFetch();
|
|
270
|
+
const makePage = () => ({
|
|
271
|
+
getViewport: vi.fn(({ scale }: { scale: number }) => ({ width: 600 * scale, height: 800 * scale })),
|
|
272
|
+
render: vi.fn(() => ({ promise: Promise.resolve(), cancel: vi.fn() })),
|
|
273
|
+
cleanup: vi.fn(),
|
|
274
|
+
});
|
|
275
|
+
const pages: Record<number, ReturnType<typeof makePage>> = { 1: makePage(), 2: makePage() };
|
|
276
|
+
pdfjsMock.doc.getPage.mockImplementation(async (n: number) => pages[n] ?? makePage());
|
|
277
|
+
|
|
278
|
+
const user = userEvent.setup();
|
|
279
|
+
renderWithWorkspace('ws-1');
|
|
280
|
+
await screen.findByText('Page 1 of 3');
|
|
281
|
+
await waitFor(() => expect(pages[1].render).toHaveBeenCalled());
|
|
282
|
+
// Page 1's resources stay live while page 1 is the one on screen.
|
|
283
|
+
expect(pages[1].cleanup).not.toHaveBeenCalled();
|
|
284
|
+
|
|
285
|
+
await user.click(screen.getByRole('button', { name: 'Next' }));
|
|
286
|
+
await screen.findByText('Page 2 of 3');
|
|
287
|
+
await waitFor(() => expect(pages[2].render).toHaveBeenCalled());
|
|
288
|
+
// After page 2's render settles the prior page is released…
|
|
289
|
+
await waitFor(() => expect(pages[1].cleanup).toHaveBeenCalledTimes(1));
|
|
290
|
+
// …and the CURRENT page is not.
|
|
291
|
+
expect(pages[2].cleanup).not.toHaveBeenCalled();
|
|
138
292
|
});
|
|
139
293
|
|
|
140
294
|
it('does not fetch when workspaceId is null', async () => {
|
|
@@ -146,22 +300,6 @@ describe('PdfRenderer', () => {
|
|
|
146
300
|
// Give effects a chance to run.
|
|
147
301
|
await new Promise((r) => setTimeout(r, 10));
|
|
148
302
|
expect(fetchSpy).not.toHaveBeenCalled();
|
|
149
|
-
expect(screen.getByText('Loading PDF
|
|
150
|
-
});
|
|
151
|
-
|
|
152
|
-
it('URL-encodes filePath segments so paths with spaces and slashes work', async () => {
|
|
153
|
-
const pdfBytes = new Blob([new Uint8Array([0x25])], { type: 'application/pdf' });
|
|
154
|
-
const fetchSpy = vi.fn<typeof fetch>(async () => new Response(pdfBytes, { status: 200 }));
|
|
155
|
-
vi.stubGlobal('fetch', fetchSpy);
|
|
156
|
-
|
|
157
|
-
render(
|
|
158
|
-
<WorkspaceContext.Provider value={workspaceCtx('ws-1')}>
|
|
159
|
-
<PdfRenderer {...baseProps} filePath="My Folder/Q3 report.pdf" />
|
|
160
|
-
</WorkspaceContext.Provider>,
|
|
161
|
-
);
|
|
162
|
-
|
|
163
|
-
await waitFor(() => screen.getByTitle('My Folder/Q3 report.pdf'));
|
|
164
|
-
const calledUrl = String(fetchSpy.mock.calls[0]![0]);
|
|
165
|
-
expect(calledUrl).toContain('path=My%20Folder%2FQ3%20report.pdf');
|
|
303
|
+
expect(screen.getByText('Loading PDF…')).toBeInTheDocument();
|
|
166
304
|
});
|
|
167
305
|
});
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
|
+
import { render, screen, within } from '@testing-library/react';
|
|
3
|
+
import JSZip from 'jszip';
|
|
4
|
+
import {
|
|
5
|
+
WorkspaceContext,
|
|
6
|
+
type WorkspaceContextValue,
|
|
7
|
+
} from '../../../state/workspace.context';
|
|
8
|
+
|
|
9
|
+
const apiMock = vi.hoisted(() => ({ authFetch: vi.fn() }));
|
|
10
|
+
vi.mock('../../../../../lib/api', () => ({ authFetch: apiMock.authFetch }));
|
|
11
|
+
|
|
12
|
+
import { PptxRenderer } from '../PptxRenderer';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The viewer over a real (minimal) deck: parsing is `pptxOutline.test.ts`'s
|
|
16
|
+
* job; this proves the outline actually reaches the screen in the promised
|
|
17
|
+
* shape — slide sections in order, notes under their slide, the up-front
|
|
18
|
+
* "this is an outline" note, and Download on both the happy and the failed
|
|
19
|
+
* path. Everything from the document renders as React text nodes, so there
|
|
20
|
+
* is no HTML-injection surface to pin here (unlike `DocxRenderer`).
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
async function deckBytes(): Promise<ArrayBuffer> {
|
|
24
|
+
const zip = new JSZip();
|
|
25
|
+
zip.file(
|
|
26
|
+
'ppt/slides/slide1.xml',
|
|
27
|
+
'<p:sld><a:p><a:r><a:t>Road</a:t></a:r><a:r><a:t>map</a:t></a:r></a:p></p:sld>',
|
|
28
|
+
);
|
|
29
|
+
zip.file('ppt/slides/slide2.xml', '<p:sld><a:p><a:t>The ask</a:t></a:p></p:sld>');
|
|
30
|
+
zip.file(
|
|
31
|
+
'ppt/notesSlides/notesSlide2.xml',
|
|
32
|
+
'<p:notes><a:p><a:t>pause for questions</a:t></a:p></p:notes>',
|
|
33
|
+
);
|
|
34
|
+
return zip.generateAsync({ type: 'arraybuffer' });
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function renderPptx() {
|
|
38
|
+
return render(
|
|
39
|
+
<WorkspaceContext.Provider
|
|
40
|
+
value={{ workspaceId: 'ws-1' } as unknown as WorkspaceContextValue}
|
|
41
|
+
>
|
|
42
|
+
<PptxRenderer filePath="Inbox/all-hands.pptx" content="" onSave={async () => {}} />
|
|
43
|
+
</WorkspaceContext.Provider>,
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
beforeEach(() => {
|
|
48
|
+
apiMock.authFetch.mockReset();
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
describe('PptxRenderer', () => {
|
|
52
|
+
it('renders the outline: slide sections in order, runs joined, notes attached', async () => {
|
|
53
|
+
const bytes = await deckBytes();
|
|
54
|
+
apiMock.authFetch.mockResolvedValue({ ok: true, arrayBuffer: async () => bytes });
|
|
55
|
+
|
|
56
|
+
renderPptx();
|
|
57
|
+
|
|
58
|
+
const slide1 = await screen.findByRole('heading', { name: 'Slide 1' });
|
|
59
|
+
const slide2 = screen.getByRole('heading', { name: 'Slide 2' });
|
|
60
|
+
// Package order on screen.
|
|
61
|
+
expect(
|
|
62
|
+
slide1.compareDocumentPosition(slide2) & Node.DOCUMENT_POSITION_FOLLOWING,
|
|
63
|
+
).toBeTruthy();
|
|
64
|
+
// Runs joined with no separator.
|
|
65
|
+
expect(screen.getByText('Roadmap')).toBeInTheDocument();
|
|
66
|
+
// Notes under THEIR slide: scoped inside Slide 2's section, so a
|
|
67
|
+
// wrong-slide attachment fails instead of passing on a page-wide match.
|
|
68
|
+
const slide2Section = slide2.closest('section')!;
|
|
69
|
+
expect(within(slide2Section).getByText('Notes')).toBeInTheDocument();
|
|
70
|
+
expect(within(slide2Section).getByText('pause for questions')).toBeInTheDocument();
|
|
71
|
+
expect(within(slide1.closest('section')!).queryByText('Notes')).not.toBeInTheDocument();
|
|
72
|
+
// The honest header: what this view is, and the way to the real deck.
|
|
73
|
+
expect(screen.getByText(/Text outline of the presentation/)).toBeInTheDocument();
|
|
74
|
+
expect(screen.getByRole('button', { name: /Download/ })).toBeInTheDocument();
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('says the file could not be parsed as a .pptx and still offers Download', async () => {
|
|
78
|
+
const bytes = new TextEncoder().encode('renamed zip that is not one').buffer;
|
|
79
|
+
apiMock.authFetch.mockResolvedValue({ ok: true, arrayBuffer: async () => bytes });
|
|
80
|
+
|
|
81
|
+
renderPptx();
|
|
82
|
+
|
|
83
|
+
expect(
|
|
84
|
+
await screen.findByText(/This file could not be parsed as a \.pptx/),
|
|
85
|
+
).toBeInTheDocument();
|
|
86
|
+
expect(screen.getByRole('button', { name: /Download/ })).toBeInTheDocument();
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('rejects an over-cap declared Content-Length AND ends the transfer — even when the body has already errored', async () => {
|
|
90
|
+
// `body.cancel()` REJECTS on a stream that has already errored, and a
|
|
91
|
+
// rejection here used to escape the guard into the generic parse-error
|
|
92
|
+
// path — the reader must leave by the oversized door regardless.
|
|
93
|
+
let signal: AbortSignal | undefined;
|
|
94
|
+
apiMock.authFetch.mockImplementation(async (_url: string, init?: { signal?: AbortSignal }) => {
|
|
95
|
+
signal = init?.signal;
|
|
96
|
+
return {
|
|
97
|
+
ok: true,
|
|
98
|
+
headers: {
|
|
99
|
+
get: (name: string) =>
|
|
100
|
+
name === 'content-length' ? String(201 * 1024 * 1024) : null,
|
|
101
|
+
},
|
|
102
|
+
body: { cancel: () => Promise.reject(new Error('stream already errored')) },
|
|
103
|
+
};
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
renderPptx();
|
|
107
|
+
|
|
108
|
+
expect(
|
|
109
|
+
await screen.findByText(/This presentation is too large to preview/),
|
|
110
|
+
).toBeInTheDocument();
|
|
111
|
+
expect(screen.getByRole('button', { name: /Download/ })).toBeInTheDocument();
|
|
112
|
+
// The guard is only real if it ends the transfer, not just the render.
|
|
113
|
+
expect(signal?.aborted).toBe(true);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it('reports a transport failure as a load error', async () => {
|
|
117
|
+
apiMock.authFetch.mockResolvedValue({ ok: false, status: 403 });
|
|
118
|
+
|
|
119
|
+
renderPptx();
|
|
120
|
+
|
|
121
|
+
expect(
|
|
122
|
+
await screen.findByText(/Failed to load presentation \(HTTP 403\)/),
|
|
123
|
+
).toBeInTheDocument();
|
|
124
|
+
});
|
|
125
|
+
});
|