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

154 lines
4.9 KiB
JavaScript

import assert from 'node:assert/strict';
import test from 'node:test';
import {
ERROR_ACTIONS,
budgetShortfallOf,
classifyCreationError,
describeCreationError,
extractCreationError,
moderationInputOf,
} from './creationError.js';
const axiosError = (status, data) => ({ response: { status, data } });
const fmt = (k, p) =>
p ? k.replace(/\{\{(\w+)\}\}/g, (_, name) => String(p[name])) : k;
test('extract from axios error, body, nested error and string', () => {
assert.deepEqual(
extractCreationError(axiosError(400, { code: 'x', message: 'm' })),
{ code: 'x', message: 'm', status: 400, type: '' },
);
assert.deepEqual(
extractCreationError({
data: { error: { message: 'blocked', type: 'content_guard_error' } },
}),
{ code: '', message: 'blocked', status: 0, type: 'content_guard_error' },
);
assert.equal(extractCreationError('timeout').message, 'timeout');
});
test('concurrency limit', () => {
const a = classifyCreationError(
axiosError(429, { code: 'too_many_running_tasks' }),
{ running: 3 },
);
assert.equal(a.category, 'concurrency');
assert.equal(a.action, ERROR_ACTIONS.WAIT);
assert.deepEqual(a.titleParams, { n: 3 });
assert.equal(
classifyCreationError(axiosError(429, {})).category,
'concurrency',
);
});
test('balance → topup, org budget → contact admin', () => {
const b = classifyCreationError({ code: 'insufficient_user_quota' });
assert.equal(b.category, 'balance');
assert.equal(b.action, ERROR_ACTIONS.TOPUP);
assert.equal(classifyCreationError('用户余额不足').action, 'topup');
const o = classifyCreationError('部门预算已达上限');
assert.equal(o.category, 'budget');
assert.equal(o.passthrough, true);
assert.equal(o.action, ERROR_ACTIONS.CONTACT_ADMIN);
assert.equal(describeCreationError(fmt, o).title, '部门预算已达上限');
});
test('moderation names the triggering input when the backend says so', () => {
const p = classifyCreationError({
code: 'sensitive_words_detected',
message: 'prompt contains sensitive words',
});
assert.equal(p.category, 'moderation');
assert.deepEqual(p.input, { key: 'prompt' });
assert.equal(p.action, ERROR_ACTIONS.EDIT_PROMPT);
const ref = classifyCreationError(
'reference_images[1] rejected by moderation',
);
assert.deepEqual(ref.input, { key: 'reference_images', index: 1 });
assert.equal(ref.action, ERROR_ACTIONS.REPLACE_IMAGE);
assert.equal(
describeCreationError(fmt, ref).detail,
'参考图 2触发了内容安全策略,请更换图片',
);
const first = classifyCreationError('首帧图片审核未通过');
assert.deepEqual(first.input, { key: 'first_frame' });
const unknown = classifyCreationError({
data: { error: { message: 'blocked', type: 'content_guard_error' } },
});
assert.equal(unknown.category, 'moderation');
assert.equal(unknown.input, null);
assert.equal(moderationInputOf('image 1024x1024 nsfw')?.key, 'image');
});
test('timeout / busy → retry', () => {
assert.equal(
classifyCreationError('context deadline exceeded').category,
'timeout',
);
assert.equal(
classifyCreationError({ code: 'fallback_total_timeout' }).action,
ERROR_ACTIONS.RETRY,
);
assert.equal(
classifyCreationError(axiosError(503, { code: 'channel_not_enabled' }))
.category,
'busy',
);
});
test('expired reference image', () => {
const e = classifyCreationError(
axiosError(400, {
code: 'invalid_image',
message: '原图已失效,请重新上传',
}),
);
assert.equal(e.category, 'image_expired');
assert.equal(e.action, ERROR_ACTIONS.REPLACE_IMAGE);
});
test('validation passes through, unknown falls back to generic + raw detail', () => {
const v = classifyCreationError(
axiosError(400, {
code: 'unsupported_parameter',
message: '时长不支持 8 秒',
}),
);
assert.equal(v.category, 'validation');
assert.equal(describeCreationError(fmt, v).title, '时长不支持 8 秒');
const u = classifyCreationError('upstream said: weird');
assert.equal(u.category, 'unknown');
const d = describeCreationError(fmt, u);
assert.equal(d.title, '生成失败');
assert.equal(d.detail, 'upstream said: weird');
assert.equal(d.actionLabel, '重试');
});
test('budgetShortfallOf maps budget_source', () => {
assert.equal(budgetShortfallOf({ balance_enough: true }), null);
assert.equal(budgetShortfallOf(null), null);
assert.deepEqual(
budgetShortfallOf({ balance_enough: false, budget_source: 'user' }),
{ personal: true, source: 'user', labelKey: '余额不足' },
);
assert.equal(
budgetShortfallOf({
balance_enough: false,
budget_source: 'org_budget:department',
}).labelKey,
'部门预算不足',
);
assert.equal(
budgetShortfallOf({ balance_enough: false, budget_source: 'org' }).personal,
false,
);
assert.equal(
budgetShortfallOf({ balance_enough: false, budget_source: 'token' })
.labelKey,
'令牌额度不足',
);
});