import assert from 'node:assert/strict'; import fs from 'node:fs'; import { Ledger } from '../core/ledger.js'; import routes from '../server/routes/index.js'; import { createRouter } from '../server/router.js'; import { orderActionCoverage, orderChangeCoverage } from './writes/orders-coverage.mjs'; const manifest = JSON.parse(fs.readFileSync(new URL('./writes/routes.json', import.meta.url), 'utf8')); const excluded = { '/api/private-operations': 'server/routes/funds.routes.js 中 POST handler 为 notFound。实际人数写入 /api/private-operations/people。', '/api/people-count-history': 'server/routes/funds.routes.js 中 POST handler 为 notFound。历史列表只接受 GET。', '/api/team-members/:member(member)/password': 'public/team-funds-ui.js 的 loadTeamMembers 在已加载员工模块时直接调用 loadEmployees。旧密码表单的按钮位于已替换的分支。实际管理入口走 /api/employees/:member/update,自助改密走 /api/password。', }; const router = createRouter(routes); const generic = '/api/orders/:id(hexid)/:action(word)'; const change = '/api/orders/:id(hexid)/change'; function actionKey(template, params, hit) { if (template === generic) return params.action; if (template === change) return (hit.action || 'edit') + (hit.afterSale ? '/after-sale' : ''); const variable = template.match(/:(\w+)\(([^)]+\|[^)]+)\)/); return variable ? params[variable[1]] : ''; } export function coverage(allHits) { const supported = [...Ledger.prototype.actionInternal.toString().matchAll(/\baction === ['"]([a-z]+)['"]/g)].map( m => m[1], ); assert.deepEqual([...new Set(supported)].sort(), Object.keys(orderActionCoverage).sort(), '订单通配动作清单已变化'); const changeSource = fs.readFileSync(new URL('../domains/orders/order-changes.js', import.meta.url), 'utf8'); const labels = changeSource.match(/const ACTION_LABELS = \{([^}]+)\}/)?.[1] || ''; assert.deepEqual( [...labels.matchAll(/\b(\w+):/g)].map(m => m[1]).sort(), Object.keys(orderChangeCoverage).sort(), '订单申请动作清单已变化', ); const templates = Object.values(manifest).flat(); const actual = routes.filter(route => route.method === 'POST'); assert.deepEqual([...templates].sort(), actual.map(route => route.path).sort(), 'POST 声明变化,请更新业务场景清单'); const result = []; for (const template of templates) { const variable = template.match(/:\w+\(([^)]+\|[^)]+)\)/); const actions = template === generic ? Object.keys(orderActionCoverage) : template === change ? [...Object.keys(orderChangeCoverage), 'return/after-sale'] : variable ? variable[1].split('|') : ['']; for (const action of actions) { const reason = excluded[template] || (template === generic && orderActionCoverage[action]?.reason); result.push({ template, action, ...(reason ? { reason } : {}), old: [], new: [] }); } } for (const [group, both] of Object.entries(allHits)) for (const version of ['old', 'new']) { for (const hit of both[version]) { const match = router.match('POST', hit.endpoint); assert.ok(match, '未声明的 POST ' + hit.endpoint); const template = match.route.path, action = actionKey(template, match.params, hit); const row = result.find(r => r.template === template && r.action === action); assert.ok(row && !row.reason, '未登记或标为无入口的业务动作 ' + template + ' ' + action); const via = { group, role: hit.role, member: hit.member, step: hit.step || '表单报价', status: hit.status }; if (!row[version].some(x => JSON.stringify(x) === JSON.stringify(via))) row[version].push(via); } } const missing = result.filter( row => !row.reason && ['old', 'new'].some( version => !row[version].some( hit => hit.status === 200 || (row.template === '/api/daily-funds/confirm' && hit.status === 409), ), ), ); return { routes: actual.length, writeFlags: actual.filter(route => route.write).length, actions: result, missing }; }