|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import { ProviderError } from './provider-interface'; |
| 3 | +import type { CloudFileMetadata, SyncProvider } from './provider-interface'; |
| 4 | + |
| 5 | +const getCache = vi.fn(); |
| 6 | + |
| 7 | +vi.mock('./cache-manager', () => ({ |
| 8 | + cacheManager: { getCache: (...args: unknown[]) => getCache(...args) } |
| 9 | +})); |
| 10 | + |
| 11 | +vi.mock('../snackbar', () => ({ showSnackbar: vi.fn() })); |
| 12 | + |
| 13 | +vi.mock('../progress-tracker', () => ({ |
| 14 | + progressTrackerStore: { |
| 15 | + addProcess: vi.fn(), |
| 16 | + updateProcess: vi.fn(), |
| 17 | + removeProcess: vi.fn() |
| 18 | + } |
| 19 | +})); |
| 20 | + |
| 21 | +vi.mock('$lib/settings', async () => { |
| 22 | + const { writable } = await import('svelte/store'); |
| 23 | + return { |
| 24 | + volumesWithTrash: writable({}), |
| 25 | + profiles: writable({}), |
| 26 | + profilesWithTrash: writable({}), |
| 27 | + migrateProfiles: vi.fn((p: unknown) => p), |
| 28 | + parseVolumesFromJson: vi.fn((json: string) => JSON.parse(json)) |
| 29 | + }; |
| 30 | +}); |
| 31 | + |
| 32 | +import { unifiedSyncService } from './unified-sync-service'; |
| 33 | + |
| 34 | +// downloadVolumeDataFile is private; these tests target it directly because it |
| 35 | +// owns the duplicate-merge behavior that broke MEGA sync (ghost duplicates). |
| 36 | +const svc = unifiedSyncService as any; |
| 37 | + |
| 38 | +function fileMeta(fileId: string): CloudFileMetadata { |
| 39 | + return { |
| 40 | + provider: 'mega', |
| 41 | + fileId, |
| 42 | + path: 'volume-data.json', |
| 43 | + modifiedTime: '2026-01-01T00:00:00Z' |
| 44 | + } as unknown as CloudFileMetadata; |
| 45 | +} |
| 46 | + |
| 47 | +const jsonBlob = (data: unknown) => ({ text: async () => JSON.stringify(data) }) as unknown as Blob; |
| 48 | + |
| 49 | +const notFound = () => new ProviderError('File not found: volume-data.json', 'mega', 'NOT_FOUND'); |
| 50 | + |
| 51 | +function makeProvider( |
| 52 | + download: (file: CloudFileMetadata) => Promise<Blob>, |
| 53 | + del: (file: CloudFileMetadata) => Promise<void> = async () => {} |
| 54 | +): SyncProvider { |
| 55 | + return { |
| 56 | + type: 'mega', |
| 57 | + downloadFile: vi.fn(download), |
| 58 | + deleteFile: vi.fn(del) |
| 59 | + } as unknown as SyncProvider; |
| 60 | +} |
| 61 | + |
| 62 | +function stubCache(files: CloudFileMetadata[]) { |
| 63 | + const cache = { |
| 64 | + getAll: vi.fn(() => files), |
| 65 | + get: vi.fn(() => null), |
| 66 | + fetch: vi.fn(async () => {}) |
| 67 | + }; |
| 68 | + getCache.mockReturnValue(cache); |
| 69 | + return cache; |
| 70 | +} |
| 71 | + |
| 72 | +beforeEach(() => { |
| 73 | + vi.clearAllMocks(); |
| 74 | +}); |
| 75 | + |
| 76 | +describe('downloadVolumeDataFile — duplicate handling with ghost copies', () => { |
| 77 | + const goodData = { 'vol-1': { lastProgressUpdate: '2026-01-02T00:00:00Z', progress: 5 } }; |
| 78 | + |
| 79 | + it('merges the readable copies and skips a ghost duplicate instead of discarding everything', async () => { |
| 80 | + const [good, ghost] = [fileMeta('good'), fileMeta('ghost')]; |
| 81 | + const cache = stubCache([good, ghost]); |
| 82 | + const provider = makeProvider(async (file) => { |
| 83 | + if (file.fileId === 'ghost') throw notFound(); |
| 84 | + return jsonBlob(goodData); |
| 85 | + }); |
| 86 | + |
| 87 | + const result = await svc.downloadVolumeDataFile(provider); |
| 88 | + |
| 89 | + expect(result).toEqual(goodData); |
| 90 | + expect(provider.deleteFile).toHaveBeenCalledTimes(1); |
| 91 | + expect(provider.deleteFile).toHaveBeenCalledWith(ghost); |
| 92 | + expect(cache.fetch).not.toHaveBeenCalled(); |
| 93 | + }); |
| 94 | + |
| 95 | + it('keeps the readable copy when the FIRST listed duplicate is the ghost', async () => { |
| 96 | + const [ghost, good] = [fileMeta('ghost'), fileMeta('good')]; |
| 97 | + stubCache([ghost, good]); |
| 98 | + const provider = makeProvider(async (file) => { |
| 99 | + if (file.fileId === 'ghost') throw notFound(); |
| 100 | + return jsonBlob(goodData); |
| 101 | + }); |
| 102 | + |
| 103 | + const result = await svc.downloadVolumeDataFile(provider); |
| 104 | + |
| 105 | + expect(result).toEqual(goodData); |
| 106 | + expect(provider.deleteFile).toHaveBeenCalledTimes(1); |
| 107 | + expect(provider.deleteFile).toHaveBeenCalledWith(ghost); |
| 108 | + }); |
| 109 | + |
| 110 | + it('tolerates NOT_FOUND from deleting a ghost duplicate (already converged)', async () => { |
| 111 | + const [good, ghost] = [fileMeta('good'), fileMeta('ghost')]; |
| 112 | + stubCache([good, ghost]); |
| 113 | + const provider = makeProvider( |
| 114 | + async (file) => { |
| 115 | + if (file.fileId === 'ghost') throw notFound(); |
| 116 | + return jsonBlob(goodData); |
| 117 | + }, |
| 118 | + async () => { |
| 119 | + throw notFound(); |
| 120 | + } |
| 121 | + ); |
| 122 | + |
| 123 | + await expect(svc.downloadVolumeDataFile(provider)).resolves.toEqual(goodData); |
| 124 | + }); |
| 125 | + |
| 126 | + it('returns null after one cache refresh when every copy is missing', async () => { |
| 127 | + const cache = stubCache([fileMeta('ghost-1'), fileMeta('ghost-2')]); |
| 128 | + const provider = makeProvider(async () => { |
| 129 | + throw notFound(); |
| 130 | + }); |
| 131 | + |
| 132 | + const result = await svc.downloadVolumeDataFile(provider); |
| 133 | + |
| 134 | + expect(result).toBeNull(); |
| 135 | + expect(cache.fetch).toHaveBeenCalledTimes(1); |
| 136 | + expect(provider.deleteFile).not.toHaveBeenCalled(); |
| 137 | + }); |
| 138 | + |
| 139 | + it('merges duplicates newest-lastProgressUpdate-wins and deletes the extra copy', async () => { |
| 140 | + const [first, second] = [fileMeta('first'), fileMeta('second')]; |
| 141 | + stubCache([first, second]); |
| 142 | + const newerData = { |
| 143 | + 'vol-1': { lastProgressUpdate: '2026-01-03T00:00:00Z', progress: 9 }, |
| 144 | + 'vol-2': { lastProgressUpdate: '2026-01-01T00:00:00Z', progress: 1 } |
| 145 | + }; |
| 146 | + const provider = makeProvider(async (file) => |
| 147 | + jsonBlob(file.fileId === 'first' ? goodData : newerData) |
| 148 | + ); |
| 149 | + |
| 150 | + const result = await svc.downloadVolumeDataFile(provider); |
| 151 | + |
| 152 | + expect(result['vol-1'].progress).toBe(9); |
| 153 | + expect(result['vol-2'].progress).toBe(1); |
| 154 | + expect(provider.deleteFile).toHaveBeenCalledTimes(1); |
| 155 | + expect(provider.deleteFile).toHaveBeenCalledWith(second); |
| 156 | + }); |
| 157 | + |
| 158 | + it('propagates transient download errors rather than treating them as missing data', async () => { |
| 159 | + stubCache([fileMeta('good'), fileMeta('flaky')]); |
| 160 | + const provider = makeProvider(async (file) => { |
| 161 | + if (file.fileId === 'flaky') throw new Error('network down'); |
| 162 | + return jsonBlob(goodData); |
| 163 | + }); |
| 164 | + |
| 165 | + await expect(svc.downloadVolumeDataFile(provider)).rejects.toThrow('network down'); |
| 166 | + expect(provider.deleteFile).not.toHaveBeenCalled(); |
| 167 | + }); |
| 168 | +}); |
0 commit comments