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

189 lines
5.6 KiB
JavaScript

import assert from 'node:assert/strict';
import test from 'node:test';
import React from 'react';
import { readFile } from 'node:fs/promises';
import { runInNewContext } from 'node:vm';
import { transformWithEsbuild } from 'vite';
import * as model from '../../src/pages/WebsiteSettings/insightsModel.js';
const filename = new URL(
'../../src/pages/WebsiteSettings/ArticleEditor.jsx',
import.meta.url,
);
const { code } = await transformWithEsbuild(
await readFile(filename, 'utf8'),
filename.pathname,
{ loader: 'jsx', format: 'cjs' },
);
function harness() {
const initial = model.newArticle('company');
const states = [],
messages = [],
confirms = [],
applied = [];
let stateCursor = 0,
mounting = true,
tree,
closed = false;
const Modal = { confirm: (options) => confirms.push(options) };
const modules = {
'react-i18next': { useTranslation: () => ({ t: (key) => key }) },
react: {
...React,
useEffect() {},
useState(value) {
const index = stateCursor++;
if (mounting) states[index] = value;
return [
states[index],
(next) => {
states[index] =
typeof next === 'function' ? next(states[index]) : next;
},
];
},
},
'@/components/ui': {
Button: 'Button',
Card: 'Card',
Checkbox: 'Checkbox',
Form: { Slot: 'Slot' },
Image: 'Image',
Input: 'Input',
Modal,
Select: 'Select',
SideSheet: 'SideSheet',
TextArea: 'TextArea',
Typography: { Title: 'Title', Text: 'Text', Paragraph: 'Paragraph' },
},
'../../helpers': { showError: (message) => messages.push(message) },
'./insightsModel': model,
'./insightsContent': {
sanitizeArticleHtml: (html) => html,
articleHeadings: () => [],
},
'./ArticleRichEditor': { default: 'ArticleRichEditor', __esModule: true },
};
const module = { exports: {} };
runInNewContext(code, {
exports: module.exports,
module,
require: (name) => {
assert.ok(modules[name], name);
return modules[name];
},
});
const render = () => {
stateCursor = 0;
tree = module.exports.default({
initial,
insights: model.emptyInsights(),
onApply: (article) => applied.push(article),
onClose: () => {
closed = true;
},
});
mounting = false;
};
const all = (node) =>
Array.isArray(node)
? node.flatMap(all)
: node && typeof node === 'object'
? [node, ...all(node.props?.children), ...all(node.props?.footer)]
: [];
const find = (type, predicate = () => true) =>
all(tree).find((node) => node.type === type && predicate(node.props));
render();
return {
render,
find,
messages,
confirms,
applied,
closed: () => closed,
save: () => find('Button', (p) => p.children === '保存').props.onClick(),
};
}
test('article editor blocks incomplete drafts, applies valid data and confirms discarding changes', () => {
const page = harness();
page.save();
page.render();
assert.equal(page.applied.length, 0);
assert.equal(page.messages.length, 0);
assert.equal(
page.find('Slot', (p) => p.label.text === '文章标题').props.error.error,
'请输入文章标题',
);
assert.equal(
page.find('Slot', (p) => p.label.text === '文章描述').props.error.error,
'请输入文章描述',
);
assert.equal(
page.find('Slot', (p) => p.label.text === '文章正文').props.error.error,
'请输入文章正文',
);
page
.find('Input', (p) => p['aria-label'] === '文章标题')
.props.onChange(' 测试标题 ');
page.render();
page.find('TextArea').props.onChange(' 摘要 ');
page.render();
page.find('ArticleRichEditor').props.onChange('<h2>章节</h2><p>正文</p>');
page.render();
page.save();
assert.equal(page.applied[0].title, '测试标题');
assert.equal(page.applied[0].summary, '摘要');
page.find('SideSheet').props.onCancel();
assert.equal(page.closed(), false);
page.confirms.at(-1).onOk();
assert.equal(page.closed(), true);
});
test('article editor removes cover controls and blocks submission while body images are processing', () => {
const page = harness();
assert.equal(page.find('Image'), undefined);
assert.equal(
page.find('Slot', (p) => p.label.text === '封面图'),
undefined,
);
page.find('ArticleRichEditor').props.onBusyChange(true);
page.render();
assert.equal(
page.find('Button', (p) => p.children === '图片处理中').props.disabled,
true,
);
page.find('Button', (p) => p.children === '图片处理中').props.onClick();
assert.equal(page.applied.length, 0);
page.find('ArticleRichEditor').props.onBusyChange(false);
page.render();
assert.equal(
page.find('Button', (p) => p.children === '保存').props.disabled,
false,
);
});
test('article editor only exposes website fields and real categories', () => {
const page = harness();
for (const label of ['详情副标题', '卡片摘要'])
assert.equal(
page.find('Slot', (p) => p.label.text === label),
undefined,
label,
);
assert.equal(
page.find(
'Checkbox',
(p) => 'checked' in p && p.children === '根据正文标题自动生成',
),
undefined,
);
const options = page
.find('Select', (p) => p['aria-label'] === '所属分类')
.props.optionList.map((o) => o.value);
assert.deepEqual(options, ['company', 'product', 'industry']);
assert.ok(page.find('Slot', (p) => p.label.text === '文章描述'));
assert.ok(page.find('Slot', (p) => p.label.text === '角标标签'));
assert.ok(page.find('Slot', (p) => p.label.text === '发布日期'));
});