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
6 changes: 1 addition & 5 deletions src/extensions/core/imageCompositor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ function makeNode() {
const savedValue = { layers: [] }
const compositorWidget = {
name: 'compositor',
value: savedValue,
callback: vi.fn()
value: savedValue
} as unknown as IBaseWidget
const priorOnExecuted = vi.fn()
const priorOnRemoved = vi.fn()
Expand All @@ -45,7 +44,6 @@ function makeNode() {
onRemoved: priorOnRemoved,
constructor: { comfyClass: 'ImageCompositor' },
widgets: [compositorWidget],
widgets_values: [savedValue],
graph: { setDirtyCanvas: vi.fn() }
} as unknown as LGraphNode
return { node, compositorWidget, priorOnExecuted, priorOnRemoved }
Expand Down Expand Up @@ -144,8 +142,6 @@ describe('ImageCompositor extension', () => {
})

expect(compositorWidget.value).toEqual({})
expect(compositorWidget.callback).toHaveBeenCalledWith({})
expect(node.widgets_values).toEqual([{}])
expect(node.graph?.setDirtyCanvas).toHaveBeenCalled()
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import { beforeEach, describe, expect, it, vi } from 'vitest'
import { defaultMode } from '@/renderer/extensions/layerEditor/engine/mode'
import type { RasterData } from '@/renderer/extensions/layerEditor/engine/node'
import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode'
import type { IBaseWidget } from '@/lib/litegraph/src/types/widgets'
import { useWidgetValueStore } from '@/stores/widgetValueStore'
import { toNodeId } from '@/types/nodeId'
import { widgetId } from '@/types/widgetId'
import { createMockLGraphNode } from '@/utils/__tests__/litegraphTestUtils'

import type { CompositorLayerState } from './compositorLayerState'
import {
saveCompositorLayerState,
saveCompositorPreview
Expand Down Expand Up @@ -48,25 +49,28 @@ function makeSession() {
}
}

function makeNode() {
const compositorWidget = {
name: 'compositor',
value: {},
callback: vi.fn()
} as unknown as IBaseWidget
const node = {
id: toNodeId(7),
widgets: [compositorWidget],
widgets_values: [{}]
} as unknown as LGraphNode
return { node, compositorWidget }
const GRAPH_ID = 'compositor-save-test'
const NODE_ID = toNodeId(7)

function makeNode(): { node: LGraphNode } {
useWidgetValueStore().registerWidget(
widgetId(GRAPH_ID, NODE_ID, 'compositor'),
{ type: 'compositor', value: {}, options: {} }
)
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
const node = createMockLGraphNode({
id: NODE_ID,
graph: { rootGraph: { id: GRAPH_ID } }
})
return { node }
}

function widgetValue(widget: IBaseWidget): CompositorLayerState {
return widget.value as CompositorLayerState
function storedValue(): unknown {
return useWidgetValueStore().getWidget(
widgetId(GRAPH_ID, NODE_ID, 'compositor')
)?.value
}

const cacheNode = { id: toNodeId(7) } as unknown as LGraphNode
const cacheNode = createMockLGraphNode({ id: NODE_ID })

beforeEach(() => {
clearCompositorLayers(cacheNode)
Expand All @@ -84,22 +88,21 @@ describe('saveCompositorLayerState', () => {
it('writes the layer state recipe to the compositor widget', () => {
cacheFingerprint()
const session = makeSession()
const { node, compositorWidget } = makeNode()
const { node } = makeNode()

expect(saveCompositorLayerState(session, node)).toBe(true)

const savedState = widgetValue(compositorWidget)
expect(compositorWidget.callback).toHaveBeenCalledWith(savedState)
expect(node.widgets_values?.[0]).toBe(compositorWidget.value)

expect(savedState.canvas).toEqual({ w: 64, h: 48 })
expect(savedState.layers).toHaveLength(1)
expect(savedState.layers[0]).toMatchObject({
name: 'Background',
opacity: 0.5,
blend: 'multiply',
flipH: true,
flipV: false
expect(storedValue()).toMatchObject({
canvas: { w: 64, h: 48 },
layers: [
{
name: 'Background',
opacity: 0.5,
blend: 'multiply',
flipH: true,
flipV: false
}
]
})
})

Expand All @@ -109,20 +112,20 @@ describe('saveCompositorLayerState', () => {
[{ filename: 'a.png', subfolder: '', type: 'temp' }],
['hash-a', 'hash-b']
)
const { node, compositorWidget } = makeNode()
const { node } = makeNode()

saveCompositorLayerState(makeSession(), node)

expect(widgetValue(compositorWidget).inputs).toEqual(['hash-a', 'hash-b'])
expect(storedValue()).toMatchObject({ inputs: ['hash-a', 'hash-b'] })
})

it('refuses to save when no fingerprint is cached', () => {
const { node, compositorWidget } = makeNode()
const { node } = makeNode()

expect(saveCompositorLayerState(makeSession(), node)).toBe(false)

expect(compositorWidget.callback).not.toHaveBeenCalled()
expect(node.widgets_values).toEqual([{}])
expect(useWidgetValueStore().setValue).not.toHaveBeenCalled()
expect(storedValue()).toEqual({})
})

it('keeps widgets untouched when extraction fails', () => {
Expand All @@ -131,13 +134,12 @@ describe('saveCompositorLayerState', () => {
session.layerFlips = () => {
throw new Error('boom')
}
const { node, compositorWidget } = makeNode()
const { node } = makeNode()

expect(saveCompositorLayerState(session, node)).toBe(false)

expect(compositorWidget.callback).not.toHaveBeenCalled()
expect(compositorWidget.value).toEqual({})
expect(node.widgets_values).toEqual([{}])
expect(useWidgetValueStore().setValue).not.toHaveBeenCalled()
expect(storedValue()).toEqual({})
})
})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,81 +1,105 @@
import { describe, expect, it, vi } from 'vitest'
import { fromPartial } from '@total-typescript/shoehorn'
import { describe, expect, it } from 'vitest'

import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode'
import type { IBaseWidget } from '@/lib/litegraph/src/types/widgets'
import type { ICompositorWidget } from '@/lib/litegraph/src/types/widgets'
import { useWidgetValueStore } from '@/stores/widgetValueStore'
import { toNodeId } from '@/types/nodeId'
import { widgetId } from '@/types/widgetId'
import { createMockLGraphNode } from '@/utils/__tests__/litegraphTestUtils'

import {
getCompositorWidgetValue,
resetCompositorStateWidgets,
setCompositorWidgetValue
} from './compositorWidgets'

function makeNode() {
const compositorWidget = {
name: 'compositor',
value: { layers: [] },
callback: vi.fn()
} as unknown as IBaseWidget
const otherWidget = {
name: 'other',
value: 42,
callback: vi.fn()
} as unknown as IBaseWidget
const node = {
widgets: [otherWidget, compositorWidget],
widgets_values: [42, { layers: [] }]
} as unknown as LGraphNode
return { node, compositorWidget, otherWidget }
const GRAPH_ID = 'compositor-test-graph'

let nodeCounter = 0

function makeNode({ registered = true } = {}): LGraphNode {
nodeCounter += 1
const nodeId = toNodeId(nodeCounter)
if (registered) {
useWidgetValueStore().registerWidget(
widgetId(GRAPH_ID, nodeId, 'compositor'),
{ type: 'compositor', value: { layers: [] }, options: {} }
)
}
return createMockLGraphNode({
id: nodeId,
graph: { rootGraph: { id: GRAPH_ID } }
})
}

function storedValue(node: LGraphNode): unknown {
return useWidgetValueStore().getWidget(
widgetId(GRAPH_ID, node.id, 'compositor')
)?.value
}

describe('setCompositorWidgetValue', () => {
it('writes the widget value, fires its callback, and syncs widgets_values', () => {
const { node, compositorWidget } = makeNode()
it('writes the widget value to the store', () => {
const node = makeNode()
const next = { canvas: { w: 8, h: 8 }, layers: [] }

setCompositorWidgetValue(node, next)

expect(compositorWidget.value).toEqual(next)
expect(compositorWidget.callback).toHaveBeenCalledWith(next)
expect(node.widgets_values?.[1]).toEqual(next)
expect(storedValue(node)).toEqual(next)
})

it('is a no-op when the compositor widget is missing', () => {
const { node, otherWidget } = makeNode()
node.widgets = [otherWidget]
node.widgets_values = [42]
it('falls back to the widget object when the store has no entry', () => {
const node = makeNode({ registered: false })
const widget = fromPartial<ICompositorWidget>({
name: 'compositor',
type: 'compositor',
value: { layers: [] }
})
node.widgets = [widget]
const next = { canvas: { w: 4, h: 4 }, layers: [] }

setCompositorWidgetValue(node, next)

expect(widget.value).toEqual(next)
expect(getCompositorWidgetValue(node)).toEqual(next)
})

it('is a no-op when the widget exists nowhere', () => {
const node = makeNode({ registered: false })

setCompositorWidgetValue(node, { layers: [] })

expect(otherWidget.value).toBe(42)
expect(node.widgets_values).toEqual([42])
expect(storedValue(node)).toBeUndefined()
})
})

describe('getCompositorWidgetValue', () => {
it('returns the widget value when it has the expected shape', () => {
const { node } = makeNode()
const node = makeNode()

expect(getCompositorWidgetValue(node)).toEqual({ layers: [] })
})

it('returns null for missing widgets or malformed values', () => {
const { node, compositorWidget } = makeNode()
compositorWidget.value = 'legacy-string'
const node = makeNode()
useWidgetValueStore().setValue(
widgetId(GRAPH_ID, node.id, 'compositor'),
'legacy-string'
)
expect(getCompositorWidgetValue(node)).toBeNull()

node.widgets = []
expect(getCompositorWidgetValue(node)).toBeNull()
expect(getCompositorWidgetValue(makeNode({ registered: false }))).toBeNull()
})
})

describe('resetCompositorStateWidgets', () => {
it('resets the compositor widget to the empty value durably', () => {
const { node, compositorWidget } = makeNode()
const node = makeNode()
setCompositorWidgetValue(node, { canvas: { w: 8, h: 8 }, layers: [] })

resetCompositorStateWidgets(node)

expect(compositorWidget.value).toEqual({})
expect(compositorWidget.callback).toHaveBeenCalledWith({})
expect(node.widgets_values).toEqual([42, {}])
expect(storedValue(node)).toEqual({})
})
})
Original file line number Diff line number Diff line change
@@ -1,29 +1,38 @@
import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode'
import {
emptyCompositorWidgetValue,
isCompositorWidgetValue
} from '@/renderer/extensions/compositor/components/types'
import type { CompositorWidgetValue } from '@/renderer/extensions/compositor/components/types'
import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode'
import { useWidgetValueStore } from '@/stores/widgetValueStore'
import type { WidgetId } from '@/types/widgetId'
import { widgetId } from '@/types/widgetId'

const COMPOSITOR_WIDGET = 'compositor'

function compositorWidgetId(node: LGraphNode): WidgetId | null {
const graphId = node.graph?.rootGraph?.id
return graphId ? widgetId(graphId, node.id, COMPOSITOR_WIDGET) : null
}

export function setCompositorWidgetValue(
node: LGraphNode,
value: CompositorWidgetValue
): void {
const widget = node.widgets?.find((w) => w.name === 'compositor')
if (!widget) return
widget.value = value
widget.callback?.(value)
if (node.widgets_values && node.widgets) {
const index = node.widgets.indexOf(widget)
if (index >= 0) node.widgets_values[index] = value
}
const id = compositorWidgetId(node)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably make a more universal way to do this so nodes don't have to write this each time.

if (id && useWidgetValueStore().setValue(id, value)) return

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blocker: This early return, together with the fallback below, removes the prior widget.callback invocation and node.widgets_values synchronization. The existing src/extensions/core/imageCompositor.test.ts suite now fails at line 147 with zero callback calls; its line 148 shadow assertion would remain stale too. Current ECS compatibility docs still describe live callbacks and widget-value shadows as compatibility contracts. Please either preserve these effects or deliberately migrate the remaining test and compatibility documentation, including the required extension-impact guidance. Serialization rebuilding values later does not preserve pre-serialization callback or shadow behavior.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-evaluated against the production widget and CI failure: the compositor constructor intentionally supplies a null callback, and serialization rebuilds widget-value shadows from live values. Commit 6d5982e33 removes the stale callback/shadow expectations while retaining the reset-value and dirty-canvas assertions.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jtydhr88 Want to double check the expectations here?

const widget = node.widgets?.find((w) => w.name === COMPOSITOR_WIDGET)
if (widget) widget.value = value
}

export function getCompositorWidgetValue(
node: LGraphNode
): CompositorWidgetValue | null {
const widget = node.widgets?.find((w) => w.name === 'compositor')
return isCompositorWidgetValue(widget?.value) ? widget.value : null
const id = compositorWidgetId(node)
const value =
(id ? useWidgetValueStore().getWidget(id)?.value : undefined) ??
node.widgets?.find((w) => w.name === COMPOSITOR_WIDGET)?.value
Comment on lines +32 to +34

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const value =
(id ? useWidgetValueStore().getWidget(id)?.value : undefined) ??
node.widgets?.find((w) => w.name === COMPOSITOR_WIDGET)?.value
const widget = id ? useWidgetValueStore().getWidget(id) ? node.widgets?.find((w) => w.name === COMPOSITOR_WIDGET)
const value = widget?.value

return isCompositorWidgetValue(value) ? value : null
}

export function resetCompositorStateWidgets(node: LGraphNode): void {
Expand Down
Loading
Loading