'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/openapi should include openapi default query fields in debug area', async () => { const res = await app.httpRequest() .get('/api/docs/openapi') .expect('Content-Type', /html/) .expect(200); const html = res.text; assert.match(html, /Query 参数(自动生成)/); assert.match(html, /]*readonly[^>]*data-auto-generated="true"/i); assert.match(html, /app_key=/); assert.match(html, /method=index\.ping/); assert.match(html, /timestamp=/); assert.match(html, /self_app_secret/); assert.match(html, /req-app-key/); assert.match(html, /wms_docs_openapi_app_key/); assert.match(html, /localStorage\.getItem\(OPENAPI_APP_KEY_STORE_KEY\)/); assert.match(html, /localStorage\.setItem\(OPENAPI_APP_KEY_STORE_KEY,/); assert.match(html, /buildOpenapiQuery/); assert.match(html, /refreshOpenapiQueryDisplays/); assert.match(html, /applyOpenapiSign/); assert.match(html, /function\s+normalizeParamValue\s*\(/); assert.match(html, /return\s+key\s*\+\s*['"]=['"]\s*\+\s*normalizeParamValue\(queryParams\[key\]\)/); assert.match(html, /var\s+ignoredSet\s*=\s*\{[\s\S]*sign\s*:\s*true,[\s\S]*signature\s*:\s*true,[\s\S]*\}/); assert.match(html, /!ignoredSet\[String\(key\)\.toLowerCase\(\)\]/); assert.match(html, /var\s+sign\s*=\s*md5\(canonicalQuery\s*\+\s*normalizedSecret\s*\+\s*bodySign\)/); assert.match(html, /raw\.startsWith\(['"]\?['"]\)/); assert.match(html, /raw\s*=\s*raw\.slice\(1\)/); assert.doesNotMatch(html, /replace\(\/\^\?\//); assert.match(html, /bodySign/); assert.match(html, /signature/); }); it('GET /api/docs/system should not inject openapi style query fields', async () => { const res = await app.httpRequest() .get('/api/docs/system') .expect('Content-Type', /html/) .expect(200); assert.doesNotMatch(res.text, /app_key=&method=.*&v=1\.0&sign=×tamp=/i); assert.doesNotMatch(res.text, / { 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); }); });