integration tests
This commit is contained in:
@@ -2,346 +2,130 @@
|
||||
* Integration Test: Team Detail Use Case Orchestration
|
||||
*
|
||||
* Tests the orchestration logic of team detail-related Use Cases:
|
||||
* - GetTeamDetailUseCase: Retrieves detailed team information including roster, performance, achievements, and history
|
||||
* - Validates that Use Cases correctly interact with their Ports (Repositories, Event Publishers)
|
||||
* - GetTeamDetailsUseCase: Retrieves detailed team information including roster and management permissions
|
||||
* - Validates that Use Cases correctly interact with their Ports (Repositories)
|
||||
* - Uses In-Memory adapters for fast, deterministic testing
|
||||
*
|
||||
* Focus: Business logic orchestration, NOT UI rendering
|
||||
*/
|
||||
|
||||
import { describe, it, expect, beforeAll, afterAll, beforeEach } from 'vitest';
|
||||
import { InMemoryTeamRepository } from '../../../adapters/teams/persistence/inmemory/InMemoryTeamRepository';
|
||||
import { InMemoryDriverRepository } from '../../../adapters/drivers/persistence/inmemory/InMemoryDriverRepository';
|
||||
import { InMemoryLeagueRepository } from '../../../adapters/leagues/persistence/inmemory/InMemoryLeagueRepository';
|
||||
import { InMemoryEventPublisher } from '../../../adapters/events/InMemoryEventPublisher';
|
||||
import { GetTeamDetailUseCase } from '../../../core/teams/use-cases/GetTeamDetailUseCase';
|
||||
import { GetTeamDetailQuery } from '../../../core/teams/ports/GetTeamDetailQuery';
|
||||
import { describe, it, expect, beforeAll, beforeEach } from 'vitest';
|
||||
import { InMemoryTeamRepository } from '../../../adapters/racing/persistence/inmemory/InMemoryTeamRepository';
|
||||
import { InMemoryTeamMembershipRepository } from '../../../adapters/racing/persistence/inmemory/InMemoryTeamMembershipRepository';
|
||||
import { GetTeamDetailsUseCase } from '../../../core/racing/application/use-cases/GetTeamDetailsUseCase';
|
||||
import { Team } from '../../../core/racing/domain/entities/Team';
|
||||
import { Logger } from '../../../core/shared/domain/Logger';
|
||||
|
||||
describe('Team Detail Use Case Orchestration', () => {
|
||||
let teamRepository: InMemoryTeamRepository;
|
||||
let driverRepository: InMemoryDriverRepository;
|
||||
let leagueRepository: InMemoryLeagueRepository;
|
||||
let eventPublisher: InMemoryEventPublisher;
|
||||
let getTeamDetailUseCase: GetTeamDetailUseCase;
|
||||
let membershipRepository: InMemoryTeamMembershipRepository;
|
||||
let getTeamDetailsUseCase: GetTeamDetailsUseCase;
|
||||
let mockLogger: Logger;
|
||||
|
||||
beforeAll(() => {
|
||||
// TODO: Initialize In-Memory repositories and event publisher
|
||||
// teamRepository = new InMemoryTeamRepository();
|
||||
// driverRepository = new InMemoryDriverRepository();
|
||||
// leagueRepository = new InMemoryLeagueRepository();
|
||||
// eventPublisher = new InMemoryEventPublisher();
|
||||
// getTeamDetailUseCase = new GetTeamDetailUseCase({
|
||||
// teamRepository,
|
||||
// driverRepository,
|
||||
// leagueRepository,
|
||||
// eventPublisher,
|
||||
// });
|
||||
mockLogger = {
|
||||
info: () => {},
|
||||
debug: () => {},
|
||||
warn: () => {},
|
||||
error: () => {},
|
||||
} as unknown as Logger;
|
||||
|
||||
teamRepository = new InMemoryTeamRepository(mockLogger);
|
||||
membershipRepository = new InMemoryTeamMembershipRepository(mockLogger);
|
||||
getTeamDetailsUseCase = new GetTeamDetailsUseCase(teamRepository, membershipRepository);
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
// TODO: Clear all In-Memory repositories before each test
|
||||
// teamRepository.clear();
|
||||
// driverRepository.clear();
|
||||
// leagueRepository.clear();
|
||||
// eventPublisher.clear();
|
||||
teamRepository.clear();
|
||||
membershipRepository.clear();
|
||||
});
|
||||
|
||||
describe('GetTeamDetailUseCase - Success Path', () => {
|
||||
it('should retrieve complete team detail with all information', async () => {
|
||||
// TODO: Implement test
|
||||
// Scenario: Team with complete information
|
||||
// Given: A team exists with multiple members
|
||||
// And: The team has captain, admins, and drivers
|
||||
// And: The team has performance statistics
|
||||
// And: The team has achievements
|
||||
// And: The team has race history
|
||||
// When: GetTeamDetailUseCase.execute() is called with team ID
|
||||
// Then: The result should contain all team information
|
||||
// And: The result should show team name, description, and logo
|
||||
// And: The result should show team roster with roles
|
||||
// And: The result should show team performance statistics
|
||||
// And: The result should show team achievements
|
||||
// And: The result should show team race history
|
||||
// And: EventPublisher should emit TeamDetailAccessedEvent
|
||||
describe('GetTeamDetailsUseCase - Success Path', () => {
|
||||
it('should retrieve team detail with membership and management permissions for owner', async () => {
|
||||
// Scenario: Team owner views team details
|
||||
// Given: A team exists
|
||||
const teamId = 't1';
|
||||
const ownerId = 'd1';
|
||||
const team = Team.create({ id: teamId, name: 'Team 1', tag: 'T1', description: 'Desc', ownerId, leagues: [] });
|
||||
await teamRepository.create(team);
|
||||
|
||||
// And: The driver is the owner
|
||||
await membershipRepository.saveMembership({
|
||||
teamId,
|
||||
driverId: ownerId,
|
||||
role: 'owner',
|
||||
status: 'active',
|
||||
joinedAt: new Date()
|
||||
});
|
||||
|
||||
// When: GetTeamDetailsUseCase.execute() is called
|
||||
const result = await getTeamDetailsUseCase.execute({ teamId, driverId: ownerId });
|
||||
|
||||
// Then: The result should contain team information and management permissions
|
||||
expect(result.isOk()).toBe(true);
|
||||
const data = result.unwrap();
|
||||
expect(data.team.id.toString()).toBe(teamId);
|
||||
expect(data.membership?.role).toBe('owner');
|
||||
expect(data.canManage).toBe(true);
|
||||
});
|
||||
|
||||
it('should retrieve team detail with minimal roster', async () => {
|
||||
// TODO: Implement test
|
||||
// Scenario: Team with minimal roster
|
||||
// Given: A team exists with only the captain
|
||||
// When: GetTeamDetailUseCase.execute() is called with team ID
|
||||
// Then: The result should contain team information
|
||||
// And: The roster should show only the captain
|
||||
// And: EventPublisher should emit TeamDetailAccessedEvent
|
||||
it('should retrieve team detail for a non-member', async () => {
|
||||
// Scenario: Non-member views team details
|
||||
// Given: A team exists
|
||||
const teamId = 't2';
|
||||
const team = Team.create({ id: teamId, name: 'Team 2', tag: 'T2', description: 'Desc', ownerId: 'owner', leagues: [] });
|
||||
await teamRepository.create(team);
|
||||
|
||||
// When: GetTeamDetailsUseCase.execute() is called with a driver who is not a member
|
||||
const result = await getTeamDetailsUseCase.execute({ teamId, driverId: 'non-member' });
|
||||
|
||||
// Then: The result should contain team information but no membership and no management permissions
|
||||
expect(result.isOk()).toBe(true);
|
||||
const data = result.unwrap();
|
||||
expect(data.team.id.toString()).toBe(teamId);
|
||||
expect(data.membership).toBeNull();
|
||||
expect(data.canManage).toBe(false);
|
||||
});
|
||||
|
||||
it('should retrieve team detail with pending join requests', async () => {
|
||||
// TODO: Implement test
|
||||
// Scenario: Team with pending requests
|
||||
// Given: A team exists with pending join requests
|
||||
// When: GetTeamDetailUseCase.execute() is called with team ID
|
||||
// Then: The result should contain pending requests
|
||||
// And: Each request should display driver name and request date
|
||||
// And: EventPublisher should emit TeamDetailAccessedEvent
|
||||
});
|
||||
it('should retrieve team detail for a regular member', async () => {
|
||||
// Scenario: Regular member views team details
|
||||
// Given: A team exists
|
||||
const teamId = 't3';
|
||||
const memberId = 'd3';
|
||||
const team = Team.create({ id: teamId, name: 'Team 3', tag: 'T3', description: 'Desc', ownerId: 'owner', leagues: [] });
|
||||
await teamRepository.create(team);
|
||||
|
||||
// And: The driver is a regular member
|
||||
await membershipRepository.saveMembership({
|
||||
teamId,
|
||||
driverId: memberId,
|
||||
role: 'driver',
|
||||
status: 'active',
|
||||
joinedAt: new Date()
|
||||
});
|
||||
|
||||
it('should retrieve team detail with team performance statistics', async () => {
|
||||
// TODO: Implement test
|
||||
// Scenario: Team with performance statistics
|
||||
// Given: A team exists with performance data
|
||||
// When: GetTeamDetailUseCase.execute() is called with team ID
|
||||
// Then: The result should show win rate
|
||||
// And: The result should show podium finishes
|
||||
// And: The result should show total races
|
||||
// And: The result should show championship points
|
||||
// And: EventPublisher should emit TeamDetailAccessedEvent
|
||||
});
|
||||
// When: GetTeamDetailsUseCase.execute() is called
|
||||
const result = await getTeamDetailsUseCase.execute({ teamId, driverId: memberId });
|
||||
|
||||
it('should retrieve team detail with team achievements', async () => {
|
||||
// TODO: Implement test
|
||||
// Scenario: Team with achievements
|
||||
// Given: A team exists with achievements
|
||||
// When: GetTeamDetailUseCase.execute() is called with team ID
|
||||
// Then: The result should show achievement badges
|
||||
// And: The result should show achievement names
|
||||
// And: The result should show achievement dates
|
||||
// And: EventPublisher should emit TeamDetailAccessedEvent
|
||||
});
|
||||
|
||||
it('should retrieve team detail with team race history', async () => {
|
||||
// TODO: Implement test
|
||||
// Scenario: Team with race history
|
||||
// Given: A team exists with race history
|
||||
// When: GetTeamDetailUseCase.execute() is called with team ID
|
||||
// Then: The result should show past races
|
||||
// And: The result should show race results
|
||||
// And: The result should show race dates
|
||||
// And: The result should show race tracks
|
||||
// And: EventPublisher should emit TeamDetailAccessedEvent
|
||||
});
|
||||
|
||||
it('should retrieve team detail with league information', async () => {
|
||||
// TODO: Implement test
|
||||
// Scenario: Team with league information
|
||||
// Given: A team exists in a league
|
||||
// When: GetTeamDetailUseCase.execute() is called with team ID
|
||||
// Then: The result should show league name
|
||||
// And: The result should show league tier
|
||||
// And: The result should show league season
|
||||
// And: EventPublisher should emit TeamDetailAccessedEvent
|
||||
});
|
||||
|
||||
it('should retrieve team detail with social links', async () => {
|
||||
// TODO: Implement test
|
||||
// Scenario: Team with social links
|
||||
// Given: A team exists with social links
|
||||
// When: GetTeamDetailUseCase.execute() is called with team ID
|
||||
// Then: The result should show social media links
|
||||
// And: The result should show website link
|
||||
// And: The result should show Discord link
|
||||
// And: EventPublisher should emit TeamDetailAccessedEvent
|
||||
});
|
||||
|
||||
it('should retrieve team detail with roster size limit', async () => {
|
||||
// TODO: Implement test
|
||||
// Scenario: Team with roster size limit
|
||||
// Given: A team exists with roster size limit
|
||||
// When: GetTeamDetailUseCase.execute() is called with team ID
|
||||
// Then: The result should show current roster size
|
||||
// And: The result should show maximum roster size
|
||||
// And: EventPublisher should emit TeamDetailAccessedEvent
|
||||
});
|
||||
|
||||
it('should retrieve team detail with team full indicator', async () => {
|
||||
// TODO: Implement test
|
||||
// Scenario: Team is full
|
||||
// Given: A team exists and is full
|
||||
// When: GetTeamDetailUseCase.execute() is called with team ID
|
||||
// Then: The result should show team is full
|
||||
// And: The result should not show join request option
|
||||
// And: EventPublisher should emit TeamDetailAccessedEvent
|
||||
// Then: The result should contain team information and membership but no management permissions
|
||||
expect(result.isOk()).toBe(true);
|
||||
const data = result.unwrap();
|
||||
expect(data.team.id.toString()).toBe(teamId);
|
||||
expect(data.membership?.role).toBe('driver');
|
||||
expect(data.canManage).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('GetTeamDetailUseCase - Edge Cases', () => {
|
||||
it('should handle team with no career history', async () => {
|
||||
// TODO: Implement test
|
||||
// Scenario: Team with no career history
|
||||
// Given: A team exists
|
||||
// And: The team has no career history
|
||||
// When: GetTeamDetailUseCase.execute() is called with team ID
|
||||
// Then: The result should contain team detail
|
||||
// And: Career history section should be empty
|
||||
// And: EventPublisher should emit TeamDetailAccessedEvent
|
||||
});
|
||||
|
||||
it('should handle team with no recent race results', async () => {
|
||||
// TODO: Implement test
|
||||
// Scenario: Team with no recent race results
|
||||
// Given: A team exists
|
||||
// And: The team has no recent race results
|
||||
// When: GetTeamDetailUseCase.execute() is called with team ID
|
||||
// Then: The result should contain team detail
|
||||
// And: Recent race results section should be empty
|
||||
// And: EventPublisher should emit TeamDetailAccessedEvent
|
||||
});
|
||||
|
||||
it('should handle team with no championship standings', async () => {
|
||||
// TODO: Implement test
|
||||
// Scenario: Team with no championship standings
|
||||
// Given: A team exists
|
||||
// And: The team has no championship standings
|
||||
// When: GetTeamDetailUseCase.execute() is called with team ID
|
||||
// Then: The result should contain team detail
|
||||
// And: Championship standings section should be empty
|
||||
// And: EventPublisher should emit TeamDetailAccessedEvent
|
||||
});
|
||||
|
||||
it('should handle team with no data at all', async () => {
|
||||
// TODO: Implement test
|
||||
// Scenario: Team with absolutely no data
|
||||
// Given: A team exists
|
||||
// And: The team has no statistics
|
||||
// And: The team has no career history
|
||||
// And: The team has no recent race results
|
||||
// And: The team has no championship standings
|
||||
// And: The team has no social links
|
||||
// When: GetTeamDetailUseCase.execute() is called with team ID
|
||||
// Then: The result should contain basic team info
|
||||
// And: All sections should be empty or show default values
|
||||
// And: EventPublisher should emit TeamDetailAccessedEvent
|
||||
});
|
||||
});
|
||||
|
||||
describe('GetTeamDetailUseCase - Error Handling', () => {
|
||||
describe('GetTeamDetailsUseCase - Error Handling', () => {
|
||||
it('should throw error when team does not exist', async () => {
|
||||
// TODO: Implement test
|
||||
// Scenario: Non-existent team
|
||||
// Given: No team exists with the given ID
|
||||
// When: GetTeamDetailUseCase.execute() is called with non-existent team ID
|
||||
// Then: Should throw TeamNotFoundError
|
||||
// And: EventPublisher should NOT emit any events
|
||||
});
|
||||
// When: GetTeamDetailsUseCase.execute() is called with non-existent team ID
|
||||
const result = await getTeamDetailsUseCase.execute({ teamId: 'nonexistent', driverId: 'any' });
|
||||
|
||||
it('should throw error when team ID is invalid', async () => {
|
||||
// TODO: Implement test
|
||||
// Scenario: Invalid team ID
|
||||
// Given: An invalid team ID (e.g., empty string, null, undefined)
|
||||
// When: GetTeamDetailUseCase.execute() is called with invalid team ID
|
||||
// Then: Should throw ValidationError
|
||||
// And: EventPublisher should NOT emit any events
|
||||
});
|
||||
|
||||
it('should handle repository errors gracefully', async () => {
|
||||
// TODO: Implement test
|
||||
// Scenario: Repository throws error
|
||||
// Given: A team exists
|
||||
// And: TeamRepository throws an error during query
|
||||
// When: GetTeamDetailUseCase.execute() is called
|
||||
// Then: Should propagate the error appropriately
|
||||
// And: EventPublisher should NOT emit any events
|
||||
});
|
||||
});
|
||||
|
||||
describe('Team Detail Data Orchestration', () => {
|
||||
it('should correctly calculate team statistics from race results', async () => {
|
||||
// TODO: Implement test
|
||||
// Scenario: Team statistics calculation
|
||||
// Given: A team exists
|
||||
// And: The team has 10 completed races
|
||||
// And: The team has 3 wins
|
||||
// And: The team has 5 podiums
|
||||
// When: GetTeamDetailUseCase.execute() is called
|
||||
// Then: Team statistics should show:
|
||||
// - Starts: 10
|
||||
// - Wins: 3
|
||||
// - Podiums: 5
|
||||
// - Rating: Calculated based on performance
|
||||
// - Rank: Calculated based on rating
|
||||
});
|
||||
|
||||
it('should correctly format career history with league and team information', async () => {
|
||||
// TODO: Implement test
|
||||
// Scenario: Career history formatting
|
||||
// Given: A team exists
|
||||
// And: The team has participated in 2 leagues
|
||||
// And: The team has been on 3 teams across seasons
|
||||
// When: GetTeamDetailUseCase.execute() is called
|
||||
// Then: Career history should show:
|
||||
// - League A: Season 2024, Team X
|
||||
// - League B: Season 2024, Team Y
|
||||
// - League A: Season 2023, Team Z
|
||||
});
|
||||
|
||||
it('should correctly format recent race results with proper details', async () => {
|
||||
// TODO: Implement test
|
||||
// Scenario: Recent race results formatting
|
||||
// Given: A team exists
|
||||
// And: The team has 5 recent race results
|
||||
// When: GetTeamDetailUseCase.execute() is called
|
||||
// Then: Recent race results should show:
|
||||
// - Race name
|
||||
// - Track name
|
||||
// - Finishing position
|
||||
// - Points earned
|
||||
// - Race date (sorted newest first)
|
||||
});
|
||||
|
||||
it('should correctly aggregate championship standings across leagues', async () => {
|
||||
// TODO: Implement test
|
||||
// Scenario: Championship standings aggregation
|
||||
// Given: A team exists
|
||||
// And: The team is in 2 championships
|
||||
// And: In Championship A: Position 5, 150 points, 20 drivers
|
||||
// And: In Championship B: Position 12, 85 points, 15 drivers
|
||||
// When: GetTeamDetailUseCase.execute() is called
|
||||
// Then: Championship standings should show:
|
||||
// - League A: Position 5, 150 points, 20 drivers
|
||||
// - League B: Position 12, 85 points, 15 drivers
|
||||
});
|
||||
|
||||
it('should correctly format social links with proper URLs', async () => {
|
||||
// TODO: Implement test
|
||||
// Scenario: Social links formatting
|
||||
// Given: A team exists
|
||||
// And: The team has social links (Discord, Twitter, iRacing)
|
||||
// When: GetTeamDetailUseCase.execute() is called
|
||||
// Then: Social links should show:
|
||||
// - Discord: https://discord.gg/username
|
||||
// - Twitter: https://twitter.com/username
|
||||
// - iRacing: https://members.iracing.com/membersite/member/profile?username=username
|
||||
});
|
||||
|
||||
it('should correctly format team roster with roles', async () => {
|
||||
// TODO: Implement test
|
||||
// Scenario: Team roster formatting
|
||||
// Given: A team exists
|
||||
// And: The team has captain, admins, and drivers
|
||||
// When: GetTeamDetailUseCase.execute() is called
|
||||
// Then: Team roster should show:
|
||||
// - Captain: Highlighted with badge
|
||||
// - Admins: Listed with admin role
|
||||
// - Drivers: Listed with driver role
|
||||
// - Each member should show name, avatar, and join date
|
||||
});
|
||||
});
|
||||
|
||||
describe('GetTeamDetailUseCase - Event Orchestration', () => {
|
||||
it('should emit TeamDetailAccessedEvent with correct payload', async () => {
|
||||
// TODO: Implement test
|
||||
// Scenario: Event emission
|
||||
// Given: A team exists
|
||||
// When: GetTeamDetailUseCase.execute() is called
|
||||
// Then: EventPublisher should emit TeamDetailAccessedEvent
|
||||
// And: The event should contain team ID and requesting driver ID
|
||||
});
|
||||
|
||||
it('should not emit events on validation failure', async () => {
|
||||
// TODO: Implement test
|
||||
// Scenario: No events on validation failure
|
||||
// Given: No team exists
|
||||
// When: GetTeamDetailUseCase.execute() is called with invalid data
|
||||
// Then: EventPublisher should NOT emit any events
|
||||
// Then: Should return error
|
||||
expect(result.isErr()).toBe(true);
|
||||
const error = result.unwrapErr();
|
||||
expect(error.code).toBe('TEAM_NOT_FOUND');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user