From ea948aa0f4a654b383a907b99e41b876e1d7e146 Mon Sep 17 00:00:00 2001 From: jaeone94 <89377375+jaeone94@users.noreply.github.com> Date: Thu, 8 Oct 2026 17:30:58 +0900 Subject: [PATCH 1/2] test(templates): prove a landed input asset becomes visible to availability The renderer stops treating a template input as missing once availability reports `present`. Nothing checked that against a real directory: the unit tests inject `access`, which proves the branch but not that the component writing the file and the component looking for it agree on where it goes. Three cases against a real temp tree and no injection: a file written where the download manager is pointed flips `missing` to `present`; one written beside that directory does not; and a name that escapes it is refused rather than resolved. Breaking the directory calculation fails the first, and removing the safe-name check fails the third. This is the stage the frontend browser test in ComfyUI_frontend#20496 could not reach - a renderer-side bridge mock cannot make a file appear to the backend that lists the combo options, so the warning clearing after a download could only be asserted in a way that passed whatever the event said. --- .../templateInputAssets.integration.test.ts | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 src/main/sources/standalone/templateInputAssets.integration.test.ts diff --git a/src/main/sources/standalone/templateInputAssets.integration.test.ts b/src/main/sources/standalone/templateInputAssets.integration.test.ts new file mode 100644 index 000000000..992fa35af --- /dev/null +++ b/src/main/sources/standalone/templateInputAssets.integration.test.ts @@ -0,0 +1,76 @@ +// @vitest-environment node +// The unit tests inject `access`; this one uses a real directory, so the writer +// and the reader have to agree on a path. +import fs from 'fs' +import os from 'os' +import path from 'path' +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' + +import type { InstallationRecord } from '../../installations' + +vi.mock('electron', () => ({ + app: { + isPackaged: false, + getPath: () => os.tmpdir(), + getVersion: () => '0.0.0-test', + getLocale: () => 'en' + } +})) + +const { resolveInputDir, resolveTemplateInputAssetAvailability } = + await import('./templateInputAssets') + +let installPath: string + +function installation(): InstallationRecord { + // Without `useSharedInput: false` the lookup escapes to the user's own + // configured input directory. + return { + id: 'inst-test', + installPath, + useSharedInput: false + } as InstallationRecord +} + +beforeEach(() => { + installPath = fs.mkdtempSync(path.join(os.tmpdir(), 'comfy-input-contract-')) + fs.mkdirSync(path.join(installPath, 'ComfyUI', 'input'), { recursive: true }) +}) + +afterEach(() => { + fs.rmSync(installPath, { recursive: true, force: true }) +}) + +describe('template input availability against a real input directory', () => { + it('sees a file written where a download would put it', async () => { + const inputDir = resolveInputDir(installation()) + const [before] = await resolveTemplateInputAssetAvailability(installation(), ['subject.png']) + expect(before).toEqual({ filename: 'subject.png', status: 'missing' }) + + fs.writeFileSync(path.join(inputDir, 'subject.png'), 'bytes') + + const [after] = await resolveTemplateInputAssetAvailability(installation(), ['subject.png']) + expect(after).toEqual({ filename: 'subject.png', status: 'present' }) + }) + + it('does not count a file that landed outside the input directory', async () => { + fs.writeFileSync(path.join(installPath, 'subject.png'), 'bytes') + + const [availability] = await resolveTemplateInputAssetAvailability(installation(), [ + 'subject.png' + ]) + expect(availability).toEqual({ filename: 'subject.png', status: 'missing' }) + }) + + it('answers nothing for a name that escapes the input directory', async () => { + const outside = path.join(installPath, 'escaped.png') + fs.writeFileSync(outside, 'bytes') + + await expect( + resolveTemplateInputAssetAvailability(installation(), [ + '../escaped.png', + 'nested/subject.png' + ]) + ).resolves.toEqual([]) + }) +}) From 8c4f36e83729894a791f825667d99f9da03ea292 Mon Sep 17 00:00:00 2001 From: jaeone94 <89377375+jaeone94@users.noreply.github.com> Date: Sun, 11 Oct 2026 09:41:43 +0900 Subject: [PATCH 2/2] test(templates): let the real writer choose where the asset lands The previous version wrote the file itself, straight to the path the reader checks. It would have stayed green if `downloadTemplateInputAssets` sent the file somewhere else, so it did not show what the commit claimed. My own check missed it: breaking `resolveInputDir` broke the test's write and the reader's lookup together, which proves the reader is self-consistent, not that the two components agree. Only the network boundary is replaced now. `download` writes to whatever `destPath` the writer picked, the writer runs for real, and the real reader is asked about the file afterwards. Pointing the writer at a subdirectory while leaving the reader alone now fails it. The already-present case comes along for free, since the writer reports `downloaded: false` and skips the transfer only if it looks where the reader looks. --- .../templateInputAssets.integration.test.ts | 82 +++++++++++++------ 1 file changed, 56 insertions(+), 26 deletions(-) diff --git a/src/main/sources/standalone/templateInputAssets.integration.test.ts b/src/main/sources/standalone/templateInputAssets.integration.test.ts index 992fa35af..0f85d1838 100644 --- a/src/main/sources/standalone/templateInputAssets.integration.test.ts +++ b/src/main/sources/standalone/templateInputAssets.integration.test.ts @@ -1,6 +1,8 @@ // @vitest-environment node -// The unit tests inject `access`; this one uses a real directory, so the writer -// and the reader have to agree on a path. +// The unit tests inject `access`, so they prove the branch but not that the +// component placing a file and the one looking for it pick the same path. Here +// the real writer chooses the destination and the real reader is asked about +// it; only the network boundary is replaced. import fs from 'fs' import os from 'os' import path from 'path' @@ -17,55 +19,83 @@ vi.mock('electron', () => ({ } })) -const { resolveInputDir, resolveTemplateInputAssetAvailability } = +/** Writes to whatever destination the writer chose, as a transfer would. */ +const download = vi.fn(async (_url: string, destPath: string) => { + await fs.promises.mkdir(path.dirname(destPath), { recursive: true }) + await fs.promises.writeFile(destPath, 'bytes') +}) + +vi.mock('../../lib/download', () => ({ + download: (...args: unknown[]) => download(...(args as [string, string])) +})) + +const { downloadTemplateInputAssets, resolveTemplateInputAssetAvailability } = await import('./templateInputAssets') +const ASSET = 'subject.png' +const workflow = { nodes: [{ type: 'LoadImage', widgets_values: [ASSET] }] } + let installPath: string +const log = () => {} function installation(): InstallationRecord { // Without `useSharedInput: false` the lookup escapes to the user's own // configured input directory. - return { - id: 'inst-test', - installPath, - useSharedInput: false - } as InstallationRecord + return { id: 'inst-test', installPath, useSharedInput: false } as InstallationRecord +} + +function availability(name: string) { + return resolveTemplateInputAssetAvailability(installation(), [name]).then(([entry]) => entry) } beforeEach(() => { + download.mockClear() installPath = fs.mkdtempSync(path.join(os.tmpdir(), 'comfy-input-contract-')) - fs.mkdirSync(path.join(installPath, 'ComfyUI', 'input'), { recursive: true }) }) afterEach(() => { fs.rmSync(installPath, { recursive: true, force: true }) }) -describe('template input availability against a real input directory', () => { - it('sees a file written where a download would put it', async () => { - const inputDir = resolveInputDir(installation()) - const [before] = await resolveTemplateInputAssetAvailability(installation(), ['subject.png']) - expect(before).toEqual({ filename: 'subject.png', status: 'missing' }) +describe('a downloaded template input asset', () => { + it('lands where the availability reader looks for it', async () => { + expect(await availability(ASSET)).toEqual({ + filename: ASSET, + status: 'missing' + }) - fs.writeFileSync(path.join(inputDir, 'subject.png'), 'bytes') + const placed = await downloadTemplateInputAssets( + installation(), + 'template-a', + log, + undefined, + workflow + ) - const [after] = await resolveTemplateInputAssetAvailability(installation(), ['subject.png']) - expect(after).toEqual({ filename: 'subject.png', status: 'present' }) + expect(placed).toEqual([{ filename: ASSET, downloaded: true }]) + expect(await availability(ASSET)).toEqual({ + filename: ASSET, + status: 'present' + }) }) - it('does not count a file that landed outside the input directory', async () => { - fs.writeFileSync(path.join(installPath, 'subject.png'), 'bytes') + it('is not fetched again when it is already there', async () => { + await downloadTemplateInputAssets(installation(), 'template-a', log, undefined, workflow) + download.mockClear() - const [availability] = await resolveTemplateInputAssetAvailability(installation(), [ - 'subject.png' - ]) - expect(availability).toEqual({ filename: 'subject.png', status: 'missing' }) + const placed = await downloadTemplateInputAssets( + installation(), + 'template-a', + log, + undefined, + workflow + ) + + expect(placed).toEqual([{ filename: ASSET, downloaded: false }]) + expect(download).not.toHaveBeenCalled() }) it('answers nothing for a name that escapes the input directory', async () => { - const outside = path.join(installPath, 'escaped.png') - fs.writeFileSync(outside, 'bytes') - await expect( resolveTemplateInputAssetAvailability(installation(), [ '../escaped.png',