import { describe, it, expect, beforeEach, afterEach } from 'vitest'; import { PlaywrightAutomationAdapter } from '../../packages/infrastructure/adapters/automation/PlaywrightAutomationAdapter'; import { FixtureServer } from '../../packages/infrastructure/adapters/automation/FixtureServer'; import { StepId } from '../../packages/domain/value-objects/StepId'; import { PinoLogAdapter } from '../../packages/infrastructure/adapters/logging/PinoLogAdapter'; /** * Regression Test: Step 9 State Synchronization * * This test prevents regression of the critical bug where Step 9 (ADD_CAR) * executes while the browser is already on Step 11 (SET_TRACK). * * **Root Cause**: Validation was checking `validation.isErr()` instead of * `validationResult.isValid`, causing validation failures to be silently ignored. * * **Evidence**: Debug dump showed: * - Wizard Footer: "← Cars | Track Options →" * - Actual Page: Step 11 (SET_TRACK) * - Expected Page: Step 8/9 (SET_CARS) * - Discrepancy: 3 steps ahead */ describe('Step 9 State Validation Regression Test', () => { let server: FixtureServer; let adapter: PlaywrightAutomationAdapter; let logger: PinoLogAdapter; beforeEach(async () => { // Setup fixture server server = new FixtureServer(); const serverInfo = await server.start(); // Setup logger logger = new PinoLogAdapter(); // Setup adapter in mock mode adapter = new PlaywrightAutomationAdapter( { headless: true, timeout: 5000, mode: 'mock', baseUrl: serverInfo.url, }, logger ); await adapter.connect(); }); afterEach(async () => { await adapter.disconnect(); await server.stop(); }); it('should throw error if Step 9 executes on Track page instead of Cars page', async () => { // Arrange: Navigate directly to Track page (Step 11) await adapter.navigateToPage(server.getFixtureUrl(11)); // Wait for page to load await adapter.getPage()?.waitForLoadState('domcontentloaded'); // Act & Assert: Attempt to execute Step 9 (should fail immediately) await expect(async () => { await adapter.executeStep(StepId.create(9), { carSearch: 'Mazda MX-5' }); }).rejects.toThrow(/Step 9 FAILED validation/i); }); it('should detect state mismatch when Cars button is missing', async () => { // Arrange: Navigate to Track page await adapter.navigateToPage(server.getFixtureUrl(11)); await adapter.getPage()?.waitForLoadState('domcontentloaded'); // Act & Assert await expect(async () => { await adapter.executeStep(StepId.create(9), { carSearch: 'Porsche 911' }); }).rejects.toThrow(/Expected cars step/i); }); it('should detect when #set-track container is present instead of Cars page', async () => { // Arrange: Navigate to Track page await adapter.navigateToPage(server.getFixtureUrl(11)); await adapter.getPage()?.waitForLoadState('domcontentloaded'); // Act & Assert: Error should mention we're 3 steps ahead await expect(async () => { await adapter.executeStep(StepId.create(9), { carSearch: 'Ferrari 488' }); }).rejects.toThrow(/3 steps ahead|Track page/i); }); it('should pass validation when actually on Cars page', async () => { // Arrange: Navigate to correct page (Step 8 - Cars) await adapter.navigateToPage(server.getFixtureUrl(8)); await adapter.getPage()?.waitForLoadState('domcontentloaded'); // Act: Execute Step 9 (should succeed) const result = await adapter.executeStep(StepId.create(9), { carSearch: 'Mazda MX-5' }); // Assert: Should complete successfully expect(result.success).toBe(true); }); it('should fail fast on Step 8 if already past Cars page', async () => { // Arrange: Navigate to Track page (Step 11) await adapter.navigateToPage(server.getFixtureUrl(11)); await adapter.getPage()?.waitForLoadState('domcontentloaded'); // Act & Assert: Step 8 should also fail validation await expect(async () => { await adapter.executeStep(StepId.create(8), {}); }).rejects.toThrow(/Step 8 FAILED validation/i); }); it('should provide detailed error context in validation failure', async () => { // Arrange: Navigate to Track page await adapter.navigateToPage(server.getFixtureUrl(11)); await adapter.getPage()?.waitForLoadState('domcontentloaded'); // Act: Capture error details let errorMessage = ''; try { await adapter.executeStep(StepId.create(9), { carSearch: 'BMW M4' }); } catch (error) { errorMessage = error instanceof Error ? error.message : String(error); } // Assert: Error should contain diagnostic information expect(errorMessage).toContain('Step 9'); expect(errorMessage).toMatch(/validation|mismatch|wrong page/i); }); it('should validate page state before attempting any Step 9 actions', async () => { // Arrange: Navigate to wrong page await adapter.navigateToPage(server.getFixtureUrl(11)); await adapter.getPage()?.waitForLoadState('domcontentloaded'); const page = adapter.getPage(); if (!page) { throw new Error('Page not available'); } // Track if any car-related actions were attempted let carModalOpened = false; page.on('framenavigated', () => { // If we navigate, it means we got past validation (bad!) carModalOpened = true; }); // Act: Try to execute Step 9 let validationError = false; try { await adapter.executeStep(StepId.create(9), { carSearch: 'Audi R8' }); } catch (error) { validationError = true; } // Assert: Should fail validation before attempting any actions expect(validationError).toBe(true); expect(carModalOpened).toBe(false); }); it('should check wizard footer state in Step 9', async () => { // This test verifies the wizard footer check is working // Arrange: Navigate to Track page await adapter.navigateToPage(server.getFixtureUrl(11)); await adapter.getPage()?.waitForLoadState('domcontentloaded'); // Act & Assert: Error should reference wizard footer state await expect(async () => { await adapter.executeStep(StepId.create(9), { carSearch: 'McLaren 720S' }); }).rejects.toThrow(); // Will throw due to validation failure }); }); describe('Step 8 State Validation Regression Test', () => { let server: FixtureServer; let adapter: PlaywrightAutomationAdapter; let logger: PinoLogAdapter; beforeEach(async () => { server = new FixtureServer(); const serverInfo = await server.start(); logger = new PinoLogAdapter(); adapter = new PlaywrightAutomationAdapter( { headless: true, timeout: 5000, mode: 'mock', baseUrl: serverInfo.url, }, logger ); await adapter.connect(); }); afterEach(async () => { await adapter.disconnect(); await server.stop(); }); it('should validate page state in Step 8 before proceeding', async () => { // Arrange: Navigate to wrong page (Track instead of Cars) await adapter.navigateToPage(server.getFixtureUrl(11)); await adapter.getPage()?.waitForLoadState('domcontentloaded'); // Act & Assert: Step 8 should fail validation await expect(async () => { await adapter.executeStep(StepId.create(8), {}); }).rejects.toThrow(/Step 8 FAILED validation/i); }); it('should pass Step 8 validation when on correct page', async () => { // Arrange: Navigate to Cars page await adapter.navigateToPage(server.getFixtureUrl(8)); await adapter.getPage()?.waitForLoadState('domcontentloaded'); // Act: Execute Step 8 const result = await adapter.executeStep(StepId.create(8), {}); // Assert: Should succeed expect(result.success).toBe(true); }); }); describe('Step 11 State Validation Regression Test', () => { let server: FixtureServer; let adapter: PlaywrightAutomationAdapter; let logger: PinoLogAdapter; beforeEach(async () => { server = new FixtureServer(); const serverInfo = await server.start(); logger = new PinoLogAdapter(); adapter = new PlaywrightAutomationAdapter( { headless: true, timeout: 5000, mode: 'mock', baseUrl: serverInfo.url, }, logger ); await adapter.connect(); }); afterEach(async () => { await adapter.disconnect(); await server.stop(); }); it('should validate Step 11 is on Track page', async () => { // Arrange: Navigate to wrong page (Cars instead of Track) await adapter.navigateToPage(server.getFixtureUrl(8)); await adapter.getPage()?.waitForLoadState('domcontentloaded'); // Act & Assert: Step 11 should fail validation await expect(async () => { await adapter.executeStep(StepId.create(11), {}); }).rejects.toThrow(/Step 11 FAILED validation/i); }); it('should pass Step 11 validation when on Track page', async () => { // Arrange: Navigate to Track page await adapter.navigateToPage(server.getFixtureUrl(11)); await adapter.getPage()?.waitForLoadState('domcontentloaded'); // Act: Execute Step 11 const result = await adapter.executeStep(StepId.create(11), {}); // Assert: Should succeed expect(result.success).toBe(true); }); });