From 1cfdcd82e1007d4a965efaddae6cd84878e86738 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=B9=E5=8D=9A=E6=B7=B3?= Date: Mon, 17 Aug 2026 07:19:49 +0800 Subject: [PATCH] fix: skip non-image ComfyUI outputs (#5910) * fix: skip non-image ComfyUI outputs * test: cover ComfyUI gif fallback and no-output error paths Extends the skip-non-image regression with the gifs fallback that animated workflows rely on and the error path when no output node carries images or gifs. Co-Authored-By: Claude Fable 5 --------- Co-authored-by: CaoBochun Co-authored-by: Wolfsblvt Co-authored-by: Claude Fable 5 --- src/endpoints/stable-diffusion.js | 3 +- tests/stable-diffusion.test.js | 113 ++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 tests/stable-diffusion.test.js diff --git a/src/endpoints/stable-diffusion.js b/src/endpoints/stable-diffusion.js index f09979f2b..077c2f961 100644 --- a/src/endpoints/stable-diffusion.js +++ b/src/endpoints/stable-diffusion.js @@ -611,7 +611,8 @@ comfy.post('/generate', async (request, response) => { } const outputs = Object.keys(item.outputs).map(it => item.outputs[it]); console.debug('ComfyUI outputs:', outputs); - const imgInfo = outputs.map(it => it.images).flat()[0] ?? outputs.map(it => it.gifs).flat()[0]; + const imgInfo = outputs.map(it => it.images).filter(Array.isArray).flat()[0] + ?? outputs.map(it => it.gifs).filter(Array.isArray).flat()[0]; if (!imgInfo) { throw new Error('ComfyUI did not return any recognizable outputs.'); } diff --git a/tests/stable-diffusion.test.js b/tests/stable-diffusion.test.js new file mode 100644 index 000000000..6a33571d8 --- /dev/null +++ b/tests/stable-diffusion.test.js @@ -0,0 +1,113 @@ +import { afterAll, beforeAll, beforeEach, describe, expect, jest, test } from '@jest/globals'; + +const fetchMock = jest.fn(); +jest.unstable_mockModule('node-fetch', () => ({ default: fetchMock })); +jest.unstable_mockModule('../src/endpoints/secrets.js', () => ({ + readSecret: jest.fn(), + SECRET_KEYS: {}, +})); + +describe('ComfyUI generation', () => { + /** @type {import('node:http').Server} */ + let server; + let baseUrl; + + beforeAll(async () => { + const { default: express } = await import('express'); + const { router } = await import('../src/endpoints/stable-diffusion.js'); + const app = express(); + app.use(express.json()); + app.use(router); + server = app.listen(0, '127.0.0.1'); + await new Promise(resolve => server.once('listening', resolve)); + const address = server.address(); + baseUrl = `http://127.0.0.1:${address.port}`; + }); + + afterAll(async () => { + await new Promise((resolve, reject) => server.close(error => error ? reject(error) : resolve())); + }); + + beforeEach(() => { + fetchMock.mockReset(); + }); + + test('skips output nodes without images', async () => { + fetchMock + .mockResolvedValueOnce({ ok: true, json: async () => ({ prompt_id: 'prompt-1' }) }) + .mockResolvedValueOnce({ + ok: true, + json: async () => ({ + 'prompt-1': { + status: { status_str: 'success' }, + outputs: { + 117: { a_images: [{ filename: 'comparison.png' }] }, + 197: { images: [{ filename: 'result.png', subfolder: '', type: 'temp' }] }, + }, + }, + }), + }) + .mockResolvedValueOnce({ ok: true, arrayBuffer: async () => Uint8Array.from([1, 2, 3]).buffer }); + + const response = await fetch(`${baseUrl}/comfy/generate`, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ url: 'http://127.0.0.1:8188', prompt: '{}' }), + }); + + expect(response.status).toBe(200); + expect(await response.json()).toEqual({ format: 'png', data: 'AQID' }); + }); + + test('falls back to gifs when no output node has images', async () => { + fetchMock + .mockResolvedValueOnce({ ok: true, json: async () => ({ prompt_id: 'prompt-2' }) }) + .mockResolvedValueOnce({ + ok: true, + json: async () => ({ + 'prompt-2': { + status: { status_str: 'success' }, + outputs: { + 10: { text: ['some non-media output'] }, + 42: { gifs: [{ filename: 'animation.webp', subfolder: '', type: 'output' }] }, + }, + }, + }), + }) + .mockResolvedValueOnce({ ok: true, arrayBuffer: async () => Uint8Array.from([4, 5, 6]).buffer }); + + const response = await fetch(`${baseUrl}/comfy/generate`, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ url: 'http://127.0.0.1:8188', prompt: '{}' }), + }); + + expect(response.status).toBe(200); + expect(await response.json()).toEqual({ format: 'webp', data: 'BAUG' }); + }); + + test('reports an error when no output has images or gifs', async () => { + fetchMock + .mockResolvedValueOnce({ ok: true, json: async () => ({ prompt_id: 'prompt-3' }) }) + .mockResolvedValueOnce({ + ok: true, + json: async () => ({ + 'prompt-3': { + status: { status_str: 'success' }, + outputs: { + 10: { text: ['no media at all'] }, + }, + }, + }), + }); + + const response = await fetch(`${baseUrl}/comfy/generate`, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ url: 'http://127.0.0.1:8188', prompt: '{}' }), + }); + + expect(response.status).toBe(500); + expect(await response.text()).toContain('did not return any recognizable outputs'); + }); +});