|
| 1 | +import type { WriteStream } from 'fs'; |
| 2 | +import { call } from '../../../../tests/vitest/utils.helper'; |
| 3 | + |
| 4 | +const { |
| 5 | + addFileMock, |
| 6 | + addFolderMock, |
| 7 | + closeMock, |
| 8 | + createWriteStreamMock, |
| 9 | + downloadFileV2Mock, |
| 10 | + getBackupFolderTreeSnapshotMock, |
| 11 | +} = vi.hoisted(() => { |
| 12 | + return { |
| 13 | + addFileMock: vi.fn(), |
| 14 | + addFolderMock: vi.fn(), |
| 15 | + closeMock: vi.fn().mockResolvedValue(undefined), |
| 16 | + createWriteStreamMock: vi.fn(), |
| 17 | + downloadFileV2Mock: vi.fn(), |
| 18 | + getBackupFolderTreeSnapshotMock: vi.fn(), |
| 19 | + }; |
| 20 | +}); |
| 21 | + |
| 22 | +vi.mock('fs', () => { |
| 23 | + return { |
| 24 | + default: { |
| 25 | + createWriteStream: createWriteStreamMock, |
| 26 | + }, |
| 27 | + createWriteStream: createWriteStreamMock, |
| 28 | + }; |
| 29 | +}); |
| 30 | + |
| 31 | +vi.mock('./zip.service', () => { |
| 32 | + class FlatFolderZip { |
| 33 | + constructor() { |
| 34 | + // noop |
| 35 | + } |
| 36 | + |
| 37 | + addFile(name: string, source: ReadableStream<Uint8Array>) { |
| 38 | + addFileMock(name, source); |
| 39 | + } |
| 40 | + |
| 41 | + addFolder(name: string) { |
| 42 | + addFolderMock(name); |
| 43 | + } |
| 44 | + |
| 45 | + async close() { |
| 46 | + return closeMock(); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + return { FlatFolderZip }; |
| 51 | +}); |
| 52 | + |
| 53 | +vi.mock('./downloadv2', () => { |
| 54 | + return { |
| 55 | + default: downloadFileV2Mock, |
| 56 | + }; |
| 57 | +}); |
| 58 | + |
| 59 | +vi.mock('@internxt/lib', () => { |
| 60 | + return { |
| 61 | + items: { |
| 62 | + getItemDisplayName: ({ name }: { name: string }) => name, |
| 63 | + }, |
| 64 | + }; |
| 65 | +}); |
| 66 | + |
| 67 | +vi.mock('../../../backend/features/backup/get-backup-folder-tree-snapshot', () => { |
| 68 | + return { |
| 69 | + getBackupFolderTreeSnapshot: getBackupFolderTreeSnapshotMock, |
| 70 | + }; |
| 71 | +}); |
| 72 | + |
| 73 | +import { downloadFolderAsZip } from './download'; |
| 74 | + |
| 75 | +describe('download', () => { |
| 76 | + beforeEach(() => { |
| 77 | + const fakeWriteStream = { |
| 78 | + write: (_chunk: Buffer, cb?: (error?: Error | null) => void) => cb?.(null), |
| 79 | + end: (cb?: (error?: Error | null) => void) => cb?.(null), |
| 80 | + destroy: vi.fn(), |
| 81 | + } as unknown as WriteStream; |
| 82 | + |
| 83 | + createWriteStreamMock.mockReturnValue(fakeWriteStream); |
| 84 | + downloadFileV2Mock.mockClear(); |
| 85 | + addFileMock.mockClear(); |
| 86 | + addFolderMock.mockClear(); |
| 87 | + closeMock.mockClear(); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should add empty file to zip without remote download when backup file has no fileId', async () => { |
| 91 | + getBackupFolderTreeSnapshotMock.mockResolvedValue({ |
| 92 | + data: { |
| 93 | + tree: { |
| 94 | + id: 11, |
| 95 | + plainName: 'root', |
| 96 | + files: [ |
| 97 | + { |
| 98 | + id: 77, |
| 99 | + type: '', |
| 100 | + bucket: 'bucket-id', |
| 101 | + fileId: null, |
| 102 | + size: '0', |
| 103 | + }, |
| 104 | + ], |
| 105 | + children: [], |
| 106 | + }, |
| 107 | + folderDecryptedNames: { |
| 108 | + 11: 'Ubuntu', |
| 109 | + }, |
| 110 | + fileDecryptedNames: { |
| 111 | + 77: 'empty.txt', |
| 112 | + }, |
| 113 | + size: 0, |
| 114 | + }, |
| 115 | + }); |
| 116 | + |
| 117 | + await downloadFolderAsZip( |
| 118 | + 'Ubuntu', |
| 119 | + 'https://gateway.internxt.com', |
| 120 | + 'folder-uuid', |
| 121 | + '/tmp/backup.zip', |
| 122 | + { |
| 123 | + bridgeUser: 'bridge-user', |
| 124 | + bridgePass: 'bridge-pass', |
| 125 | + encryptionKey: 'mnemonic', |
| 126 | + }, |
| 127 | + {}, |
| 128 | + ); |
| 129 | + |
| 130 | + expect(downloadFileV2Mock).not.toHaveBeenCalled(); |
| 131 | + call(addFolderMock).toBe('Ubuntu'); |
| 132 | + expect(addFileMock).toHaveBeenCalledTimes(1); |
| 133 | + |
| 134 | + const addFileCall = addFileMock.mock.calls[0] as [string, ReadableStream<Uint8Array>]; |
| 135 | + const fileName = addFileCall[0]; |
| 136 | + const source = addFileCall[1]; |
| 137 | + expect(fileName).toBe('Ubuntu/empty.txt'); |
| 138 | + expect(source).toBeInstanceOf(ReadableStream); |
| 139 | + |
| 140 | + const reader = source.getReader(); |
| 141 | + const firstRead = await reader.read(); |
| 142 | + |
| 143 | + expect(firstRead.done).toBe(true); |
| 144 | + }); |
| 145 | +}); |
0 commit comments