refactor
This commit is contained in:
486
core/racing/domain/entities/Season.test.ts
Normal file
486
core/racing/domain/entities/Season.test.ts
Normal file
@@ -0,0 +1,486 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
|
||||
import {
|
||||
RacingDomainInvariantError,
|
||||
RacingDomainValidationError,
|
||||
} from '@core/racing/domain/errors/RacingDomainError';
|
||||
import {
|
||||
Season,
|
||||
type SeasonStatus,
|
||||
} from '@core/racing/domain/entities/Season';
|
||||
import { SeasonScoringConfig } from '@core/racing/domain/value-objects/SeasonScoringConfig';
|
||||
import {
|
||||
SeasonDropPolicy,
|
||||
type SeasonDropStrategy,
|
||||
} from '@core/racing/domain/value-objects/SeasonDropPolicy';
|
||||
import { SeasonStewardingConfig } from '@core/racing/domain/value-objects/SeasonStewardingConfig';
|
||||
import { createMinimalSeason, createBaseSeason } from '../../testing/factories/racing/SeasonFactory';
|
||||
|
||||
|
||||
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', () => {
|
||||
|
||||
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({
|
||||
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,
|
||||
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);
|
||||
});
|
||||
});
|
||||
88
core/racing/domain/services/DropScoreApplier.test.ts
Normal file
88
core/racing/domain/services/DropScoreApplier.test.ts
Normal file
@@ -0,0 +1,88 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
|
||||
import { DropScoreApplier } from '@core/racing/domain/services/DropScoreApplier';
|
||||
import type { EventPointsEntry } from '@core/racing/domain/services/DropScoreApplier';
|
||||
import type { DropScorePolicy } from '@core/racing/domain/types/DropScorePolicy';
|
||||
|
||||
describe('DropScoreApplier', () => {
|
||||
it('with strategy none counts all events and drops none', () => {
|
||||
const applier = new DropScoreApplier();
|
||||
|
||||
const policy: DropScorePolicy = {
|
||||
strategy: 'none',
|
||||
};
|
||||
|
||||
const events: EventPointsEntry[] = [
|
||||
{ eventId: 'event-1', points: 25 },
|
||||
{ eventId: 'event-2', points: 18 },
|
||||
{ eventId: 'event-3', points: 15 },
|
||||
];
|
||||
|
||||
const result = applier.apply(policy, events);
|
||||
|
||||
expect(result.counted).toHaveLength(3);
|
||||
expect(result.dropped).toHaveLength(0);
|
||||
expect(result.totalPoints).toBe(25 + 18 + 15);
|
||||
});
|
||||
|
||||
it('with bestNResults keeps the highest scoring events and drops the rest', () => {
|
||||
const applier = new DropScoreApplier();
|
||||
|
||||
const policy: DropScorePolicy = {
|
||||
strategy: 'bestNResults',
|
||||
count: 6,
|
||||
};
|
||||
|
||||
const events: EventPointsEntry[] = [
|
||||
{ eventId: 'event-1', points: 25 },
|
||||
{ eventId: 'event-2', points: 18 },
|
||||
{ eventId: 'event-3', points: 15 },
|
||||
{ eventId: 'event-4', points: 12 },
|
||||
{ eventId: 'event-5', points: 10 },
|
||||
{ eventId: 'event-6', points: 8 },
|
||||
{ eventId: 'event-7', points: 6 },
|
||||
{ eventId: 'event-8', points: 4 },
|
||||
];
|
||||
|
||||
const result = applier.apply(policy, events);
|
||||
|
||||
expect(result.counted).toHaveLength(6);
|
||||
expect(result.dropped).toHaveLength(2);
|
||||
|
||||
const countedIds = result.counted.map((e) => e.eventId);
|
||||
expect(countedIds).toEqual([
|
||||
'event-1',
|
||||
'event-2',
|
||||
'event-3',
|
||||
'event-4',
|
||||
'event-5',
|
||||
'event-6',
|
||||
]);
|
||||
|
||||
const droppedIds = result.dropped.map((e) => e.eventId);
|
||||
expect(droppedIds).toEqual(['event-7', 'event-8']);
|
||||
|
||||
expect(result.totalPoints).toBe(25 + 18 + 15 + 12 + 10 + 8);
|
||||
});
|
||||
|
||||
it('bestNResults with count greater than available events counts all of them', () => {
|
||||
const applier = new DropScoreApplier();
|
||||
|
||||
const policy: DropScorePolicy = {
|
||||
strategy: 'bestNResults',
|
||||
count: 10,
|
||||
};
|
||||
|
||||
const events: EventPointsEntry[] = [
|
||||
{ eventId: 'event-1', points: 25 },
|
||||
{ eventId: 'event-2', points: 18 },
|
||||
{ eventId: 'event-3', points: 15 },
|
||||
];
|
||||
|
||||
const result = applier.apply(policy, events);
|
||||
|
||||
expect(result.counted).toHaveLength(3);
|
||||
expect(result.dropped).toHaveLength(0);
|
||||
expect(result.totalPoints).toBe(25 + 18 + 15);
|
||||
});
|
||||
});
|
||||
211
core/racing/domain/services/EventScoringService.test.ts
Normal file
211
core/racing/domain/services/EventScoringService.test.ts
Normal file
@@ -0,0 +1,211 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
|
||||
import { EventScoringService } from '@core/racing/domain/services/EventScoringService';
|
||||
import type { ParticipantRef } from '@core/racing/domain/types/ParticipantRef';
|
||||
import type { SessionType } from '@core/racing/domain/types/SessionType';
|
||||
import { PointsTable } from '@core/racing/domain/value-objects/PointsTable';
|
||||
import type { BonusRule } from '@core/racing/domain/types/BonusRule';
|
||||
import type { ChampionshipConfig } from '@core/racing/domain/types/ChampionshipConfig';
|
||||
import { Result } from '@core/racing/domain/entities/Result';
|
||||
import type { Penalty } from '@core/racing/domain/entities/Penalty';
|
||||
import type { ChampionshipType } from '@core/racing/domain/types/ChampionshipType';
|
||||
import { makeDriverRef } from '../../testing/factories/racing/DriverRefFactory';
|
||||
import { makePointsTable } from '../../testing/factories/racing/PointsTableFactory';
|
||||
import { makeChampionshipConfig } from '../../testing/factories/racing/ChampionshipConfigFactory';
|
||||
|
||||
|
||||
describe('EventScoringService', () => {
|
||||
const seasonId = 'season-1';
|
||||
|
||||
it('assigns base points based on finishing positions for a main race', () => {
|
||||
const service = new EventScoringService();
|
||||
|
||||
const championship = makeChampionshipConfig({
|
||||
id: 'champ-1',
|
||||
name: 'Driver Championship',
|
||||
sessionTypes: ['main'],
|
||||
mainPoints: [25, 18, 15, 12, 10],
|
||||
});
|
||||
|
||||
const results: Result[] = [
|
||||
Result.create({
|
||||
id: 'result-1',
|
||||
raceId: 'race-1',
|
||||
driverId: 'driver-1',
|
||||
position: 1,
|
||||
fastestLap: 90000,
|
||||
incidents: 0,
|
||||
startPosition: 1,
|
||||
}),
|
||||
Result.create({
|
||||
id: 'result-2',
|
||||
raceId: 'race-1',
|
||||
driverId: 'driver-2',
|
||||
position: 2,
|
||||
fastestLap: 90500,
|
||||
incidents: 0,
|
||||
startPosition: 2,
|
||||
}),
|
||||
Result.create({
|
||||
id: 'result-3',
|
||||
raceId: 'race-1',
|
||||
driverId: 'driver-3',
|
||||
position: 3,
|
||||
fastestLap: 91000,
|
||||
incidents: 0,
|
||||
startPosition: 3,
|
||||
}),
|
||||
Result.create({
|
||||
id: 'result-4',
|
||||
raceId: 'race-1',
|
||||
driverId: 'driver-4',
|
||||
position: 4,
|
||||
fastestLap: 91500,
|
||||
incidents: 0,
|
||||
startPosition: 4,
|
||||
}),
|
||||
Result.create({
|
||||
id: 'result-5',
|
||||
raceId: 'race-1',
|
||||
driverId: 'driver-5',
|
||||
position: 5,
|
||||
fastestLap: 92000,
|
||||
incidents: 0,
|
||||
startPosition: 5,
|
||||
}),
|
||||
];
|
||||
|
||||
const penalties: Penalty[] = [];
|
||||
|
||||
const points = service.scoreSession({
|
||||
seasonId,
|
||||
championship,
|
||||
sessionType: 'main',
|
||||
results,
|
||||
penalties,
|
||||
});
|
||||
|
||||
const byParticipant = new Map(points.map((p) => [p.participant.id, p]));
|
||||
|
||||
expect(byParticipant.get('driver-1')?.basePoints).toBe(25);
|
||||
expect(byParticipant.get('driver-2')?.basePoints).toBe(18);
|
||||
expect(byParticipant.get('driver-3')?.basePoints).toBe(15);
|
||||
expect(byParticipant.get('driver-4')?.basePoints).toBe(12);
|
||||
expect(byParticipant.get('driver-5')?.basePoints).toBe(10);
|
||||
|
||||
for (const entry of byParticipant.values()) {
|
||||
expect(entry.bonusPoints).toBe(0);
|
||||
expect(entry.penaltyPoints).toBe(0);
|
||||
expect(entry.totalPoints).toBe(entry.basePoints);
|
||||
}
|
||||
});
|
||||
|
||||
it('applies fastest lap bonus only when inside top 10', () => {
|
||||
const service = new EventScoringService();
|
||||
|
||||
const fastestLapBonus: BonusRule = {
|
||||
id: 'bonus-fastest-lap',
|
||||
type: 'fastestLap',
|
||||
points: 1,
|
||||
requiresFinishInTopN: 10,
|
||||
};
|
||||
|
||||
const championship = makeChampionshipConfig({
|
||||
id: 'champ-1',
|
||||
name: 'Driver Championship',
|
||||
sessionTypes: ['main'],
|
||||
mainPoints: [25, 18, 15, 12, 10, 8, 6, 4, 2, 1],
|
||||
mainBonusRules: [fastestLapBonus],
|
||||
});
|
||||
|
||||
const baseResultTemplate = {
|
||||
raceId: 'race-1',
|
||||
incidents: 0,
|
||||
} as const;
|
||||
|
||||
const resultsP11Fastest: Result[] = [
|
||||
Result.create({
|
||||
id: 'result-1',
|
||||
...baseResultTemplate,
|
||||
driverId: 'driver-1',
|
||||
position: 1,
|
||||
startPosition: 1,
|
||||
fastestLap: 91000,
|
||||
}),
|
||||
Result.create({
|
||||
id: 'result-2',
|
||||
...baseResultTemplate,
|
||||
driverId: 'driver-2',
|
||||
position: 2,
|
||||
startPosition: 2,
|
||||
fastestLap: 90500,
|
||||
}),
|
||||
Result.create({
|
||||
id: 'result-3',
|
||||
...baseResultTemplate,
|
||||
driverId: 'driver-3',
|
||||
position: 11,
|
||||
startPosition: 15,
|
||||
fastestLap: 90000,
|
||||
}),
|
||||
];
|
||||
|
||||
const penalties: Penalty[] = [];
|
||||
|
||||
const pointsNoBonus = service.scoreSession({
|
||||
seasonId,
|
||||
championship,
|
||||
sessionType: 'main',
|
||||
results: resultsP11Fastest,
|
||||
penalties,
|
||||
});
|
||||
|
||||
const mapNoBonus = new Map(pointsNoBonus.map((p) => [p.participant.id, p]));
|
||||
|
||||
expect(mapNoBonus.get('driver-3')?.bonusPoints).toBe(0);
|
||||
|
||||
const resultsP8Fastest: Result[] = [
|
||||
Result.create({
|
||||
id: 'result-1',
|
||||
...baseResultTemplate,
|
||||
driverId: 'driver-1',
|
||||
position: 1,
|
||||
startPosition: 1,
|
||||
fastestLap: 91000,
|
||||
}),
|
||||
Result.create({
|
||||
id: 'result-2',
|
||||
...baseResultTemplate,
|
||||
driverId: 'driver-2',
|
||||
position: 2,
|
||||
startPosition: 2,
|
||||
fastestLap: 90500,
|
||||
}),
|
||||
Result.create({
|
||||
id: 'result-3',
|
||||
...baseResultTemplate,
|
||||
driverId: 'driver-3',
|
||||
position: 8,
|
||||
startPosition: 15,
|
||||
fastestLap: 90000,
|
||||
}),
|
||||
];
|
||||
|
||||
const pointsWithBonus = service.scoreSession({
|
||||
seasonId,
|
||||
championship,
|
||||
sessionType: 'main',
|
||||
results: resultsP8Fastest,
|
||||
penalties,
|
||||
});
|
||||
|
||||
const mapWithBonus = new Map(pointsWithBonus.map((p) => [p.participant.id, p]));
|
||||
|
||||
expect(mapWithBonus.get('driver-3')?.bonusPoints).toBe(1);
|
||||
expect(mapWithBonus.get('driver-3')?.totalPoints).toBe(
|
||||
(mapWithBonus.get('driver-3')?.basePoints || 0) +
|
||||
(mapWithBonus.get('driver-3')?.bonusPoints || 0) -
|
||||
(mapWithBonus.get('driver-3')?.penaltyPoints || 0),
|
||||
);
|
||||
});
|
||||
});
|
||||
278
core/racing/domain/services/ScheduleCalculator.test.ts
Normal file
278
core/racing/domain/services/ScheduleCalculator.test.ts
Normal file
@@ -0,0 +1,278 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { calculateRaceDates, getNextWeekday, type ScheduleConfig } from '@core/racing/domain/services/ScheduleCalculator';
|
||||
import type { Weekday } from '@core/racing/domain/types/Weekday';
|
||||
|
||||
describe('ScheduleCalculator', () => {
|
||||
describe('calculateRaceDates', () => {
|
||||
describe('with empty or invalid input', () => {
|
||||
it('should return empty array when weekdays is empty', () => {
|
||||
// Given
|
||||
const config: ScheduleConfig = {
|
||||
weekdays: [],
|
||||
frequency: 'weekly',
|
||||
rounds: 8,
|
||||
startDate: new Date('2024-01-01'),
|
||||
};
|
||||
|
||||
// When
|
||||
const result = calculateRaceDates(config);
|
||||
|
||||
// Then
|
||||
expect(result.raceDates).toEqual([]);
|
||||
expect(result.seasonDurationWeeks).toBe(0);
|
||||
});
|
||||
|
||||
it('should return empty array when rounds is 0', () => {
|
||||
// Given
|
||||
const config: ScheduleConfig = {
|
||||
weekdays: ['Sat'] as Weekday[],
|
||||
frequency: 'weekly',
|
||||
rounds: 0,
|
||||
startDate: new Date('2024-01-01'),
|
||||
};
|
||||
|
||||
// When
|
||||
const result = calculateRaceDates(config);
|
||||
|
||||
// Then
|
||||
expect(result.raceDates).toEqual([]);
|
||||
});
|
||||
|
||||
it('should return empty array when rounds is negative', () => {
|
||||
// Given
|
||||
const config: ScheduleConfig = {
|
||||
weekdays: ['Sat'] as Weekday[],
|
||||
frequency: 'weekly',
|
||||
rounds: -5,
|
||||
startDate: new Date('2024-01-01'),
|
||||
};
|
||||
|
||||
// When
|
||||
const result = calculateRaceDates(config);
|
||||
|
||||
// Then
|
||||
expect(result.raceDates).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('weekly scheduling', () => {
|
||||
it('should schedule 8 races on Saturdays starting from a Saturday', () => {
|
||||
// Given - January 6, 2024 is a Saturday
|
||||
const config: ScheduleConfig = {
|
||||
weekdays: ['Sat'] as Weekday[],
|
||||
frequency: 'weekly',
|
||||
rounds: 8,
|
||||
startDate: new Date('2024-01-06'),
|
||||
};
|
||||
|
||||
// When
|
||||
const result = calculateRaceDates(config);
|
||||
|
||||
// Then
|
||||
expect(result.raceDates.length).toBe(8);
|
||||
// All dates should be Saturdays
|
||||
result.raceDates.forEach(date => {
|
||||
expect(date.getDay()).toBe(6); // Saturday
|
||||
});
|
||||
// First race should be Jan 6
|
||||
expect(result.raceDates[0]!.toISOString().split('T')[0]).toBe('2024-01-06');
|
||||
// Last race should be 7 weeks later (Feb 24)
|
||||
expect(result.raceDates[7]!.toISOString().split('T')[0]).toBe('2024-02-24');
|
||||
});
|
||||
|
||||
it('should schedule races on multiple weekdays', () => {
|
||||
// Given
|
||||
const config: ScheduleConfig = {
|
||||
weekdays: ['Wed', 'Sat'] as Weekday[],
|
||||
frequency: 'weekly',
|
||||
rounds: 8,
|
||||
startDate: new Date('2024-01-01'), // Monday
|
||||
};
|
||||
|
||||
// When
|
||||
const result = calculateRaceDates(config);
|
||||
|
||||
// Then
|
||||
expect(result.raceDates.length).toBe(8);
|
||||
// Should alternate between Wednesday and Saturday
|
||||
result.raceDates.forEach(date => {
|
||||
const day = date.getDay();
|
||||
expect([3, 6]).toContain(day); // Wed=3, Sat=6
|
||||
});
|
||||
});
|
||||
|
||||
it('should schedule 8 races on Sundays', () => {
|
||||
// Given - January 7, 2024 is a Sunday
|
||||
const config: ScheduleConfig = {
|
||||
weekdays: ['Sun'] as Weekday[],
|
||||
frequency: 'weekly',
|
||||
rounds: 8,
|
||||
startDate: new Date('2024-01-01'),
|
||||
};
|
||||
|
||||
// When
|
||||
const result = calculateRaceDates(config);
|
||||
|
||||
// Then
|
||||
expect(result.raceDates.length).toBe(8);
|
||||
result.raceDates.forEach(date => {
|
||||
expect(date.getDay()).toBe(0); // Sunday
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('bi-weekly scheduling', () => {
|
||||
it('should schedule races every 2 weeks on Saturdays', () => {
|
||||
// Given - January 6, 2024 is a Saturday
|
||||
const config: ScheduleConfig = {
|
||||
weekdays: ['Sat'] as Weekday[],
|
||||
frequency: 'everyNWeeks',
|
||||
rounds: 4,
|
||||
startDate: new Date('2024-01-06'),
|
||||
intervalWeeks: 2,
|
||||
};
|
||||
|
||||
// When
|
||||
const result = calculateRaceDates(config);
|
||||
|
||||
// Then
|
||||
expect(result.raceDates.length).toBe(4);
|
||||
// First race Jan 6
|
||||
expect(result.raceDates[0]!.toISOString().split('T')[0]).toBe('2024-01-06');
|
||||
// Second race 2 weeks later (Jan 20)
|
||||
expect(result.raceDates[1]!.toISOString().split('T')[0]).toBe('2024-01-20');
|
||||
// Third race 2 weeks later (Feb 3)
|
||||
expect(result.raceDates[2]!.toISOString().split('T')[0]).toBe('2024-02-03');
|
||||
// Fourth race 2 weeks later (Feb 17)
|
||||
expect(result.raceDates[3]!.toISOString().split('T')[0]).toBe('2024-02-17');
|
||||
});
|
||||
});
|
||||
|
||||
describe('with start and end dates', () => {
|
||||
it('should evenly distribute races across the date range', () => {
|
||||
// Given - 3 month season
|
||||
const config: ScheduleConfig = {
|
||||
weekdays: ['Sat'] as Weekday[],
|
||||
frequency: 'weekly',
|
||||
rounds: 8,
|
||||
startDate: new Date('2024-01-06'),
|
||||
endDate: new Date('2024-03-30'),
|
||||
};
|
||||
|
||||
// When
|
||||
const result = calculateRaceDates(config);
|
||||
|
||||
// Then
|
||||
expect(result.raceDates.length).toBe(8);
|
||||
// First race should be at or near start
|
||||
expect(result.raceDates[0]!.toISOString().split('T')[0]).toBe('2024-01-06');
|
||||
// Races should be spread across the range, not consecutive weeks
|
||||
});
|
||||
|
||||
it('should use all available days if fewer than rounds requested', () => {
|
||||
// Given - short period with only 3 Saturdays
|
||||
const config: ScheduleConfig = {
|
||||
weekdays: ['Sat'] as Weekday[],
|
||||
frequency: 'weekly',
|
||||
rounds: 10,
|
||||
startDate: new Date('2024-01-06'),
|
||||
endDate: new Date('2024-01-21'),
|
||||
};
|
||||
|
||||
// When
|
||||
const result = calculateRaceDates(config);
|
||||
|
||||
// Then
|
||||
// Only 3 Saturdays in this range: Jan 6, 13, 20
|
||||
expect(result.raceDates.length).toBe(3);
|
||||
});
|
||||
});
|
||||
|
||||
describe('season duration calculation', () => {
|
||||
it('should calculate correct season duration in weeks', () => {
|
||||
// Given
|
||||
const config: ScheduleConfig = {
|
||||
weekdays: ['Sat'] as Weekday[],
|
||||
frequency: 'weekly',
|
||||
rounds: 8,
|
||||
startDate: new Date('2024-01-06'),
|
||||
};
|
||||
|
||||
// When
|
||||
const result = calculateRaceDates(config);
|
||||
|
||||
// Then
|
||||
// 8 races, 1 week apart = 7 weeks duration
|
||||
expect(result.seasonDurationWeeks).toBe(7);
|
||||
});
|
||||
|
||||
it('should return 0 duration for single race', () => {
|
||||
// Given
|
||||
const config: ScheduleConfig = {
|
||||
weekdays: ['Sat'] as Weekday[],
|
||||
frequency: 'weekly',
|
||||
rounds: 1,
|
||||
startDate: new Date('2024-01-06'),
|
||||
};
|
||||
|
||||
// When
|
||||
const result = calculateRaceDates(config);
|
||||
|
||||
// Then
|
||||
expect(result.raceDates.length).toBe(1);
|
||||
expect(result.seasonDurationWeeks).toBe(0);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('getNextWeekday', () => {
|
||||
it('should return next Saturday from a Monday', () => {
|
||||
// Given - January 1, 2024 is a Monday
|
||||
const fromDate = new Date('2024-01-01');
|
||||
|
||||
// When
|
||||
const result = getNextWeekday(fromDate, 'Sat');
|
||||
|
||||
// Then
|
||||
expect(result.toISOString().split('T')[0]).toBe('2024-01-06');
|
||||
expect(result.getDay()).toBe(6);
|
||||
});
|
||||
|
||||
it('should return next occurrence when already on that weekday', () => {
|
||||
// Given - January 6, 2024 is a Saturday
|
||||
const fromDate = new Date('2024-01-06');
|
||||
|
||||
// When
|
||||
const result = getNextWeekday(fromDate, 'Sat');
|
||||
|
||||
// Then
|
||||
// Should return NEXT Saturday (7 days later), not same day
|
||||
expect(result.toISOString().split('T')[0]).toBe('2024-01-13');
|
||||
});
|
||||
|
||||
it('should return next Sunday from a Friday', () => {
|
||||
// Given - January 5, 2024 is a Friday
|
||||
const fromDate = new Date('2024-01-05');
|
||||
|
||||
// When
|
||||
const result = getNextWeekday(fromDate, 'Sun');
|
||||
|
||||
// Then
|
||||
expect(result.toISOString().split('T')[0]).toBe('2024-01-07');
|
||||
expect(result.getDay()).toBe(0);
|
||||
});
|
||||
|
||||
it('should return next Wednesday from a Thursday', () => {
|
||||
// Given - January 4, 2024 is a Thursday
|
||||
const fromDate = new Date('2024-01-04');
|
||||
|
||||
// When
|
||||
const result = getNextWeekday(fromDate, 'Wed');
|
||||
|
||||
// Then
|
||||
// Next Wednesday is 6 days later
|
||||
expect(result.toISOString().split('T')[0]).toBe('2024-01-10');
|
||||
expect(result.getDay()).toBe(3);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user