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
68 changes: 68 additions & 0 deletions browser_tests/fixtures/helpers/SubgraphHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ import { SubgraphSlotReference } from '@e2e/fixtures/utils/litegraphUtils'
import { getAllHostPromotedWidgets } from '@e2e/fixtures/utils/promotedWidgets'
import type { PromotedWidgetEntry } from '@e2e/fixtures/utils/promotedWidgets'

interface SubgraphDefinitionRow {
name: string
nodes: number
links: number
}

type SubgraphDefinitionInventory = SubgraphDefinitionRow[]

export class SubgraphHelper {
public readonly editor: SubgraphEditor

Expand Down Expand Up @@ -586,6 +594,66 @@ export class SubgraphHelper {
return id
}

/** Descends `levels` times, requiring exactly one subgraph node at each level. */
async descendSubgraphs(levels: number): Promise<void> {
for (let level = 0; level < levels; level++) {
await expect.poll(() => this.countSubgraphNodes()).toBe(1)
const parentGraphId = await this.getActiveGraphId()
await this.enterSubgraphWithFallback(await this.findSubgraphNodeId())
await expect.poll(() => this.getActiveGraphId()).not.toBe(parentGraphId)
}
}

/** Number of subgraph host nodes in the graph currently shown on the canvas. */
async countSubgraphNodes(): Promise<number> {
return this.page.evaluate(
() =>
window.app!.canvas.graph!.nodes.filter((node) => node.isSubgraphNode())
.length
)
}

/**
* Interior node and link counts of every subgraph definition, one row per
* definition sorted by name, then node count, then link count. Definitions
* sharing a name stay as separate rows, because `Convert to Subgraph` names
* every new definition 'New Subgraph'.
*/
async getDefinitionInventory(): Promise<SubgraphDefinitionInventory> {
return this.page.evaluate(() =>
[...window.app!.rootGraph.subgraphs.values()]
.map((definition) => ({
name: definition.name,
nodes: definition.nodes.length,
links: definition.links.size
}))
.sort(
(a, b) =>
a.name.localeCompare(b.name) ||
a.nodes - b.nodes ||
a.links - b.links
)
)
}

/** Same inventory, read back from a serialized workflow rather than live state. */
async getSerializedDefinitionInventory(): Promise<SubgraphDefinitionInventory> {
return this.page.evaluate(() =>
(window.app!.rootGraph.asSerialisable().definitions?.subgraphs ?? [])
.map((definition) => ({
name: definition.name,
nodes: definition.nodes?.length ?? 0,
links: definition.links?.length ?? 0
}))
.sort(
(a, b) =>
a.name.localeCompare(b.name) ||
a.nodes - b.nodes ||
a.links - b.links
)
)
}

async getBoundaryLinkSnapshot() {
return this.page.evaluate(() => {
const graph = window.app!.graph
Expand Down
101 changes: 101 additions & 0 deletions browser_tests/tests/subgraph/subgraphWrapNestedDefinition.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { expect } from '@playwright/test'

import { comfyPageFixture as test } from '@e2e/fixtures/ComfyPage'

test.use({ initialSettings: { 'Comfy.UseNewMenu': 'Disabled' } })

test.describe(
'Wrapping a subgraph host that owns nested definitions',
{ tag: ['@subgraph', '@slow'] },
() => {
const WRAPPED_SELECTION = ['subgraph 2', 'VAE Decode', 'Save Image']

const NESTED_DEFINITIONS_INTACT = [
{ name: 'subgraph 2', nodes: 3, links: 7 },
{ name: 'subgraph 3', nodes: 3, links: 6 }
]

// The wrapper's 4th link is the boundary link feeding Save Image's
// filename_prefix widget, which conversion promotes to a wrapper input.
const NESTED_DEFINITIONS_AFTER_WRAP = [
{ name: 'New Subgraph', nodes: 3, links: 4 },
...NESTED_DEFINITIONS_INTACT
]

test.beforeEach(async ({ comfyPage }) => {
await comfyPage.workflow.loadWorkflow('subgraphs/nested-subgraph')
})

test('keeps the wrapped and nested definitions populated', async ({
comfyPage
}) => {
await expect
.poll(() => comfyPage.subgraph.getDefinitionInventory())
.toEqual(NESTED_DEFINITIONS_INTACT)

await test.step('Wrap the subgraph host together with its siblings', async () => {
await comfyPage.nodeOps.selectNodes(WRAPPED_SELECTION)
expect(
[...(await comfyPage.nodeOps.getSelectedNodeIds())].sort(),
'all three nodes must be selected, or the conversion under test is not the one being asserted'
).toEqual(['10', '8', '9'])

const host = await comfyPage.nodeOps.getNodeRefById('10')
await host.convertToSubgraph()
})

await expect
.poll(() => comfyPage.subgraph.getDefinitionInventory())
.toEqual(NESTED_DEFINITIONS_AFTER_WRAP)
})

test('keeps the nested definitions populated in the saved workflow', async ({
comfyPage
}) => {
await test.step('Wrap the subgraph host together with its siblings', async () => {
await comfyPage.nodeOps.selectNodes(WRAPPED_SELECTION)
const host = await comfyPage.nodeOps.getNodeRefById('10')
await host.convertToSubgraph()
})

await comfyPage.subgraph.serializeAndReload()

await expect
.poll(() => comfyPage.subgraph.getSerializedDefinitionInventory())
.toEqual(NESTED_DEFINITIONS_AFTER_WRAP)
})

test(
'still renders the nested interior after wrapping',
{ tag: ['@vue-nodes'] },
async ({ comfyPage }) => {
await test.step('Wrap the subgraph host together with its siblings', async () => {
await comfyPage.nodeOps.selectNodes(WRAPPED_SELECTION)
const host = await comfyPage.nodeOps.getNodeRefById('10')
await host.convertToSubgraph()
})

await test.step('Descend through the wrapper into the deepest nested subgraph', async () => {
await comfyPage.subgraph.descendSubgraphs(3)
})

await test.step('Assert the deepest interior still renders its three nodes', async () => {
await expect(comfyPage.vueNodes.nodes).toHaveCount(3)
await expect(
comfyPage.vueNodes.getNodeByTitle('Load Checkpoint')
).toBeVisible()
await expect(
comfyPage.vueNodes.getNodeByTitle('Empty Latent Image')
).toBeVisible()
await expect(
comfyPage.vueNodes.getNodeByTitle('CLIP Text Encode (Prompt)')
).toBeVisible()
await comfyPage.attachScreenshot(
'nested-subgraph-interior-after-wrap',
{ runInCI: true }
)
})
}
)
}
)
16 changes: 13 additions & 3 deletions src/lib/litegraph/src/LGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ import {
import type { UnpackedTargetInput } from './subgraph/unpackSubgraph'
import {
findUnresolvableSubgraphLink,
findOrphanedSubgraphs,
findReleasableSubgraphs,
findUsedSubgraphIds,
getBoundaryLinks,
Expand Down Expand Up @@ -234,6 +235,8 @@ export interface GraphRemoveOptions {
* Same-id replacement state is left intact.
*/
preserveCanonicalState?: boolean
/** Keep the subgraph definitions the node references; the caller re-creates the node elsewhere in the same root graph. */
preserveSubgraphDefinitions?: boolean
}

export interface LGraphExtra extends Dictionary<unknown> {
Expand Down Expand Up @@ -1495,7 +1498,7 @@ export class LGraph
}
}

if (node.isSubgraphNode()) {
if (node.isSubgraphNode() && !options.preserveSubgraphDefinitions) {
this.releaseSubgraphs(findReleasableSubgraphs(this.rootGraph, node))
}

Expand Down Expand Up @@ -2216,11 +2219,18 @@ export class LGraph
resolved.inputNode
)

for (const node of nodes) this.remove(node)
for (const node of nodes)
this.remove(node, { preserveSubgraphDefinitions: true })
for (const reroute of reroutes) this.removeReroute(reroute.id)
for (const group of groups) this.remove(group)

const subgraph = this.createSubgraph(data)
let subgraph: Subgraph
try {
subgraph = this.createSubgraph(data)
} catch (error) {
this.releaseSubgraphs(findOrphanedSubgraphs(this.rootGraph, nodes))
throw error
}
for (const node of subgraph.nodes) node.onGraphConfigured?.()
for (const node of subgraph.nodes) node.onAfterGraphConfigured?.()

Expand Down
Loading
Loading