|
| 1 | +import {isMobile} from '@libs/Browser'; |
| 2 | +import validateAttachmentFile from '@libs/validateAttachmentFile'; |
| 3 | + |
| 4 | +import type {FileObject} from '@src/types/utils/Attachment'; |
| 5 | + |
| 6 | +import CONST from '../../src/CONST'; |
| 7 | +import * as FileUtils from '../../src/libs/fileDownload/FileUtils'; |
| 8 | + |
| 9 | +// Jest resolves the .native variant of platform-split modules; force the web implementation |
| 10 | +// since the OS-file snapshot behavior under test is web-only. |
| 11 | +jest.mock('@src/libs/snapshotPickedFile', () => jest.requireActual<{default: (file: File, name: string) => Promise<File>}>('@src/libs/snapshotPickedFile/index.ts')); |
| 12 | + |
| 13 | +// The web snapshot only copies bytes on desktop browsers; make the browser type controllable per test. |
| 14 | +jest.mock('@src/libs/Browser', () => ({ |
| 15 | + ...jest.requireActual<Record<string, unknown>>('@src/libs/Browser'), |
| 16 | + isMobile: jest.fn(() => false), |
| 17 | +})); |
| 18 | + |
| 19 | +// Mock only normalizeFileObject and validateImageForCorruption; keep the rest real |
| 20 | +jest.mock('@src/libs/fileDownload/FileUtils', () => { |
| 21 | + const actual = jest.requireActual<typeof FileUtils>('@src/libs/fileDownload/FileUtils'); |
| 22 | + return { |
| 23 | + ...actual, |
| 24 | + normalizeFileObject: jest.fn(), |
| 25 | + validateImageForCorruption: jest.fn(), |
| 26 | + }; |
| 27 | +}); |
| 28 | + |
| 29 | +const mockFileUtils = jest.mocked(FileUtils); |
| 30 | + |
| 31 | +describe('validateAttachmentFile OS-backed file snapshot (web)', () => { |
| 32 | + beforeEach(() => { |
| 33 | + jest.clearAllMocks(); |
| 34 | + mockFileUtils.normalizeFileObject.mockImplementation(async (file) => file); |
| 35 | + mockFileUtils.validateImageForCorruption.mockResolvedValue(undefined); |
| 36 | + jest.mocked(isMobile).mockReturnValue(false); |
| 37 | + }); |
| 38 | + |
| 39 | + it('snapshots the picked file bytes into a new memory-backed File', async () => { |
| 40 | + const createObjectURLSpy = jest.spyOn(URL, 'createObjectURL').mockReturnValue('blob:new-url'); |
| 41 | + try { |
| 42 | + const file: FileObject = new File([new Blob(['content'], {type: 'text/plain'})], 'image.png', {type: 'image/png'}); |
| 43 | + // The RN File polyfill in Jest has no arrayBuffer; emulate the web File API. |
| 44 | + const arrayBufferSpy = jest.fn().mockResolvedValue(new ArrayBuffer(7)); |
| 45 | + Object.defineProperty(file, 'arrayBuffer', {value: arrayBufferSpy, configurable: true}); |
| 46 | + |
| 47 | + const result = await validateAttachmentFile(file); |
| 48 | + |
| 49 | + expect(result.isValid).toBe(true); |
| 50 | + if (!result.isValid) { |
| 51 | + throw new Error('validateAttachmentFile should return a valid result'); |
| 52 | + } |
| 53 | + expect(arrayBufferSpy).toHaveBeenCalled(); |
| 54 | + // The returned File must be a fresh memory-backed copy, not the OS-backed original. |
| 55 | + expect(result.file).not.toBe(file); |
| 56 | + } finally { |
| 57 | + createObjectURLSpy.mockRestore(); |
| 58 | + } |
| 59 | + }); |
| 60 | + |
| 61 | + it('returns FILE_INVALID when the picked file can no longer be read (deleted or modified on disk)', async () => { |
| 62 | + const file: FileObject = new File([new Blob(['content'], {type: 'text/plain'})], 'image.png', {type: 'image/png'}); |
| 63 | + // Chromium rejects the read when the backing OS file changed since it was picked. |
| 64 | + const arrayBufferSpy = jest.fn().mockRejectedValue(new DOMException('The requested file could not be read', 'NotReadableError')); |
| 65 | + Object.defineProperty(file, 'arrayBuffer', {value: arrayBufferSpy, configurable: true}); |
| 66 | + |
| 67 | + const result = await validateAttachmentFile(file); |
| 68 | + |
| 69 | + expect(result.isValid).toBe(false); |
| 70 | + if (result.isValid) { |
| 71 | + throw new Error('validateAttachmentFile should return an invalid result'); |
| 72 | + } |
| 73 | + expect(result.error).toBe(CONST.FILE_VALIDATION_ERRORS.FILE_INVALID); |
| 74 | + }); |
| 75 | + |
| 76 | + it('keeps the lazy OS-backed File on mobile browsers so a multi-file selection is not held in memory', async () => { |
| 77 | + jest.mocked(isMobile).mockReturnValue(true); |
| 78 | + const createObjectURLSpy = jest.spyOn(URL, 'createObjectURL').mockReturnValue('blob:new-url'); |
| 79 | + try { |
| 80 | + const file: FileObject = new File([new Blob(['content'], {type: 'text/plain'})], 'image.png', {type: 'image/png'}); |
| 81 | + const arrayBufferSpy = jest.fn(); |
| 82 | + Object.defineProperty(file, 'arrayBuffer', {value: arrayBufferSpy, configurable: true}); |
| 83 | + |
| 84 | + const result = await validateAttachmentFile(file); |
| 85 | + |
| 86 | + expect(result.isValid).toBe(true); |
| 87 | + if (!result.isValid) { |
| 88 | + throw new Error('validateAttachmentFile should return a valid result'); |
| 89 | + } |
| 90 | + // Mobile-picked files are sandboxed temp copies, so the bytes are not copied into memory. |
| 91 | + expect(arrayBufferSpy).not.toHaveBeenCalled(); |
| 92 | + expect(result.file).toBe(file); |
| 93 | + } finally { |
| 94 | + createObjectURLSpy.mockRestore(); |
| 95 | + } |
| 96 | + }); |
| 97 | + |
| 98 | + it('keeps the lazy File on iPadOS Safari in desktop mode (Macintosh user agent with touch points)', async () => { |
| 99 | + const createObjectURLSpy = jest.spyOn(URL, 'createObjectURL').mockReturnValue('blob:new-url'); |
| 100 | + const originalUserAgent = navigator.userAgent; |
| 101 | + const originalMaxTouchPoints = navigator.maxTouchPoints; |
| 102 | + Object.defineProperty(navigator, 'userAgent', { |
| 103 | + value: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.4 Safari/605.1.15', |
| 104 | + configurable: true, |
| 105 | + }); |
| 106 | + Object.defineProperty(navigator, 'maxTouchPoints', {value: 5, configurable: true}); |
| 107 | + try { |
| 108 | + const file: FileObject = new File([new Blob(['content'], {type: 'text/plain'})], 'image.png', {type: 'image/png'}); |
| 109 | + const arrayBufferSpy = jest.fn(); |
| 110 | + Object.defineProperty(file, 'arrayBuffer', {value: arrayBufferSpy, configurable: true}); |
| 111 | + |
| 112 | + const result = await validateAttachmentFile(file); |
| 113 | + |
| 114 | + expect(result.isValid).toBe(true); |
| 115 | + if (!result.isValid) { |
| 116 | + throw new Error('validateAttachmentFile should return a valid result'); |
| 117 | + } |
| 118 | + expect(arrayBufferSpy).not.toHaveBeenCalled(); |
| 119 | + expect(result.file).toBe(file); |
| 120 | + } finally { |
| 121 | + createObjectURLSpy.mockRestore(); |
| 122 | + Object.defineProperty(navigator, 'userAgent', {value: originalUserAgent, configurable: true}); |
| 123 | + Object.defineProperty(navigator, 'maxTouchPoints', {value: originalMaxTouchPoints, configurable: true}); |
| 124 | + } |
| 125 | + }); |
| 126 | +}); |
0 commit comments