/* Copyright (C) 2025 QuantumNous This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . For commercial licensing, please contact support@quantumnous.com */ import assert from 'node:assert/strict'; import test from 'node:test'; import { COMPARE_MAX, autoSubmitDecision, compareBlockOf, compareParamSummary, compareQuoteRequest, comparableBatchTasks, groupBatchRows, needsBatchConfirm, newBatchId, pickSharedValues, resolveCompareEntries, selectedCompareModels, sharedParamsOf, sharedValuesOf, toggleCompareModel, } from './creationCompare.js'; const sizeParam = (options, extra = {}) => ({ key: 'size', type: 'size', options, default: options[0], ...extra, }); const gpt = { model: 'gpt-image-1', modes: ['text2image', 'edit'], inputs: { reference_images: { modes: ['edit'], max: 4 } }, params: [ sizeParam(['1024x1024', '1536x1024', '1024x1536']), { key: 'quality', type: 'chips', options: ['low', 'medium', 'high'], default: 'high', }, { key: 'n', type: 'slider', min: 1, max: 4, default: 1 }, { key: 'background', type: 'chips', options: ['auto', 'transparent'] }, ], }; const seedream = { model: 'seedream-4', modes: ['text2image', 'edit'], inputs: { reference_images: { modes: ['edit'], max: 1 } }, params: [ sizeParam(['1024x1024', '2048x2048', '1536x1024'], { custom_size: { min: 512, max: 4096, step: 8 }, }), { key: 'quality', type: 'chips', options: ['standard', 'high'], default: 'standard', }, { key: 'n', type: 'slider', min: 1, max: 2, default: 1 }, { key: 'seed', type: 'seed' }, ], }; const dalle = { model: 'dall-e-3', modes: ['text2image'], params: [ sizeParam(['1024x1024', '1792x1024']), { key: 'quality', type: 'chips', options: ['standard', 'hd'] }, ], }; const locked = { ...dalle, model: 'locked', level_locked: true }; test('compareBlockOf:模式 / 参考图张数 / 等级', () => { assert.equal(compareBlockOf(gpt, { mode: 'text2image' }), null); assert.deepEqual(compareBlockOf(dalle, { mode: 'edit', refCount: 1 }), { key: '不支持图生图', }); assert.deepEqual(compareBlockOf(seedream, { mode: 'edit', refCount: 2 }), { key: '最多支持 {{n}} 张参考图', values: { n: 1 }, }); assert.equal(compareBlockOf(seedream, { mode: 'edit', refCount: 1 }), null); assert.deepEqual(compareBlockOf(locked, {}), { key: '需升级' }); assert.deepEqual(compareBlockOf(null), { key: '模型不可用' }); }); test('toggleCompareModel / selectedCompareModels:上限与剔除不可选', () => { let r = toggleCompareModel([], 'a'); assert.deepEqual(r, { selected: ['a'], full: false }); r = toggleCompareModel(['a', 'b', 'c', 'd'], 'e'); assert.equal(r.full, true); assert.equal(r.selected.length, COMPARE_MAX); assert.deepEqual(toggleCompareModel(['a', 'b'], 'a').selected, ['b']); const models = [gpt, seedream, dalle, locked]; assert.deepEqual( selectedCompareModels(models, ['dall-e-3', 'gpt-image-1', 'locked', 'x'], { mode: 'text2image', }).map((m) => m.model), ['dall-e-3', 'gpt-image-1'], ); assert.deepEqual( selectedCompareModels(models, ['dall-e-3', 'gpt-image-1'], { mode: 'edit', refCount: 1, }).map((m) => m.model), ['gpt-image-1'], ); }); test('sharedParamsOf:键交集 + 选项交集 + 范围交集', () => { const shared = sharedParamsOf([gpt, seedream]); assert.deepEqual( shared.map((p) => p.key), ['size', 'quality', 'n'], ); const size = shared.find((p) => p.key === 'size'); assert.deepEqual(size.options, ['1024x1024', '1536x1024']); assert.equal( size.custom_size, undefined, 'gpt 不支持自定义尺寸,交集也不支持', ); const quality = shared.find((p) => p.key === 'quality'); // requestParamsOf 给画质补了显示名 assert.deepEqual(quality.options, [{ value: 'high', label: '高' }]); assert.equal(quality.default, 'high'); const n = shared.find((p) => p.key === 'n'); assert.equal(n.min, 1); assert.equal(n.max, 2); // 选项交集为空的参数不显示 assert.deepEqual( sharedParamsOf([gpt, dalle]).map((p) => p.key), ['size'], ); // 单个模型 = 自身参数 assert.equal(sharedParamsOf([seedream]).length, 4); assert.deepEqual(sharedParamsOf([]), []); // 两个都支持自定义尺寸:范围取交集 const a = { ...seedream, params: [ sizeParam(['1024x1024'], { custom_size: { min: 256, max: 2048, step: 16 }, }), ], }; const merged = sharedParamsOf([seedream, a])[0]; assert.deepEqual(merged.custom_size, { min: 512, max: 2048, step: 16 }); }); test('sharedValuesOf:对比值 → 创作台值 → 默认', () => { const shared = sharedParamsOf([gpt, seedream]); assert.deepEqual(sharedValuesOf(shared, {}, {}), { size: '1024x1024', quality: 'high', n: 1, }); assert.deepEqual( sharedValuesOf( shared, { quality: 'low', n: 2 }, { size: '1536x1024', quality: 'high', n: 4 }, ), { size: '1536x1024', quality: 'high', n: 2 }, ); assert.deepEqual(pickSharedValues(shared, { size: 'x', seed: 1, n: 2 }), { size: 'x', n: 2, }); }); test('resolveCompareEntries:共同参数统一,其余按模型记忆 / 默认;带 batch_id', () => { const form = { modelName: 'gpt-image-1', mode: 'text2image', prompt: 'a cat', params: { size: '1536x1024', quality: 'high', background: 'transparent' }, paramMemory: { 'seedream-4': { seed: 42 } }, images: { reference_images: [] }, }; const shared = sharedParamsOf([gpt, seedream]); const values = sharedValuesOf(shared, {}, form.params); const entries = resolveCompareEntries([gpt, seedream], form, values, 'b-1'); assert.equal(entries.length, 2); assert.deepEqual(entries[0].payload, { model: 'gpt-image-1', prompt: 'a cat', source: 'creation_studio', size: '1536x1024', quality: 'high', n: 1, background: 'transparent', batch_id: 'b-1', }); assert.deepEqual(entries[1].payload, { model: 'seedream-4', prompt: 'a cat', source: 'creation_studio', size: '1536x1024', quality: 'high', n: 1, seed: 42, batch_id: 'b-1', }); const quote = compareQuoteRequest(entries); assert.deepEqual(quote, { kind: 'image', items: [ { model: 'gpt-image-1', mode: 'text2image', params: { size: '1536x1024', quality: 'high' }, n: 1, }, { model: 'seedream-4', mode: 'text2image', params: { size: '1536x1024', quality: 'high' }, n: 1, }, ], }); assert.equal(compareQuoteRequest([]), null); assert.equal( compareParamSummary(entries[1].payload), '1536×1024 · high · seed 42', ); }); test('resolveCompareEntries:编辑模式按各模型上限提交参考图', () => { const form = { modelName: 'gpt-image-1', mode: 'edit', prompt: 'p', params: {}, images: { reference_images: [{ id: 'a', status: 'ready', ref: 'task://t1' }], }, }; const entries = resolveCompareEntries([gpt, seedream], form, {}, ''); assert.equal(entries[0].payload.image, 'task://t1'); assert.equal(entries[1].payload.image, 'task://t1'); assert.equal(entries[0].payload.batch_id, undefined); }); test('newBatchId:UUID v4 格式(有无 randomUUID 都可)', () => { const re = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/; assert.match(newBatchId(), re); assert.match(newBatchId({ getRandomValues: (b) => b.fill(255) }), re); assert.match(newBatchId({}), re); }); test('needsBatchConfirm:只对多张 / 对比生成且超过阈值', () => { assert.equal(needsBatchConfirm(200, 100, { batch: true }), true); assert.equal(needsBatchConfirm(100, 100, { batch: true }), false); assert.equal(needsBatchConfirm(200, 100), false); assert.equal(needsBatchConfirm(200, '0', { batch: true }), false); assert.equal(needsBatchConfirm(200, '', { batch: true }), false); assert.equal(needsBatchConfirm(200, '136986', { batch: true }), false); assert.equal(needsBatchConfirm(200000, '136986', { batch: true }), true); }); test('autoSubmitDecision:再生成 / 换种子与手动提交同一金额阈值', () => { const big = { total: 200000 }; const small = { total: 1000 }; // 单张 / 阈值关闭:直接提交,不等估价 assert.equal(autoSubmitDecision({ n: 1, threshold: 136986 }), 'submit'); assert.equal(autoSubmitDecision({ threshold: 136986 }), 'submit'); assert.equal( autoSubmitDecision({ n: 4, threshold: 0, quote: big, quoteFresh: true }), 'submit', ); assert.equal(autoSubmitDecision({ n: 4, threshold: '' }), 'submit'); // 多张:估价未对应当前参数时等待(不能用上一次参数的估价) assert.equal( autoSubmitDecision({ n: 4, threshold: 136986, quote: small }), 'wait', ); // 超阈值确认,未超直接提交(与 needsBatchConfirm 一致,等于阈值不确认) assert.equal( autoSubmitDecision({ n: 4, threshold: 136986, quote: big, quoteFresh: true, }), 'confirm', ); assert.equal( autoSubmitDecision({ n: '2', threshold: '136986', quote: { total: 136986 }, quoteFresh: true, }), 'submit', ); assert.equal( autoSubmitDecision({ n: 4, threshold: 136986, quote: small, quoteFresh: true, }), 'submit', ); // 估价失败 / 无估价:不自动提交 assert.equal( autoSubmitDecision({ n: 4, threshold: 136986, quoteFresh: true, quoteFailed: true, }), 'manual', ); assert.equal( autoSubmitDecision({ n: 4, threshold: 136986, quoteFresh: true }), 'manual', ); }); test('groupBatchRows:同批次成组,位置取首个成员;单个成员按普通卡片', () => { const tasks = [ { id: '1', batchId: 'b' }, { id: '2' }, { id: '3', batchId: 'b' }, { id: '4', batchId: 'c' }, { id: '5', batchId: 'b' }, ]; const rows = groupBatchRows(tasks); assert.deepEqual( rows.map((r) => r.type === 'batch' ? `${r.batchId}:${r.tasks.map((t) => t.id).join('')}` : r.task.id, ), ['b:135', '2', '4'], ); assert.deepEqual(groupBatchRows(null), []); assert.deepEqual( comparableBatchTasks([ { id: 'a', status: 'success', images: [{ url: 'u' }] }, { id: 'b', status: 'failed', images: [] }, { id: 'c', status: 'success', images: [{ url: '' }] }, ]).map((t) => t.id), ['a'], ); });