|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 2 | + |
| 3 | +const { |
| 4 | + mockIsLoggedIn, |
| 5 | + mockCanAccessSubscriptionFeatures, |
| 6 | + mockRefreshRemoteConfig, |
| 7 | + mockRegisterExtension, |
| 8 | + mockWatchDebounced |
| 9 | +} = vi.hoisted(() => ({ |
| 10 | + mockIsLoggedIn: { value: false }, |
| 11 | + mockCanAccessSubscriptionFeatures: { value: true }, |
| 12 | + mockRefreshRemoteConfig: vi.fn(), |
| 13 | + mockRegisterExtension: vi.fn(), |
| 14 | + mockWatchDebounced: vi.fn() |
| 15 | +})) |
| 16 | + |
| 17 | +vi.mock('@vueuse/core', () => ({ |
| 18 | + watchDebounced: mockWatchDebounced |
| 19 | +})) |
| 20 | + |
| 21 | +vi.mock('@/composables/auth/useCurrentUser', () => ({ |
| 22 | + useCurrentUser: () => ({ |
| 23 | + isLoggedIn: mockIsLoggedIn |
| 24 | + }) |
| 25 | +})) |
| 26 | + |
| 27 | +vi.mock('@/composables/billing/useBillingContext', () => ({ |
| 28 | + useBillingContext: () => ({ |
| 29 | + canAccessSubscriptionFeatures: mockCanAccessSubscriptionFeatures |
| 30 | + }) |
| 31 | +})) |
| 32 | + |
| 33 | +vi.mock('@/platform/remoteConfig/refreshRemoteConfig', () => ({ |
| 34 | + refreshRemoteConfig: mockRefreshRemoteConfig |
| 35 | +})) |
| 36 | + |
| 37 | +vi.mock('@/services/extensionService', () => ({ |
| 38 | + useExtensionService: () => ({ |
| 39 | + registerExtension: mockRegisterExtension |
| 40 | + }) |
| 41 | +})) |
| 42 | + |
| 43 | +describe('cloudRemoteConfig extension', () => { |
| 44 | + beforeEach(() => { |
| 45 | + vi.clearAllMocks() |
| 46 | + vi.resetModules() |
| 47 | + mockIsLoggedIn.value = false |
| 48 | + mockCanAccessSubscriptionFeatures.value = true |
| 49 | + }) |
| 50 | + |
| 51 | + it('registers extension with correct name', async () => { |
| 52 | + await import('./cloudRemoteConfig') |
| 53 | + |
| 54 | + expect(mockRegisterExtension).toHaveBeenCalledWith( |
| 55 | + expect.objectContaining({ |
| 56 | + name: 'Comfy.Cloud.RemoteConfig' |
| 57 | + }) |
| 58 | + ) |
| 59 | + }) |
| 60 | + |
| 61 | + it('setup watches isLoggedIn and canAccessSubscriptionFeatures', async () => { |
| 62 | + await import('./cloudRemoteConfig') |
| 63 | + |
| 64 | + const registeredExtension = mockRegisterExtension.mock.calls[0][0] |
| 65 | + await registeredExtension.setup() |
| 66 | + |
| 67 | + expect(mockWatchDebounced).toHaveBeenCalledWith( |
| 68 | + [mockIsLoggedIn, mockCanAccessSubscriptionFeatures], |
| 69 | + expect.any(Function), |
| 70 | + expect.objectContaining({ debounce: 256, immediate: true }) |
| 71 | + ) |
| 72 | + }) |
| 73 | + |
| 74 | + it('watch callback refreshes config when logged in', async () => { |
| 75 | + await import('./cloudRemoteConfig') |
| 76 | + |
| 77 | + const registeredExtension = mockRegisterExtension.mock.calls[0][0] |
| 78 | + await registeredExtension.setup() |
| 79 | + |
| 80 | + // Get the callback passed to watchDebounced |
| 81 | + const watchCallback = mockWatchDebounced.mock.calls[0][1] |
| 82 | + |
| 83 | + // Simulate logged in state |
| 84 | + mockIsLoggedIn.value = true |
| 85 | + watchCallback() |
| 86 | + |
| 87 | + expect(mockRefreshRemoteConfig).toHaveBeenCalled() |
| 88 | + }) |
| 89 | + |
| 90 | + it('watch callback does not refresh when not logged in', async () => { |
| 91 | + await import('./cloudRemoteConfig') |
| 92 | + |
| 93 | + const registeredExtension = mockRegisterExtension.mock.calls[0][0] |
| 94 | + await registeredExtension.setup() |
| 95 | + |
| 96 | + const watchCallback = mockWatchDebounced.mock.calls[0][1] |
| 97 | + |
| 98 | + mockIsLoggedIn.value = false |
| 99 | + watchCallback() |
| 100 | + |
| 101 | + expect(mockRefreshRemoteConfig).not.toHaveBeenCalled() |
| 102 | + }) |
| 103 | +}) |
0 commit comments