Files
SupplierSystem/server/tests/freeCostInvoiceMatch.test.ts
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

117 lines
4.8 KiB
TypeScript

import { readFileSync } from 'node:fs';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { describe, expect, it } from 'vitest';
import { assertFreeCostVoucherStoragePath } from '../src/services/freeCostService';
import { assertFreeCostInvoiceStoragePath } from '../src/services/freeCostInvoiceService';
import {
assertInvoiceLinkAmounts,
invoiceAmountClose,
invoiceFaceAmount,
parseInvoiceDateYmd,
pickSuggestedInvoiceIds,
rankInvoiceCandidates,
scoreInvoiceToEntry,
} from '../src/services/freeCostInvoiceMatch';
const here = dirname(fileURLToPath(import.meta.url));
describe('free cost invoice match scoring', () => {
const entry = {
id: 'a',
amount: 128.5,
created_at: '2026-09-18T02:00:00.000Z',
description: '停车费',
subtype_label: '杂费',
};
it('gives amount hit when total is within 0.05 or 0.5%', () => {
expect(invoiceAmountClose(128.5, 128.5)).toBe(true);
expect(invoiceAmountClose(128.55, 128.5)).toBe(true);
expect(invoiceAmountClose(200, 128.5)).toBe(false);
expect(invoiceFaceAmount({ total_amount: 10, amount: 99 })).toBe(10);
});
it('parses invoice dates and scores date/name overlap', () => {
expect(parseInvoiceDateYmd('2026年09月18日')).toBe('2026-09-18');
const scored = scoreInvoiceToEntry(
{
total_amount: 128.5,
invoice_date: '2026-09-18',
seller_name: '停车费',
},
entry
);
expect(scored.amount_matched).toBe(true);
expect(scored.date_matched).toBe(true);
expect(scored.name_matched).toBe(true);
expect(scored.score).toBe(105);
});
it('auto-suggests unique high amount match and withholds when two are close', () => {
const ranked = rankInvoiceCandidates({ total_amount: 100 }, [
{ id: '1', amount: 100, created_at: '2026-09-18T00:00:00+08:00', description: '', subtype_label: '' },
{ id: '2', amount: 40, created_at: '2026-09-18T00:00:00+08:00', description: '', subtype_label: '' },
]);
expect(pickSuggestedInvoiceIds(ranked)).toEqual(['1']);
const tied = rankInvoiceCandidates({ total_amount: 100 }, [
{ id: '1', amount: 100, created_at: '2026-09-10T00:00:00+08:00', description: '', subtype_label: '' },
{ id: '2', amount: 100, created_at: '2026-09-11T00:00:00+08:00', description: '', subtype_label: '' },
]);
expect(pickSuggestedInvoiceIds(tied)).toEqual([]);
});
it('rejects linking one invoice to multiple entries', () => {
expect(() => assertInvoiceLinkAmounts(100, [40, 60])).toThrow(/一条凭证/);
expect(() => assertInvoiceLinkAmounts(100, [100])).not.toThrow();
expect(() => assertInvoiceLinkAmounts(100, [99])).toThrow(/价税合计/);
expect(() => assertInvoiceLinkAmounts(null, [10])).not.toThrow();
});
});
describe('free cost invoice path and approval isolation', () => {
it('only allows free-cost-invoice/ and keeps voucher prefix separate', () => {
expect(assertFreeCostInvoiceStoragePath('free-cost-invoice/a.jpg')).toBe('free-cost-invoice/a.jpg');
expect(() => assertFreeCostInvoiceStoragePath('free-cost/a.jpg')).toThrow(/free-cost-invoice/);
expect(assertFreeCostVoucherStoragePath('free-cost/a.jpg')).toBe('free-cost/a.jpg');
expect(() => assertFreeCostVoucherStoragePath('free-cost-invoice/a.jpg')).toThrow(/仅支持/);
});
it('invoice handlers do not read finance-approval toggle', () => {
const files = [
'freeCostInvoiceService.ts',
'freeCostInvoiceMatch.ts',
'invoiceOcrService.ts',
].map((name) => readFileSync(join(here, '../src/services', name), 'utf8'));
const routes = readFileSync(
join(here, '../src/modules/workflow/routes/financeRoutes.ts'),
'utf8'
);
const extractBlock = routes.split("router.post('/cost/free-cost/extract-invoice'")[1]?.split(
"router.post('/cost/free-cost/invoices'"
)[0];
const saveBlock = routes.split("router.post('/cost/free-cost/invoices'")[1]?.split(
"router.post('/cost/free-cost/voucher-amount'"
)[0];
expect(extractBlock).toBeTruthy();
expect(saveBlock).toBeTruthy();
expect(files[0]).toMatch(/ids\.length > 1/);
for (const src of [...files, extractBlock || '', saveBlock || '']) {
expect(src).not.toMatch(/getFreeCostRequiresFinanceApproval/);
expect(src).not.toMatch(/require_finance_approval/);
expect(src).not.toMatch(/createFreeCostEntry/);
}
});
it('list entries still expose original fields and only append invoice keys', () => {
const routes = readFileSync(
join(here, '../src/modules/workflow/routes/financeRoutes.ts'),
'utf8'
);
expect(routes).toContain('amount_total: data.amountTotal');
expect(routes).toContain('status_counts: data.statusCounts');
expect(routes).toContain('decorateFreeCostRowsWithInvoices');
});
});