test: replace late portfolio sleep with started and done barriers (WealthLedger/backend#63) (#101)
Backend verify / commits (push) Skipped
Backend verify / security (push) Successful in 12s
Backend verify / verify (push) Failing after 6m21s

This commit was merged in pull request #101.
This commit is contained in:
2026-10-15 12:23:14 +03:00
parent 9e4f9d9cab
commit 7bc7d8ddec
2 changed files with 92 additions and 4 deletions
+19 -4
View File
@@ -3,6 +3,7 @@ import type { Server } from 'node:http';
import { INestApplication } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { Test } from '@nestjs/testing';
import assert from 'node:assert/strict';
import { setTimeout as delay } from 'node:timers/promises';
import request from 'supertest';
@@ -573,16 +574,30 @@ describe('local accounts and portfolio HTTP (SQLite)', () => {
await put().expect(200);
await seedMembership();
const blocked = deferred<TbankPortfolio>();
upstream.getPortfolio.mockReturnValueOnce(blocked.promise);
await get('brokerage-accounts/account/portfolio').expect(200);
const started = deferred<void>();
upstream.getPortfolio.mockImplementationOnce(() => {
started.resolve();
return blocked.promise;
});
// Starts the real portfolio producer, the same path the hourly walk uses.
const late = await portfolioService.request(
'owner',
(await prisma.tbankConnection.findUniqueOrThrow({ where: { userId: 'owner' } })).revision,
'account',
true,
);
assert(late);
// The late upstream call must have begun before the connection is replaced.
await started.promise;
await put().expect(200);
await seedMembership();
blocked.resolve({ positions: [] });
await delay(20);
// Barrier: the fenced producer's completion, not a timer.
await late.done;
expect(await prisma.tbankPortfolio.count()).toBe(0);
const replaced = await get(
'brokerage-accounts/account/portfolio?sync=observe',
).expect(200);
expect((replaced.body as PortfolioResourceDto).data).toBeNull();
expect(await prisma.tbankPortfolio.count()).toBe(0);
});
});