import { describe, it, expect } from 'vitest'; import { RacingDomainInvariantError, RacingDomainValidationError, } from '@gridpilot/racing/domain/errors/RacingDomainError'; import { Season, type SeasonStatus, } from '@gridpilot/racing/domain/entities/Season'; import { SeasonScoringConfig } from '@gridpilot/racing/domain/value-objects/SeasonScoringConfig'; import { SeasonDropPolicy, type SeasonDropStrategy, } from '@gridpilot/racing/domain/value-objects/SeasonDropPolicy'; import { SeasonStewardingConfig } from '@gridpilot/racing/domain/value-objects/SeasonStewardingConfig'; function createMinimalSeason(overrides?: Partial & { status?: SeasonStatus }) { return Season.create({ id: 'season-1', leagueId: 'league-1', gameId: 'iracing', name: 'Test Season', status: overrides?.status ?? 'planned', }); } describe('Season aggregate lifecycle', () => { it('transitions Planned → Active → Completed → Archived with timestamps', () => { const planned = createMinimalSeason({ status: 'planned' }); const activated = planned.activate(); expect(activated.status).toBe('active'); expect(activated.startDate).toBeInstanceOf(Date); expect(activated.endDate).toBeUndefined(); const completed = activated.complete(); expect(completed.status).toBe('completed'); expect(completed.startDate).toEqual(activated.startDate); expect(completed.endDate).toBeInstanceOf(Date); const archived = completed.archive(); expect(archived.status).toBe('archived'); expect(archived.startDate).toEqual(completed.startDate); expect(archived.endDate).toEqual(completed.endDate); }); it('throws when activating a non-planned season', () => { const active = createMinimalSeason({ status: 'active' }); const completed = createMinimalSeason({ status: 'completed' }); const archived = createMinimalSeason({ status: 'archived' }); const cancelled = createMinimalSeason({ status: 'cancelled' }); expect(() => active.activate()).toThrow(RacingDomainInvariantError); expect(() => completed.activate()).toThrow(RacingDomainInvariantError); expect(() => archived.activate()).toThrow(RacingDomainInvariantError); expect(() => cancelled.activate()).toThrow(RacingDomainInvariantError); }); it('throws when completing a non-active season', () => { const planned = createMinimalSeason({ status: 'planned' }); const completed = createMinimalSeason({ status: 'completed' }); const archived = createMinimalSeason({ status: 'archived' }); const cancelled = createMinimalSeason({ status: 'cancelled' }); expect(() => planned.complete()).toThrow(RacingDomainInvariantError); expect(() => completed.complete()).toThrow(RacingDomainInvariantError); expect(() => archived.complete()).toThrow(RacingDomainInvariantError); expect(() => cancelled.complete()).toThrow(RacingDomainInvariantError); }); it('throws when archiving a non-completed season', () => { const planned = createMinimalSeason({ status: 'planned' }); const active = createMinimalSeason({ status: 'active' }); const archived = createMinimalSeason({ status: 'archived' }); const cancelled = createMinimalSeason({ status: 'cancelled' }); expect(() => planned.archive()).toThrow(RacingDomainInvariantError); expect(() => active.archive()).toThrow(RacingDomainInvariantError); expect(() => archived.archive()).toThrow(RacingDomainInvariantError); expect(() => cancelled.archive()).toThrow(RacingDomainInvariantError); }); it('allows cancelling planned or active seasons and rejects completed/archived', () => { const planned = createMinimalSeason({ status: 'planned' }); const active = createMinimalSeason({ status: 'active' }); const completed = createMinimalSeason({ status: 'completed' }); const archived = createMinimalSeason({ status: 'archived' }); const cancelledFromPlanned = planned.cancel(); expect(cancelledFromPlanned.status).toBe('cancelled'); expect(cancelledFromPlanned.startDate).toBe(planned.startDate); expect(cancelledFromPlanned.endDate).toBeInstanceOf(Date); const cancelledFromActive = active.cancel(); expect(cancelledFromActive.status).toBe('cancelled'); expect(cancelledFromActive.startDate).toBe(active.startDate); expect(cancelledFromActive.endDate).toBeInstanceOf(Date); expect(() => completed.cancel()).toThrow(RacingDomainInvariantError); expect(() => archived.cancel()).toThrow(RacingDomainInvariantError); }); it('cancel is idempotent for already cancelled seasons', () => { const planned = createMinimalSeason({ status: 'planned' }); const cancelled = planned.cancel(); const cancelledAgain = cancelled.cancel(); expect(cancelledAgain).toBe(cancelled); }); it('canWithdrawFromWallet only when completed', () => { const planned = createMinimalSeason({ status: 'planned' }); const active = createMinimalSeason({ status: 'active' }); const completed = createMinimalSeason({ status: 'completed' }); const archived = createMinimalSeason({ status: 'archived' }); const cancelled = createMinimalSeason({ status: 'cancelled' }); expect(planned.canWithdrawFromWallet()).toBe(false); expect(active.canWithdrawFromWallet()).toBe(false); expect(completed.canWithdrawFromWallet()).toBe(true); expect(archived.canWithdrawFromWallet()).toBe(false); expect(cancelled.canWithdrawFromWallet()).toBe(false); }); }); describe('Season configuration updates', () => { function createBaseSeason() { return Season.create({ id: 'season-1', leagueId: 'league-1', gameId: 'iracing', name: 'Config Season', status: 'planned', startDate: new Date('2025-01-01T00:00:00Z'), endDate: undefined, maxDrivers: 24, }); } it('withScoringConfig returns a new Season with updated scoringConfig only', () => { const season = createBaseSeason(); const scoringConfig = new SeasonScoringConfig({ scoringPresetId: 'sprint-main-driver', customScoringEnabled: true, }); const updated = season.withScoringConfig(scoringConfig); expect(updated).not.toBe(season); expect(updated.scoringConfig).toBe(scoringConfig); expect(updated.schedule).toBe(season.schedule); expect(updated.dropPolicy).toBe(season.dropPolicy); expect(updated.stewardingConfig).toBe(season.stewardingConfig); expect(updated.maxDrivers).toBe(season.maxDrivers); }); it('withDropPolicy returns a new Season with updated dropPolicy only', () => { const season = createBaseSeason(); const dropPolicy = new SeasonDropPolicy({ strategy: 'bestNResults', n: 3, }); const updated = season.withDropPolicy(dropPolicy); expect(updated).not.toBe(season); expect(updated.dropPolicy).toBe(dropPolicy); expect(updated.scoringConfig).toBe(season.scoringConfig); expect(updated.schedule).toBe(season.schedule); expect(updated.stewardingConfig).toBe(season.stewardingConfig); expect(updated.maxDrivers).toBe(season.maxDrivers); }); it('withStewardingConfig returns a new Season with updated stewardingConfig only', () => { const season = createBaseSeason(); const stewardingConfig = new SeasonStewardingConfig({ decisionMode: 'steward_vote', requiredVotes: 3, requireDefense: true, defenseTimeLimit: 24, voteTimeLimit: 24, protestDeadlineHours: 48, stewardingClosesHours: 72, notifyAccusedOnProtest: true, notifyOnVoteRequired: true, }); const updated = season.withStewardingConfig(stewardingConfig); expect(updated).not.toBe(season); expect(updated.stewardingConfig).toBe(stewardingConfig); expect(updated.scoringConfig).toBe(season.scoringConfig); expect(updated.schedule).toBe(season.schedule); expect(updated.dropPolicy).toBe(season.dropPolicy); expect(updated.maxDrivers).toBe(season.maxDrivers); }); it('withMaxDrivers updates maxDrivers when positive', () => { const season = createBaseSeason(); const updated = season.withMaxDrivers(30); expect(updated.maxDrivers).toBe(30); expect(updated.id).toBe(season.id); expect(updated.leagueId).toBe(season.leagueId); expect(updated.gameId).toBe(season.gameId); }); it('withMaxDrivers allows undefined to clear value', () => { const season = createBaseSeason(); const updated = season.withMaxDrivers(undefined); expect(updated.maxDrivers).toBeUndefined(); }); it('withMaxDrivers rejects non-positive values', () => { const season = createBaseSeason(); expect(() => season.withMaxDrivers(0)).toThrow( RacingDomainValidationError, ); expect(() => season.withMaxDrivers(-5)).toThrow( RacingDomainValidationError, ); }); }); describe('SeasonScoringConfig', () => { it('constructs from preset id and customScoringEnabled', () => { const config = new SeasonScoringConfig({ scoringPresetId: 'club-default', customScoringEnabled: true, }); expect(config.scoringPresetId).toBe('club-default'); expect(config.customScoringEnabled).toBe(true); expect(config.props.scoringPresetId).toBe('club-default'); expect(config.props.customScoringEnabled).toBe(true); }); it('normalizes customScoringEnabled to false when omitted', () => { const config = new SeasonScoringConfig({ scoringPresetId: 'sprint-main-driver', }); expect(config.customScoringEnabled).toBe(false); expect(config.props.customScoringEnabled).toBeUndefined(); }); it('throws when scoringPresetId is empty', () => { expect( () => new SeasonScoringConfig({ // @ts-expect-error intentional invalid input scoringPresetId: ' ', }), ).toThrow(RacingDomainValidationError); }); it('equals compares by preset id and customScoringEnabled', () => { const a = new SeasonScoringConfig({ scoringPresetId: 'club-default', customScoringEnabled: false, }); const b = new SeasonScoringConfig({ scoringPresetId: 'club-default', customScoringEnabled: false, }); const c = new SeasonScoringConfig({ scoringPresetId: 'club-default', customScoringEnabled: true, }); expect(a.equals(b)).toBe(true); expect(a.equals(c)).toBe(false); }); }); describe('SeasonDropPolicy', () => { it('allows strategy "none" with undefined n', () => { const policy = new SeasonDropPolicy({ strategy: 'none' }); expect(policy.strategy).toBe('none'); expect(policy.n).toBeUndefined(); }); it('throws when strategy "none" has n defined', () => { expect( () => new SeasonDropPolicy({ strategy: 'none', n: 1, }), ).toThrow(RacingDomainValidationError); }); it('requires positive integer n for "bestNResults" and "dropWorstN"', () => { const strategies: SeasonDropStrategy[] = ['bestNResults', 'dropWorstN']; for (const strategy of strategies) { expect( () => new SeasonDropPolicy({ strategy, n: 0, }), ).toThrow(RacingDomainValidationError); expect( () => new SeasonDropPolicy({ strategy, n: -1, }), ).toThrow(RacingDomainValidationError); } const okBest = new SeasonDropPolicy({ strategy: 'bestNResults', n: 3, }); const okDrop = new SeasonDropPolicy({ strategy: 'dropWorstN', n: 2, }); expect(okBest.n).toBe(3); expect(okDrop.n).toBe(2); }); it('equals compares strategy and n', () => { const a = new SeasonDropPolicy({ strategy: 'none' }); const b = new SeasonDropPolicy({ strategy: 'none' }); const c = new SeasonDropPolicy({ strategy: 'bestNResults', n: 3 }); expect(a.equals(b)).toBe(true); expect(a.equals(c)).toBe(false); }); }); describe('SeasonStewardingConfig', () => { it('creates a valid config with voting mode and requiredVotes', () => { const config = new SeasonStewardingConfig({ decisionMode: 'steward_vote', requiredVotes: 3, requireDefense: true, defenseTimeLimit: 24, voteTimeLimit: 24, protestDeadlineHours: 48, stewardingClosesHours: 72, notifyAccusedOnProtest: true, notifyOnVoteRequired: true, }); expect(config.decisionMode).toBe('steward_vote'); expect(config.requiredVotes).toBe(3); expect(config.requireDefense).toBe(true); expect(config.defenseTimeLimit).toBe(24); expect(config.voteTimeLimit).toBe(24); expect(config.protestDeadlineHours).toBe(48); expect(config.stewardingClosesHours).toBe(72); expect(config.notifyAccusedOnProtest).toBe(true); expect(config.notifyOnVoteRequired).toBe(true); }); it('throws when decisionMode is missing', () => { expect( () => new SeasonStewardingConfig({ // @ts-expect-error intentional invalid decisionMode: undefined, requireDefense: true, defenseTimeLimit: 24, voteTimeLimit: 24, protestDeadlineHours: 48, stewardingClosesHours: 72, notifyAccusedOnProtest: true, notifyOnVoteRequired: true, }), ).toThrow(RacingDomainValidationError); }); it('requires requiredVotes for voting/veto modes', () => { const votingModes = [ 'steward_vote', 'member_vote', 'steward_veto', 'member_veto', ] as const; for (const mode of votingModes) { expect( () => new SeasonStewardingConfig({ decisionMode: mode, // @ts-expect-error intentional invalid requiredVotes: undefined, requireDefense: true, defenseTimeLimit: 24, voteTimeLimit: 24, protestDeadlineHours: 48, stewardingClosesHours: 72, notifyAccusedOnProtest: true, notifyOnVoteRequired: true, }), ).toThrow(RacingDomainValidationError); } }); it('validates numeric limits as non-negative / positive integers', () => { expect( () => new SeasonStewardingConfig({ decisionMode: 'steward_decides', requireDefense: true, defenseTimeLimit: -1, voteTimeLimit: 24, protestDeadlineHours: 48, stewardingClosesHours: 72, notifyAccusedOnProtest: true, notifyOnVoteRequired: true, }), ).toThrow(RacingDomainValidationError); expect( () => new SeasonStewardingConfig({ decisionMode: 'steward_decides', requireDefense: true, defenseTimeLimit: 24, voteTimeLimit: 0, protestDeadlineHours: 48, stewardingClosesHours: 72, notifyAccusedOnProtest: true, notifyOnVoteRequired: true, }), ).toThrow(RacingDomainValidationError); expect( () => new SeasonStewardingConfig({ decisionMode: 'steward_decides', requireDefense: true, defenseTimeLimit: 24, voteTimeLimit: 24, protestDeadlineHours: 0, stewardingClosesHours: 72, notifyAccusedOnProtest: true, notifyOnVoteRequired: true, }), ).toThrow(RacingDomainValidationError); expect( () => new SeasonStewardingConfig({ decisionMode: 'steward_decides', requireDefense: true, defenseTimeLimit: 24, voteTimeLimit: 24, protestDeadlineHours: 48, stewardingClosesHours: 0, notifyAccusedOnProtest: true, notifyOnVoteRequired: true, }), ).toThrow(RacingDomainValidationError); }); it('equals compares all props', () => { const a = new SeasonStewardingConfig({ decisionMode: 'steward_vote', requiredVotes: 3, requireDefense: true, defenseTimeLimit: 24, voteTimeLimit: 24, protestDeadlineHours: 48, stewardingClosesHours: 72, notifyAccusedOnProtest: true, notifyOnVoteRequired: true, }); const b = new SeasonStewardingConfig({ decisionMode: 'steward_vote', requiredVotes: 3, requireDefense: true, defenseTimeLimit: 24, voteTimeLimit: 24, protestDeadlineHours: 48, stewardingClosesHours: 72, notifyAccusedOnProtest: true, notifyOnVoteRequired: true, }); const c = new SeasonStewardingConfig({ decisionMode: 'steward_decides', requireDefense: false, defenseTimeLimit: 0, voteTimeLimit: 24, protestDeadlineHours: 24, stewardingClosesHours: 48, notifyAccusedOnProtest: false, notifyOnVoteRequired: false, }); expect(a.equals(b)).toBe(true); expect(a.equals(c)).toBe(false); }); });