// 已读取AGENTS.md import * as fs from 'fs'; import * as path from 'path'; /** * 第十批回归闸门:订单评价/动态发布与删除的身份收口 + 关键路径可观测性 */ describe('第十批身份收口与可观测性闸门 (评价 / 动态发布 / 动态删除)', () => { const rootDir = path.resolve(__dirname, '../../'); const indexCode = fs.readFileSync(path.join(rootDir, 'server/src/index.ts'), 'utf-8'); const repoCode = fs.readFileSync(path.join(rootDir, 'server/src/db/repository.ts'), 'utf-8'); const apiCode = fs.readFileSync(path.join(rootDir, 'pwuniapp/common/api.js'), 'utf-8'); const routeBlock = (marker: string, len = 900) => { const start = indexCode.indexOf(marker); expect(start).toBeGreaterThan(-1); return indexCode.slice(start, start + len); }; describe('1. 订单评价身份以服务端会话为准', () => { it('评价路由必须经 resolveSecureUserId 绑定买家身份', () => { const block = routeBlock("app.post('/api/orders/:id/review'"); expect(block).toContain('resolveSecureUserId('); expect(block).toContain("'提交订单评价'"); expect(block).toContain('userId: reviewerId'); expect(block).not.toContain('userId: userId ? Number(userId) : undefined'); }); it('仓储层仍保留服务端归属校验(仅下单买家)', () => { expect(repoCode).toContain('仅下单买家本人可以评价该订单'); }); }); describe('2. 动态发布身份以服务端会话为准', () => { it('发布路由必须经 resolveSecureUserId 且写库用服务端 uid', () => { const block = routeBlock("app.post('/api/posts',"); expect(block).toContain("'发布动态'"); expect(block).toContain('repo.createPost({ ...(req.body || {}), userId: uid })'); }); }); describe('3. 动态删除身份以服务端会话为准', () => { it('删除路由必须经 resolveSecureUserId', () => { const block = routeBlock("app.delete('/api/posts/:id'"); expect(block).toContain("'删除动态'"); }); }); describe('4. 关键路径不得静默吞错', () => { it('openid 持久化/预支付解析/用户设置 IO 必须记录错误', () => { expect(indexCode).toContain("logServerError('login.updateUserOpenid'"); expect(indexCode).toContain("logServerError('prepay.resolveOpenid'"); expect(indexCode).toContain("logServerError('userSettings.write'"); expect(repoCode).toContain("logServerError('getDashboardTodos.reports'"); }); }); describe('5. 小程序端写接口携带真实身份', () => { it('发布动态/提交评价必须注入当前用户身份', () => { expect(apiCode).toContain('userId: normalizeUserId(payload && payload.userId)'); expect(apiCode).toContain('userId: normalizeUserId(reviewData.userId)'); expect(apiCode).toContain('const uid = normalizeUserId(userId);'); }); }); });