61 lines
2.3 KiB
JavaScript
61 lines
2.3 KiB
JavaScript
'use strict';
|
|
|
|
const assert = require('node:assert/strict');
|
|
const { app } = require('egg-mock/bootstrap');
|
|
|
|
function hasSemanticKey(keys, candidates) {
|
|
return candidates.some(candidate => keys.some(key => key === candidate || key.includes(candidate)));
|
|
}
|
|
|
|
describe('test/app/controller/docs.test.js', () => {
|
|
it('GET /api/docs/system/changes should return 200 and a change-history JSON array', async () => {
|
|
const res = await app.httpRequest()
|
|
.get('/api/docs/system/changes')
|
|
.expect('Content-Type', /json/)
|
|
.expect(200);
|
|
|
|
assert.ok(Array.isArray(res.body), 'body 应直接返回数组 JSON');
|
|
assert.ok(res.body.length > 0, '变更记录数组至少应包含一条记录');
|
|
|
|
const firstItem = res.body[0];
|
|
assert.equal(typeof firstItem, 'object');
|
|
|
|
const keys = Object.keys(firstItem).map(key => String(key).toLowerCase());
|
|
assert.ok(
|
|
hasSemanticKey(keys, [ 'version', 'ver', '版本' ]),
|
|
'数组项至少应包含版本字段'
|
|
);
|
|
assert.ok(
|
|
hasSemanticKey(keys, [ 'date', 'updatedat', 'updated_at', '日期', '时间' ]),
|
|
'数组项至少应包含日期字段'
|
|
);
|
|
assert.ok(
|
|
hasSemanticKey(keys, [ 'summary', 'change', 'desc', 'description', '摘要', '说明' ]),
|
|
'数组项至少应包含 summary/摘要字段'
|
|
);
|
|
});
|
|
|
|
it('GET /api/docs/system should render HTML entries for change history and raw JSON', async () => {
|
|
const res = await app.httpRequest()
|
|
.get('/api/docs/system')
|
|
.expect('Content-Type', /html/)
|
|
.expect(200);
|
|
|
|
const html = res.text;
|
|
assert.match(html, /\/api\/docs\/system\/changes/, '页面中应提供 /api/docs/system/changes 入口');
|
|
assert.match(html, /查看变更记录|变更记录|changelog/i, '页面中应出现“查看变更记录”或同等入口文案');
|
|
assert.match(html, /\/api\/docs\/system\/raw/, '页面中应提供 /api/docs/system/raw 入口');
|
|
assert.match(html, /查看\s*JSON|raw\s*json/i, '页面中应出现“查看 JSON”入口');
|
|
});
|
|
|
|
it('GET /api/docs/invalid-name!/changes should return 400', async () => {
|
|
const res = await app.httpRequest()
|
|
.get('/api/docs/invalid-name!/changes')
|
|
.expect('Content-Type', /json/)
|
|
.expect(400);
|
|
|
|
assert.equal(Number(res.body.code), 400);
|
|
assert.match(String(res.body.message || ''), /无效|invalid/i);
|
|
});
|
|
});
|