Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(docx): download the uploaded DOCX before seeding collaboration (#7447)

Jacob Beckermane5b4e3d08d7f
diff --git a/apps/web/src/lib/service-clients/service-storage/util/downloadExportedDocument.test.ts b/apps/web/src/lib/service-clients/service-storage/util/downloadExportedDocument.test.ts
new file mode 100644
index 0000000000..ab6f6930c5
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/util/downloadExportedDocument.test.ts
@@ -0,0 +1,56 @@
+import { err, ok } from 'neverthrow';
+import { beforeEach, describe, expect, it, vi } from 'vitest';
+import { downloadExportedDocument } from './downloadExportedDocument';
+
+const mocks = vi.hoisted(() => ({
+ exportDocument: vi.fn(),
+ platformFetch: vi.fn(),
+}));
+vi.mock('../client', () => ({
+ storageServiceClient: { exportDocument: mocks.exportDocument },
+}));
+vi.mock('@core/util/platformFetch', () => ({
+ platformFetch: mocks.platformFetch,
+}));
+
+const docx = new Uint8Array([80, 75, 3, 4, 20, 0]);
+
+describe('downloadExportedDocument', () => {
+ beforeEach(() => {
+ mocks.exportDocument.mockReset();
+ mocks.platformFetch.mockReset();
+ });
+
+ it('downloads the exported file bytes from the presigned URL', async () => {
+ mocks.exportDocument.mockResolvedValue(
+ ok({ presigned_url: 'https://bucket/export.docx' })
+ );
+ mocks.platformFetch.mockResolvedValue(new Response(docx));
+ const bytes = await downloadExportedDocument({ documentId: 'doc' });
+ expect(bytes).toBeInstanceOf(Uint8Array);
+ expect(bytes).toEqual(docx);
+ expect(mocks.exportDocument).toHaveBeenCalledWith({ documentId: 'doc' });
+ expect(mocks.platformFetch).toHaveBeenCalledWith(
+ 'https://bucket/export.docx',
+ undefined
+ );
+ });
+
+ it('fails when the export or the download fails', async () => {
+ mocks.exportDocument.mockResolvedValueOnce(
+ err([{ code: 'SERVER_ERROR', message: 'boom' }])
+ );
+ await expect(
+ downloadExportedDocument({ documentId: 'doc' })
+ ).rejects.toThrow('Unable to export the document.');
+ mocks.exportDocument.mockResolvedValueOnce(
+ ok({ presigned_url: 'https://bucket/export.docx' })
+ );
+ mocks.platformFetch.mockResolvedValueOnce(
+ new Response('', { status: 403 })
+ );
+ await expect(
+ downloadExportedDocument({ documentId: 'doc' })
+ ).rejects.toThrow('Unable to download the exported document');
+ });
+});
diff --git a/apps/web/src/lib/service-clients/service-storage/util/downloadExportedDocument.ts b/apps/web/src/lib/service-clients/service-storage/util/downloadExportedDocument.ts
index 4fab22e116..5808e00a15 100644
--- a/apps/web/src/lib/service-clients/service-storage/util/downloadExportedDocument.ts
+++ b/apps/web/src/lib/service-clients/service-storage/util/downloadExportedDocument.ts
@@ -14,7 +14,7 @@ export async function downloadExportedDocument({
if (exported.isErr()) throw new Error('Unable to export the document.');
const bytes = await fetchPresigned(
exported.value.presigned_url,
- 'arraybuffer'
+ 'arrayBuffer'
);
if (bytes.isErr())
throw new Error(
diff --git a/apps/web/src/lib/service-clients/service-storage/util/fetchPresigned.test.ts b/apps/web/src/lib/service-clients/service-storage/util/fetchPresigned.test.ts
new file mode 100644
index 0000000000..ded00948c1
--- /dev/null
+++ b/apps/web/src/lib/service-clients/service-storage/util/fetchPresigned.test.ts
@@ -0,0 +1,44 @@
+import { beforeEach, describe, expect, it, vi } from 'vitest';
+import { fetchPresigned } from './fetchPresigned';
+
+const platformFetch = vi.hoisted(() => vi.fn());
+vi.mock('@core/util/platformFetch', () => ({ platformFetch }));
+
+const bytes = new Uint8Array([80, 75, 3, 4]);
+
+describe('fetchPresigned', () => {
+ beforeEach(() => platformFetch.mockReset());
+
+ it('reads the body as an ArrayBuffer', async () => {
+ platformFetch.mockResolvedValue(new Response(bytes));
+ const result = await fetchPresigned('https://bucket/file', 'arrayBuffer');
+ expect(new Uint8Array(result._unsafeUnwrap())).toEqual(bytes);
+ });
+
+ // jsdom's Blob may lack arrayBuffer(); size is common to every Blob.
+ it('reads the body as a Blob', async () => {
+ platformFetch.mockResolvedValue(new Response(bytes));
+ const result = await fetchPresigned('https://bucket/file', 'blob');
+ expect(result._unsafeUnwrap().size).toBe(bytes.length);
+ });
+
+ it('reads text and JSON bodies', async () => {
+ platformFetch.mockResolvedValueOnce(new Response('hello'));
+ expect(
+ (await fetchPresigned('https://bucket/file', 'text'))._unsafeUnwrap()
+ ).toBe('hello');
+ platformFetch.mockResolvedValueOnce(new Response('{"a":1}'));
+ expect(
+ (await fetchPresigned('https://bucket/file', 'json'))._unsafeUnwrap()
+ ).toEqual({ a: 1 });
+ });
+
+ it('maps HTTP failures', async () => {
+ platformFetch.mockResolvedValue(new Response('', { status: 404 }));
+ expect(
+ (
+ await fetchPresigned('https://bucket/file', 'arrayBuffer')
+ )._unsafeUnwrapErr()[0]?.code
+ ).toBe('NOT_FOUND');
+ });
+});
diff --git a/apps/web/src/lib/service-clients/service-storage/util/fetchPresigned.ts b/apps/web/src/lib/service-clients/service-storage/util/fetchPresigned.ts
index 1eaab838a9..dd04a145ca 100644
--- a/apps/web/src/lib/service-clients/service-storage/util/fetchPresigned.ts
+++ b/apps/web/src/lib/service-clients/service-storage/util/fetchPresigned.ts
@@ -3,8 +3,9 @@ import { platformFetch } from '@core/util/platformFetch';
import type { ObjectLike, ResultError } from '@core/util/result';
import { err, ok, type Result } from 'neverthrow';
+/** Keyed by the `Response` method that reads each body type. */
type ResultMap = {
- arraybuffer: ArrayBuffer;
+ arrayBuffer: ArrayBuffer;
blob: Blob;
text: string & {};
json: ObjectLike;
@@ -58,8 +59,7 @@ export async function fetchPresigned<K extends keyof ResultMap>(
return httpStatusToError(response.status);
}
- const data =
- await response[responseType as keyof Response & keyof ResultMap]();
+ const data = (await response[responseType]()) as ResultMap[K];
return ok(data);
} catch (error) {
return fetchExceptionToError(error);