import { describe, expect, it, beforeEach } from 'vitest'; import { useAutomationStore } from '../src/store/automationStore'; import { parseLedger } from '../src/domain/ledger'; import type { ImportedEvent } from '../src/domain/types'; 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\n' }]); const sampleEvent: ImportedEvent = { id: 'ocr-test-1', occurredAt: '2026-07-13T14:30:00', amount: '24.5', currency: 'CNY', direction: 'expense', channel: 'WeChat', counterparty: '星巴克', memo: 'OCR识别', raw: {}, }; beforeEach(() => { useAutomationStore.getState().clear(); }); describe('automationStore', () => { it('addDetected 添加检测事件 + 统计', () => { useAutomationStore.getState().addDetected('ocr', sampleEvent); useAutomationStore.getState().addDetected('screenshot', { ...sampleEvent, id: 'ss-1' }); const state = useAutomationStore.getState(); expect(state.detected).toHaveLength(2); expect(state.stats.ocr).toBe(1); expect(state.stats.screenshot).toBe(1); }); it('processAll 走 BillPipeline 生成草稿', async () => { useAutomationStore.getState().addDetected('ocr', sampleEvent); await useAutomationStore.getState().processAll( ledger, [], [], [], { WeChat: 'Assets:Alipay' }, ); const state = useAutomationStore.getState(); expect(state.drafts.length).toBeGreaterThan(0); expect(state.detected).toHaveLength(0); // 处理后清空 expect(state.isProcessing).toBe(false); }); it('空 detected 时 processAll 无操作', async () => { await useAutomationStore.getState().processAll(ledger, [], [], [], {}); expect(useAutomationStore.getState().drafts).toHaveLength(0); }); it('confirmDraft 移除草稿并返回', async () => { useAutomationStore.getState().addDetected('ocr', sampleEvent); await useAutomationStore.getState().processAll(ledger, [], [], [], { WeChat: 'Assets:Alipay' }); const before = useAutomationStore.getState().drafts.length; const draft = useAutomationStore.getState().confirmDraft(0); expect(draft).not.toBeNull(); expect(useAutomationStore.getState().drafts.length).toBe(before - 1); }); it('rejectDraft 移除草稿', async () => { useAutomationStore.getState().addDetected('ocr', sampleEvent); await useAutomationStore.getState().processAll(ledger, [], [], [], { WeChat: 'Assets:Alipay' }); const before = useAutomationStore.getState().drafts.length; useAutomationStore.getState().rejectDraft(0); expect(useAutomationStore.getState().drafts.length).toBe(before - 1); }); it('clear 重置全部状态', () => { useAutomationStore.getState().addDetected('notification', sampleEvent); useAutomationStore.getState().clear(); const state = useAutomationStore.getState(); expect(state.detected).toHaveLength(0); expect(state.drafts).toHaveLength(0); expect(state.stats.notification).toBe(0); }); it('多来源事件混合处理', async () => { useAutomationStore.getState().addDetected('ocr', sampleEvent); useAutomationStore.getState().addDetected('sms', { ...sampleEvent, id: 'sms-1', channel: 'Bank:1234' }); useAutomationStore.getState().addDetected('notification', { ...sampleEvent, id: 'notif-1' }); await useAutomationStore.getState().processAll(ledger, [], [], [], { WeChat: 'Assets:Alipay', 'Bank:1234': 'Assets:Alipay' }); const state = useAutomationStore.getState(); expect(state.stats.ocr).toBe(1); expect(state.stats.sms).toBe(1); expect(state.stats.notification).toBe(1); }); });