add tests to core
This commit is contained in:
@@ -0,0 +1,207 @@
|
||||
import { beforeEach, describe, expect, it, vi, type Mock } from 'vitest';
|
||||
import {
|
||||
GetSeasonSponsorshipsUseCase,
|
||||
type GetSeasonSponsorshipsInput,
|
||||
type GetSeasonSponsorshipsResult,
|
||||
type GetSeasonSponsorshipsErrorCode,
|
||||
} from './GetSeasonSponsorshipsUseCase';
|
||||
import type { ISeasonSponsorshipRepository } from '../../domain/repositories/ISeasonSponsorshipRepository';
|
||||
import type { ISeasonRepository } from '../../domain/repositories/ISeasonRepository';
|
||||
import type { ILeagueRepository } from '../../domain/repositories/ILeagueRepository';
|
||||
import type { ILeagueMembershipRepository } from '../../domain/repositories/ILeagueMembershipRepository';
|
||||
import type { IRaceRepository } from '../../domain/repositories/IRaceRepository';
|
||||
import { Season } from '../../domain/entities/season/Season';
|
||||
import { League } from '../../domain/entities/League';
|
||||
import { SeasonSponsorship } from '../../domain/entities/season/SeasonSponsorship';
|
||||
import { Money } from '../../domain/value-objects/Money';
|
||||
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
|
||||
describe('GetSeasonSponsorshipsUseCase', () => {
|
||||
let seasonSponsorshipRepository: {
|
||||
findBySeasonId: Mock;
|
||||
};
|
||||
let seasonRepository: {
|
||||
findById: Mock;
|
||||
};
|
||||
let leagueRepository: {
|
||||
findById: Mock;
|
||||
};
|
||||
let leagueMembershipRepository: {
|
||||
getLeagueMembers: Mock;
|
||||
};
|
||||
let raceRepository: {
|
||||
findByLeagueId: Mock;
|
||||
};
|
||||
|
||||
let output: UseCaseOutputPort<GetSeasonSponsorshipsResult> & { present: Mock };
|
||||
|
||||
let useCase: GetSeasonSponsorshipsUseCase;
|
||||
|
||||
beforeEach(() => {
|
||||
seasonSponsorshipRepository = {
|
||||
findBySeasonId: vi.fn(),
|
||||
};
|
||||
seasonRepository = {
|
||||
findById: vi.fn(),
|
||||
};
|
||||
leagueRepository = {
|
||||
findById: vi.fn(),
|
||||
};
|
||||
leagueMembershipRepository = {
|
||||
getLeagueMembers: vi.fn(),
|
||||
};
|
||||
raceRepository = {
|
||||
findByLeagueId: vi.fn(),
|
||||
};
|
||||
|
||||
output = {
|
||||
present: vi.fn(),
|
||||
} as unknown as UseCaseOutputPort<GetSeasonSponsorshipsResult> & { present: Mock };
|
||||
|
||||
useCase = new GetSeasonSponsorshipsUseCase(
|
||||
seasonSponsorshipRepository as unknown as ISeasonSponsorshipRepository,
|
||||
seasonRepository as unknown as ISeasonRepository,
|
||||
leagueRepository as unknown as ILeagueRepository,
|
||||
leagueMembershipRepository as unknown as ILeagueMembershipRepository,
|
||||
raceRepository as unknown as IRaceRepository,
|
||||
output,
|
||||
);
|
||||
});
|
||||
|
||||
it('returns SEASON_NOT_FOUND when season does not exist', async () => {
|
||||
const input: GetSeasonSponsorshipsInput = { seasonId: 'season-1' };
|
||||
|
||||
seasonRepository.findById.mockResolvedValue(null);
|
||||
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const err = result.unwrapErr() as ApplicationErrorCode<
|
||||
GetSeasonSponsorshipsErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
expect(err.code).toBe('SEASON_NOT_FOUND');
|
||||
expect(err.details.message).toBe('Season not found');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('returns LEAGUE_NOT_FOUND when league for season does not exist', async () => {
|
||||
const input: GetSeasonSponsorshipsInput = { seasonId: 'season-1' };
|
||||
|
||||
const season = Season.create({
|
||||
id: 'season-1',
|
||||
leagueId: 'league-1',
|
||||
gameId: 'game-1',
|
||||
name: 'Season 1',
|
||||
status: 'active',
|
||||
});
|
||||
|
||||
seasonRepository.findById.mockResolvedValue(season);
|
||||
leagueRepository.findById.mockResolvedValue(null);
|
||||
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const err = result.unwrapErr() as ApplicationErrorCode<
|
||||
GetSeasonSponsorshipsErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
expect(err.code).toBe('LEAGUE_NOT_FOUND');
|
||||
expect(err.details.message).toBe('League not found for season');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('presents sponsorship details with computed metrics', async () => {
|
||||
const input: GetSeasonSponsorshipsInput = { seasonId: 'season-1' };
|
||||
|
||||
const season = Season.create({
|
||||
id: 'season-1',
|
||||
leagueId: 'league-1',
|
||||
gameId: 'game-1',
|
||||
name: 'Season 1',
|
||||
status: 'active',
|
||||
startDate: new Date('2025-01-01T00:00:00.000Z'),
|
||||
endDate: new Date('2025-02-01T00:00:00.000Z'),
|
||||
});
|
||||
|
||||
const league = League.create({
|
||||
id: 'league-1',
|
||||
name: 'Test League',
|
||||
description: 'Test',
|
||||
ownerId: 'owner-1',
|
||||
});
|
||||
|
||||
const sponsorship = SeasonSponsorship.create({
|
||||
id: 'sponsorship-1',
|
||||
sponsorId: 'sponsor-1',
|
||||
seasonId: 'season-1',
|
||||
tier: 'main',
|
||||
pricing: Money.create(1000, 'USD'),
|
||||
activatedAt: new Date('2025-01-02T00:00:00.000Z'),
|
||||
createdAt: new Date('2025-01-01T00:00:00.000Z'),
|
||||
});
|
||||
|
||||
seasonRepository.findById.mockResolvedValue(season);
|
||||
leagueRepository.findById.mockResolvedValue(league);
|
||||
seasonSponsorshipRepository.findBySeasonId.mockResolvedValue([sponsorship]);
|
||||
|
||||
leagueMembershipRepository.getLeagueMembers.mockResolvedValue([
|
||||
{ driverId: 'driver-1' },
|
||||
{ driverId: 'driver-2' },
|
||||
{ driverId: 'driver-3' },
|
||||
]);
|
||||
|
||||
raceRepository.findByLeagueId.mockResolvedValue([
|
||||
{ id: 'race-1', status: 'completed' },
|
||||
{ id: 'race-2', status: 'completed' },
|
||||
{ id: 'race-3', status: 'scheduled' },
|
||||
]);
|
||||
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
|
||||
expect(output.present).toHaveBeenCalledTimes(1);
|
||||
const presented = (output.present as Mock).mock.calls[0]?.[0] as GetSeasonSponsorshipsResult;
|
||||
|
||||
expect(presented.seasonId).toBe('season-1');
|
||||
expect(presented.sponsorships).toHaveLength(1);
|
||||
|
||||
const detail = presented.sponsorships[0]!;
|
||||
expect(detail.id).toBe('sponsorship-1');
|
||||
expect(detail.leagueId).toBe('league-1');
|
||||
expect(detail.leagueName).toBe('Test League');
|
||||
expect(detail.seasonId).toBe('season-1');
|
||||
expect(detail.seasonName).toBe('Season 1');
|
||||
expect(detail.seasonStartDate).toEqual(new Date('2025-01-01T00:00:00.000Z'));
|
||||
expect(detail.seasonEndDate).toEqual(new Date('2025-02-01T00:00:00.000Z'));
|
||||
expect(detail.activatedAt).toEqual(new Date('2025-01-02T00:00:00.000Z'));
|
||||
|
||||
expect(detail.metrics.drivers).toBe(3);
|
||||
expect(detail.metrics.races).toBe(3);
|
||||
expect(detail.metrics.completedRaces).toBe(2);
|
||||
expect(detail.metrics.impressions).toBe(2 * 3 * 100);
|
||||
|
||||
expect(detail.pricing).toEqual({ amount: 1000, currency: 'USD' });
|
||||
expect(detail.platformFee).toEqual({ amount: 100, currency: 'USD' });
|
||||
expect(detail.netAmount).toEqual({ amount: 900, currency: 'USD' });
|
||||
});
|
||||
|
||||
it('returns REPOSITORY_ERROR when repository throws', async () => {
|
||||
const input: GetSeasonSponsorshipsInput = { seasonId: 'season-1' };
|
||||
|
||||
seasonRepository.findById.mockRejectedValue(new Error('DB error'));
|
||||
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const err = result.unwrapErr() as ApplicationErrorCode<
|
||||
GetSeasonSponsorshipsErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
expect(err.code).toBe('REPOSITORY_ERROR');
|
||||
expect(err.details.message).toBe('DB error');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
88
core/racing/application/use-cases/GetSponsorUseCase.test.ts
Normal file
88
core/racing/application/use-cases/GetSponsorUseCase.test.ts
Normal file
@@ -0,0 +1,88 @@
|
||||
import { beforeEach, describe, expect, it, vi, type Mock } from 'vitest';
|
||||
import {
|
||||
GetSponsorUseCase,
|
||||
type GetSponsorInput,
|
||||
type GetSponsorResult,
|
||||
type GetSponsorErrorCode,
|
||||
} from './GetSponsorUseCase';
|
||||
import type { ISponsorRepository } from '../../domain/repositories/ISponsorRepository';
|
||||
import { Sponsor } from '../../domain/entities/sponsor/Sponsor';
|
||||
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
|
||||
describe('GetSponsorUseCase', () => {
|
||||
let sponsorRepository: {
|
||||
findById: Mock;
|
||||
};
|
||||
|
||||
let output: UseCaseOutputPort<GetSponsorResult> & { present: Mock };
|
||||
|
||||
let useCase: GetSponsorUseCase;
|
||||
|
||||
beforeEach(() => {
|
||||
sponsorRepository = {
|
||||
findById: vi.fn(),
|
||||
};
|
||||
|
||||
output = {
|
||||
present: vi.fn(),
|
||||
} as unknown as UseCaseOutputPort<GetSponsorResult> & { present: Mock };
|
||||
|
||||
useCase = new GetSponsorUseCase(
|
||||
sponsorRepository as unknown as ISponsorRepository,
|
||||
output,
|
||||
);
|
||||
});
|
||||
|
||||
it('presents sponsor when found', async () => {
|
||||
const sponsor = Sponsor.create({
|
||||
id: 'sponsor-1',
|
||||
name: 'Test Sponsor',
|
||||
contactEmail: 'test@example.com',
|
||||
});
|
||||
|
||||
sponsorRepository.findById.mockResolvedValue(sponsor);
|
||||
|
||||
const input: GetSponsorInput = { sponsorId: 'sponsor-1' };
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(output.present).toHaveBeenCalledWith({ sponsor });
|
||||
});
|
||||
|
||||
it('returns SPONSOR_NOT_FOUND when sponsor does not exist', async () => {
|
||||
sponsorRepository.findById.mockResolvedValue(null);
|
||||
|
||||
const input: GetSponsorInput = { sponsorId: 'sponsor-404' };
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
|
||||
const err = result.unwrapErr() as ApplicationErrorCode<
|
||||
GetSponsorErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
|
||||
expect(err.code).toBe('SPONSOR_NOT_FOUND');
|
||||
expect(err.details.message).toBe('Sponsor not found');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('returns REPOSITORY_ERROR when repository throws', async () => {
|
||||
sponsorRepository.findById.mockRejectedValue(new Error('DB error'));
|
||||
|
||||
const input: GetSponsorInput = { sponsorId: 'sponsor-1' };
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
|
||||
const err = result.unwrapErr() as ApplicationErrorCode<
|
||||
GetSponsorErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
|
||||
expect(err.code).toBe('REPOSITORY_ERROR');
|
||||
expect(err.details.message).toBe('DB error');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
174
core/racing/application/use-cases/ReopenRaceUseCase.test.ts
Normal file
174
core/racing/application/use-cases/ReopenRaceUseCase.test.ts
Normal file
@@ -0,0 +1,174 @@
|
||||
import { beforeEach, describe, expect, it, vi, type Mock } from 'vitest';
|
||||
import {
|
||||
ReopenRaceUseCase,
|
||||
type ReopenRaceInput,
|
||||
type ReopenRaceResult,
|
||||
type ReopenRaceErrorCode,
|
||||
} from './ReopenRaceUseCase';
|
||||
import type { IRaceRepository } from '../../domain/repositories/IRaceRepository';
|
||||
import type { Logger } from '@core/shared/application';
|
||||
import { Race } from '../../domain/entities/Race';
|
||||
import { SessionType } from '../../domain/value-objects/SessionType';
|
||||
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
|
||||
describe('ReopenRaceUseCase', () => {
|
||||
let raceRepository: {
|
||||
findById: Mock;
|
||||
update: Mock;
|
||||
};
|
||||
let logger: {
|
||||
debug: Mock;
|
||||
warn: Mock;
|
||||
info: Mock;
|
||||
error: Mock;
|
||||
};
|
||||
let output: UseCaseOutputPort<ReopenRaceResult> & { present: Mock };
|
||||
|
||||
let useCase: ReopenRaceUseCase;
|
||||
|
||||
beforeEach(() => {
|
||||
raceRepository = {
|
||||
findById: vi.fn(),
|
||||
update: vi.fn(),
|
||||
};
|
||||
|
||||
logger = {
|
||||
debug: vi.fn(),
|
||||
warn: vi.fn(),
|
||||
info: vi.fn(),
|
||||
error: vi.fn(),
|
||||
};
|
||||
|
||||
output = {
|
||||
present: vi.fn(),
|
||||
} as unknown as UseCaseOutputPort<ReopenRaceResult> & { present: Mock };
|
||||
|
||||
useCase = new ReopenRaceUseCase(
|
||||
raceRepository as unknown as IRaceRepository,
|
||||
logger as unknown as Logger,
|
||||
output,
|
||||
);
|
||||
});
|
||||
|
||||
it('returns RACE_NOT_FOUND when race does not exist', async () => {
|
||||
const input: ReopenRaceInput = { raceId: 'race-404', reopenedById: 'admin-1' };
|
||||
|
||||
raceRepository.findById.mockResolvedValue(null);
|
||||
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const err = result.unwrapErr() as ApplicationErrorCode<
|
||||
ReopenRaceErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
expect(err.code).toBe('RACE_NOT_FOUND');
|
||||
expect(err.details.message).toContain('race-404');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('reopens a completed race, persists, and presents the result', async () => {
|
||||
const race = Race.create({
|
||||
id: 'race-1',
|
||||
leagueId: 'league-1',
|
||||
scheduledAt: new Date('2025-01-01T00:00:00.000Z'),
|
||||
track: 'Track 1',
|
||||
car: 'Car 1',
|
||||
sessionType: SessionType.main(),
|
||||
status: 'completed',
|
||||
});
|
||||
|
||||
raceRepository.findById.mockResolvedValue(race);
|
||||
raceRepository.update.mockResolvedValue(race.reopen());
|
||||
|
||||
const input: ReopenRaceInput = { raceId: 'race-1', reopenedById: 'admin-1' };
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
|
||||
expect(raceRepository.update).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
id: 'race-1',
|
||||
status: 'scheduled',
|
||||
}),
|
||||
);
|
||||
|
||||
expect(output.present).toHaveBeenCalledWith({
|
||||
race: expect.objectContaining({
|
||||
id: 'race-1',
|
||||
status: 'scheduled',
|
||||
}),
|
||||
});
|
||||
|
||||
expect(logger.info).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('returns INVALID_RACE_STATE when race is already scheduled', async () => {
|
||||
const race = Race.create({
|
||||
id: 'race-1',
|
||||
leagueId: 'league-1',
|
||||
scheduledAt: new Date('2025-01-01T00:00:00.000Z'),
|
||||
track: 'Track 1',
|
||||
car: 'Car 1',
|
||||
sessionType: SessionType.main(),
|
||||
status: 'scheduled',
|
||||
});
|
||||
|
||||
raceRepository.findById.mockResolvedValue(race);
|
||||
|
||||
const input: ReopenRaceInput = { raceId: 'race-1', reopenedById: 'admin-1' };
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const err = result.unwrapErr() as ApplicationErrorCode<
|
||||
ReopenRaceErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
expect(err.code).toBe('INVALID_RACE_STATE');
|
||||
expect(err.details.message).toContain('already scheduled');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('returns INVALID_RACE_STATE when race is running', async () => {
|
||||
const race = Race.create({
|
||||
id: 'race-1',
|
||||
leagueId: 'league-1',
|
||||
scheduledAt: new Date('2025-01-01T00:00:00.000Z'),
|
||||
track: 'Track 1',
|
||||
car: 'Car 1',
|
||||
sessionType: SessionType.main(),
|
||||
status: 'running',
|
||||
});
|
||||
|
||||
raceRepository.findById.mockResolvedValue(race);
|
||||
|
||||
const input: ReopenRaceInput = { raceId: 'race-1', reopenedById: 'admin-1' };
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const err = result.unwrapErr() as ApplicationErrorCode<
|
||||
ReopenRaceErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
expect(err.code).toBe('INVALID_RACE_STATE');
|
||||
expect(err.details.message).toContain('running race');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('returns REPOSITORY_ERROR when repository throws unexpected error', async () => {
|
||||
raceRepository.findById.mockRejectedValue(new Error('DB error'));
|
||||
|
||||
const input: ReopenRaceInput = { raceId: 'race-1', reopenedById: 'admin-1' };
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const err = result.unwrapErr() as ApplicationErrorCode<
|
||||
ReopenRaceErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
expect(err.code).toBe('REPOSITORY_ERROR');
|
||||
expect(err.details.message).toBe('DB error');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,292 @@
|
||||
import { beforeEach, describe, expect, it, vi, type Mock } from 'vitest';
|
||||
import {
|
||||
WithdrawFromLeagueWalletUseCase,
|
||||
type WithdrawFromLeagueWalletErrorCode,
|
||||
type WithdrawFromLeagueWalletInput,
|
||||
type WithdrawFromLeagueWalletResult,
|
||||
} from './WithdrawFromLeagueWalletUseCase';
|
||||
import type { ILeagueRepository } from '../../domain/repositories/ILeagueRepository';
|
||||
import type { ILeagueWalletRepository } from '../../domain/repositories/ILeagueWalletRepository';
|
||||
import type { ITransactionRepository } from '../../domain/repositories/ITransactionRepository';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
import type { Logger, UseCaseOutputPort } from '@core/shared/application';
|
||||
import { League } from '../../domain/entities/League';
|
||||
import { LeagueWallet } from '../../domain/entities/league-wallet/LeagueWallet';
|
||||
import { Money } from '../../domain/value-objects/Money';
|
||||
|
||||
describe('WithdrawFromLeagueWalletUseCase', () => {
|
||||
let leagueRepository: { findById: Mock };
|
||||
let walletRepository: { findByLeagueId: Mock; update: Mock };
|
||||
let transactionRepository: { create: Mock };
|
||||
let logger: Logger & { error: Mock };
|
||||
let output: UseCaseOutputPort<WithdrawFromLeagueWalletResult> & { present: Mock };
|
||||
let useCase: WithdrawFromLeagueWalletUseCase;
|
||||
|
||||
beforeEach(() => {
|
||||
leagueRepository = { findById: vi.fn() };
|
||||
walletRepository = { findByLeagueId: vi.fn(), update: vi.fn() };
|
||||
transactionRepository = { create: vi.fn() };
|
||||
|
||||
logger = { error: vi.fn() } as unknown as Logger & { error: Mock };
|
||||
|
||||
output = { present: vi.fn() } as unknown as UseCaseOutputPort<WithdrawFromLeagueWalletResult> & {
|
||||
present: Mock;
|
||||
};
|
||||
|
||||
useCase = new WithdrawFromLeagueWalletUseCase(
|
||||
leagueRepository as unknown as ILeagueRepository,
|
||||
walletRepository as unknown as ILeagueWalletRepository,
|
||||
transactionRepository as unknown as ITransactionRepository,
|
||||
logger,
|
||||
output,
|
||||
);
|
||||
});
|
||||
|
||||
it('returns LEAGUE_NOT_FOUND when league is missing', async () => {
|
||||
leagueRepository.findById.mockResolvedValue(null);
|
||||
|
||||
const input: WithdrawFromLeagueWalletInput = {
|
||||
leagueId: 'league-1',
|
||||
requestedById: 'owner-1',
|
||||
amount: 100,
|
||||
currency: 'USD',
|
||||
};
|
||||
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const err = result.unwrapErr() as ApplicationErrorCode<
|
||||
WithdrawFromLeagueWalletErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
|
||||
expect(err.code).toBe('LEAGUE_NOT_FOUND');
|
||||
expect(err.details.message).toBe('League with id league-1 not found');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('returns WALLET_NOT_FOUND when wallet is missing', async () => {
|
||||
const league = League.create({
|
||||
id: 'league-1',
|
||||
name: 'Test League',
|
||||
description: 'desc',
|
||||
ownerId: 'owner-1',
|
||||
});
|
||||
|
||||
leagueRepository.findById.mockResolvedValue(league);
|
||||
walletRepository.findByLeagueId.mockResolvedValue(null);
|
||||
|
||||
const input: WithdrawFromLeagueWalletInput = {
|
||||
leagueId: 'league-1',
|
||||
requestedById: 'owner-1',
|
||||
amount: 100,
|
||||
currency: 'USD',
|
||||
};
|
||||
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const err = result.unwrapErr() as ApplicationErrorCode<
|
||||
WithdrawFromLeagueWalletErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
|
||||
expect(err.code).toBe('WALLET_NOT_FOUND');
|
||||
expect(err.details.message).toBe('Wallet for league league-1 not found');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('returns UNAUTHORIZED_WITHDRAWAL when requester is not owner', async () => {
|
||||
const league = League.create({
|
||||
id: 'league-1',
|
||||
name: 'Test League',
|
||||
description: 'desc',
|
||||
ownerId: 'owner-1',
|
||||
});
|
||||
|
||||
const wallet = LeagueWallet.create({
|
||||
id: 'wallet-1',
|
||||
leagueId: 'league-1',
|
||||
balance: Money.create(1000, 'USD'),
|
||||
});
|
||||
|
||||
leagueRepository.findById.mockResolvedValue(league);
|
||||
walletRepository.findByLeagueId.mockResolvedValue(wallet);
|
||||
|
||||
const input: WithdrawFromLeagueWalletInput = {
|
||||
leagueId: 'league-1',
|
||||
requestedById: 'not-owner',
|
||||
amount: 100,
|
||||
currency: 'USD',
|
||||
};
|
||||
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const err = result.unwrapErr() as ApplicationErrorCode<
|
||||
WithdrawFromLeagueWalletErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
|
||||
expect(err.code).toBe('UNAUTHORIZED_WITHDRAWAL');
|
||||
expect(err.details.message).toBe('Only the league owner can withdraw from the league wallet');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('returns INSUFFICIENT_FUNDS when wallet cannot withdraw amount', async () => {
|
||||
const league = League.create({
|
||||
id: 'league-1',
|
||||
name: 'Test League',
|
||||
description: 'desc',
|
||||
ownerId: 'owner-1',
|
||||
});
|
||||
|
||||
const wallet = LeagueWallet.create({
|
||||
id: 'wallet-1',
|
||||
leagueId: 'league-1',
|
||||
balance: Money.create(100, 'USD'),
|
||||
});
|
||||
|
||||
leagueRepository.findById.mockResolvedValue(league);
|
||||
walletRepository.findByLeagueId.mockResolvedValue(wallet);
|
||||
|
||||
const input: WithdrawFromLeagueWalletInput = {
|
||||
leagueId: 'league-1',
|
||||
requestedById: 'owner-1',
|
||||
amount: 200,
|
||||
currency: 'USD',
|
||||
};
|
||||
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const err = result.unwrapErr() as ApplicationErrorCode<
|
||||
WithdrawFromLeagueWalletErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
|
||||
expect(err.code).toBe('INSUFFICIENT_FUNDS');
|
||||
expect(err.details.message).toBe('Insufficient balance for withdrawal');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('creates withdrawal transaction and updates wallet on success', async () => {
|
||||
vi.useFakeTimers();
|
||||
vi.setSystemTime(new Date('2025-01-01T00:00:00.000Z'));
|
||||
|
||||
const league = League.create({
|
||||
id: 'league-1',
|
||||
name: 'Test League',
|
||||
description: 'desc',
|
||||
ownerId: 'owner-1',
|
||||
});
|
||||
|
||||
const startingBalance = Money.create(1000, 'USD');
|
||||
const wallet = LeagueWallet.create({
|
||||
id: 'wallet-1',
|
||||
leagueId: 'league-1',
|
||||
balance: startingBalance,
|
||||
});
|
||||
|
||||
leagueRepository.findById.mockResolvedValue(league);
|
||||
walletRepository.findByLeagueId.mockResolvedValue(wallet);
|
||||
transactionRepository.create.mockResolvedValue(undefined);
|
||||
walletRepository.update.mockResolvedValue(undefined);
|
||||
|
||||
const input: WithdrawFromLeagueWalletInput = {
|
||||
leagueId: 'league-1',
|
||||
requestedById: 'owner-1',
|
||||
amount: 250,
|
||||
currency: 'USD',
|
||||
reason: 'Payout',
|
||||
};
|
||||
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toBeUndefined();
|
||||
|
||||
expect(transactionRepository.create).toHaveBeenCalledTimes(1);
|
||||
const createdTx = (transactionRepository.create as Mock).mock.calls[0]![0] as {
|
||||
id: { toString(): string };
|
||||
type: string;
|
||||
amount: { amount: number; currency: string };
|
||||
description: string | undefined;
|
||||
metadata: Record<string, unknown> | undefined;
|
||||
walletId: { toString(): string };
|
||||
};
|
||||
|
||||
const expectedTransactionId = `txn-${new Date('2025-01-01T00:00:00.000Z').getTime()}`;
|
||||
|
||||
expect(createdTx.id.toString()).toBe(expectedTransactionId);
|
||||
expect(createdTx.type).toBe('withdrawal');
|
||||
expect(createdTx.amount.amount).toBe(250);
|
||||
expect(createdTx.amount.currency).toBe('USD');
|
||||
expect(createdTx.description).toBe('Payout');
|
||||
expect(createdTx.metadata).toEqual({ reason: 'Payout', requestedById: 'owner-1' });
|
||||
expect(createdTx.walletId.toString()).toBe(wallet.id.toString());
|
||||
|
||||
expect(walletRepository.update).toHaveBeenCalledTimes(1);
|
||||
const updatedWallet = (walletRepository.update as Mock).mock.calls[0]![0] as LeagueWallet;
|
||||
|
||||
expect(updatedWallet.balance.amount).toBe(750);
|
||||
expect(updatedWallet.balance.currency).toBe('USD');
|
||||
expect(updatedWallet.getTransactionIds()).toContain(expectedTransactionId);
|
||||
|
||||
expect(output.present).toHaveBeenCalledTimes(1);
|
||||
const presented = (output.present as Mock).mock.calls[0]![0] as WithdrawFromLeagueWalletResult;
|
||||
|
||||
expect(presented.leagueId).toBe('league-1');
|
||||
expect(presented.amount.amount).toBe(250);
|
||||
expect(presented.amount.currency).toBe('USD');
|
||||
expect(presented.transactionId).toBe(expectedTransactionId);
|
||||
expect(presented.walletBalanceAfter.amount).toBe(750);
|
||||
expect(presented.walletBalanceAfter.currency).toBe('USD');
|
||||
|
||||
const createOrder = (transactionRepository.create as Mock).mock.invocationCallOrder[0]!;
|
||||
const updateOrder = (walletRepository.update as Mock).mock.invocationCallOrder[0]!;
|
||||
expect(createOrder).toBeLessThan(updateOrder);
|
||||
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it('returns REPOSITORY_ERROR and logs when repository throws', async () => {
|
||||
const league = League.create({
|
||||
id: 'league-1',
|
||||
name: 'Test League',
|
||||
description: 'desc',
|
||||
ownerId: 'owner-1',
|
||||
});
|
||||
|
||||
const wallet = LeagueWallet.create({
|
||||
id: 'wallet-1',
|
||||
leagueId: 'league-1',
|
||||
balance: Money.create(1000, 'USD'),
|
||||
});
|
||||
|
||||
leagueRepository.findById.mockResolvedValue(league);
|
||||
walletRepository.findByLeagueId.mockResolvedValue(wallet);
|
||||
|
||||
transactionRepository.create.mockRejectedValue(new Error('DB down'));
|
||||
|
||||
const input: WithdrawFromLeagueWalletInput = {
|
||||
leagueId: 'league-1',
|
||||
requestedById: 'owner-1',
|
||||
amount: 100,
|
||||
currency: 'USD',
|
||||
};
|
||||
|
||||
const result = await useCase.execute(input);
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
const err = result.unwrapErr() as ApplicationErrorCode<
|
||||
WithdrawFromLeagueWalletErrorCode,
|
||||
{ message: string }
|
||||
>;
|
||||
|
||||
expect(err.code).toBe('REPOSITORY_ERROR');
|
||||
expect(err.details.message).toBe('DB down');
|
||||
expect(output.present).not.toHaveBeenCalled();
|
||||
expect(logger.error).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user