import { readFileSync } from 'node:fs'; import path from 'node:path'; import { describe, expect, it } from 'vitest'; import { statementHasLiveOrderLinkSql, statementLineCountedSql } from '../src/services/statementScopeSql'; /** * 无源空壳结算单(订单已被清除、只剩表头金额、零明细)必须**不展示、不计算**(2026-09-22 开发者要求)。 * 结算页列表本来就靠「挂到有效订单行」过滤;本测试钉住其余汇总也用了同一谓词。 */ describe('statementHasLiveOrderLinkSql', () => { it('供应商:只认「明细挂到存在且未软删的订单」', () => { const sql = statementHasLiveOrderLinkSql('ss', 'supplier'); expect(sql).toContain('statement_lines sl'); expect(sql).toContain('sl.statement_no = ss.statement_no'); expect(sql).toContain("sl.statement_type = 'supplier'"); expect(sql).toContain('o.deleted_at IS NULL'); expect(sql).not.toContain('o2.'); }); it('客户:额外认「statement_no 本身即订单号」的一单一结', () => { const sql = statementHasLiveOrderLinkSql('cs', 'customer'); expect(sql).toContain("sl.statement_type = 'customer'"); expect(sql).toContain('o2.order_code = cs.statement_no'); expect(sql).toContain('o2.deleted_at IS NULL'); }); it('只用列等值连接(不可索引的 id::text 写法会造成线上慢查询事故)', () => { for (const type of ['supplier', 'customer'] as const) { const sql = statementHasLiveOrderLinkSql('x', type); expect(sql).not.toMatch(/::text/); expect(sql).not.toContain('IS NOT DISTINCT FROM'); } }); }); describe('无源空壳单在汇总里已被排除(源码棘轮)', () => { const read = (rel: string) => readFileSync(path.join(__dirname, '..', rel), 'utf8'); it('财务概览 应收/应付聚合都带谓词', () => { const src = read('src/services/marginQueryService.ts'); expect(src).toContain("statementHasLiveOrderLinkSql('cs', 'customer')"); expect(src).toContain("statementHasLiveOrderLinkSql('ss', 'supplier')"); }); it('看板三处计数(客户待确认 / 供应商待确认 / 逾期未收款)都带谓词', () => { const src = read('src/services/dashboardService.ts'); const hits = src.match(/statementHasLiveOrderLinkSql\(/g) || []; expect(hits.length).toBeGreaterThanOrEqual(3); }); it('应收账龄催收与催收策略都带谓词', () => { expect(read('src/services/financeReconciliationService.ts')).toContain('statementHasLiveOrderLinkSql('); expect(read('src/services/collectionStrategyService.ts')).toContain( "statementHasLiveOrderLinkSql('cs', 'customer')" ); }); it('谓词实现集中在 statementScopeSql.ts(唯一实现,便于审计)', () => { const files = [ 'src/services/marginQueryService.ts', 'src/services/dashboardService.ts', 'src/services/financeReconciliationService.ts', 'src/services/collectionStrategyService.ts', ]; for (const f of files) { expect(read(f)).toContain("from './statementScopeSql'"); } }); }); // 「订单已软删的结算明细不计入金额」的口径必须唯一(2026-09-22:列表页漏了这条,页面应付虚高 ¥1,628.50)。 describe('statementLineCountedSql', () => { it('排除已作废行;排除订单已软删的行;保留无订单号的行', () => { const sql = statementLineCountedSql('stl'); expect(sql).toContain('COALESCE(stl.is_voided, false) = false'); expect(sql).toContain('stl.order_code IS NULL'); expect(sql).toContain('o.order_code = stl.order_code AND o.deleted_at IS NULL'); expect(sql).not.toMatch(/::text/); }); it('生成/重算/列表/明细四处共用同一片段', () => { const fr = readFileSync(path.join(__dirname, '..', 'src/services/financeReconciliationService.ts'), 'utf8'); // 列表 SQL、getStatementLines 重算、applyStatementRecalcs 求和 const hits = fr.match(/statementLineCountedSql\(/g) || []; expect(hits.length).toBeGreaterThanOrEqual(3); const asa = readFileSync(path.join(__dirname, '..', 'src/services/afterSaleStatementAdjustService.ts'), 'utf8'); expect(asa).toContain('statementLineCountedSql('); // 不再出现「只过滤 is_voided」的旧写法 expect(fr).not.toContain('SUM(line_amount) FILTER (WHERE COALESCE(is_voided, false) = false), 0)::numeric AS active_total'); }); });