import { createRequire } from 'node:module'; import { readFileSync } from 'node:fs'; import { dirname, join } from 'node:path'; import { fileURLToPath } from 'node:url'; import { describe, expect, it } from 'vitest'; import { getPromptDefault } from '../src/prompts/defaults'; import { parseFreeCostDocumentKind } from '../src/services/costAiService'; const here = dirname(fileURLToPath(import.meta.url)); const require = createRequire(import.meta.url); const { expandFreeCostGroupToEntries, pickInvoiceVoucher, normalizeDocumentKind, } = require(join(here, '../../miniprogram/utils/costManagement.js')); describe('free cost add invoice kind', () => { it('prompt default asks for document_kind and prefers voucher when unsure', () => { const tpl = getPromptDefault('cost.free.voucher_amount')!.systemTemplate; expect(tpl).toContain('"document_kind":"voucher"'); expect(tpl).toContain('吃不准时 document_kind 必须填 voucher'); expect(tpl).not.toMatch(/invoice\.ocr/); }); it('parse defaults unknown/missing kind to voucher', () => { expect(parseFreeCostDocumentKind('invoice')).toBe('invoice'); expect(parseFreeCostDocumentKind('发票')).toBe('invoice'); expect(parseFreeCostDocumentKind('voucher')).toBe('voucher'); expect(parseFreeCostDocumentKind('')).toBe('voucher'); expect(parseFreeCostDocumentKind(undefined)).toBe('voucher'); expect(parseFreeCostDocumentKind('receipt')).toBe('voucher'); expect(normalizeDocumentKind('发票')).toBe('invoice'); expect(normalizeDocumentKind('')).toBe('voucher'); }); it('split submit only attaches invoice local path on invoice images', () => { const group = { subtype: 'misc', amount: '30.00', remark: '', _amountTouched: false, vouchers: [ { path: 'free-cost/a.jpg', localPath: '/tmp/a.jpg', amount: 10, document_kind: 'invoice', }, { path: 'free-cost/b.jpg', localPath: '/tmp/b.jpg', amount: 20, document_kind: 'voucher', }, ], }; expect(expandFreeCostGroupToEntries(group)).toEqual([ { subtype: 'misc', amount: 10, remark: '', voucher_urls: [], invoice_local_path: '/tmp/a.jpg', }, { subtype: 'misc', amount: 20, remark: '', voucher_urls: ['free-cost/b.jpg'], invoice_local_path: '', }, ]); }); it('unsplit group with multiple invoices picks the closest amount', () => { const vouchers = [ { path: 'free-cost/a.jpg', localPath: '/tmp/a.jpg', amount: 40, document_kind: 'invoice' }, { path: 'free-cost/b.jpg', localPath: '/tmp/b.jpg', amount: 99, document_kind: 'invoice' }, { path: 'free-cost/c.jpg', localPath: '/tmp/c.jpg', amount: 12, document_kind: 'voucher' }, ]; const picked = pickInvoiceVoucher(vouchers, 100); expect(picked.localPath).toBe('/tmp/b.jpg'); const entries = expandFreeCostGroupToEntries({ subtype: 'misc', amount: '100.00', remark: '', _amountTouched: true, vouchers, }); expect(entries).toHaveLength(1); expect(entries[0].invoice_local_path).toBe('/tmp/b.jpg'); expect(entries[0].voucher_urls).toEqual(['free-cost/c.jpg']); }); it('history candidates already exclude linked cost_detail ids', () => { const src = readFileSync(join(here, '../src/services/freeCostInvoiceService.ts'), 'utf8'); expect(src).toMatch(/listUnlinkedMineEntries/); expect(src).toMatch(/NOT EXISTS/); expect(src).toMatch(/free_cost_invoice_links/); }); it('invoice-only rows skip voucher_url and satisfy required photo via invoice_photo', () => { const createSrc = readFileSync(join(here, '../src/services/freeCostService.ts'), 'utf8'); expect(createSrc).toMatch(/voucherPaths\.length < 1 && !input\.invoicePhoto/); const routes = readFileSync( join(here, '../src/modules/workflow/routes/financeRoutes.ts'), 'utf8' ); expect(routes).toMatch(/invoicePhoto: mpFreeCost && \(req\.body\?\.invoice_photo/); }); });