const test = require('node:test'); const assert = require('node:assert/strict'); const fs = require('node:fs'); const path = require('node:path'); const { app, jobFromHistory, statusFromHistory, readVideoSize } = require('./server'); const { createJobStore } = require('./job-store'); test('serves frontend assets before authentication redirects', async (t) => { const server = app.listen(0); const browserDir = path.join(__dirname, 'frontend', 'dist', 'browser'); const asset = '__qa-static-asset.js'; fs.mkdirSync(browserDir, { recursive: true }); fs.writeFileSync(path.join(browserDir, asset), 'window.__qaStaticAsset = true;\n'); t.after(() => server.close()); t.after(() => fs.rmSync(path.join(browserDir, asset), { force: true })); await new Promise((resolve) => server.once('listening', resolve)); const port = server.address().port; const response = await fetch(`http://127.0.0.1:${port}/${asset}`); assert.equal(response.status, 200); assert.match(response.headers.get('content-type') || '', /javascript/); }); test('maps completed ComfyUI history into a read-only job summary', () => { const job = jobFromHistory('abc123', { prompt: [1, 'abc123', { '7': { class_type: 'CheckpointLoaderSimple', inputs: { ckpt_name: 'portrait.safetensors' } } }, { title: 'Portrait' }], status: { completed: true, messages: [['execution_start', { timestamp: 1000 }], ['execution_success', { timestamp: 3250 }]] }, outputs: { '7': { images: [{ filename: 'result.png', subfolder: '', type: 'output' }] } } }); assert.deepEqual(job, { promptId: 'abc123', title: 'Portrait', status: 'completed', queuedAt: 1000, completedAt: 3250, durationMs: 2250, outputCount: 1, assets: [{ filename: 'result.png', subfolder: '', type: 'image', url: '/api/assets?filename=result.png&subfolder=&type=output' }], models: [{ nodeId: '7', type: 'CheckpointLoaderSimple', name: 'portrait.safetensors' }], progress: null }); }); test('maps ComfyUI audio outputs, including FLAC, into downloadable audio assets', () => { const job = jobFromHistory('audio123', { prompt: [1, 'audio123', {}, { title: 'Neo-soul piano' }], status: { completed: true }, outputs: { '9': { audio: [{ filename: 'neo_soul.flac', subfolder: '', type: 'output' }] } }, }); assert.equal(job.outputCount, 1); assert.deepEqual(job.assets[0], { filename: 'neo_soul.flac', subfolder: '', type: 'audio', url: '/api/assets?filename=neo_soul.flac&subfolder=&type=output', }); }); test('recognises audio output arrays used by alternate ComfyUI nodes', () => { const job = jobFromHistory('audio456', { prompt: [1, 'audio456', {}, { title: 'Audio' }], status: { completed: true }, outputs: { '9': { audios: [{ filename: 'result.mp3', subfolder: 'music', type: 'output' }] } }, }); assert.equal(job.assets[0].type, 'audio'); assert.match(job.assets[0].url, /filename=result.mp3/); }); test('reads video size with HEAD without downloading the video', async (t) => { const originalFetch = global.fetch; const calls = []; global.fetch = async (_url, options = {}) => { calls.push(options); return new Response(null, { status: 200, headers: { 'content-length': '987654' } }); }; t.after(() => { global.fetch = originalFetch; }); assert.equal(await readVideoSize('http://comfy/view?filename=clip.mp4'), 987654); assert.deepEqual(calls, [{ method: 'HEAD' }]); }); test('falls back to a one-byte range probe when video HEAD has no length', async (t) => { const originalFetch = global.fetch; const calls = []; global.fetch = async (_url, options = {}) => { calls.push(options); if (options.method === 'HEAD') return new Response(null, { status: 200 }); return new Response(new Uint8Array([0]), { status: 206, headers: { 'content-range': 'bytes 0-0/1234567' } }); }; t.after(() => { global.fetch = originalFetch; }); assert.equal(await readVideoSize('http://comfy/view?filename=clip.mp4'), 1234567); assert.equal(calls[1].headers.Range, 'bytes=0-0'); }); test('classifies failed history without exposing mutation paths', () => { assert.equal(statusFromHistory({ status: { messages: [['execution_error', {}]] } }), 'failed'); }); test('persists jobs across syncs and deduplicates model usage by prompt id', () => { const directory = fs.mkdtempSync(path.join(__dirname, 'job-store-test-')); const store = createJobStore(path.join(directory, 'jobs.json')); const job = { promptId: 'prompt-1', title: 'First', status: 'completed', outputCount: 1, models: [{ nodeId: '1', type: 'Checkpoint', name: 'model-a.safetensors' }, { nodeId: '2', type: 'Lora', name: 'style.safetensors' }] }; store.sync([job]); store.sync([{ ...job, status: 'completed' }]); store.sync([{ promptId: 'prompt-2', title: 'Second', status: 'completed', outputCount: 1, models: [{ nodeId: '1', type: 'Checkpoint', name: 'model-a.safetensors' }] }]); assert.deepEqual(store.stats(), { totalJobs: 2, totalModels: 2, models: [{ name: 'model-a.safetensors', jobCount: 2 }, { name: 'style.safetensors', jobCount: 1 }] }); assert.equal(JSON.parse(fs.readFileSync(store.path, 'utf8')).jobs['prompt-1'].promptId, 'prompt-1'); fs.rmSync(directory, { recursive: true, force: true }); });