Skip to content
Merged
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
106 changes: 106 additions & 0 deletions src/main/sources/standalone/templateInputAssets.integration.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// @vitest-environment node
// 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'
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'
}
}))

/** Writes to whatever destination the writer chose, as a transfer would. */
const download = vi.fn(async (_url: string, destPath: string) => {

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.

🟢 Low — The download mock is typed and cast as a two-argument function, so the signal that production passes (download(url, destPath, null, { signal })) is dropped, and every call site here passes undefined for the abort signal. Widening the mock signature and adding a cancellation case would exercise the abort path the install task always supplies. Raised by 1 of 8 reviewers (claude-opus-5-thinking-max adversarial).

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

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.

⚪ Nit — The suite performs real filesystem writes and relies on useSharedInput: false inside an as InstallationRecord cast to keep them in the temp dir; the cast suppresses the type check, so a rename of that field would silently redirect writes to the user's real shared input dir, which afterEach does not clean up. Building the record from a typed factory or satisfying the full type would keep that guard enforced. Raised by 1 of 8 reviewers (claude-opus-5-thinking-max adversarial).

}

function availability(name: string) {
return resolveTemplateInputAssetAvailability(installation(), [name]).then(([entry]) => entry)
}

beforeEach(() => {
download.mockClear()
installPath = fs.mkdtempSync(path.join(os.tmpdir(), 'comfy-input-contract-'))
})

afterEach(() => {
fs.rmSync(installPath, { recursive: true, force: true })
})

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'
})

const placed = await downloadTemplateInputAssets(
installation(),
'template-a',
log,
undefined,
workflow
)

expect(placed).toEqual([{ filename: ASSET, downloaded: true }])
expect(await availability(ASSET)).toEqual({
filename: ASSET,
status: 'present'

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.

🟢 Low — This test shows the writer and reader agree on the path, but resolveTemplateInputAssetAvailability reports present for anything access can stat, so a half-written asset makes the IPC handler answer already-present and the renderer has no way to repair it. A companion case that pre-creates a partial file would pin down the intended behavior. Raised by 1 of 8 reviewers (claude-opus-5-thinking-max edge-case).

})
})

it('is not fetched again when it is already there', async () => {
await downloadTemplateInputAssets(installation(), 'template-a', log, undefined, workflow)

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.

🟢 Low — The idempotency guarantee asserted here only holds for sequential calls: the access probe followed by a direct download to the final path has no per-destination lock and bypasses the comfyDownloadManager job registry, so an install plus a concurrent desktop2-download-template-input-asset request (or two installs sharing the default shared input dir) can both pass the probe and interleave writes. Worth noting the limitation in the test's intent comment, or covering it with a concurrent case. Raised by 2 of 8 reviewers (claude-opus-5-thinking-max adversarial/edge-case, kimi-k2.7-code adversarial).

download.mockClear()

const placed = await downloadTemplateInputAssets(
installation(),
'template-a',
log,
undefined,
workflow
)

expect(placed).toEqual([{ filename: ASSET, downloaded: false }])

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.

🟡 Medium — The stub download always writes a complete file, so pinning downloaded: false here ratifies downloadTemplateInputAssets' bare fs.promises.access(destPath) existence check as sufficient. The real download streams directly to the final path and leaves a truncated file plus .dl-meta sidecar behind on abort or stall, so an interrupted asset is skipped forever instead of resumed; consider using isDownloadComplete() (as installer.ts does) and having the test write a partial file to cover that case. Raised by 2 of 8 reviewers (claude-opus-5-thinking-max adversarial, claude-opus-5-thinking-max edge-case).

expect(download).not.toHaveBeenCalled()
})

it('answers nothing for a name that escapes the input directory', async () => {
await expect(
resolveTemplateInputAssetAvailability(installation(), [
'../escaped.png',
'nested/subject.png'
])
).resolves.toEqual([])
})
})
Loading