mirror of
https://github.com/willmiao/ComfyUI-Lora-Manager.git
synced 2026-08-14 09:43:22 -03:00
feat(workflow): exclude text-capable nodes with connected text from send targets
CLIP Text Encode and friends whose text widget is backed by a connected input cannot have their text changed via the widget (execution reads the linked input), so sending to them was a silent no-op. - Registry: compute text_widget_connected capability from the widget's backing input link state; has_text_widget drops to false when wired; include the flag in the registration fingerprint so link changes re-register the affected nodes - Registry: hook link connect/disconnect (graph events on new litegraph, onAfterChange fallback for classic) on root and subgraphs, plus subgraph-created for future subgraphs - applyWidgetUpdate: skip inject_text when the target widget is connected and self-heal the registry instead of writing a value that is ignored - Web UI: drop text_widget_connected nodes from prompt/embedding send candidates; show a Mark as -> Send Prompt Target hint toast when no candidates remain (new uiHelpers.workflow.noPromptTargets key, synced to all locales; zh-CN/zh-TW translated) - Extract shared resolveTextWidget() used by both the candidate-set logic and the write path so the two cannot drift apart - Tests: workflow registry connection-state registration, subgraph handling, fingerprint re-registration, inject_text write/skip paths, setup link-change hooks; uiHelpers candidate filtering and hint toast
This commit is contained in:
@@ -0,0 +1,246 @@
|
||||
import { beforeEach, afterEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const { APP_MODULE, API_MODULE, STYLES_MODULE, REGISTRY_MODULE, appMock, apiMock, registeredExtensions } =
|
||||
vi.hoisted(() => {
|
||||
const registeredExtensions = [];
|
||||
const appMock = {
|
||||
graph: null,
|
||||
registerExtension: (ext) => registeredExtensions.push(ext),
|
||||
};
|
||||
const apiMock = {
|
||||
clientId: "client-1",
|
||||
initialClientId: null,
|
||||
addEventListener: vi.fn(),
|
||||
};
|
||||
return {
|
||||
APP_MODULE: new URL("../../../scripts/app.js", import.meta.url).pathname,
|
||||
API_MODULE: new URL("../../../scripts/api.js", import.meta.url).pathname,
|
||||
STYLES_MODULE: new URL("../../../web/comfyui/lm_styles_loader.js", import.meta.url).pathname,
|
||||
REGISTRY_MODULE: new URL("../../../web/comfyui/workflow_registry.js", import.meta.url).pathname,
|
||||
appMock,
|
||||
apiMock,
|
||||
registeredExtensions,
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock(APP_MODULE, () => ({ app: appMock }));
|
||||
vi.mock(API_MODULE, () => ({ api: apiMock }));
|
||||
vi.mock(STYLES_MODULE, () => ({ ensureLmStyles: vi.fn() }));
|
||||
|
||||
function createTextEncodeNode({ linked = false, id = 1 } = {}) {
|
||||
const textWidget = { name: "text", type: "customtext", value: "old prompt", callback: null };
|
||||
return {
|
||||
id,
|
||||
comfyClass: "CLIPTextEncode",
|
||||
title: "CLIP Text Encode",
|
||||
mode: 0,
|
||||
properties: {},
|
||||
widgets: [textWidget, { name: "clip", type: "combo" }],
|
||||
widgets_values: ["old prompt", "clip-1"],
|
||||
inputs: [
|
||||
{ name: "text", type: "STRING", widget: textWidget, link: linked ? 101 : null },
|
||||
{ name: "clip", type: "CLIP", link: null },
|
||||
],
|
||||
setDirtyCanvas: vi.fn(),
|
||||
graph: null,
|
||||
};
|
||||
}
|
||||
|
||||
function createSubgraph({ id = "sub-1", nodes = [] } = {}) {
|
||||
const graph = {
|
||||
id,
|
||||
_nodes: nodes,
|
||||
_subgraphs: new Map(),
|
||||
getNodeById: vi.fn((nodeId) => nodes.find((n) => n.id === nodeId) ?? null),
|
||||
events: { addEventListener: vi.fn() },
|
||||
};
|
||||
for (const node of nodes) {
|
||||
node.graph = graph;
|
||||
}
|
||||
return graph;
|
||||
}
|
||||
|
||||
function createGraph({ nodes = [], subgraphs = [] } = {}) {
|
||||
const graph = {
|
||||
id: "root",
|
||||
_nodes: nodes,
|
||||
_subgraphs: new Map(),
|
||||
getNodeById: vi.fn((nodeId) => nodes.find((n) => n.id === nodeId) ?? null),
|
||||
events: { addEventListener: vi.fn() },
|
||||
};
|
||||
for (const subgraph of subgraphs) {
|
||||
graph._subgraphs.set(subgraph.id, subgraph);
|
||||
}
|
||||
for (const node of nodes) {
|
||||
node.graph = graph;
|
||||
}
|
||||
return graph;
|
||||
}
|
||||
|
||||
function lastRegisterPayload(fetchMock) {
|
||||
const calls = fetchMock.mock.calls.filter(
|
||||
([url]) => url === "/api/lm/register-nodes"
|
||||
);
|
||||
expect(calls.length).toBeGreaterThan(0);
|
||||
return JSON.parse(calls[calls.length - 1][1].body);
|
||||
}
|
||||
|
||||
describe("LoraManager.WorkflowRegistry", () => {
|
||||
let extension;
|
||||
let fetchMock;
|
||||
|
||||
beforeEach(async () => {
|
||||
vi.resetModules();
|
||||
registeredExtensions.length = 0;
|
||||
appMock.graph = null;
|
||||
apiMock.addEventListener.mockClear();
|
||||
fetchMock = vi.fn().mockResolvedValue({ ok: true });
|
||||
global.fetch = fetchMock;
|
||||
await import(REGISTRY_MODULE);
|
||||
extension = registeredExtensions.find(
|
||||
(ext) => ext.name === "LoraManager.WorkflowRegistry"
|
||||
);
|
||||
expect(extension).toBeDefined();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
delete global.fetch;
|
||||
});
|
||||
|
||||
describe("refreshRegistry", () => {
|
||||
it("registers an unconnected CLIPTextEncode as a text target", async () => {
|
||||
appMock.graph = createGraph({ nodes: [createTextEncodeNode()] });
|
||||
|
||||
await extension.refreshRegistry(true);
|
||||
|
||||
const body = lastRegisterPayload(fetchMock);
|
||||
expect(body.nodes).toHaveLength(1);
|
||||
expect(body.nodes[0].capabilities.has_text_widget).toBe(true);
|
||||
expect(body.nodes[0].capabilities.text_widget_connected).toBe(false);
|
||||
});
|
||||
|
||||
it("excludes a CLIPTextEncode whose text input is connected", async () => {
|
||||
appMock.graph = createGraph({ nodes: [createTextEncodeNode({ linked: true })] });
|
||||
|
||||
await extension.refreshRegistry(true);
|
||||
|
||||
const body = lastRegisterPayload(fetchMock);
|
||||
expect(body.nodes).toHaveLength(1);
|
||||
expect(body.nodes[0].capabilities.has_text_widget).toBe(false);
|
||||
expect(body.nodes[0].capabilities.text_widget_connected).toBe(true);
|
||||
});
|
||||
|
||||
it("registers connection state for nodes inside subgraphs", async () => {
|
||||
const inner = createTextEncodeNode({ linked: true, id: 7 });
|
||||
const subgraph = createSubgraph({ id: "sub-1", nodes: [inner] });
|
||||
appMock.graph = createGraph({ subgraphs: [subgraph] });
|
||||
|
||||
await extension.refreshRegistry(true);
|
||||
|
||||
const body = lastRegisterPayload(fetchMock);
|
||||
expect(body.nodes).toHaveLength(1);
|
||||
expect(body.nodes[0].graph_id).toBe("sub-1");
|
||||
expect(body.nodes[0].node_id).toBe(7);
|
||||
expect(body.nodes[0].capabilities.text_widget_connected).toBe(true);
|
||||
});
|
||||
|
||||
it("re-registers when text_widget_connected changes (fingerprint)", async () => {
|
||||
const node = createTextEncodeNode();
|
||||
appMock.graph = createGraph({ nodes: [node] });
|
||||
|
||||
await extension.refreshRegistry(true);
|
||||
await extension.refreshRegistry();
|
||||
expect(
|
||||
fetchMock.mock.calls.filter(([url]) => url === "/api/lm/register-nodes")
|
||||
).toHaveLength(1);
|
||||
|
||||
node.inputs[0].link = 101;
|
||||
await extension.refreshRegistry();
|
||||
const body = lastRegisterPayload(fetchMock);
|
||||
expect(body.nodes[0].capabilities.text_widget_connected).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("applyWidgetUpdate (inject_text)", () => {
|
||||
it("updates the widget value when the text input is not connected", async () => {
|
||||
const node = createTextEncodeNode();
|
||||
const callback = vi.fn();
|
||||
node.widgets[0].callback = callback;
|
||||
appMock.graph = createGraph({ nodes: [node] });
|
||||
extension.flashWidget = vi.fn();
|
||||
|
||||
await extension.applyWidgetUpdate({
|
||||
node_id: 1,
|
||||
action: "inject_text",
|
||||
value: "hello",
|
||||
mode: "replace",
|
||||
});
|
||||
|
||||
expect(node.widgets[0].value).toBe("hello");
|
||||
expect(node.widgets_values[0]).toBe("hello");
|
||||
expect(callback).toHaveBeenCalledWith("hello");
|
||||
});
|
||||
|
||||
it("skips inject_text when the target widget is connected and self-heals the registry", async () => {
|
||||
const node = createTextEncodeNode({ linked: true });
|
||||
appMock.graph = createGraph({ nodes: [node] });
|
||||
extension.flashWidget = vi.fn();
|
||||
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
|
||||
|
||||
await extension.applyWidgetUpdate({
|
||||
node_id: 1,
|
||||
graph_id: "root",
|
||||
action: "inject_text",
|
||||
value: "new prompt",
|
||||
mode: "replace",
|
||||
});
|
||||
|
||||
expect(node.widgets[0].value).toBe("old prompt");
|
||||
expect(node.widgets_values[0]).toBe("old prompt");
|
||||
expect(warnSpy).toHaveBeenCalledWith(
|
||||
expect.stringContaining("connected to an input"),
|
||||
expect.anything(),
|
||||
expect.anything()
|
||||
);
|
||||
await vi.waitFor(() => {
|
||||
expect(
|
||||
fetchMock.mock.calls.some(([url]) => url === "/api/lm/register-nodes")
|
||||
).toBe(true);
|
||||
});
|
||||
warnSpy.mockRestore();
|
||||
});
|
||||
});
|
||||
|
||||
describe("setup link-change hooks", () => {
|
||||
it("hooks root events, existing subgraphs, and future subgraphs", () => {
|
||||
const subgraph = createSubgraph({ id: "sub-1", nodes: [] });
|
||||
const graph = createGraph({ subgraphs: [subgraph] });
|
||||
appMock.graph = graph;
|
||||
|
||||
extension.setup();
|
||||
|
||||
expect(graph.events.addEventListener).toHaveBeenCalledWith(
|
||||
"node:slot-links:changed",
|
||||
expect.any(Function)
|
||||
);
|
||||
expect(graph.events.addEventListener).toHaveBeenCalledWith(
|
||||
"subgraph-created",
|
||||
expect.any(Function)
|
||||
);
|
||||
expect(subgraph.events.addEventListener).toHaveBeenCalledWith(
|
||||
"node:slot-links:changed",
|
||||
expect.any(Function)
|
||||
);
|
||||
|
||||
const createdHandler = graph.events.addEventListener.mock.calls.find(
|
||||
([name]) => name === "subgraph-created"
|
||||
)[1];
|
||||
const laterSubgraph = createSubgraph({ id: "sub-2", nodes: [] });
|
||||
createdHandler({ subgraph: laterSubgraph });
|
||||
expect(laterSubgraph.events.addEventListener).toHaveBeenCalledWith(
|
||||
"node:slot-links:changed",
|
||||
expect.any(Function)
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -315,6 +315,238 @@ describe('UI helper DOM utilities', () => {
|
||||
]);
|
||||
});
|
||||
|
||||
it('excludes prompt targets whose text widget is connected to an input', async () => {
|
||||
const registryResponse = {
|
||||
success: true,
|
||||
data: {
|
||||
node_count: 4,
|
||||
nodes: {
|
||||
'root:1': {
|
||||
id: 1,
|
||||
graph_id: 'root',
|
||||
graph_name: null,
|
||||
title: 'Free Text',
|
||||
type: 'CLIPTextEncode',
|
||||
mode: 0,
|
||||
marker_role: null,
|
||||
capabilities: {
|
||||
has_text_widget: true,
|
||||
text_widget_connected: false,
|
||||
widget_names: ['text', 'clip'],
|
||||
},
|
||||
},
|
||||
'root:2': {
|
||||
id: 2,
|
||||
graph_id: 'root',
|
||||
graph_name: null,
|
||||
title: 'Wired Text',
|
||||
type: 'CLIPTextEncode',
|
||||
mode: 0,
|
||||
marker_role: null,
|
||||
capabilities: {
|
||||
has_text_widget: true,
|
||||
text_widget_connected: true,
|
||||
widget_names: ['text', 'clip'],
|
||||
},
|
||||
},
|
||||
'root:3': {
|
||||
id: 3,
|
||||
graph_id: 'root',
|
||||
graph_name: null,
|
||||
title: 'Marked But Wired',
|
||||
type: 'KSampler',
|
||||
mode: 0,
|
||||
marker_role: 'send_prompt_target',
|
||||
capabilities: {
|
||||
has_text_widget: false,
|
||||
text_widget_connected: true,
|
||||
widget_names: ['seed'],
|
||||
},
|
||||
},
|
||||
'root:4': {
|
||||
id: 4,
|
||||
graph_id: 'root',
|
||||
graph_name: null,
|
||||
title: 'Free Text 2',
|
||||
type: 'CLIPTextEncode',
|
||||
mode: 0,
|
||||
marker_role: null,
|
||||
capabilities: {
|
||||
has_text_widget: true,
|
||||
text_widget_connected: false,
|
||||
widget_names: ['text', 'clip'],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
global.fetch = vi.fn().mockResolvedValue({
|
||||
json: async () => registryResponse,
|
||||
});
|
||||
|
||||
document.body.innerHTML = '<div id="nodeSelector"></div>';
|
||||
|
||||
const { sendPromptToWorkflow } = await import(UI_HELPERS_MODULE);
|
||||
|
||||
const result = await sendPromptToWorkflow('a cat');
|
||||
|
||||
expect(result).toBe(true);
|
||||
|
||||
const nodeLabels = Array.from(
|
||||
document.querySelectorAll('#nodeSelector .node-item[data-node-id] span')
|
||||
).map((span) => span.textContent.trim());
|
||||
|
||||
expect(nodeLabels).toEqual(['#1 Free Text', '#4 Free Text 2']);
|
||||
});
|
||||
|
||||
it('returns false when the only prompt target has its text widget connected', async () => {
|
||||
const registryResponse = {
|
||||
success: true,
|
||||
data: {
|
||||
node_count: 1,
|
||||
nodes: {
|
||||
'root:1': {
|
||||
id: 1,
|
||||
graph_id: 'root',
|
||||
graph_name: null,
|
||||
title: 'Wired Text',
|
||||
type: 'CLIPTextEncode',
|
||||
mode: 0,
|
||||
marker_role: null,
|
||||
capabilities: {
|
||||
has_text_widget: true,
|
||||
text_widget_connected: true,
|
||||
widget_names: ['text', 'clip'],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
global.fetch = vi.fn().mockResolvedValue({
|
||||
json: async () => registryResponse,
|
||||
});
|
||||
|
||||
document.body.innerHTML = '<div id="nodeSelector"></div>';
|
||||
translateMock.mockReturnValue(
|
||||
'No compatible prompt targets in the workflow.\nRight-click a node in ComfyUI → Mark as → Send Prompt Target'
|
||||
);
|
||||
|
||||
const { sendPromptToWorkflow } = await import(UI_HELPERS_MODULE);
|
||||
|
||||
const result = await sendPromptToWorkflow('a cat');
|
||||
|
||||
expect(result).toBe(false);
|
||||
expect(document.querySelectorAll('#nodeSelector .node-item').length).toBe(0);
|
||||
|
||||
const toast = document.querySelector('.toast-container .toast');
|
||||
expect(toast).not.toBeNull();
|
||||
expect(toast.textContent).toContain('Mark as');
|
||||
expect(toast.textContent).toContain('Send Prompt Target');
|
||||
});
|
||||
|
||||
it('shows the mark-as hint when no embedding target is available', async () => {
|
||||
const registryResponse = {
|
||||
success: true,
|
||||
data: {
|
||||
node_count: 1,
|
||||
nodes: {
|
||||
'root:1': {
|
||||
id: 1,
|
||||
graph_id: 'root',
|
||||
graph_name: null,
|
||||
title: 'Wired Text',
|
||||
type: 'CLIPTextEncode',
|
||||
mode: 0,
|
||||
marker_role: null,
|
||||
capabilities: {
|
||||
has_text_widget: true,
|
||||
text_widget_connected: true,
|
||||
widget_names: ['text', 'clip'],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
global.fetch = vi.fn().mockResolvedValue({
|
||||
json: async () => registryResponse,
|
||||
});
|
||||
|
||||
document.body.innerHTML = '<div id="nodeSelector"></div>';
|
||||
translateMock.mockReturnValue(
|
||||
'No compatible prompt targets in the workflow.\nRight-click a node in ComfyUI → Mark as → Send Prompt Target'
|
||||
);
|
||||
|
||||
const { sendEmbeddingToWorkflow } = await import(UI_HELPERS_MODULE);
|
||||
|
||||
const result = await sendEmbeddingToWorkflow('embeddingcode');
|
||||
|
||||
expect(result).toBe(false);
|
||||
|
||||
const toast = document.querySelector('.toast-container .toast');
|
||||
expect(toast).not.toBeNull();
|
||||
expect(toast.textContent).toContain('Send Prompt Target');
|
||||
});
|
||||
|
||||
it('keeps unconnected marker targets in the prompt candidate list', async () => {
|
||||
const registryResponse = {
|
||||
success: true,
|
||||
data: {
|
||||
node_count: 2,
|
||||
nodes: {
|
||||
'root:1': {
|
||||
id: 1,
|
||||
graph_id: 'root',
|
||||
graph_name: null,
|
||||
title: 'Marked Target',
|
||||
type: 'KSampler',
|
||||
mode: 0,
|
||||
marker_role: 'send_prompt_target',
|
||||
capabilities: {
|
||||
has_text_widget: false,
|
||||
text_widget_connected: false,
|
||||
widget_names: ['seed'],
|
||||
},
|
||||
},
|
||||
'root:2': {
|
||||
id: 2,
|
||||
graph_id: 'root',
|
||||
graph_name: null,
|
||||
title: 'Marked Target 2',
|
||||
type: 'KSampler',
|
||||
mode: 0,
|
||||
marker_role: 'send_prompt_target',
|
||||
capabilities: {
|
||||
has_text_widget: false,
|
||||
text_widget_connected: false,
|
||||
widget_names: ['seed'],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
global.fetch = vi.fn().mockResolvedValue({
|
||||
json: async () => registryResponse,
|
||||
});
|
||||
|
||||
document.body.innerHTML = '<div id="nodeSelector"></div>';
|
||||
|
||||
const { sendPromptToWorkflow } = await import(UI_HELPERS_MODULE);
|
||||
|
||||
const result = await sendPromptToWorkflow('a cat');
|
||||
|
||||
expect(result).toBe(true);
|
||||
|
||||
const nodeLabels = Array.from(
|
||||
document.querySelectorAll('#nodeSelector .node-item[data-node-id] span')
|
||||
).map((span) => span.textContent.trim());
|
||||
|
||||
expect(nodeLabels).toEqual(['#1 Marked Target', '#2 Marked Target 2']);
|
||||
});
|
||||
|
||||
it('opens Civitai links using the preferred host and registers the first-use banner once', async () => {
|
||||
const openSpy = vi.fn();
|
||||
globalThis.window.open = openSpy;
|
||||
|
||||
Reference in New Issue
Block a user