This commit is contained in:
2025-12-16 12:14:06 +01:00
parent 9a891ac8b3
commit 7d3393e1b9
90 changed files with 20 additions and 974 deletions

View File

@@ -0,0 +1,167 @@
import { describe, it, expect } from 'vitest';
import { PageStateValidator } from '@core/automation/domain/services/PageStateValidator';
describe('PageStateValidator', () => {
const validator = new PageStateValidator();
describe('validateState', () => {
it('should return valid when all required selectors are present', () => {
// Arrange
const actualState = (selector: string) => {
return ['#add-car-button', '#cars-list'].includes(selector);
};
// Act
const result = validator.validateState(actualState, {
expectedStep: 'cars',
requiredSelectors: ['#add-car-button', '#cars-list']
});
// Assert
expect(result.isOk()).toBe(true);
const value = result.unwrap();
expect(value.isValid).toBe(true);
expect(value.expectedStep).toBe('cars');
expect(value.message).toContain('Page state valid');
});
it('should return invalid when required selectors are missing', () => {
// Arrange
const actualState = (selector: string) => {
return selector === '#add-car-button'; // Only one of two selectors present
};
// Act
const result = validator.validateState(actualState, {
expectedStep: 'cars',
requiredSelectors: ['#add-car-button', '#cars-list']
});
// Assert
expect(result.isOk()).toBe(true);
const value = result.unwrap();
expect(value.isValid).toBe(false);
expect(value.expectedStep).toBe('cars');
expect(value.missingSelectors).toEqual(['#cars-list']);
expect(value.message).toContain('missing required elements');
});
it('should return invalid when forbidden selectors are present', () => {
// Arrange
const actualState = (selector: string) => {
return ['#add-car-button', '#set-track'].includes(selector);
};
// Act
const result = validator.validateState(actualState, {
expectedStep: 'cars',
requiredSelectors: ['#add-car-button'],
forbiddenSelectors: ['#set-track'] // Should NOT be on track page yet
});
// Assert
expect(result.isOk()).toBe(true);
const value = result.unwrap();
expect(value.isValid).toBe(false);
expect(value.expectedStep).toBe('cars');
expect(value.unexpectedSelectors).toEqual(['#set-track']);
expect(value.message).toContain('unexpected elements');
});
it('should handle empty forbidden selectors array', () => {
// Arrange
const actualState = (selector: string) => {
return selector === '#add-car-button';
};
// Act
const result = validator.validateState(actualState, {
expectedStep: 'cars',
requiredSelectors: ['#add-car-button'],
forbiddenSelectors: []
});
// Assert
expect(result.isOk()).toBe(true);
const value = result.unwrap();
expect(value.isValid).toBe(true);
});
it('should handle undefined forbidden selectors', () => {
// Arrange
const actualState = (selector: string) => {
return selector === '#add-car-button';
};
// Act
const result = validator.validateState(actualState, {
expectedStep: 'cars',
requiredSelectors: ['#add-car-button']
// forbiddenSelectors is undefined
});
// Assert
expect(result.isOk()).toBe(true);
const value = result.unwrap();
expect(value.isValid).toBe(true);
});
it('should return error result when actualState function throws', () => {
// Arrange
const actualState = (selector: string) => {
throw new Error('Selector evaluation failed');
};
// Act
const result = validator.validateState(actualState, {
expectedStep: 'cars',
requiredSelectors: ['#add-car-button']
});
// Assert
expect(result.isErr()).toBe(true);
const error = result.unwrapErr();
expect(error.message).toContain('Selector evaluation failed');
});
it('should provide clear error messages for missing selectors', () => {
// Arrange
const actualState = () => false; // Nothing present
// Act
const result = validator.validateState(actualState, {
expectedStep: 'track',
requiredSelectors: ['#set-track', '#track-search']
});
// Assert
expect(result.isOk()).toBe(true);
const value = result.unwrap();
expect(value.isValid).toBe(false);
expect(value.message).toBe('Page state mismatch: Expected to be on "track" page but missing required elements');
expect(value.missingSelectors).toEqual(['#set-track', '#track-search']);
});
it('should validate complex state with both required and forbidden selectors', () => {
// Arrange - Simulate being on Cars page but Track page elements leaked through
const actualState = (selector: string) => {
const presentSelectors = ['#add-car-button', '#cars-list', '#set-track'];
return presentSelectors.includes(selector);
};
// Act
const result = validator.validateState(actualState, {
expectedStep: 'cars',
requiredSelectors: ['#add-car-button', '#cars-list'],
forbiddenSelectors: ['#set-track', '#track-search']
});
// Assert
expect(result.isOk()).toBe(true);
const value = result.unwrap();
expect(value.isValid).toBe(false); // Invalid due to forbidden selector
expect(value.unexpectedSelectors).toEqual(['#set-track']);
expect(value.message).toContain('unexpected elements');
});
});
});

View File

@@ -0,0 +1,231 @@
import { describe, it, expect } from 'vitest';
import { StepTransitionValidator } from '@core/automation/domain/services/StepTransitionValidator';
import { StepId } from '@core/automation/domain/value-objects/StepId';
import { SessionState } from '@core/automation/domain/value-objects/SessionState';
describe('StepTransitionValidator Service', () => {
describe('canTransition', () => {
it('should allow sequential forward transition', () => {
const currentStep = StepId.create(1);
const nextStep = StepId.create(2);
const state = SessionState.create('IN_PROGRESS');
const result = StepTransitionValidator.canTransition(currentStep, nextStep, state);
expect(result.isValid).toBe(true);
expect(result.error).toBeUndefined();
});
it('should reject transition when not IN_PROGRESS', () => {
const currentStep = StepId.create(1);
const nextStep = StepId.create(2);
const state = SessionState.create('PENDING');
const result = StepTransitionValidator.canTransition(currentStep, nextStep, state);
expect(result.isValid).toBe(false);
expect(result.error).toBe('Session must be in progress to transition steps');
});
it('should reject skipping steps', () => {
const currentStep = StepId.create(1);
const nextStep = StepId.create(3);
const state = SessionState.create('IN_PROGRESS');
const result = StepTransitionValidator.canTransition(currentStep, nextStep, state);
expect(result.isValid).toBe(false);
expect(result.error).toBe('Cannot skip steps - must progress sequentially');
});
it('should reject backward transitions', () => {
const currentStep = StepId.create(5);
const nextStep = StepId.create(4);
const state = SessionState.create('IN_PROGRESS');
const result = StepTransitionValidator.canTransition(currentStep, nextStep, state);
expect(result.isValid).toBe(false);
expect(result.error).toBe('Cannot move backward - steps must progress forward only');
});
it('should reject same step transition', () => {
const currentStep = StepId.create(5);
const nextStep = StepId.create(5);
const state = SessionState.create('IN_PROGRESS');
const result = StepTransitionValidator.canTransition(currentStep, nextStep, state);
expect(result.isValid).toBe(false);
expect(result.error).toBe('Already at this step');
});
it('should allow transition through modal steps', () => {
const currentStep = StepId.create(5);
const nextStep = StepId.create(6);
const state = SessionState.create('IN_PROGRESS');
const result = StepTransitionValidator.canTransition(currentStep, nextStep, state);
expect(result.isValid).toBe(true);
});
it('should allow transition from modal step to next', () => {
const currentStep = StepId.create(6);
const nextStep = StepId.create(7);
const state = SessionState.create('IN_PROGRESS');
const result = StepTransitionValidator.canTransition(currentStep, nextStep, state);
expect(result.isValid).toBe(true);
});
});
describe('validateModalStepTransition', () => {
it('should allow entering modal step 6', () => {
const currentStep = StepId.create(5);
const nextStep = StepId.create(6);
const result = StepTransitionValidator.validateModalStepTransition(currentStep, nextStep);
expect(result.isValid).toBe(true);
});
it('should allow entering modal step 9', () => {
const currentStep = StepId.create(8);
const nextStep = StepId.create(9);
const result = StepTransitionValidator.validateModalStepTransition(currentStep, nextStep);
expect(result.isValid).toBe(true);
});
it('should allow entering modal step 12', () => {
const currentStep = StepId.create(11);
const nextStep = StepId.create(12);
const result = StepTransitionValidator.validateModalStepTransition(currentStep, nextStep);
expect(result.isValid).toBe(true);
});
it('should allow exiting modal step 6', () => {
const currentStep = StepId.create(6);
const nextStep = StepId.create(7);
const result = StepTransitionValidator.validateModalStepTransition(currentStep, nextStep);
expect(result.isValid).toBe(true);
});
it('should allow non-modal transitions', () => {
const currentStep = StepId.create(1);
const nextStep = StepId.create(2);
const result = StepTransitionValidator.validateModalStepTransition(currentStep, nextStep);
expect(result.isValid).toBe(true);
});
});
describe('shouldStopAtStep18', () => {
it('should return true when transitioning to step 17 (final step)', () => {
const nextStep = StepId.create(17);
const shouldStop = StepTransitionValidator.shouldStopAtStep18(nextStep);
expect(shouldStop).toBe(true);
});
it('should return false for steps before 17', () => {
const nextStep = StepId.create(16);
const shouldStop = StepTransitionValidator.shouldStopAtStep18(nextStep);
expect(shouldStop).toBe(false);
});
it('should return false for early steps', () => {
const nextStep = StepId.create(1);
const shouldStop = StepTransitionValidator.shouldStopAtStep18(nextStep);
expect(shouldStop).toBe(false);
});
});
describe('getStepDescription', () => {
it('should return description for step 1', () => {
const step = StepId.create(1);
const description = StepTransitionValidator.getStepDescription(step);
expect(description).toBe('Navigate to Hosted Racing page');
});
it('should return description for step 6 (modal)', () => {
const step = StepId.create(6);
const description = StepTransitionValidator.getStepDescription(step);
expect(description).toBe('Add Admin (Modal)');
});
it('should return description for step 17 (final)', () => {
const step = StepId.create(17);
const description = StepTransitionValidator.getStepDescription(step);
expect(description).toBe('Track Conditions (STOP - Manual Submit Required)');
});
it('should return descriptions for all modal steps', () => {
const modalSteps = [6, 9, 12];
modalSteps.forEach(stepNum => {
const step = StepId.create(stepNum);
const description = StepTransitionValidator.getStepDescription(step);
expect(description).toContain('(Modal)');
});
});
});
describe('edge cases', () => {
it('should handle rapid sequential transitions', () => {
const state = SessionState.create('IN_PROGRESS');
let currentStep = StepId.create(1);
for (let i = 2; i <= 17; i++) {
const nextStep = StepId.create(i);
const result = StepTransitionValidator.canTransition(currentStep, nextStep, state);
expect(result.isValid).toBe(true);
currentStep = nextStep;
}
});
it('should prevent transitions from terminal states', () => {
const terminalStates = ['COMPLETED', 'FAILED', 'STOPPED_AT_STEP_18'] as const;
terminalStates.forEach(stateValue => {
const currentStep = StepId.create(10);
const nextStep = StepId.create(11);
const state = SessionState.create(stateValue);
const result = StepTransitionValidator.canTransition(currentStep, nextStep, state);
expect(result.isValid).toBe(false);
});
});
it('should allow transition from PAUSED when resumed', () => {
const currentStep = StepId.create(5);
const nextStep = StepId.create(6);
const state = SessionState.create('IN_PROGRESS');
const result = StepTransitionValidator.canTransition(currentStep, nextStep, state);
expect(result.isValid).toBe(true);
});
});
});