|
| 1 | +import * as commonSdk from '@powersync/common'; |
| 2 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 3 | +import { AbstractAttachmentQueue } from '../../src/AbstractAttachmentQueue'; |
| 4 | +import { AttachmentRecord, AttachmentState } from '../../src/Schema'; |
| 5 | +import { AbstractPowerSyncDatabase } from '@powersync/common'; |
| 6 | +import { StorageAdapter } from '../../src/StorageAdapter'; |
| 7 | + |
| 8 | +const record = { |
| 9 | + id: 'test-1', |
| 10 | + filename: 'test.jpg', |
| 11 | + state: AttachmentState.QUEUED_DOWNLOAD |
| 12 | + } |
| 13 | + |
| 14 | +const mockPowerSync = { |
| 15 | + currentStatus: { status: 'initial' }, |
| 16 | + registerListener: vi.fn(() => {}), |
| 17 | + resolveTables: vi.fn(() => ['table1', 'table2']), |
| 18 | + onChangeWithCallback: vi.fn(), |
| 19 | + getAll: vi.fn(() => Promise.resolve([{id: 'test-1'}, {id: 'test-2'}])), |
| 20 | + execute: vi.fn(() => Promise.resolve()), |
| 21 | + getOptional: vi.fn((_query, params) => Promise.resolve(record)), |
| 22 | + watch: vi.fn((query, params, callbacks) => { |
| 23 | + callbacks?.onResult?.({ rows: { _array: [{id: 'test-1'}, {id: 'test-2'}] } }); |
| 24 | + }), |
| 25 | + writeTransaction: vi.fn(async (callback) => { |
| 26 | + await callback({ |
| 27 | + execute: vi.fn(() => Promise.resolve()) |
| 28 | + }); |
| 29 | + }) |
| 30 | +}; |
| 31 | + |
| 32 | +const mockStorage: StorageAdapter = { |
| 33 | + downloadFile: vi.fn(), |
| 34 | + uploadFile: vi.fn(), |
| 35 | + deleteFile: vi.fn(), |
| 36 | + writeFile: vi.fn(), |
| 37 | + readFile: vi.fn(), |
| 38 | + fileExists: vi.fn(), |
| 39 | + makeDir: vi.fn(), |
| 40 | + copyFile: vi.fn(), |
| 41 | + getUserStorageDirectory: vi.fn() |
| 42 | +}; |
| 43 | + |
| 44 | +class TestAttachmentQueue extends AbstractAttachmentQueue { |
| 45 | + onAttachmentIdsChange(onUpdate: (ids: string[]) => void): void { |
| 46 | + throw new Error('Method not implemented.'); |
| 47 | + } |
| 48 | + newAttachmentRecord(record?: Partial<AttachmentRecord>): Promise<AttachmentRecord> { |
| 49 | + throw new Error('Method not implemented.'); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +describe('attachments', () => { |
| 54 | + beforeEach(() => { |
| 55 | + vi.clearAllMocks(); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should not download attachments when downloadRecord is called with downloadAttachments false', async () => { |
| 59 | + const queue = new TestAttachmentQueue({ |
| 60 | + powersync: mockPowerSync as any, |
| 61 | + storage: mockStorage, |
| 62 | + downloadAttachments: false |
| 63 | + }); |
| 64 | + |
| 65 | + await queue.downloadRecord(record); |
| 66 | + |
| 67 | + expect(mockStorage.downloadFile).not.toHaveBeenCalled(); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should download attachments when downloadRecord is called with downloadAttachments true', async () => { |
| 71 | + const queue = new TestAttachmentQueue({ |
| 72 | + powersync: mockPowerSync as any, |
| 73 | + storage: mockStorage, |
| 74 | + downloadAttachments: true |
| 75 | + }); |
| 76 | + |
| 77 | + await queue.downloadRecord(record); |
| 78 | + |
| 79 | + expect(mockStorage.downloadFile).toHaveBeenCalled(); |
| 80 | + }); |
| 81 | + |
| 82 | + // Testing the inverse of this test, i.e. when downloadAttachments is false, is not required as you can't wait for something that does not happen |
| 83 | + it('should not download attachments with watchDownloads is called with downloadAttachments false', async () => { |
| 84 | + const queue = new TestAttachmentQueue({ |
| 85 | + powersync: mockPowerSync as any, |
| 86 | + storage: mockStorage, |
| 87 | + downloadAttachments: true |
| 88 | + }); |
| 89 | + |
| 90 | + queue.watchDownloads(); |
| 91 | + await vi.waitFor(() => { |
| 92 | + expect(mockStorage.downloadFile).toBeCalledTimes(2); |
| 93 | + }); |
| 94 | + }); |
| 95 | +}); |
0 commit comments