|
| 1 | +import { DownloadStatus } from '@comfyorg/comfyui-electron-types' |
| 2 | +import { createTestingPinia } from '@pinia/testing' |
| 3 | +import { setActivePinia } from 'pinia' |
| 4 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | + |
| 6 | +import { useElectronDownloadStore } from '@/stores/electronDownloadStore' |
| 7 | + |
| 8 | +const downloadManagerMock = vi.hoisted(() => ({ |
| 9 | + cancelDownload: vi.fn(), |
| 10 | + getAllDownloads: vi.fn(), |
| 11 | + onDownloadProgress: vi.fn(), |
| 12 | + pauseDownload: vi.fn(), |
| 13 | + resumeDownload: vi.fn(), |
| 14 | + startDownload: vi.fn() |
| 15 | +})) |
| 16 | + |
| 17 | +vi.mock('@/platform/distribution/types', () => ({ |
| 18 | + isDesktop: true |
| 19 | +})) |
| 20 | + |
| 21 | +vi.mock('@/utils/envUtil', () => ({ |
| 22 | + electronAPI: () => ({ |
| 23 | + DownloadManager: downloadManagerMock |
| 24 | + }) |
| 25 | +})) |
| 26 | + |
| 27 | +describe('electronDownloadStore', () => { |
| 28 | + beforeEach(() => { |
| 29 | + setActivePinia(createTestingPinia({ stubActions: false })) |
| 30 | + Object.values(downloadManagerMock).forEach((mock) => mock.mockReset()) |
| 31 | + downloadManagerMock.getAllDownloads.mockResolvedValue([ |
| 32 | + { |
| 33 | + filename: 'done.bin', |
| 34 | + status: DownloadStatus.COMPLETED, |
| 35 | + url: 'https://example.com/done.bin' |
| 36 | + } |
| 37 | + ]) |
| 38 | + }) |
| 39 | + |
| 40 | + it('loads existing downloads and applies progress updates by URL', async () => { |
| 41 | + let progressCallback: |
| 42 | + | Parameters<typeof downloadManagerMock.onDownloadProgress>[0] |
| 43 | + | undefined |
| 44 | + downloadManagerMock.onDownloadProgress.mockImplementation((callback) => { |
| 45 | + progressCallback = callback |
| 46 | + }) |
| 47 | + const store = useElectronDownloadStore() |
| 48 | + |
| 49 | + await store.initialize() |
| 50 | + progressCallback?.({ |
| 51 | + filename: 'model.bin', |
| 52 | + progress: 25, |
| 53 | + savePath: '/tmp/model.bin', |
| 54 | + status: DownloadStatus.IN_PROGRESS, |
| 55 | + url: 'https://example.com/model.bin' |
| 56 | + }) |
| 57 | + progressCallback?.({ |
| 58 | + filename: 'model.bin', |
| 59 | + progress: 50, |
| 60 | + savePath: '/tmp/model.bin', |
| 61 | + status: DownloadStatus.IN_PROGRESS, |
| 62 | + url: 'https://example.com/model.bin' |
| 63 | + }) |
| 64 | + |
| 65 | + expect(store.findByUrl('https://example.com/done.bin')?.status).toBe( |
| 66 | + DownloadStatus.COMPLETED |
| 67 | + ) |
| 68 | + expect(store.findByUrl('https://example.com/model.bin')).toMatchObject({ |
| 69 | + filename: 'model.bin', |
| 70 | + progress: 50, |
| 71 | + status: DownloadStatus.IN_PROGRESS |
| 72 | + }) |
| 73 | + expect(store.inProgressDownloads).toHaveLength(1) |
| 74 | + }) |
| 75 | + |
| 76 | + it('delegates download controls to the Electron bridge', async () => { |
| 77 | + const store = useElectronDownloadStore() |
| 78 | + |
| 79 | + await store.start({ |
| 80 | + filename: 'model.bin', |
| 81 | + savePath: '/tmp/model.bin', |
| 82 | + url: 'https://example.com/model.bin' |
| 83 | + }) |
| 84 | + await store.pause('https://example.com/model.bin') |
| 85 | + await store.resume('https://example.com/model.bin') |
| 86 | + await store.cancel('https://example.com/model.bin') |
| 87 | + |
| 88 | + expect(downloadManagerMock.startDownload).toHaveBeenCalledWith( |
| 89 | + 'https://example.com/model.bin', |
| 90 | + '/tmp/model.bin', |
| 91 | + 'model.bin' |
| 92 | + ) |
| 93 | + expect(downloadManagerMock.pauseDownload).toHaveBeenCalledWith( |
| 94 | + 'https://example.com/model.bin' |
| 95 | + ) |
| 96 | + expect(downloadManagerMock.resumeDownload).toHaveBeenCalledWith( |
| 97 | + 'https://example.com/model.bin' |
| 98 | + ) |
| 99 | + expect(downloadManagerMock.cancelDownload).toHaveBeenCalledWith( |
| 100 | + 'https://example.com/model.bin' |
| 101 | + ) |
| 102 | + }) |
| 103 | +}) |
0 commit comments