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

166 lines
5.3 KiB
JavaScript

import assert from 'node:assert/strict';
import { readFile, readdir } from 'node:fs/promises';
import test from 'node:test';
import React from 'react';
import { parse } from '@babel/parser';
import { transformWithEsbuild } from 'vite';
let pageKey = 'setting';
const token = {
colorInfoBg: 'info-background',
colorInfoBorder: 'info-border',
colorInfo: 'info-icon',
colorWarningBg: 'warning-background',
colorWarningBorder: 'warning-border',
};
const filename = new URL(
'../../src/components/console/ConsolePrimitives.jsx',
import.meta.url,
);
const { code } = await transformWithEsbuild(
await readFile(filename, 'utf8'),
filename.pathname,
{ loader: 'jsx', format: 'cjs' },
);
const module = { exports: {} };
const mocks = {
react: React,
antd: {
Alert: 'alert',
Table: 'table',
Empty: Object.assign(() => null, { PRESENTED_IMAGE_SIMPLE: 'simple' }),
theme: { useToken: () => ({ token }) },
},
'react-i18next': {},
'../ui/CenterActionTable': 'table',
'../common/table/TableEmpty': () => null,
'../common/table/FilterSearchButton': () => null,
'./console.css': {},
'./ConsoleContext': { useConsolePage: () => ({ page: { key: pageKey } }) },
};
new Function('require', 'module', 'exports', code)(
(name) => {
assert.ok(Object.hasOwn(mocks, name), name);
return mocks[name];
},
module,
module.exports,
);
const { ConsoleAlert, ConsoleTable } = module.exports;
test('settings alerts share the reference surface while retaining their semantic type', () => {
pageKey = 'setting';
for (const type of ['info', 'warning', 'error', 'success']) {
const element = ConsoleAlert({ type, description: 'Existing message' });
assert.equal(element.props.type, type);
assert.equal(element.props.description, 'Existing message');
assert.equal(element.props.style.background, token.colorInfoBg);
assert.equal(element.props.style.borderColor, token.colorInfoBorder);
assert.equal(
element.props.style['--settings-alert-icon-color'],
token.colorInfo,
);
}
pageKey = 'token';
const element = ConsoleAlert({ type: 'warning' });
assert.equal(element.props.style.background, token.colorWarningBg);
assert.equal(element.props.style.borderColor, token.colorWarningBorder);
});
test('settings tables use token-list density and keep columns and callbacks intact', () => {
const columns = [{ dataIndex: 'value', render: (value) => value }];
const onChange = () => {};
const props = { columns, onChange, size: 'small' };
pageKey = 'setting';
const element = ConsoleTable.render(props, null);
assert.equal(element.props.size, 'middle');
assert.equal(element.props.columns, columns);
assert.equal(element.props.onChange, onChange);
pageKey = 'token';
assert.equal(ConsoleTable.render(props, null).props.size, 'small');
});
function walk(node, visit) {
if (!node || typeof node !== 'object') return;
visit(node);
for (const [key, value] of Object.entries(node)) {
if (key === 'loc') continue;
if (Array.isArray(value)) value.forEach((child) => walk(child, visit));
else if (value && typeof value === 'object') walk(value, visit);
}
}
async function sourceFiles(directory) {
const entries = await readdir(directory, { withFileTypes: true });
const files = [];
for (const entry of entries) {
const child = new URL(
entry.name + (entry.isDirectory() ? '/' : ''),
directory,
);
if (entry.isDirectory()) files.push(...(await sourceFiles(child)));
else if (entry.name.endsWith('.jsx')) files.push(child);
}
return files;
}
test('every settings action column uses labeled links instead of icon buttons', async () => {
const files = [
...(await sourceFiles(
new URL('../../src/pages/Setting/', import.meta.url),
)),
...(await sourceFiles(
new URL('../../src/components/settings/', import.meta.url),
)),
];
let count = 0;
for (const file of files) {
const source = await readFile(file, 'utf8');
const ast = parse(source, { sourceType: 'module', plugins: ['jsx'] });
walk(ast, (node) => {
if (node.type !== 'ObjectExpression') return;
const title = node.properties.find(
(property) => property.key?.name === 'title',
);
const render = node.properties.find(
(property) => property.key?.name === 'render',
);
if (
!title ||
!render ||
!/t\(['"]\u64cd\u4f5c['"]\)/.test(
source.slice(title.value.start, title.value.end),
)
)
return;
walk(render, (child) => {
if (
child.type !== 'JSXElement' ||
!['Button', 'AntButton'].includes(child.openingElement.name.name)
)
return;
const attributes = child.openingElement.attributes;
const type = attributes.find(
(attribute) => attribute.name?.name === 'type',
);
const location = `${file.pathname}:${child.loc.start.line}`;
assert.equal(type?.value?.value, 'link', location);
assert.ok(
!attributes.some((attribute) =>
['icon', 'theme'].includes(attribute.name?.name),
),
location,
);
assert.ok(
child.children.some(
(content) => content.type !== 'JSXText' || content.value.trim(),
),
location,
);
count++;
});
});
}
assert.ok(count > 0);
});