import { readFileSync } from 'fs'; import { describe, expect, it } from 'vitest'; import { extraDishPhotosSatisfied, loadingPhotosSatisfied, } from '../src/services/deliveryLoadingService'; const src = readFileSync(new URL('../src/services/deliveryLoadingService.ts', import.meta.url), 'utf8'); function sliceFn(name: string, nextExport: string) { const start = src.indexOf(name); const end = src.indexOf(nextExport); expect(start).toBeGreaterThan(0); expect(end).toBeGreaterThan(start); return src.slice(start, end); } describe('配送核货收容加菜(司机自买仍须核)', () => { it('分拣快照与漏行均不排除加菜;跨拣货单按订单合并', () => { const loadingSql = sliceFn('const LOADING_ITEMS_SQL', 'function mapLoadingItem'); expect(loadingSql).not.toContain('COALESCE(oi.is_extra_dish, false) = false'); expect(loadingSql).toContain('COALESCE(oi.is_extra_dish, false) AS is_extra_dish'); expect(loadingSql).toContain('pi.order_code = $1'); expect(loadingSql).not.toContain('pi.pick_code = $1'); const missingSql = sliceFn('const MISSING_NON_EXTRA_LOADING_SQL', 'async function listLoadingItems'); expect(missingSql).not.toContain('COALESCE(oi.is_extra_dish, false) = false'); expect(missingSql).toContain('COALESCE(oi.is_extra_dish, false) AS is_extra_dish'); expect(missingSql).toContain('SELECT 1 FROM picking_items pi WHERE pi.order_item_id = oi.id'); expect(missingSql).toContain('oi.order_code = $1'); const listFn = sliceFn('async function listLoadingItems', 'async function markLoadingCompleteIfReady'); expect(listFn).toContain('MISSING_NON_EXTRA_LOADING_SQL'); expect(listFn).not.toContain('INSERT INTO picking_items'); expect(src).toContain('displayPickedMeasureForUnit'); expect(loadingSql).toContain('pi.picked_qty'); }); it('跳过分拣补建仍不把加菜写入仓内拣货单', () => { const ensure = sliceFn( 'async function ensureDeliveryLoadingPickTask', 'const LOADING_ITEMS_SQL' ); expect(ensure).toContain('listExtraDishOrderItemIds'); expect(ensure).toContain('!extraIds.has(String(item.id))'); expect(ensure).toContain('WHERE pi.order_code = $1'); }); it('点有货可为加菜漏行 INSERT,不再拒绝加菜确认', () => { expect(src).toContain('async function resolveOrCreatePickingItemForLoading'); expect(src).not.toContain("code: 'extra_dish'"); expect(src).not.toContain('加菜商品无需配送员核货'); const resolve = sliceFn( 'async function resolveOrCreatePickingItemForLoading', 'export async function confirmDeliveryLoadingItem' ); expect(resolve).toContain('INSERT INTO picking_items'); expect(resolve).not.toContain('isExtraDishFlag(row.is_extra_dish)'); }); it('扫码走确认同一套显隐,不直接 UPDATE 漏行', () => { const scan = sliceFn( 'export async function scanDeliveryLoadingBarcode', 'export async function assertDeliveryLoadingComplete' ); expect(scan).toContain('confirmDeliveryLoadingItem'); expect(scan).toContain('listLoadingItems'); expect(scan).not.toMatch(/UPDATE picking_items SET loaded_qty/); }); it('空列表不视为核货完成(避免全加菜假完成发车)', () => { expect(src).toContain('const allLoaded = items.length > 0 && items.every((item) => item.loaded_done)'); expect(src).toContain('const allLoaded = loadedCount === items.length && items.length > 0'); }); it('加菜必须核货照;未拍不能算送达就绪', () => { expect( loadingPhotosSatisfied( [{ needs_loading_photo: true, loading_photo_count: 0, is_extra_dish: true }], false ) ).toBe(false); expect( extraDishPhotosSatisfied([ { is_extra_dish: true, loaded_done: true, loading_photo_count: 0 }, ]) ).toBe(false); expect( extraDishPhotosSatisfied([ { is_extra_dish: true, loaded_done: true, loading_photo_count: 1 }, ]) ).toBe(true); expect(src).toContain('task_status) >= 20 && !isExtraDishFlag(item.is_extra_dish)'); expect(src).toContain('export async function assertExtraDishLoadingPhotos'); }); });