import { readFileSync } from 'node:fs'; import { describe, expect, it } from 'vitest'; import { buildProgressStatusHint } from '../src/services/orderStatusDisplayService'; /** * 客户「是否取货」(自提,无配送)—— 2026-10-15 开发者需求。 * 验收清单见 `.team/customer-self-pickup/requirements.md`(团队产物,不入库)。 * * 核心口径:客户级开关 → **录单时快照到 `orders.self_pickup`** → 该单照常备货/验收/**分拣** * (分拣完毕 = 出库 = 扣库存)→ 停 **50「待取货」** → **管理员**点「确认取货」→ 结案 90 * (复用 `completeOrderWithoutDriverDelivery`,**不动状态链**);**不建配送任务、不派车**。 * * 本文件同时钉住"**不影响其它业务功能**":改动都是**附加式**守卫/字段, * 且必须放在原有各守卫之后、`pool.connect()` 之前(池满自锁是既有教训)。 */ const read = (rel: string) => readFileSync(new URL(rel, import.meta.url), 'utf8'); const migration = read('../../migrations/20261049_customer_self_pickup.sql'); const submit = read('../src/services/customerOrderSubmitService.ts'); const fulfillment = read('../src/services/orderWorkflowFulfillmentService.ts'); const listQuery = read('../src/services/orderManagementQueryService.ts'); const routes = read('../src/modules/workflow/routes/orderRoutes.ts'); const service = read('../src/services/selfPickupService.ts'); describe('自提 · 数据模型(只加列、默认 false ⇒ 存量行为不变)', () => { it('迁移只加两列,且都是 NOT NULL DEFAULT false;不新建表、不动表名单', () => { expect(migration).toContain('ADD COLUMN IF NOT EXISTS is_self_pickup boolean NOT NULL DEFAULT false'); expect(migration).toContain('ADD COLUMN IF NOT EXISTS self_pickup boolean NOT NULL DEFAULT false'); expect(migration).not.toContain('CREATE TABLE'); // 表名单**重定义**不得出现(注释里提到名字没关系);只加列 ⇒ 不动 tenant_business_tables() expect(migration).not.toContain('CREATE OR REPLACE FUNCTION public.tenant_business_tables'); // 幂等 + 覆盖 tenant_template(新分公司克隆得到) expect(migration).toContain('ADD COLUMN IF NOT EXISTS'); expect(migration).toContain('tenant_template'); }); it('录单时从客户主数据**快照**(子查询一处收口;客户开关事后改了不影响已有单)', () => { expect(submit).toContain('self_pickup'); // 客户码用**独立参数**($13),不复用前面 VALUES 里的 $4 —— 复用会让 PG 报 // 「inconsistent types deduced for parameter $4」(text vs varchar),本地录单直接 500。 expect(submit).toContain( 'COALESCE((SELECT c.is_self_pickup FROM customers c WHERE c.customer_code = $13 LIMIT 1), false)' ); expect(submit).not.toContain('c.customer_code = $4'); }); }); describe('自提 · 不建配送任务 & 不派车(附加式守卫)', () => { it('ensureDeliveryTaskForOrder:自提单跳过,且位于原有三道守卫之后、pool.connect() 之前', () => { const fnStart = fulfillment.indexOf('export async function ensureDeliveryTaskForOrder'); const body = fulfillment.slice(fnStart, fnStart + 4000); const retailAt = body.indexOf('isRetailOrder('); const directAt = body.indexOf('isOrderOnlyDirectDelivery('); const independentAt = body.indexOf('isOrderIndependentInbound('); const selfPickupAt = body.indexOf('isSelfPickupOrder('); // 注意:注释里也出现过 `pool.connect()` 字样 ⇒ 必须锚定**真实调用**那一行 const connectAt = body.indexOf('const ownClient = existingClient ? null : await pool.connect();'); expect(selfPickupAt).toBeGreaterThan(-1); expect(selfPickupAt).toBeGreaterThan(retailAt); expect(selfPickupAt).toBeGreaterThan(directAt); expect(selfPickupAt).toBeGreaterThan(independentAt); expect(connectAt).toBeGreaterThan(selfPickupAt); // 必须早于取连接(池满自锁) expect(body).toContain("if (await isSelfPickupOrder(code, existingClient || pool)) return '';"); // 原有三道守卫仍在(防误删回归) expect(body).toContain('isRetailOrder('); expect(body).toContain('isOrderOnlyDirectDelivery('); expect(body).toContain('isOrderIndependentInbound('); }); it('tryAdvanceOrderToDelivering:自提单发车被拒(400/self_pickup_no_delivery),且在任何扣库之前', () => { const fnStart = fulfillment.indexOf('export async function tryAdvanceOrderToDelivering'); const fnEnd = fulfillment.indexOf('export async function', fnStart + 10); const body = fulfillment.slice(fnStart, fnEnd); const guardAt = body.indexOf("code: 'self_pickup_no_delivery'"); const deductAt = body.indexOf('deductInventoryForOrderDeparture('); const earlyReturn60At = body.indexOf('if (status >= 60) {'); const earlyReturn50At = body.indexOf('if (status < 50) {'); expect(guardAt).toBeGreaterThan(-1); expect(deductAt).toBeGreaterThan(guardAt); // 拦在扣库之前 // 必须在两条早退**之后**:否则 orderStatusReconcileService 周期性调本函数时, // 已 60/90 的自提单会从 advanced:true 变成抛错(分析代理 2026-10-15 指出) expect(earlyReturn60At).toBeGreaterThan(-1); expect(earlyReturn50At).toBeGreaterThan(earlyReturn60At); expect(guardAt).toBeGreaterThan(earlyReturn50At); expect(body).toContain('该单为客户自提(不走配送):请在订单列表点「确认取货」结案'); // 读取 self_pickup 的那条 SQL 里带上该列 expect(body).toContain('COALESCE(self_pickup, false) AS self_pickup'); }); }); describe('自提 · 列表派生(50 显示「待取货」,与主系统免入库区分来源)', () => { it('列表查询与行映射都带上 self_pickup,且不影响既有 is_skip_inbound', () => { expect(listQuery).toContain('COALESCE(o.self_pickup, false) AS self_pickup'); expect(listQuery).toContain('self_pickup: row.self_pickup === true,'); // 既有主系统免入库派生仍在 expect(listQuery).toContain('is_skip_inbound: skipInboundMap.get(orderCode) === true,'); // 行类型补了字段 expect(listQuery).toContain('self_pickup?: boolean;'); }); /** * 2026-10-16 真机报障「为什么不上待取货」:徽标已是「待取货」(前端派生), * 但**副文案**还是「待配送员开始配送」—— `buildProgressStatusHint` 只认免入库, * 自提单传下去还是走普通 50 分支。副文案必须与徽标同口径(同一坑 2026-10-11 免入库踩过)。 */ it('50 的副文案区分来源:自提单不得写「待配送员开始配送」', () => { const facts = {} as unknown as Parameters[2]; expect(buildProgressStatusHint(50, 'none', facts, undefined, undefined, false, true)).toBe( '待客户上门取货' ); // 免入库口径不受影响 expect(buildProgressStatusHint(50, 'none', facts, undefined, undefined, true, false)).toBe( '待主系统骑手上门取货' ); // 普通单仍是原文案 expect(buildProgressStatusHint(50, 'none', facts, undefined, undefined, false, false)).toBe( '待配送员开始配送' ); // 列表侧必须把 self_pickup 作为第 7 个参数传进去,否则上面这条分支永远不生效 expect(listQuery).toContain('row.self_pickup === true'); }); }); describe('自提 · 确认取货(管理员)', () => { it('接口存在、要求权限 + **仅管理员**', () => { expect(routes).toContain("router.post('/orders/:orderCode/self-pickup-complete'"); expect(routes).toContain("requireAdminRole(ctx, '仅管理员可确认客户自提取货')"); expect(routes).toContain("requireWorkflowPermission(ctx, ['order_manage', 'order_edit', 'order_status_update'])"); expect(routes).toContain('selfPickupComplete'); }); it('服务:只允许自提单、只允许停在 50、已 90 幂等;走无配送结案入口 + 写审计', () => { expect(service).toContain('const ALLOWED_CLOSE_STATUSES = new Set([50]);'); expect(service).toContain("'not_self_pickup'"); expect(service).toContain("'invalid_state'"); expect(service).toContain('if (status >= 90)'); expect(service).toContain('completeOrderWithoutDriverDelivery(orderCode, undefined, { settleInventory: true })'); expect(service).toContain("operationType: 'self_pickup_complete'"); // 不动状态链:本文件不得出现状态常量表或裸推进 expect(service).not.toContain('ORDER_STATUS_ALLOWED'); expect(service).not.toContain('advanceOrderWorkflow'); }); }); describe('自提 · 状态链未被改动(订单冻结红线)', () => { it('未新增任何状态边(45→90 等)', () => { const statusKernel = read('../src/modules/kernel/orderStatus.ts'); expect(statusKernel).toContain('60: [90],'); expect(statusKernel).not.toContain('45: [90]'); expect(statusKernel).not.toContain('45: [50, 90]'); }); });