Files
monkey-ai-web/src/hooks/creation/creationCompare.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

370 lines
11 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
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 {
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'],
);
});