2aa9d73325
集中保留原版权声明,移除文件头检查和全局测试忽略规则。验证:全量单测、受影响用例复测及静态检查通过。
197 lines
5.9 KiB
JavaScript
197 lines
5.9 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import test from 'node:test';
|
|
import {
|
|
articleErrors,
|
|
categoryError,
|
|
emptyInsights,
|
|
filterArticles,
|
|
moveArticle,
|
|
newArticle,
|
|
normalizeInsights,
|
|
removeArticle,
|
|
validateInsights,
|
|
} from '../../src/pages/WebsiteSettings/insightsModel.js';
|
|
|
|
const article = (id, extra = {}) => ({
|
|
...newArticle('company'),
|
|
id,
|
|
title: `文章 ${id}`,
|
|
summary: '文章描述',
|
|
body: '<h2>章节</h2><p>正文</p>',
|
|
...extra,
|
|
});
|
|
|
|
test('category names reject blanks, duplicates, reserved names and excessive length', () => {
|
|
const { categories } = emptyInsights();
|
|
for (const name of [
|
|
'',
|
|
' ',
|
|
'公司动态',
|
|
'全部内容',
|
|
'全部分类',
|
|
'字'.repeat(13),
|
|
])
|
|
assert.ok(categoryError(name, categories));
|
|
assert.equal(categoryError('公司动态', categories, 'company'), '');
|
|
assert.equal(categoryError('新分类', categories), '');
|
|
});
|
|
|
|
test('article validation covers required content, limits and related references', () => {
|
|
const value = { ...emptyInsights(), articles: [article('a'), article('b')] };
|
|
assert.deepEqual(
|
|
articleErrors(article('c', { related: ['a', 'b'] }), value),
|
|
{},
|
|
);
|
|
for (const [key, bad] of [
|
|
['title', ' '],
|
|
['title', '字'.repeat(81)],
|
|
['summary', ''],
|
|
['summary', '字'.repeat(201)],
|
|
['tag', '字'.repeat(13)],
|
|
['category', 'missing'],
|
|
['category', 'all'],
|
|
['category', ''],
|
|
['date', ''],
|
|
['date', '2026-02-30'],
|
|
['date', '2026-11-14 10:00:00'],
|
|
['date', '2026-11-14 10:00'],
|
|
['date', '2025-02-29'],
|
|
['date', '2026/11/14'],
|
|
['date', '2026-11-14T10:00:00'],
|
|
['body', '<p><br></p>'],
|
|
['body', '<p> </p>'],
|
|
['body', '<img src="x" onerror="alert(1)">'],
|
|
['body', '<script>alert(1)</script>'],
|
|
['body', '<img src="data:image/svg+xml;base64,AAAA">'],
|
|
['related', ['a', 'a']],
|
|
['related', ['missing']],
|
|
['related', ['c']],
|
|
]) {
|
|
assert.ok(articleErrors(article('c', { [key]: bad }), value)[key], key);
|
|
}
|
|
for (const good of ['2026-11-14', '2024-02-29'])
|
|
assert.equal(
|
|
articleErrors(article('c', { date: good }), value).date,
|
|
undefined,
|
|
good,
|
|
);
|
|
const more = {
|
|
...value,
|
|
articles: ['a', 'b', 'd', 'e'].map((id) => article(id)),
|
|
};
|
|
assert.ok(
|
|
articleErrors(article('c', { related: ['a', 'b', 'd', 'e'] }), more)
|
|
.related,
|
|
);
|
|
});
|
|
|
|
test('new articles carry only website fields and never default to the all filter', () => {
|
|
const draft = newArticle('company');
|
|
assert.deepEqual(Object.keys(draft).sort(), [
|
|
'body',
|
|
'category',
|
|
'date',
|
|
'id',
|
|
'related',
|
|
'summary',
|
|
'tag',
|
|
'title',
|
|
]);
|
|
assert.equal(draft.category, 'company');
|
|
assert.equal(newArticle('all').category, '');
|
|
assert.equal(newArticle().category, '');
|
|
assert.match(draft.date, /^\d{4}-\d{2}-\d{2}$/);
|
|
});
|
|
|
|
test('deleting an article clears all related references without mutating source', () => {
|
|
const value = {
|
|
...emptyInsights(),
|
|
articles: [article('a'), article('b', { related: ['a'] })],
|
|
};
|
|
const next = removeArticle(value, 'a');
|
|
assert.deepEqual(next.articles[0].related, []);
|
|
assert.deepEqual(value.articles[1].related, ['a']);
|
|
});
|
|
|
|
test('search combines category and case-insensitive title or summary filtering in list order', () => {
|
|
const value = {
|
|
...emptyInsights(),
|
|
articles: [
|
|
article('a', { title: 'AI 模型' }),
|
|
article('b', { summary: 'AI 平台' }),
|
|
article('c', { title: 'AI 新闻', category: 'industry' }),
|
|
],
|
|
};
|
|
assert.deepEqual(
|
|
filterArticles(value, ' ai ', 'company').map((a) => a.id),
|
|
['a', 'b'],
|
|
);
|
|
assert.deepEqual(
|
|
filterArticles(value, '', 'all').map((a) => a.id),
|
|
['a', 'b', 'c'],
|
|
);
|
|
assert.equal(filterArticles(value, 'missing', 'all').length, 0);
|
|
});
|
|
|
|
test('moving an article swaps neighbours and ignores out-of-range moves', () => {
|
|
const value = {
|
|
...emptyInsights(),
|
|
articles: [article('a'), article('b'), article('c')],
|
|
};
|
|
const ids = (insights) => insights.articles.map((a) => a.id);
|
|
assert.deepEqual(ids(moveArticle(value, 'b', -1)), ['b', 'a', 'c']);
|
|
assert.deepEqual(ids(moveArticle(value, 'b', 1)), ['a', 'c', 'b']);
|
|
assert.equal(moveArticle(value, 'a', -1), value);
|
|
assert.equal(moveArticle(value, 'c', 1), value);
|
|
assert.equal(moveArticle(value, 'missing', 1), value);
|
|
assert.deepEqual(ids(value), ['a', 'b', 'c']);
|
|
});
|
|
|
|
test('normalization keeps only the article schema with trimmed values', () => {
|
|
const value = {
|
|
...emptyInsights(),
|
|
extension: 'keep',
|
|
articles: [
|
|
article('a', { title: ' 标题 ', date: ' 2026-10-31 ', extra: 'drop' }),
|
|
],
|
|
};
|
|
const normalized = normalizeInsights(value);
|
|
const [first] = normalized.articles;
|
|
assert.equal(first.title, '标题');
|
|
assert.equal(first.date, '2026-10-31');
|
|
assert.equal('extra' in first, false);
|
|
assert.equal(normalized.extension, 'keep');
|
|
assert.deepEqual(Object.keys(first).sort(), [
|
|
'body',
|
|
'category',
|
|
'date',
|
|
'id',
|
|
'related',
|
|
'summary',
|
|
'tag',
|
|
'title',
|
|
]);
|
|
assert.equal(validateInsights(normalized), '');
|
|
assert.ok(validateInsights({ ...value, categories: [] }));
|
|
assert.ok(
|
|
validateInsights({ ...value, articles: [article('a'), article('a')] }),
|
|
);
|
|
const fresh = emptyInsights();
|
|
fresh.categories[0].name = 'changed';
|
|
assert.equal(emptyInsights().categories[0].name, '公司动态');
|
|
assert.equal(validateInsights({ ...emptyInsights(), articles: [] }), '');
|
|
});
|
|
|
|
test('required article errors use Chinese control-specific prompts', () => {
|
|
const errors = articleErrors(newArticle(), emptyInsights());
|
|
assert.equal(errors.title, '请输入文章标题');
|
|
assert.equal(errors.summary, '请输入文章描述');
|
|
assert.equal(errors.body, '请输入文章正文');
|
|
assert.equal(errors.category, '请选择所属分类');
|
|
assert.equal(
|
|
articleErrors({ ...newArticle(), date: '' }, emptyInsights()).date,
|
|
'请选择发布日期',
|
|
);
|
|
assert.equal(errors.date, undefined);
|
|
});
|