mirror of
https://github.com/Comfy-Org/ComfyUI_frontend.git
synced 2026-03-03 12:10:11 +00:00
## Summary
This PR removes unsafe type assertions ("as unknown as Type") from test
files and improves type safety across the codebase.
### Key Changes
#### Type Safety Improvements
- Removed improper `as unknown as Type` patterns from 17 test files in
Group 8 part 7
- Replaced with proper TypeScript patterns using factory functions and
Mock types
- Fixed createTestingPinia usage in test files (was incorrectly using
createPinia)
- Fixed vi.hoisted pattern for mockSetDirty in viewport tests
- Fixed vi.doMock lint issues with vi.mock and vi.hoisted pattern
- Retained necessary `as unknown as` casts only for complex mock objects
where direct type assertions would fail
### Files Changed
Test files (Group 8 part 7 - services, stores, utils):
- src/services/nodeOrganizationService.test.ts
- src/services/providers/algoliaSearchProvider.test.ts
- src/services/providers/registrySearchProvider.test.ts
- src/stores/comfyRegistryStore.test.ts
- src/stores/domWidgetStore.test.ts
- src/stores/executionStore.test.ts
- src/stores/firebaseAuthStore.test.ts
- src/stores/modelToNodeStore.test.ts
- src/stores/queueStore.test.ts
- src/stores/subgraphNavigationStore.test.ts
- src/stores/subgraphNavigationStore.viewport.test.ts
- src/stores/subgraphStore.test.ts
- src/stores/systemStatsStore.test.ts
- src/stores/workspace/nodeHelpStore.test.ts
- src/utils/colorUtil.test.ts
- src/utils/executableGroupNodeChildDTO.test.ts
Source files:
- src/stores/modelStore.ts - Improved type handling
### Testing
- All TypeScript type checking passes (`pnpm typecheck`)
- All affected test files pass (`pnpm test:unit`)
- Linting passes without errors (`pnpm lint`)
- Code formatting applied (`pnpm format`)
Part of the "Road to No Explicit Any" initiative, cleaning up type
casting issues from branch `fix/remove-any-types-part8`.
### Previous PRs in this series:
- Part 2: #7401
- Part 3: #7935
- Part 4: #7970
- Part 5: #8064
- Part 6: #8083
- Part 7: #8092
- Part 8 Group 1: #8253
- Part 8 Group 2: #8258
- Part 8 Group 3: #8304
- Part 8 Group 4: #8314
- Part 8 Group 5: #8329
- Part 8 Group 6: #8344
- Part 8 Group 7: #8459 (this PR)
┆Issue is synchronized with this [Notion
page](https://www.notion.so/PR-8459-Road-to-No-explicit-any-Group-8-part-7-test-files-2f86d73d36508114ad28d82e72a3a5e9)
by [Unito](https://www.unito.io)
275 lines
8.2 KiB
TypeScript
275 lines
8.2 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
import { useComfyRegistrySearchProvider } from '@/services/providers/registrySearchProvider'
|
|
import { useComfyRegistryStore } from '@/stores/comfyRegistryStore'
|
|
|
|
// Mock the store
|
|
vi.mock('@/stores/comfyRegistryStore', () => ({
|
|
useComfyRegistryStore: vi.fn()
|
|
}))
|
|
|
|
describe('useComfyRegistrySearchProvider', () => {
|
|
const mockSearchCall = vi.fn()
|
|
const mockSearchClear = vi.fn()
|
|
const mockListAllPacksCall = vi.fn()
|
|
const mockListAllPacksClear = vi.fn()
|
|
|
|
const createMockStore = (
|
|
params: Partial<ReturnType<typeof useComfyRegistryStore>> = {}
|
|
) => {
|
|
return {
|
|
search: {
|
|
call: mockSearchCall,
|
|
clear: mockSearchClear,
|
|
cancel: vi.fn()
|
|
},
|
|
listAllPacks: {
|
|
call: mockListAllPacksCall,
|
|
clear: mockListAllPacksClear,
|
|
cancel: vi.fn()
|
|
},
|
|
...params
|
|
} as Partial<ReturnType<typeof useComfyRegistryStore>> as ReturnType<
|
|
typeof useComfyRegistryStore
|
|
>
|
|
}
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks()
|
|
|
|
// Setup store mock
|
|
vi.mocked(useComfyRegistryStore).mockReturnValue(createMockStore())
|
|
})
|
|
|
|
describe('searchPacks', () => {
|
|
it('should search for packs by name', async () => {
|
|
const mockResults = {
|
|
nodes: [
|
|
{ id: '1', name: 'Test Pack 1' },
|
|
{ id: '2', name: 'Test Pack 2' }
|
|
]
|
|
}
|
|
mockSearchCall.mockResolvedValue(mockResults)
|
|
|
|
const provider = useComfyRegistrySearchProvider()
|
|
const result = await provider.searchPacks('test', {
|
|
pageSize: 10,
|
|
pageNumber: 0,
|
|
restrictSearchableAttributes: ['name', 'description']
|
|
})
|
|
|
|
expect(mockSearchCall).toHaveBeenCalledWith({
|
|
search: 'test',
|
|
comfy_node_search: undefined,
|
|
limit: 10,
|
|
page: 1
|
|
})
|
|
expect(result.nodePacks).toEqual(mockResults.nodes)
|
|
expect(result.querySuggestions).toEqual([])
|
|
})
|
|
|
|
it('should search for packs by node names', async () => {
|
|
const mockResults = {
|
|
nodes: [{ id: '1', name: 'Pack with LoadImage node' }]
|
|
}
|
|
mockSearchCall.mockResolvedValue(mockResults)
|
|
|
|
const provider = useComfyRegistrySearchProvider()
|
|
const result = await provider.searchPacks('LoadImage', {
|
|
pageSize: 20,
|
|
pageNumber: 1,
|
|
restrictSearchableAttributes: ['comfy_nodes']
|
|
})
|
|
|
|
expect(mockSearchCall).toHaveBeenCalledWith({
|
|
search: undefined,
|
|
comfy_node_search: 'LoadImage',
|
|
limit: 20,
|
|
page: 2
|
|
})
|
|
expect(result.nodePacks).toEqual(mockResults.nodes)
|
|
})
|
|
|
|
it('should handle empty results', async () => {
|
|
mockSearchCall.mockResolvedValue({ nodes: [] })
|
|
|
|
const provider = useComfyRegistrySearchProvider()
|
|
const result = await provider.searchPacks('nonexistent', {
|
|
pageSize: 10,
|
|
pageNumber: 0
|
|
})
|
|
|
|
expect(result.nodePacks).toEqual([])
|
|
expect(result.querySuggestions).toEqual([])
|
|
})
|
|
|
|
it('should handle null results', async () => {
|
|
mockSearchCall.mockResolvedValue(null)
|
|
|
|
const provider = useComfyRegistrySearchProvider()
|
|
const result = await provider.searchPacks('test', {
|
|
pageSize: 10,
|
|
pageNumber: 0
|
|
})
|
|
|
|
expect(result.nodePacks).toEqual([])
|
|
expect(result.querySuggestions).toEqual([])
|
|
})
|
|
|
|
it('should handle results without nodes property', async () => {
|
|
mockSearchCall.mockResolvedValue({})
|
|
|
|
const provider = useComfyRegistrySearchProvider()
|
|
const result = await provider.searchPacks('test', {
|
|
pageSize: 10,
|
|
pageNumber: 0
|
|
})
|
|
|
|
expect(result.nodePacks).toEqual([])
|
|
expect(result.querySuggestions).toEqual([])
|
|
})
|
|
|
|
it('should use listAllPacks for empty query', async () => {
|
|
const mockResults = {
|
|
nodes: [
|
|
{ id: '1', name: 'Pack 1' },
|
|
{ id: '2', name: 'Pack 2' }
|
|
]
|
|
}
|
|
mockListAllPacksCall.mockResolvedValue(mockResults)
|
|
|
|
const provider = useComfyRegistrySearchProvider()
|
|
const result = await provider.searchPacks('', {
|
|
pageSize: 20,
|
|
pageNumber: 0
|
|
})
|
|
|
|
expect(mockListAllPacksCall).toHaveBeenCalledWith({
|
|
limit: 20,
|
|
page: 1
|
|
})
|
|
expect(mockSearchCall).not.toHaveBeenCalled()
|
|
expect(result.nodePacks).toEqual(mockResults.nodes)
|
|
expect(result.querySuggestions).toEqual([])
|
|
})
|
|
|
|
it('should use listAllPacks for whitespace-only query', async () => {
|
|
const mockResults = {
|
|
nodes: [{ id: '1', name: 'Pack 1' }]
|
|
}
|
|
mockListAllPacksCall.mockResolvedValue(mockResults)
|
|
|
|
const provider = useComfyRegistrySearchProvider()
|
|
const result = await provider.searchPacks(' ', {
|
|
pageSize: 10,
|
|
pageNumber: 0
|
|
})
|
|
|
|
expect(mockListAllPacksCall).toHaveBeenCalledWith({
|
|
limit: 10,
|
|
page: 1
|
|
})
|
|
expect(mockSearchCall).not.toHaveBeenCalled()
|
|
expect(result.nodePacks).toEqual(mockResults.nodes)
|
|
})
|
|
|
|
it('should handle empty results from listAllPacks', async () => {
|
|
mockListAllPacksCall.mockResolvedValue({ nodes: [] })
|
|
|
|
const provider = useComfyRegistrySearchProvider()
|
|
const result = await provider.searchPacks('', {
|
|
pageSize: 10,
|
|
pageNumber: 0
|
|
})
|
|
|
|
expect(result.nodePacks).toEqual([])
|
|
expect(result.querySuggestions).toEqual([])
|
|
})
|
|
|
|
it('should handle null results from listAllPacks', async () => {
|
|
mockListAllPacksCall.mockResolvedValue(null)
|
|
|
|
const provider = useComfyRegistrySearchProvider()
|
|
const result = await provider.searchPacks('', {
|
|
pageSize: 10,
|
|
pageNumber: 0
|
|
})
|
|
|
|
expect(result.nodePacks).toEqual([])
|
|
expect(result.querySuggestions).toEqual([])
|
|
})
|
|
})
|
|
|
|
describe('clearSearchCache', () => {
|
|
it('should clear both search and listAllPacks caches', () => {
|
|
const provider = useComfyRegistrySearchProvider()
|
|
provider.clearSearchCache()
|
|
|
|
expect(mockSearchClear).toHaveBeenCalled()
|
|
expect(mockListAllPacksClear).toHaveBeenCalled()
|
|
})
|
|
})
|
|
|
|
describe('getSortValue', () => {
|
|
const testPack = {
|
|
id: '1',
|
|
name: 'Test Pack',
|
|
downloads: 100,
|
|
publisher: { id: 'pub1', name: 'Publisher One' },
|
|
latest_version: {
|
|
version: '1.0.0',
|
|
createdAt: '2024-01-15T10:00:00Z'
|
|
}
|
|
}
|
|
|
|
it('should return download count for downloads field', () => {
|
|
const provider = useComfyRegistrySearchProvider()
|
|
expect(provider.getSortValue(testPack, 'downloads')).toBe(100)
|
|
})
|
|
|
|
it('should return pack name for name field', () => {
|
|
const provider = useComfyRegistrySearchProvider()
|
|
expect(provider.getSortValue(testPack, 'name')).toBe('Test Pack')
|
|
})
|
|
|
|
it('should return publisher name for publisher field', () => {
|
|
const provider = useComfyRegistrySearchProvider()
|
|
expect(provider.getSortValue(testPack, 'publisher')).toBe('Publisher One')
|
|
})
|
|
|
|
it('should return timestamp for updated field', () => {
|
|
const provider = useComfyRegistrySearchProvider()
|
|
const timestamp = new Date('2024-01-15T10:00:00Z').getTime()
|
|
expect(provider.getSortValue(testPack, 'updated')).toBe(timestamp)
|
|
})
|
|
|
|
it('should handle missing values gracefully', () => {
|
|
const incompletePack = { id: '1', name: 'Incomplete' }
|
|
const provider = useComfyRegistrySearchProvider()
|
|
|
|
expect(provider.getSortValue(incompletePack, 'downloads')).toBe(0)
|
|
expect(provider.getSortValue(incompletePack, 'publisher')).toBe('')
|
|
expect(provider.getSortValue(incompletePack, 'updated')).toBe(0)
|
|
})
|
|
|
|
it('should return 0 for unknown sort fields', () => {
|
|
const provider = useComfyRegistrySearchProvider()
|
|
expect(provider.getSortValue(testPack, 'unknown')).toBe(0)
|
|
})
|
|
})
|
|
|
|
describe('getSortableFields', () => {
|
|
it('should return supported sort fields', () => {
|
|
const provider = useComfyRegistrySearchProvider()
|
|
const fields = provider.getSortableFields()
|
|
|
|
expect(fields).toEqual([
|
|
{ id: 'downloads', label: 'Downloads', direction: 'desc' },
|
|
{ id: 'name', label: 'Name', direction: 'asc' },
|
|
{ id: 'publisher', label: 'Publisher', direction: 'asc' },
|
|
{ id: 'updated', label: 'Updated', direction: 'desc' }
|
|
])
|
|
})
|
|
})
|
|
})
|