Files
ComfyUI_frontend/src/utils/litegraphUtil.test.ts
Alexander Brown 10feb1fd5b 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>
2026-01-05 16:32:24 -08:00

174 lines
4.6 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import type { ISerialisedGraph } from '@/lib/litegraph/src/types/serialisation'
import type { IWidget } from '@/lib/litegraph/src/types/widgets'
import type { InputSpec } from '@/schemas/nodeDef/nodeDefSchemaV2'
import {
compressWidgetInputSlots,
migrateWidgetsValues
} from '@/utils/litegraphUtil'
describe('migrateWidgetsValues', () => {
it('should remove widget values for forceInput inputs', () => {
const inputDefs: Record<string, InputSpec> = {
normalInput: {
type: 'INT',
name: 'normalInput'
},
forceInputField: {
type: 'STRING',
name: 'forceInputField',
forceInput: true
},
anotherNormal: {
type: 'FLOAT',
name: 'anotherNormal'
}
}
const widgets: IWidget[] = [
{ name: 'normalInput', type: 'number' },
{ name: 'anotherNormal', type: 'number' }
] as unknown as IWidget[]
const widgetValues = [42, 'dummy value', 3.14]
const result = migrateWidgetsValues(inputDefs, widgets, widgetValues)
expect(result).toEqual([42, 3.14])
})
it('should return original values if lengths do not match', () => {
const inputDefs: Record<string, InputSpec> = {
input1: {
type: 'INT',
name: 'input1',
forceInput: true
}
}
const widgets: IWidget[] = []
const widgetValues = [42, 'extra value']
const result = migrateWidgetsValues(inputDefs, widgets, widgetValues)
expect(result).toEqual(widgetValues)
})
it('should handle empty widgets and values', () => {
const inputDefs: Record<string, InputSpec> = {}
const widgets: IWidget[] = []
const widgetValues: any[] = []
const result = migrateWidgetsValues(inputDefs, widgets, widgetValues)
expect(result).toEqual([])
})
it('should preserve order of non-forceInput widget values', () => {
const inputDefs: Record<string, InputSpec> = {
first: {
type: 'INT',
name: 'first'
},
forced: {
type: 'STRING',
name: 'forced',
forceInput: true
},
last: {
type: 'FLOAT',
name: 'last'
}
}
const widgets: IWidget[] = [
{ name: 'first', type: 'number' },
{ name: 'last', type: 'number' }
] as unknown as IWidget[]
const widgetValues = ['first value', 'dummy', 'last value']
const result = migrateWidgetsValues(inputDefs, widgets, widgetValues)
expect(result).toEqual(['first value', 'last value'])
})
})
describe('compressWidgetInputSlots', () => {
it('should remove unconnected widget input slots', () => {
const graph: ISerialisedGraph = {
nodes: [
{
id: 1,
type: 'foo',
pos: [0, 0],
size: [100, 100],
flags: {},
order: 0,
mode: 0,
inputs: [
{ widget: { name: 'foo' }, link: null, type: 'INT', name: 'foo' },
{ widget: { name: 'bar' }, link: 2, type: 'INT', name: 'bar' },
{ widget: { name: 'baz' }, link: null, type: 'INT', name: 'baz' }
],
outputs: []
}
],
links: [[2, 1, 0, 1, 0, 'INT']]
} as unknown as ISerialisedGraph
compressWidgetInputSlots(graph)
expect(graph.nodes[0].inputs).toEqual([
{ widget: { name: 'bar' }, link: 2, type: 'INT', name: 'bar' }
])
})
it('should update link target slots correctly', () => {
const graph: ISerialisedGraph = {
nodes: [
{
id: 1,
type: 'foo',
pos: [0, 0],
size: [100, 100],
flags: {},
order: 0,
mode: 0,
inputs: [
{ widget: { name: 'foo' }, link: null, type: 'INT', name: 'foo' },
{ widget: { name: 'bar' }, link: 2, type: 'INT', name: 'bar' },
{ widget: { name: 'baz' }, link: 3, type: 'INT', name: 'baz' }
],
outputs: []
}
],
links: [
[2, 1, 0, 1, 1, 'INT'],
[3, 1, 0, 1, 2, 'INT']
]
} as unknown as ISerialisedGraph
compressWidgetInputSlots(graph)
expect(graph.nodes[0].inputs).toEqual([
{ widget: { name: 'bar' }, link: 2, type: 'INT', name: 'bar' },
{ widget: { name: 'baz' }, link: 3, type: 'INT', name: 'baz' }
])
expect(graph.links).toEqual([
[2, 1, 0, 1, 0, 'INT'],
[3, 1, 0, 1, 1, 'INT']
])
})
it('should handle graphs with no nodes gracefully', () => {
const graph: ISerialisedGraph = {
nodes: [],
links: []
} as unknown as ISerialisedGraph
compressWidgetInputSlots(graph)
expect(graph.nodes).toEqual([])
expect(graph.links).toEqual([])
})
})