品牌与配置 - 重命名 Bean Mobile → 浮记(DriftLedger),slug 改为 drift-ledger - 所有 Config Plugin 从 app.json 动态读取 appId,替换硬编码 com.beancount.mobile - 预构建时自动从 constants.ts 同步支付包名列表到 Kotlin 端 - size-optimization 插件新增 NDK 版本强制统一(27.1.12297006) - 添加 app icon(direction_a_feather.png) Decimal 精度改造 - 全面消除 parseFloat 用于金额计算的场景,改用 string decimal 运算 - 新增 divideDecimals(除法)、toDateString(Date→YYYY-MM-DD) - negateDecimal 零值特殊处理,避免产生 "-0" - 年报/月报/预算/信用卡/汇率/图表统计等模块全部迁移 - 去重指纹用 parseDecimal 归一化金额格式 - 转账识别增加 normalizeAmount,支持不同精度格式配对(100 vs 100.00) 架构变更 - mobile.bean → main.bean:移除启动时合并迁移逻辑,直接读写 main.bean - TransactionDraft.sourceEventId → sourceEventIds(数组,支持转账双方) - 新增 buildPostings() 纯函数,从 transactionBuilder 提取复式分录构建逻辑 - 新增 csvUtils.ts(引号感知 CSV 行拆分),statements/adapters 共用 - 新增 accessibilityParser.ts:从 automationPipeline 提取微信/支付宝文本解析 - 新增 floatingBillManager.ts:悬浮窗草稿管理(LRU+超时)与排序数据构建 - 新增 txKeyStore.ts:交易级去重键持久化(原子写入+500条LRU) - ledger.ts 新增 resolveIncludes 选项,支持 include 指令递归解析 - metadataStore CRUD 用 createCrudActions 工厂消除7组实体重复代码 安全与持久化 - PIN 哈希改用带盐 SHA-256,兼容旧版无 salt 数据迁移 - storePersistence 从 persistTo 回调改为 subscribe 自动触发 - 持久化增加深比较跳过无变化写入 + 滞后重试 - 新增 flushPersistence(),App 切后台时立即刷盘 - CrashReporter 新增文件持久化(CrashFs 抽象) - settingsStore hydrate 期间跳过自动持久化(skipPersist 标志) 去重与管道 - batchDedup 改用日期索引 Map,候选查找从 O(n) 降至 O(1) - dedupAgainstHistory 按日期分组,仅扫描时间窗口内候选 - OcrProcessor 从布尔标志改为 Promise 队列,不再丢弃并发请求 - 去重缓存改用 Map<hash,timestamp> LRU,阈值超20%时批量淘汰 - 悬浮球全局开关(floatingBallEnabled) + 原生 bridge API 其他改进 - recurring 计算修复月/年溢出(1月31日+1月→2月28日,闰年2月29日) - 预算进度按 posting 分录金额求和(不再仅过滤负数金额) - recurring.ts 用 Date.parse 本地时间替代 toISOString 的 UTC 偏移 - 信用卡账单日/还款日限制不超过28,防止月份溢出 - 类别模糊匹配增加最小长度2,避免单字符误匹配 - 还款检测排除 refund 方向,避免退款被误判为还款 - WebDAV 同步增加 HTTP 日期格式解析 - Git 合并策略改为以 remote 为基础追加 local 独有行 性能优化 - commonStyles 全局 useMemo 缓存 - Button/Card 的 pressIn/pressOut 改用 useCallback - TransactionCard/AccountTreeNode 包裹 React.memo - PostingEditor 用 refs + 函数式更新避免重建 - CreditCard 页预计算账户余额 Map,避免渲染循环重复遍历 - OCR 处理器单例复用,配置变更时才重建 - getLocalAuth/getAccessibilityBridge 结果缓存 测试 - 新增 decimal/constants/channelConfig/transactionBuilder 单测 - 修复金额格式断言(-24.5→-24.50),匹配字符串精度行为 - billPipeline 新增不同格式金额配对、指纹唯一性测试
161 lines
7.0 KiB
TypeScript
161 lines
7.0 KiB
TypeScript
import { describe, expect, it } from 'vitest';
|
|
import { VoiceInput, MockAudioRecorder, MockSpeechToText, DEFAULT_VOICE_CONFIG } from '../src/ai/voiceInput';
|
|
import { calculatePosterStats, SharePosterService, MockViewShotter, type PosterConfig } from '../src/services/sharePoster';
|
|
import { exportToExcel, exportToExcelMultiSheet, exportToCsv, transactionToRow, transactionsToRows, MockExcelWorkbook } from '../src/services/exportToExcel';
|
|
import type { Transaction } from '../src/domain/types';
|
|
|
|
const tx = (over: Partial<Transaction>): Transaction => ({
|
|
id: 't1', date: '2026-07-10', flag: '*', postings: [], tags: [], links: [], source: 'mobile.bean', raw: '',
|
|
...over,
|
|
});
|
|
|
|
const sampleTx: Transaction[] = [
|
|
tx({ id: 't1', date: '2026-07-05', postings: [{ account: 'Assets:Alipay', amount: '-100', currency: 'CNY' }, { account: 'Expenses:Food', amount: '100', currency: 'CNY' }], tags: ['food'] }),
|
|
tx({ id: 't2', date: '2026-07-10', postings: [{ account: 'Assets:Bank', amount: '5000', currency: 'CNY' }, { account: 'Income:Salary', amount: '-5000', currency: 'CNY' }] }),
|
|
tx({ id: 't3', date: '2026-07-15', postings: [{ account: 'Assets:Alipay', amount: '-200', currency: 'CNY' }, { account: 'Expenses:Food', amount: '200', currency: 'CNY' }] }),
|
|
];
|
|
|
|
// ============ voiceInput ============
|
|
|
|
describe('voiceInput', () => {
|
|
it('DEFAULT_VOICE_CONFIG 默认 press-to-talk', () => {
|
|
expect(DEFAULT_VOICE_CONFIG.mode).toBe('press-to-talk');
|
|
});
|
|
|
|
it('startRecording 请求权限并开始', async () => {
|
|
const recorder = new MockAudioRecorder();
|
|
const stt = new MockSpeechToText('花了30元');
|
|
const voice = new VoiceInput(recorder, stt, { config: { id: 'x', name: 'x', apiKey: '', baseUrl: '', model: '' }, chat: async () => '{}' } as never);
|
|
await voice.startRecording();
|
|
expect(recorder.startCount).toBe(1);
|
|
});
|
|
|
|
it('未授权权限时 startRecording 抛错', async () => {
|
|
const recorder = new MockAudioRecorder();
|
|
recorder.permissionGranted = false;
|
|
const voice = new VoiceInput(recorder, new MockSpeechToText(), { config: { id: '', name: '', apiKey: '', baseUrl: '', model: '' }, chat: async () => '' } as never);
|
|
await expect(voice.startRecording()).rejects.toThrow('录音权限');
|
|
});
|
|
|
|
it('stopAndProcess 返回转写 + 解析', async () => {
|
|
const recorder = new MockAudioRecorder();
|
|
const stt = new MockSpeechToText('星巴克花了35');
|
|
const aiResponse = JSON.stringify({ type: 'expense', amount: '35', currency: 'CNY', counterparty: '星巴克', narration: '咖啡', time: '2026-07-13' });
|
|
const voice = new VoiceInput(recorder, stt, { config: { id: '', name: '', apiKey: '', baseUrl: '', model: '' }, chat: async () => aiResponse } as never);
|
|
await voice.startRecording();
|
|
const result = await voice.stopAndProcess();
|
|
expect(result.transcription).toBe('星巴克花了35');
|
|
expect(result.parsed?.type).toBe('draft');
|
|
});
|
|
|
|
it('空转写返回 null parsed', async () => {
|
|
const recorder = new MockAudioRecorder();
|
|
const stt = new MockSpeechToText('');
|
|
const voice = new VoiceInput(recorder, stt, { config: { id: '', name: '', apiKey: '', baseUrl: '', model: '' }, chat: async () => '' } as never);
|
|
await voice.startRecording();
|
|
const result = await voice.stopAndProcess();
|
|
expect(result.transcription).toBe('');
|
|
expect(result.parsed).toBeNull();
|
|
});
|
|
|
|
it('未录音时 stopAndProcess 抛错', async () => {
|
|
const voice = new VoiceInput(new MockAudioRecorder(), new MockSpeechToText(), { config: { id: '', name: '', apiKey: '', baseUrl: '', model: '' }, chat: async () => '' } as never);
|
|
await expect(voice.stopAndProcess()).rejects.toThrow('未在录音');
|
|
});
|
|
});
|
|
|
|
// ============ sharePoster ============
|
|
|
|
describe('sharePoster', () => {
|
|
it('calculatePosterStats month 类型', () => {
|
|
const config: PosterConfig = {
|
|
type: 'month', title: '7月报告', transactions: sampleTx, year: 2026, month: 7,
|
|
};
|
|
const stats = calculatePosterStats(config);
|
|
expect(stats.totalExpense).toBe('300');
|
|
expect(stats.totalIncome).toBe('5000');
|
|
expect(stats.transactionCount).toBe(3);
|
|
expect(stats.topCategory?.category).toContain('Food');
|
|
});
|
|
|
|
it('calculatePosterStats year 类型含 annualReport', () => {
|
|
const config: PosterConfig = {
|
|
type: 'year', title: '2026年报', transactions: sampleTx, year: 2026,
|
|
};
|
|
const stats = calculatePosterStats(config);
|
|
expect(stats.annualReport).toBeDefined();
|
|
expect(stats.annualReport?.transactionCount).toBe(3);
|
|
});
|
|
|
|
it('SharePosterService generateAndSave', async () => {
|
|
const shotter = new MockViewShotter();
|
|
const service = new SharePosterService(shotter);
|
|
const uri = await service.generateAndSave(
|
|
{ type: 'month', title: '测试', transactions: sampleTx, year: 2026, month: 7 },
|
|
'poster-ref',
|
|
);
|
|
expect(uri).toContain('poster-ref');
|
|
expect(shotter.captured).toHaveLength(1);
|
|
expect(shotter.savedToAlbum).toHaveLength(1);
|
|
});
|
|
|
|
it('SharePosterService generateAndShare', async () => {
|
|
const shotter = new MockViewShotter();
|
|
const service = new SharePosterService(shotter);
|
|
await service.generateAndShare(
|
|
{ type: 'month', title: '分享', transactions: [], year: 2026, month: 7 },
|
|
'ref',
|
|
);
|
|
expect(shotter.shared).toHaveLength(1);
|
|
});
|
|
|
|
it('previewStats 仅计算不截图', () => {
|
|
const shotter = new MockViewShotter();
|
|
const service = new SharePosterService(shotter);
|
|
const stats = service.previewStats({ type: 'month', title: 'x', transactions: sampleTx, year: 2026, month: 7 });
|
|
expect(stats.totalExpense).toBe('300');
|
|
expect(shotter.captured).toHaveLength(0);
|
|
});
|
|
});
|
|
|
|
// ============ exportToExcel ============
|
|
|
|
describe('exportToExcel', () => {
|
|
it('transactionToRow 转换', () => {
|
|
const row = transactionToRow(sampleTx[0]);
|
|
expect(row.日期).toBe('2026-07-05');
|
|
expect(row.支出).toBe('100');
|
|
expect(row.账户).toContain('Food');
|
|
expect(row.标签).toBe('#food');
|
|
});
|
|
|
|
it('transactionsToRows 批量', () => {
|
|
const rows = transactionsToRows(sampleTx);
|
|
expect(rows).toHaveLength(3);
|
|
});
|
|
|
|
it('exportToExcel 单工作表', async () => {
|
|
const wb = new MockExcelWorkbook();
|
|
const path = await exportToExcel(sampleTx, wb, '/tmp/test.xlsx');
|
|
expect(path).toBe('/tmp/test.xlsx');
|
|
expect(wb.sheets).toHaveLength(1);
|
|
expect(wb.sheets[0].name).toBe('交易');
|
|
expect(wb.sheets[0].rows).toHaveLength(3);
|
|
});
|
|
|
|
it('exportToExcelMultiSheet 多工作表', async () => {
|
|
const wb = new MockExcelWorkbook();
|
|
await exportToExcelMultiSheet(sampleTx, wb, '/tmp/multi.xlsx');
|
|
expect(wb.sheets.length).toBeGreaterThanOrEqual(2);
|
|
expect(wb.sheets.some(s => s.name === '交易')).toBe(true);
|
|
expect(wb.sheets.some(s => s.name === '账户余额')).toBe(true);
|
|
});
|
|
|
|
it('exportToCsv 降级 CSV', () => {
|
|
const csv = exportToCsv(sampleTx);
|
|
expect(csv).toContain('日期,摘要,收入,支出');
|
|
expect(csv).toContain('2026-07-05');
|
|
expect(csv.split('\n').length).toBe(4); // header + 3 rows
|
|
});
|
|
});
|