import { ConfigService } from '@nestjs/config'; import assert from 'node:assert/strict'; import { PortfolioRepository } from '../src/modules/investments/portfolio/infrastructure/portfolio.repository'; import { PrismaService } from '../src/prisma/prisma.service'; // Real SQLite transaction: a running attempt of the current revision publishes its snapshot. describe('portfolio publication (SQLite)', () => { const prisma = new PrismaService( new ConfigService({ DATABASE_URL: process.env.DATABASE_URL }), ); const repository = new PortfolioRepository(prisma); const snapshot = { lastSuccessfulAt: '2026-01-01T00:00:00.000Z', isStale: false, totalsByCurrency: [], positions: [], cashBalances: [], structure: {}, } as never; const accountId = 'account'; beforeAll(async () => { await prisma.$connect(); await prisma.user.create({ data: { id: 'owner', email: 'owner@example.invalid', passwordHash: 'synthetic' }, }); }); afterAll(async () => prisma.$disconnect()); beforeEach(async () => { await prisma.tbankConnection.deleteMany(); await prisma.tbankConnection.create({ data: { userId: 'owner', revision: 'current', encryptedToken: 'synthetic', connectedAt: new Date(), accounts: { create: { accountId, type: 'brokerage', name: 'Synthetic', upstreamOrder: 0, }, }, }, }); }); it('publishes the same snapshot for the running attempt of the current revision', async () => { await prisma.tbankSyncWork.create({ data: { id: 'live', userId: 'owner', revision: 'current', scopeKey: `portfolio:${accountId}`, intent: {}, state: 'running', attemptId: 'live-attempt', processRunId: 'run', priority: 1, progress: {}, }, }); const published = await repository.publish( { id: 'live', attemptId: 'live-attempt', userId: 'owner', revision: 'current' }, accountId, snapshot, ); assert.equal(published, true); expect(await prisma.tbankPortfolio.count()).toBe(1); }); });