Files
monkey-ai-web/src/hooks/video/videoCreation.test.js
T
qiubaishen 6160a23117 sync: 同步总仓库 luoguang/monkey-ai weiyuan@bfdfb339 的 web/ 改动
同步基准:总仓库 a2ca13cb(= 本仓库初始导入 d8f3aee2)
范围:a2ca13cb..bfdfb339 中触及 web/ 的提交(不含 web/.claude/settings.local.json)

- 8490ac02 feat(aliyun): integrate Wan 3 and Qwen Image models
- 2aff08ec perf(analytics): 运营看板概览改读汇总表,首屏从 19s 降到 0.4s
- dc69335e feat(experience): 体验中心图像/视频创作升级(能力 schema、估价、新创作台)
- b059c657 fix(org): 下线 /org/create 自助加入企业页与 POST /api/org/join
- 4fcf47e6 fix(i18n): 修复中/英/韩/繁体多语言错乱,补齐日/越/泰译文
- e4c301d5 fix: merge enterprise task record date filters
- c9369b95 feat: expose enterprise image and video task records
- a410fee5 feat(claude): 无断点请求自动注入 cache_control 断点(方案 2)
- 521b3376 feat(claude): 缓存命中率优化——上游粘性路由 + 缓存时长强制改写
- 73b615dd feat(pricing): 企业成员不论登录身份均按所属企业等级计价与扣费
- f73f55c3 feat(client): 新增 WeyAi 版本说明管理与公开读取接口
- 8cdf1994 fix(topup): reset amount when switching from PayPal
- 93a4ac34 feat(baidu): configure Seedance liveness header by channel
- cb1818c5 fix(weyai): 按评审修正内容清理竞态、默认适用端与中文退回(M21)
- 04214d83 feat(weyai): WeyAi 内容管理与客户端内容接口(M21 第二部分)
- e3c4a57c feat(weyai): 设置页新增 WeyAi 页签与内容专用 COS 桶(M21 第一部分)
- b5ee8e70 fix(auth): 会话角色/状态以 DB 为准,修复提升管理员后管理接口报权限不足
- e4618fc2 fix: 修复企业看板日志聚合死循环导致 CPU 打满,并降低看板查询开销
- 584aad52 style(web): 个人中心侧边栏按 prettier 换行
- b8d87064 fix: 小程序教程隐藏充值后保留原步骤编号,企业充值判断直接用 isMiniProgramWebView
- eee7f5b2 fix: 小程序里企业首页不落到充值页,隐藏钱包入口与教程充值步骤
- 8b254d49 fix: 小程序里隐藏企业充值入口与页面
- a67661b4 fix(web): 小程序里个人中心默认页跳过充值和发票,避免来回跳转
- cff6c543 feat(web): WeyAi 小程序里打开个人中心时不出现充值和开票
- c6c99eb6 feat(billing): Claude 全部缓存写入统一按 1h 倍率计费
- 0699d1bf fix(web): 小程序 web-view 里注册页同样只显示站内方式
- 04853331 feat(web): WeyAi 小程序 web-view 里登录页只显示站内登录方式
2026-11-10 11:05:34 +08:00

363 lines
10 KiB
JavaScript

/*
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 <https://www.gnu.org/licenses/>.
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);
});