import { readFileSync } from 'node:fs'; import path from 'node:path'; import { describe, expect, it } from 'vitest'; /** * 硬门槛(用户 2026-09-15):不合格的不能入库,入库的即为合格。 * * AI 判定只是初判、可能不准;人工把这个判定改成合格之后才入库。 * 不合格请走「退回」(退供,不入库)。 * * 三端(Web 验收页 / 称端 / 小程序)提交都汇合到 submitInspectionCompliance, * 因此门禁落在服务端一处即可对三端同时生效。 * * 本文件取代旧契约 server/tests/failMarkStillInbound.test.ts *(旧契约断言「不合格仍入库」,与硬门槛冲突,已按用户确认改写)。 * * 设计文档:docs/design/20260915-inspection-inbound-requires-pass.md */ const read = (rel: string) => readFileSync(path.join(__dirname, '..', '..', rel), 'utf8'); const readServer = (rel: string) => readFileSync(path.join(__dirname, '..', rel), 'utf8'); describe('服务端硬门槛:不合格不得入库', () => { const svc = readServer('src/services/inspectionComplianceService.ts'); it('入库条件必须同时满足:未退回 + 实收>0 + 非不合格', () => { expect(svc).toContain('const ratingIsFail = rating === INSPECTION_RESULT_FAIL'); expect(svc).toContain( 'const writeInbound = !isRejected && actualQty > 0 && !ratingIsFail' ); }); it('不合格且未点退回时,明确报错而不是静默不入库', () => { expect(svc).toContain('inspection_fail_cannot_inbound'); expect(svc).toContain('不合格不得入库'); // 报错须发生在提交前,且排除「点退回」这条合法路径 expect(svc).toMatch( /if \(rating === INSPECTION_RESULT_FAIL && input\.reject_fulfillment !== true\)/ ); }); it('仍保留「退回即退供」:reject_fulfillment 语义未被削弱', () => { expect(svc).toContain('reject_fulfillment?: boolean'); expect(svc).toContain('const isRejected = input.reject_fulfillment === true'); }); it('退回与入库共用照片闸门,allowMissingPhotos 仍可放行', () => { expect(svc).toContain( 'if (photoRequired && photoCount <= 0 && !options?.allowMissingPhotos)' ); expect(svc).not.toContain('photoCount <= 0 && !isRejected && !options'); }); it('不合格常量取自统一实现,避免各端自定义字面量', () => { expect(svc).toContain('INSPECTION_RESULT_FAIL'); expect(svc).toMatch(/from '\.\/inspectionResultUtils'/); }); it('不再出现「不合格入库」这类绕过硬门槛的文案', () => { expect(svc).not.toContain('(不合格入库)'); }); }); describe('三端入口共用同一服务(门禁天然覆盖)', () => { it('Web 路由与移动端路由都调用 submitInspectionCompliance', () => { const web = readServer('src/modules/workflow/routes/inspectionRoutes.ts'); const mobile = readServer('src/routes/mobile/mobileWarehouseRoutes.ts'); expect(web).toContain('submitInspectionCompliance'); expect(mobile).toContain('submitInspectionCompliance'); }); it('Web 验收页保留「确认入库」与「退回」两个动作(文案在 i18n)', () => { const page = read('src/pages/inspection/InspectionManagement.tsx'); expect(page).toContain('rejectFulfillment: true'); expect(page).toContain("t('inspection.act.confirm')"); expect(page).toContain("t('inspection.act.reject')"); expect(page).not.toContain('!rejectFulfillment && selectedTaskPhotoRequired'); const i18n = read('src/i18n/locales/zh-CN/inspection.ts'); expect(i18n).toContain("'act.confirm': '确认入库'"); expect(i18n).toContain("'act.reject': '退回'"); }); it('小程序「确认入库」与「退回」并存,退回带 reject_fulfillment', () => { const panel = read('miniprogram/components/inspection-panel/index.js'); expect(panel).toContain('async submitPassInspection'); expect(panel).toContain('reject_fulfillment: true'); expect(panel).not.toContain('!rejectFulfillment && photoRequired && photoCount <= 0'); const wxml = read('miniprogram/components/inspection-panel/index.wxml'); expect(wxml).toContain('>确认入库'); expect(wxml).toContain('一键全部合格入库'); }); it('称端退回明确告知不会入库', () => { const java = read( 'android-weigh-terminal/app/src/main/java/com/canteen/weigh/ui/InspectionWeighActivity.java' ); expect(java).toContain('不会入库'); expect(java).toContain('doSubmit(item, "不合格", qty, weight, true, false)'); expect(java).not.toContain('rejectFulfillment || task.inspectionPhotoCount > 0'); }); }); describe('历史数据修订迁移', () => { const sql = readFileSync( path.join(__dirname, '..', '..', 'migrations', '20260915_inspection_inbound_requires_pass.sql'), 'utf8' ); it('修订范围为「不合格 + 已入库」,不动未入库的不合格', () => { expect(sql).toContain("COALESCE(quality_rating, '') LIKE '%%不合格%%'"); expect(sql).toContain("NULLIF(TRIM(COALESCE(inbound_batch_no, '')), '') IS NOT NULL"); expect(sql).toContain("SET quality_rating = '合格'"); }); it('留痕可区分「规则修订」与「人工复核」', () => { expect(sql).toContain("ADD COLUMN IF NOT EXISTS reason TEXT"); expect(sql).toContain("'rule:inbound_requires_pass'"); expect(sql).toContain('previous_review_rating'); expect(sql).toContain('original_quality_rating'); }); it('修订人需可解析,取不到则跳过该 schema(不写假身份)', () => { expect(sql).toContain('inspection_review_logs ORDER BY created_at DESC LIMIT 1'); expect(sql).toContain('找不到可用修订人,跳过'); }); });