stock-portfolio/__tests__/prisma-mock.ts
kennethcheng 0051f92b2b v1.0.5 (2026-04-13)
- 📛 证券名称增强:修复 INTC 等证券名称显示为空的问题 (BUG-101)
- 🔒 SELL超量校验:修复卖出数量超过持仓导致持仓变负的Bug (BUG-002)
- 🔄 撤销BUY成本还原:修复撤销买入时平均成本计算公式错误 (BUG-003)
- 💹 Decimal精度计算:持仓分析改用Prisma.Decimal进行金融计算,防止浮点精度丢失 (BUG-004)
- 📛 证券名称显示:在持仓分析卡片、资产分布、盈亏排行等位置同时显示股票代码和名称
- 📋 证券数据库扩展:新增 Intel Corp. (INTC) 证券记录
- 🔍 回退逻辑增强:确保证券名称为空时显示代码而非空白
- 📈 腾讯行情解析升级:精准解析股票名称(索引1),支持港/A/美股及 ETF 名称自动获取
- 🔀 多市场涨跌解析修复:重构腾讯行情多市场适配层,美股(索引4/5)与港A股(索引31/32)使用差异化索引解析涨跌数据
- 🇨🇳 GBK中文解码修复:改用 `arrayBuffer() + TextDecoder('gbk')` 替代 `text()`,彻底解决A股/港股中文股票名称乱码问题
- 💱 JisuAPI实时汇率:接入 JisuAPI 获取实时汇率,缓存1小时,支持 CNY/HKD/USD 转换
- 📊 资产配置动态图:环形图改由后端实时聚合持仓数据驱动,支持 Tooltip 和百分比显示
- 🎨 资产配置图表优化:精美毛玻璃 Tooltip、颜色图标、去除生硬描边、useMemo 性能优化
- 💱 全局货币联动:资产配置图表数值随 CNY/USD/HKD 切换实时转换
- 📝 交易流水增强:新增证券名称列,显示"名称+代码"双行格式
- 💹 全局汇率展示:在导航栏实时显示 USD/CNY/HKD 汇率信息
- 🔧 BUG-202 修复:修正 `convertCurrency` 汇率换算逻辑(原逻辑除法/乘法颠倒,导致 USD→CNY 换算失效)
- 🔧 BUG-201 修复:腾讯行情 API 获取失败时,`priceAvailable` 标记配合前端显示 "N/A" 替代虚假 0%
- 🔧 BUG-203 增强:持仓分析 `name` 字段确保回退到 `pos.symbol`,名称永不空
- 💹 Decimal 精度保障:所有盈亏/汇率计算均使用 Prisma.Decimal,防止浮点精度丢失
2026-04-13 18:41:37 +08:00

127 lines
2.8 KiB
TypeScript

import { Prisma } from '@prisma/client'
export const mockPrisma = {
user: {
findFirst: jest.fn(),
findUnique: jest.fn(),
create: jest.fn(),
update: jest.fn(),
delete: jest.fn(),
},
account: {
findMany: jest.fn(),
findUnique: jest.fn(),
create: jest.fn(),
update: jest.fn(),
delete: jest.fn(),
},
security: {
findMany: jest.fn(),
findUnique: jest.fn(),
create: jest.fn(),
update: jest.fn(),
delete: jest.fn(),
},
transaction: {
findMany: jest.fn(),
findUnique: jest.fn(),
create: jest.fn(),
update: jest.fn(),
delete: jest.fn(),
count: jest.fn(),
},
position: {
findMany: jest.fn(),
findUnique: jest.fn(),
create: jest.fn(),
update: jest.fn(),
delete: jest.fn(),
},
exchangeRate: {
findMany: jest.fn(),
findUnique: jest.fn(),
create: jest.fn(),
update: jest.fn(),
delete: jest.fn(),
},
$transaction: jest.fn((callback) => callback(mockPrisma)),
$connect: jest.fn(),
$disconnect: jest.fn(),
}
export const resetMockPrisma = () => {
Object.values(mockPrisma).forEach((module) => {
if (typeof module === 'object') {
Object.values(module).forEach((fn) => {
if (typeof fn === 'function') {
;(fn as jest.Mock).mockReset()
}
})
}
})
}
export const createMockPosition = (overrides: Partial<{
id: string
accountId: string
symbol: string
quantity: Prisma.Decimal
averageCost: Prisma.Decimal
currency: string
}> = {}) => ({
id: 'pos-1',
accountId: 'acc-1',
symbol: 'AAPL',
quantity: new Prisma.Decimal('10'),
averageCost: new Prisma.Decimal('150'),
currency: 'USD',
updatedAt: new Date(),
...overrides,
})
export const createMockAccount = (overrides: Partial<{
id: string
userId: string
name: string
marketType: 'US' | 'CN' | 'HK' | 'CRYPTO'
baseCurrency: string
balance: Prisma.Decimal
}> = {}) => ({
id: 'acc-1',
userId: 'user-1',
name: 'Test Account',
marketType: 'US' as const,
baseCurrency: 'USD',
balance: new Prisma.Decimal('10000'),
createdAt: new Date(),
updatedAt: new Date(),
...overrides,
})
export const createMockTransaction = (overrides: Partial<{
id: string
accountId: string
type: 'BUY' | 'SELL' | 'DEPOSIT' | 'WITHDRAW' | 'DIVIDEND'
symbol: string | null
quantity: Prisma.Decimal | null
price: Prisma.Decimal | null
amount: Prisma.Decimal
fee: Prisma.Decimal
currency: string
}> = {}) => ({
id: 'txn-1',
accountId: 'acc-1',
type: 'BUY' as const,
symbol: 'AAPL',
quantity: new Prisma.Decimal('10'),
price: new Prisma.Decimal('150'),
amount: new Prisma.Decimal('1500'),
fee: new Prisma.Decimal('1'),
networkFee: null,
currency: 'USD',
exchangeRate: null,
notes: null,
executedAt: new Date(),
createdAt: new Date(),
...overrides,
})