// Money + points paths against a throwaway in-memory Postgres (PGlite), migrated from prisma/migrations. import { readdirSync, readFileSync } from 'node:fs'; import { PGlite } from '@electric-sql/pglite'; import { PGLiteSocketServer } from '@electric-sql/pglite-socket'; import { afterAll, beforeAll, describe, expect, it } from 'vitest'; process.env.JWT_SECRET = 'test-secret-test-secret-test-secret-00'; process.env.DATABASE_POOL_MAX = '1'; process.env.RESEND_API_KEY = ''; const db = await PGlite.create(); for (const dir of readdirSync('prisma/migrations').filter((d) => /^\d/.test(d)).sort()) { await db.exec(readFileSync(`prisma/migrations/${dir}/migration.sql`, 'utf8')); } const server = new PGLiteSocketServer({ db, port: 5544, host: '127.0.0.1', maxConnections: 20 }); await server.start(); process.env.DATABASE_URL = 'postgresql://postgres:postgres@127.0.0.1:5544/postgres'; const { PrismaService } = await import('../src/prisma.service.js'); const { CommerceService } = await import('../src/modules/commerce.js'); const { GameService } = await import('../src/modules/game.js'); const prisma = new PrismaService(); const commerce = new CommerceService(prisma); const game = new GameService(prisma); const order = (discountCode?: string, quantity = 1) => commerce.createTicketOrder({ ticketId: 'pass', quantity, name: 'Abebe Kebede', email: 'abebe@example.com', paymentMethod: 'bank', consent: true, discountCode, }); beforeAll(async () => { await prisma.ticketTier.create({ data: { code: 'pass', name: 'Pass', priceMinor: 10000, capacity: 3 } }); }); afterAll(async () => { await prisma.$disconnect(); await server.stop(); await db.close(); }); describe('discounts', () => { it('applies percent and caps fixed amounts at the subtotal', async () => { await prisma.discountCode.create({ data: { code: 'TEN', percentOff: 10 } }); await prisma.discountCode.create({ data: { code: 'BIG', amountOffMinor: 999_999 } }); expect((await commerce.quote('pass', 2, 'ten')).totalMinor).toBe(18000); expect((await commerce.quote('pass', 1, 'BIG')).totalMinor).toBe(0); }); it('respects maxUses, counting only paid orders', async () => { await prisma.discountCode.create({ data: { code: 'ONCE', percentOff: 50, maxUses: 1 } }); const first = await order('ONCE'); expect((await commerce.quote('pass', 1, 'ONCE')).valid).toBe(true); // pending orders don't use it up await commerce.markPaid(first.id); const q = await commerce.quote('pass', 1, 'ONCE'); expect(q.valid).toBe(false); await expect(order('ONCE')).rejects.toMatchObject({ status: 400 }); }); }); describe('orders', () => { it('mark-paid issues one ticket per seat, once', async () => { const o = await order(undefined, 2); const paid = await commerce.markPaid(o.id, 'REF'); expect(paid.tickets).toHaveLength(2); expect(new Set(paid.tickets.map((t) => t.qrToken)).size).toBe(2); await expect(commerce.markPaid(o.id)).rejects.toMatchObject({ status: 409 }); }); it('refuses to oversell tier capacity', async () => { // capacity 3: 1 (ONCE) + 2 already issued await expect(order()).rejects.toMatchObject({ status: 409 }); }); }); describe('scans', () => { it('awards points once, doubles the mystery investee, and draws a raffle winner', async () => { const user = await prisma.user.create({ data: { email: 'a@x.com' } }); const booth = await prisma.checkpoint.create({ data: { kind: 'BOOTH', label: 'B', points: 10, qrToken: 'booth1' } }); await prisma.checkpoint.create({ data: { kind: 'MYSTERY_INVESTEE', label: 'M', points: 50, qrToken: 'myst1' } }); expect((await game.scan(user, `https://grv.ethiopiatrustfund.org/q/${booth.qrToken}`)).total).toBe(10); await expect(game.scan(user, 'booth1')).rejects.toMatchObject({ status: 409 }); const mystery = await game.scan(user, 'myst1'); expect(mystery).toMatchObject({ pointsAwarded: 100, total: 110, mystery: true }); await expect(game.scan(user, 'nope')).rejects.toMatchObject({ status: 404 }); const raffle = await prisma.raffle.create({ data: { name: 'R', prize: 'P', minPoints: 100 } }); expect((await game.draw(raffle.id)).winnerUserId).toBe(user.id); await expect(game.draw(raffle.id)).rejects.toMatchObject({ status: 409 }); }); });