test: simplify test file mocking patterns (#8320)

Simplifies test mocking patterns across multiple test files.

- Removes redundant `vi.hoisted()` calls
- Cleans up mock implementations
- Removes unused imports and variables

┆Issue is synchronized with this [Notion
page](https://www.notion.so/PR-8320-test-simplify-test-file-mocking-patterns-2f46d73d36508150981bd8ecb99a6a11)
by [Unito](https://www.unito.io)

---------

Co-authored-by: Amp <amp@ampcode.com>
Co-authored-by: GitHub Action <action@github.com>
This commit is contained in:
Alexander Brown
2026-01-28 12:17:16 -08:00
committed by GitHub
parent 3e2352423b
commit e44b411ff6
24 changed files with 627 additions and 511 deletions

View File

@@ -4,6 +4,8 @@ import { beforeEach, describe, expect, it, vi } from 'vitest'
import type { LGraphNode } from '@/lib/litegraph/src/litegraph'
import type { AssetItem } from '@/platform/assets/schemas/assetSchema'
import { useMediaAssetActions } from './useMediaAssetActions'
// Use vi.hoisted to create a mutable reference for isCloud
const mockIsCloud = vi.hoisted(() => ({ value: false }))
@@ -126,7 +128,6 @@ describe('useMediaAssetActions', () => {
})
it('should use asset.name as filename', async () => {
const { useMediaAssetActions } = await import('./useMediaAssetActions')
const actions = useMediaAssetActions()
const asset = createMockAsset({
@@ -146,7 +147,6 @@ describe('useMediaAssetActions', () => {
})
it('should use asset_hash as filename when available', async () => {
const { useMediaAssetActions } = await import('./useMediaAssetActions')
const actions = useMediaAssetActions()
const asset = createMockAsset({
@@ -160,7 +160,6 @@ describe('useMediaAssetActions', () => {
})
it('should fall back to asset.name when asset_hash is not available', async () => {
const { useMediaAssetActions } = await import('./useMediaAssetActions')
const actions = useMediaAssetActions()
const asset = createMockAsset({
@@ -174,7 +173,6 @@ describe('useMediaAssetActions', () => {
})
it('should fall back to asset.name when asset_hash is null', async () => {
const { useMediaAssetActions } = await import('./useMediaAssetActions')
const actions = useMediaAssetActions()
const asset = createMockAsset({
@@ -196,7 +194,6 @@ describe('useMediaAssetActions', () => {
})
it('should use asset_hash for each asset', async () => {
const { useMediaAssetActions } = await import('./useMediaAssetActions')
const actions = useMediaAssetActions()
const assets = [

View File

@@ -1,27 +1,27 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { useFeatureUsageTracker } from './useFeatureUsageTracker'
const STORAGE_KEY = 'Comfy.FeatureUsage'
describe('useFeatureUsageTracker', () => {
beforeEach(() => {
localStorage.clear()
vi.resetModules()
vi.clearAllMocks()
})
afterEach(() => {
localStorage.clear()
})
it('initializes with zero count for new feature', async () => {
const { useFeatureUsageTracker } = await import('./useFeatureUsageTracker')
const { useCount } = useFeatureUsageTracker('test-feature')
it('initializes with zero count for new feature', () => {
const { useCount } = useFeatureUsageTracker('test-feature-1')
expect(useCount.value).toBe(0)
})
it('increments count on trackUsage', async () => {
const { useFeatureUsageTracker } = await import('./useFeatureUsageTracker')
const { useCount, trackUsage } = useFeatureUsageTracker('test-feature')
it('increments count on trackUsage', () => {
const { useCount, trackUsage } = useFeatureUsageTracker('test-feature-2')
expect(useCount.value).toBe(0)
@@ -32,14 +32,12 @@ describe('useFeatureUsageTracker', () => {
expect(useCount.value).toBe(2)
})
it('sets firstUsed only on first use', async () => {
it('sets firstUsed only on first use', () => {
vi.useFakeTimers()
const firstTs = 1000000
vi.setSystemTime(firstTs)
try {
const { useFeatureUsageTracker } =
await import('./useFeatureUsageTracker')
const { usage, trackUsage } = useFeatureUsageTracker('test-feature')
const { usage, trackUsage } = useFeatureUsageTracker('test-feature-3')
trackUsage()
expect(usage.value?.firstUsed).toBe(firstTs)
@@ -52,12 +50,10 @@ describe('useFeatureUsageTracker', () => {
}
})
it('updates lastUsed on each use', async () => {
it('updates lastUsed on each use', () => {
vi.useFakeTimers()
try {
const { useFeatureUsageTracker } =
await import('./useFeatureUsageTracker')
const { usage, trackUsage } = useFeatureUsageTracker('test-feature')
const { usage, trackUsage } = useFeatureUsageTracker('test-feature-4')
trackUsage()
const firstLastUsed = usage.value?.lastUsed ?? 0
@@ -71,10 +67,9 @@ describe('useFeatureUsageTracker', () => {
}
})
it('reset clears feature data', async () => {
const { useFeatureUsageTracker } = await import('./useFeatureUsageTracker')
it('reset clears feature data', () => {
const { useCount, trackUsage, reset } =
useFeatureUsageTracker('test-feature')
useFeatureUsageTracker('test-feature-5')
trackUsage()
trackUsage()
@@ -84,8 +79,7 @@ describe('useFeatureUsageTracker', () => {
expect(useCount.value).toBe(0)
})
it('tracks multiple features independently', async () => {
const { useFeatureUsageTracker } = await import('./useFeatureUsageTracker')
it('tracks multiple features independently', () => {
const featureA = useFeatureUsageTracker('feature-a')
const featureB = useFeatureUsageTracker('feature-b')
@@ -100,8 +94,6 @@ describe('useFeatureUsageTracker', () => {
it('persists to localStorage', async () => {
vi.useFakeTimers()
try {
const { useFeatureUsageTracker } =
await import('./useFeatureUsageTracker')
const { trackUsage } = useFeatureUsageTracker('persisted-feature')
trackUsage()
@@ -114,7 +106,7 @@ describe('useFeatureUsageTracker', () => {
}
})
it('loads existing data from localStorage', async () => {
it('loads existing data from localStorage', () => {
localStorage.setItem(
STORAGE_KEY,
JSON.stringify({
@@ -122,8 +114,6 @@ describe('useFeatureUsageTracker', () => {
})
)
vi.resetModules()
const { useFeatureUsageTracker } = await import('./useFeatureUsageTracker')
const { useCount } = useFeatureUsageTracker('existing-feature')
expect(useCount.value).toBe(5)

View File

@@ -1,6 +1,10 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'
import type * as TopupTrackerModule from '@/platform/telemetry/topupTracker'
import {
startTopupTracking,
checkForCompletedTopup,
clearTopupTracking
} from '@/platform/telemetry/topupTracker'
import type { AuditLog } from '@/services/customerEventsService'
// Mock localStorage
@@ -25,19 +29,15 @@ vi.mock('@/platform/telemetry', () => ({
}))
describe('topupTracker', () => {
let topupTracker: typeof TopupTrackerModule
beforeEach(async () => {
beforeEach(() => {
vi.clearAllMocks()
// Dynamically import to ensure fresh module state
topupTracker = await import('@/platform/telemetry/topupTracker')
})
describe('startTopupTracking', () => {
it('should save current timestamp to localStorage', () => {
const beforeTimestamp = Date.now()
topupTracker.startTopupTracking()
startTopupTracking()
expect(mockLocalStorage.setItem).toHaveBeenCalledWith(
'pending_topup_timestamp',
@@ -57,7 +57,7 @@ describe('topupTracker', () => {
it('should return false if no pending topup exists', () => {
mockLocalStorage.getItem.mockReturnValue(null)
const result = topupTracker.checkForCompletedTopup([])
const result = checkForCompletedTopup([])
expect(result).toBe(false)
expect(mockTelemetry.trackApiCreditTopupSucceeded).not.toHaveBeenCalled()
@@ -66,7 +66,7 @@ describe('topupTracker', () => {
it('should return false if events array is empty', () => {
mockLocalStorage.getItem.mockReturnValue(Date.now().toString())
const result = topupTracker.checkForCompletedTopup([])
const result = checkForCompletedTopup([])
expect(result).toBe(false)
expect(mockTelemetry.trackApiCreditTopupSucceeded).not.toHaveBeenCalled()
@@ -75,7 +75,7 @@ describe('topupTracker', () => {
it('should return false if events array is null', () => {
mockLocalStorage.getItem.mockReturnValue(Date.now().toString())
const result = topupTracker.checkForCompletedTopup(null)
const result = checkForCompletedTopup(null)
expect(result).toBe(false)
expect(mockTelemetry.trackApiCreditTopupSucceeded).not.toHaveBeenCalled()
@@ -94,7 +94,7 @@ describe('topupTracker', () => {
}
]
const result = topupTracker.checkForCompletedTopup(events)
const result = checkForCompletedTopup(events)
expect(result).toBe(false)
expect(mockLocalStorage.removeItem).toHaveBeenCalledWith(
@@ -122,7 +122,7 @@ describe('topupTracker', () => {
}
]
const result = topupTracker.checkForCompletedTopup(events)
const result = checkForCompletedTopup(events)
expect(result).toBe(true)
expect(mockTelemetry.trackApiCreditTopupSucceeded).toHaveBeenCalledOnce()
@@ -144,7 +144,7 @@ describe('topupTracker', () => {
}
]
const result = topupTracker.checkForCompletedTopup(events)
const result = checkForCompletedTopup(events)
expect(result).toBe(false)
expect(mockTelemetry.trackApiCreditTopupSucceeded).not.toHaveBeenCalled()
@@ -164,7 +164,7 @@ describe('topupTracker', () => {
}
]
const result = topupTracker.checkForCompletedTopup(events)
const result = checkForCompletedTopup(events)
expect(result).toBe(false)
expect(mockTelemetry.trackApiCreditTopupSucceeded).not.toHaveBeenCalled()
@@ -189,7 +189,7 @@ describe('topupTracker', () => {
}
]
const result = topupTracker.checkForCompletedTopup(events)
const result = checkForCompletedTopup(events)
expect(result).toBe(false)
expect(mockTelemetry.trackApiCreditTopupSucceeded).not.toHaveBeenCalled()
@@ -198,7 +198,7 @@ describe('topupTracker', () => {
describe('clearTopupTracking', () => {
it('should remove pending topup from localStorage', () => {
topupTracker.clearTopupTracking()
clearTopupTracking()
expect(mockLocalStorage.removeItem).toHaveBeenCalledWith(
'pending_topup_timestamp'

View File

@@ -1,5 +1,7 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { useTelemetry } from '@/platform/telemetry'
vi.mock('@/platform/distribution/types', () => ({
isCloud: false
}))
@@ -9,17 +11,14 @@ describe('useTelemetry', () => {
vi.clearAllMocks()
})
it('should return null when not in cloud distribution', async () => {
const { useTelemetry } = await import('@/platform/telemetry')
it('should return null when not in cloud distribution', () => {
const provider = useTelemetry()
// Should return null for OSS builds
expect(provider).toBeNull()
}, 10000)
it('should return null consistently for OSS builds', async () => {
const { useTelemetry } = await import('@/platform/telemetry')
})
it('should return null consistently for OSS builds', () => {
const provider1 = useTelemetry()
const provider2 = useTelemetry()

View File

@@ -1,19 +1,96 @@
import { createPinia, setActivePinia } from 'pinia'
import { compare, valid } from 'semver'
import type { Mock } from 'vitest'
import { until } from '@vueuse/core'
import { setActivePinia } from 'pinia'
import { compare } from 'semver'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { ref } from 'vue'
import type { ReleaseNote } from '@/platform/updates/common/releaseService'
import { useSettingStore } from '@/platform/settings/settingStore'
import { useReleaseStore } from '@/platform/updates/common/releaseStore'
import { useReleaseService } from '@/platform/updates/common/releaseService'
import { useSystemStatsStore } from '@/stores/systemStatsStore'
import { isElectron } from '@/utils/envUtil'
import { createTestingPinia } from '@pinia/testing'
import type { SystemStats } from '@/types'
// Mock the dependencies
vi.mock('semver')
vi.mock('@/utils/envUtil')
vi.mock('semver', () => ({
compare: vi.fn(),
valid: vi.fn(() => '1.0.0')
}))
vi.mock('@/utils/envUtil', () => ({
isElectron: vi.fn(() => true)
}))
vi.mock('@/platform/distribution/types', () => ({ isCloud: false }))
vi.mock('@/platform/updates/common/releaseService')
vi.mock('@/platform/settings/settingStore')
vi.mock('@/stores/systemStatsStore')
vi.mock('@/platform/updates/common/releaseService', () => {
const getReleases = vi.fn()
const isLoading = ref(false)
const error = ref<string | null>(null)
return {
useReleaseService: () => ({
getReleases,
isLoading,
error
})
}
})
vi.mock('@/platform/settings/settingStore', () => {
const get = vi.fn((key: string) => {
if (key === 'Comfy.Notification.ShowVersionUpdates') return true
return null
})
const set = vi.fn()
return {
useSettingStore: () => ({ get, set })
}
})
const mockSystemStatsState = vi.hoisted(() => ({
systemStats: {
system: {
comfyui_version: '1.0.0',
argv: []
}
} satisfies {
system: Partial<SystemStats['system']>
},
isInitialized: true,
reset() {
this.systemStats = {
system: {
comfyui_version: '1.0.0',
argv: []
} satisfies Partial<SystemStats['system']>
}
this.isInitialized = true
}
}))
vi.mock('@/stores/systemStatsStore', () => {
const refetchSystemStats = vi.fn()
const getFormFactor = vi.fn(() => 'git-windows')
return {
useSystemStatsStore: () => ({
get systemStats() {
return mockSystemStatsState.systemStats
},
set systemStats(val) {
mockSystemStatsState.systemStats = val
},
get isInitialized() {
return mockSystemStatsState.isInitialized
},
set isInitialized(val) {
mockSystemStatsState.isInitialized = val
},
refetchSystemStats,
getFormFactor
})
}
})
vi.mock('@vueuse/core', () => ({
until: vi.fn(() => Promise.resolve()),
useStorage: vi.fn(() => ({ value: {} })),
@@ -21,27 +98,6 @@ vi.mock('@vueuse/core', () => ({
}))
describe('useReleaseStore', () => {
let store: ReturnType<typeof useReleaseStore>
let mockReleaseService: {
getReleases: Mock
isLoading: ReturnType<typeof ref<boolean>>
error: ReturnType<typeof ref<string | null>>
}
let mockSettingStore: { get: Mock; set: Mock }
let mockSystemStatsStore: {
systemStats: {
system: {
comfyui_version: string
argv?: string[]
[key: string]: unknown
}
devices?: unknown[]
} | null
isInitialized: boolean
refetchSystemStats: Mock
getFormFactor: Mock
}
const mockRelease = {
id: 1,
project: 'comfyui' as const,
@@ -51,71 +107,16 @@ describe('useReleaseStore', () => {
attention: 'high' as const
}
beforeEach(async () => {
setActivePinia(createPinia())
beforeEach(() => {
setActivePinia(createTestingPinia({ stubActions: false }))
// Reset all mocks
vi.clearAllMocks()
// Setup mock services with proper refs
mockReleaseService = {
getReleases: vi.fn(),
isLoading: ref(false),
error: ref(null)
}
mockSettingStore = {
get: vi.fn(),
set: vi.fn()
}
mockSystemStatsStore = {
systemStats: {
system: {
comfyui_version: '1.0.0'
}
},
isInitialized: true,
refetchSystemStats: vi.fn(),
getFormFactor: vi.fn(() => 'git-windows')
}
// Setup mock implementations
const { useReleaseService } =
await import('@/platform/updates/common/releaseService')
const { useSettingStore } = await import('@/platform/settings/settingStore')
const { useSystemStatsStore } = await import('@/stores/systemStatsStore')
const { isElectron } = await import('@/utils/envUtil')
vi.mocked(useReleaseService).mockReturnValue(
mockReleaseService as Partial<
ReturnType<typeof useReleaseService>
> as ReturnType<typeof useReleaseService>
)
vi.mocked(useSettingStore).mockReturnValue(
mockSettingStore as Partial<
ReturnType<typeof useSettingStore>
> as ReturnType<typeof useSettingStore>
)
vi.mocked(useSystemStatsStore).mockReturnValue(
mockSystemStatsStore as Partial<
ReturnType<typeof useSystemStatsStore>
> as ReturnType<typeof useSystemStatsStore>
)
vi.mocked(isElectron).mockReturnValue(true)
vi.mocked(valid).mockReturnValue('1.0.0')
// Default showVersionUpdates to true
mockSettingStore.get.mockImplementation((key: string) => {
if (key === 'Comfy.Notification.ShowVersionUpdates') return true
return null
})
store = useReleaseStore()
vi.resetAllMocks()
mockSystemStatsState.reset()
})
describe('initial state', () => {
it('should initialize with default state', () => {
const store = useReleaseStore()
expect(store.releases).toEqual([])
expect(store.isLoading).toBe(false)
expect(store.error).toBeNull()
@@ -124,6 +125,7 @@ describe('useReleaseStore', () => {
describe('computed properties', () => {
it('should return most recent release', () => {
const store = useReleaseStore()
const olderRelease = {
...mockRelease,
id: 2,
@@ -136,6 +138,7 @@ describe('useReleaseStore', () => {
})
it('should return 3 most recent releases', () => {
const store = useReleaseStore()
const releases = [
mockRelease,
{ ...mockRelease, id: 2, version: '1.1.0' },
@@ -148,6 +151,7 @@ describe('useReleaseStore', () => {
})
it('should show update button (shouldShowUpdateButton)', () => {
const store = useReleaseStore()
vi.mocked(compare).mockReturnValue(1) // newer version available
store.releases = [mockRelease]
@@ -155,6 +159,7 @@ describe('useReleaseStore', () => {
})
it('should not show update button when no new version', () => {
const store = useReleaseStore()
vi.mocked(compare).mockReturnValue(-1) // current version is newer
store.releases = [mockRelease]
@@ -163,19 +168,17 @@ describe('useReleaseStore', () => {
})
describe('showVersionUpdates setting', () => {
beforeEach(async () => {
store.releases = [mockRelease]
})
describe('when notifications are enabled', () => {
beforeEach(async () => {
mockSettingStore.get.mockImplementation((key: string) => {
beforeEach(() => {
const settingStore = useSettingStore()
vi.mocked(settingStore.get).mockImplementation((key: string) => {
if (key === 'Comfy.Notification.ShowVersionUpdates') return true
return null
})
})
it('should show toast for medium/high attention releases', () => {
const store = useReleaseStore()
vi.mocked(compare).mockReturnValue(1)
store.releases = [mockRelease]
@@ -183,6 +186,7 @@ describe('useReleaseStore', () => {
})
it('should not show toast for low attention releases', () => {
const store = useReleaseStore()
vi.mocked(compare).mockReturnValue(1)
const lowAttentionRelease = {
@@ -196,13 +200,18 @@ describe('useReleaseStore', () => {
})
it('should show red dot for new versions', () => {
const store = useReleaseStore()
store.releases = [mockRelease]
vi.mocked(compare).mockReturnValue(1)
expect(store.shouldShowRedDot).toBe(true)
})
it('should show popup for latest version', () => {
mockSystemStatsStore.systemStats!.system.comfyui_version = '1.2.0'
const store = useReleaseStore()
store.releases = [mockRelease]
const systemStatsStore = useSystemStatsStore()
systemStatsStore.systemStats!.system.comfyui_version = '1.2.0'
vi.mocked(compare).mockReturnValue(0)
@@ -210,11 +219,13 @@ describe('useReleaseStore', () => {
})
it('should fetch releases during initialization', async () => {
mockReleaseService.getReleases.mockResolvedValue([mockRelease])
const store = useReleaseStore()
const releaseService = useReleaseService()
vi.mocked(releaseService.getReleases).mockResolvedValue([mockRelease])
await store.initialize()
expect(mockReleaseService.getReleases).toHaveBeenCalledWith({
expect(releaseService.getReleases).toHaveBeenCalledWith({
project: 'comfyui',
current_version: '1.0.0',
form_factor: 'git-windows',
@@ -224,27 +235,35 @@ describe('useReleaseStore', () => {
})
describe('when notifications are disabled', () => {
beforeEach(async () => {
mockSettingStore.get.mockImplementation((key: string) => {
beforeEach(() => {
const settingStore = useSettingStore()
vi.mocked(settingStore.get).mockImplementation((key: string) => {
if (key === 'Comfy.Notification.ShowVersionUpdates') return false
return null
})
})
it('should not show toast even with new version available', () => {
const store = useReleaseStore()
store.releases = [mockRelease]
vi.mocked(compare).mockReturnValue(1)
expect(store.shouldShowToast).toBe(false)
})
it('should not show red dot even with new version available', () => {
const store = useReleaseStore()
store.releases = [mockRelease]
vi.mocked(compare).mockReturnValue(1)
expect(store.shouldShowRedDot).toBe(false)
})
it('should not show popup even for latest version', () => {
mockSystemStatsStore.systemStats!.system.comfyui_version = '1.2.0'
const store = useReleaseStore()
store.releases = [mockRelease]
const systemStatsStore = useSystemStatsStore()
systemStatsStore.systemStats!.system.comfyui_version = '1.2.0'
vi.mocked(compare).mockReturnValue(0)
@@ -252,15 +271,19 @@ describe('useReleaseStore', () => {
})
it('should skip fetching releases during initialization', async () => {
const store = useReleaseStore()
const releaseService = useReleaseService()
await store.initialize()
expect(mockReleaseService.getReleases).not.toHaveBeenCalled()
expect(releaseService.getReleases).not.toHaveBeenCalled()
})
it('should not fetch releases when calling fetchReleases directly', async () => {
const store = useReleaseStore()
const releaseService = useReleaseService()
await store.fetchReleases()
expect(mockReleaseService.getReleases).not.toHaveBeenCalled()
expect(releaseService.getReleases).not.toHaveBeenCalled()
expect(store.isLoading).toBe(false)
})
})
@@ -268,11 +291,13 @@ describe('useReleaseStore', () => {
describe('release initialization', () => {
it('should fetch releases successfully', async () => {
mockReleaseService.getReleases.mockResolvedValue([mockRelease])
const store = useReleaseStore()
const releaseService = useReleaseService()
vi.mocked(releaseService.getReleases).mockResolvedValue([mockRelease])
await store.initialize()
expect(mockReleaseService.getReleases).toHaveBeenCalledWith({
expect(releaseService.getReleases).toHaveBeenCalledWith({
project: 'comfyui',
current_version: '1.0.0',
form_factor: 'git-windows',
@@ -282,12 +307,15 @@ describe('useReleaseStore', () => {
})
it('should include form_factor in API call', async () => {
mockSystemStatsStore.getFormFactor.mockReturnValue('desktop-mac')
mockReleaseService.getReleases.mockResolvedValue([mockRelease])
const store = useReleaseStore()
const releaseService = useReleaseService()
const systemStatsStore = useSystemStatsStore()
vi.mocked(systemStatsStore.getFormFactor).mockReturnValue('desktop-mac')
vi.mocked(releaseService.getReleases).mockResolvedValue([mockRelease])
await store.initialize()
expect(mockReleaseService.getReleases).toHaveBeenCalledWith({
expect(releaseService.getReleases).toHaveBeenCalledWith({
project: 'comfyui',
current_version: '1.0.0',
form_factor: 'desktop-mac',
@@ -296,16 +324,22 @@ describe('useReleaseStore', () => {
})
it('should skip fetching when --disable-api-nodes is present', async () => {
mockSystemStatsStore.systemStats!.system.argv = ['--disable-api-nodes']
const store = useReleaseStore()
const releaseService = useReleaseService()
const systemStatsStore = useSystemStatsStore()
systemStatsStore.systemStats!.system.argv = ['--disable-api-nodes']
await store.initialize()
expect(mockReleaseService.getReleases).not.toHaveBeenCalled()
expect(releaseService.getReleases).not.toHaveBeenCalled()
expect(store.isLoading).toBe(false)
})
it('should skip fetching when --disable-api-nodes is one of multiple args', async () => {
mockSystemStatsStore.systemStats!.system.argv = [
const store = useReleaseStore()
const releaseService = useReleaseService()
const systemStatsStore = useSystemStatsStore()
systemStatsStore.systemStats!.system.argv = [
'--port',
'8080',
'--disable-api-nodes',
@@ -314,37 +348,46 @@ describe('useReleaseStore', () => {
await store.initialize()
expect(mockReleaseService.getReleases).not.toHaveBeenCalled()
expect(releaseService.getReleases).not.toHaveBeenCalled()
expect(store.isLoading).toBe(false)
})
it('should fetch normally when --disable-api-nodes is not present', async () => {
mockSystemStatsStore.systemStats!.system.argv = [
const store = useReleaseStore()
const releaseService = useReleaseService()
const systemStatsStore = useSystemStatsStore()
systemStatsStore.systemStats!.system.argv = [
'--port',
'8080',
'--verbose'
]
mockReleaseService.getReleases.mockResolvedValue([mockRelease])
vi.mocked(releaseService.getReleases).mockResolvedValue([mockRelease])
await store.initialize()
expect(mockReleaseService.getReleases).toHaveBeenCalled()
expect(releaseService.getReleases).toHaveBeenCalled()
expect(store.releases).toEqual([mockRelease])
})
it('should fetch normally when argv is undefined', async () => {
mockSystemStatsStore.systemStats!.system.argv = undefined
mockReleaseService.getReleases.mockResolvedValue([mockRelease])
const store = useReleaseStore()
const releaseService = useReleaseService()
const systemStatsStore = useSystemStatsStore()
// TODO: Consider deleting this test since the types have to be violated for it to be relevant
delete (systemStatsStore.systemStats!.system as { argv?: string[] }).argv
vi.mocked(releaseService.getReleases).mockResolvedValue([mockRelease])
await store.initialize()
expect(mockReleaseService.getReleases).toHaveBeenCalled()
expect(releaseService.getReleases).toHaveBeenCalled()
expect(store.releases).toEqual([mockRelease])
})
it('should handle API errors gracefully', async () => {
mockReleaseService.getReleases.mockResolvedValue(null)
mockReleaseService.error.value = 'API Error'
const store = useReleaseStore()
const releaseService = useReleaseService()
vi.mocked(releaseService.getReleases).mockResolvedValue(null)
releaseService.error.value = 'API Error'
await store.initialize()
@@ -353,7 +396,9 @@ describe('useReleaseStore', () => {
})
it('should handle non-Error objects', async () => {
mockReleaseService.getReleases.mockRejectedValue('String error')
const store = useReleaseStore()
const releaseService = useReleaseService()
vi.mocked(releaseService.getReleases).mockRejectedValue('String error')
await store.initialize()
@@ -361,12 +406,14 @@ describe('useReleaseStore', () => {
})
it('should set loading state correctly', async () => {
const store = useReleaseStore()
const releaseService = useReleaseService()
let resolvePromise: (value: ReleaseNote[] | null) => void
const promise = new Promise<ReleaseNote[] | null>((resolve) => {
resolvePromise = resolve
})
mockReleaseService.getReleases.mockReturnValue(promise)
vi.mocked(releaseService.getReleases).mockReturnValue(promise)
const initPromise = store.initialize()
expect(store.isLoading).toBe(true)
@@ -378,19 +425,23 @@ describe('useReleaseStore', () => {
})
it('should fetch system stats if not available', async () => {
const { until } = await import('@vueuse/core')
mockSystemStatsStore.systemStats = null
mockSystemStatsStore.isInitialized = false
mockReleaseService.getReleases.mockResolvedValue([mockRelease])
const store = useReleaseStore()
const releaseService = useReleaseService()
const systemStatsStore = useSystemStatsStore()
systemStatsStore.systemStats = null
systemStatsStore.isInitialized = false
vi.mocked(releaseService.getReleases).mockResolvedValue([mockRelease])
await store.initialize()
expect(until).toHaveBeenCalled()
expect(mockReleaseService.getReleases).toHaveBeenCalled()
expect(vi.mocked(until)).toHaveBeenCalled()
expect(releaseService.getReleases).toHaveBeenCalled()
})
it('should not set loading state when notifications disabled', async () => {
mockSettingStore.get.mockImplementation((key: string) => {
const store = useReleaseStore()
const settingStore = useSettingStore()
vi.mocked(settingStore.get).mockImplementation((key: string) => {
if (key === 'Comfy.Notification.ShowVersionUpdates') return false
return null
})
@@ -403,16 +454,22 @@ describe('useReleaseStore', () => {
describe('--disable-api-nodes argument handling', () => {
it('should skip fetchReleases when --disable-api-nodes is present', async () => {
mockSystemStatsStore.systemStats!.system.argv = ['--disable-api-nodes']
const store = useReleaseStore()
const releaseService = useReleaseService()
const systemStatsStore = useSystemStatsStore()
systemStatsStore.systemStats!.system.argv = ['--disable-api-nodes']
await store.fetchReleases()
expect(mockReleaseService.getReleases).not.toHaveBeenCalled()
expect(releaseService.getReleases).not.toHaveBeenCalled()
expect(store.isLoading).toBe(false)
})
it('should skip fetchReleases when --disable-api-nodes is among other args', async () => {
mockSystemStatsStore.systemStats!.system.argv = [
const store = useReleaseStore()
const releaseService = useReleaseService()
const systemStatsStore = useSystemStatsStore()
systemStatsStore.systemStats!.system.argv = [
'--port',
'8080',
'--disable-api-nodes',
@@ -421,96 +478,109 @@ describe('useReleaseStore', () => {
await store.fetchReleases()
expect(mockReleaseService.getReleases).not.toHaveBeenCalled()
expect(releaseService.getReleases).not.toHaveBeenCalled()
expect(store.isLoading).toBe(false)
})
it('should proceed with fetchReleases when --disable-api-nodes is not present', async () => {
mockSystemStatsStore.systemStats!.system.argv = [
const store = useReleaseStore()
const releaseService = useReleaseService()
const systemStatsStore = useSystemStatsStore()
systemStatsStore.systemStats!.system.argv = [
'--port',
'8080',
'--verbose'
]
mockReleaseService.getReleases.mockResolvedValue([mockRelease])
vi.mocked(releaseService.getReleases).mockResolvedValue([mockRelease])
await store.fetchReleases()
expect(mockReleaseService.getReleases).toHaveBeenCalled()
expect(releaseService.getReleases).toHaveBeenCalled()
})
it('should proceed with fetchReleases when argv is undefined', async () => {
mockSystemStatsStore.systemStats!.system.argv = undefined
mockReleaseService.getReleases.mockResolvedValue([mockRelease])
const store = useReleaseStore()
const releaseService = useReleaseService()
const systemStatsStore = useSystemStatsStore()
delete (systemStatsStore.systemStats!.system as { argv?: string[] }).argv
vi.mocked(releaseService.getReleases).mockResolvedValue([mockRelease])
await store.fetchReleases()
expect(mockReleaseService.getReleases).toHaveBeenCalled()
expect(releaseService.getReleases).toHaveBeenCalled()
})
it('should proceed with fetchReleases when system stats are not available', async () => {
const { until } = await import('@vueuse/core')
mockSystemStatsStore.systemStats = null
mockSystemStatsStore.isInitialized = false
mockReleaseService.getReleases.mockResolvedValue([mockRelease])
const store = useReleaseStore()
const releaseService = useReleaseService()
const systemStatsStore = useSystemStatsStore()
systemStatsStore.systemStats = null
systemStatsStore.isInitialized = false
vi.mocked(releaseService.getReleases).mockResolvedValue([mockRelease])
await store.fetchReleases()
expect(until).toHaveBeenCalled()
expect(mockReleaseService.getReleases).toHaveBeenCalled()
expect(releaseService.getReleases).toHaveBeenCalled()
})
})
describe('action handlers', () => {
beforeEach(async () => {
store.releases = [mockRelease]
})
it('should handle skip release', async () => {
const store = useReleaseStore()
store.releases = [mockRelease]
const settingStore = useSettingStore()
await store.handleSkipRelease('1.2.0')
expect(mockSettingStore.set).toHaveBeenCalledWith(
expect(settingStore.set).toHaveBeenCalledWith(
'Comfy.Release.Version',
'1.2.0'
)
expect(mockSettingStore.set).toHaveBeenCalledWith(
expect(settingStore.set).toHaveBeenCalledWith(
'Comfy.Release.Status',
'skipped'
)
expect(mockSettingStore.set).toHaveBeenCalledWith(
expect(settingStore.set).toHaveBeenCalledWith(
'Comfy.Release.Timestamp',
expect.any(Number)
)
})
it('should handle show changelog', async () => {
const store = useReleaseStore()
store.releases = [mockRelease]
const settingStore = useSettingStore()
await store.handleShowChangelog('1.2.0')
expect(mockSettingStore.set).toHaveBeenCalledWith(
expect(settingStore.set).toHaveBeenCalledWith(
'Comfy.Release.Version',
'1.2.0'
)
expect(mockSettingStore.set).toHaveBeenCalledWith(
expect(settingStore.set).toHaveBeenCalledWith(
'Comfy.Release.Status',
'changelog seen'
)
expect(mockSettingStore.set).toHaveBeenCalledWith(
expect(settingStore.set).toHaveBeenCalledWith(
'Comfy.Release.Timestamp',
expect.any(Number)
)
})
it('should handle whats new seen', async () => {
const store = useReleaseStore()
store.releases = [mockRelease]
const settingStore = useSettingStore()
await store.handleWhatsNewSeen('1.2.0')
expect(mockSettingStore.set).toHaveBeenCalledWith(
expect(settingStore.set).toHaveBeenCalledWith(
'Comfy.Release.Version',
'1.2.0'
)
expect(mockSettingStore.set).toHaveBeenCalledWith(
expect(settingStore.set).toHaveBeenCalledWith(
'Comfy.Release.Status',
"what's new seen"
)
expect(mockSettingStore.set).toHaveBeenCalledWith(
expect(settingStore.set).toHaveBeenCalledWith(
'Comfy.Release.Timestamp',
expect.any(Number)
)
@@ -519,7 +589,9 @@ describe('useReleaseStore', () => {
describe('popup visibility', () => {
it('should show toast for medium/high attention releases', () => {
mockSettingStore.get.mockImplementation((key: string) => {
const store = useReleaseStore()
const settingStore = useSettingStore()
vi.mocked(settingStore.get).mockImplementation((key: string) => {
if (key === 'Comfy.Release.Version') return null
if (key === 'Comfy.Release.Status') return null
if (key === 'Comfy.Notification.ShowVersionUpdates') return true
@@ -534,8 +606,10 @@ describe('useReleaseStore', () => {
})
it('should show red dot for new versions', () => {
const store = useReleaseStore()
const settingStore = useSettingStore()
vi.mocked(compare).mockReturnValue(1)
mockSettingStore.get.mockImplementation((key: string) => {
vi.mocked(settingStore.get).mockImplementation((key: string) => {
if (key === 'Comfy.Notification.ShowVersionUpdates') return true
return null
})
@@ -546,8 +620,11 @@ describe('useReleaseStore', () => {
})
it('should show popup for latest version', () => {
mockSystemStatsStore.systemStats!.system.comfyui_version = '1.2.0' // Same as release
mockSettingStore.get.mockImplementation((key: string) => {
const store = useReleaseStore()
const systemStatsStore = useSystemStatsStore()
const settingStore = useSettingStore()
systemStatsStore.systemStats!.system.comfyui_version = '1.2.0' // Same as release
vi.mocked(settingStore.get).mockImplementation((key: string) => {
if (key === 'Comfy.Notification.ShowVersionUpdates') return true
return null
})
@@ -562,8 +639,11 @@ describe('useReleaseStore', () => {
describe('edge cases', () => {
it('should handle missing system stats gracefully', async () => {
mockSystemStatsStore.systemStats = null
mockSettingStore.get.mockImplementation((key: string) => {
const store = useReleaseStore()
const systemStatsStore = useSystemStatsStore()
const settingStore = useSettingStore()
systemStatsStore.systemStats = null
vi.mocked(settingStore.get).mockImplementation((key: string) => {
if (key === 'Comfy.Notification.ShowVersionUpdates') return false
return null
})
@@ -571,11 +651,13 @@ describe('useReleaseStore', () => {
await store.initialize()
// Should not fetch system stats when notifications disabled
expect(mockSystemStatsStore.refetchSystemStats).not.toHaveBeenCalled()
expect(systemStatsStore.refetchSystemStats).not.toHaveBeenCalled()
})
it('should handle concurrent fetchReleases calls', async () => {
mockReleaseService.getReleases.mockImplementation(
const store = useReleaseStore()
const releaseService = useReleaseService()
vi.mocked(releaseService.getReleases).mockImplementation(
() =>
new Promise((resolve) =>
setTimeout(() => resolve([mockRelease]), 100)
@@ -589,41 +671,37 @@ describe('useReleaseStore', () => {
await Promise.all([promise1, promise2])
// Should only call API once due to loading check
expect(mockReleaseService.getReleases).toHaveBeenCalledTimes(1)
expect(releaseService.getReleases).toHaveBeenCalledTimes(1)
})
})
describe('isElectron environment checks', () => {
beforeEach(async () => {
// Set up a new version available
store.releases = [mockRelease]
mockSettingStore.get.mockImplementation((key: string) => {
if (key === 'Comfy.Notification.ShowVersionUpdates') return true
return null
})
})
describe('when running in Electron (desktop)', () => {
beforeEach(async () => {
const { isElectron } = await import('@/utils/envUtil')
beforeEach(() => {
vi.mocked(isElectron).mockReturnValue(true)
})
it('should show toast when conditions are met', () => {
vi.mocked(compare).mockReturnValue(1)
const store = useReleaseStore()
store.releases = [mockRelease]
vi.mocked(compare).mockReturnValue(1)
expect(store.shouldShowToast).toBe(true)
})
it('should show red dot when new version available', () => {
const store = useReleaseStore()
store.releases = [mockRelease]
vi.mocked(compare).mockReturnValue(1)
expect(store.shouldShowRedDot).toBe(true)
})
it('should show popup for latest version', () => {
mockSystemStatsStore.systemStats!.system.comfyui_version = '1.2.0'
const store = useReleaseStore()
store.releases = [mockRelease]
const systemStatsStore = useSystemStatsStore()
systemStatsStore.systemStats!.system.comfyui_version = '1.2.0'
vi.mocked(compare).mockReturnValue(0)
@@ -632,12 +710,12 @@ describe('useReleaseStore', () => {
})
describe('when NOT running in Electron (web)', () => {
beforeEach(async () => {
const { isElectron } = await import('@/utils/envUtil')
beforeEach(() => {
vi.mocked(isElectron).mockReturnValue(false)
})
it('should NOT show toast even when all other conditions are met', () => {
const store = useReleaseStore()
vi.mocked(compare).mockReturnValue(1)
// Set up all conditions that would normally show toast
@@ -647,12 +725,15 @@ describe('useReleaseStore', () => {
})
it('should NOT show red dot even when new version available', () => {
const store = useReleaseStore()
store.releases = [mockRelease]
vi.mocked(compare).mockReturnValue(1)
expect(store.shouldShowRedDot).toBe(false)
})
it('should NOT show toast regardless of attention level', () => {
const store = useReleaseStore()
vi.mocked(compare).mockReturnValue(1)
// Test with high attention releases
@@ -672,6 +753,7 @@ describe('useReleaseStore', () => {
})
it('should NOT show red dot even with high attention release', () => {
const store = useReleaseStore()
vi.mocked(compare).mockReturnValue(1)
store.releases = [{ ...mockRelease, attention: 'high' as const }]
@@ -680,7 +762,10 @@ describe('useReleaseStore', () => {
})
it('should NOT show popup even for latest version', () => {
mockSystemStatsStore.systemStats!.system.comfyui_version = '1.2.0'
const store = useReleaseStore()
store.releases = [mockRelease]
const systemStatsStore = useSystemStatsStore()
systemStatsStore.systemStats!.system.comfyui_version = '1.2.0'
vi.mocked(compare).mockReturnValue(0)

View File

@@ -1,3 +1,4 @@
import { until } from '@vueuse/core'
import { createPinia, setActivePinia } from 'pinia'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { ref } from 'vue'
@@ -357,17 +358,15 @@ describe('useVersionCompatibilityStore', () => {
describe('initialization', () => {
it('should fetch system stats if not available', async () => {
const { until } = await import('@vueuse/core')
mockSystemStatsStore.systemStats = null
mockSystemStatsStore.isInitialized = false
await store.initialize()
expect(until).toHaveBeenCalled()
expect(vi.mocked(until)).toHaveBeenCalled()
})
it('should not fetch system stats if already available', async () => {
const { until } = await import('@vueuse/core')
mockSystemStatsStore.systemStats = {
system: {
comfyui_version: '1.24.0',
@@ -378,7 +377,7 @@ describe('useVersionCompatibilityStore', () => {
await store.initialize()
expect(until).not.toHaveBeenCalled()
expect(vi.mocked(until)).not.toHaveBeenCalled()
})
})
})