Files
SupplierSystem/server/tests/skuCostCategoryFromOrderLine.test.ts
T
Ubuntu deb3e72750
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
chore: 以 AI 生鲜配送系统 当前版本作为 SupplierSystem 起点
来源仓库:jxc/ai-fresh-delivery
来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d
快照树:  09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件)

按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据
(生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、
deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值),
整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。
供应商系统后续开发自本提交开始。
2026-10-08 17:35:43 +08:00

62 lines
2.8 KiB
TypeScript

import { readFileSync } from 'node:fs';
import { describe, expect, it } from 'vitest';
/**
* 订单商品成本的「分类」以订单行分类为基准(开发者 2026-09-22 批准)。
*
* 旧实现取 `skus.category_code`(商品主数据),而主数据会被录单手动选分类回写,
* 导致同一商品在本页与订单详情显示不一致,且按品类筛选查不到。
* 见 docs/design/20260922-sku-cost-category-from-order-line.md
*/
const src = readFileSync(
new URL('../src/services/marginQueryService.ts', import.meta.url),
'utf8'
);
const sliceFn = (name: string) => {
const start = src.indexOf(`export async function ${name}(`);
expect(start, `未找到 ${name}`).toBeGreaterThan(-1);
const next = src.indexOf('\nexport ', start + 10);
return src.slice(start, next === -1 ? undefined : next);
};
describe('sku cost category follows order line', () => {
const paged = sliceFn('getSkuDirectCostsPaged');
const lines = sliceFn('getSkuDirectCostLinesPaged');
it('列表:订单行分类进入聚合,代表分类 = 数量占比最大', () => {
expect(paged).toContain('oi.category_code AS line_category_code');
expect(paged).toContain('cat_qty AS (');
expect(paged).toContain(
'SELECT DISTINCT ON (sku_code, is_extra_dish, cost_bucket, is_warehouse_processing)'
);
expect(paged).toContain('cat_qty DESC, line_category_code');
expect(paged).toContain('line_category_code AS rep_category_code');
});
it('列表:分类名与分类筛选都走 rep_cat(订单行口径),不再用 skus.category_code', () => {
expect(paged).toContain('LEFT JOIN rep_cat rc');
expect(paged).toContain('WHERE c.category_code = rc.rep_category_code');
expect(paged).toContain('AND rc.rep_category_code = ANY(');
expect(paged).not.toContain('WHERE c.category_code = s.category_code');
expect(paged).not.toContain('AND s.category_code = ANY(');
});
it('列表:表头两个桶的合计仍不套来源过滤(前端两张表头共用一个 state)', () => {
const lt = paged.slice(paged.indexOf('ledger_totals AS ('));
const body = lt.slice(0, lt.indexOf('ranked AS ('));
expect(body).toContain("WHEN t.cost_bucket = 'self'");
expect(body).toContain("WHEN t.cost_bucket = 'supplier'");
// 不得引入 sourceFilter 条件,否则其中一张表头会变 0
expect(body).not.toContain('sourceFilter');
expect(body).toContain('WHERE NOT sc.is_warehouse_processing');
});
it('导出明细:一行一订单,分类与筛选都用该行 order_items.category_code', () => {
expect(lines).toContain('WHERE c.category_code = oi.category_code');
expect(lines).toContain('AND oi.category_code = ANY(');
expect(lines).not.toContain('WHERE c.category_code = s.category_code');
expect(lines).not.toContain('AND s.category_code = ANY(');
});
});