refactor racing use cases

This commit is contained in:
2025-12-21 00:43:42 +01:00
parent e9d6f90bb2
commit c12656d671
308 changed files with 14401 additions and 7419 deletions

View File

@@ -1,5 +1,11 @@
import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
import { GetLeagueWalletUseCase } from './GetLeagueWalletUseCase';
import { describe, it, expect, beforeEach, vi, type Mock } from 'vitest';
import {
GetLeagueWalletUseCase,
type GetLeagueWalletResult,
type GetLeagueWalletInput,
type GetLeagueWalletErrorCode,
} from './GetLeagueWalletUseCase';
import type { ILeagueRepository } from '../../domain/repositories/ILeagueRepository';
import type { ILeagueWalletRepository } from '../../domain/repositories/ILeagueWalletRepository';
import type { ITransactionRepository } from '../../domain/repositories/ITransactionRepository';
import { LeagueWallet } from '../../domain/entities/league-wallet/LeagueWallet';
@@ -7,17 +13,27 @@ import { Money } from '../../domain/value-objects/Money';
import { Transaction } from '../../domain/entities/league-wallet/Transaction';
import { TransactionId } from '../../domain/entities/league-wallet/TransactionId';
import { LeagueWalletId } from '../../domain/entities/league-wallet/LeagueWalletId';
import type { UseCaseOutputPort } from '@core/shared/application';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
describe('GetLeagueWalletUseCase', () => {
let leagueRepository: {
exists: Mock;
};
let leagueWalletRepository: {
findByLeagueId: Mock;
};
let transactionRepository: {
findByWalletId: Mock;
};
let output: UseCaseOutputPort<GetLeagueWalletResult> & { present: Mock };
let useCase: GetLeagueWalletUseCase;
beforeEach(() => {
leagueRepository = {
exists: vi.fn(),
};
leagueWalletRepository = {
findByLeagueId: vi.fn(),
};
@@ -26,9 +42,15 @@ describe('GetLeagueWalletUseCase', () => {
findByWalletId: vi.fn(),
};
output = {
present: vi.fn(),
} as unknown as UseCaseOutputPort<GetLeagueWalletResult> & { present: Mock };
useCase = new GetLeagueWalletUseCase(
leagueRepository as unknown as ILeagueRepository,
leagueWalletRepository as unknown as ILeagueWalletRepository,
transactionRepository as unknown as ITransactionRepository,
output,
);
});
@@ -42,6 +64,7 @@ describe('GetLeagueWalletUseCase', () => {
balance,
});
leagueRepository.exists.mockResolvedValue(true);
leagueWalletRepository.findByLeagueId.mockResolvedValue(wallet);
const sponsorshipTx = Transaction.create({
@@ -99,65 +122,103 @@ describe('GetLeagueWalletUseCase', () => {
transactionRepository.findByWalletId.mockResolvedValue(transactions);
const result = await useCase.execute({ leagueId });
const input: GetLeagueWalletInput = { leagueId };
const result = await useCase.execute(input);
expect(result.isOk()).toBe(true);
const viewModel = result.unwrap();
expect(result.unwrap()).toBeUndefined();
expect(output.present).toHaveBeenCalledTimes(1);
expect(viewModel.balance).toBe(balance.amount);
expect(viewModel.currency).toBe(balance.currency);
const presented = (output.present as Mock).mock.calls[0]![0] as GetLeagueWalletResult;
expect(presented.wallet).toBe(wallet);
expect(presented.transactions).toHaveLength(transactions.length);
expect(presented.transactions[0]!.id).toEqual(
transactions.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime())[0]!
.id,
);
const { aggregates } = presented;
const expectedTotalRevenue =
sponsorshipTx.amount.amount +
membershipTx.amount.amount +
pendingPrizeTx.amount.amount;
sponsorshipTx.amount.add(membershipTx.amount).add(pendingPrizeTx.amount);
const expectedTotalFees =
sponsorshipTx.platformFee.amount +
membershipTx.platformFee.amount +
pendingPrizeTx.platformFee.amount;
sponsorshipTx.platformFee
.add(membershipTx.platformFee)
.add(pendingPrizeTx.platformFee);
const expectedTotalWithdrawals = withdrawalTx.netAmount.amount;
const expectedPendingPayouts = pendingPrizeTx.netAmount.amount;
const expectedTotalWithdrawals = withdrawalTx.netAmount;
const expectedPendingPayouts = pendingPrizeTx.netAmount;
expect(viewModel.totalRevenue).toBe(expectedTotalRevenue);
expect(viewModel.totalFees).toBe(expectedTotalFees);
expect(viewModel.totalWithdrawals).toBe(expectedTotalWithdrawals);
expect(viewModel.pendingPayouts).toBe(expectedPendingPayouts);
expect(viewModel.transactions).toHaveLength(transactions.length);
expect(viewModel.transactions[0]!.id).toBe(transactions.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime())[0]!.id.toString());
expect(viewModel.transactions.find(t => t.type === 'sponsorship')).toBeTruthy();
expect(viewModel.transactions.find(t => t.type === 'membership')).toBeTruthy();
expect(viewModel.transactions.find(t => t.type === 'withdrawal')).toBeTruthy();
expect(viewModel.transactions.find(t => t.type === 'prize')).toBeTruthy();
expect(aggregates.balance).toBe(balance);
expect(aggregates.totalRevenue.amount).toBe(expectedTotalRevenue.amount);
expect(aggregates.totalFees.amount).toBe(expectedTotalFees.amount);
expect(aggregates.totalWithdrawals.amount).toBe(
expectedTotalWithdrawals.amount,
);
expect(aggregates.pendingPayouts.amount).toBe(expectedPendingPayouts.amount);
});
it('returns error result when wallet is missing', async () => {
const leagueId = 'league-missing';
leagueRepository.exists.mockResolvedValue(true);
leagueWalletRepository.findByLeagueId.mockResolvedValue(null);
const result = await useCase.execute({ leagueId });
const input: GetLeagueWalletInput = { leagueId };
const result = await useCase.execute(input);
expect(result.isErr()).toBe(true);
expect(result.unwrapErr()).toEqual({
code: 'REPOSITORY_ERROR',
message: 'Wallet not found',
});
const err = result.unwrapErr() as ApplicationErrorCode<
GetLeagueWalletErrorCode,
{ message: string }
>;
expect(err.code).toBe('WALLET_NOT_FOUND');
expect(err.details.message).toBe('League wallet not found');
expect(output.present).not.toHaveBeenCalled();
});
it('returns league not found when league does not exist', async () => {
const leagueId = 'league-missing';
leagueRepository.exists.mockResolvedValue(false);
const input: GetLeagueWalletInput = { leagueId };
const result = await useCase.execute(input);
expect(result.isErr()).toBe(true);
const err = result.unwrapErr() as ApplicationErrorCode<
GetLeagueWalletErrorCode,
{ message: string }
>;
expect(err.code).toBe('LEAGUE_NOT_FOUND');
expect(err.details.message).toBe('League not found');
expect(output.present).not.toHaveBeenCalled();
});
it('returns repository error when repository throws', async () => {
const leagueId = 'league-1';
leagueWalletRepository.findByLeagueId.mockRejectedValue(new Error('DB error'));
leagueRepository.exists.mockRejectedValue(new Error('DB error'));
const result = await useCase.execute({ leagueId });
const input: GetLeagueWalletInput = { leagueId };
const result = await useCase.execute(input);
expect(result.isErr()).toBe(true);
expect(result.unwrapErr()).toEqual({
code: 'REPOSITORY_ERROR',
message: 'Failed to fetch league wallet',
});
const err = result.unwrapErr() as ApplicationErrorCode<
GetLeagueWalletErrorCode,
{ message: string }
>;
expect(err.code).toBe('REPOSITORY_ERROR');
expect(err.details.message).toBe('DB error');
expect(output.present).not.toHaveBeenCalled();
});
});