/* 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 { DRAFT_STORAGE_KEY, buildVideoRequest, composeParams, formFromTaskRequest, imageBlockReason, imageSlotsOf, loadDraft, modeOptionsOf, pickModeForModel, pollDelay, saveDraft, } from './videoCreation.js'; const seedance = { model: 'seedance', modes: ['text2video', 'image2video', 'first_last_frame'], params: [ { key: 'aspect_ratio', type: 'ratio', options: ['16:9', '9:16', '1:1'], default: '16:9', placement: 'dock', }, { key: 'duration', type: 'segmented', options: [5, 10], default: 5, unit: 's', placement: 'dock', }, { key: 'generate_audio', type: 'switch', default: true, placement: 'advanced', }, { key: 'seed', type: 'seed', placement: 'advanced' }, { key: 'camera_fixed', type: 'switch', default: false, placement: 'advanced', }, { key: 'return_last_frame', type: 'switch', default: true, placement: 'hidden', }, ], inputs: { first_frame: { label: '首帧', modes: ['image2video', 'first_last_frame'] }, last_frame: { label: '尾帧', modes: ['first_last_frame'] }, }, }; const vidu = { model: 'vidu', modes: ['text2video', 'reference2video'], disabled_modes: [{ mode: 'image2video', reason: 'COS 未开启' }], params: [ { key: 'duration', type: 'segmented', options: [4, 8], default: 4, placement: 'dock', }, ], inputs: { reference_images: { label: '参考图', modes: ['reference2video'], max: 2 }, }, }; const ready = (ref) => ({ id: ref, ref, status: 'ready' }); test('modeOptionsOf 列出可用与禁用模式并保持顺序', () => { assert.deepEqual(modeOptionsOf(vidu), [ { mode: 'text2video', disabled: false, reason: '' }, { mode: 'image2video', disabled: true, reason: 'COS 未开启' }, { mode: 'reference2video', disabled: false, reason: '' }, ]); }); test('首尾帧模式按 inputs 生成带标签的两个槽', () => { const slots = imageSlotsOf(seedance, 'first_last_frame', { first_frame: null, last_frame: null, reference_images: [], }); assert.deepEqual( slots.map((s) => [s.key, s.label, s.active]), [ ['first_frame', '首帧', true], ['last_frame', '尾帧', true], ], ); }); test('当前模式用不到的图片保留为非激活槽且不提交', () => { const images = { first_frame: ready('cos://playground/1/a.png'), last_frame: ready('cos://playground/1/b.png'), reference_images: [], }; const slots = imageSlotsOf(seedance, 'image2video', images); assert.equal(slots.find((s) => s.key === 'last_frame').active, false); const body = buildVideoRequest(seedance, { mode: 'image2video', prompt: 'p', params: {}, images, }); assert.equal(body.image, 'cos://playground/1/a.png'); assert.equal(body.last_image, undefined); }); test('参考图槽数受 max 限制,超出的不提交', () => { const images = { first_frame: null, last_frame: null, reference_images: [ready('r1'), ready('r2'), ready('r3')], }; const slots = imageSlotsOf(vidu, 'reference2video', images); assert.deepEqual( slots.map((s) => [s.labelIndex, s.active]), [ [1, true], [2, true], [3, false], ], ); const body = buildVideoRequest(vidu, { mode: 'reference2video', prompt: 'p', params: {}, images, }); assert.deepEqual(body.reference_images, ['r1', 'r2']); }); test('请求体只带 schema 内参数,比例 / 音频写入别名,随机种子不提交', () => { const body = buildVideoRequest(seedance, { mode: 'text2video', prompt: 'hello', params: { aspect_ratio: '9:16', duration: 10, seed: null, fps: 30 }, images: undefined, }); assert.deepEqual(body, { mode: 'text2video', prompt: 'hello', aspect_ratio: '9:16', ratio: '9:16', duration: 10, generate_audio: true, audio: true, return_last_frame: true, }); }); test('缺图 / 上传中 / 失败时给出阻断原因', () => { const base = { first_frame: null, last_frame: null, reference_images: [] }; assert.deepEqual(imageBlockReason(seedance, 'image2video', base), { key: '请上传首帧图片', }); assert.deepEqual( imageBlockReason(seedance, 'first_last_frame', { ...base, first_frame: ready('a'), }), { key: '缺少尾帧' }, ); assert.deepEqual( imageBlockReason(seedance, 'image2video', { ...base, first_frame: { id: 'x', status: 'uploading' }, }), { key: '图片上传中,请稍候' }, ); assert.equal( imageBlockReason(seedance, 'image2video', { ...base, first_frame: ready('a'), }), null, ); assert.deepEqual(imageBlockReason(vidu, 'image2video', base), { key: '当前模型不支持该生成模式,请切换模式', }); }); test('切换模型:会话值优先、记忆其次,不合法值吸附并报告', () => { const { values, adjustments } = composeParams(vidu, { sessionValues: { duration: 10, aspect_ratio: '9:16' }, memory: { duration: 4 }, }); assert.deepEqual(values, { duration: 8 }); assert.deepEqual(adjustments, [{ key: 'duration', from: 10, to: 8 }]); const kept = composeParams(seedance, { memory: { duration: 10 } }); assert.equal(kept.values.duration, 10); assert.equal(kept.values.camera_fixed, undefined); }); test('切换模型时的模式选择', () => { const images = { first_frame: ready('a'), last_frame: null, reference_images: [], }; assert.equal( pickModeForModel(seedance, 'reference2video', images), 'image2video', ); assert.equal(pickModeForModel(vidu, 'image2video', images), 'text2video'); assert.equal( pickModeForModel(seedance, 'first_last_frame', images), 'first_last_frame', ); }); const memoryStorage = (quota = Infinity) => { const map = new Map(); return { getItem: (k) => (map.has(k) ? map.get(k) : null), setItem: (k, v) => { if (v.length > quota) throw new Error('QuotaExceededError'); map.set(k, v); }, map, }; }; test('草稿 v3 往返;配额不足时丢弃内联图片', () => { const storage = memoryStorage(400); const form = { mode: 'image2video', prompt: 'cat', selectedTemplate: '', modelName: 'seedance', params: { duration: 5 }, paramMemory: {}, images: { first_frame: ready(`data:image/png;base64,${'A'.repeat(500)}`), last_frame: { id: 'u', status: 'uploading' }, reference_images: [], }, }; assert.equal(saveDraft(storage, form), true); const loaded = loadDraft(storage); assert.equal(loaded.prompt, 'cat'); assert.equal(loaded.images.first_frame, null); assert.equal(loaded.images.last_frame, null); assert.ok(storage.map.has(DRAFT_STORAGE_KEY)); }); test('损坏的草稿与抛异常的存储不影响加载', () => { const broken = { getItem: () => '{not json', }; assert.equal(loadDraft(broken).mode, 'text2video'); const throwing = { getItem: () => { throw new Error('SecurityError'); }, setItem: () => { throw new Error('SecurityError'); }, }; assert.equal(loadDraft(throwing).prompt, ''); assert.equal(saveDraft(throwing, loadDraft(throwing)), false); }); test('轮询退避 3s → 5s → 10s,隐藏时降频', () => { assert.deepEqual( [0, 1, 2, 5].map((s) => pollDelay(s, false)), [3000, 5000, 10000, 10000], ); assert.equal(pollDelay(0, true), 30000); }); test('formFromTaskRequest maps v2 request refs and params', () => { let n = 0; const form = formFromTaskRequest( { model: 'seedance', mode: 'first_last_frame', prompt: 'p', resolution: '1080p', duration: 10, ratio: '9:16', aspect_ratio: '9:16', generate_audio: false, audio: true, seed: 0, image: 'https://cos/first.png?sig', image_ref: 'task://t1#first', last_image: 'https://cos/last.png?sig', last_image_ref: 'cos://u/last.png', reference_images: ['asset://a1'], reference_image_refs: ['asset://a1'], }, { newId: () => `id${n++}` }, ); assert.equal(form.modelName, 'seedance'); assert.equal(form.mode, 'first_last_frame'); assert.deepEqual(form.params, { aspect_ratio: '9:16', resolution: '1080p', duration: 10, generate_audio: false, watermark: false, seed: 0, }); assert.equal(form.images.first_frame.ref, 'task://t1#first'); assert.equal(form.images.first_frame.previewUrl, 'https://cos/first.png?sig'); assert.equal(form.images.last_frame.ref, 'cos://u/last.png'); assert.equal(form.images.reference_images[0].ref, 'asset://a1'); assert.equal(form.images.reference_images[0].previewUrl, null); assert.equal(form.images.reference_images[0].identityType, 'real_person'); // 吸附后 seedance 不支持 resolution,不会出现在请求里 const { values } = composeParams(seedance, { sessionValues: form.params }); const payload = buildVideoRequest(seedance, { ...form, params: values }); assert.equal(payload.resolution, undefined); assert.equal(payload.image, 'task://t1#first'); assert.equal(payload.last_image, 'cos://u/last.png'); }); test('formFromTaskRequest falls back to legacy image field', () => { const form = formFromTaskRequest({ mode: 'image2video', image: 'https://x/a.png', watermark: true, }); assert.equal(form.images.first_frame.ref, 'https://x/a.png'); assert.equal(form.images.last_frame, null); assert.equal(form.params.watermark, true); });