8a48765a46
集中保留原版权声明,移除文件头检查和全局测试忽略规则。验证:全量单测、受影响用例复测及静态检查通过。
238 lines
7.6 KiB
JavaScript
238 lines
7.6 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import test from 'node:test';
|
|
import {
|
|
TABS,
|
|
emptySettings,
|
|
normalizeSettings,
|
|
serializeSettings,
|
|
sanitizeMetricInput,
|
|
splitAmount,
|
|
joinAmount,
|
|
metricPreview,
|
|
validateField,
|
|
validateSettings,
|
|
validateImageFile,
|
|
resolveImageUrl,
|
|
} from '../../src/pages/WebsiteSettings/model.js';
|
|
|
|
const validValues = () => ({
|
|
insights: {
|
|
categories: [{ id: 'news', name: '新闻' }],
|
|
articles: [
|
|
{
|
|
id: 'article-1',
|
|
title: 'News',
|
|
summary: 'Summary',
|
|
category: 'news',
|
|
date: '2026-11-10',
|
|
body: '<p>Content</p>',
|
|
related: [],
|
|
tag: '',
|
|
},
|
|
],
|
|
},
|
|
paidCustomers: '300',
|
|
dailyTokens: '500亿',
|
|
costReduction: '20',
|
|
teamMembers: '50',
|
|
researchRatio: '60',
|
|
headquarters: '珠海 / 广州',
|
|
serviceNetwork: '辐射全国',
|
|
businessPhone: '020-88888888',
|
|
businessEmail: 'business@example.com',
|
|
wechatQrCode: 'https://cdn.example.com/qr.png',
|
|
address: '广州市天河区示例路 1 号',
|
|
});
|
|
|
|
test('final save serializes rich text and structured insights exactly once', () => {
|
|
const settings = {
|
|
...validValues(),
|
|
insights: {
|
|
categories: [{ id: 'company', name: '公司动态' }],
|
|
articles: [
|
|
{
|
|
id: 'news',
|
|
title: ' 标题 ',
|
|
category: 'company',
|
|
date: '2026-10-31',
|
|
summary: '摘要',
|
|
tag: '',
|
|
body: '<h2>章节</h2><p><strong>正文</strong></p>',
|
|
related: [],
|
|
},
|
|
],
|
|
},
|
|
};
|
|
const serialized = serializeSettings(settings);
|
|
assert.equal(typeof serialized, 'string');
|
|
const decoded = JSON.parse(serialized);
|
|
assert.equal(decoded.insights.articles[0].title, '标题');
|
|
assert.equal(
|
|
decoded.insights.articles[0].body,
|
|
settings.insights.articles[0].body,
|
|
);
|
|
assert.ok(Array.isArray(decoded.insights.articles));
|
|
assert.ok(Array.isArray(decoded.insights.articles[0].related));
|
|
assert.equal(settings.insights.articles[0].title, ' 标题 ');
|
|
});
|
|
|
|
test('backend upload mode rejects local preview data URLs', () => {
|
|
assert.ok(validateField('wechatQrCode', 'data:image/png;base64,AAAA'));
|
|
assert.ok(validateField('wechatQrCode', 'data:text/html;base64,AAAA'));
|
|
assert.ok(validateField('wechatQrCode', 'data:image/svg+xml;base64,AAAA'));
|
|
});
|
|
|
|
test('validates all tabs using a flat payload with one shared team count', () => {
|
|
assert.deepEqual(validateSettings(validValues()), {});
|
|
assert.equal(Object.keys(validateSettings(emptySettings())).length, 11);
|
|
assert.equal(validateSettings(emptySettings()).insights, undefined);
|
|
assert.ok(TABS[0].fields.includes('teamMembers'));
|
|
assert.ok(TABS[1].fields.includes('teamMembers'));
|
|
assert.equal(
|
|
Object.keys(emptySettings()).filter((key) => key === 'teamMembers').length,
|
|
1,
|
|
);
|
|
const values = validValues();
|
|
values.address = ' ';
|
|
assert.ok(validateSettings(values).address);
|
|
});
|
|
|
|
test('global saving allows an empty article list for the website empty state', () => {
|
|
assert.deepEqual(
|
|
validateSettings({
|
|
...validValues(),
|
|
insights: emptySettings().insights,
|
|
}),
|
|
{},
|
|
);
|
|
assert.ok(
|
|
validateSettings({
|
|
...validValues(),
|
|
insights: {
|
|
...emptySettings().insights,
|
|
articles: [{ ...validValues().insights.articles[0], category: 'all' }],
|
|
},
|
|
}).insights,
|
|
);
|
|
assert.deepEqual(validateSettings(validValues()), {});
|
|
});
|
|
|
|
test('metrics store plain numbers and reject units typed by hand', () => {
|
|
for (const value of ['1', '50', '300'])
|
|
assert.equal(validateField('teamMembers', value), '');
|
|
for (const value of ['-1', '0', '01', '1.2', 'abc', '50+'])
|
|
assert.ok(validateField('teamMembers', value));
|
|
for (const value of ['500亿', '0.5万', '12.25亿'])
|
|
assert.equal(validateField('dailyTokens', value), '');
|
|
for (const value of [
|
|
'0亿',
|
|
'500',
|
|
'-3亿',
|
|
'500亿abc',
|
|
'500亿+',
|
|
'05亿',
|
|
'1.234亿',
|
|
])
|
|
assert.ok(validateField('dailyTokens', value));
|
|
for (const value of ['0', '100', '99.99', '0.5'])
|
|
assert.equal(validateField('researchRatio', value), '');
|
|
for (const value of ['101', '-1', '60.123', '↓20', '20%', '007'])
|
|
assert.ok(validateField('costReduction', value));
|
|
});
|
|
|
|
test('metric helpers sanitize input, split units and preview the website text', () => {
|
|
assert.equal(sanitizeMetricInput(' 300+ '), '300');
|
|
assert.equal(sanitizeMetricInput('12。5%'), '12.5');
|
|
assert.equal(sanitizeMetricInput('1,000'), '1000');
|
|
assert.deepEqual(splitAmount('500万'), { number: '500', unit: '万' });
|
|
assert.deepEqual(splitAmount(''), { number: '', unit: '亿' });
|
|
assert.equal(joinAmount('500', '亿'), '500亿');
|
|
assert.equal(joinAmount('', '万'), '');
|
|
assert.equal(metricPreview('paidCustomers', '300'), '300+');
|
|
assert.equal(metricPreview('dailyTokens', '500亿'), '500亿+');
|
|
assert.equal(metricPreview('costReduction', '20'), '↓20%');
|
|
assert.equal(metricPreview('researchRatio', '60'), '60%');
|
|
assert.equal(metricPreview('teamMembers', '50+'), '');
|
|
assert.equal(metricPreview('headquarters', '珠海'), '');
|
|
});
|
|
|
|
test('checks business telephone and email formats', () => {
|
|
for (const value of [
|
|
'13800138000',
|
|
'+86 13800138000',
|
|
'020-88888888',
|
|
'01088888888转123',
|
|
'400-123-4567',
|
|
])
|
|
assert.equal(validateField('businessPhone', value), '');
|
|
for (const value of ['123', 'abcdefgh', '138001380001', '100-123-4567'])
|
|
assert.ok(validateField('businessPhone', value));
|
|
for (const value of ['sales@example.com', 'sales+demo@sub.example.cn'])
|
|
assert.equal(validateField('businessEmail', value), '');
|
|
for (const value of [
|
|
'sales',
|
|
'sales@',
|
|
'@example.com',
|
|
'a b@example.com',
|
|
'a..b@example.com',
|
|
'.ab@example.com',
|
|
'ab.@example.com',
|
|
'ab@-example.com',
|
|
])
|
|
assert.ok(validateField('businessEmail', value));
|
|
});
|
|
|
|
test('clear values remove every field and normalization keeps only the flat schema', () => {
|
|
assert.ok(
|
|
Object.entries(emptySettings())
|
|
.filter(([key]) => key !== 'insights')
|
|
.every(([, value]) => value === ''),
|
|
);
|
|
assert.deepEqual(emptySettings().insights.articles, []);
|
|
const result = normalizeSettings({
|
|
...validValues(),
|
|
address: ' 地址 ',
|
|
home: { extra: true },
|
|
});
|
|
assert.equal(result.address, '地址');
|
|
assert.equal(result.home, undefined);
|
|
assert.equal(Object.keys(result).length, 12);
|
|
});
|
|
|
|
test('restricts uploads to supported image types and at most 2 MB', () => {
|
|
assert.equal(
|
|
validateImageFile({ type: 'image/png', size: 2 * 1024 * 1024 }),
|
|
'',
|
|
);
|
|
for (const type of ['image/jpeg', 'image/webp'])
|
|
assert.equal(validateImageFile({ type, size: 100 }), '');
|
|
for (const size of [0, 2 * 1024 * 1024 + 1])
|
|
assert.ok(validateImageFile({ type: 'image/png', size }));
|
|
for (const type of ['image/svg+xml', 'image/gif', 'text/plain'])
|
|
assert.ok(validateImageFile({ type, size: 100 }));
|
|
});
|
|
|
|
test('requires permanent HTTP(S) resource URLs and an explicit base for relative paths', () => {
|
|
assert.equal(
|
|
resolveImageUrl('https://cdn.example.com/qr.png'),
|
|
'https://cdn.example.com/qr.png',
|
|
);
|
|
assert.equal(
|
|
resolveImageUrl('/uploads/qr.png', 'https://cdn.example.com'),
|
|
'https://cdn.example.com/uploads/qr.png',
|
|
);
|
|
for (const url of [
|
|
'/uploads/qr.png',
|
|
'blob:https://example.com/id',
|
|
'data:image/png;base64,123',
|
|
'javascript:alert(1)',
|
|
'https://user:pass@example.com/qr.png',
|
|
]) {
|
|
assert.throws(() => resolveImageUrl(url));
|
|
assert.ok(validateField('wechatQrCode', url));
|
|
}
|
|
assert.throws(() =>
|
|
resolveImageUrl('//unknown.example/qr.png', 'https://cdn.example.com'),
|
|
);
|
|
});
|