品牌与配置 - 重命名 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 新增不同格式金额配对、指纹唯一性测试
127 lines
5.2 KiB
TypeScript
127 lines
5.2 KiB
TypeScript
import { describe, expect, it, beforeEach } from 'vitest';
|
||
import { useLedgerStore } from '../src/store/ledgerStore';
|
||
import { MemoryBackend } from '../src/domain/mobileStore';
|
||
import type { LedgerFile } from '../src/domain/types';
|
||
|
||
const sampleLedger: LedgerFile = {
|
||
path: 'main.bean',
|
||
content: `option "operating_currency" "CNY"
|
||
2026-01-01 open Assets:Alipay CNY
|
||
2026-01-01 open Expenses:Food CNY
|
||
2026-01-01 open Expenses:Uncategorized CNY
|
||
2026-01-01 open Income:Uncategorized CNY
|
||
include "mobile.bean"
|
||
`,
|
||
};
|
||
|
||
const usdLedger: LedgerFile = {
|
||
path: 'main.bean',
|
||
content: `option "operating_currency" "USD"
|
||
2026-01-01 open Assets:Bank USD
|
||
2026-01-01 open Expenses:Food USD
|
||
include "mobile.bean"
|
||
`,
|
||
};
|
||
|
||
beforeEach(() => {
|
||
useLedgerStore.getState().reset();
|
||
});
|
||
|
||
describe('ledgerStore 写操作', () => {
|
||
it('autoOpenAccounts 跳过已 open 的账户(去重)', async () => {
|
||
await useLedgerStore.getState().loadLedger([sampleLedger], new MemoryBackend());
|
||
const before = useLedgerStore.getState().mobileBean;
|
||
|
||
// Assets:Alipay 已存在 → 应跳过
|
||
await useLedgerStore.getState().autoOpenAccounts(['Assets:Alipay', 'Assets:NewAccount']);
|
||
const after = useLedgerStore.getState().mobileBean;
|
||
|
||
// 只新增 Assets:NewAccount
|
||
expect(after).toContain('Assets:NewAccount');
|
||
// 不应添加重复的 open Assets:Alipay(只保留原有的)
|
||
const openAlipayCount = (after.match(/open Assets:Alipay/g) ?? []).length;
|
||
expect(openAlipayCount).toBe(1); // 只有原始的一条,没有重复添加
|
||
});
|
||
|
||
it('autoOpenAccounts 对已全部 open 的列表是 no-op', async () => {
|
||
await useLedgerStore.getState().loadLedger([sampleLedger], new MemoryBackend());
|
||
const before = useLedgerStore.getState().mobileBean;
|
||
|
||
await useLedgerStore.getState().autoOpenAccounts(['Assets:Alipay']);
|
||
const after = useLedgerStore.getState().mobileBean;
|
||
|
||
expect(after).toBe(before); // 无变化
|
||
});
|
||
|
||
it('autoOpenAccounts 使用 ledger 的 operating_currency', async () => {
|
||
await useLedgerStore.getState().loadLedger([usdLedger], new MemoryBackend());
|
||
await useLedgerStore.getState().autoOpenAccounts(['Assets:NewUSD']);
|
||
const after = useLedgerStore.getState().mobileBean;
|
||
|
||
// 应写 USD 而非 CNY
|
||
expect(after).toContain('open Assets:NewUSD USD');
|
||
expect(after).not.toContain('open Assets:NewUSD CNY');
|
||
});
|
||
|
||
it('adjustAccountBalance 使用 ledger 的 operating_currency', async () => {
|
||
await useLedgerStore.getState().loadLedger([usdLedger], new MemoryBackend());
|
||
await useLedgerStore.getState().adjustAccountBalance('Assets:Bank', '5000', '2026-07-15');
|
||
const after = useLedgerStore.getState().mobileBean;
|
||
|
||
// balance 断言应使用 USD
|
||
expect(after).toContain('balance Assets:Bank 5000 USD');
|
||
});
|
||
|
||
it('appendTransactionsBatch 在锁内拼接多笔交易', async () => {
|
||
await useLedgerStore.getState().loadLedger([sampleLedger], new MemoryBackend());
|
||
const before = useLedgerStore.getState().mobileBean;
|
||
|
||
await useLedgerStore.getState().appendTransactionsBatch([
|
||
{ date: '2026-07-10', narration: '午餐', postings: [{ account: 'Assets:Alipay', amount: '-20', currency: 'CNY' }, { account: 'Expenses:Food', amount: '20', currency: 'CNY' }] },
|
||
{ date: '2026-07-11', narration: '晚餐', postings: [{ account: 'Assets:Alipay', amount: '-30', currency: 'CNY' }, { account: 'Expenses:Food', amount: '30', currency: 'CNY' }] },
|
||
]);
|
||
|
||
const after = useLedgerStore.getState().mobileBean;
|
||
expect(after).toContain('午餐');
|
||
expect(after).toContain('晚餐');
|
||
expect(after.length).toBeGreaterThan(before.length);
|
||
});
|
||
|
||
it('并发 addTransaction + autoOpenAccounts 不丢失数据', async () => {
|
||
await useLedgerStore.getState().loadLedger([sampleLedger], new MemoryBackend());
|
||
|
||
// 并发执行:添加交易 + 自动开户
|
||
await Promise.all([
|
||
useLedgerStore.getState().addTransaction({
|
||
date: '2026-07-10', narration: '交易A',
|
||
postings: [{ account: 'Assets:Alipay', amount: '-10', currency: 'CNY' }, { account: 'Expenses:Food', amount: '10', currency: 'CNY' }],
|
||
}),
|
||
useLedgerStore.getState().autoOpenAccounts(['Assets:NewAcct']),
|
||
]);
|
||
|
||
const after = useLedgerStore.getState().mobileBean;
|
||
// 两者都应存在(互斥锁保证了不丢失)
|
||
expect(after).toContain('交易A');
|
||
expect(after).toContain('Assets:NewAcct');
|
||
});
|
||
|
||
it('并发两笔 addTransaction 都保留', async () => {
|
||
await useLedgerStore.getState().loadLedger([sampleLedger], new MemoryBackend());
|
||
|
||
await Promise.all([
|
||
useLedgerStore.getState().addTransaction({
|
||
date: '2026-07-10', narration: '交易1',
|
||
postings: [{ account: 'Assets:Alipay', amount: '-5', currency: 'CNY' }, { account: 'Expenses:Food', amount: '5', currency: 'CNY' }],
|
||
}),
|
||
useLedgerStore.getState().addTransaction({
|
||
date: '2026-07-10', narration: '交易2',
|
||
postings: [{ account: 'Assets:Alipay', amount: '-8', currency: 'CNY' }, { account: 'Expenses:Food', amount: '8', currency: 'CNY' }],
|
||
}),
|
||
]);
|
||
|
||
const txns = useLedgerStore.getState().ledger!.transactions;
|
||
expect(txns).toHaveLength(2);
|
||
expect(txns.map(t => t.narration).sort()).toEqual(['交易1', '交易2']);
|
||
});
|
||
});
|