|
| 1 | +import supertest from 'supertest'; |
| 2 | +import app from '../../../app'; |
| 3 | +import { prisma } from '../../../utils/prisma.utils'; |
| 4 | + |
| 5 | +describe('GET /api/v1/wallets/:address/holdings - multiple price snapshot updates', () => { |
| 6 | + const WALLET_ADDRESS = 'GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'; |
| 7 | + const USER_ID = 'wallet-multi-snap-user'; |
| 8 | + const CREATOR_ID = 'wallet-multi-snap-creator'; |
| 9 | + const HOLDING_BALANCE = 5.0; // 5 keys held |
| 10 | + |
| 11 | + beforeAll(async () => { |
| 12 | + // Clean up database tables to avoid tests leaking into each other |
| 13 | + await prisma.keyOwnership.deleteMany({}); |
| 14 | + await prisma.creatorPriceSnapshot.deleteMany({}); |
| 15 | + await prisma.creatorProfile.deleteMany({}); |
| 16 | + await prisma.user.deleteMany({}); |
| 17 | + |
| 18 | + // Seed a User |
| 19 | + await prisma.user.create({ |
| 20 | + data: { |
| 21 | + id: USER_ID, |
| 22 | + email: 'wallet-multi-snap@example.test', |
| 23 | + passwordHash: 'dummy-hash', |
| 24 | + firstName: 'Wallet', |
| 25 | + lastName: 'Multi Snap', |
| 26 | + }, |
| 27 | + }); |
| 28 | + |
| 29 | + // Seed a Creator |
| 30 | + await prisma.creatorProfile.create({ |
| 31 | + data: { |
| 32 | + id: CREATOR_ID, |
| 33 | + userId: USER_ID, |
| 34 | + handle: 'wallet_multi_snap_creator', |
| 35 | + displayName: 'Wallet Multi Snap Creator', |
| 36 | + }, |
| 37 | + }); |
| 38 | + |
| 39 | + // Seed KeyOwnership |
| 40 | + await prisma.keyOwnership.create({ |
| 41 | + data: { |
| 42 | + ownerAddress: WALLET_ADDRESS, |
| 43 | + creatorId: CREATOR_ID, |
| 44 | + balance: HOLDING_BALANCE, |
| 45 | + }, |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + afterAll(async () => { |
| 50 | + // Clean up seeded database tables |
| 51 | + await prisma.keyOwnership.deleteMany({}); |
| 52 | + await prisma.creatorPriceSnapshot.deleteMany({}); |
| 53 | + await prisma.creatorProfile.deleteMany({}); |
| 54 | + await prisma.user.deleteMany({}); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should reflect the correct total_value after multiple price snapshot updates', async () => { |
| 58 | + // First snapshot update |
| 59 | + await prisma.creatorPriceSnapshot.upsert({ |
| 60 | + where: { creatorId: CREATOR_ID }, |
| 61 | + update: { currentPrice: 100n }, |
| 62 | + create: { creatorId: CREATOR_ID, currentPrice: 100n }, |
| 63 | + }); |
| 64 | + |
| 65 | + let res = await supertest(app).get(`/api/v1/wallets/${WALLET_ADDRESS}/holdings`); |
| 66 | + expect(res.status).toBe(200); |
| 67 | + expect(res.body.success).toBe(true); |
| 68 | + let items = res.body.data.items; |
| 69 | + expect(items).toHaveLength(1); |
| 70 | + expect(items[0].current_price).toBe('100'); |
| 71 | + expect(items[0].total_value).toBe('500'); |
| 72 | + |
| 73 | + // Second snapshot update |
| 74 | + await prisma.creatorPriceSnapshot.upsert({ |
| 75 | + where: { creatorId: CREATOR_ID }, |
| 76 | + update: { currentPrice: 250n }, |
| 77 | + create: { creatorId: CREATOR_ID, currentPrice: 250n }, |
| 78 | + }); |
| 79 | + |
| 80 | + res = await supertest(app).get(`/api/v1/wallets/${WALLET_ADDRESS}/holdings`); |
| 81 | + expect(res.status).toBe(200); |
| 82 | + expect(res.body.success).toBe(true); |
| 83 | + items = res.body.data.items; |
| 84 | + expect(items).toHaveLength(1); |
| 85 | + expect(items[0].current_price).toBe('250'); |
| 86 | + expect(items[0].total_value).toBe('1250'); |
| 87 | + |
| 88 | + // Third snapshot update |
| 89 | + await prisma.creatorPriceSnapshot.upsert({ |
| 90 | + where: { creatorId: CREATOR_ID }, |
| 91 | + update: { currentPrice: 300n }, |
| 92 | + create: { creatorId: CREATOR_ID, currentPrice: 300n }, |
| 93 | + }); |
| 94 | + |
| 95 | + res = await supertest(app).get(`/api/v1/wallets/${WALLET_ADDRESS}/holdings`); |
| 96 | + expect(res.status).toBe(200); |
| 97 | + expect(res.body.success).toBe(true); |
| 98 | + items = res.body.data.items; |
| 99 | + expect(items).toHaveLength(1); |
| 100 | + expect(items[0].current_price).toBe('300'); |
| 101 | + expect(items[0].total_value).toBe('1500'); |
| 102 | + }); |
| 103 | +}); |
0 commit comments