This commit is contained in:
2025-11-26 17:03:29 +01:00
parent ff3528e5ef
commit fef75008d8
147 changed files with 112370 additions and 5162 deletions

View File

@@ -0,0 +1,111 @@
import { describe, test, expect } from 'vitest';
import { BrowserAuthenticationState } from '../../../../packages/domain/value-objects/BrowserAuthenticationState';
import { AuthenticationState } from '../../../../packages/domain/value-objects/AuthenticationState';
describe('BrowserAuthenticationState', () => {
describe('isFullyAuthenticated()', () => {
test('should return true when both cookies and page authenticated', () => {
const state = new BrowserAuthenticationState(true, true);
expect(state.isFullyAuthenticated()).toBe(true);
});
test('should return false when cookies valid but page unauthenticated', () => {
const state = new BrowserAuthenticationState(true, false);
expect(state.isFullyAuthenticated()).toBe(false);
});
test('should return false when cookies invalid but page authenticated', () => {
const state = new BrowserAuthenticationState(false, true);
expect(state.isFullyAuthenticated()).toBe(false);
});
test('should return false when both cookies and page unauthenticated', () => {
const state = new BrowserAuthenticationState(false, false);
expect(state.isFullyAuthenticated()).toBe(false);
});
});
describe('getAuthenticationState()', () => {
test('should return AUTHENTICATED when both cookies and page authenticated', () => {
const state = new BrowserAuthenticationState(true, true);
expect(state.getAuthenticationState()).toBe(AuthenticationState.AUTHENTICATED);
});
test('should return EXPIRED when cookies valid but page unauthenticated', () => {
const state = new BrowserAuthenticationState(true, false);
expect(state.getAuthenticationState()).toBe(AuthenticationState.EXPIRED);
});
test('should return UNKNOWN when cookies invalid', () => {
const state = new BrowserAuthenticationState(false, false);
expect(state.getAuthenticationState()).toBe(AuthenticationState.UNKNOWN);
});
test('should return UNKNOWN when cookies invalid regardless of page state', () => {
const state = new BrowserAuthenticationState(false, true);
expect(state.getAuthenticationState()).toBe(AuthenticationState.UNKNOWN);
});
});
describe('requiresReauthentication()', () => {
test('should return false when fully authenticated', () => {
const state = new BrowserAuthenticationState(true, true);
expect(state.requiresReauthentication()).toBe(false);
});
test('should return true when cookies valid but page unauthenticated', () => {
const state = new BrowserAuthenticationState(true, false);
expect(state.requiresReauthentication()).toBe(true);
});
test('should return true when cookies invalid', () => {
const state = new BrowserAuthenticationState(false, false);
expect(state.requiresReauthentication()).toBe(true);
});
test('should return true when cookies invalid but page authenticated', () => {
const state = new BrowserAuthenticationState(false, true);
expect(state.requiresReauthentication()).toBe(true);
});
});
describe('getCookieValidity()', () => {
test('should return true when cookies are valid', () => {
const state = new BrowserAuthenticationState(true, true);
expect(state.getCookieValidity()).toBe(true);
});
test('should return false when cookies are invalid', () => {
const state = new BrowserAuthenticationState(false, false);
expect(state.getCookieValidity()).toBe(false);
});
});
describe('getPageAuthenticationStatus()', () => {
test('should return true when page is authenticated', () => {
const state = new BrowserAuthenticationState(true, true);
expect(state.getPageAuthenticationStatus()).toBe(true);
});
test('should return false when page is unauthenticated', () => {
const state = new BrowserAuthenticationState(true, false);
expect(state.getPageAuthenticationStatus()).toBe(false);
});
});
});

View File

@@ -0,0 +1,90 @@
import { describe, it, expect } from 'vitest';
import { CheckoutConfirmation } from '../../../../packages/domain/value-objects/CheckoutConfirmation';
describe('CheckoutConfirmation Value Object', () => {
describe('create', () => {
it('should create confirmed decision', () => {
const confirmation = CheckoutConfirmation.create('confirmed');
expect(confirmation.value).toBe('confirmed');
});
it('should create cancelled decision', () => {
const confirmation = CheckoutConfirmation.create('cancelled');
expect(confirmation.value).toBe('cancelled');
});
it('should create timeout decision', () => {
const confirmation = CheckoutConfirmation.create('timeout');
expect(confirmation.value).toBe('timeout');
});
it('should throw error for invalid decision', () => {
expect(() => CheckoutConfirmation.create('invalid' as any)).toThrow('Invalid checkout confirmation decision');
});
});
describe('isConfirmed', () => {
it('should return true for confirmed decision', () => {
const confirmation = CheckoutConfirmation.create('confirmed');
expect(confirmation.isConfirmed()).toBe(true);
});
it('should return false for cancelled decision', () => {
const confirmation = CheckoutConfirmation.create('cancelled');
expect(confirmation.isConfirmed()).toBe(false);
});
it('should return false for timeout decision', () => {
const confirmation = CheckoutConfirmation.create('timeout');
expect(confirmation.isConfirmed()).toBe(false);
});
});
describe('isCancelled', () => {
it('should return true for cancelled decision', () => {
const confirmation = CheckoutConfirmation.create('cancelled');
expect(confirmation.isCancelled()).toBe(true);
});
it('should return false for confirmed decision', () => {
const confirmation = CheckoutConfirmation.create('confirmed');
expect(confirmation.isCancelled()).toBe(false);
});
it('should return false for timeout decision', () => {
const confirmation = CheckoutConfirmation.create('timeout');
expect(confirmation.isCancelled()).toBe(false);
});
});
describe('isTimeout', () => {
it('should return true for timeout decision', () => {
const confirmation = CheckoutConfirmation.create('timeout');
expect(confirmation.isTimeout()).toBe(true);
});
it('should return false for confirmed decision', () => {
const confirmation = CheckoutConfirmation.create('confirmed');
expect(confirmation.isTimeout()).toBe(false);
});
it('should return false for cancelled decision', () => {
const confirmation = CheckoutConfirmation.create('cancelled');
expect(confirmation.isTimeout()).toBe(false);
});
});
describe('equals', () => {
it('should return true for equal confirmations', () => {
const confirmation1 = CheckoutConfirmation.create('confirmed');
const confirmation2 = CheckoutConfirmation.create('confirmed');
expect(confirmation1.equals(confirmation2)).toBe(true);
});
it('should return false for different confirmations', () => {
const confirmation1 = CheckoutConfirmation.create('confirmed');
const confirmation2 = CheckoutConfirmation.create('cancelled');
expect(confirmation1.equals(confirmation2)).toBe(false);
});
});
});

View File

@@ -0,0 +1,163 @@
import { describe, it, expect } from 'vitest';
import { CheckoutPrice } from '../../../../packages/domain/value-objects/CheckoutPrice';
/**
* CheckoutPrice Value Object - GREEN PHASE
*
* Tests for price validation, parsing, and formatting.
*/
describe('CheckoutPrice Value Object', () => {
describe('Construction', () => {
it('should create with valid price $0.50', () => {
expect(() => new CheckoutPrice(0.50)).not.toThrow();
});
it('should create with valid price $10.00', () => {
expect(() => new CheckoutPrice(10.00)).not.toThrow();
});
it('should create with valid price $100.00', () => {
expect(() => new CheckoutPrice(100.00)).not.toThrow();
});
it('should reject negative prices', () => {
expect(() => new CheckoutPrice(-0.50)).toThrow(/negative/i);
});
it('should reject excessive prices over $10,000', () => {
expect(() => new CheckoutPrice(10000.01)).toThrow(/excessive|maximum/i);
});
it('should accept exactly $10,000', () => {
expect(() => new CheckoutPrice(10000.00)).not.toThrow();
});
it('should accept $0.00 (zero price)', () => {
expect(() => new CheckoutPrice(0.00)).not.toThrow();
});
});
describe('fromString() parsing', () => {
it('should extract $0.50 from string', () => {
const price = CheckoutPrice.fromString('$0.50');
expect(price.getAmount()).toBe(0.50);
});
it('should extract $10.00 from string', () => {
const price = CheckoutPrice.fromString('$10.00');
expect(price.getAmount()).toBe(10.00);
});
it('should extract $100.00 from string', () => {
const price = CheckoutPrice.fromString('$100.00');
expect(price.getAmount()).toBe(100.00);
});
it('should reject string without dollar sign', () => {
expect(() => CheckoutPrice.fromString('10.00')).toThrow(/invalid.*format/i);
});
it('should reject string with multiple dollar signs', () => {
expect(() => CheckoutPrice.fromString('$$10.00')).toThrow(/invalid.*format/i);
});
it('should reject non-numeric values', () => {
expect(() => CheckoutPrice.fromString('$abc')).toThrow(/invalid.*format/i);
});
it('should reject empty string', () => {
expect(() => CheckoutPrice.fromString('')).toThrow(/invalid.*format/i);
});
it('should handle prices with commas $1,000.00', () => {
const price = CheckoutPrice.fromString('$1,000.00');
expect(price.getAmount()).toBe(1000.00);
});
it('should handle whitespace around price', () => {
const price = CheckoutPrice.fromString(' $5.00 ');
expect(price.getAmount()).toBe(5.00);
});
});
describe('Display formatting', () => {
it('should format $0.50 as "$0.50"', () => {
const price = new CheckoutPrice(0.50);
expect(price.toDisplayString()).toBe('$0.50');
});
it('should format $10.00 as "$10.00"', () => {
const price = new CheckoutPrice(10.00);
expect(price.toDisplayString()).toBe('$10.00');
});
it('should format $100.00 as "$100.00"', () => {
const price = new CheckoutPrice(100.00);
expect(price.toDisplayString()).toBe('$100.00');
});
it('should always show two decimal places', () => {
const price = new CheckoutPrice(5);
expect(price.toDisplayString()).toBe('$5.00');
});
it('should round to two decimal places', () => {
const price = new CheckoutPrice(5.129);
expect(price.toDisplayString()).toBe('$5.13');
});
});
describe('Zero check', () => {
it('should detect $0.00 correctly', () => {
const price = new CheckoutPrice(0.00);
expect(price.isZero()).toBe(true);
});
it('should return false for non-zero prices', () => {
const price = new CheckoutPrice(0.50);
expect(price.isZero()).toBe(false);
});
it('should handle floating point precision for zero', () => {
const price = new CheckoutPrice(0.0000001);
expect(price.isZero()).toBe(true);
});
});
describe('Edge Cases', () => {
it('should handle very small prices $0.01', () => {
const price = new CheckoutPrice(0.01);
expect(price.toDisplayString()).toBe('$0.01');
});
it('should handle large prices $9,999.99', () => {
const price = new CheckoutPrice(9999.99);
expect(price.toDisplayString()).toBe('$9999.99');
});
it('should be immutable after creation', () => {
const price = new CheckoutPrice(5.00);
const amount = price.getAmount();
expect(amount).toBe(5.00);
// Verify no setters exist
expect(typeof (price as any).setAmount).toBe('undefined');
});
});
describe('BDD Scenarios', () => {
it('Given price string "$0.50", When parsing, Then amount is 0.50', () => {
const price = CheckoutPrice.fromString('$0.50');
expect(price.getAmount()).toBe(0.50);
});
it('Given amount 10.00, When formatting, Then display is "$10.00"', () => {
const price = new CheckoutPrice(10.00);
expect(price.toDisplayString()).toBe('$10.00');
});
it('Given negative amount, When constructing, Then error is thrown', () => {
expect(() => new CheckoutPrice(-5.00)).toThrow();
});
});
});

View File

@@ -0,0 +1,126 @@
import { describe, it, expect } from 'vitest';
import { CheckoutState, CheckoutStateEnum } from '../../../../packages/domain/value-objects/CheckoutState';
/**
* CheckoutState Value Object - GREEN PHASE
*
* Tests for checkout button state detection.
*/
describe('CheckoutState Value Object', () => {
describe('READY state', () => {
it('should create READY state from btn-success class', () => {
const state = CheckoutState.fromButtonClasses('btn btn-success');
expect(state.getValue()).toBe(CheckoutStateEnum.READY);
});
it('should detect ready state correctly', () => {
const state = CheckoutState.fromButtonClasses('btn btn-success');
expect(state.isReady()).toBe(true);
expect(state.hasInsufficientFunds()).toBe(false);
});
it('should handle additional classes with btn-success', () => {
const state = CheckoutState.fromButtonClasses('btn btn-lg btn-success pull-right');
expect(state.getValue()).toBe(CheckoutStateEnum.READY);
});
it('should be case-insensitive for btn-success', () => {
const state = CheckoutState.fromButtonClasses('btn BTN-SUCCESS');
expect(state.getValue()).toBe(CheckoutStateEnum.READY);
});
});
describe('INSUFFICIENT_FUNDS state', () => {
it('should create INSUFFICIENT_FUNDS from btn-default without btn-success', () => {
const state = CheckoutState.fromButtonClasses('btn btn-default');
expect(state.getValue()).toBe(CheckoutStateEnum.INSUFFICIENT_FUNDS);
});
it('should detect insufficient funds correctly', () => {
const state = CheckoutState.fromButtonClasses('btn btn-default');
expect(state.isReady()).toBe(false);
expect(state.hasInsufficientFunds()).toBe(true);
});
it('should handle btn-primary as insufficient funds', () => {
const state = CheckoutState.fromButtonClasses('btn btn-primary');
expect(state.getValue()).toBe(CheckoutStateEnum.INSUFFICIENT_FUNDS);
});
it('should handle btn-warning as insufficient funds', () => {
const state = CheckoutState.fromButtonClasses('btn btn-warning');
expect(state.getValue()).toBe(CheckoutStateEnum.INSUFFICIENT_FUNDS);
});
it('should handle disabled button as insufficient funds', () => {
const state = CheckoutState.fromButtonClasses('btn btn-default disabled');
expect(state.getValue()).toBe(CheckoutStateEnum.INSUFFICIENT_FUNDS);
});
});
describe('UNKNOWN state', () => {
it('should create UNKNOWN when no btn class exists', () => {
const state = CheckoutState.fromButtonClasses('some-other-class');
expect(state.getValue()).toBe(CheckoutStateEnum.UNKNOWN);
});
it('should create UNKNOWN from empty string', () => {
const state = CheckoutState.fromButtonClasses('');
expect(state.getValue()).toBe(CheckoutStateEnum.UNKNOWN);
});
it('should detect unknown state correctly', () => {
const state = CheckoutState.fromButtonClasses('');
expect(state.isReady()).toBe(false);
expect(state.hasInsufficientFunds()).toBe(false);
});
});
describe('Edge Cases', () => {
it('should handle whitespace in class names', () => {
const state = CheckoutState.fromButtonClasses(' btn btn-success ');
expect(state.getValue()).toBe(CheckoutStateEnum.READY);
});
it('should handle multiple spaces between classes', () => {
const state = CheckoutState.fromButtonClasses('btn btn-success');
expect(state.getValue()).toBe(CheckoutStateEnum.READY);
});
it('should be immutable after creation', () => {
const state = CheckoutState.fromButtonClasses('btn btn-success');
const originalState = state.getValue();
expect(originalState).toBe(CheckoutStateEnum.READY);
// Verify no setters exist
expect(typeof (state as any).setState).toBe('undefined');
});
});
describe('BDD Scenarios', () => {
it('Given button with btn-success, When checking state, Then state is READY', () => {
const state = CheckoutState.fromButtonClasses('btn btn-success');
expect(state.getValue()).toBe(CheckoutStateEnum.READY);
});
it('Given button without btn-success, When checking state, Then state is INSUFFICIENT_FUNDS', () => {
const state = CheckoutState.fromButtonClasses('btn btn-default');
expect(state.getValue()).toBe(CheckoutStateEnum.INSUFFICIENT_FUNDS);
});
it('Given no button classes, When checking state, Then state is UNKNOWN', () => {
const state = CheckoutState.fromButtonClasses('');
expect(state.getValue()).toBe(CheckoutStateEnum.UNKNOWN);
});
it('Given READY state, When checking isReady, Then returns true', () => {
const state = CheckoutState.fromButtonClasses('btn btn-success');
expect(state.isReady()).toBe(true);
});
it('Given INSUFFICIENT_FUNDS state, When checking hasInsufficientFunds, Then returns true', () => {
const state = CheckoutState.fromButtonClasses('btn btn-default');
expect(state.hasInsufficientFunds()).toBe(true);
});
});
});

View File

@@ -0,0 +1,288 @@
import { describe, test, expect } from 'vitest';
import { CookieConfiguration } from '../../../../packages/domain/value-objects/CookieConfiguration';
describe('CookieConfiguration', () => {
const validTargetUrl = 'https://members-ng.iracing.com/jjwtauth/success';
describe('domain validation', () => {
test('should accept exact domain match', () => {
const config = {
name: 'test_cookie',
value: 'test_value',
domain: 'members-ng.iracing.com',
path: '/',
};
expect(() => new CookieConfiguration(config, validTargetUrl)).not.toThrow();
});
test('should accept wildcard domain for subdomain match', () => {
const config = {
name: 'test_cookie',
value: 'test_value',
domain: '.iracing.com',
path: '/',
};
expect(() => new CookieConfiguration(config, validTargetUrl)).not.toThrow();
});
test('should accept wildcard domain for base domain match', () => {
const config = {
name: 'test_cookie',
value: 'test_value',
domain: '.iracing.com',
path: '/',
};
const baseUrl = 'https://iracing.com/';
expect(() => new CookieConfiguration(config, baseUrl)).not.toThrow();
});
test('should match wildcard domain with multiple subdomain levels', () => {
const config = {
name: 'test_cookie',
value: 'test_value',
domain: '.iracing.com',
path: '/',
};
const deepUrl = 'https://api.members-ng.iracing.com/endpoint';
expect(() => new CookieConfiguration(config, deepUrl)).not.toThrow();
});
test('should throw error when domain does not match target', () => {
const config = {
name: 'test_cookie',
value: 'test_value',
domain: 'example.com',
path: '/',
};
expect(() => new CookieConfiguration(config, validTargetUrl))
.toThrow(/domain mismatch/i);
});
test('should throw error when wildcard domain does not match target', () => {
const config = {
name: 'test_cookie',
value: 'test_value',
domain: '.example.com',
path: '/',
};
expect(() => new CookieConfiguration(config, validTargetUrl))
.toThrow(/domain mismatch/i);
});
test('should throw error when subdomain does not match wildcard', () => {
const config = {
name: 'test_cookie',
value: 'test_value',
domain: '.racing.com',
path: '/',
};
expect(() => new CookieConfiguration(config, validTargetUrl))
.toThrow(/domain mismatch/i);
});
test('should accept cookies from related subdomains with same base domain', () => {
const cookie = {
name: 'XSESSIONID',
value: 'session_value',
domain: 'members.iracing.com',
path: '/',
};
// Should work: members.iracing.com → members-ng.iracing.com
// Both share base domain "iracing.com"
expect(() =>
new CookieConfiguration(cookie, 'https://members-ng.iracing.com/web/racing')
).not.toThrow();
const config = new CookieConfiguration(cookie, 'https://members-ng.iracing.com/web/racing');
expect(config.getValidatedCookie().name).toBe('XSESSIONID');
});
test('should reject cookies from different base domains', () => {
const cookie = {
name: 'SESSION',
value: 'session_value',
domain: 'example.com',
path: '/',
};
// Should fail: example.com ≠ iracing.com
expect(() =>
new CookieConfiguration(cookie, 'https://members.iracing.com/web/racing')
).toThrow(/domain mismatch/i);
});
test('should accept cookies from exact subdomain match', () => {
const cookie = {
name: 'SESSION',
value: 'session_value',
domain: 'members-ng.iracing.com',
path: '/',
};
// Exact match should always work
expect(() =>
new CookieConfiguration(cookie, 'https://members-ng.iracing.com/web/racing')
).not.toThrow();
});
test('should accept cookies between different subdomains of same base domain', () => {
const cookie = {
name: 'AUTH_TOKEN',
value: 'token_value',
domain: 'api.iracing.com',
path: '/',
};
// Should work: api.iracing.com → members-ng.iracing.com
expect(() =>
new CookieConfiguration(cookie, 'https://members-ng.iracing.com/api')
).not.toThrow();
});
test('should reject subdomain cookies when base domain has insufficient parts', () => {
const cookie = {
name: 'TEST',
value: 'test_value',
domain: 'localhost',
path: '/',
};
// Single-part domain should not match different single-part domain
expect(() =>
new CookieConfiguration(cookie, 'https://example/path')
).toThrow(/domain mismatch/i);
});
});
describe('path validation', () => {
test('should accept root path for any target path', () => {
const config = {
name: 'test_cookie',
value: 'test_value',
domain: 'members-ng.iracing.com',
path: '/',
};
expect(() => new CookieConfiguration(config, validTargetUrl)).not.toThrow();
});
test('should accept path that is prefix of target path', () => {
const config = {
name: 'test_cookie',
value: 'test_value',
domain: 'members-ng.iracing.com',
path: '/jjwtauth',
};
expect(() => new CookieConfiguration(config, validTargetUrl)).not.toThrow();
});
test('should throw error when path is not prefix of target path', () => {
const config = {
name: 'test_cookie',
value: 'test_value',
domain: 'members-ng.iracing.com',
path: '/other/path',
};
expect(() => new CookieConfiguration(config, validTargetUrl))
.toThrow(/path.*not valid/i);
});
test('should throw error when path is longer than target path', () => {
const config = {
name: 'test_cookie',
value: 'test_value',
domain: 'members-ng.iracing.com',
path: '/jjwtauth/success/extra',
};
expect(() => new CookieConfiguration(config, validTargetUrl))
.toThrow(/path.*not valid/i);
});
});
describe('getValidatedCookie()', () => {
test('should return cookie with validated domain and path', () => {
const config = {
name: 'test_cookie',
value: 'test_value',
domain: 'members-ng.iracing.com',
path: '/',
};
const cookieConfig = new CookieConfiguration(config, validTargetUrl);
const cookie = cookieConfig.getValidatedCookie();
expect(cookie.name).toBe('test_cookie');
expect(cookie.value).toBe('test_value');
expect(cookie.domain).toBe('members-ng.iracing.com');
expect(cookie.path).toBe('/');
});
test('should preserve all cookie properties', () => {
const config = {
name: 'test_cookie',
value: 'test_value',
domain: 'members-ng.iracing.com',
path: '/',
secure: true,
httpOnly: true,
sameSite: 'Lax' as const,
};
const cookieConfig = new CookieConfiguration(config, validTargetUrl);
const cookie = cookieConfig.getValidatedCookie();
expect(cookie.secure).toBe(true);
expect(cookie.httpOnly).toBe(true);
expect(cookie.sameSite).toBe('Lax');
});
});
describe('edge cases', () => {
test('should handle empty domain', () => {
const config = {
name: 'test_cookie',
value: 'test_value',
domain: '',
path: '/',
};
expect(() => new CookieConfiguration(config, validTargetUrl))
.toThrow(/domain mismatch/i);
});
test('should handle empty path', () => {
const config = {
name: 'test_cookie',
value: 'test_value',
domain: 'members-ng.iracing.com',
path: '',
};
expect(() => new CookieConfiguration(config, validTargetUrl))
.toThrow(/path.*not valid/i);
});
test('should handle malformed target URL', () => {
const config = {
name: 'test_cookie',
value: 'test_value',
domain: 'members-ng.iracing.com',
path: '/',
};
expect(() => new CookieConfiguration(config, 'not-a-valid-url'))
.toThrow();
});
});
});

View File

@@ -0,0 +1,107 @@
import { describe, it, expect } from 'vitest';
import { RaceCreationResult } from '../../../../packages/domain/value-objects/RaceCreationResult';
describe('RaceCreationResult Value Object', () => {
describe('create', () => {
it('should create race creation result with all fields', () => {
const result = RaceCreationResult.create({
sessionId: 'test-session-123',
price: '$10.00',
timestamp: new Date('2025-11-25T12:00:00Z'),
});
expect(result.sessionId).toBe('test-session-123');
expect(result.price).toBe('$10.00');
expect(result.timestamp).toEqual(new Date('2025-11-25T12:00:00Z'));
});
it('should throw error for empty session ID', () => {
expect(() =>
RaceCreationResult.create({
sessionId: '',
price: '$10.00',
timestamp: new Date(),
})
).toThrow('Session ID cannot be empty');
});
it('should throw error for empty price', () => {
expect(() =>
RaceCreationResult.create({
sessionId: 'test-session-123',
price: '',
timestamp: new Date(),
})
).toThrow('Price cannot be empty');
});
});
describe('equals', () => {
it('should return true for equal results', () => {
const timestamp = new Date('2025-11-25T12:00:00Z');
const result1 = RaceCreationResult.create({
sessionId: 'test-session-123',
price: '$10.00',
timestamp,
});
const result2 = RaceCreationResult.create({
sessionId: 'test-session-123',
price: '$10.00',
timestamp,
});
expect(result1.equals(result2)).toBe(true);
});
it('should return false for different session IDs', () => {
const timestamp = new Date('2025-11-25T12:00:00Z');
const result1 = RaceCreationResult.create({
sessionId: 'test-session-123',
price: '$10.00',
timestamp,
});
const result2 = RaceCreationResult.create({
sessionId: 'test-session-456',
price: '$10.00',
timestamp,
});
expect(result1.equals(result2)).toBe(false);
});
it('should return false for different prices', () => {
const timestamp = new Date('2025-11-25T12:00:00Z');
const result1 = RaceCreationResult.create({
sessionId: 'test-session-123',
price: '$10.00',
timestamp,
});
const result2 = RaceCreationResult.create({
sessionId: 'test-session-123',
price: '$20.00',
timestamp,
});
expect(result1.equals(result2)).toBe(false);
});
});
describe('toJSON', () => {
it('should serialize to JSON correctly', () => {
const timestamp = new Date('2025-11-25T12:00:00Z');
const result = RaceCreationResult.create({
sessionId: 'test-session-123',
price: '$10.00',
timestamp,
});
const json = result.toJSON();
expect(json).toEqual({
sessionId: 'test-session-123',
price: '$10.00',
timestamp: timestamp.toISOString(),
});
});
});
});

View File

@@ -0,0 +1,103 @@
import { describe, it, expect } from 'vitest';
import { SessionLifetime } from '../../../../packages/domain/value-objects/SessionLifetime';
describe('SessionLifetime Value Object', () => {
describe('Construction', () => {
it('should create with valid expiry date', () => {
const futureDate = new Date(Date.now() + 3600000);
expect(() => new SessionLifetime(futureDate)).not.toThrow();
});
it('should create with null expiry (no expiration)', () => {
expect(() => new SessionLifetime(null)).not.toThrow();
});
it('should reject invalid dates', () => {
const invalidDate = new Date('invalid');
expect(() => new SessionLifetime(invalidDate)).toThrow();
});
it('should reject dates in the past', () => {
const pastDate = new Date(Date.now() - 3600000);
expect(() => new SessionLifetime(pastDate)).toThrow();
});
});
describe('isExpired()', () => {
it('should return true for expired date', () => {
const pastDate = new Date(Date.now() - 1000);
const lifetime = new SessionLifetime(pastDate);
expect(lifetime.isExpired()).toBe(true);
});
it('should return false for valid future date', () => {
const futureDate = new Date(Date.now() + 3600000);
const lifetime = new SessionLifetime(futureDate);
expect(lifetime.isExpired()).toBe(false);
});
it('should return false for null expiry (never expires)', () => {
const lifetime = new SessionLifetime(null);
expect(lifetime.isExpired()).toBe(false);
});
it('should consider buffer time (5 minutes)', () => {
const nearExpiryDate = new Date(Date.now() + 240000);
const lifetime = new SessionLifetime(nearExpiryDate);
expect(lifetime.isExpired()).toBe(true);
});
it('should not consider expired when beyond buffer', () => {
const safeDate = new Date(Date.now() + 360000);
const lifetime = new SessionLifetime(safeDate);
expect(lifetime.isExpired()).toBe(false);
});
});
describe('isExpiringSoon()', () => {
it('should return true for date within buffer window', () => {
const soonDate = new Date(Date.now() + 240000);
const lifetime = new SessionLifetime(soonDate);
expect(lifetime.isExpiringSoon()).toBe(true);
});
it('should return false for date far in future', () => {
const farDate = new Date(Date.now() + 3600000);
const lifetime = new SessionLifetime(farDate);
expect(lifetime.isExpiringSoon()).toBe(false);
});
it('should return false for null expiry', () => {
const lifetime = new SessionLifetime(null);
expect(lifetime.isExpiringSoon()).toBe(false);
});
it('should return true exactly at buffer boundary (5 minutes)', () => {
const boundaryDate = new Date(Date.now() + 300000);
const lifetime = new SessionLifetime(boundaryDate);
expect(lifetime.isExpiringSoon()).toBe(true);
});
});
describe('Edge Cases', () => {
it('should handle timezone correctly', () => {
const utcDate = new Date('2025-12-31T23:59:59Z');
const lifetime = new SessionLifetime(utcDate);
expect(lifetime.getExpiry()).toEqual(utcDate);
});
it('should handle millisecond precision', () => {
const preciseDate = new Date(Date.now() + 299999);
const lifetime = new SessionLifetime(preciseDate);
expect(lifetime.isExpiringSoon()).toBe(true);
});
it('should provide remaining time', () => {
const futureDate = new Date(Date.now() + 3600000);
const lifetime = new SessionLifetime(futureDate);
const remaining = lifetime.getRemainingTime();
expect(remaining).toBeGreaterThan(3000000);
expect(remaining).toBeLessThanOrEqual(3600000);
});
});
});

View File

@@ -33,6 +33,16 @@ describe('SessionState Value Object', () => {
expect(state.value).toBe('STOPPED_AT_STEP_18');
});
it('should create AWAITING_CHECKOUT_CONFIRMATION state', () => {
const state = SessionState.create('AWAITING_CHECKOUT_CONFIRMATION');
expect(state.value).toBe('AWAITING_CHECKOUT_CONFIRMATION');
});
it('should create CANCELLED state', () => {
const state = SessionState.create('CANCELLED');
expect(state.value).toBe('CANCELLED');
});
it('should throw error for invalid state', () => {
expect(() => SessionState.create('INVALID' as any)).toThrow('Invalid session state');
});
@@ -183,5 +193,62 @@ describe('SessionState Value Object', () => {
const state = SessionState.create('PAUSED');
expect(state.isTerminal()).toBe(false);
});
it('should return false for AWAITING_CHECKOUT_CONFIRMATION state', () => {
const state = SessionState.create('AWAITING_CHECKOUT_CONFIRMATION');
expect(state.isTerminal()).toBe(false);
});
it('should return true for CANCELLED state', () => {
const state = SessionState.create('CANCELLED');
expect(state.isTerminal()).toBe(true);
});
});
describe('state transitions with new states', () => {
it('should allow transition from IN_PROGRESS to AWAITING_CHECKOUT_CONFIRMATION', () => {
const state = SessionState.create('IN_PROGRESS');
expect(state.canTransitionTo(SessionState.create('AWAITING_CHECKOUT_CONFIRMATION'))).toBe(true);
});
it('should allow transition from AWAITING_CHECKOUT_CONFIRMATION to COMPLETED', () => {
const state = SessionState.create('AWAITING_CHECKOUT_CONFIRMATION');
expect(state.canTransitionTo(SessionState.create('COMPLETED'))).toBe(true);
});
it('should allow transition from AWAITING_CHECKOUT_CONFIRMATION to CANCELLED', () => {
const state = SessionState.create('AWAITING_CHECKOUT_CONFIRMATION');
expect(state.canTransitionTo(SessionState.create('CANCELLED'))).toBe(true);
});
it('should not allow transition from CANCELLED to any other state', () => {
const state = SessionState.create('CANCELLED');
expect(state.canTransitionTo(SessionState.create('IN_PROGRESS'))).toBe(false);
expect(state.canTransitionTo(SessionState.create('COMPLETED'))).toBe(false);
});
});
describe('isAwaitingCheckoutConfirmation', () => {
it('should return true for AWAITING_CHECKOUT_CONFIRMATION state', () => {
const state = SessionState.create('AWAITING_CHECKOUT_CONFIRMATION');
expect(state.isAwaitingCheckoutConfirmation()).toBe(true);
});
it('should return false for IN_PROGRESS state', () => {
const state = SessionState.create('IN_PROGRESS');
expect(state.isAwaitingCheckoutConfirmation()).toBe(false);
});
});
describe('isCancelled', () => {
it('should return true for CANCELLED state', () => {
const state = SessionState.create('CANCELLED');
expect(state.isCancelled()).toBe(true);
});
it('should return false for COMPLETED state', () => {
const state = SessionState.create('COMPLETED');
expect(state.isCancelled()).toBe(false);
});
});
});