refactor to adapters
This commit is contained in:
400
tests/application/CheckAuthenticationUseCase.spec.ts
Normal file
400
tests/application/CheckAuthenticationUseCase.spec.ts
Normal file
@@ -0,0 +1,400 @@
|
||||
import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
|
||||
import { CheckAuthenticationUseCase } from '../../../../core/automation/application/use-cases/CheckAuthenticationUseCase';
|
||||
import { AuthenticationState } from '@gridpilot/automation/domain/value-objects/AuthenticationState';
|
||||
import { BrowserAuthenticationState } from '@gridpilot/automation/domain/value-objects/BrowserAuthenticationState';
|
||||
import { Result } from '../../../../core/shared/result/Result';
|
||||
import type { AuthenticationServicePort } from '../../../../core/automation/application/ports/AuthenticationServicePort';
|
||||
|
||||
interface ISessionValidator {
|
||||
validateSession(): Promise<Result<boolean>>;
|
||||
}
|
||||
|
||||
describe('CheckAuthenticationUseCase', () => {
|
||||
let mockAuthService: {
|
||||
checkSession: Mock;
|
||||
initiateLogin: Mock;
|
||||
clearSession: Mock;
|
||||
getState: Mock;
|
||||
validateServerSide: Mock;
|
||||
refreshSession: Mock;
|
||||
getSessionExpiry: Mock;
|
||||
verifyPageAuthentication: Mock;
|
||||
};
|
||||
let mockSessionValidator: {
|
||||
validateSession: Mock;
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
mockAuthService = {
|
||||
checkSession: vi.fn(),
|
||||
initiateLogin: vi.fn(),
|
||||
clearSession: vi.fn(),
|
||||
getState: vi.fn(),
|
||||
validateServerSide: vi.fn(),
|
||||
refreshSession: vi.fn(),
|
||||
getSessionExpiry: vi.fn(),
|
||||
verifyPageAuthentication: vi.fn(),
|
||||
};
|
||||
|
||||
mockSessionValidator = {
|
||||
validateSession: vi.fn(),
|
||||
};
|
||||
});
|
||||
|
||||
describe('File-based validation only', () => {
|
||||
it('should return AUTHENTICATED when cookies are valid', async () => {
|
||||
const useCase = new CheckAuthenticationUseCase(
|
||||
mockAuthService as unknown as AuthenticationServicePort
|
||||
);
|
||||
|
||||
mockAuthService.checkSession.mockResolvedValue(
|
||||
Result.ok(AuthenticationState.AUTHENTICATED)
|
||||
);
|
||||
mockAuthService.getSessionExpiry.mockResolvedValue(
|
||||
Result.ok(new Date(Date.now() + 3600000))
|
||||
);
|
||||
|
||||
const result = await useCase.execute();
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toBe(AuthenticationState.AUTHENTICATED);
|
||||
expect(mockAuthService.checkSession).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('should return EXPIRED when cookies are expired', async () => {
|
||||
const useCase = new CheckAuthenticationUseCase(
|
||||
mockAuthService as unknown as AuthenticationServicePort
|
||||
);
|
||||
|
||||
mockAuthService.checkSession.mockResolvedValue(
|
||||
Result.ok(AuthenticationState.EXPIRED)
|
||||
);
|
||||
mockAuthService.getSessionExpiry.mockResolvedValue(
|
||||
Result.ok(new Date(Date.now() - 3600000))
|
||||
);
|
||||
|
||||
const result = await useCase.execute();
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toBe(AuthenticationState.EXPIRED);
|
||||
});
|
||||
|
||||
it('should return UNKNOWN when no session exists', async () => {
|
||||
const useCase = new CheckAuthenticationUseCase(
|
||||
mockAuthService as unknown as AuthenticationServicePort
|
||||
);
|
||||
|
||||
mockAuthService.checkSession.mockResolvedValue(
|
||||
Result.ok(AuthenticationState.UNKNOWN)
|
||||
);
|
||||
mockAuthService.getSessionExpiry.mockResolvedValue(
|
||||
Result.ok(null)
|
||||
);
|
||||
|
||||
const result = await useCase.execute();
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toBe(AuthenticationState.UNKNOWN);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Server-side validation enabled', () => {
|
||||
it('should confirm AUTHENTICATED when file and server both validate', async () => {
|
||||
const useCase = new CheckAuthenticationUseCase(
|
||||
mockAuthService as unknown as AuthenticationServicePort,
|
||||
mockSessionValidator as unknown as ISessionValidator
|
||||
);
|
||||
|
||||
mockAuthService.checkSession.mockResolvedValue(
|
||||
Result.ok(AuthenticationState.AUTHENTICATED)
|
||||
);
|
||||
mockAuthService.getSessionExpiry.mockResolvedValue(
|
||||
Result.ok(new Date(Date.now() + 3600000))
|
||||
);
|
||||
mockSessionValidator.validateSession.mockResolvedValue(
|
||||
Result.ok(true)
|
||||
);
|
||||
|
||||
const result = await useCase.execute();
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toBe(AuthenticationState.AUTHENTICATED);
|
||||
expect(mockSessionValidator.validateSession).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('should return EXPIRED when file says valid but server rejects', async () => {
|
||||
const useCase = new CheckAuthenticationUseCase(
|
||||
mockAuthService as unknown as AuthenticationServicePort,
|
||||
mockSessionValidator as unknown as ISessionValidator
|
||||
);
|
||||
|
||||
mockAuthService.checkSession.mockResolvedValue(
|
||||
Result.ok(AuthenticationState.AUTHENTICATED)
|
||||
);
|
||||
mockAuthService.getSessionExpiry.mockResolvedValue(
|
||||
Result.ok(new Date(Date.now() + 3600000))
|
||||
);
|
||||
mockSessionValidator.validateSession.mockResolvedValue(
|
||||
Result.ok(false)
|
||||
);
|
||||
|
||||
const result = await useCase.execute();
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toBe(AuthenticationState.EXPIRED);
|
||||
});
|
||||
|
||||
it('should work without ISessionValidator injected (optional dependency)', async () => {
|
||||
const useCase = new CheckAuthenticationUseCase(
|
||||
mockAuthService as unknown as AuthenticationServicePort
|
||||
);
|
||||
|
||||
mockAuthService.checkSession.mockResolvedValue(
|
||||
Result.ok(AuthenticationState.AUTHENTICATED)
|
||||
);
|
||||
mockAuthService.getSessionExpiry.mockResolvedValue(
|
||||
Result.ok(new Date(Date.now() + 3600000))
|
||||
);
|
||||
|
||||
const result = await useCase.execute();
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toBe(AuthenticationState.AUTHENTICATED);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Error handling', () => {
|
||||
it('should not block file-based result if server validation fails', async () => {
|
||||
const useCase = new CheckAuthenticationUseCase(
|
||||
mockAuthService as unknown as AuthenticationServicePort,
|
||||
mockSessionValidator as unknown as ISessionValidator
|
||||
);
|
||||
|
||||
mockAuthService.checkSession.mockResolvedValue(
|
||||
Result.ok(AuthenticationState.AUTHENTICATED)
|
||||
);
|
||||
mockAuthService.getSessionExpiry.mockResolvedValue(
|
||||
Result.ok(new Date(Date.now() + 3600000))
|
||||
);
|
||||
mockSessionValidator.validateSession.mockResolvedValue(
|
||||
Result.err('Network error')
|
||||
);
|
||||
|
||||
const result = await useCase.execute();
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toBe(AuthenticationState.AUTHENTICATED);
|
||||
});
|
||||
|
||||
it('should handle authentication service errors gracefully', async () => {
|
||||
const useCase = new CheckAuthenticationUseCase(
|
||||
mockAuthService as unknown as AuthenticationServicePort
|
||||
);
|
||||
|
||||
mockAuthService.checkSession.mockResolvedValue(
|
||||
Result.err('File read error')
|
||||
);
|
||||
|
||||
const result = await useCase.execute();
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.unwrapErr()).toContain('File read error');
|
||||
});
|
||||
|
||||
it('should handle session expiry check errors gracefully', async () => {
|
||||
const useCase = new CheckAuthenticationUseCase(
|
||||
mockAuthService as unknown as AuthenticationServicePort
|
||||
);
|
||||
|
||||
mockAuthService.checkSession.mockResolvedValue(
|
||||
Result.ok(AuthenticationState.AUTHENTICATED)
|
||||
);
|
||||
mockAuthService.getSessionExpiry.mockResolvedValue(
|
||||
Result.err('Invalid session format')
|
||||
);
|
||||
|
||||
const result = await useCase.execute();
|
||||
|
||||
// Should not block on expiry check errors, return file-based state
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toBe(AuthenticationState.AUTHENTICATED);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Page content verification', () => {
|
||||
it('should call verifyPageAuthentication when verifyPageContent is true', async () => {
|
||||
const useCase = new CheckAuthenticationUseCase(
|
||||
mockAuthService as unknown as AuthenticationServicePort
|
||||
);
|
||||
|
||||
mockAuthService.checkSession.mockResolvedValue(
|
||||
Result.ok(AuthenticationState.AUTHENTICATED)
|
||||
);
|
||||
mockAuthService.getSessionExpiry.mockResolvedValue(
|
||||
Result.ok(new Date(Date.now() + 3600000))
|
||||
);
|
||||
mockAuthService.verifyPageAuthentication = vi.fn().mockResolvedValue(
|
||||
Result.ok(new BrowserAuthenticationState(true, true))
|
||||
);
|
||||
|
||||
await useCase.execute({ verifyPageContent: true });
|
||||
|
||||
expect(mockAuthService.verifyPageAuthentication).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('should return EXPIRED when cookies valid but page shows login UI', async () => {
|
||||
const useCase = new CheckAuthenticationUseCase(
|
||||
mockAuthService as unknown as AuthenticationServicePort
|
||||
);
|
||||
|
||||
mockAuthService.checkSession.mockResolvedValue(
|
||||
Result.ok(AuthenticationState.AUTHENTICATED)
|
||||
);
|
||||
mockAuthService.getSessionExpiry.mockResolvedValue(
|
||||
Result.ok(new Date(Date.now() + 3600000))
|
||||
);
|
||||
mockAuthService.verifyPageAuthentication = vi.fn().mockResolvedValue(
|
||||
Result.ok(new BrowserAuthenticationState(true, false))
|
||||
);
|
||||
|
||||
const result = await useCase.execute({ verifyPageContent: true });
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toBe(AuthenticationState.EXPIRED);
|
||||
});
|
||||
|
||||
it('should return AUTHENTICATED when both cookies AND page authenticated', async () => {
|
||||
const useCase = new CheckAuthenticationUseCase(
|
||||
mockAuthService as unknown as AuthenticationServicePort
|
||||
);
|
||||
|
||||
mockAuthService.checkSession.mockResolvedValue(
|
||||
Result.ok(AuthenticationState.AUTHENTICATED)
|
||||
);
|
||||
mockAuthService.getSessionExpiry.mockResolvedValue(
|
||||
Result.ok(new Date(Date.now() + 3600000))
|
||||
);
|
||||
mockAuthService.verifyPageAuthentication = vi.fn().mockResolvedValue(
|
||||
Result.ok(new BrowserAuthenticationState(true, true))
|
||||
);
|
||||
|
||||
const result = await useCase.execute({ verifyPageContent: true });
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toBe(AuthenticationState.AUTHENTICATED);
|
||||
});
|
||||
|
||||
it('should default verifyPageContent to false (backward compatible)', async () => {
|
||||
const useCase = new CheckAuthenticationUseCase(
|
||||
mockAuthService as unknown as AuthenticationServicePort
|
||||
);
|
||||
|
||||
mockAuthService.checkSession.mockResolvedValue(
|
||||
Result.ok(AuthenticationState.AUTHENTICATED)
|
||||
);
|
||||
mockAuthService.getSessionExpiry.mockResolvedValue(
|
||||
Result.ok(new Date(Date.now() + 3600000))
|
||||
);
|
||||
mockAuthService.verifyPageAuthentication = vi.fn();
|
||||
|
||||
await useCase.execute();
|
||||
|
||||
expect(mockAuthService.verifyPageAuthentication).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should handle verifyPageAuthentication errors gracefully', async () => {
|
||||
const useCase = new CheckAuthenticationUseCase(
|
||||
mockAuthService as unknown as AuthenticationServicePort
|
||||
);
|
||||
|
||||
mockAuthService.checkSession.mockResolvedValue(
|
||||
Result.ok(AuthenticationState.AUTHENTICATED)
|
||||
);
|
||||
mockAuthService.getSessionExpiry.mockResolvedValue(
|
||||
Result.ok(new Date(Date.now() + 3600000))
|
||||
);
|
||||
mockAuthService.verifyPageAuthentication = vi.fn().mockResolvedValue(
|
||||
Result.err('Page navigation failed')
|
||||
);
|
||||
|
||||
const result = await useCase.execute({ verifyPageContent: true });
|
||||
|
||||
// Should not block on page verification errors, return cookie-based state
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(result.unwrap()).toBe(AuthenticationState.AUTHENTICATED);
|
||||
});
|
||||
});
|
||||
|
||||
describe('BDD Scenarios', () => {
|
||||
it('Given valid session cookies, When checking auth, Then return AUTHENTICATED', async () => {
|
||||
const useCase = new CheckAuthenticationUseCase(
|
||||
mockAuthService as unknown as AuthenticationServicePort
|
||||
);
|
||||
|
||||
mockAuthService.checkSession.mockResolvedValue(
|
||||
Result.ok(AuthenticationState.AUTHENTICATED)
|
||||
);
|
||||
mockAuthService.getSessionExpiry.mockResolvedValue(
|
||||
Result.ok(new Date(Date.now() + 7200000))
|
||||
);
|
||||
|
||||
const result = await useCase.execute();
|
||||
|
||||
expect(result.unwrap()).toBe(AuthenticationState.AUTHENTICATED);
|
||||
});
|
||||
|
||||
it('Given expired session cookies, When checking auth, Then return EXPIRED', async () => {
|
||||
const useCase = new CheckAuthenticationUseCase(
|
||||
mockAuthService as unknown as AuthenticationServicePort
|
||||
);
|
||||
|
||||
mockAuthService.checkSession.mockResolvedValue(
|
||||
Result.ok(AuthenticationState.EXPIRED)
|
||||
);
|
||||
mockAuthService.getSessionExpiry.mockResolvedValue(
|
||||
Result.ok(new Date(Date.now() - 1000))
|
||||
);
|
||||
|
||||
const result = await useCase.execute();
|
||||
|
||||
expect(result.unwrap()).toBe(AuthenticationState.EXPIRED);
|
||||
});
|
||||
|
||||
it('Given no session file, When checking auth, Then return UNKNOWN', async () => {
|
||||
const useCase = new CheckAuthenticationUseCase(
|
||||
mockAuthService as unknown as AuthenticationServicePort
|
||||
);
|
||||
|
||||
mockAuthService.checkSession.mockResolvedValue(
|
||||
Result.ok(AuthenticationState.UNKNOWN)
|
||||
);
|
||||
mockAuthService.getSessionExpiry.mockResolvedValue(
|
||||
Result.ok(null)
|
||||
);
|
||||
|
||||
const result = await useCase.execute();
|
||||
|
||||
expect(result.unwrap()).toBe(AuthenticationState.UNKNOWN);
|
||||
});
|
||||
|
||||
it('Given valid cookies but page shows login, When verifying page content, Then return EXPIRED', async () => {
|
||||
const useCase = new CheckAuthenticationUseCase(
|
||||
mockAuthService as unknown as AuthenticationServicePort
|
||||
);
|
||||
|
||||
mockAuthService.checkSession.mockResolvedValue(
|
||||
Result.ok(AuthenticationState.AUTHENTICATED)
|
||||
);
|
||||
mockAuthService.getSessionExpiry.mockResolvedValue(
|
||||
Result.ok(new Date(Date.now() + 3600000))
|
||||
);
|
||||
mockAuthService.verifyPageAuthentication = vi.fn().mockResolvedValue(
|
||||
Result.ok(new BrowserAuthenticationState(true, false))
|
||||
);
|
||||
|
||||
const result = await useCase.execute({ verifyPageContent: true });
|
||||
|
||||
expect(result.unwrap()).toBe(AuthenticationState.EXPIRED);
|
||||
});
|
||||
});
|
||||
});
|
||||
121
tests/application/CompleteRaceCreationUseCase.spec.ts
Normal file
121
tests/application/CompleteRaceCreationUseCase.spec.ts
Normal file
@@ -0,0 +1,121 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { CompleteRaceCreationUseCase } from '../../../../core/automation/application/use-cases/CompleteRaceCreationUseCase';
|
||||
import { Result } from '../../../../core/shared/result/Result';
|
||||
import { RaceCreationResult } from '@gridpilot/automation/domain/value-objects/RaceCreationResult';
|
||||
import { CheckoutPrice } from '@gridpilot/automation/domain/value-objects/CheckoutPrice';
|
||||
import type { CheckoutServicePort } from '../../../../core/automation/application/ports/CheckoutServicePort';
|
||||
import { CheckoutState } from '@gridpilot/automation/domain/value-objects/CheckoutState';
|
||||
|
||||
describe('CompleteRaceCreationUseCase', () => {
|
||||
let mockCheckoutService: CheckoutServicePort;
|
||||
let useCase: CompleteRaceCreationUseCase;
|
||||
|
||||
beforeEach(() => {
|
||||
mockCheckoutService = {
|
||||
extractCheckoutInfo: vi.fn(),
|
||||
proceedWithCheckout: vi.fn(),
|
||||
};
|
||||
|
||||
useCase = new CompleteRaceCreationUseCase(mockCheckoutService);
|
||||
});
|
||||
|
||||
describe('execute', () => {
|
||||
it('should extract checkout price and create RaceCreationResult', async () => {
|
||||
const price = CheckoutPrice.fromString('$25.50');
|
||||
const state = CheckoutState.ready();
|
||||
const sessionId = 'test-session-123';
|
||||
|
||||
vi.mocked(mockCheckoutService.extractCheckoutInfo).mockResolvedValue(
|
||||
Result.ok({ price, state, buttonHtml: '<a>$25.50</a>' })
|
||||
);
|
||||
|
||||
const result = await useCase.execute(sessionId);
|
||||
|
||||
expect(mockCheckoutService.extractCheckoutInfo).toHaveBeenCalled();
|
||||
expect(result.isOk()).toBe(true);
|
||||
|
||||
const raceCreationResult = result.unwrap();
|
||||
expect(raceCreationResult).toBeInstanceOf(RaceCreationResult);
|
||||
expect(raceCreationResult.sessionId).toBe(sessionId);
|
||||
expect(raceCreationResult.price).toBe('$25.50');
|
||||
expect(raceCreationResult.timestamp).toBeInstanceOf(Date);
|
||||
});
|
||||
|
||||
it('should return error if checkout info extraction fails', async () => {
|
||||
vi.mocked(mockCheckoutService.extractCheckoutInfo).mockResolvedValue(
|
||||
Result.err(new Error('Failed to extract checkout info'))
|
||||
);
|
||||
|
||||
const result = await useCase.execute('test-session-123');
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.unwrapErr().message).toContain('Failed to extract checkout info');
|
||||
});
|
||||
|
||||
it('should return error if price is missing', async () => {
|
||||
const state = CheckoutState.ready();
|
||||
|
||||
vi.mocked(mockCheckoutService.extractCheckoutInfo).mockResolvedValue(
|
||||
Result.ok({ price: null, state, buttonHtml: '<a>n/a</a>' })
|
||||
);
|
||||
|
||||
const result = await useCase.execute('test-session-123');
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.unwrapErr().message).toContain('Could not extract price');
|
||||
});
|
||||
|
||||
it('should validate session ID is provided', async () => {
|
||||
const result = await useCase.execute('');
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.unwrapErr().message).toContain('Session ID is required');
|
||||
});
|
||||
|
||||
it('should format different price values correctly', async () => {
|
||||
const testCases = [
|
||||
{ input: '$10.00', expected: '$10.00' },
|
||||
{ input: '$100.50', expected: '$100.50' },
|
||||
{ input: '$0.99', expected: '$0.99' },
|
||||
];
|
||||
|
||||
for (const testCase of testCases) {
|
||||
const price = CheckoutPrice.fromString(testCase.input);
|
||||
const state = CheckoutState.ready();
|
||||
|
||||
vi.mocked(mockCheckoutService.extractCheckoutInfo).mockResolvedValue(
|
||||
Result.ok({ price, state, buttonHtml: `<a>${testCase.input}</a>` })
|
||||
);
|
||||
|
||||
const result = await useCase.execute('test-session');
|
||||
expect(result.isOk()).toBe(true);
|
||||
|
||||
const raceCreationResult = result.unwrap();
|
||||
expect(raceCreationResult.price).toBe(testCase.expected);
|
||||
}
|
||||
});
|
||||
|
||||
it('should capture current timestamp when creating result', async () => {
|
||||
const price = CheckoutPrice.fromString('$25.50');
|
||||
const state = CheckoutState.ready();
|
||||
const beforeExecution = new Date();
|
||||
|
||||
vi.mocked(mockCheckoutService.extractCheckoutInfo).mockResolvedValue(
|
||||
Result.ok({ price, state, buttonHtml: '<a>$25.50</a>' })
|
||||
);
|
||||
|
||||
const result = await useCase.execute('test-session');
|
||||
const afterExecution = new Date();
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
const raceCreationResult = result.unwrap();
|
||||
|
||||
expect(raceCreationResult.timestamp.getTime()).toBeGreaterThanOrEqual(
|
||||
beforeExecution.getTime()
|
||||
);
|
||||
expect(raceCreationResult.timestamp.getTime()).toBeLessThanOrEqual(
|
||||
afterExecution.getTime()
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
164
tests/application/ConfirmCheckoutUseCase.enhanced.spec.ts
Normal file
164
tests/application/ConfirmCheckoutUseCase.enhanced.spec.ts
Normal file
@@ -0,0 +1,164 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { ConfirmCheckoutUseCase } from '@gridpilot/automation/application/use-cases/ConfirmCheckoutUseCase';
|
||||
import { Result } from '@gridpilot/shared-result';
|
||||
import { CheckoutPrice } from '@gridpilot/automation/domain/value-objects/CheckoutPrice';
|
||||
import { CheckoutState } from '@gridpilot/automation/domain/value-objects/CheckoutState';
|
||||
import { CheckoutConfirmation } from '@gridpilot/automation/domain/value-objects/CheckoutConfirmation';
|
||||
import type { CheckoutServicePort } from '@gridpilot/automation/application/ports/CheckoutServicePort';
|
||||
import type { CheckoutConfirmationPort } from '@gridpilot/automation/application/ports/CheckoutConfirmationPort';
|
||||
|
||||
describe('ConfirmCheckoutUseCase - Enhanced with Confirmation Port', () => {
|
||||
let mockCheckoutService: CheckoutServicePort;
|
||||
let mockConfirmationPort: CheckoutConfirmationPort;
|
||||
let useCase: ConfirmCheckoutUseCase;
|
||||
|
||||
beforeEach(() => {
|
||||
mockCheckoutService = {
|
||||
extractCheckoutInfo: vi.fn(),
|
||||
proceedWithCheckout: vi.fn(),
|
||||
};
|
||||
|
||||
mockConfirmationPort = {
|
||||
requestCheckoutConfirmation: vi.fn(),
|
||||
};
|
||||
|
||||
useCase = new ConfirmCheckoutUseCase(mockCheckoutService, mockConfirmationPort);
|
||||
});
|
||||
|
||||
describe('with new confirmation flow', () => {
|
||||
it('should extract price, request confirmation via port, then proceed', async () => {
|
||||
const price = CheckoutPrice.fromString('$25.50');
|
||||
const state = CheckoutState.ready();
|
||||
|
||||
vi.mocked(mockCheckoutService.extractCheckoutInfo).mockResolvedValue(
|
||||
Result.ok({ price, state, buttonHtml: '' })
|
||||
);
|
||||
|
||||
vi.mocked(mockConfirmationPort.requestCheckoutConfirmation).mockResolvedValue(
|
||||
Result.ok(CheckoutConfirmation.create('confirmed'))
|
||||
);
|
||||
|
||||
vi.mocked(mockCheckoutService.proceedWithCheckout).mockResolvedValue(
|
||||
Result.ok(undefined)
|
||||
);
|
||||
|
||||
const result = await useCase.execute();
|
||||
|
||||
expect(mockCheckoutService.extractCheckoutInfo).toHaveBeenCalled();
|
||||
expect(mockConfirmationPort.requestCheckoutConfirmation).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
price: expect.any(CheckoutPrice),
|
||||
state: expect.any(CheckoutState),
|
||||
})
|
||||
);
|
||||
expect(mockCheckoutService.proceedWithCheckout).toHaveBeenCalled();
|
||||
expect(result.isOk()).toBe(true);
|
||||
});
|
||||
|
||||
it('should not proceed if user cancels confirmation', async () => {
|
||||
const price = CheckoutPrice.fromString('$10.00');
|
||||
const state = CheckoutState.ready();
|
||||
|
||||
vi.mocked(mockCheckoutService.extractCheckoutInfo).mockResolvedValue(
|
||||
Result.ok({ price, state, buttonHtml: '' })
|
||||
);
|
||||
|
||||
vi.mocked(mockConfirmationPort.requestCheckoutConfirmation).mockResolvedValue(
|
||||
Result.ok(CheckoutConfirmation.create('cancelled'))
|
||||
);
|
||||
|
||||
const result = await useCase.execute();
|
||||
|
||||
expect(mockConfirmationPort.requestCheckoutConfirmation).toHaveBeenCalled();
|
||||
expect(mockCheckoutService.proceedWithCheckout).not.toHaveBeenCalled();
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.unwrapErr().message).toContain('cancelled');
|
||||
});
|
||||
|
||||
it('should not proceed if confirmation times out', async () => {
|
||||
const price = CheckoutPrice.fromString('$10.00');
|
||||
const state = CheckoutState.ready();
|
||||
|
||||
vi.mocked(mockCheckoutService.extractCheckoutInfo).mockResolvedValue(
|
||||
Result.ok({ price, state, buttonHtml: '' })
|
||||
);
|
||||
|
||||
vi.mocked(mockConfirmationPort.requestCheckoutConfirmation).mockResolvedValue(
|
||||
Result.ok(CheckoutConfirmation.create('timeout'))
|
||||
);
|
||||
|
||||
const result = await useCase.execute();
|
||||
|
||||
expect(mockConfirmationPort.requestCheckoutConfirmation).toHaveBeenCalled();
|
||||
expect(mockCheckoutService.proceedWithCheckout).not.toHaveBeenCalled();
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.unwrapErr().message).toContain('timeout');
|
||||
});
|
||||
|
||||
it('should fail if confirmation port returns error', async () => {
|
||||
const price = CheckoutPrice.fromString('$10.00');
|
||||
const state = CheckoutState.ready();
|
||||
|
||||
vi.mocked(mockCheckoutService.extractCheckoutInfo).mockResolvedValue(
|
||||
Result.ok({ price, state, buttonHtml: '' })
|
||||
);
|
||||
|
||||
vi.mocked(mockConfirmationPort.requestCheckoutConfirmation).mockResolvedValue(
|
||||
Result.err(new Error('IPC communication failed'))
|
||||
);
|
||||
|
||||
const result = await useCase.execute();
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.unwrapErr().message).toContain('IPC communication failed');
|
||||
});
|
||||
|
||||
it('should still reject insufficient funds before confirmation', async () => {
|
||||
const price = CheckoutPrice.fromString('$10.00');
|
||||
const state = CheckoutState.insufficientFunds();
|
||||
|
||||
vi.mocked(mockCheckoutService.extractCheckoutInfo).mockResolvedValue(
|
||||
Result.ok({ price, state, buttonHtml: '' })
|
||||
);
|
||||
|
||||
const result = await useCase.execute();
|
||||
|
||||
expect(mockConfirmationPort.requestCheckoutConfirmation).not.toHaveBeenCalled();
|
||||
expect(mockCheckoutService.proceedWithCheckout).not.toHaveBeenCalled();
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.unwrapErr().message).toContain('Insufficient funds');
|
||||
});
|
||||
|
||||
it('should pass session metadata to confirmation port', async () => {
|
||||
const price = CheckoutPrice.fromString('$25.50');
|
||||
const state = CheckoutState.ready();
|
||||
const sessionMetadata = {
|
||||
sessionName: 'Test Race',
|
||||
trackId: 'spa',
|
||||
carIds: ['car1', 'car2'],
|
||||
};
|
||||
|
||||
vi.mocked(mockCheckoutService.extractCheckoutInfo).mockResolvedValue(
|
||||
Result.ok({ price, state, buttonHtml: '' })
|
||||
);
|
||||
|
||||
vi.mocked(mockConfirmationPort.requestCheckoutConfirmation).mockResolvedValue(
|
||||
Result.ok(CheckoutConfirmation.create('confirmed'))
|
||||
);
|
||||
|
||||
vi.mocked(mockCheckoutService.proceedWithCheckout).mockResolvedValue(
|
||||
Result.ok(undefined)
|
||||
);
|
||||
|
||||
const result = await useCase.execute(sessionMetadata);
|
||||
|
||||
expect(mockConfirmationPort.requestCheckoutConfirmation).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
sessionMetadata,
|
||||
timeoutMs: expect.any(Number),
|
||||
})
|
||||
);
|
||||
expect(result.isOk()).toBe(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
405
tests/application/ConfirmCheckoutUseCase.spec.ts
Normal file
405
tests/application/ConfirmCheckoutUseCase.spec.ts
Normal file
@@ -0,0 +1,405 @@
|
||||
import { describe, it, expect, beforeEach, vi, Mock } from 'vitest';
|
||||
import { Result } from '../../../../core/shared/result/Result';
|
||||
import { ConfirmCheckoutUseCase } from '../../../../core/automation/application/use-cases/ConfirmCheckoutUseCase';
|
||||
import type { CheckoutServicePort } from '../../../../core/automation/application/ports/CheckoutServicePort';
|
||||
import type { CheckoutConfirmationPort } from '../../../../core/automation/application/ports/CheckoutConfirmationPort';
|
||||
import type { CheckoutInfoDTO } from '../../../../core/automation/application/dto/CheckoutInfoDTO';
|
||||
import { CheckoutPrice } from '@gridpilot/automation/domain/value-objects/CheckoutPrice';
|
||||
import { CheckoutState, CheckoutStateEnum } from '@gridpilot/automation/domain/value-objects/CheckoutState';
|
||||
import { CheckoutConfirmation } from '@gridpilot/automation/domain/value-objects/CheckoutConfirmation';
|
||||
|
||||
/**
|
||||
* ConfirmCheckoutUseCase - GREEN PHASE
|
||||
*
|
||||
* Tests for checkout confirmation flow including price extraction,
|
||||
* insufficient funds detection, and user confirmation.
|
||||
*/
|
||||
|
||||
describe('ConfirmCheckoutUseCase', () => {
|
||||
let mockCheckoutService: {
|
||||
extractCheckoutInfo: Mock;
|
||||
proceedWithCheckout: Mock;
|
||||
};
|
||||
let mockConfirmationPort: {
|
||||
requestCheckoutConfirmation: Mock;
|
||||
};
|
||||
let mockPrice: CheckoutPrice;
|
||||
|
||||
beforeEach(() => {
|
||||
mockCheckoutService = {
|
||||
extractCheckoutInfo: vi.fn(),
|
||||
proceedWithCheckout: vi.fn(),
|
||||
};
|
||||
|
||||
mockConfirmationPort = {
|
||||
requestCheckoutConfirmation: vi.fn(),
|
||||
};
|
||||
|
||||
mockPrice = {
|
||||
getAmount: vi.fn(() => 0.50),
|
||||
toDisplayString: vi.fn(() => '$0.50'),
|
||||
isZero: vi.fn(() => false),
|
||||
} as unknown as CheckoutPrice;
|
||||
});
|
||||
|
||||
describe('Success flow', () => {
|
||||
it('should extract price, get user confirmation, and proceed with checkout', async () => {
|
||||
const useCase = new ConfirmCheckoutUseCase(
|
||||
mockCheckoutService as unknown as CheckoutServicePort,
|
||||
mockConfirmationPort as unknown as CheckoutConfirmationPort
|
||||
);
|
||||
|
||||
mockCheckoutService.extractCheckoutInfo.mockResolvedValue(
|
||||
Result.ok({
|
||||
price: mockPrice,
|
||||
state: CheckoutState.ready(),
|
||||
buttonHtml: '<a class="btn btn-success"><span>$0.50</span></a>',
|
||||
})
|
||||
);
|
||||
mockConfirmationPort.requestCheckoutConfirmation.mockResolvedValue(
|
||||
Result.ok(CheckoutConfirmation.create('confirmed'))
|
||||
);
|
||||
mockCheckoutService.proceedWithCheckout.mockResolvedValue(Result.ok(undefined));
|
||||
|
||||
const result = await useCase.execute();
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(mockCheckoutService.extractCheckoutInfo).toHaveBeenCalledTimes(1);
|
||||
expect(mockConfirmationPort.requestCheckoutConfirmation).toHaveBeenCalledTimes(1);
|
||||
expect(mockConfirmationPort.requestCheckoutConfirmation).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ price: mockPrice })
|
||||
);
|
||||
expect(mockCheckoutService.proceedWithCheckout).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('should include price in confirmation message', async () => {
|
||||
const useCase = new ConfirmCheckoutUseCase(
|
||||
mockCheckoutService as unknown as CheckoutServicePort,
|
||||
mockConfirmationPort as unknown as CheckoutConfirmationPort
|
||||
);
|
||||
|
||||
mockCheckoutService.extractCheckoutInfo.mockResolvedValue(
|
||||
Result.ok({
|
||||
price: mockPrice,
|
||||
state: CheckoutState.ready(),
|
||||
buttonHtml: '<a class="btn btn-success"><span>$0.50</span></a>',
|
||||
})
|
||||
);
|
||||
mockConfirmationPort.requestCheckoutConfirmation.mockResolvedValue(
|
||||
Result.ok(CheckoutConfirmation.create('confirmed'))
|
||||
);
|
||||
mockCheckoutService.proceedWithCheckout.mockResolvedValue(Result.ok(undefined));
|
||||
|
||||
await useCase.execute();
|
||||
|
||||
expect(mockConfirmationPort.requestCheckoutConfirmation).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ price: mockPrice })
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('User cancellation', () => {
|
||||
it('should abort checkout when user cancels confirmation', async () => {
|
||||
const useCase = new ConfirmCheckoutUseCase(
|
||||
mockCheckoutService as unknown as CheckoutServicePort,
|
||||
mockConfirmationPort as unknown as CheckoutConfirmationPort
|
||||
);
|
||||
|
||||
mockCheckoutService.extractCheckoutInfo.mockResolvedValue(
|
||||
Result.ok({
|
||||
price: mockPrice,
|
||||
state: CheckoutState.ready(),
|
||||
buttonHtml: '<a class="btn btn-success"><span>$0.50</span></a>',
|
||||
})
|
||||
);
|
||||
mockConfirmationPort.requestCheckoutConfirmation.mockResolvedValue(
|
||||
Result.ok(CheckoutConfirmation.create('cancelled'))
|
||||
);
|
||||
|
||||
const result = await useCase.execute();
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.unwrapErr().message).toMatch(/cancel/i);
|
||||
expect(mockCheckoutService.proceedWithCheckout).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should not proceed with checkout after cancellation', async () => {
|
||||
const useCase = new ConfirmCheckoutUseCase(
|
||||
mockCheckoutService as unknown as CheckoutServicePort,
|
||||
mockConfirmationPort as unknown as CheckoutConfirmationPort
|
||||
);
|
||||
|
||||
mockCheckoutService.extractCheckoutInfo.mockResolvedValue(
|
||||
Result.ok({
|
||||
price: mockPrice,
|
||||
state: CheckoutState.ready(),
|
||||
buttonHtml: '<a class="btn btn-success"><span>$0.50</span></a>',
|
||||
})
|
||||
);
|
||||
mockConfirmationPort.requestCheckoutConfirmation.mockResolvedValue(
|
||||
Result.ok(CheckoutConfirmation.create('cancelled'))
|
||||
);
|
||||
|
||||
await useCase.execute();
|
||||
|
||||
expect(mockCheckoutService.proceedWithCheckout).toHaveBeenCalledTimes(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Insufficient funds detection', () => {
|
||||
it('should return error when checkout state is INSUFFICIENT_FUNDS', async () => {
|
||||
const useCase = new ConfirmCheckoutUseCase(
|
||||
mockCheckoutService as unknown as CheckoutServicePort,
|
||||
mockConfirmationPort as unknown as CheckoutConfirmationPort
|
||||
);
|
||||
|
||||
mockCheckoutService.extractCheckoutInfo.mockResolvedValue(
|
||||
Result.ok({
|
||||
price: mockPrice,
|
||||
state: CheckoutState.insufficientFunds(),
|
||||
buttonHtml: '<a class="btn btn-default"><span>$0.50</span></a>',
|
||||
})
|
||||
);
|
||||
|
||||
const result = await useCase.execute();
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.unwrapErr().message).toMatch(/insufficient.*funds/i);
|
||||
expect(mockConfirmationPort.requestCheckoutConfirmation).not.toHaveBeenCalled();
|
||||
expect(mockCheckoutService.proceedWithCheckout).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should not ask for confirmation when funds are insufficient', async () => {
|
||||
const useCase = new ConfirmCheckoutUseCase(
|
||||
mockCheckoutService as unknown as CheckoutServicePort,
|
||||
mockConfirmationPort as unknown as CheckoutConfirmationPort
|
||||
);
|
||||
|
||||
mockCheckoutService.extractCheckoutInfo.mockResolvedValue(
|
||||
Result.ok({
|
||||
price: mockPrice,
|
||||
state: CheckoutState.insufficientFunds(),
|
||||
buttonHtml: '<a class="btn btn-default"><span>$0.50</span></a>',
|
||||
})
|
||||
);
|
||||
|
||||
await useCase.execute();
|
||||
|
||||
expect(mockConfirmationPort.requestCheckoutConfirmation).toHaveBeenCalledTimes(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Price extraction failure', () => {
|
||||
it('should return error when price cannot be extracted', async () => {
|
||||
const useCase = new ConfirmCheckoutUseCase(
|
||||
mockCheckoutService as unknown as CheckoutServicePort,
|
||||
mockConfirmationPort as unknown as CheckoutConfirmationPort
|
||||
);
|
||||
|
||||
mockCheckoutService.extractCheckoutInfo.mockResolvedValue(
|
||||
Result.ok({
|
||||
price: null,
|
||||
state: CheckoutState.unknown(),
|
||||
buttonHtml: '',
|
||||
})
|
||||
);
|
||||
|
||||
const result = await useCase.execute();
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.unwrapErr().message).toMatch(/extract|price|not found/i);
|
||||
expect(mockConfirmationPort.requestCheckoutConfirmation).not.toHaveBeenCalled();
|
||||
expect(mockCheckoutService.proceedWithCheckout).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should return error when extraction service fails', async () => {
|
||||
const useCase = new ConfirmCheckoutUseCase(
|
||||
mockCheckoutService as unknown as CheckoutServicePort,
|
||||
mockConfirmationPort as unknown as CheckoutConfirmationPort
|
||||
);
|
||||
|
||||
mockCheckoutService.extractCheckoutInfo.mockResolvedValue(
|
||||
Result.err('Button not found')
|
||||
);
|
||||
|
||||
const result = await useCase.execute();
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(mockConfirmationPort.requestCheckoutConfirmation).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('Zero price warning', () => {
|
||||
it('should still require confirmation for $0.00 price', async () => {
|
||||
const zeroPriceMock = {
|
||||
getAmount: vi.fn(() => 0.00),
|
||||
toDisplayString: vi.fn(() => '$0.00'),
|
||||
isZero: vi.fn(() => true),
|
||||
} as unknown as CheckoutPrice;
|
||||
|
||||
const useCase = new ConfirmCheckoutUseCase(
|
||||
mockCheckoutService as unknown as CheckoutServicePort,
|
||||
mockConfirmationPort as unknown as CheckoutConfirmationPort
|
||||
);
|
||||
|
||||
mockCheckoutService.extractCheckoutInfo.mockResolvedValue(
|
||||
Result.ok({
|
||||
price: zeroPriceMock,
|
||||
state: CheckoutState.ready(),
|
||||
buttonHtml: '<a class="btn btn-success"><span>$0.00</span></a>',
|
||||
})
|
||||
);
|
||||
mockConfirmationPort.requestCheckoutConfirmation.mockResolvedValue(
|
||||
Result.ok(CheckoutConfirmation.create('confirmed'))
|
||||
);
|
||||
mockCheckoutService.proceedWithCheckout.mockResolvedValue(Result.ok(undefined));
|
||||
|
||||
const result = await useCase.execute();
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(mockConfirmationPort.requestCheckoutConfirmation).toHaveBeenCalledTimes(1);
|
||||
expect(mockConfirmationPort.requestCheckoutConfirmation).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ price: zeroPriceMock })
|
||||
);
|
||||
});
|
||||
|
||||
it('should proceed with checkout for zero price after confirmation', async () => {
|
||||
const zeroPriceMock = {
|
||||
getAmount: vi.fn(() => 0.00),
|
||||
toDisplayString: vi.fn(() => '$0.00'),
|
||||
isZero: vi.fn(() => true),
|
||||
} as unknown as CheckoutPrice;
|
||||
|
||||
const useCase = new ConfirmCheckoutUseCase(
|
||||
mockCheckoutService as unknown as CheckoutServicePort,
|
||||
mockConfirmationPort as unknown as CheckoutConfirmationPort
|
||||
);
|
||||
|
||||
mockCheckoutService.extractCheckoutInfo.mockResolvedValue(
|
||||
Result.ok({
|
||||
price: zeroPriceMock,
|
||||
state: CheckoutState.ready(),
|
||||
buttonHtml: '<a class="btn btn-success"><span>$0.00</span></a>',
|
||||
})
|
||||
);
|
||||
mockConfirmationPort.requestCheckoutConfirmation.mockResolvedValue(
|
||||
Result.ok(CheckoutConfirmation.create('confirmed'))
|
||||
);
|
||||
mockCheckoutService.proceedWithCheckout.mockResolvedValue(Result.ok(undefined));
|
||||
|
||||
await useCase.execute();
|
||||
|
||||
expect(mockCheckoutService.proceedWithCheckout).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Checkout execution failure', () => {
|
||||
it('should return error when proceedWithCheckout fails', async () => {
|
||||
const useCase = new ConfirmCheckoutUseCase(
|
||||
mockCheckoutService as unknown as CheckoutServicePort,
|
||||
mockConfirmationPort as unknown as CheckoutConfirmationPort
|
||||
);
|
||||
|
||||
mockCheckoutService.extractCheckoutInfo.mockResolvedValue(
|
||||
Result.ok({
|
||||
price: mockPrice,
|
||||
state: CheckoutState.ready(),
|
||||
buttonHtml: '<a class="btn btn-success"><span>$0.50</span></a>',
|
||||
})
|
||||
);
|
||||
mockConfirmationPort.requestCheckoutConfirmation.mockResolvedValue(
|
||||
Result.ok(CheckoutConfirmation.create('confirmed'))
|
||||
);
|
||||
mockCheckoutService.proceedWithCheckout.mockResolvedValue(
|
||||
Result.err('Network error')
|
||||
);
|
||||
|
||||
const result = await useCase.execute();
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.unwrapErr()).toContain('Network error');
|
||||
});
|
||||
});
|
||||
|
||||
describe('BDD Scenarios', () => {
|
||||
it('Given checkout price $0.50 and READY state, When user confirms, Then checkout proceeds', async () => {
|
||||
const useCase = new ConfirmCheckoutUseCase(
|
||||
mockCheckoutService as unknown as CheckoutServicePort,
|
||||
mockConfirmationPort as unknown as CheckoutConfirmationPort
|
||||
);
|
||||
|
||||
mockCheckoutService.extractCheckoutInfo.mockResolvedValue(
|
||||
Result.ok({
|
||||
price: mockPrice,
|
||||
state: CheckoutState.ready(),
|
||||
buttonHtml: '<a class="btn btn-success"><span>$0.50</span></a>',
|
||||
})
|
||||
);
|
||||
mockConfirmationPort.requestCheckoutConfirmation.mockResolvedValue(
|
||||
Result.ok(CheckoutConfirmation.create('confirmed'))
|
||||
);
|
||||
mockCheckoutService.proceedWithCheckout.mockResolvedValue(Result.ok(undefined));
|
||||
|
||||
const result = await useCase.execute();
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
});
|
||||
|
||||
it('Given checkout price $0.50, When user cancels, Then checkout is aborted', async () => {
|
||||
const useCase = new ConfirmCheckoutUseCase(
|
||||
mockCheckoutService as unknown as CheckoutServicePort,
|
||||
mockConfirmationPort as unknown as CheckoutConfirmationPort
|
||||
);
|
||||
|
||||
mockCheckoutService.extractCheckoutInfo.mockResolvedValue(
|
||||
Result.ok({
|
||||
price: mockPrice,
|
||||
state: CheckoutState.ready(),
|
||||
buttonHtml: '<a class="btn btn-success"><span>$0.50</span></a>',
|
||||
})
|
||||
);
|
||||
mockConfirmationPort.requestCheckoutConfirmation.mockResolvedValue(
|
||||
Result.ok(CheckoutConfirmation.create('cancelled'))
|
||||
);
|
||||
|
||||
const result = await useCase.execute();
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(mockCheckoutService.proceedWithCheckout).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('Given INSUFFICIENT_FUNDS state, When executing, Then error is returned', async () => {
|
||||
const useCase = new ConfirmCheckoutUseCase(
|
||||
mockCheckoutService as unknown as CheckoutServicePort,
|
||||
mockConfirmationPort as unknown as CheckoutConfirmationPort
|
||||
);
|
||||
|
||||
mockCheckoutService.extractCheckoutInfo.mockResolvedValue(
|
||||
Result.ok({
|
||||
price: mockPrice,
|
||||
state: CheckoutState.insufficientFunds(),
|
||||
buttonHtml: '<a class="btn btn-default"><span>$0.50</span></a>',
|
||||
})
|
||||
);
|
||||
|
||||
const result = await useCase.execute();
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
});
|
||||
|
||||
it('Given price extraction failure, When executing, Then error is returned', async () => {
|
||||
const useCase = new ConfirmCheckoutUseCase(
|
||||
mockCheckoutService as unknown as CheckoutServicePort,
|
||||
mockConfirmationPort as unknown as CheckoutConfirmationPort
|
||||
);
|
||||
|
||||
mockCheckoutService.extractCheckoutInfo.mockResolvedValue(
|
||||
Result.err('Button not found')
|
||||
);
|
||||
|
||||
const result = await useCase.execute();
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
180
tests/application/ICheckoutConfirmationPort.spec.ts
Normal file
180
tests/application/ICheckoutConfirmationPort.spec.ts
Normal file
@@ -0,0 +1,180 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { Result } from '@gridpilot/shared-result';
|
||||
import { CheckoutConfirmation } from '@gridpilot/automation/domain/value-objects/CheckoutConfirmation';
|
||||
import { CheckoutPrice } from '@gridpilot/automation/domain/value-objects/CheckoutPrice';
|
||||
import { CheckoutState } from '@gridpilot/automation/domain/value-objects/CheckoutState';
|
||||
|
||||
/**
|
||||
* Contract tests for ICheckoutConfirmationPort
|
||||
*
|
||||
* Any implementation must:
|
||||
* 1. Accept CheckoutConfirmationRequest with price, state, sessionMetadata, timeoutMs
|
||||
* 2. Return Result<CheckoutConfirmation> with decision: confirmed, cancelled, or timeout
|
||||
* 3. Handle timeout gracefully by returning timeout decision
|
||||
* 4. Validate request parameters before processing
|
||||
*/
|
||||
|
||||
export interface CheckoutConfirmationRequest {
|
||||
price: CheckoutPrice;
|
||||
state: CheckoutState;
|
||||
sessionMetadata: {
|
||||
sessionName: string;
|
||||
trackId: string;
|
||||
carIds: string[];
|
||||
};
|
||||
timeoutMs: number;
|
||||
}
|
||||
|
||||
export interface ICheckoutConfirmationPort {
|
||||
requestCheckoutConfirmation(
|
||||
request: CheckoutConfirmationRequest
|
||||
): Promise<Result<CheckoutConfirmation>>;
|
||||
}
|
||||
|
||||
describe('ICheckoutConfirmationPort contract', () => {
|
||||
it('should define the required interface structure', () => {
|
||||
// This test verifies the port interface contract exists
|
||||
const mockPort: ICheckoutConfirmationPort = {
|
||||
requestCheckoutConfirmation: async (_request: CheckoutConfirmationRequest) => {
|
||||
return Result.ok(CheckoutConfirmation.create('confirmed'));
|
||||
},
|
||||
};
|
||||
|
||||
expect(mockPort.requestCheckoutConfirmation).toBeDefined();
|
||||
expect(typeof mockPort.requestCheckoutConfirmation).toBe('function');
|
||||
});
|
||||
|
||||
it('should accept valid CheckoutConfirmationRequest', async () => {
|
||||
const mockPort: ICheckoutConfirmationPort = {
|
||||
requestCheckoutConfirmation: async (request: CheckoutConfirmationRequest) => {
|
||||
expect(request.price).toBeInstanceOf(CheckoutPrice);
|
||||
expect(request.state).toBeInstanceOf(CheckoutState);
|
||||
expect(request.sessionMetadata).toBeDefined();
|
||||
expect(request.sessionMetadata.sessionName).toBeTruthy();
|
||||
expect(request.sessionMetadata.trackId).toBeTruthy();
|
||||
expect(Array.isArray(request.sessionMetadata.carIds)).toBe(true);
|
||||
expect(request.timeoutMs).toBeGreaterThan(0);
|
||||
return Result.ok(CheckoutConfirmation.create('confirmed'));
|
||||
},
|
||||
};
|
||||
|
||||
const request: CheckoutConfirmationRequest = {
|
||||
price: CheckoutPrice.fromString('$10.00'),
|
||||
state: CheckoutState.ready(),
|
||||
sessionMetadata: {
|
||||
sessionName: 'Test Session',
|
||||
trackId: 'spa',
|
||||
carIds: ['car1', 'car2'],
|
||||
},
|
||||
timeoutMs: 30000,
|
||||
};
|
||||
|
||||
const result = await mockPort.requestCheckoutConfirmation(request);
|
||||
expect(result.isOk()).toBe(true);
|
||||
});
|
||||
|
||||
it('should return Result with CheckoutConfirmation on success', async () => {
|
||||
const mockPort: ICheckoutConfirmationPort = {
|
||||
requestCheckoutConfirmation: async () => {
|
||||
return Result.ok(CheckoutConfirmation.create('confirmed'));
|
||||
},
|
||||
};
|
||||
|
||||
const request: CheckoutConfirmationRequest = {
|
||||
price: CheckoutPrice.fromString('$10.00'),
|
||||
state: CheckoutState.ready(),
|
||||
sessionMetadata: {
|
||||
sessionName: 'Test Session',
|
||||
trackId: 'spa',
|
||||
carIds: ['car1'],
|
||||
},
|
||||
timeoutMs: 30000,
|
||||
};
|
||||
|
||||
const result = await mockPort.requestCheckoutConfirmation(request);
|
||||
expect(result.isOk()).toBe(true);
|
||||
|
||||
const confirmation = result.unwrap();
|
||||
expect(confirmation).toBeInstanceOf(CheckoutConfirmation);
|
||||
expect(confirmation.isConfirmed()).toBe(true);
|
||||
});
|
||||
|
||||
it('should support cancelled decision', async () => {
|
||||
const mockPort: ICheckoutConfirmationPort = {
|
||||
requestCheckoutConfirmation: async () => {
|
||||
return Result.ok(CheckoutConfirmation.create('cancelled'));
|
||||
},
|
||||
};
|
||||
|
||||
const request: CheckoutConfirmationRequest = {
|
||||
price: CheckoutPrice.fromString('$10.00'),
|
||||
state: CheckoutState.ready(),
|
||||
sessionMetadata: {
|
||||
sessionName: 'Test Session',
|
||||
trackId: 'spa',
|
||||
carIds: ['car1'],
|
||||
},
|
||||
timeoutMs: 30000,
|
||||
};
|
||||
|
||||
const result = await mockPort.requestCheckoutConfirmation(request);
|
||||
expect(result.isOk()).toBe(true);
|
||||
|
||||
const confirmation = result.unwrap();
|
||||
expect(confirmation.isCancelled()).toBe(true);
|
||||
});
|
||||
|
||||
it('should support timeout decision', async () => {
|
||||
const mockPort: ICheckoutConfirmationPort = {
|
||||
requestCheckoutConfirmation: async () => {
|
||||
return Result.ok(CheckoutConfirmation.create('timeout'));
|
||||
},
|
||||
};
|
||||
|
||||
const request: CheckoutConfirmationRequest = {
|
||||
price: CheckoutPrice.fromString('$10.00'),
|
||||
state: CheckoutState.ready(),
|
||||
sessionMetadata: {
|
||||
sessionName: 'Test Session',
|
||||
trackId: 'spa',
|
||||
carIds: ['car1'],
|
||||
},
|
||||
timeoutMs: 1000,
|
||||
};
|
||||
|
||||
const result = await mockPort.requestCheckoutConfirmation(request);
|
||||
expect(result.isOk()).toBe(true);
|
||||
|
||||
const confirmation = result.unwrap();
|
||||
expect(confirmation.isTimeout()).toBe(true);
|
||||
});
|
||||
|
||||
it('should return error Result for invalid requests', async () => {
|
||||
const mockPort: ICheckoutConfirmationPort = {
|
||||
requestCheckoutConfirmation: async (request: CheckoutConfirmationRequest) => {
|
||||
if (request.timeoutMs <= 0) {
|
||||
return Result.err(new Error('Timeout must be positive'));
|
||||
}
|
||||
if (!request.sessionMetadata.sessionName) {
|
||||
return Result.err(new Error('Session name is required'));
|
||||
}
|
||||
return Result.ok(CheckoutConfirmation.create('confirmed'));
|
||||
},
|
||||
};
|
||||
|
||||
const invalidRequest: CheckoutConfirmationRequest = {
|
||||
price: CheckoutPrice.fromString('$10.00'),
|
||||
state: CheckoutState.ready(),
|
||||
sessionMetadata: {
|
||||
sessionName: '',
|
||||
trackId: 'spa',
|
||||
carIds: ['car1'],
|
||||
},
|
||||
timeoutMs: 30000,
|
||||
};
|
||||
|
||||
const result = await mockPort.requestCheckoutConfirmation(invalidRequest);
|
||||
expect(result.isErr()).toBe(true);
|
||||
expect(result.unwrapErr().message).toContain('Session name');
|
||||
});
|
||||
});
|
||||
52
tests/application/OverlaySyncService.spec.ts
Normal file
52
tests/application/OverlaySyncService.spec.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
import { describe, expect, test } from 'vitest'
|
||||
import { OverlayAction, ActionAck } from '../../../../core/automation/application/ports/IOverlaySyncPort'
|
||||
import { IAutomationEventPublisher, AutomationEvent } from '../../../../core/automation/application/ports/IAutomationEventPublisher'
|
||||
import { IAutomationLifecycleEmitter, LifecycleCallback } from '../../../../core/automation/infrastructure/adapters/IAutomationLifecycleEmitter'
|
||||
import { OverlaySyncService } from '../../../../core/automation/application/services/OverlaySyncService'
|
||||
|
||||
class MockLifecycleEmitter implements IAutomationLifecycleEmitter {
|
||||
private callbacks: Set<LifecycleCallback> = new Set()
|
||||
onLifecycle(cb: LifecycleCallback): void {
|
||||
this.callbacks.add(cb)
|
||||
}
|
||||
offLifecycle(cb: LifecycleCallback): void {
|
||||
this.callbacks.delete(cb)
|
||||
}
|
||||
async emit(event: AutomationEvent) {
|
||||
for (const cb of Array.from(this.callbacks)) {
|
||||
// fire without awaiting to simulate async emitter
|
||||
cb(event)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
describe('OverlaySyncService (unit)', () => {
|
||||
test('startAction resolves as confirmed only after action-started event is emitted', async () => {
|
||||
const emitter = new MockLifecycleEmitter()
|
||||
// create service wiring: pass emitter as dependency (constructor shape expected)
|
||||
const svc = new OverlaySyncService({
|
||||
lifecycleEmitter: emitter,
|
||||
logger: console as any,
|
||||
publisher: { publish: async () => {} },
|
||||
})
|
||||
|
||||
const action: OverlayAction = { id: 'add-car', label: 'Adding...' }
|
||||
|
||||
// start the action but don't emit event yet
|
||||
const promise = svc.startAction(action)
|
||||
|
||||
// wait a small tick to ensure promise hasn't resolved prematurely
|
||||
await new Promise((r) => setTimeout(r, 10))
|
||||
|
||||
let resolved = false
|
||||
promise.then(() => (resolved = true))
|
||||
expect(resolved).toBe(false)
|
||||
|
||||
// now emit action-started
|
||||
await emitter.emit({ type: 'action-started', actionId: 'add-car', timestamp: Date.now() })
|
||||
|
||||
const ack = await promise
|
||||
expect(ack.status).toBe('confirmed')
|
||||
expect(ack.id).toBe('add-car')
|
||||
})
|
||||
})
|
||||
40
tests/application/OverlaySyncService.timeout.spec.ts
Normal file
40
tests/application/OverlaySyncService.timeout.spec.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import { describe, expect, test } from 'vitest'
|
||||
import { OverlayAction } from '../../../../core/automation/application/ports/OverlaySyncPort'
|
||||
import { IAutomationLifecycleEmitter, LifecycleCallback } from '../../../../core/automation/infrastructure/adapters/IAutomationLifecycleEmitter'
|
||||
import { OverlaySyncService } from '../../../../core/automation/application/services/OverlaySyncService'
|
||||
|
||||
class MockLifecycleEmitter implements IAutomationLifecycleEmitter {
|
||||
private callbacks: Set<LifecycleCallback> = new Set()
|
||||
onLifecycle(cb: LifecycleCallback): void {
|
||||
this.callbacks.add(cb)
|
||||
}
|
||||
offLifecycle(cb: LifecycleCallback): void {
|
||||
this.callbacks.delete(cb)
|
||||
}
|
||||
async emit(event: { type: 'panel-attached'|'modal-opened'|'action-started'|'action-complete'|'action-failed'|'panel-missing'; actionId: string; timestamp: number }) {
|
||||
for (const cb of Array.from(this.callbacks)) {
|
||||
cb(event)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
describe('OverlaySyncService timeout (unit)', () => {
|
||||
test('startAction with short timeout resolves as tentative when no events', async () => {
|
||||
const emitter = new MockLifecycleEmitter()
|
||||
const svc = new OverlaySyncService({
|
||||
lifecycleEmitter: emitter,
|
||||
logger: console as any,
|
||||
publisher: { publish: async () => {} },
|
||||
})
|
||||
|
||||
const action: OverlayAction = { id: 'add-car', label: 'Adding...', timeoutMs: 50 }
|
||||
|
||||
const start = Date.now()
|
||||
const ack = await svc.startAction(action)
|
||||
const elapsed = Date.now() - start
|
||||
|
||||
expect(ack.status).toBe('tentative')
|
||||
expect(ack.id).toBe('add-car')
|
||||
expect(elapsed).toBeGreaterThanOrEqual(40)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,549 @@
|
||||
import { describe, it, expect, beforeEach } from 'vitest';
|
||||
|
||||
import { RecalculateChampionshipStandingsUseCase } from '@gridpilot/racing/application/use-cases/RecalculateChampionshipStandingsUseCase';
|
||||
import type { ISeasonRepository } from '@gridpilot/racing/domain/repositories/ISeasonRepository';
|
||||
import type { ILeagueScoringConfigRepository } from '@gridpilot/racing/domain/repositories/ILeagueScoringConfigRepository';
|
||||
import type { IRaceRepository } from '@gridpilot/racing/domain/repositories/IRaceRepository';
|
||||
import type { IResultRepository } from '@gridpilot/racing/domain/repositories/IResultRepository';
|
||||
import type { IPenaltyRepository } from '@gridpilot/racing/domain/repositories/IPenaltyRepository';
|
||||
import type { IChampionshipStandingRepository } from '@gridpilot/racing/domain/repositories/IChampionshipStandingRepository';
|
||||
|
||||
import { Season } from '@gridpilot/racing/domain/entities/Season';
|
||||
import type { LeagueScoringConfig } from '@gridpilot/racing/domain/entities/LeagueScoringConfig';
|
||||
import { Race } from '@gridpilot/racing/domain/entities/Race';
|
||||
import { Result } from '@gridpilot/racing/domain/entities/Result';
|
||||
import type { Penalty } from '@gridpilot/racing/domain/entities/Penalty';
|
||||
import type { ChampionshipStanding } from '@gridpilot/racing/domain/entities/ChampionshipStanding';
|
||||
import type { ChampionshipConfig } from '@gridpilot/racing/domain/types/ChampionshipConfig';
|
||||
import { EventScoringService } from '@gridpilot/racing/domain/services/EventScoringService';
|
||||
import { DropScoreApplier } from '@gridpilot/racing/domain/services/DropScoreApplier';
|
||||
import { ChampionshipAggregator } from '@gridpilot/racing/domain/services/ChampionshipAggregator';
|
||||
import { PointsTable } from '@gridpilot/racing/domain/value-objects/PointsTable';
|
||||
import type { SessionType } from '@gridpilot/racing/domain/types/SessionType';
|
||||
import type { BonusRule } from '@gridpilot/racing/domain/types/BonusRule';
|
||||
import type { DropScorePolicy } from '@gridpilot/racing/domain/types/DropScorePolicy';
|
||||
|
||||
class InMemorySeasonRepository implements ISeasonRepository {
|
||||
private seasons: Season[] = [];
|
||||
|
||||
async findById(id: string): Promise<Season | null> {
|
||||
return this.seasons.find((s) => s.id === id) || null;
|
||||
}
|
||||
|
||||
async findByLeagueId(leagueId: string): Promise<Season[]> {
|
||||
return this.seasons.filter((s) => s.leagueId === leagueId);
|
||||
}
|
||||
|
||||
async create(season: Season): Promise<Season> {
|
||||
this.seasons.push(season);
|
||||
return season;
|
||||
}
|
||||
|
||||
async add(season: Season): Promise<void> {
|
||||
this.seasons.push(season);
|
||||
}
|
||||
|
||||
async update(season: Season): Promise<void> {
|
||||
const index = this.seasons.findIndex((s) => s.id === season.id);
|
||||
if (index >= 0) {
|
||||
this.seasons[index] = season;
|
||||
}
|
||||
}
|
||||
|
||||
async listByLeague(leagueId: string): Promise<Season[]> {
|
||||
return this.seasons.filter((s) => s.leagueId === leagueId);
|
||||
}
|
||||
|
||||
async listActiveByLeague(leagueId: string): Promise<Season[]> {
|
||||
return this.seasons.filter((s) => s.leagueId === leagueId && s.status === 'active');
|
||||
}
|
||||
|
||||
seedSeason(season: Season): void {
|
||||
this.seasons.push(season);
|
||||
}
|
||||
}
|
||||
|
||||
class InMemoryLeagueScoringConfigRepository implements ILeagueScoringConfigRepository {
|
||||
private configs: LeagueScoringConfig[] = [];
|
||||
|
||||
async findBySeasonId(seasonId: string): Promise<LeagueScoringConfig | null> {
|
||||
return this.configs.find((c) => c.seasonId === seasonId) || null;
|
||||
}
|
||||
|
||||
async save(config: LeagueScoringConfig): Promise<LeagueScoringConfig> {
|
||||
const index = this.configs.findIndex((c) => c.id === config.id);
|
||||
if (index >= 0) {
|
||||
this.configs[index] = config;
|
||||
} else {
|
||||
this.configs.push(config);
|
||||
}
|
||||
return config;
|
||||
}
|
||||
|
||||
seedConfig(config: LeagueScoringConfig): void {
|
||||
this.configs.push(config);
|
||||
}
|
||||
}
|
||||
|
||||
class InMemoryRaceRepository implements IRaceRepository {
|
||||
private races: Race[] = [];
|
||||
|
||||
async findById(id: string): Promise<Race | null> {
|
||||
return this.races.find((r) => r.id === id) || null;
|
||||
}
|
||||
|
||||
async findAll(): Promise<Race[]> {
|
||||
return [...this.races];
|
||||
}
|
||||
|
||||
async findByLeagueId(leagueId: string): Promise<Race[]> {
|
||||
return this.races.filter((r) => r.leagueId === leagueId);
|
||||
}
|
||||
|
||||
async findUpcomingByLeagueId(): Promise<Race[]> {
|
||||
return [];
|
||||
}
|
||||
|
||||
async findCompletedByLeagueId(): Promise<Race[]> {
|
||||
return [];
|
||||
}
|
||||
|
||||
async findByStatus(): Promise<Race[]> {
|
||||
return [];
|
||||
}
|
||||
|
||||
async findByDateRange(): Promise<Race[]> {
|
||||
return [];
|
||||
}
|
||||
|
||||
async create(race: Race): Promise<Race> {
|
||||
this.races.push(race);
|
||||
return race;
|
||||
}
|
||||
|
||||
async update(race: Race): Promise<Race> {
|
||||
const index = this.races.findIndex((r) => r.id === race.id);
|
||||
if (index >= 0) {
|
||||
this.races[index] = race;
|
||||
} else {
|
||||
this.races.push(race);
|
||||
}
|
||||
return race;
|
||||
}
|
||||
|
||||
async delete(id: string): Promise<void> {
|
||||
this.races = this.races.filter((r) => r.id !== id);
|
||||
}
|
||||
|
||||
async exists(id: string): Promise<boolean> {
|
||||
return this.races.some((r) => r.id === id);
|
||||
}
|
||||
|
||||
seedRace(race: Race): void {
|
||||
this.races.push(race);
|
||||
}
|
||||
}
|
||||
|
||||
class InMemoryResultRepository implements IResultRepository {
|
||||
private results: Result[] = [];
|
||||
|
||||
async findById(id: string): Promise<Result | null> {
|
||||
return this.results.find((r) => r.id === id) || null;
|
||||
}
|
||||
|
||||
async findAll(): Promise<Result[]> {
|
||||
return [...this.results];
|
||||
}
|
||||
|
||||
async findByRaceId(raceId: string): Promise<Result[]> {
|
||||
return this.results.filter((r) => r.raceId === raceId);
|
||||
}
|
||||
|
||||
async findByDriverId(driverId: string): Promise<Result[]> {
|
||||
return this.results.filter((r) => r.driverId === driverId);
|
||||
}
|
||||
|
||||
async findByDriverIdAndLeagueId(driverId: string, leagueId: string): Promise<Result[]> {
|
||||
return this.results.filter((r) => r.driverId === driverId && r.raceId.startsWith(leagueId));
|
||||
}
|
||||
|
||||
async create(result: Result): Promise<Result> {
|
||||
this.results.push(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
async createMany(results: Result[]): Promise<Result[]> {
|
||||
this.results.push(...results);
|
||||
return results;
|
||||
}
|
||||
|
||||
async update(result: Result): Promise<Result> {
|
||||
const index = this.results.findIndex((r) => r.id === result.id);
|
||||
if (index >= 0) {
|
||||
this.results[index] = result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
async delete(id: string): Promise<void> {
|
||||
this.results = this.results.filter((r) => r.id !== id);
|
||||
}
|
||||
|
||||
async deleteByRaceId(raceId: string): Promise<void> {
|
||||
this.results = this.results.filter((r) => r.raceId !== raceId);
|
||||
}
|
||||
|
||||
async exists(id: string): Promise<boolean> {
|
||||
return this.results.some((r) => r.id === id);
|
||||
}
|
||||
|
||||
async existsByRaceId(raceId: string): Promise<boolean> {
|
||||
return this.results.some((r) => r.raceId === raceId);
|
||||
}
|
||||
|
||||
seedResult(result: Result): void {
|
||||
this.results.push(result);
|
||||
}
|
||||
}
|
||||
|
||||
class InMemoryPenaltyRepository implements IPenaltyRepository {
|
||||
private penalties: Penalty[] = [];
|
||||
|
||||
async findByRaceId(raceId: string): Promise<Penalty[]> {
|
||||
return this.penalties.filter((p) => p.raceId === raceId);
|
||||
}
|
||||
|
||||
async findByLeagueId(leagueId: string): Promise<Penalty[]> {
|
||||
return this.penalties.filter((p) => p.leagueId === leagueId);
|
||||
}
|
||||
|
||||
async findByLeagueIdAndDriverId(
|
||||
leagueId: string,
|
||||
driverId: string,
|
||||
): Promise<Penalty[]> {
|
||||
return this.penalties.filter(
|
||||
(p) => p.leagueId === leagueId && p.driverId === driverId,
|
||||
);
|
||||
}
|
||||
|
||||
async findAll(): Promise<Penalty[]> {
|
||||
return [...this.penalties];
|
||||
}
|
||||
|
||||
async findById(id: string): Promise<Penalty | null> {
|
||||
return this.penalties.find((p) => p.id === id) || null;
|
||||
}
|
||||
|
||||
async findByDriverId(driverId: string): Promise<Penalty[]> {
|
||||
return this.penalties.filter((p) => p.driverId === driverId);
|
||||
}
|
||||
|
||||
async findByProtestId(protestId: string): Promise<Penalty[]> {
|
||||
return this.penalties.filter((p) => p.protestId === protestId);
|
||||
}
|
||||
|
||||
async findPending(): Promise<Penalty[]> {
|
||||
return this.penalties.filter((p) => p.status === 'pending');
|
||||
}
|
||||
|
||||
async findIssuedBy(stewardId: string): Promise<Penalty[]> {
|
||||
return this.penalties.filter((p) => p.issuedBy === stewardId);
|
||||
}
|
||||
|
||||
async create(penalty: Penalty): Promise<void> {
|
||||
this.penalties.push(penalty);
|
||||
}
|
||||
|
||||
async update(penalty: Penalty): Promise<void> {
|
||||
const index = this.penalties.findIndex((p) => p.id === penalty.id);
|
||||
if (index >= 0) {
|
||||
this.penalties[index] = penalty;
|
||||
}
|
||||
}
|
||||
|
||||
async exists(id: string): Promise<boolean> {
|
||||
return this.penalties.some((p) => p.id === id);
|
||||
}
|
||||
|
||||
seedPenalty(penalty: Penalty): void {
|
||||
this.penalties.push(penalty);
|
||||
}
|
||||
}
|
||||
|
||||
class InMemoryChampionshipStandingRepository implements IChampionshipStandingRepository {
|
||||
private standings: ChampionshipStanding[] = [];
|
||||
|
||||
async findBySeasonAndChampionship(
|
||||
seasonId: string,
|
||||
championshipId: string,
|
||||
): Promise<ChampionshipStanding[]> {
|
||||
return this.standings.filter(
|
||||
(s) => s.seasonId === seasonId && s.championshipId === championshipId,
|
||||
);
|
||||
}
|
||||
|
||||
async saveAll(standings: ChampionshipStanding[]): Promise<void> {
|
||||
this.standings = standings;
|
||||
}
|
||||
|
||||
getAll(): ChampionshipStanding[] {
|
||||
return [...this.standings];
|
||||
}
|
||||
}
|
||||
|
||||
function makePointsTable(points: number[]): PointsTable {
|
||||
const byPos: Record<number, number> = {};
|
||||
points.forEach((p, idx) => {
|
||||
byPos[idx + 1] = p;
|
||||
});
|
||||
return new PointsTable(byPos);
|
||||
}
|
||||
|
||||
function makeChampionshipConfig(): ChampionshipConfig {
|
||||
const mainPoints = makePointsTable([25, 18, 15, 12, 10, 8, 6, 4, 2, 1]);
|
||||
const sprintPoints = makePointsTable([8, 7, 6, 5, 4, 3, 2, 1]);
|
||||
|
||||
const fastestLapBonus: BonusRule = {
|
||||
id: 'fastest-lap-main',
|
||||
type: 'fastestLap',
|
||||
points: 1,
|
||||
requiresFinishInTopN: 10,
|
||||
};
|
||||
|
||||
const sessionTypes: SessionType[] = ['sprint', 'main'];
|
||||
const pointsTableBySessionType: Record<SessionType, PointsTable> = {
|
||||
sprint: sprintPoints,
|
||||
main: mainPoints,
|
||||
practice: new PointsTable({}),
|
||||
qualifying: new PointsTable({}),
|
||||
q1: new PointsTable({}),
|
||||
q2: new PointsTable({}),
|
||||
q3: new PointsTable({}),
|
||||
timeTrial: new PointsTable({}),
|
||||
};
|
||||
|
||||
const bonusRulesBySessionType: Record<SessionType, BonusRule[]> = {
|
||||
sprint: [],
|
||||
main: [fastestLapBonus],
|
||||
practice: [],
|
||||
qualifying: [],
|
||||
q1: [],
|
||||
q2: [],
|
||||
q3: [],
|
||||
timeTrial: [],
|
||||
};
|
||||
|
||||
const dropScorePolicy: DropScorePolicy = {
|
||||
strategy: 'bestNResults',
|
||||
count: 6,
|
||||
};
|
||||
|
||||
return {
|
||||
id: 'driver-champ',
|
||||
name: 'Driver Championship',
|
||||
type: 'driver',
|
||||
sessionTypes,
|
||||
pointsTableBySessionType,
|
||||
bonusRulesBySessionType,
|
||||
dropScorePolicy,
|
||||
};
|
||||
}
|
||||
|
||||
describe('RecalculateChampionshipStandingsUseCase', () => {
|
||||
const leagueId = 'league-1';
|
||||
const seasonId = 'season-1';
|
||||
const championshipId = 'driver-champ';
|
||||
|
||||
let seasonRepository: InMemorySeasonRepository;
|
||||
let leagueScoringConfigRepository: InMemoryLeagueScoringConfigRepository;
|
||||
let raceRepository: InMemoryRaceRepository;
|
||||
let resultRepository: InMemoryResultRepository;
|
||||
let penaltyRepository: InMemoryPenaltyRepository;
|
||||
let championshipStandingRepository: InMemoryChampionshipStandingRepository;
|
||||
|
||||
let useCase: RecalculateChampionshipStandingsUseCase;
|
||||
|
||||
beforeEach(() => {
|
||||
seasonRepository = new InMemorySeasonRepository();
|
||||
leagueScoringConfigRepository = new InMemoryLeagueScoringConfigRepository();
|
||||
raceRepository = new InMemoryRaceRepository();
|
||||
resultRepository = new InMemoryResultRepository();
|
||||
penaltyRepository = new InMemoryPenaltyRepository();
|
||||
championshipStandingRepository = new InMemoryChampionshipStandingRepository();
|
||||
|
||||
const eventScoringService = new EventScoringService();
|
||||
const dropScoreApplier = new DropScoreApplier();
|
||||
const championshipAggregator = new ChampionshipAggregator(dropScoreApplier);
|
||||
|
||||
useCase = new RecalculateChampionshipStandingsUseCase(
|
||||
seasonRepository,
|
||||
leagueScoringConfigRepository,
|
||||
raceRepository,
|
||||
resultRepository,
|
||||
penaltyRepository,
|
||||
championshipStandingRepository,
|
||||
eventScoringService,
|
||||
championshipAggregator,
|
||||
);
|
||||
|
||||
const season = Season.create({
|
||||
id: seasonId,
|
||||
leagueId,
|
||||
gameId: 'iracing',
|
||||
name: 'Demo Season',
|
||||
status: 'active',
|
||||
year: 2025,
|
||||
order: 1,
|
||||
startDate: new Date('2025-01-01'),
|
||||
endDate: new Date('2025-12-31'),
|
||||
});
|
||||
|
||||
seasonRepository.seedSeason(season);
|
||||
|
||||
const championship = makeChampionshipConfig();
|
||||
|
||||
const leagueScoringConfig: LeagueScoringConfig = {
|
||||
id: 'lsc-1',
|
||||
seasonId,
|
||||
championships: [championship],
|
||||
};
|
||||
|
||||
leagueScoringConfigRepository.seedConfig(leagueScoringConfig);
|
||||
|
||||
const races: Race[] = [
|
||||
Race.create({
|
||||
id: 'race-1-sprint',
|
||||
leagueId,
|
||||
scheduledAt: new Date('2025-02-01'),
|
||||
track: 'Track 1',
|
||||
car: 'Car A',
|
||||
sessionType: 'race',
|
||||
status: 'completed',
|
||||
}),
|
||||
Race.create({
|
||||
id: 'race-1-main',
|
||||
leagueId,
|
||||
scheduledAt: new Date('2025-02-01'),
|
||||
track: 'Track 1',
|
||||
car: 'Car A',
|
||||
sessionType: 'race',
|
||||
status: 'completed',
|
||||
}),
|
||||
Race.create({
|
||||
id: 'race-2-sprint',
|
||||
leagueId,
|
||||
scheduledAt: new Date('2025-03-01'),
|
||||
track: 'Track 2',
|
||||
car: 'Car A',
|
||||
sessionType: 'race',
|
||||
status: 'completed',
|
||||
}),
|
||||
Race.create({
|
||||
id: 'race-2-main',
|
||||
leagueId,
|
||||
scheduledAt: new Date('2025-03-01'),
|
||||
track: 'Track 2',
|
||||
car: 'Car A',
|
||||
sessionType: 'race',
|
||||
status: 'completed',
|
||||
}),
|
||||
Race.create({
|
||||
id: 'race-3-sprint',
|
||||
leagueId,
|
||||
scheduledAt: new Date('2025-04-01'),
|
||||
track: 'Track 3',
|
||||
car: 'Car A',
|
||||
sessionType: 'race',
|
||||
status: 'completed',
|
||||
}),
|
||||
Race.create({
|
||||
id: 'race-3-main',
|
||||
leagueId,
|
||||
scheduledAt: new Date('2025-04-01'),
|
||||
track: 'Track 3',
|
||||
car: 'Car A',
|
||||
sessionType: 'race',
|
||||
status: 'completed',
|
||||
}),
|
||||
];
|
||||
|
||||
races.forEach((race) => raceRepository.seedRace(race));
|
||||
|
||||
const drivers = ['driver-1', 'driver-2', 'driver-3'];
|
||||
|
||||
const resultsData: Array<{
|
||||
raceId: string;
|
||||
finishingOrder: string[];
|
||||
fastestLapDriverId: string;
|
||||
}> = [
|
||||
{
|
||||
raceId: 'race-1-sprint',
|
||||
finishingOrder: ['driver-1', 'driver-2', 'driver-3'],
|
||||
fastestLapDriverId: 'driver-2',
|
||||
},
|
||||
{
|
||||
raceId: 'race-1-main',
|
||||
finishingOrder: ['driver-2', 'driver-1', 'driver-3'],
|
||||
fastestLapDriverId: 'driver-1',
|
||||
},
|
||||
{
|
||||
raceId: 'race-2-sprint',
|
||||
finishingOrder: ['driver-1', 'driver-3', 'driver-2'],
|
||||
fastestLapDriverId: 'driver-1',
|
||||
},
|
||||
{
|
||||
raceId: 'race-2-main',
|
||||
finishingOrder: ['driver-1', 'driver-2', 'driver-3'],
|
||||
fastestLapDriverId: 'driver-1',
|
||||
},
|
||||
{
|
||||
raceId: 'race-3-sprint',
|
||||
finishingOrder: ['driver-2', 'driver-1', 'driver-3'],
|
||||
fastestLapDriverId: 'driver-2',
|
||||
},
|
||||
{
|
||||
raceId: 'race-3-main',
|
||||
finishingOrder: ['driver-3', 'driver-1', 'driver-2'],
|
||||
fastestLapDriverId: 'driver-3',
|
||||
},
|
||||
];
|
||||
|
||||
let resultIdCounter = 1;
|
||||
for (const raceData of resultsData) {
|
||||
raceData.finishingOrder.forEach((driverId, index) => {
|
||||
const result = Result.create({
|
||||
id: `result-${resultIdCounter++}`,
|
||||
raceId: raceData.raceId,
|
||||
driverId,
|
||||
position: index + 1,
|
||||
fastestLap: driverId === raceData.fastestLapDriverId ? 90000 : 91000 + index * 100,
|
||||
incidents: 0,
|
||||
startPosition: index + 1,
|
||||
});
|
||||
resultRepository.seedResult(result);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
it('recalculates standings for a driver championship with sprint and main races', async () => {
|
||||
const dto = await useCase.execute({
|
||||
seasonId,
|
||||
championshipId,
|
||||
});
|
||||
|
||||
expect(dto.seasonId).toBe(seasonId);
|
||||
expect(dto.championshipId).toBe(championshipId);
|
||||
expect(dto.championshipName).toBe('Driver Championship');
|
||||
expect(dto.rows.length).toBeGreaterThan(0);
|
||||
|
||||
const rows = dto.rows;
|
||||
const sorted = [...rows].sort((a, b) => b.totalPoints - a.totalPoints);
|
||||
expect(rows.map((r) => r.participant.id)).toEqual(
|
||||
sorted.map((r) => r.participant.id),
|
||||
);
|
||||
|
||||
const leader = rows[0]!;
|
||||
expect(leader.resultsCounted).toBeLessThanOrEqual(6);
|
||||
expect(leader.resultsDropped).toBeGreaterThanOrEqual(0);
|
||||
});
|
||||
});
|
||||
293
tests/application/StartAutomationSession.spec.ts
Normal file
293
tests/application/StartAutomationSession.spec.ts
Normal file
@@ -0,0 +1,293 @@
|
||||
import { describe, it, expect, vi, beforeEach, Mock } from 'vitest';
|
||||
import { StartAutomationSessionUseCase } from '../../../../core/automation/application/use-cases/StartAutomationSessionUseCase';
|
||||
import { AutomationEnginePort as IAutomationEngine } from '../../../../core/automation/application/ports/AutomationEnginePort';
|
||||
import { IBrowserAutomation as IScreenAutomation } from '../../../../core/automation/application/ports/ScreenAutomationPort';
|
||||
import { SessionRepositoryPort as ISessionRepository } from '../../../../core/automation/application/ports/SessionRepositoryPort';
|
||||
import { AutomationSession } from '@gridpilot/automation/domain/entities/AutomationSession';
|
||||
|
||||
describe('StartAutomationSessionUseCase', () => {
|
||||
let mockAutomationEngine: {
|
||||
executeStep: Mock;
|
||||
validateConfiguration: Mock;
|
||||
};
|
||||
let mockBrowserAutomation: {
|
||||
navigateToPage: Mock;
|
||||
fillFormField: Mock;
|
||||
clickElement: Mock;
|
||||
waitForElement: Mock;
|
||||
handleModal: Mock;
|
||||
};
|
||||
let mockSessionRepository: {
|
||||
save: Mock;
|
||||
findById: Mock;
|
||||
update: Mock;
|
||||
delete: Mock;
|
||||
};
|
||||
let useCase: StartAutomationSessionUseCase;
|
||||
|
||||
beforeEach(() => {
|
||||
mockAutomationEngine = {
|
||||
executeStep: vi.fn(),
|
||||
validateConfiguration: vi.fn(),
|
||||
};
|
||||
|
||||
mockBrowserAutomation = {
|
||||
navigateToPage: vi.fn(),
|
||||
fillFormField: vi.fn(),
|
||||
clickElement: vi.fn(),
|
||||
waitForElement: vi.fn(),
|
||||
handleModal: vi.fn(),
|
||||
};
|
||||
|
||||
mockSessionRepository = {
|
||||
save: vi.fn(),
|
||||
findById: vi.fn(),
|
||||
update: vi.fn(),
|
||||
delete: vi.fn(),
|
||||
};
|
||||
|
||||
useCase = new StartAutomationSessionUseCase(
|
||||
mockAutomationEngine as unknown as IAutomationEngine,
|
||||
mockBrowserAutomation as unknown as IScreenAutomation,
|
||||
mockSessionRepository as unknown as ISessionRepository
|
||||
);
|
||||
});
|
||||
|
||||
describe('execute - happy path', () => {
|
||||
it('should create and persist a new automation session', async () => {
|
||||
const config = {
|
||||
sessionName: 'Test Race Session',
|
||||
trackId: 'spa',
|
||||
carIds: ['dallara-f3'],
|
||||
};
|
||||
|
||||
mockAutomationEngine.validateConfiguration.mockResolvedValue({ isValid: true });
|
||||
mockSessionRepository.save.mockResolvedValue(undefined);
|
||||
|
||||
const result = await useCase.execute(config);
|
||||
|
||||
expect(result.sessionId).toBeDefined();
|
||||
expect(result.state).toBe('PENDING');
|
||||
expect(result.currentStep).toBe(1);
|
||||
expect(mockAutomationEngine.validateConfiguration).toHaveBeenCalledWith(config);
|
||||
expect(mockSessionRepository.save).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
config,
|
||||
currentStep: expect.objectContaining({ value: 1 }),
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
it('should return session DTO with correct structure', async () => {
|
||||
const config = {
|
||||
sessionName: 'Test Race Session',
|
||||
trackId: 'spa',
|
||||
carIds: ['dallara-f3'],
|
||||
};
|
||||
|
||||
mockAutomationEngine.validateConfiguration.mockResolvedValue({ isValid: true });
|
||||
mockSessionRepository.save.mockResolvedValue(undefined);
|
||||
|
||||
const result = await useCase.execute(config);
|
||||
|
||||
expect(result).toMatchObject({
|
||||
sessionId: expect.any(String),
|
||||
state: 'PENDING',
|
||||
currentStep: 1,
|
||||
config: {
|
||||
sessionName: 'Test Race Session',
|
||||
trackId: 'spa',
|
||||
carIds: ['dallara-f3'],
|
||||
},
|
||||
});
|
||||
expect(result.startedAt).toBeUndefined();
|
||||
expect(result.completedAt).toBeUndefined();
|
||||
expect(result.errorMessage).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should validate configuration before creating session', async () => {
|
||||
const config = {
|
||||
sessionName: 'Test Race Session',
|
||||
trackId: 'spa',
|
||||
carIds: ['dallara-f3'],
|
||||
};
|
||||
|
||||
mockAutomationEngine.validateConfiguration.mockResolvedValue({ isValid: true });
|
||||
mockSessionRepository.save.mockResolvedValue(undefined);
|
||||
|
||||
await useCase.execute(config);
|
||||
|
||||
expect(mockAutomationEngine.validateConfiguration).toHaveBeenCalledWith(config);
|
||||
expect(mockSessionRepository.save).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('execute - validation failures', () => {
|
||||
it('should throw error for empty session name', async () => {
|
||||
const config = {
|
||||
sessionName: '',
|
||||
trackId: 'spa',
|
||||
carIds: ['dallara-f3'],
|
||||
};
|
||||
|
||||
await expect(useCase.execute(config)).rejects.toThrow('Session name cannot be empty');
|
||||
expect(mockSessionRepository.save).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should throw error for missing track ID', async () => {
|
||||
const config = {
|
||||
sessionName: 'Test Race',
|
||||
trackId: '',
|
||||
carIds: ['dallara-f3'],
|
||||
};
|
||||
|
||||
await expect(useCase.execute(config)).rejects.toThrow('Track ID is required');
|
||||
expect(mockSessionRepository.save).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should throw error for empty car list', async () => {
|
||||
const config = {
|
||||
sessionName: 'Test Race',
|
||||
trackId: 'spa',
|
||||
carIds: [],
|
||||
};
|
||||
|
||||
await expect(useCase.execute(config)).rejects.toThrow('At least one car must be selected');
|
||||
expect(mockSessionRepository.save).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should throw error when automation engine validation fails', async () => {
|
||||
const config = {
|
||||
sessionName: 'Test Race',
|
||||
trackId: 'invalid-track',
|
||||
carIds: ['dallara-f3'],
|
||||
};
|
||||
|
||||
mockAutomationEngine.validateConfiguration.mockResolvedValue({
|
||||
isValid: false,
|
||||
error: 'Invalid track ID: invalid-track',
|
||||
});
|
||||
|
||||
await expect(useCase.execute(config)).rejects.toThrow('Invalid track ID: invalid-track');
|
||||
expect(mockSessionRepository.save).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should throw error when automation engine validation rejects', async () => {
|
||||
const config = {
|
||||
sessionName: 'Test Race',
|
||||
trackId: 'spa',
|
||||
carIds: ['invalid-car'],
|
||||
};
|
||||
|
||||
mockAutomationEngine.validateConfiguration.mockRejectedValue(
|
||||
new Error('Validation service unavailable')
|
||||
);
|
||||
|
||||
await expect(useCase.execute(config)).rejects.toThrow('Validation service unavailable');
|
||||
expect(mockSessionRepository.save).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('execute - port interactions', () => {
|
||||
it('should call automation engine before saving session', async () => {
|
||||
const config = {
|
||||
sessionName: 'Test Race',
|
||||
trackId: 'spa',
|
||||
carIds: ['dallara-f3'],
|
||||
};
|
||||
|
||||
const callOrder: string[] = [];
|
||||
|
||||
mockAutomationEngine.validateConfiguration.mockImplementation(async () => {
|
||||
callOrder.push('validateConfiguration');
|
||||
return { isValid: true };
|
||||
});
|
||||
|
||||
mockSessionRepository.save.mockImplementation(async () => {
|
||||
callOrder.push('save');
|
||||
});
|
||||
|
||||
await useCase.execute(config);
|
||||
|
||||
expect(callOrder).toEqual(['validateConfiguration', 'save']);
|
||||
});
|
||||
|
||||
it('should persist session with domain entity', async () => {
|
||||
const config = {
|
||||
sessionName: 'Test Race',
|
||||
trackId: 'spa',
|
||||
carIds: ['dallara-f3'],
|
||||
};
|
||||
|
||||
mockAutomationEngine.validateConfiguration.mockResolvedValue({ isValid: true });
|
||||
mockSessionRepository.save.mockResolvedValue(undefined);
|
||||
|
||||
await useCase.execute(config);
|
||||
|
||||
expect(mockSessionRepository.save).toHaveBeenCalledWith(
|
||||
expect.any(AutomationSession)
|
||||
);
|
||||
});
|
||||
|
||||
it('should throw error when repository save fails', async () => {
|
||||
const config = {
|
||||
sessionName: 'Test Race',
|
||||
trackId: 'spa',
|
||||
carIds: ['dallara-f3'],
|
||||
};
|
||||
|
||||
mockAutomationEngine.validateConfiguration.mockResolvedValue({ isValid: true });
|
||||
mockSessionRepository.save.mockRejectedValue(new Error('Database connection failed'));
|
||||
|
||||
await expect(useCase.execute(config)).rejects.toThrow('Database connection failed');
|
||||
});
|
||||
});
|
||||
|
||||
describe('execute - edge cases', () => {
|
||||
it('should handle very long session names', async () => {
|
||||
const config = {
|
||||
sessionName: 'A'.repeat(200),
|
||||
trackId: 'spa',
|
||||
carIds: ['dallara-f3'],
|
||||
};
|
||||
|
||||
mockAutomationEngine.validateConfiguration.mockResolvedValue({ isValid: true });
|
||||
mockSessionRepository.save.mockResolvedValue(undefined);
|
||||
|
||||
const result = await useCase.execute(config);
|
||||
|
||||
expect(result.config.sessionName).toBe('A'.repeat(200));
|
||||
});
|
||||
|
||||
it('should handle multiple cars in configuration', async () => {
|
||||
const config = {
|
||||
sessionName: 'Multi-car Race',
|
||||
trackId: 'spa',
|
||||
carIds: ['dallara-f3', 'porsche-911-gt3', 'bmw-m4-gt4'],
|
||||
};
|
||||
|
||||
mockAutomationEngine.validateConfiguration.mockResolvedValue({ isValid: true });
|
||||
mockSessionRepository.save.mockResolvedValue(undefined);
|
||||
|
||||
const result = await useCase.execute(config);
|
||||
|
||||
expect(result.config.carIds).toEqual(['dallara-f3', 'porsche-911-gt3', 'bmw-m4-gt4']);
|
||||
});
|
||||
|
||||
it('should handle special characters in session name', async () => {
|
||||
const config = {
|
||||
sessionName: 'Test & Race #1 (2025)',
|
||||
trackId: 'spa',
|
||||
carIds: ['dallara-f3'],
|
||||
};
|
||||
|
||||
mockAutomationEngine.validateConfiguration.mockResolvedValue({ isValid: true });
|
||||
mockSessionRepository.save.mockResolvedValue(undefined);
|
||||
|
||||
const result = await useCase.execute(config);
|
||||
|
||||
expect(result.config.sessionName).toBe('Test & Race #1 (2025)');
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
107
tests/application/VerifyAuthenticatedPageUseCase.spec.ts
Normal file
107
tests/application/VerifyAuthenticatedPageUseCase.spec.ts
Normal file
@@ -0,0 +1,107 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import { VerifyAuthenticatedPageUseCase } from '../../../../core/automation/application/use-cases/VerifyAuthenticatedPageUseCase';
|
||||
import { AuthenticationServicePort as IAuthenticationService } from '../../../../core/automation/application/ports/AuthenticationServicePort';
|
||||
import { Result } from '../../../../core/shared/result/Result';
|
||||
import { BrowserAuthenticationState } from '@gridpilot/automation/domain/value-objects/BrowserAuthenticationState';
|
||||
import { AuthenticationState } from '@gridpilot/automation/domain/value-objects/AuthenticationState';
|
||||
|
||||
describe('VerifyAuthenticatedPageUseCase', () => {
|
||||
let useCase: VerifyAuthenticatedPageUseCase;
|
||||
let mockAuthService: {
|
||||
checkSession: ReturnType<typeof vi.fn>;
|
||||
verifyPageAuthentication: ReturnType<typeof vi.fn>;
|
||||
initiateLogin: ReturnType<typeof vi.fn>;
|
||||
clearSession: ReturnType<typeof vi.fn>;
|
||||
getState: ReturnType<typeof vi.fn>;
|
||||
validateServerSide: ReturnType<typeof vi.fn>;
|
||||
refreshSession: ReturnType<typeof vi.fn>;
|
||||
getSessionExpiry: ReturnType<typeof vi.fn>;
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
mockAuthService = {
|
||||
checkSession: vi.fn(),
|
||||
verifyPageAuthentication: vi.fn(),
|
||||
initiateLogin: vi.fn(),
|
||||
clearSession: vi.fn(),
|
||||
getState: vi.fn(),
|
||||
validateServerSide: vi.fn(),
|
||||
refreshSession: vi.fn(),
|
||||
getSessionExpiry: vi.fn(),
|
||||
};
|
||||
useCase = new VerifyAuthenticatedPageUseCase(
|
||||
mockAuthService as unknown as IAuthenticationService
|
||||
);
|
||||
});
|
||||
|
||||
it('should return fully authenticated browser state', async () => {
|
||||
const mockBrowserState = new BrowserAuthenticationState(true, true);
|
||||
mockAuthService.verifyPageAuthentication.mockResolvedValue(
|
||||
Result.ok(mockBrowserState)
|
||||
);
|
||||
|
||||
const result = await useCase.execute();
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
const browserState = result.unwrap();
|
||||
expect(browserState.isFullyAuthenticated()).toBe(true);
|
||||
expect(browserState.getAuthenticationState()).toBe(AuthenticationState.AUTHENTICATED);
|
||||
});
|
||||
|
||||
it('should return unauthenticated state when page not authenticated', async () => {
|
||||
const mockBrowserState = new BrowserAuthenticationState(true, false);
|
||||
mockAuthService.verifyPageAuthentication.mockResolvedValue(
|
||||
Result.ok(mockBrowserState)
|
||||
);
|
||||
|
||||
const result = await useCase.execute();
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
const browserState = result.unwrap();
|
||||
expect(browserState.isFullyAuthenticated()).toBe(false);
|
||||
expect(browserState.getAuthenticationState()).toBe(AuthenticationState.EXPIRED);
|
||||
});
|
||||
|
||||
it('should return requires reauth state when cookies invalid', async () => {
|
||||
const mockBrowserState = new BrowserAuthenticationState(false, false);
|
||||
mockAuthService.verifyPageAuthentication.mockResolvedValue(
|
||||
Result.ok(mockBrowserState)
|
||||
);
|
||||
|
||||
const result = await useCase.execute();
|
||||
|
||||
expect(result.isOk()).toBe(true);
|
||||
const browserState = result.unwrap();
|
||||
expect(browserState.requiresReauthentication()).toBe(true);
|
||||
expect(browserState.getAuthenticationState()).toBe(AuthenticationState.UNKNOWN);
|
||||
});
|
||||
|
||||
it('should propagate errors from verifyPageAuthentication', async () => {
|
||||
const error = new Error('Verification failed');
|
||||
mockAuthService.verifyPageAuthentication.mockResolvedValue(
|
||||
Result.err(error)
|
||||
);
|
||||
|
||||
const result = await useCase.execute();
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
if (result.isErr()) {
|
||||
expect(result.error).toBeInstanceOf(Error);
|
||||
expect(result.error?.message).toBe('Verification failed');
|
||||
}
|
||||
});
|
||||
|
||||
it('should handle unexpected errors', async () => {
|
||||
mockAuthService.verifyPageAuthentication.mockRejectedValue(
|
||||
new Error('Unexpected error')
|
||||
);
|
||||
|
||||
const result = await useCase.execute();
|
||||
|
||||
expect(result.isErr()).toBe(true);
|
||||
if (result.isErr()) {
|
||||
expect(result.error).toBeInstanceOf(Error);
|
||||
expect(result.error?.message).toBe('Page verification failed: Unexpected error');
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user