Files
SupplierSystem/server/tests/financePayableCardDateCaliber.test.ts
Ubuntu 5fa2bbfd55
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(audit-20261019): T1-T9 审计修复、存储归属与审计日志列宽
- T1 资金/库存/报价/自采 P1(BIZ-1/2/3/4/10/13/14)
- T2 主系统反向事件墓碑/改派/孤儿处理(BIZ-6/8/9)
- T3 选分公司 ticket、trust proxy、角色权限收口(SEC-2/3/4)
- T4 缺列/RLS/tenant 名单/FK/迁移执行器(DATA-1..8)
- T5 401 登出/分页/映射口径/blob/ZIP 中文名(FE-1..4/BIZ-15)
- T6 测试脚本目标守卫/config fail-fast/容器配置(OPS-2..8)
- T7 前后端测试与 lint 门禁清零
- T8 应付卡片按关联订单日(口径 A)
- T9 存储对象全链路归属校验、敏感列禁写、审计 target_id 超长修复
2026-10-19 16:16:51 +08:00

170 lines
8.1 KiB
TypeScript
Raw Permalink 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';
/**
* T8 / BIZ-5(2026-10-19,用户批准口径 A):应付卡片与应付列表统一按**关联订单日**。
*
* 缺陷:卡片按 `supplier_statements.cycle_code` 账期区间重叠统计,列表按关联订单的
* 配送日/下单日/入库日过滤 ⇒ 账期日与订单日跨窗口时两边数字相反:
* 审计样本 `SS20261015001 | cycle_code=20261015 | 订单 delivery_date=2026-10-16`
* ⇒ 10-15 窗口卡片有、列表无;10-16 窗口反过来。
*
* 修复:卡片改用与列表**同一个** `statementLinkedOrdersInDateRangeSql('ss','supplier',dateField,1,2)`。
* 本文件:① 源码契约(卡片必须复用该谓词、不再用 cycle 重叠);② 真实 PG(临时表)跨窗口对照。
*/
const marginSource = readFileSync(new URL('../src/services/marginQueryService.ts', import.meta.url), 'utf8');
const financeSource = readFileSync(
new URL('../src/services/financeReconciliationService.ts', import.meta.url),
'utf8'
);
describe('T8 源码契约:应付卡片复用列表的关联订单日谓词', () => {
it('financeReconciliationService 导出 statementLinkedOrdersInDateRangeSql', () => {
expect(financeSource).toContain('export function statementLinkedOrdersInDateRangeSql(');
});
it('marginQueryService 的应付卡片使用同一谓词(ss/supplier/dateField/1/2)', () => {
expect(marginSource).toContain(
"statementLinkedOrdersInDateRangeSql('ss', 'supplier', dateField, 1, 2)"
);
});
it('应付卡片不再用 cycle_code 重叠统计(cycleOverlapSqlPay 已移除)', () => {
const start = marginSource.indexOf('const payParams: unknown[] = [rangeFromDate, rangeToDate]');
expect(start).toBeGreaterThan(-1);
const block = marginSource.slice(start, start + 1400);
expect(block).not.toContain('cycleOverlapSqlPay');
expect(block).not.toContain('cycleOverlapSql');
// payable_amount 汇总口径保留
expect(block).toContain('SUM(ss.payable_amount)');
});
});
/* ── 真实 PG:临时表模拟跨窗口样本,卡片/列表同窗口一致(无库时跳过) ───────────── */
type PgClient = {
connect: () => Promise<void>;
end: () => Promise<void>;
query: (sql: string, params?: unknown[]) => Promise<{ rows: Array<Record<string, unknown>> }>;
};
async function loadPgClient(): Promise<PgClient | null> {
const { loadEnv } = await import('../src/env');
loadEnv();
if (!process.env.DATABASE_URL) return null;
const pg = (await import('pg')).default as unknown as {
Client: new (cfg: { connectionString: string }) => PgClient;
};
const client = new pg.Client({ connectionString: process.env.DATABASE_URL });
try {
await client.connect();
} catch {
return null;
}
return client;
}
const CYCLE_OVERLAP_SQL = `
(CASE
WHEN ss.cycle_code ~ '^\\d{8}$' THEN to_date(ss.cycle_code, 'YYYYMMDD')
END) <= $2::date
AND
(CASE
WHEN ss.cycle_code ~ '^\\d{8}$' THEN to_date(ss.cycle_code, 'YYYYMMDD')
END) >= $1::date`;
describe('T8 真实 PG:cycle_code 与关联订单日跨窗口时两面一致', () => {
it('10-15 两边都为 0;10-16 两边都 13.80/1(旧 cycle 口径正好相反)', async (ctx) => {
const client = await loadPgClient();
if (!client) {
ctx.skip();
return;
}
const { statementLinkedOrdersInDateRangeSql } = await import('../src/services/financeReconciliationService');
try {
await client.query('BEGIN');
await client.query(`
CREATE TEMP TABLE supplier_statements (
statement_no text, cycle_code text, supplier_code text, branch_code text,
payable_amount numeric, statement_status int, total_amount numeric, deduction_amount numeric
) ON COMMIT DROP;
CREATE TEMP TABLE statement_lines (
statement_no text, statement_type text, order_code text, line_amount numeric
) ON COMMIT DROP;
CREATE TEMP TABLE orders (
order_code text, delivery_date date, created_at timestamptz, updated_at timestamptz, deleted_at timestamptz
) ON COMMIT DROP;
CREATE TEMP TABLE inspections (
order_code text, inspection_time timestamptz, inbound_batch_no text
) ON COMMIT DROP;
`);
await client.query(`SET search_path TO pg_temp`);
await client.query(`
INSERT INTO supplier_statements (statement_no, cycle_code, supplier_code, branch_code, payable_amount, statement_status)
VALUES ('SS20261015001', '20261015', 'SUP1', 'BR002', 13.80, 10);
INSERT INTO statement_lines (statement_no, statement_type, order_code, line_amount)
VALUES ('SS20261015001', 'supplier', 'ORD_X', 13.80);
INSERT INTO orders (order_code, delivery_date, created_at, updated_at, deleted_at)
VALUES ('ORD_X', '2026-10-16', '2026-10-15T10:00:00+08:00', '2026-10-17T10:00:00+08:00', NULL);
`);
const cardSql = (predicate: string) =>
`SELECT COALESCE(SUM(ss.payable_amount), 0)::numeric AS total,
COALESCE(SUM(CASE WHEN COALESCE(ss.statement_status,0) < 30 AND COALESCE(ss.payable_amount,0) > 0 THEN 1 ELSE 0 END), 0)::int AS cnt
FROM supplier_statements ss WHERE ${predicate}`;
const listSql = (predicate: string) =>
`SELECT COALESCE(SUM(ss.payable_amount), 0)::numeric AS total, COUNT(*)::int AS cnt
FROM supplier_statements ss WHERE ${predicate}`;
const runWindow = async (from: string, to: string) => {
const params = [from, to];
const linked = statementLinkedOrdersInDateRangeSql('ss', 'supplier', 'delivery_date', 1, 2);
const card = (await client.query(cardSql(linked), params)).rows[0];
const list = (await client.query(listSql(linked), params)).rows[0];
const oldCard = (
await client.query(cardSql(`(${CYCLE_OVERLAP_SQL.replace(/\$1/g, '$1').replace(/\$2/g, '$2')})`), params)
).rows[0];
return {
card: { total: Number(card.total), cnt: Number(card.cnt) },
list: { total: Number(list.total), cnt: Number(list.cnt) },
oldCard: { total: Number(oldCard.total), cnt: Number(oldCard.cnt) },
};
};
const d15 = await runWindow('2026-10-15', '2026-10-15');
const d16 = await runWindow('2026-10-16', '2026-10-16');
// 修复后:同一窗口卡片与列表完全一致
expect(d15.card).toEqual({ total: 0, cnt: 0 });
expect(d15.list).toEqual({ total: 0, cnt: 0 });
expect(d16.card).toEqual({ total: 13.8, cnt: 1 });
expect(d16.list).toEqual({ total: 13.8, cnt: 1 });
// 修复前(cycle_code 重叠)恰好相反 —— 证明缺陷存在且已消除
expect(d15.oldCard).toEqual({ total: 13.8, cnt: 1 });
expect(d16.oldCard).toEqual({ total: 0, cnt: 0 });
// 同窗口两面相等(核心验收)
expect(d15.card).toEqual(d15.list);
expect(d16.card).toEqual(d16.list);
// dateField=created_at 也同轴:订单下单日 10-15
const createdLinked = statementLinkedOrdersInDateRangeSql('ss', 'supplier', 'created_at', 1, 2);
const createdCard = (await client.query(cardSql(createdLinked), ['2026-10-15', '2026-10-15'])).rows[0];
const createdList = (await client.query(listSql(createdLinked), ['2026-10-15', '2026-10-15'])).rows[0];
expect(Number(createdCard.total)).toBe(13.8);
expect(Number(createdCard.total)).toBe(Number(createdList.total));
expect(Number(createdCard.cnt)).toBe(Number(createdList.cnt));
// dateField=inbound(无验收记录 → 兜底 orders.updated_at=10-17)两面同轴
const inboundLinked = statementLinkedOrdersInDateRangeSql('ss', 'supplier', 'inbound', 1, 2);
const inboundCard = (await client.query(cardSql(inboundLinked), ['2026-10-17', '2026-10-17'])).rows[0];
const inboundList = (await client.query(listSql(inboundLinked), ['2026-10-17', '2026-10-17'])).rows[0];
expect(Number(inboundCard.total)).toBe(13.8);
expect(Number(inboundCard.total)).toBe(Number(inboundList.total));
} finally {
await client.query('ROLLBACK').catch(() => undefined);
await client.end();
}
});
});