import { describe, expect, it } from 'vitest'; import { readFileSync } from 'fs'; import path from 'path'; import { countReportsCoveringDayItem, isQualityReportSelfSupplier, parseQualityReportPurchaserCode, qualityReportPurchaserPartyKey, QUALITY_REPORT_SELF_SUPPLIER_KEY, isPrepInlineReportCovered, shareModeForReportType, proposePrepInlineHits, intersectPrepInlineHits, skuCodesCoveredByDayReport, } from '../src/services/qualityReportService'; describe('检验报告采购员键与一对多覆盖计数', () => { it('p:采购员编码识别为自采方,真实供应商不是', () => { expect(qualityReportPurchaserPartyKey('ZHANG')).toBe('p:ZHANG'); expect(parseQualityReportPurchaserCode('p:ZHANG')).toBe('ZHANG'); expect(isQualityReportSelfSupplier('p:ZHANG')).toBe(true); expect(isQualityReportSelfSupplier(QUALITY_REPORT_SELF_SUPPLIER_KEY)).toBe(true); expect(isQualityReportSelfSupplier('SUP01')).toBe(false); }); it('检疫叶子分类:一份报告覆盖同分类其它 SKU', () => { const hits = [ { reportId: 'r1', skuCode: 'PORK_BELLY', categoryCode: 'PORK', shareMode: 'leaf_category' as const, }, ]; expect( countReportsCoveringDayItem({ sku_code: 'PORK_BELLY', category_code: 'PORK' }, hits) ).toBe(1); expect( countReportsCoveringDayItem({ sku_code: 'PORK_RIB', category_code: 'PORK' }, hits) ).toBe(1); expect( countReportsCoveringDayItem({ sku_code: 'CHICKEN', category_code: 'POULTRY' }, hits) ).toBe(0); }); it('手动多选 share_mode=manual 只覆盖勾选的 SKU', () => { const hits = [ { reportId: 'r3', skuCode: 'PEPPER', categoryCode: null, shareMode: 'manual' as const, }, { reportId: 'r3', skuCode: 'BEAN', categoryCode: null, shareMode: 'manual' as const, }, ]; expect(countReportsCoveringDayItem({ sku_code: 'PEPPER', category_code: 'A' }, hits)).toBe(1); expect(countReportsCoveringDayItem({ sku_code: 'BEAN', category_code: 'B' }, hits)).toBe(1); expect(countReportsCoveringDayItem({ sku_code: 'OTHER', category_code: 'A' }, hits)).toBe(0); }); it('农残 produce_day:覆盖当日所有行', () => { const hits = [ { reportId: 'r2', skuCode: 'LEAF', categoryCode: 'VEG', shareMode: 'produce_day' as const, }, ]; expect(countReportsCoveringDayItem({ sku_code: 'ROOT', category_code: 'VEG2' }, hits)).toBe(1); }); }); describe('listQualityReportDayItems 自采/一对多 SQL 防回归', () => { const src = () => readFileSync(path.join(__dirname, '../src/services/qualityReportService.ts'), 'utf8'); function dayItemsFnBody() { const text = src(); const start = text.indexOf('export async function listQualityReportDayItems'); const end = text.indexOf('export async function matchUploadQualityReports'); expect(start).toBeGreaterThan(-1); expect(end).toBeGreaterThan(start); return text.slice(start, end); } it('自采清单按采购员过滤并排除作废 40,不把待采收成 IN (10,32)', () => { const body = dayItemsFnBody(); expect(body).toMatch(/po\.purchaser_code\s*=/); expect(body).toMatch(/po\.purchase_status\s*<>\s*40/); expect(body).not.toMatch(/purchase_status\s+IN\s*\(\s*10\s*,\s*32\s*\)/); }); it('供应商路径计数含 quality_report_prep_links,自采查 purchase_orders', () => { const text = src(); expect(text).toMatch(/quality_report_prep_links/); expect(dayItemsFnBody()).toMatch(/purchase_orders po/); }); }); describe('供货录入行内覆盖(不改全局 produce_day 推导)', () => { it('shareModeForReportType 农残仍是 produce_day', () => { expect(shareModeForReportType('pesticide')).toBe('produce_day'); expect(shareModeForReportType('quarantine')).toBe('leaf_category'); }); it('检疫映射即覆盖;农残 produce_day 不盖其它 SKU', () => { const produceDay = { share_mode: 'produce_day', preparation_id: 'prep-cabbage', sku_code: 'CABBAGE', }; expect( isPrepInlineReportCovered({ prepId: 'prep-pork-rib', skuCode: 'PORK_RIB', reportType: 'quarantine', mapped: { share_mode: 'leaf_category', sku_code: 'PORK_BELLY' }, }) ).toBe(true); expect( isPrepInlineReportCovered({ prepId: 'prep-cabbage', skuCode: 'CABBAGE', reportType: 'pesticide', mapped: produceDay, }) ).toBe(true); expect( isPrepInlineReportCovered({ prepId: 'prep-potato', skuCode: 'POTATO', reportType: 'pesticide', mapped: produceDay, }) ).toBe(false); expect( isPrepInlineReportCovered({ prepId: 'prep-potato', skuCode: 'POTATO', reportType: 'pesticide', mapped: { share_mode: 'manual', sku_code: 'CABBAGE' }, }) ).toBe(true); expect( isPrepInlineReportCovered({ prepId: 'prep-pork', skuCode: 'PORK', reportType: 'quarantine', mapped: { share_mode: 'line', share_key: 'st|prep-pork', sku_code: 'PORK' }, }) ).toBe(false); }); it('检疫默认命中同分类;农残只命中识别到的 SKU', () => { const dayItems = [ { id: 'p1', sku_code: 'BELLY', product_name: '夹子肉', category_code: 'PORK' }, { id: 'p2', sku_code: 'RIB', product_name: '二刀肉', category_code: 'PORK' }, { id: 'p3', sku_code: 'CABBAGE', product_name: '白菜', category_code: 'VEG' }, { id: 'p4', sku_code: 'POTATO', product_name: '土豆', category_code: 'VEG' }, ]; const porkHits = proposePrepInlineHits({ reportType: 'quarantine', sourcePrepId: 'p1', sourceSkuCode: 'BELLY', sourceCategoryCode: 'PORK', dayItems, aiNames: [], }); expect(porkHits.map((h) => h.sku_code).sort()).toEqual(['BELLY', 'RIB']); const vegHits = proposePrepInlineHits({ reportType: 'pesticide', sourcePrepId: 'p3', sourceSkuCode: 'CABBAGE', sourceCategoryCode: 'VEG', dayItems, aiNames: ['白菜'], }); expect(vegHits.map((h) => h.sku_code).sort()).toEqual(['CABBAGE']); }); it('确认勾选与候选求交,空选为空', () => { const candidates = [ { id: 'p3', sku_code: 'CABBAGE', product_name: '白菜', category_code: 'VEG' }, { id: 'p4', sku_code: 'POTATO', product_name: '土豆', category_code: 'VEG' }, ]; expect(intersectPrepInlineHits(candidates, ['p4', 'ghost']).map((h) => h.id)).toEqual(['p4']); expect(intersectPrepInlineHits(candidates, []).length).toBe(0); expect(intersectPrepInlineHits(candidates, null).length).toBe(0); }); it('Web 覆盖勾选:检疫同分类展开,农残/暂存不扩散', () => { const dayItems = [ { sku_code: 'BELLY', category_code: 'PORK' }, { sku_code: 'RIB', category_code: 'PORK' }, { sku_code: 'CABBAGE', category_code: 'VEG' }, ]; expect( skuCodesCoveredByDayReport( { sku_code: 'BELLY', category_code: 'PORK', share_mode: 'leaf_category', }, dayItems ).sort() ).toEqual(['BELLY', 'RIB']); expect( skuCodesCoveredByDayReport( { sku_code: 'CABBAGE', category_code: 'VEG', share_mode: 'manual', linked_sku_codes: ['CABBAGE'], }, dayItems ) ).toEqual(['CABBAGE']); expect( skuCodesCoveredByDayReport( { sku_code: 'BELLY', category_code: 'PORK', share_mode: 'line', share_key: 'st|prep-belly', }, dayItems ) ).toEqual(['BELLY']); }); it('小程序隐藏报告上传模块;暂存确认仅 defer_coverage;Web 上传不改', () => { const root = path.join(__dirname, '../..'); const registry = readFileSync(path.join(root, 'miniprogram/modules/registry.js'), 'utf8'); const home = readFileSync(path.join(root, 'miniprogram/pages/home/home.js'), 'utf8'); const svc = readFileSync(path.join(__dirname, '../src/services/qualityReportService.ts'), 'utf8'); const supplierRoutes = readFileSync( path.join(__dirname, '../src/routes/supplier/supplierBusinessRoutes.ts'), 'utf8' ); const webPrep = readFileSync( path.join(__dirname, '../src/modules/workflow/routes/preparationRoutes.ts'), 'utf8' ); const webPurchase = readFileSync( path.join(__dirname, '../src/modules/workflow/routes/purchaseRoutes.ts'), 'utf8' ); expect(registry).toMatch(/if \(mod\.id === 'quality-reports'\) return false/); expect(home).toMatch(/id === 'quality-reports'\) return/); expect(supplierRoutes).toMatch(/deferPrepCoverage:\s*req\.body\?\.defer_coverage === true/); expect(supplierRoutes).toMatch(/prep_ids/); expect(supplierRoutes).toMatch(/candidate_hits/); expect(webPrep).not.toMatch(/deferPrepCoverage/); expect(webPurchase).not.toMatch(/deferPrepCoverage/); expect(svc).toContain("if (h.shareMode === 'produce_day') {\n ids.add(rid);"); expect(shareModeForReportType('pesticide')).toBe('produce_day'); }); it('行内上传走 multer 对象路径,不再把 base64 塞进 JSON', () => { const root = path.join(__dirname, '../..'); const prepJs = readFileSync( path.join(root, 'miniprogram/components/preparation-panel/index.js'), 'utf8' ); const requestJs = readFileSync(path.join(root, 'miniprogram/utils/request.js'), 'utf8'); const svc = readFileSync(path.join(__dirname, '../src/services/qualityReportService.ts'), 'utf8'); expect(prepJs).toContain('uploadQualityReportStorageFile'); expect(prepJs).not.toContain('data:image/jpeg;base64'); expect(prepJs).not.toContain('data:application/pdf;base64'); expect(requestJs).toContain('/storage/quality-reports/upload?path='); const helperStart = requestJs.indexOf('function uploadQualityReportStorageFile'); expect(helperStart).toBeGreaterThan(-1); const helper = requestJs.slice(helperStart, requestJs.indexOf('\nfunction ', helperStart + 10)); expect(helper).toContain('wx.uploadFile'); expect(helper).not.toContain('application/json'); expect(svc).toContain('const parsed = await resolveMatchUploadEntry(file)'); }); });