Files
SupplierSystem/server/tests/freeCostPaymentVoucher.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

168 lines
6.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 { describe, expect, it } from 'vitest';
import {
PAYMENT_AMOUNT_TOLERANCE,
assertFreeCostPaymentStoragePath,
normalizePaidAt,
normalizePaymentRange,
payerNameMatches,
pickPaymentSuggestion,
rankPaymentCandidates,
toDateOnly,
toIsoString,
type PaymentCandidate,
} from '../src/services/freeCostPaymentVoucherService';
/**
* 其他成本付款凭证:一对一核销的纯逻辑部分。
* 设计:docs/design/20261008-free-cost-payment-voucher.md
* · 时间范围作用于其他成本的入账日(cycle_code,YYYYMMDD)
* · 先按金额(容差 ±0.05),金额并列才看付款人
* · 并列第一 = 多解 → 不给建议,交人工
*/
function candidate(part: Partial<PaymentCandidate> & { id: string }): PaymentCandidate {
return {
amount: 0,
cycle_code: '20261001',
created_at: null,
description: '',
subtype_label: '',
applicant_id: null,
applicant_name: '',
cost_voucher_urls: [],
amount_matched: false,
payer_matched: false,
score: 0,
...part,
};
}
describe('付款凭证 · 金额容差与排序', () => {
it('容差是 0.05(抹零/手续费不算不匹配)', () => {
expect(PAYMENT_AMOUNT_TOLERANCE).toBe(0.05);
});
it('金额命中优先于付款人命中', () => {
const ranked = rankPaymentCandidates([
candidate({ id: 'payer-only', score: 10, payer_matched: true }),
candidate({ id: 'amount-hit', score: 100, amount_matched: true }),
]);
expect(ranked.map((row) => row.id)).toEqual(['amount-hit', 'payer-only']);
});
it('同分时按入账日新→旧', () => {
const ranked = rankPaymentCandidates([
candidate({ id: 'old', score: 100, cycle_code: '20261001' }),
candidate({ id: 'new', score: 100, cycle_code: '20261005' }),
]);
expect(ranked.map((row) => row.id)).toEqual(['new', 'old']);
});
it('唯一命中给建议;并列第一不给建议并标多解', () => {
expect(pickPaymentSuggestion([candidate({ id: 'a', score: 100 })])).toEqual({
suggestedId: 'a',
ambiguous: false,
});
expect(
pickPaymentSuggestion([candidate({ id: 'a', score: 100 }), candidate({ id: 'b', score: 100 })])
).toEqual({ suggestedId: null, ambiguous: true });
});
it('全不命中(score=0)不给建议,也不算多解', () => {
expect(pickPaymentSuggestion([candidate({ id: 'a' }), candidate({ id: 'b' })])).toEqual({
suggestedId: null,
ambiguous: false,
});
});
});
describe('付款凭证 · 付款人比对', () => {
it('忽略空格与全角括号后互为子串即命中', () => {
expect(payerNameMatches('张涛', '张涛')).toBe(true);
expect(payerNameMatches('张涛(代付)', '张涛')).toBe(true);
expect(payerNameMatches('张 涛', '张涛')).toBe(true);
expect(payerNameMatches('李四', '张涛')).toBe(false);
});
it('空值不命中(不猜)', () => {
expect(payerNameMatches('', '张涛')).toBe(false);
expect(payerNameMatches('张涛', null)).toBe(false);
expect(payerNameMatches(null, undefined)).toBe(false);
});
});
describe('付款凭证 · 时间范围', () => {
it('合法 YYYYMMDD 原样使用;带横杠也能接受', () => {
expect(normalizePaymentRange('20261001', '20261008')).toEqual({
from: '20261001',
to: '20261008',
});
expect(normalizePaymentRange('2026-10-01', '2026-10-08')).toEqual({
from: '20261001',
to: '20261008',
});
});
it('起止颠倒时自动纠正', () => {
expect(normalizePaymentRange('20261008', '20261001')).toEqual({
from: '20261001',
to: '20261008',
});
});
it('缺省给最近 30 天(不倒挂)', () => {
const range = normalizePaymentRange(undefined, undefined);
expect(range.from).toMatch(/^\d{8}$/);
expect(range.to).toMatch(/^\d{8}$/);
expect(range.from <= range.to).toBe(true);
});
});
describe('付款凭证 · 存储路径前缀', () => {
it('只接受 free-cost-payment/ 前缀', () => {
expect(assertFreeCostPaymentStoragePath('free-cost-payment/a.jpg')).toBe('free-cost-payment/a.jpg');
expect(() => assertFreeCostPaymentStoragePath('free-cost/a.jpg')).toThrow();
expect(() => assertFreeCostPaymentStoragePath('free-cost-invoice/a.jpg')).toThrow();
expect(() => assertFreeCostPaymentStoragePath('')).toThrow();
});
});
describe('付款凭证 · 付款日归一', () => {
it('接受 8 位日期串,横杠/斜杠都能过', () => {
expect(normalizePaidAt('2026-10-08')).toBe('2026-10-08');
expect(normalizePaidAt('2026/10/08')).toBe('2026-10-08');
expect(normalizePaidAt('20261008')).toBe('2026-10-08');
});
it('位数不足/越界一律 null(不猜)', () => {
expect(normalizePaidAt('2026/1/8')).toBeNull();
expect(normalizePaidAt('2026-13-01')).toBeNull();
expect(normalizePaidAt('2026-10-32')).toBeNull();
expect(normalizePaidAt('0000-00-00')).toBeNull();
expect(normalizePaidAt(null)).toBeNull();
expect(normalizePaidAt('')).toBeNull();
});
});
describe('付款凭证 · 日期格式化(pg 的 Date 对象不能直接 String)', () => {
it('DATE 对象按**本地**日历日取,不受时区影响(pg 把 DATE 解析成本地午夜)', () => {
// 本地午夜:TZ=Asia/Shanghai 时 toISOString() 会给 2026-10-07(差一天),本地字段才是对的
expect(toDateOnly(new Date(2026, 9, 8))).toBe('2026-10-08');
expect(toDateOnly(new Date(2026, 0, 1))).toBe('2026-01-01');
});
it('DATE 对象 → YYYY-MM-DD;字符串原样规整', () => {
expect(toDateOnly(new Date('2026-10-08T00:00:00.000Z'))).toBe('2026-10-08');
expect(toDateOnly('2026-10-08')).toBe('2026-10-08');
expect(toDateOnly('2026-10-08T00:00:00.000Z')).toBe('2026-10-08');
expect(toDateOnly(null)).toBeNull();
});
it('TIMESTAMPTZ → ISO 字符串(不是 "Thu Oct 08 …")', () => {
const iso = toIsoString(new Date('2026-10-08T03:12:13.000Z'));
expect(iso).toBe('2026-10-08T03:12:13.000Z');
expect(String(iso)).not.toMatch(/^[A-Z][a-z]{2} /);
expect(toIsoString(null)).toBeNull();
});
});