'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 /docs should return an index page with docs file links', async () => { const res = await app.httpRequest() .get('/docs') .expect('Content-Type', /html/) .expect(200); assert.match(res.text, /docs 目录索引/); assert.match(res.text, /]*data-window-href="\/docs\/order-workbench-list-api\.md"/i); assert.doesNotMatch(res.text, /
  • \s*]*href="\/docs\/order-workbench-list-api\.md"/i); }); it('GET /docs/order-workbench-list-api.md should return markdown content', async () => { const res = await app.httpRequest() .get('/docs/order-workbench-list-api.md') .expect('Content-Type', /text\/markdown/) .expect(200); assert.match(res.text, /^# 订单工作台 List 接口说明/m); }); it('GET /docs should block path traversal attempts', async () => { const res = await app.httpRequest() .get('/docs/%2e%2e%2fapp.js') .expect('Content-Type', /json/) .expect(400); assert.equal(Number(res.body.code), 400); assert.match(String(res.body.message || ''), /无效|越界|path/i); }); it('GET /api/docs/system should still be available', async () => { const res = await app.httpRequest() .get('/api/docs/system') .expect('Content-Type', /html/) .expect(200); assert.match(res.text, /系统接口|API 文档/); assert.match(res.text, /\/api\/docs\/system\/raw/); assert.match(res.text, /]*data-window-href="\/api\/docs\/system\/raw"/i); }); 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, /]*data-window-href="\/api\/docs\/system\/changes"/i, '页面中应提供 /api/docs/system/changes 的 window 入口'); assert.match(html, /查看变更记录|变更记录|changelog/i, '页面中应出现“查看变更记录”或同等入口文案'); assert.match(html, /]*data-window-href="\/api\/docs\/system\/raw"/i, '页面中应提供 /api/docs/system/raw 的 window 入口'); 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); }); });