Files
SupplierSystem/server/tests/retailOrderSentinel.test.ts
T
Ubuntu 9aa7483244
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
fix(散客录单): 不再被通用推进显示成「待分拣」;客户显示名改为「散客录单」
开发者 2026-10-15 核查反馈两条:
①「为什么当前录单完毕 散客录单 直接库存抵扣完毕就是待分拣?其他单这样是正常的,但是散客录单是供应商的货物
   已经买出去了,录这个单纯粹就是抵扣库存的呀记录销售的呀」;
②「为什么还叫散客,应该叫散客录单」。

① 状态机收口(根因):`processingTaskService.maybeAdvanceOrderAfterFulfillment` 里本就有
「独立进货:全部入库即自动 90」的分支,而散客单没被拦 ⇒ 录完 + 全额抵扣后被推到 **45「待分拣」**。
现加散客守卫:**不参与状态自动推进**,停在 **30「待确认」**,由人在订单详情点「确认完成」走既有 30→90 通道。
与独立进货的区别写进注释:那张单是**进货**、入库即自动 90;这张单是**销售**、必须人工确认出库后才 90。
另:`confirmRetailOrderComplete` 增加**兼容归一化**(清分拣行 + 40/45/50 → 30 再收口),
让守卫上线前录下的历史散客单也能一键完成(实测:手工置 45 后确认 → 90 成功)。

② 显示名:`RETAIL_CUSTOMER_NAME`「散客」→「散客录单」;`20261046` 迁移同步改(新装即新名);
新增 `20261047_retail_customer_display_name.sql` 改**存量**行(只改旧默认名'散客'、
**不动**用户自定义名;RLS 安全:写入前注入 app.branch_code)。
踩到并修掉:`customers` **没有 `updated_at` 列**(照抄别的表会报错,迁移里已注明)。
本地已应用:br001/br002 均为「散客录单」;用户那张 45 的散客单已按新规则归一化为 30(清掉分拣行)。

真跑验证:建单 30 ⇒ 显式调 maybeAdvanceOrderAfterFulfillment ⇒ `{advanced:false,
message:'散客录单:停在待确认,由「确认完成」收口'}`、状态**仍 30**、分拣任务 **0**;
历史 45 单确认 → `{order_status:90, deducted:true, outbound_types:['retail_out']}`。

验证:server tsc 0 error;vitest 26 failed / 3032 passed(447 文件)——失败文件集**全部落在既有基线**
(comm 与基线清单比对为空),无新增;retail 两个测试文件 23 例全绿。
2026-10-15 09:58:45 +08:00

111 lines
5.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { readFileSync } from 'node:fs';
import { describe, expect, it } from 'vitest';
import {
RETAIL_CUSTOMER_CODE,
RETAIL_CUSTOMER_NAME,
isRetailCustomer,
nonRetailOrderSql,
retailCustomerOption,
retailOrderOnlySql,
} from '../src/services/retailOrder';
import { salesVisibleOrderSql } from '../src/services/independentInbound';
/**
* 散客录单(销售单)—— 哨兵主体与判据(2026-10-15 开发者立项,设计 §5 S1)
*
* ⚠️ 本文件的核心是**方向性不变量**:散客是**销售**,与「独立进货」(进货)**方向相反**。
* 最容易被后人写错的一步是"顺手把散客也加进销售排除名单" —— 那样散客就不计入销售额了。
*/
const src = readFileSync(new URL('../src/services/retailOrder.ts', import.meta.url), 'utf8');
describe('散客哨兵:标识与判据', () => {
it('编码/名字是固定哨兵(与真实客户编码不会冲突)', () => {
expect(RETAIL_CUSTOMER_CODE).toBe('__RETAIL__');
// 2026-10-15 开发者指示:显示名由「散客」改为「散客录单」(与功能名一致)
expect(RETAIL_CUSTOMER_NAME).toBe('散客录单');
// 改名迁移也必须把存量行的旧默认名改过来(且不动用户自定义名)
const renameMig = readFileSync(
new URL('../../migrations/20261047_retail_customer_display_name.sql', import.meta.url),
'utf8'
);
expect(renameMig).toContain("company_name = ''散客''");
expect(renameMig).toContain('__RETAIL__');
expect(RETAIL_CUSTOMER_CODE.startsWith('__')).toBe(true);
});
it('isRetailCustomer:只认精确编码,空值/空白/其它编码都 false', () => {
expect(isRetailCustomer('__RETAIL__')).toBe(true);
expect(isRetailCustomer(' __RETAIL__ ')).toBe(true);
expect(isRetailCustomer('__INDEPENDENT_INBOUND__')).toBe(false);
expect(isRetailCustomer('MAIN_BR002')).toBe(false);
expect(isRetailCustomer(null)).toBe(false);
expect(isRetailCustomer(undefined)).toBe(false);
expect(isRetailCustomer('')).toBe(false);
});
it('录单注入选项:不送货、不挂账期', () => {
const o = retailCustomerOption();
expect(o.customer_code).toBe(RETAIL_CUSTOMER_CODE);
expect(o.company_name).toBe(RETAIL_CUSTOMER_NAME);
expect(o.address).toBeNull();
expect(o.settlement_days).toBeNull();
expect(o.delivery_time).toBeNull();
});
});
describe('排除/限定片段:只能用在「散客不该出现的地方」', () => {
it('必须 NULL 安全(IS DISTINCT FROM,不是 <>)', () => {
expect(nonRetailOrderSql('o')).toContain('IS DISTINCT FROM');
expect(nonRetailOrderSql('o')).toContain(RETAIL_CUSTOMER_CODE);
// `<>` 会因为 customer_code IS NULL 把整批真实客户订单滤掉
expect(nonRetailOrderSql('o')).not.toMatch(/o\.customer_code\s*<>/);
expect(retailOrderOnlySql('o')).toContain('=');
});
it('跟随别名(不同查询用不同 orders 别名)', () => {
expect(nonRetailOrderSql('ord')).toContain('ord.customer_code');
expect(retailOrderOnlySql('ord')).toContain('ord.customer_code');
});
});
describe('方向性不变量(ratchet):散客必须留在销售面', () => {
it('销售排除片段(独立进货用)里**不得**出现散客编码', () => {
const sales = salesVisibleOrderSql('o');
expect(sales).toContain('__INDEPENDENT_INBOUND__');
// 关键:散客是销售,不能被销售口径排除
expect(sales).not.toContain(RETAIL_CUSTOMER_CODE);
});
it('散客模块**不提供**任何 salesVisible/销售排除函数(避免被误用)', () => {
expect(src).not.toMatch(/export function salesVisible/);
expect(src).not.toContain('销售口径排除');
});
it('反向验证:若把散客塞进销售排除片段 ⇒ 本断言必红', () => {
const injected = `${salesVisibleOrderSql('o')} AND o.customer_code IS DISTINCT FROM '${RETAIL_CUSTOMER_CODE}'`;
expect(injected).toContain(RETAIL_CUSTOMER_CODE); // 注入后确实含散客
expect(salesVisibleOrderSql('o')).not.toContain(RETAIL_CUSTOMER_CODE);
});
});
describe('客户行:新租户走运行时幂等补行(不重写 clone_tenant_schema)', () => {
it('ensureRetailCustomer 幂等且带 source=retail', () => {
const fn = src.slice(src.indexOf('export async function ensureRetailCustomer'));
expect(fn).toContain('ON CONFLICT (customer_code) DO NOTHING');
expect(fn).toContain("'retail'");
expect(fn).toContain('customers');
});
it('迁移只补存量租户、不重写 clone_tenant_schema(并行改动安全)', () => {
const mig = readFileSync(
new URL('../../migrations/20261046_retail_customer.sql', import.meta.url),
'utf8'
);
expect(mig).toContain('ensure_retail_customer');
expect(mig).toContain('ON CONFLICT (customer_code) DO NOTHING');
expect(mig).not.toMatch(/CREATE OR REPLACE FUNCTION public\.clone_tenant_schema/);
// RLS:写入前必须注入 app.branch_code
expect(mig).toContain("set_config('app.branch_code'");
});
});