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

206 lines
6.7 KiB
JavaScript

import assert from 'node:assert/strict';
import test from 'node:test';
import * as model from '../../src/pages/WebsiteSettings/model.js';
import * as richTextImages from '../../src/components/common/ui/richTextImages.js';
import { loadFrontendModule } from './helpers/frontendModule.js';
const validValues = () => ({
insights: {
categories: [{ id: 'news', name: 'News' }],
articles: [
{
id: 'article-1',
title: 'News',
summary: 'Summary',
category: 'news',
date: '2026-11-10',
body: '<p>Content</p>',
related: [],
tag: '',
},
],
},
paidCustomers: '200',
dailyTokens: '10亿',
costReduction: '30',
teamMembers: '100',
researchRatio: '70',
headquarters: '深圳',
serviceNetwork: '全国',
businessPhone: '0755-12345678',
businessEmail: 'contact@example.com',
wechatQrCode: 'https://cdn.example.com/qr.png',
address: '联调测试地址',
});
async function harness(...args) {
const initial = args.length ? args[0] : null;
const requests = [];
let stored = initial;
let failure;
const api = await loadFrontendModule(
new URL('../../src/pages/WebsiteSettings/settingsApi.js', import.meta.url),
{
'../../helpers/websiteApi': {
requestWebsite: async (method, path, body) => {
requests.push({ method, path, body });
if (failure) throw failure;
if (path === '/api/customer-service/upload')
return { url: 'https://cdn.example.com/new.png' };
if (method === 'get') return stored;
assert.equal(typeof body.config, 'string');
stored = body.config;
return null;
},
},
'./model': model,
'../../components/common/ui/richTextImages': richTextImages,
},
);
return {
...api,
requests,
fail: (message) => {
failure = new Error(message);
},
};
}
test('loads an unconfigured site and rejects failed, malformed or invalid JSON reads', async () => {
assert.equal(await (await harness()).getWebsiteSettings(), null);
for (const value of [
undefined,
{},
'not json',
'null',
'[]',
'{"home":[]}',
]) {
await assert.rejects((await harness(value)).getWebsiteSettings());
}
const api = await harness();
api.fail('配置读取失败');
await assert.rejects(api.getWebsiteSettings(), /配置读取失败/);
});
test('round-trips the complete grouped configuration and preserves unknown fields', async () => {
const original = {
...model.settingsToConfig(validValues()),
extension: { enabled: true },
};
original.home.futureMetric = 'future';
original.insights.extra = { locale: 'zh' };
const api = await harness(JSON.stringify(original));
const config = await api.getWebsiteSettings();
const values = model.settingsFromConfig(config);
assert.equal(values.researchRatio, '70');
values.teamMembers = '120';
values.address = ' 新地址 ';
await api.saveWebsiteSettings(model.serializeSettings(values, config));
const saved = await api.getWebsiteSettings();
assert.deepEqual(JSON.parse(JSON.stringify(saved.extension)), {
enabled: true,
});
assert.equal(saved.home.futureMetric, 'future');
assert.equal(saved.insights.extra.locale, 'zh');
assert.equal(saved.about.teamMembers, '120');
assert.equal(saved.home.teamMembers, undefined);
assert.equal(saved.demo.address, '新地址');
assert.equal(api.requests[1].path, '/api/website/page-config');
assert.equal(api.requests[1].method, 'post');
});
test('rejects incomplete and unserialized configurations before sending', async () => {
const api = await harness();
await assert.rejects(api.saveWebsiteSettings({}), /JSON 字符串/);
await assert.rejects(api.saveWebsiteSettings('{}'), /完整填写/);
assert.equal(api.requests.length, 0);
});
test('large configurations are sent because the backend stores LONGTEXT', async () => {
const api = await harness();
const config = model.settingsToConfig(validValues());
config.extension = '中'.repeat(21000);
await api.saveWebsiteSettings(JSON.stringify(config));
assert.equal(api.requests.at(-1).path, '/api/website/page-config');
});
test('saving errors propagate without reporting success', async () => {
const api = await harness();
api.fail('没有保存权限');
await assert.rejects(
api.saveWebsiteSettings(model.serializeSettings(validValues())),
/没有保存权限/,
);
});
test('an empty article list can be saved for the website empty state', async () => {
const api = await harness();
const values = { ...validValues(), insights: model.emptySettings().insights };
await api.saveWebsiteSettings(model.serializeSettings(values));
assert.equal(api.requests.at(-1).path, '/api/website/page-config');
});
test('articles under the all filter are rejected before any request', async () => {
const api = await harness();
const values = validValues();
values.insights.articles[0].category = 'all';
await assert.rejects(
api.saveWebsiteSettings(model.serializeSettings(values)),
/请完整填写并检查官网设置/,
);
assert.equal(api.requests.length, 0);
});
test('uploads the actual image as multipart file and returns a persistent URL', async () => {
const api = await harness();
const file = new File(['png'], 'qr.png', { type: 'image/png' });
assert.equal(
await api.uploadWebsiteQrCode(file),
'https://cdn.example.com/new.png',
);
const { body, method, path } = api.requests[0];
assert.equal(method, 'post');
assert.equal(path, '/api/customer-service/upload');
assert.equal(body.get('file'), file);
await assert.rejects(
api.uploadWebsiteQrCode(
new File(['svg'], 'qr.svg', { type: 'image/svg+xml' }),
),
/仅支持/,
);
});
test('uploads local article images once and stores public URLs instead of base64', async () => {
const api = await harness();
const config = model.settingsToConfig(validValues());
config.insights = {
categories: [{ id: 'news', name: '新闻' }],
articles: [
{
id: 'a',
title: '测试文章',
category: 'news',
date: '2026-10-31',
summary: '测试摘要',
tag: '',
body: '<p>正文</p><img src="data:image/png;base64,AQID"><img src="data:image/png;base64,AQID">',
related: [],
},
],
};
const result = await api.saveWebsiteSettings(JSON.stringify(config));
assert.equal(
api.requests.filter((item) => item.path === '/api/customer-service/upload')
.length,
1,
);
assert.equal(api.requests.at(-1).path, '/api/website/page-config');
assert.equal(api.requests.at(-1).body.config.includes('data:image'), false);
assert.equal(
result.config.insights.articles[0].body.match(
/https:\/\/cdn.example.com\/new.png/g,
).length,
2,
);
});