stock-portfolio/__tests__/positions.test.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

113 lines
3.1 KiB
TypeScript

import { Prisma } from '@prisma/client'
const mockPositionFindMany = jest.fn()
const mockExchangeRateFindMany = jest.fn()
const mockSecurityFindMany = jest.fn()
jest.mock('@/lib/prisma', () => ({
prisma: {
position: {
findMany: (...args: any[]) => mockPositionFindMany(...args),
},
exchangeRate: {
findMany: (...args: any[]) => mockExchangeRateFindMany(...args),
},
security: {
findMany: (...args: any[]) => mockSecurityFindMany(...args),
},
},
}))
import { GET } from '../src/app/api/positions/route'
describe('Positions API - GET', () => {
beforeEach(() => {
jest.clearAllMocks()
})
it('返回空数组当无持仓', async () => {
mockPositionFindMany.mockResolvedValue([])
mockExchangeRateFindMany.mockResolvedValue([])
mockSecurityFindMany.mockResolvedValue([])
const req = new Request('http://localhost/api/positions')
const res = await GET(req)
expect(res.status).toBe(200)
const data = await res.json()
expect(data).toEqual([])
})
it('返回持仓列表及市值计算', async () => {
const mockPositions = [
{
id: 'pos-1',
accountId: 'acc-1',
symbol: 'AAPL',
quantity: new Prisma.Decimal('10'),
averageCost: new Prisma.Decimal('150'),
currency: 'USD',
account: { name: '美股账户', marketType: 'US' as const },
},
{
id: 'pos-2',
accountId: 'acc-2',
symbol: 'BTC',
quantity: new Prisma.Decimal('0.5'),
averageCost: new Prisma.Decimal('50000'),
currency: 'USD',
account: { name: '加密账户', marketType: 'CRYPTO' as const },
},
]
const mockSecurities = [
{ symbol: 'AAPL', name: 'Apple Inc', lotSize: 1, priceDecimals: 2, isCrypto: false },
{ symbol: 'BTC', name: 'Bitcoin', lotSize: 1, priceDecimals: 2, isCrypto: true },
]
mockPositionFindMany.mockResolvedValue(mockPositions)
mockExchangeRateFindMany.mockResolvedValue([])
mockSecurityFindMany.mockResolvedValue(mockSecurities)
const req = new Request('http://localhost/api/positions')
const res = await GET(req)
expect(res.status).toBe(200)
const data = await res.json()
expect(data).toHaveLength(2)
expect(data[0]).toMatchObject({
symbol: 'AAPL',
quantity: '10',
averageCost: '150',
currency: 'USD',
accountName: '美股账户',
marketType: 'US',
isCrypto: false,
})
expect(data[1]).toMatchObject({
symbol: 'BTC',
quantity: '0.5',
averageCost: '50000',
currency: 'USD',
accountName: '加密账户',
marketType: 'CRYPTO',
isCrypto: true,
})
})
it('按accountId过滤持仓', async () => {
mockPositionFindMany.mockResolvedValue([])
mockExchangeRateFindMany.mockResolvedValue([])
mockSecurityFindMany.mockResolvedValue([])
const req = new Request('http://localhost/api/positions?accountId=acc-1')
await GET(req)
expect(mockPositionFindMany).toHaveBeenCalledWith(
expect.objectContaining({
where: { accountId: 'acc-1' },
})
)
})
})