mirror of
https://github.com/Comfy-Org/ComfyUI_frontend.git
synced 2026-04-22 23:39:45 +00:00
chore: migrate tests from tests-ui/ to colocate with source files (#7811)
## Summary Migrates all unit tests from `tests-ui/` to colocate with their source files in `src/`, improving discoverability and maintainability. ## Changes - **What**: Relocated all unit tests to be adjacent to the code they test, following the `<source>.test.ts` naming convention - **Config**: Updated `vitest.config.ts` to remove `tests-ui` include pattern and `@tests-ui` alias - **Docs**: Moved testing documentation to `docs/testing/` with updated paths and patterns ## Review Focus - Migration patterns documented in `temp/plans/migrate-tests-ui-to-src.md` - Tests use `@/` path aliases instead of relative imports - Shared fixtures placed in `__fixtures__/` directories ┆Issue is synchronized with this [Notion page](https://www.notion.so/PR-7811-chore-migrate-tests-from-tests-ui-to-colocate-with-source-files-2da6d73d36508147a4cce85365dee614) 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:
208
src/platform/telemetry/topupTracker.test.ts
Normal file
208
src/platform/telemetry/topupTracker.test.ts
Normal file
@@ -0,0 +1,208 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
|
||||
import type * as TopupTrackerModule from '@/platform/telemetry/topupTracker'
|
||||
import type { AuditLog } from '@/services/customerEventsService'
|
||||
|
||||
// Mock localStorage
|
||||
const mockLocalStorage = vi.hoisted(() => ({
|
||||
getItem: vi.fn(),
|
||||
setItem: vi.fn(),
|
||||
removeItem: vi.fn()
|
||||
}))
|
||||
|
||||
Object.defineProperty(window, 'localStorage', {
|
||||
value: mockLocalStorage,
|
||||
writable: true
|
||||
})
|
||||
|
||||
// Mock telemetry
|
||||
const mockTelemetry = vi.hoisted(() => ({
|
||||
trackApiCreditTopupSucceeded: vi.fn()
|
||||
}))
|
||||
|
||||
vi.mock('@/platform/telemetry', () => ({
|
||||
useTelemetry: vi.fn(() => mockTelemetry)
|
||||
}))
|
||||
|
||||
describe('topupTracker', () => {
|
||||
let topupTracker: typeof TopupTrackerModule
|
||||
|
||||
beforeEach(async () => {
|
||||
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()
|
||||
|
||||
expect(mockLocalStorage.setItem).toHaveBeenCalledWith(
|
||||
'pending_topup_timestamp',
|
||||
expect.any(String)
|
||||
)
|
||||
|
||||
const savedTimestamp = parseInt(
|
||||
mockLocalStorage.setItem.mock.calls[0][1],
|
||||
10
|
||||
)
|
||||
expect(savedTimestamp).toBeGreaterThanOrEqual(beforeTimestamp)
|
||||
expect(savedTimestamp).toBeLessThanOrEqual(Date.now())
|
||||
})
|
||||
})
|
||||
|
||||
describe('checkForCompletedTopup', () => {
|
||||
it('should return false if no pending topup exists', () => {
|
||||
mockLocalStorage.getItem.mockReturnValue(null)
|
||||
|
||||
const result = topupTracker.checkForCompletedTopup([])
|
||||
|
||||
expect(result).toBe(false)
|
||||
expect(mockTelemetry.trackApiCreditTopupSucceeded).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('should return false if events array is empty', () => {
|
||||
mockLocalStorage.getItem.mockReturnValue(Date.now().toString())
|
||||
|
||||
const result = topupTracker.checkForCompletedTopup([])
|
||||
|
||||
expect(result).toBe(false)
|
||||
expect(mockTelemetry.trackApiCreditTopupSucceeded).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('should return false if events array is null', () => {
|
||||
mockLocalStorage.getItem.mockReturnValue(Date.now().toString())
|
||||
|
||||
const result = topupTracker.checkForCompletedTopup(null)
|
||||
|
||||
expect(result).toBe(false)
|
||||
expect(mockTelemetry.trackApiCreditTopupSucceeded).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('should auto-cleanup if timestamp is older than 24 hours', () => {
|
||||
const oldTimestamp = Date.now() - 25 * 60 * 60 * 1000 // 25 hours ago
|
||||
mockLocalStorage.getItem.mockReturnValue(oldTimestamp.toString())
|
||||
|
||||
const events: AuditLog[] = [
|
||||
{
|
||||
event_id: 'test-1',
|
||||
event_type: 'credit_added',
|
||||
createdAt: new Date().toISOString(),
|
||||
params: { amount: 500 }
|
||||
}
|
||||
]
|
||||
|
||||
const result = topupTracker.checkForCompletedTopup(events)
|
||||
|
||||
expect(result).toBe(false)
|
||||
expect(mockLocalStorage.removeItem).toHaveBeenCalledWith(
|
||||
'pending_topup_timestamp'
|
||||
)
|
||||
expect(mockTelemetry.trackApiCreditTopupSucceeded).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('should detect completed topup and fire telemetry', () => {
|
||||
const startTimestamp = Date.now() - 5 * 60 * 1000 // 5 minutes ago
|
||||
mockLocalStorage.getItem.mockReturnValue(startTimestamp.toString())
|
||||
|
||||
const events: AuditLog[] = [
|
||||
{
|
||||
event_id: 'test-1',
|
||||
event_type: 'api_usage_completed',
|
||||
createdAt: new Date(startTimestamp - 1000).toISOString(),
|
||||
params: {}
|
||||
},
|
||||
{
|
||||
event_id: 'test-2',
|
||||
event_type: 'credit_added',
|
||||
createdAt: new Date(startTimestamp + 1000).toISOString(),
|
||||
params: { amount: 500 }
|
||||
}
|
||||
]
|
||||
|
||||
const result = topupTracker.checkForCompletedTopup(events)
|
||||
|
||||
expect(result).toBe(true)
|
||||
expect(mockTelemetry.trackApiCreditTopupSucceeded).toHaveBeenCalledOnce()
|
||||
expect(mockLocalStorage.removeItem).toHaveBeenCalledWith(
|
||||
'pending_topup_timestamp'
|
||||
)
|
||||
})
|
||||
|
||||
it('should not detect topup if credit_added event is before tracking started', () => {
|
||||
const startTimestamp = Date.now()
|
||||
mockLocalStorage.getItem.mockReturnValue(startTimestamp.toString())
|
||||
|
||||
const events: AuditLog[] = [
|
||||
{
|
||||
event_id: 'test-1',
|
||||
event_type: 'credit_added',
|
||||
createdAt: new Date(startTimestamp - 1000).toISOString(), // Before tracking
|
||||
params: { amount: 500 }
|
||||
}
|
||||
]
|
||||
|
||||
const result = topupTracker.checkForCompletedTopup(events)
|
||||
|
||||
expect(result).toBe(false)
|
||||
expect(mockTelemetry.trackApiCreditTopupSucceeded).not.toHaveBeenCalled()
|
||||
expect(mockLocalStorage.removeItem).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('should ignore events without createdAt timestamp', () => {
|
||||
const startTimestamp = Date.now()
|
||||
mockLocalStorage.getItem.mockReturnValue(startTimestamp.toString())
|
||||
|
||||
const events: AuditLog[] = [
|
||||
{
|
||||
event_id: 'test-1',
|
||||
event_type: 'credit_added',
|
||||
createdAt: undefined,
|
||||
params: { amount: 500 }
|
||||
}
|
||||
]
|
||||
|
||||
const result = topupTracker.checkForCompletedTopup(events)
|
||||
|
||||
expect(result).toBe(false)
|
||||
expect(mockTelemetry.trackApiCreditTopupSucceeded).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('should only match credit_added events, not other event types', () => {
|
||||
const startTimestamp = Date.now()
|
||||
mockLocalStorage.getItem.mockReturnValue(startTimestamp.toString())
|
||||
|
||||
const events: AuditLog[] = [
|
||||
{
|
||||
event_id: 'test-1',
|
||||
event_type: 'api_usage_completed',
|
||||
createdAt: new Date(startTimestamp + 1000).toISOString(),
|
||||
params: {}
|
||||
},
|
||||
{
|
||||
event_id: 'test-2',
|
||||
event_type: 'account_created',
|
||||
createdAt: new Date(startTimestamp + 2000).toISOString(),
|
||||
params: {}
|
||||
}
|
||||
]
|
||||
|
||||
const result = topupTracker.checkForCompletedTopup(events)
|
||||
|
||||
expect(result).toBe(false)
|
||||
expect(mockTelemetry.trackApiCreditTopupSucceeded).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
|
||||
describe('clearTopupTracking', () => {
|
||||
it('should remove pending topup from localStorage', () => {
|
||||
topupTracker.clearTopupTracking()
|
||||
|
||||
expect(mockLocalStorage.removeItem).toHaveBeenCalledWith(
|
||||
'pending_topup_timestamp'
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
30
src/platform/telemetry/useTelemetry.test.ts
Normal file
30
src/platform/telemetry/useTelemetry.test.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
|
||||
vi.mock('@/platform/distribution/types', () => ({
|
||||
isCloud: false
|
||||
}))
|
||||
|
||||
describe('useTelemetry', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
it('should return null when not in cloud distribution', async () => {
|
||||
const { useTelemetry } = await import('@/platform/telemetry')
|
||||
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')
|
||||
|
||||
const provider1 = useTelemetry()
|
||||
const provider2 = useTelemetry()
|
||||
|
||||
// Both should be null for OSS builds
|
||||
expect(provider1).toBeNull()
|
||||
expect(provider2).toBeNull()
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user