Initial import of weiyuan frontend
This commit is contained in:
@@ -0,0 +1,424 @@
|
||||
/*
|
||||
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 test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
|
||||
import {
|
||||
computeDiscountRatio,
|
||||
pickBadgeDiscount,
|
||||
formatDiscountLabel,
|
||||
buildOfficialComparison,
|
||||
officialFillState,
|
||||
isOfficialEntryInert,
|
||||
canonicalOfficialPriceJSON,
|
||||
sanitizeOfficialPriceMap,
|
||||
tierPricingOf,
|
||||
tierPriceOf,
|
||||
buildTierComparison,
|
||||
isOfficialTierField,
|
||||
} from './officialPrice.js';
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 折扣比:实付/官方,数值层计算,加价与噪声折扣一律 null(不展示)
|
||||
// ---------------------------------------------------------------------------
|
||||
test('computeDiscountRatio basic and guard cases', () => {
|
||||
assert.equal(computeDiscountRatio(0.5, 2), 0.25);
|
||||
// 实付 ≥ 官方(含加价)→ 不展示
|
||||
assert.equal(computeDiscountRatio(2, 2), null);
|
||||
assert.equal(computeDiscountRatio(3, 2), null);
|
||||
// 9.96 折之类的噪声 → 不展示
|
||||
assert.equal(computeDiscountRatio(1.995, 2), null);
|
||||
// 非法输入
|
||||
assert.equal(computeDiscountRatio(0, 2), null);
|
||||
assert.equal(computeDiscountRatio(1, 0), null);
|
||||
assert.equal(computeDiscountRatio(NaN, 2), null);
|
||||
assert.equal(computeDiscountRatio(1, undefined), null);
|
||||
});
|
||||
|
||||
test('pickBadgeDiscount is conservative and all-or-nothing', () => {
|
||||
// 取比值最大(折扣最保守)的方向,避免夸大宣传
|
||||
assert.equal(pickBadgeDiscount([0.25, 0.5]), 0.5);
|
||||
assert.equal(pickBadgeDiscount([0.3]), 0.3);
|
||||
// 任一方向无折扣(null)→ 整体不出徽标
|
||||
assert.equal(pickBadgeDiscount([0.25, null]), null);
|
||||
assert.equal(pickBadgeDiscount([]), null);
|
||||
});
|
||||
|
||||
test('formatDiscountLabel per language', () => {
|
||||
assert.equal(formatDiscountLabel(0.25, 'zh'), '2.5折');
|
||||
assert.equal(formatDiscountLabel(0.25, 'zh-CN'), '2.5折');
|
||||
assert.equal(formatDiscountLabel(0.5, 'zh'), '5折');
|
||||
assert.equal(formatDiscountLabel(0.08, 'zh'), '0.8折');
|
||||
assert.equal(formatDiscountLabel(0.25, 'en'), '-75%');
|
||||
assert.equal(formatDiscountLabel(0.25, 'ja'), '-75%');
|
||||
// 无效比值不产生文案
|
||||
assert.equal(formatDiscountLabel(1.2, 'zh'), '');
|
||||
assert.equal(formatDiscountLabel(null, 'en'), '');
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 组装:按量 / 按次 / 按秒 × 折扣 / 加价 / 未配置
|
||||
// ---------------------------------------------------------------------------
|
||||
const perTokenRecord = {
|
||||
quota_type: 0,
|
||||
model_ratio: 1, // 实付输入 = 1×2×ratio = 2×ratio (¥/1M)
|
||||
completion_ratio: 4, // 实付输出 = 8×ratio
|
||||
};
|
||||
|
||||
test('buildOfficialComparison per-token with discount', () => {
|
||||
const c = buildOfficialComparison(
|
||||
perTokenRecord,
|
||||
{ input: 8, output: 32 },
|
||||
1, // usedGroupRatio
|
||||
);
|
||||
// 实付 2/8 = 0.25;输出 8/32 = 0.25
|
||||
assert.equal(c.discountInput, 0.25);
|
||||
assert.equal(c.discountOutput, 0.25);
|
||||
assert.equal(c.badgeDiscount, 0.25);
|
||||
assert.equal(c.officialInput, 8);
|
||||
assert.equal(c.officialOutput, 32);
|
||||
});
|
||||
|
||||
test('buildOfficialComparison respects group ratio (incl. level discount)', () => {
|
||||
const c = buildOfficialComparison(
|
||||
perTokenRecord,
|
||||
{ input: 8, output: 32 },
|
||||
0.5,
|
||||
);
|
||||
assert.equal(c.discountInput, 0.125);
|
||||
assert.equal(c.badgeDiscount, 0.125);
|
||||
});
|
||||
|
||||
test('buildOfficialComparison badge is conservative across directions', () => {
|
||||
// 输入 2.5 折、输出 5 折 → 徽标 5 折
|
||||
const c = buildOfficialComparison(
|
||||
perTokenRecord,
|
||||
{ input: 8, output: 16 },
|
||||
1,
|
||||
);
|
||||
assert.equal(c.discountInput, 0.25);
|
||||
assert.equal(c.discountOutput, 0.5);
|
||||
assert.equal(c.badgeDiscount, 0.5);
|
||||
});
|
||||
|
||||
test('buildOfficialComparison markup direction suppresses badge entirely', () => {
|
||||
// 输入打折但输出高于官方 → 整体不出徽标(防误导)
|
||||
const c = buildOfficialComparison(perTokenRecord, { input: 8, output: 4 }, 1);
|
||||
assert.equal(c.discountInput, 0.25);
|
||||
assert.equal(c.discountOutput, null);
|
||||
assert.equal(c.badgeDiscount, null);
|
||||
});
|
||||
|
||||
test('buildOfficialComparison per-call', () => {
|
||||
const record = { quota_type: 1, model_price: '0.1' };
|
||||
const c = buildOfficialComparison(record, { per_call: 0.4 }, 1);
|
||||
assert.equal(c.discountPerCall, 0.25);
|
||||
assert.equal(c.badgeDiscount, 0.25);
|
||||
assert.equal(c.officialPerCall, 0.4);
|
||||
// 加价 → null 徽标
|
||||
const up = buildOfficialComparison(record, { per_call: 0.05 }, 1);
|
||||
assert.equal(up.badgeDiscount, null);
|
||||
});
|
||||
|
||||
test('buildOfficialComparison unsupported / missing cases', () => {
|
||||
// 官方价未配置
|
||||
assert.equal(buildOfficialComparison(perTokenRecord, undefined, 1), null);
|
||||
// 按秒分档暂不支持
|
||||
assert.equal(
|
||||
buildOfficialComparison({ quota_type: 2 }, { input: 1 }, 1),
|
||||
null,
|
||||
);
|
||||
// 官方价对象存在但与计费类型不匹配(按量模型只配了 per_call)
|
||||
assert.equal(
|
||||
buildOfficialComparison(perTokenRecord, { per_call: 1 }, 1),
|
||||
null,
|
||||
);
|
||||
// 分组倍率非法
|
||||
assert.equal(buildOfficialComparison(perTokenRecord, { input: 8 }, 0), null);
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 管理端维护辅助:填写完整度 / 稳定序列化 / 导入校验
|
||||
// ---------------------------------------------------------------------------
|
||||
test('officialFillState treats half-filled per-token models as partial', () => {
|
||||
assert.equal(officialFillState(0, undefined), 'empty');
|
||||
assert.equal(officialFillState(0, {}), 'empty');
|
||||
assert.equal(officialFillState(0, { input: 8 }), 'partial');
|
||||
assert.equal(officialFillState(0, { output: 8 }), 'partial');
|
||||
assert.equal(officialFillState(0, { input: 8, output: 16 }), 'filled');
|
||||
// 非正数不算填写(与后端校验同口径)
|
||||
assert.equal(officialFillState(0, { input: 0, output: 16 }), 'partial');
|
||||
// 按次计费
|
||||
assert.equal(officialFillState(1, {}), 'empty');
|
||||
assert.equal(officialFillState(1, { per_call: 0.4 }), 'filled');
|
||||
// 按次模型误配了 token 价 → 仍算未填(市场页不会用)
|
||||
assert.equal(officialFillState(1, { input: 1 }), 'empty');
|
||||
// 按秒分档不支持
|
||||
assert.equal(officialFillState(2, { input: 1 }), 'na');
|
||||
});
|
||||
|
||||
test('canonicalOfficialPriceJSON is key-order and noise insensitive', () => {
|
||||
const a = { 'b-model': { output: 2, input: 1 }, 'a-model': { per_call: 3 } };
|
||||
const b = { 'a-model': { per_call: 3 }, 'b-model': { input: 1, output: 2 } };
|
||||
assert.equal(canonicalOfficialPriceJSON(a), canonicalOfficialPriceJSON(b));
|
||||
// 空条目 / 非正数字段被丢弃,等价于「未填」
|
||||
assert.equal(canonicalOfficialPriceJSON({ m: {} }), '{}');
|
||||
assert.equal(canonicalOfficialPriceJSON({ m: { input: 0 } }), '{}');
|
||||
assert.equal(
|
||||
canonicalOfficialPriceJSON({ m: { input: 1, output: 0 } }),
|
||||
'{"m":{"input":1}}',
|
||||
);
|
||||
assert.equal(canonicalOfficialPriceJSON(undefined), '{}');
|
||||
});
|
||||
|
||||
test('sanitizeOfficialPriceMap mirrors backend validation', () => {
|
||||
const ok = sanitizeOfficialPriceMap({
|
||||
'gpt-4o': { input: '2.5', output: 10 },
|
||||
'sd-3': { per_call: 0.4 },
|
||||
});
|
||||
assert.deepEqual(ok.errors, []);
|
||||
assert.deepEqual(ok.value, {
|
||||
'gpt-4o': { input: 2.5, output: 10 },
|
||||
'sd-3': { per_call: 0.4 },
|
||||
});
|
||||
// 非法值 / 空条目 / 未知字段一律报错,不静默丢弃
|
||||
const bad = sanitizeOfficialPriceMap({
|
||||
a: { input: 0 },
|
||||
b: {},
|
||||
c: { percall: 1 },
|
||||
d: 'x',
|
||||
});
|
||||
assert.equal(bad.errors.length, 4);
|
||||
assert.deepEqual(bad.value, {});
|
||||
// 顶层结构错误
|
||||
assert.equal(sanitizeOfficialPriceMap([]).errors.length, 1);
|
||||
assert.equal(sanitizeOfficialPriceMap(null).errors.length, 1);
|
||||
});
|
||||
|
||||
test('isOfficialEntryInert flags silently dead entries', () => {
|
||||
// 模型改成按次计费后,旧的 token 价整条不生效
|
||||
assert.equal(isOfficialEntryInert(1, { input: 8, output: 24 }), true);
|
||||
// 模型改成按量计费后,旧的按次价整条不生效
|
||||
assert.equal(isOfficialEntryInert(0, { per_call: 0.4 }), true);
|
||||
// 按秒分档:填什么都不生效
|
||||
assert.equal(isOfficialEntryInert(2, { input: 8, output: 24 }), true);
|
||||
// 正常配置 / 只填一侧 / 没填 → 不是 inert
|
||||
assert.equal(isOfficialEntryInert(0, { input: 8, output: 24 }), false);
|
||||
assert.equal(isOfficialEntryInert(0, { input: 8 }), false);
|
||||
assert.equal(isOfficialEntryInert(1, { per_call: 0.4 }), false);
|
||||
assert.equal(isOfficialEntryInert(0, undefined), false);
|
||||
assert.equal(isOfficialEntryInert(2, undefined), false);
|
||||
// 非正数不算「填了值」
|
||||
assert.equal(isOfficialEntryInert(1, { input: 0 }), false);
|
||||
});
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 分档计费(按秒 / 按分辨率):逐档官方价
|
||||
// ---------------------------------------------------------------------------
|
||||
const videoRecord = {
|
||||
quota_type: 2,
|
||||
video_second_pricing: {
|
||||
tier_param: 'resolution',
|
||||
tiers: [
|
||||
{ key: '720p', label: '720P', price_per_second: 0.1 },
|
||||
{ key: '1080p', label: '1080P', price_per_second: 0.2 },
|
||||
],
|
||||
},
|
||||
};
|
||||
const imageRecord = {
|
||||
quota_type: 3,
|
||||
image_resolution_pricing: {
|
||||
tiers: [{ key: 'hd', label: 'HD', price_per_image: 0.05 }],
|
||||
},
|
||||
};
|
||||
|
||||
test('tierPricingOf maps both tiered billing types', () => {
|
||||
assert.equal(tierPricingOf(videoRecord).field, 'per_second');
|
||||
assert.deepEqual(tierPricingOf(videoRecord).tiers[1], {
|
||||
key: '1080p',
|
||||
label: '1080P',
|
||||
basePrice: 0.2,
|
||||
});
|
||||
assert.equal(tierPricingOf(imageRecord).field, 'per_image');
|
||||
assert.equal(tierPricingOf({ quota_type: 0 }), null);
|
||||
// 分档配置被清空的模型:不是 null,而是空档位列表(页面据此提示"无档位可填")
|
||||
assert.deepEqual(tierPricingOf({ quota_type: 2 }).tiers, []);
|
||||
});
|
||||
|
||||
test('tierPriceOf is case/whitespace insensitive', () => {
|
||||
const entry = { per_second: { '1080p': 0.5 } };
|
||||
assert.equal(tierPriceOf(entry, 'per_second', '1080P'), 0.5);
|
||||
assert.equal(tierPriceOf(entry, 'per_second', ' 1080p '), 0.5);
|
||||
assert.equal(tierPriceOf(entry, 'per_second', '720p'), undefined);
|
||||
// 非正数不算填写(与后端校验同口径)
|
||||
assert.equal(
|
||||
tierPriceOf({ per_second: { a: 0 } }, 'per_second', 'a'),
|
||||
undefined,
|
||||
);
|
||||
assert.equal(tierPriceOf(undefined, 'per_second', 'a'), undefined);
|
||||
});
|
||||
|
||||
test('buildTierComparison computes per-tier discount', () => {
|
||||
const c = buildTierComparison(videoRecord, {
|
||||
per_second: { '720P': 0.4, '1080p': 0.2 },
|
||||
});
|
||||
assert.equal(c[0].discount, 0.25); // 0.1 / 0.4
|
||||
// 站内价 = 官方价 → 无折扣(null),不是"未填"(undefined)
|
||||
assert.equal(c[1].discount, null);
|
||||
// 某档未填官方价 → undefined,逐档独立,不影响其他档
|
||||
const partial = buildTierComparison(videoRecord, {
|
||||
per_second: { '720p': 0.4 },
|
||||
});
|
||||
assert.equal(partial[1].official, undefined);
|
||||
assert.equal(partial[1].discount, undefined);
|
||||
assert.equal(buildTierComparison({ quota_type: 1 }, {}), null);
|
||||
});
|
||||
|
||||
test('officialFillState requires every tier of a tiered model', () => {
|
||||
const tierKeys = ['720p', '1080p'];
|
||||
assert.equal(officialFillState(2, undefined, { tierKeys }), 'empty');
|
||||
assert.equal(
|
||||
officialFillState(2, { per_second: { '720p': 1 } }, { tierKeys }),
|
||||
'partial',
|
||||
);
|
||||
assert.equal(
|
||||
officialFillState(
|
||||
2,
|
||||
{ per_second: { '720P': 1, '1080p': 2 } },
|
||||
{ tierKeys },
|
||||
),
|
||||
'filled',
|
||||
);
|
||||
// 按张分档同理,且不吃 per_second 的值
|
||||
assert.equal(
|
||||
officialFillState(3, { per_second: { hd: 1 } }, { tierKeys: ['hd'] }),
|
||||
'empty',
|
||||
);
|
||||
assert.equal(
|
||||
officialFillState(3, { per_image: { HD: 1 } }, { tierKeys: ['hd'] }),
|
||||
'filled',
|
||||
);
|
||||
// 没有任何档位可填 → NA(不能报"未填写",管理员无从下手)
|
||||
assert.equal(officialFillState(2, {}, { tierKeys: [] }), 'na');
|
||||
assert.equal(officialFillState(2, {}), 'na');
|
||||
});
|
||||
|
||||
test('tier entries participate in inert / canonical / sanitize', () => {
|
||||
// 按秒模型配了 token 价 → 整条不生效
|
||||
assert.equal(isOfficialEntryInert(2, { input: 8 }), true);
|
||||
assert.equal(isOfficialEntryInert(2, { per_second: { '1080p': 1 } }), false);
|
||||
// 按量模型配了分档价 → 同样不生效
|
||||
assert.equal(isOfficialEntryInert(0, { per_second: { '1080p': 1 } }), true);
|
||||
// 空档位映射不算"填了值"
|
||||
assert.equal(isOfficialEntryInert(0, { per_second: {} }), false);
|
||||
|
||||
// 序列化:档位键归一 + 定序,键序与大小写差异不产生"未保存修改"
|
||||
assert.equal(
|
||||
canonicalOfficialPriceJSON({
|
||||
m: { per_second: { '1080P': 2, '720p': 1 } },
|
||||
}),
|
||||
canonicalOfficialPriceJSON({
|
||||
m: { per_second: { '720P': 1, '1080p': 2 } },
|
||||
}),
|
||||
);
|
||||
assert.equal(
|
||||
canonicalOfficialPriceJSON({ m: { per_second: { '1080P': 2 } } }),
|
||||
'{"m":{"per_second":{"1080p":2}}}',
|
||||
);
|
||||
// 非正数档位被丢弃;丢空后整条不落盘
|
||||
assert.equal(
|
||||
canonicalOfficialPriceJSON({ m: { per_image: { hd: 0 } } }),
|
||||
'{}',
|
||||
);
|
||||
|
||||
// 导入:结构/数值/重键校验
|
||||
const ok = sanitizeOfficialPriceMap({
|
||||
v: { per_second: { '1080P': '0.5' } },
|
||||
});
|
||||
assert.deepEqual(ok.errors, []);
|
||||
assert.deepEqual(ok.value, { v: { per_second: { '1080p': 0.5 } } });
|
||||
assert.equal(
|
||||
sanitizeOfficialPriceMap({ v: { per_second: 0.5 } }).errors.length,
|
||||
1,
|
||||
);
|
||||
assert.equal(
|
||||
sanitizeOfficialPriceMap({ v: { per_second: { a: 0 } } }).errors.length,
|
||||
1,
|
||||
);
|
||||
assert.equal(
|
||||
sanitizeOfficialPriceMap({ v: { per_second: { hd: 1, HD: 2 } } }).errors
|
||||
.length,
|
||||
1,
|
||||
);
|
||||
});
|
||||
|
||||
test('tierPricingOf drops tiers with an unusable (empty) key', () => {
|
||||
// 收入侧允许空 key(parseVideoPricing 无校验),但空键的牌价存不下来
|
||||
//(后端拒收 / canonical 丢弃)。若照样渲染输入框,用户填的值会静默消失。
|
||||
const rec = {
|
||||
quota_type: 2,
|
||||
video_second_pricing: {
|
||||
tiers: [
|
||||
{ key: '', price_per_second: 3 },
|
||||
{ key: ' ', price_per_second: 4 },
|
||||
{ key: '1080p', price_per_second: 0.2 },
|
||||
],
|
||||
},
|
||||
};
|
||||
assert.deepEqual(
|
||||
tierPricingOf(rec).tiers.map((t) => t.key),
|
||||
['1080p'],
|
||||
);
|
||||
});
|
||||
|
||||
test('isOfficialTierField distinguishes map-valued fields', () => {
|
||||
assert.equal(isOfficialTierField('per_second'), true);
|
||||
assert.equal(isOfficialTierField('per_image'), true);
|
||||
assert.equal(isOfficialTierField('per_call'), false);
|
||||
assert.equal(isOfficialTierField('input'), false);
|
||||
});
|
||||
|
||||
test('per_image stays applicable to per-token models', () => {
|
||||
// Responses 内嵌出图 = token 费 + 出图调用费相加,成本侧同口径按 per_image 记账。
|
||||
// 把它标成「配置不生效」会诱导管理员删掉一条正在记账的基准。
|
||||
assert.equal(
|
||||
isOfficialEntryInert(0, { input: 8, per_image: { hd: 1 } }),
|
||||
false,
|
||||
);
|
||||
assert.equal(isOfficialEntryInert(0, { per_image: { hd: 1 } }), false);
|
||||
// 按秒价对按量模型确实不生效,仍要点名
|
||||
assert.equal(isOfficialEntryInert(0, { per_second: { '1080p': 1 } }), true);
|
||||
});
|
||||
|
||||
test('canonicalOfficialPriceJSON orders tier keys after normalization', () => {
|
||||
// 原始键序('Z' < 'a')与归一后键序('a' < 'z')相反:不按归一键重排的话,
|
||||
// 同一份配置会序列化出两种字节串,页面永远挂着「有未保存的修改」
|
||||
assert.equal(
|
||||
canonicalOfficialPriceJSON({ m: { per_second: { Z: 1, a: 2 } } }),
|
||||
'{"m":{"per_second":{"a":2,"z":1}}}',
|
||||
);
|
||||
});
|
||||
|
||||
test('sanitizeOfficialPriceMap rejects array-shaped tier maps', () => {
|
||||
// 数组不拦的话会被当对象处理,静默产出档位键 "0"/"1"
|
||||
const res = sanitizeOfficialPriceMap({ v: { per_second: [1, 2] } });
|
||||
assert.equal(res.errors.length, 1);
|
||||
assert.deepEqual(res.value, {});
|
||||
});
|
||||
Reference in New Issue
Block a user