|
| 1 | +import { |
| 2 | + startTransfer, |
| 3 | + removeTransfer, |
| 4 | + stopTransfer, |
| 5 | + resumeTransfer, |
| 6 | + showSelectFileDialog, |
| 7 | + showSelectFolderDialog, |
| 8 | + showPreferences, |
| 9 | + getAllTransfers, |
| 10 | + getTransfer, |
| 11 | + showDirectory, |
| 12 | + modifyTransfer, |
| 13 | + readAsArrayBuffer, |
| 14 | + readChunkAsArrayBuffer, |
| 15 | +} from '../../src/index'; |
| 16 | +import { |
| 17 | + mockFetch, |
| 18 | + setupSdk, |
| 19 | + resetSdk, |
| 20 | + lastFetchCall, |
| 21 | + rpcOk, |
| 22 | + downloadSpec, |
| 23 | +} from '../test-helpers'; |
| 24 | + |
| 25 | +describe('Desktop App', () => { |
| 26 | + const APP_ID = 'test-app-id'; |
| 27 | + |
| 28 | + beforeEach(() => { |
| 29 | + setupSdk({mode: 'desktop-app', appId: APP_ID}); |
| 30 | + mockFetch((url, body) => rpcOk({}, body?.id || 0)); |
| 31 | + }); |
| 32 | + |
| 33 | + afterEach(() => { |
| 34 | + resetSdk(); |
| 35 | + jest.restoreAllMocks(); |
| 36 | + }); |
| 37 | + |
| 38 | + describe('startTransfer', () => { |
| 39 | + it('should call start_transfer RPC', async () => { |
| 40 | + await startTransfer(downloadSpec({token: 'test-token'}), {use_absolute_destination_path: false}); |
| 41 | + |
| 42 | + const call = lastFetchCall(); |
| 43 | + expect(call.body.method).toBe('start_transfer'); |
| 44 | + expect(call.body.params).toEqual({ |
| 45 | + transfer_spec: {direction: 'receive', remote_host: 'files.example.com', paths: [{source: '/remote/file.txt'}], token: 'test-token'}, |
| 46 | + desktop_spec: {use_absolute_destination_path: false}, |
| 47 | + app_id: APP_ID, |
| 48 | + }); |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + describe('removeTransfer', () => { |
| 53 | + it('should call remove_transfer RPC', async () => { |
| 54 | + await removeTransfer('transfer-uuid-123'); |
| 55 | + |
| 56 | + const call = lastFetchCall(); |
| 57 | + expect(call.body.method).toBe('remove_transfer'); |
| 58 | + expect(call.body.params).toEqual({transfer_id: 'transfer-uuid-123'}); |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + describe('stopTransfer', () => { |
| 63 | + it('should call stop_transfer RPC', async () => { |
| 64 | + await stopTransfer('transfer-uuid-456'); |
| 65 | + |
| 66 | + const call = lastFetchCall(); |
| 67 | + expect(call.body.method).toBe('stop_transfer'); |
| 68 | + expect(call.body.params).toEqual({transfer_id: 'transfer-uuid-456'}); |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + describe('resumeTransfer', () => { |
| 73 | + it('should call resume_transfer RPC', async () => { |
| 74 | + await resumeTransfer('transfer-uuid-789', {token: 'new-token'}); |
| 75 | + |
| 76 | + const call = lastFetchCall(); |
| 77 | + expect(call.body.method).toBe('resume_transfer'); |
| 78 | + expect(call.body.params).toEqual({ |
| 79 | + transfer_id: 'transfer-uuid-789', |
| 80 | + transfer_spec: {token: 'new-token'}, |
| 81 | + }); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + describe('showSelectFileDialog', () => { |
| 86 | + it('should call show_file_dialog RPC', async () => { |
| 87 | + await showSelectFileDialog({multiple: true}); |
| 88 | + |
| 89 | + const call = lastFetchCall(); |
| 90 | + expect(call.body.method).toBe('show_file_dialog'); |
| 91 | + expect(call.body.params).toEqual({ |
| 92 | + options: {multiple: true}, |
| 93 | + app_id: APP_ID, |
| 94 | + }); |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + describe('showSelectFolderDialog', () => { |
| 99 | + it('should call show_folder_dialog RPC', async () => { |
| 100 | + await showSelectFolderDialog({multiple: false}); |
| 101 | + |
| 102 | + const call = lastFetchCall(); |
| 103 | + expect(call.body.method).toBe('show_folder_dialog'); |
| 104 | + expect(call.body.params).toEqual({ |
| 105 | + options: {multiple: false}, |
| 106 | + app_id: APP_ID, |
| 107 | + }); |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + describe('showPreferences', () => { |
| 112 | + it('should call open_preferences RPC', async () => { |
| 113 | + await showPreferences(); |
| 114 | + |
| 115 | + const call = lastFetchCall(); |
| 116 | + expect(call.body.method).toBe('open_preferences'); |
| 117 | + }); |
| 118 | + }); |
| 119 | + |
| 120 | + describe('getAllTransfers', () => { |
| 121 | + it('should call get_all_transfers RPC', async () => { |
| 122 | + await getAllTransfers(); |
| 123 | + |
| 124 | + const call = lastFetchCall(); |
| 125 | + expect(call.body.method).toBe('get_all_transfers'); |
| 126 | + expect(call.body.params).toEqual({app_id: APP_ID}); |
| 127 | + }); |
| 128 | + }); |
| 129 | + |
| 130 | + describe('getTransfer', () => { |
| 131 | + it('should call get_transfer RPC', async () => { |
| 132 | + await getTransfer('transfer-uuid-abc'); |
| 133 | + |
| 134 | + const call = lastFetchCall(); |
| 135 | + expect(call.body.method).toBe('get_transfer'); |
| 136 | + expect(call.body.params).toEqual({transfer_id: 'transfer-uuid-abc'}); |
| 137 | + }); |
| 138 | + }); |
| 139 | + |
| 140 | + describe('showDirectory', () => { |
| 141 | + it('should call show_directory RPC', async () => { |
| 142 | + await showDirectory('transfer-uuid-def'); |
| 143 | + |
| 144 | + const call = lastFetchCall(); |
| 145 | + expect(call.body.method).toBe('show_directory'); |
| 146 | + expect(call.body.params).toEqual({transfer_id: 'transfer-uuid-def'}); |
| 147 | + }); |
| 148 | + }); |
| 149 | + |
| 150 | + describe('modifyTransfer', () => { |
| 151 | + it('should call modify_transfer RPC', async () => { |
| 152 | + await modifyTransfer('transfer-uuid-ghi', {rate_policy: 'fair'}); |
| 153 | + |
| 154 | + const call = lastFetchCall(); |
| 155 | + expect(call.body.method).toBe('modify_transfer'); |
| 156 | + expect(call.body.params).toEqual({ |
| 157 | + transfer_id: 'transfer-uuid-ghi', |
| 158 | + transfer_spec: {rate_policy: 'fair'}, |
| 159 | + }); |
| 160 | + }); |
| 161 | + }); |
| 162 | + |
| 163 | + describe('readAsArrayBuffer', () => { |
| 164 | + it('should call read_as_array_buffer RPC', async () => { |
| 165 | + await readAsArrayBuffer('/path/to/image.png'); |
| 166 | + |
| 167 | + const call = lastFetchCall(); |
| 168 | + expect(call.body.method).toBe('read_as_array_buffer'); |
| 169 | + expect(call.body.params).toEqual({ |
| 170 | + request: {path: '/path/to/image.png'}, |
| 171 | + app_id: APP_ID, |
| 172 | + }); |
| 173 | + }); |
| 174 | + }); |
| 175 | + |
| 176 | + describe('readChunkAsArrayBuffer', () => { |
| 177 | + it('should call read_chunk_as_array_buffer RPC', async () => { |
| 178 | + await readChunkAsArrayBuffer('/path/to/large-file.bin', 1024, 4096); |
| 179 | + |
| 180 | + const call = lastFetchCall(); |
| 181 | + expect(call.body.method).toBe('read_chunk_as_array_buffer'); |
| 182 | + expect(call.body.params).toEqual({ |
| 183 | + request: {path: '/path/to/large-file.bin', offset: 1024, chunkSize: 4096}, |
| 184 | + app_id: APP_ID, |
| 185 | + }); |
| 186 | + }); |
| 187 | + }); |
| 188 | +}); |
0 commit comments