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'); }); });