'use strict'; const test = require('node:test'); const assert = require('node:assert/strict'); const { createRouter } = require('../server/router'); const routes = require('../server/routes'); const router = createRouter(routes); const U = '0123abcd-0000-4000-8000-0123456789ab'; const match = (method, path, accept) => router.match(method, path, accept); const routeOf = (method, path) => match(method, path)?.route; test('路由匹配器:重叠路径按声明顺序与参数约束区分', () => { // 订单:售后与通用动作 assert.equal(routeOf('GET', `/api/orders/${U}/after-sale`).path, '/api/orders/:id(hexid)/after-sale'); assert.equal(routeOf('POST', `/api/orders/${U}/after-sale`), undefined, 'POST after-sale 无此路由,走未匹配 404'); const preview = routeOf('POST', `/api/orders/${U}/after-sale-preview`); assert.equal(preview.path, '/api/orders/:id(hexid)/after-sale-preview'); assert.ok(!preview.write, '售后试算不进入事务'); assert.equal(routeOf('POST', `/api/orders/${U}/edit`).path, '/api/orders/:id(hexid)/edit'); assert.equal(routeOf('POST', `/api/orders/${U}/change`).path, '/api/orders/:id(hexid)/change'); assert.deepEqual(match('POST', `/api/orders/${U}/confirm`).params, { id: U, action: 'confirm' }); assert.equal(routeOf('POST', '/api/orders/issue').path, '/api/orders/issue'); assert.equal(routeOf('POST', `/api/orders/${U}`), undefined); assert.equal(routeOf('GET', `/api/orders/${U}/unknown`), undefined); // 打印页:回收专用页优先,其余(含非 UUID 的 recoveries/…、空编号)归通用订单打印 assert.equal(routeOf('GET', `/print/recoveries/${U}`).path, '/print/recoveries/:id(uuid)'); assert.deepEqual(match('GET', '/print/recoveries/abc').params, { id: 'recoveries/abc' }); assert.deepEqual(match('GET', '/print/').params, { id: '' }); assert.equal(routeOf('GET', '/print'), undefined); // 购料:详情/编辑/删除允许宽松编号,申请修改要求 36 位 assert.equal(routeOf('GET', '/api/material-purchases/abc').path, '/api/material-purchases/:id(hexid)'); assert.equal(routeOf('POST', '/api/material-purchases/abc/edit').path, '/api/material-purchases/:id(hexid)/edit'); assert.equal(routeOf('POST', '/api/material-purchases/abc/change'), undefined); assert.equal(routeOf('POST', `/api/material-purchases/${U}/change`).path, '/api/material-purchases/:id(uuid)/change'); assert.equal(routeOf('GET', `/api/material-purchases/${U}/change`), undefined); // 日购料池:固定子路径与日期参数 assert.equal(routeOf('GET', '/api/material-pools/rolling-preview').path, '/api/material-pools/rolling-preview'); assert.equal(routeOf('GET', '/api/material-pools/2026-01-02').path, '/api/material-pools/:date(day)'); assert.equal(routeOf('GET', '/api/material-pools/2026-1-02'), undefined); // 库存流水导出与流水详情 assert.equal(routeOf('GET', '/api/inventory/movements/export.xlsx').path, '/api/inventory/movements/export.xlsx'); assert.equal(routeOf('GET', '/api/inventory/movements/exportxxlsx'), undefined, '点号按字面匹配'); assert.equal(routeOf('GET', `/api/inventory/movements/${U}`).path, '/api/inventory/movements/:id(uuid)'); // 锁价 assert.equal(routeOf('GET', '/api/price-locks/abc/edit-context').path, '/api/price-locks/:id(hexid)/edit-context'); assert.equal(routeOf('POST', '/api/price-locks/abc/edit-context'), undefined); // 售后与售后修改 assert.equal(routeOf('GET', '/api/after-sales/changes').path, '/api/after-sales/changes'); assert.equal(routeOf('GET', `/api/after-sales/${U}`).path, '/api/after-sales/:id(uuid)'); // 约束整体锚定 assert.equal(routeOf('GET', `/api/order-templates/${U}x`), undefined); assert.equal(routeOf('GET', `/api/order-templates/${U.toUpperCase()}`), undefined); // 货盘键与备份文件名原样传入(不解码) assert.deepEqual(match('POST', '/api/pockets/%E7%8E%B0%E8%B4%A7/update').params, { key: '%E7%8E%B0%E8%B4%A7' }); assert.deepEqual(match('GET', '/api/backup/a/b').params, { filename: 'a/b' }); assert.equal(routeOf('GET', '/api/backup'), undefined); assert.equal(routeOf('POST', '/api/backup/x'), undefined); // 打印状态:GET 与 POST 共用路径,POST 自行读请求体 assert.ok(routeOf('POST', `/api/print-status/order/${U}`).direct); assert.equal(routeOf('GET', `/api/print-status/other/${U}`), undefined); // 演示登录只在演示模式启用 const demo = r => !r.when || r.when({ mode: 'demo' }), trial = r => !r.when || r.when({ mode: 'trial' }); assert.equal(match('POST', '/api/demo-login', demo).route.auth, 'public'); assert.equal(match('POST', '/api/demo-login', trial), null); // 请求方式必须一致 assert.equal(routeOf('POST', '/api/session'), undefined); assert.equal(routeOf('GET', '/api/login'), undefined); }); // 由 git show 2b9cb82:新账系统/server.js 中全部 owner(actor) 调用整理:[方法, 示例地址]。 const OWNER_ONLY = [ ['POST', '/api/shop-settings'], ['POST', '/api/business-hours/setup'], ['POST', '/api/printing'], ['GET', '/api/employees'], ['POST', '/api/employees'], ...['update', 'deactivate', 'reactivate', 'delete'].flatMap(action => [ ['GET', `/api/employees/staff_x/${action}`], ['POST', `/api/employees/staff_x/${action}`], ]), ['POST', '/api/pockets'], ['POST', '/api/pockets/pocket_x/update'], ['GET', '/api/team-funds'], ['GET', '/api/private-operations'], ['POST', '/api/private-operations'], ['GET', '/api/people-count'], ['POST', '/api/people-count'], ['GET', '/api/people-count-history'], ['POST', '/api/people-count-history'], ['GET', '/api/team-profits'], ['GET', '/api/team-funds/transfers'], ['POST', '/api/team-funds/transfers'], ['GET', '/api/team-funds/transfer'], ['POST', '/api/team-funds/transfer'], ['GET', '/api/material-pools/rolling-preview'], ['POST', '/api/team-members/assistant/password'], ['GET', '/api/export/inventory'], ['POST', '/api/backup'], ['GET', '/api/backup/ledger-2026-01-01T00-00-00-000Z.sqlite'], ['POST', '/api/sync'], ]; test('旧代码中的店主专用路由在路由表中均标记 role: owner', () => { for (const [method, path] of OWNER_ONLY) { const route = routeOf(method, path); assert.ok(route, `${method} ${path} 缺少路由`); assert.equal(route.role, 'owner', `${method} ${path} 应为店主专用`); // 旧代码只有成员密码在事务内校验店主,其余都在读请求体之前。 assert.equal( route.ownerCheck === 'transact', path === '/api/team-members/assistant/password', `${method} ${path} 店主校验时机`, ); } const owners = routes.filter(r => r.role === 'owner').length; assert.equal(owners, OWNER_ONLY.length - 6, '路由表中的店主路由数与清单一致(员工动作 8 个示例合为 2 条路由)'); }); test('路由表没有重复的 方法 + 路径', () => { const seen = new Set(); for (const { method, path } of routes) { const key = method + ' ' + path; assert.ok(!seen.has(key), '重复路由:' + key); seen.add(key); } }); test('路由声明字段合法', () => { for (const route of routes) { const name = route.method + ' ' + route.path; assert.ok(['GET', 'POST'].includes(route.method), name); assert.equal(typeof route.handler, 'function', name); assert.ok([undefined, 'public', 'user', 'session'].includes(route.auth), name); assert.ok([undefined, 'owner'].includes(route.role), name); if (route.write) assert.equal(route.method, 'POST', name); if (route.direct) assert.ok(!route.write, name); if (route.auth === 'public') assert.ok(!route.role && !route.write, name); } });