mirror of
https://github.com/Comfy-Org/ComfyUI_frontend.git
synced 2026-05-11 16:30:57 +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)
201 lines
5.6 KiB
TypeScript
201 lines
5.6 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
import type { GroupNodeHandler } from '@/extensions/core/groupNode'
|
|
import type {
|
|
ExecutableLGraphNode,
|
|
ExecutionId,
|
|
LGraphNode
|
|
} from '@/lib/litegraph/src/litegraph'
|
|
import { ExecutableGroupNodeChildDTO } from '@/utils/executableGroupNodeChildDTO'
|
|
import { createMockLGraphNode } from './__tests__/litegraphTestUtils'
|
|
|
|
describe('ExecutableGroupNodeChildDTO', () => {
|
|
let mockNode: LGraphNode
|
|
let mockInputNode: LGraphNode
|
|
let mockNodesByExecutionId: Map<ExecutionId, ExecutableLGraphNode>
|
|
let mockGroupNodeHandler: GroupNodeHandler
|
|
|
|
beforeEach(() => {
|
|
// Create mock nodes
|
|
mockNode = createMockLGraphNode({
|
|
id: '3', // Simple node ID for most tests
|
|
graph: {},
|
|
getInputNode: vi.fn(),
|
|
getInputLink: vi.fn(),
|
|
inputs: []
|
|
})
|
|
|
|
mockInputNode = createMockLGraphNode({
|
|
id: '1',
|
|
graph: {}
|
|
})
|
|
|
|
// Create the nodesByExecutionId map
|
|
mockNodesByExecutionId = new Map()
|
|
|
|
mockGroupNodeHandler = {} as GroupNodeHandler
|
|
})
|
|
|
|
describe('resolveInput', () => {
|
|
it('should resolve input from external node (node outside the group)', () => {
|
|
// Setup: Group node child with ID '10:3'
|
|
const groupNodeChild = createMockLGraphNode({
|
|
id: '10:3',
|
|
graph: {},
|
|
getInputNode: vi.fn().mockReturnValue(mockInputNode),
|
|
getInputLink: vi.fn().mockReturnValue({
|
|
origin_slot: 0
|
|
}),
|
|
inputs: []
|
|
})
|
|
|
|
// External node with ID '1'
|
|
const externalNodeDto = {
|
|
id: '1',
|
|
type: 'TestNode'
|
|
} as ExecutableLGraphNode
|
|
|
|
mockNodesByExecutionId.set('1', externalNodeDto)
|
|
|
|
const dto = new ExecutableGroupNodeChildDTO(
|
|
groupNodeChild,
|
|
[], // No subgraph path - group is in root graph
|
|
mockNodesByExecutionId,
|
|
undefined,
|
|
mockGroupNodeHandler
|
|
)
|
|
|
|
const result = dto.resolveInput(0)
|
|
|
|
expect(result).toEqual({
|
|
node: externalNodeDto,
|
|
origin_id: '1',
|
|
origin_slot: 0
|
|
})
|
|
})
|
|
|
|
it('should resolve input from internal node (node inside the same group)', () => {
|
|
// Setup: Group node child with ID '10:3'
|
|
const groupNodeChild = createMockLGraphNode({
|
|
id: '10:3',
|
|
graph: {},
|
|
getInputNode: vi.fn(),
|
|
getInputLink: vi.fn(),
|
|
inputs: []
|
|
})
|
|
|
|
// Internal node with ID '10:2'
|
|
const internalInputNode = createMockLGraphNode({
|
|
id: '10:2',
|
|
graph: {}
|
|
})
|
|
|
|
const internalNodeDto = {
|
|
id: '2',
|
|
type: 'InternalNode'
|
|
} as ExecutableLGraphNode
|
|
|
|
// Internal nodes are stored with just their index
|
|
mockNodesByExecutionId.set('2', internalNodeDto)
|
|
|
|
vi.mocked(groupNodeChild.getInputNode).mockReturnValue(internalInputNode)
|
|
vi.mocked(groupNodeChild.getInputLink).mockReturnValue({
|
|
origin_slot: 1
|
|
} as ReturnType<LGraphNode['getInputLink']>)
|
|
|
|
const dto = new ExecutableGroupNodeChildDTO(
|
|
groupNodeChild,
|
|
[],
|
|
mockNodesByExecutionId,
|
|
undefined,
|
|
mockGroupNodeHandler
|
|
)
|
|
|
|
const result = dto.resolveInput(0)
|
|
|
|
expect(result).toEqual({
|
|
node: internalNodeDto,
|
|
origin_id: '10:2',
|
|
origin_slot: 1
|
|
})
|
|
})
|
|
|
|
it('should return undefined if no input node exists', () => {
|
|
mockNode.getInputNode = vi.fn().mockReturnValue(null)
|
|
|
|
const dto = new ExecutableGroupNodeChildDTO(
|
|
mockNode,
|
|
[],
|
|
mockNodesByExecutionId,
|
|
undefined,
|
|
mockGroupNodeHandler
|
|
)
|
|
|
|
const result = dto.resolveInput(0)
|
|
|
|
expect(result).toBeUndefined()
|
|
})
|
|
|
|
it('should throw error if input link is missing', () => {
|
|
mockNode.getInputNode = vi.fn().mockReturnValue(mockInputNode)
|
|
mockNode.getInputLink = vi.fn().mockReturnValue(null)
|
|
|
|
const dto = new ExecutableGroupNodeChildDTO(
|
|
mockNode,
|
|
[],
|
|
mockNodesByExecutionId,
|
|
undefined,
|
|
mockGroupNodeHandler
|
|
)
|
|
|
|
expect(() => dto.resolveInput(0)).toThrow('Failed to get input link')
|
|
})
|
|
|
|
it('should throw error if input node cannot be found in nodesByExecutionId', () => {
|
|
// Node exists but is not in the map
|
|
mockNode.getInputNode = vi.fn().mockReturnValue(mockInputNode)
|
|
mockNode.getInputLink = vi.fn().mockReturnValue({
|
|
origin_slot: 0
|
|
})
|
|
|
|
const dto = new ExecutableGroupNodeChildDTO(
|
|
mockNode,
|
|
[],
|
|
mockNodesByExecutionId, // Empty map
|
|
undefined,
|
|
mockGroupNodeHandler
|
|
)
|
|
|
|
expect(() => dto.resolveInput(0)).toThrow(
|
|
'Failed to get input node 1 for group node child 3 with slot 0'
|
|
)
|
|
})
|
|
|
|
it('should throw error for group nodes inside subgraphs (unsupported)', () => {
|
|
// Setup: Group node child inside a subgraph (execution ID has more than 2 segments)
|
|
const nestedGroupNode = createMockLGraphNode({
|
|
id: '1:2:3', // subgraph:groupnode:innernode
|
|
graph: {},
|
|
getInputNode: vi.fn().mockReturnValue(mockInputNode),
|
|
getInputLink: vi.fn().mockReturnValue({
|
|
origin_slot: 0
|
|
}),
|
|
inputs: []
|
|
})
|
|
|
|
// Create DTO with deeply nested path to simulate group node inside subgraph
|
|
const dto = new ExecutableGroupNodeChildDTO(
|
|
nestedGroupNode,
|
|
['1', '2'], // Path indicating it's inside a subgraph then group
|
|
mockNodesByExecutionId,
|
|
undefined,
|
|
mockGroupNodeHandler
|
|
)
|
|
|
|
expect(() => dto.resolveInput(0)).toThrow(
|
|
'Group nodes inside subgraphs are not supported. Please convert the group node to a subgraph instead.'
|
|
)
|
|
})
|
|
})
|
|
})
|