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

327 lines
10 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/InsightsPanel.jsx',
import.meta.url,
);
const { code } = await transformWithEsbuild(
await readFile(filename, 'utf8'),
filename.pathname,
{ loader: 'jsx', format: 'cjs' },
);
function harness(initial = model.emptyInsights()) {
const states = [];
const confirms = [];
const messages = [];
let value = initial;
let cursor = 0;
let mounting = true;
let tree;
const Modal = Object.assign(() => {}, {
confirm: (options) => confirms.push(options),
});
const modules = {
'react-i18next': { useTranslation: () => ({ t: (key) => key }) },
react: {
...React,
useState(initialState) {
const index = cursor++;
if (mounting)
states[index] =
typeof initialState === 'function' ? initialState() : initialState;
return [
states[index],
(next) => {
states[index] =
typeof next === 'function' ? next(states[index]) : next;
},
];
},
},
'@/components/ui': {
Button: 'Button',
Card: 'Card',
Empty: 'Empty',
Input: 'Input',
Modal,
Select: 'Select',
Typography: { Paragraph: 'Paragraph', Text: 'Text', Title: 'Title' },
},
'@/components/ui/icons': {
IconPlus: 'IconPlus',
IconSearch: 'IconSearch',
IconEdit: 'IconEdit',
IconClose: 'IconClose',
},
'../../components/common/ui/CardTable': {
default: 'CardTable',
__esModule: true,
},
'../../components/common/table/TableEmpty': {
default: 'TableEmpty',
__esModule: true,
},
'../../components/common/table/TableToolbar': {
default: 'TableToolbar',
__esModule: true,
},
'../../components/console/ConsoleModuleFilters': {
ConsoleFilterField: 'ConsoleFilterField',
},
'../../helpers/utils': {
createCardProPagination: (props) =>
React.createElement('Pagination', props),
},
'../../hooks/common/useIsMobile': { useIsMobile: () => false },
'../../helpers': {
showError: (message) => messages.push(message),
showInfo: (message) => messages.push(message),
},
'./insightsModel': model,
'./ArticleEditor': { default: 'ArticleEditor', __esModule: true },
};
const module = { exports: {} };
runInNewContext(code, {
exports: module.exports,
module,
require: (name) => {
assert.ok(modules[name], name);
return modules[name];
},
});
const render = () => {
cursor = 0;
tree = module.exports.default({
value,
onChange: (next) => {
value = next;
},
disabled: false,
});
mounting = false;
};
const all = (node) =>
Array.isArray(node)
? node.flatMap(all)
: node && typeof node === 'object'
? [node, ...all(node.props?.children)]
: [];
const find = (type, predicate = () => true) =>
all(tree).find((node) => node.type === type && predicate(node.props));
const button = (label) => find('Button', (props) => props.children === label);
const modal = () =>
find(
Modal,
(props) =>
props.visible && ['新增分类', '编辑分类'].includes(props.title),
);
render();
return {
render,
find,
button,
modal,
confirms,
messages,
value: () => value,
};
}
const actionsOf = (page, item) =>
page.find('CardTable').props.columns.at(-1).render(null, item).props.children;
const action = (page, item, label) =>
actionsOf(page, item).find((button) => button.props.children === label);
const article = (id, extra = {}) => ({
...model.newArticle('company'),
id,
title: `文章 ${id}`,
summary: 'AI 内容',
body: '<p>正文</p>',
...extra,
});
test('category modal creates, rejects duplicates and edits the bound configuration', () => {
const page = harness();
page.button('新增分类').props.onClick();
page.render();
page
.find('Input', (p) => p['aria-label'] === '分类名称')
.props.onChange('公司动态');
page.render();
page.modal().props.onOk();
page.render();
assert.equal(page.value().categories.length, 3);
assert.ok(page.find('Text', (p) => p.role === 'alert'));
page
.find('Input', (p) => p['aria-label'] === '分类名称')
.props.onChange('客户案例');
page.render();
page.modal().props.onOk();
page.render();
assert.equal(page.value().categories.at(-1).name, '客户案例');
page
.find('Button', (p) => p['aria-label'] === '编辑客户案例')
.props.onClick();
page.render();
page
.find('Input', (p) => p['aria-label'] === '分类名称')
.props.onChange('实践案例');
page.render();
page.modal().props.onOk();
page.render();
assert.equal(page.value().categories.at(-1).name, '实践案例');
});
test('article deletion cleans references and occupied category deletion is blocked', () => {
const page = harness({
...model.emptyInsights(),
articles: [article('a'), article('b', { related: ['a'] })],
});
page
.find('Button', (p) => p['aria-label'] === '删除公司动态')
.props.onClick();
assert.equal(page.confirms.length, 0);
assert.ok(page.messages[0].includes('仍有文章'));
action(page, page.value().articles[0], '删除').props.onClick();
page.confirms.at(-1).onOk();
page.render();
assert.equal(page.value().articles.length, 1);
assert.deepEqual(page.value().articles[0].related, []);
page
.find('Button', (p) => p['aria-label'] === '删除产品更新')
.props.onClick();
page.confirms.at(-1).onOk();
page.render();
assert.ok(!page.value().categories.some((c) => c.id === 'product'));
});
test('article row link actions open the editor and confirm deletion', () => {
const initial = article('a', { related: ['b'] });
const page = harness({
...model.emptyInsights(),
articles: [initial, article('b')],
});
const actions = actionsOf(page, initial);
assert.deepEqual(
Array.from(actions, (button) => button.props.children),
['编辑', '上移', '下移', '删除'],
);
for (const button of actions) {
assert.equal(button.props.type, 'link');
assert.equal(button.props.size, 'small');
}
assert.equal(action(page, initial, '删除').props.danger, true);
action(page, initial, '编辑').props.onClick();
page.render();
assert.deepEqual(
JSON.parse(JSON.stringify(page.find('ArticleEditor').props.initial)),
initial,
);
assert.notEqual(
page.find('ArticleEditor').props.initial.related,
initial.related,
);
action(page, initial, '删除').props.onClick();
assert.equal(page.value().articles.length, 2);
assert.equal(page.confirms.at(-1).title, '删除文章');
page.confirms.at(-1).onOk();
assert.equal(page.value().articles.length, 1);
});
test('local fuzzy search and category filtering display all matches without pagination', () => {
const page = harness({
...model.emptyInsights(),
articles: Array.from({ length: 23 }, (_, i) => article(`a${i}`)),
});
assert.equal(page.find('CardTable').props.dataSource.length, 23);
assert.equal(page.find('CardTable').props.hidePagination, true);
assert.equal(page.find('Pagination'), undefined);
assert.equal(page.find('ConsoleFilterField').props.label, '文章标题');
assert.equal(
page.find('Input', (p) => p['aria-label'] === '搜索文章标题或摘要').props
.showClear,
true,
);
page
.find('Input', (p) => p['aria-label'] === '搜索文章标题或摘要')
.props.onChange('a22');
page.render();
assert.equal(page.find('Pagination'), undefined);
assert.equal(page.find('CardTable').props.dataSource.length, 1);
page
.find('Input', (p) => p['aria-label'] === '搜索文章标题或摘要')
.props.onChange('');
page.render();
assert.equal(page.find('CardTable').props.dataSource.length, 23);
page.find('Select').props.onChange('industry');
page.render();
assert.equal(page.find('CardTable').props.dataSource.length, 0);
page.button('新增文章').props.onClick();
page.render();
assert.equal(page.find('ArticleEditor').props.initial.category, 'industry');
page
.find('ArticleEditor')
.props.onApply(article('', { title: '新文章', category: 'industry' }));
page.render();
assert.equal(page.value().articles.length, 24);
assert.equal(page.value().articles[0].title, '新文章');
});
test('without categories new articles are blocked with a hint', () => {
const page = harness({ categories: [], articles: [] });
assert.equal(page.button('新增文章').props.disabled, true);
assert.ok(
page.find('Paragraph', (p) =>
String(p.children).includes('请先新增内容分类'),
),
);
assert.deepEqual(
Array.from(page.find('CardTable').props.columns, (column) => column.title),
['文章标题', '所属分类', '角标', '发布日期', '操作'],
);
});
test('articles reorder only in the unfiltered list, which is the website order', () => {
const page = harness({
...model.emptyInsights(),
articles: [
article('a'),
article('b'),
article('c', { category: 'product' }),
],
});
const first = page.value().articles[0];
assert.equal(action(page, first, '上移').props.disabled, true);
action(page, first, '下移').props.onClick();
page.render();
assert.deepEqual(
page.value().articles.map((a) => a.id),
['b', 'a', 'c'],
);
const last = page.value().articles.at(-1);
assert.equal(action(page, last, '下移').props.disabled, true);
page.find('Select').props.onChange('company');
page.render();
assert.equal(
action(page, page.value().articles[1], '上移').props.disabled,
true,
);
page.find('Select').props.onChange('all');
page
.find('Input', (p) => p['aria-label'] === '搜索文章标题或摘要')
.props.onChange('a');
page.render();
assert.equal(
action(page, page.value().articles[1], '上移').props.disabled,
true,
);
});