Files
monkey-ai-web/src/components/creation/ModelPicker/modelPickerUtils.test.js
T
zhanjunhao 2aa9d73325 chore(repo): 移除重复版权文件头并纳入共享单测
集中保留原版权声明,移除文件头检查和全局测试忽略规则。验证:全量单测、受影响用例复测及静态检查通过。
2026-11-20 11:23:49 +08:00

242 lines
6.6 KiB
JavaScript

import assert from 'node:assert/strict';
import test from 'node:test';
import {
OTHER_GROUP,
RECENT_GROUP,
RECENT_LIMIT,
badgesOf,
capabilityTags,
flattenOptions,
groupModels,
incompatibilityOf,
loadRecent,
matchesQuery,
moveActive,
pushRecent,
recentKeyOf,
unitPriceOf,
} from './modelPickerUtils.js';
const seedance = {
model: 'doubao-seedance-1-5-pro',
display_name: 'Seedance 1.5 Pro',
series: 'seedance',
badges: ['recommended', 'audio'],
description: '电影级运镜',
modes: ['text2video', 'image2video', 'first_last_frame'],
params: [
{
key: 'resolution',
type: 'chips',
options: ['480P', '720P', '1080P'],
default: '720P',
},
{
key: 'duration',
type: 'segmented',
options: [{ value: 5 }, { value: 10 }],
default: 5,
},
{ key: 'generate_audio', type: 'switch' },
],
pricing: {
unit: 'second',
by: 'resolution',
price: 0.05,
prices: { '480P': 0.02, '720P': 0.05, '1080P': 0.1 },
currency: 'CNY',
},
};
const kling = {
model: 'kling-v2',
series: 'kling',
modes: ['text2video', 'image2video'],
params: [{ key: 'duration', type: 'segmented', options: [5] }],
};
const wan = {
model: 'wan2.5',
series: 'seedance',
modes: ['text2video', 'reference2video'],
params: [],
badges: ['new'],
};
const misc = { model: 'misc-video', modes: ['text2video'], params: [] };
const memoryStorage = () => {
const data = {};
return {
data,
getItem: (k) => (k in data ? data[k] : null),
setItem: (k, v) => {
data[k] = String(v);
},
};
};
test('分组:按系列首次出现顺序,"其他"在最后,最近使用置顶', () => {
const groups = groupModels([misc, seedance, kling, wan], {
recent: ['kling-v2', 'gone-model'],
});
assert.deepEqual(
groups.map((g) => g.key),
[RECENT_GROUP, 'seedance', 'kling', OTHER_GROUP],
);
assert.deepEqual(
groups[0].models.map((m) => m.model),
['kling-v2'],
);
assert.deepEqual(
groups[1].models.map((m) => m.model),
['doubao-seedance-1-5-pro', 'wan2.5'],
);
// vendor 作为系列缺失时的回退
const byVendor = groupModels([{ model: 'x', vendor: 'OpenAI' }]);
assert.equal(byVendor[0].key, 'OpenAI');
});
test('搜索:多词匹配名称 / 展示名 / 介绍,搜索时不显示最近使用', () => {
assert.equal(matchesQuery(seedance, 'seedance pro'), true);
assert.equal(matchesQuery(seedance, '运镜'), true);
assert.equal(matchesQuery(seedance, 'kling'), false);
const groups = groupModels([seedance, kling], {
recent: ['kling-v2'],
query: 'kling',
});
assert.deepEqual(
groups.map((g) => g.key),
['kling'],
);
});
test('扁平选项 id 唯一(最近使用与系列中重复出现的模型)', () => {
const groups = groupModels([seedance, kling], { recent: ['kling-v2'] });
const opts = flattenOptions(groups, 'p');
assert.equal(opts.length, 3);
assert.equal(new Set(opts.map((o) => o.id)).size, 3);
assert.ok(opts.every((o) => /^[\w-]+$/.test(o.id)));
});
test('键盘移动循环', () => {
assert.equal(moveActive(3, 2, 1), 0);
assert.equal(moveActive(3, 0, -1), 2);
assert.equal(moveActive(3, -1, 1), 0);
assert.equal(moveActive(3, -1, -1), 2);
assert.equal(moveActive(0, 0, 1), -1);
});
test('能力标签:视频由模式 / 参数推导,含最长时长', () => {
assert.deepEqual(
capabilityTags('video', seedance).map((t) => t.key),
['i2v', 'flf', 'audio', 'max_duration'],
);
assert.deepEqual(
capabilityTags('video', seedance).find((t) => t.key === 'max_duration')
.values,
{ n: 10 },
);
assert.deepEqual(
capabilityTags('video', wan).map((t) => t.key),
['reference'],
);
// 因系统配置禁用的模式不计入
assert.deepEqual(
capabilityTags('video', {
modes: ['text2video'],
disabled_modes: [{ mode: 'image2video' }],
}),
[],
);
});
test('能力标签:图像编辑与多图参考', () => {
const img = {
modes: ['text2image', 'edit'],
inputs: { reference_images: { modes: ['edit'], max: 4 } },
};
assert.deepEqual(
capabilityTags('image', img).map((t) => [t.key, t.values?.n]),
[
['edit', undefined],
['reference', 4],
],
);
assert.deepEqual(capabilityTags('image', { modes: ['text2image'] }), []);
});
test('徽标只显示推荐 / 新', () => {
assert.deepEqual(
badgesOf(seedance).map((b) => b.label),
['推荐'],
);
assert.deepEqual(
badgesOf(wan).map((b) => b.label),
['新'],
);
});
test('单价:按当前参数取档位,缺省取参数默认档,无定价返回 null', () => {
assert.deepEqual(unitPriceOf(seedance, { resolution: '1080p' }), {
amount: 0.1,
currency: 'CNY',
unit: 'second',
unitLabel: '秒',
tier: '1080P',
});
assert.equal(unitPriceOf(seedance, {}).tier, '720P');
// 档位缺失:退回默认价,不显示档位
const r = unitPriceOf(seedance, { resolution: '4K' });
assert.equal(r.amount, 0.05);
assert.equal(r.tier, '');
assert.equal(unitPriceOf(kling, {}), null);
assert.equal(
unitPriceOf({ pricing: { unit: 'task', price: 0, currency: 'CNY' } }),
null,
);
assert.equal(
unitPriceOf({ pricing: { unit: 'image', price: 0.2, currency: 'USD' } })
.unitLabel,
'张',
);
});
test('不兼容原因:非文生模式下不支持该模式的模型置灰并说明', () => {
assert.equal(incompatibilityOf('video', kling, { mode: 'text2video' }), null);
assert.equal(
incompatibilityOf('video', seedance, { mode: 'first_last_frame' }),
null,
);
assert.deepEqual(
incompatibilityOf('video', kling, { mode: 'first_last_frame' }),
{ key: '不支持{{mode}}', values: { mode: '首尾帧' } },
);
assert.deepEqual(
incompatibilityOf('image', { modes: ['text2image'] }, { mode: 'edit' })
.values,
{ mode: '图生图' },
);
});
test('最近使用:置顶去重、限长,存储异常不抛错', () => {
const storage = memoryStorage();
let list = [];
['a', 'b', 'c', 'd', 'e', 'b'].forEach((n) => {
list = pushRecent(storage, 'video', n, list);
});
assert.equal(list.length, RECENT_LIMIT);
assert.deepEqual(list, ['b', 'e', 'd', 'c']);
assert.deepEqual(loadRecent(storage, 'video'), list);
assert.deepEqual(loadRecent(storage, 'image'), []);
storage.data[recentKeyOf('image')] = 'not json';
assert.deepEqual(loadRecent(storage, 'image'), []);
const throwing = {
getItem: () => {
throw new Error('x');
},
setItem: () => {
throw new Error('x');
},
};
assert.deepEqual(loadRecent(throwing, 'video'), []);
assert.deepEqual(pushRecent(throwing, 'video', 'a', []), ['a']);
});