Skip to content

Commit 89ffa51

Browse files
committed
test: cover system and workspace stores
1 parent 25e73f3 commit 89ffa51

9 files changed

Lines changed: 820 additions & 19 deletions
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { createTestingPinia } from '@pinia/testing'
2+
import { setActivePinia } from 'pinia'
3+
import { beforeEach, describe, expect, it, vi } from 'vitest'
4+
5+
import { useElectronDownloadStore } from '@/stores/electronDownloadStore'
6+
7+
const electronAPI = vi.hoisted(() => vi.fn())
8+
9+
vi.mock('@/platform/distribution/types', () => ({ isDesktop: false }))
10+
vi.mock('@/utils/envUtil', () => ({ electronAPI }))
11+
12+
describe('electronDownloadStore outside desktop', () => {
13+
beforeEach(() => {
14+
setActivePinia(createTestingPinia({ stubActions: false }))
15+
electronAPI.mockClear()
16+
})
17+
18+
it('skips the Electron bridge when not running on desktop', async () => {
19+
const store = useElectronDownloadStore()
20+
21+
await store.initialize()
22+
23+
expect(electronAPI).not.toHaveBeenCalled()
24+
expect(store.downloads).toEqual([])
25+
})
26+
})
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
})

src/stores/systemStatsStore.test.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type { SystemStats } from '@/schemas/apiSchema'
66
import { api } from '@/scripts/api'
77
import { useSystemStatsStore } from '@/stores/systemStatsStore'
88

9-
const mockData = vi.hoisted(() => ({ isDesktop: false }))
9+
const mockData = vi.hoisted(() => ({ isCloud: false, isDesktop: false }))
1010

1111
// Mock the API
1212
vi.mock('@/scripts/api', () => ({
@@ -19,7 +19,9 @@ vi.mock('@/platform/distribution/types', () => ({
1919
get isDesktop() {
2020
return mockData.isDesktop
2121
},
22-
isCloud: false
22+
get isCloud() {
23+
return mockData.isCloud
24+
}
2325
}))
2426

2527
describe('useSystemStatsStore', () => {
@@ -138,6 +140,7 @@ describe('useSystemStatsStore', () => {
138140
describe('getFormFactor', () => {
139141
beforeEach(() => {
140142
// Reset systemStats for each test
143+
mockData.isCloud = false
141144
store.systemStats = null
142145
})
143146

@@ -162,6 +165,12 @@ describe('useSystemStatsStore', () => {
162165
expect(store.getFormFactor()).toBe('other')
163166
})
164167

168+
it('should return "cloud" in cloud mode', () => {
169+
mockData.isCloud = true
170+
171+
expect(store.getFormFactor()).toBe('cloud')
172+
})
173+
165174
describe('desktop environment', () => {
166175
beforeEach(() => {
167176
mockData.isDesktop = true

src/stores/templateRankingStore.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ describe('templateRankingStore', () => {
9090
})
9191

9292
describe('computePopularScore', () => {
93+
it('normalizes usage against itself before a largest score is loaded', () => {
94+
const store = useTemplateRankingStore()
95+
96+
expect(store.computePopularScore('2024-01-01', 10)).toBeGreaterThan(0.8)
97+
})
98+
9399
it('does not use searchRank', () => {
94100
const store = useTemplateRankingStore()
95101
store.largestUsageScore = 100
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { createTestingPinia } from '@pinia/testing'
2+
import { setActivePinia } from 'pinia'
3+
import { beforeEach, describe, expect, it } from 'vitest'
4+
5+
import { useExtensionStore } from '@/stores/extensionStore'
6+
import { useTopbarBadgeStore } from '@/stores/topbarBadgeStore'
7+
8+
describe('topbarBadgeStore', () => {
9+
beforeEach(() => {
10+
setActivePinia(createTestingPinia({ stubActions: false }))
11+
})
12+
13+
it('collects topbar badges from registered extensions', () => {
14+
const extensionStore = useExtensionStore()
15+
extensionStore.registerExtension({
16+
name: 'badges',
17+
topbarBadges: [{ text: 'Beta', label: 'BETA' }]
18+
})
19+
extensionStore.registerExtension({ name: 'plain' })
20+
21+
const store = useTopbarBadgeStore()
22+
23+
expect(store.badges).toEqual([{ text: 'Beta', label: 'BETA' }])
24+
})
25+
})

0 commit comments

Comments
 (0)