Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { applyOps } from '@comfyorg/comfy-multi-player'
import { applyOps, nodesMap } from '@comfyorg/comfy-multi-player'
import type { Op, WidgetCatalog } from '@comfyorg/comfy-multi-player'
import { fromPartial } from '@total-typescript/shoehorn'
import {
Expand All @@ -11,6 +11,7 @@ import {
vi
} from 'vitest'
import { markRaw, ref } from 'vue'
import * as Y from 'yjs'

import { LGraph, LGraphNode, LiteGraph } from '@/lib/litegraph/src/litegraph'
import type { LGraphCanvas } from '@/lib/litegraph/src/litegraph'
Expand All @@ -26,7 +27,8 @@ import { useLitegraphService } from '@/services/litegraphService'

import { followedDoc } from './__fixtures__/followedDoc'
import { LiveGraphApplier } from './liveGraphApplier'
import { attachDocOpMinter } from './docOpMinter'
import { attachDocOpMinter, wireNodeSnapshot } from './docOpMinter'
import { mintWireOps } from './opEnvelope'

const appState = vi.hoisted(() => ({
rootGraph: undefined as LGraph | undefined
Expand Down Expand Up @@ -88,6 +90,31 @@ class DynamicGroupTest extends LGraphNode {
}
}

class DynamicGroupOverflowTest extends LGraphNode {
constructor() {
super('DynamicGroupOverflowTest')
this.comfyClass = 'DynamicGroupOverflowTest'
this.serialize_widgets = true
this.addWidget('text', 'before', 'head', () => {})
useLitegraphService().addNodeInput(this, {
name: 'rows',
type: 'COMFY_DYNAMICGROUP_V3',
isOptional: false,
min: 0,
max: 3,
template: { required: { strength: ['FLOAT', { default: 1 }] } }
})
}
}

class DynamicGroupOverflowTrailingTest extends DynamicGroupOverflowTest {
constructor() {
super()
this.comfyClass = 'DynamicGroupOverflowTrailingTest'
this.addWidget('text', 'after', 'tail', () => {})
}
}

const CATALOG: WidgetCatalog = {
types: {
TestSource: { widget_order: ['steps'] },
Expand Down Expand Up @@ -121,6 +148,52 @@ function op(body: OpBody): Op {
return { ...envelope, ...body }
}

function restoredAboveMaxDoc() {
const local = new LGraph()
const node = LiteGraph.createNode('DynamicGroupTest')
assert.exists(node)
local.add(node)
node.configure({
...node.serialize(),
widgets_values: ['head', 4, 0.1, 0.2, 0.3, 0.4, 'tail']
})
const snapshot = wireNodeSnapshot(node)
assert.exists(snapshot)
const catalog: WidgetCatalog = {
types: {
DynamicGroupTest: {
widget_order: [
'before',
'rows',
'rows.0.strength',
'rows.1.strength',
'rows.2.strength',
'rows.3.strength',
'after'
]
}
}
}
const { doc, collector } = followedDoc({ nodes: [], links: [] }, catalog)
collector.take()
const add = mintWireOps(
[
{
op: 'add_node',
node_id: node.id,
class_type: snapshot.type,
pos: [0, 0],
node: snapshot
}
],
{ actor: 'human:user:tab', baseVersion: 1 }
)
expect(
applyOps(doc, add, catalog).outcomes.map(({ outcome }) => outcome)
).toEqual(['applied'])
return { doc, collector, node }
}

async function workflowJson(graph: LGraph): Promise<ComfyWorkflowJSON> {
const state = await validateComfyWorkflow(graph.serialize(), (error) => {
throw new Error(error)
Expand All @@ -133,6 +206,26 @@ beforeEach(() => {
LiteGraph.registerNodeType('TestSource', TestSource)
LiteGraph.registerNodeType('TestSink', TestSink)
LiteGraph.registerNodeType('DynamicGroupTest', DynamicGroupTest)
LiteGraph.registerNodeType(
'DynamicGroupOverflowTest',
DynamicGroupOverflowTest
)
LiteGraph.registerNodeType(
'DynamicGroupOverflowTrailingTest',
DynamicGroupOverflowTrailingTest
)
DynamicGroupOverflowTest.nodeData = fromPartial<
NonNullable<LGraphNode['constructor']['nodeData']>
>({
inputs: {
mode: {
type: 'COMFY_DYNAMICCOMBO_V3',
name: 'mode',
isOptional: false,
options: []
}
}
})
ChangeTracker.isLoadingGraph = false
})

Expand Down Expand Up @@ -319,6 +412,181 @@ describe('LiveGraphApplier with the real change tracker', () => {
)
})

it('creates a restored DynamicGroup holding more rows than its max on a follower', () => {
const { doc, collector, node } = restoredAboveMaxDoc()
const follower = new LGraph()
const applier = new LiveGraphApplier({ getGraph: () => follower })

const result = applier.applyChanges(doc, collector.take(), CONTEXT)

expect(result.failureCount).toBe(0)
expect(follower.getNodeById(node.id)?.serialize().widgets_values).toEqual([
'head',
4,
0.1,
0.2,
0.3,
0.4,
'tail'
])
})

it('creates a restored above-max DynamicGroup next to a stray positional key', () => {
const { doc, collector, node } = restoredAboveMaxDoc()
doc.transact(() => {
const widgets = nodesMap(doc).get(String(node.id))?.get('widgets')
assert.instanceOf(widgets, Y.Map)
widgets.set('_extra_9', 1)
})
const follower = new LGraph()
const applier = new LiveGraphApplier({ getGraph: () => follower })

const result = applier.applyChanges(doc, collector.take(), CONTEXT)

expect(result.failureCount).toBe(1)
expect(follower.getNodeById(node.id)?.serialize().widgets_values).toEqual([
'head',
4,
0.1,
0.2,
0.3,
0.4,
'tail'
])
})

it('creates a DynamicGroup whose row count the document stores under a positional alias', () => {
const { doc, collector } = followedDoc(
{
nodes: [
{
id: 1,
type: 'DynamicGroupOverflowTest',
pos: [0, 0],
size: [210, 100],
widgets_values: ['head', 2, 0.1, 0.2]
}
],
links: []
},
{ types: { DynamicGroupOverflowTest: { widget_order: ['before'] } } }
)
const follower = new LGraph()
const applier = new LiveGraphApplier({ getGraph: () => follower })

const result = applier.applyChanges(doc, collector.take(), CONTEXT)

expect(result.failureCount).toBe(0)
expect(
follower.getNodeById(toNodeId(1))?.serialize().widgets_values
).toEqual(['head', 2, 0.1, 0.2])
})

it.for([
{ label: 'only the leading widget', widgetOrder: ['before'] },
{ label: 'the controller', widgetOrder: ['before', 'rows'] },
{ label: 'the trailing widget', widgetOrder: ['before', 'rows', 'after'] }
])(
'restores aliased row values and the trailing widget on create when the catalog names $label',
({ widgetOrder }) => {
const { doc, collector } = followedDoc(
{
nodes: [
{
id: 1,
type: 'DynamicGroupOverflowTrailingTest',
pos: [0, 0],
size: [210, 100],
widgets_values: ['head', 2, 0.1, 0.2, 'tail']
}
],
links: []
},
{
types: {
DynamicGroupOverflowTrailingTest: { widget_order: widgetOrder }
}
}
)
const follower = new LGraph()
const applier = new LiveGraphApplier({ getGraph: () => follower })

const result = applier.applyChanges(doc, collector.take(), CONTEXT)

expect(result.failureCount).toBe(0)
expect(
follower.getNodeById(toNodeId(1))?.serialize().widgets_values
).toEqual(['head', 2, 0.1, 0.2, 'tail'])
}
)

it('still fails to create a restored above-max DynamicGroup on a node with a DynamicCombo (#21297)', () => {
const { doc, collector } = followedDoc(
{
nodes: [
{
id: 1,
type: 'DynamicGroupOverflowTest',
pos: [0, 0],
size: [210, 100],
widgets_values: ['head', 4, 0.1, 0.2, 0.3, 0.4]
}
],
links: []
},
{
types: {
DynamicGroupOverflowTest: {
widget_order: [
'before',
'rows',
'rows.0.strength',
'rows.1.strength',
'rows.2.strength',
'rows.3.strength'
]
}
}
}
)
const follower = new LGraph()
const applier = new LiveGraphApplier({ getGraph: () => follower })

const result = applier.applyChanges(doc, collector.take(), CONTEXT)

expect(result.failureCount).toBe(1)
expect(
follower.getNodeById(toNodeId(1))?.serialize().widgets_values
).toEqual(['head', 0])
})

it('refuses a crafted DynamicGroup row count without row values on create', () => {
const { doc, collector } = followedDoc(
{
nodes: [
{
id: 1,
type: 'DynamicGroupTest',
pos: [0, 0],
size: [210, 100],
widgets_values: ['head', 1e9, 'tail']
}
],
links: []
},
CATALOG
)
const follower = new LGraph()
const applier = new LiveGraphApplier({ getGraph: () => follower })

const result = applier.applyChanges(doc, collector.take(), CONTEXT)

expect(result.failureCount).toBe(1)
expect(
follower.getNodeById(toNodeId(1))?.serialize().widgets_values
).toEqual(['head', 0, 'tail'])
})

it('builds a DynamicGroup node from the catch-up frame without outbound echo and reloads to the same graph', async () => {
const graph = new LGraph()
appState.rootGraph = graph
Expand Down
Loading
Loading