DriftLedger/tests/accounting.test.ts
2026-07-13 13:34:27 +08:00

27 lines
2.3 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import { classify, commitMobileTransaction, deduplicate, importStatement, parseLedger, validateTransaction } from '../src/domain';
const ledger = parseLedger([{ path: 'main.bean', content: `2026-01-01 open Assets:Alipay CNY\n2026-01-01 open Expenses:Food CNY\n2026-01-01 open Expenses:Uncategorized CNY\n2026-01-01 open Income:Uncategorized CNY\ninclude "mobile.bean"\n` }]);
describe('复式账务', () => {
it('拒绝不平衡交易且精确处理小数', () => {
expect(validateTransaction({ date: '2026-01-02', narration: '午餐', postings: [{ account: 'Assets:Alipay', amount: '-0.1', currency: 'CNY' }, { account: 'Expenses:Food', amount: '0.1', currency: 'CNY' }] }, ledger).valid).toBe(true);
expect(validateTransaction({ date: '2026-01-02', narration: '错误', postings: [{ account: 'Assets:Alipay', amount: '-1', currency: 'CNY' }, { account: 'Expenses:Food', amount: '0.9', currency: 'CNY' }] }, ledger).errors[0]).toContain('不平衡');
});
it('只在校验通过后追加 mobile.bean', () => {
const commit = commitMobileTransaction({ date: '2026-01-02', narration: '午餐', postings: [{ account: 'Assets:Alipay', amount: '-20', currency: 'CNY' }, { account: 'Expenses:Food', amount: '20', currency: 'CNY' }] }, ledger, '');
expect(commit.content).toContain('Expenses:Food');
});
});
describe('账单导入', () => {
it('识别 CSV 并按规则生成草稿', () => {
const [event] = importStatement('交易时间,金额(元),收/支,交易对方,交易订单号\n2026-02-01,25.50,支出,咖啡店,A1', 'alipay-csv-v1');
const classified = classify(event, [{ id: 'coffee', priority: 10, counterpartyContains: '咖啡', channelAccount: 'Assets:Alipay', categoryAccount: 'Expenses:Food', hits: 0 }], ledger);
expect(classified.draft.postings).toEqual([{ account: 'Assets:Alipay', amount: '-25.50', currency: 'CNY' }, { account: 'Expenses:Food', amount: '25.50', currency: 'CNY' }]);
});
it('不会重复接收同一流水', () => {
const [event] = importStatement('交易时间,金额(元),收/支,交易订单号\n2026-02-01,1,支出,A1', 'alipay-csv-v1');
const result = deduplicate([event, event], new Set());
expect(result.accepted).toHaveLength(1); expect(result.duplicates).toHaveLength(1);
});
});