import type { Browser, Page, BrowserContext } from 'playwright'; import * as fs from 'fs'; import * as path from 'path'; import { StepId } from '../../../../domain/value-objects/StepId'; import { AuthenticationState } from '../../../../domain/value-objects/AuthenticationState'; import { BrowserAuthenticationState } from '../../../../domain/value-objects/BrowserAuthenticationState'; import { CheckoutPrice } from '../../../../domain/value-objects/CheckoutPrice'; import { CheckoutState } from '../../../../domain/value-objects/CheckoutState'; import { CheckoutConfirmation } from '../../../../domain/value-objects/CheckoutConfirmation'; import type { IBrowserAutomation } from '../../../../application/ports/IScreenAutomation'; import type { NavigationResult, FormFillResult, ClickResult, WaitResult, ModalResult, AutomationResult, } from '../../../../application/ports/AutomationResults'; import type { IAuthenticationService } from '../../../../application/ports/IAuthenticationService'; import type { ILogger } from '../../../../application/ports/ILogger'; import { Result } from '../../../../shared/result/Result'; import { IRACING_SELECTORS, IRACING_URLS, IRACING_TIMEOUTS, ALL_BLOCKED_SELECTORS, BLOCKED_KEYWORDS } from '../dom/IRacingSelectors'; import { SessionCookieStore } from '../auth/SessionCookieStore'; import { PlaywrightBrowserSession } from './PlaywrightBrowserSession'; import { getFixtureForStep } from '../engine/FixtureServer'; import { BrowserModeConfigLoader, BrowserMode } from '../../../config/BrowserModeConfig'; import { getAutomationMode } from '../../../config/AutomationConfig'; import { PageStateValidator, PageStateValidation, PageStateValidationResult } from '../../../../domain/services/PageStateValidator'; import { IRacingDomNavigator } from '../dom/IRacingDomNavigator'; import { SafeClickService } from '../dom/SafeClickService'; import { IRacingDomInteractor } from '../dom/IRacingDomInteractor'; import { PlaywrightAuthSessionService } from '../auth/PlaywrightAuthSessionService'; import { IRacingPlaywrightAuthFlow } from '../auth/IRacingPlaywrightAuthFlow'; import { WizardStepOrchestrator } from './WizardStepOrchestrator'; export type AutomationAdapterMode = 'mock' | 'real'; /** * Personality messages for the automation overlay. * These add a fun, racing-themed personality to the bot. */ const OVERLAY_PERSONALITY_MESSAGES = [ "🏎️ Warming up the tires...", "🔧 Fine-tuning the setup...", "🏁 Getting ready for the green flag...", "⚡ Optimizing lap times...", "🎯 Locking in your preferences...", "🌟 Making racing dreams come true...", "🚀 Preparing for launch...", "🏆 Setting you up for victory...", "🎮 Configuring the perfect session...", "⏱️ Every millisecond counts...", "🛞 Checking tire pressures...", "📡 Syncing with race control...", "🔥 Engines are warming up...", "💨 Almost race time!", "🗺️ Plotting the racing line...", ]; /** * Step-specific messages for the overlay. * Maps step numbers to friendly descriptions. */ const OVERLAY_STEP_MESSAGES: Record = { 1: "🔐 Checking your credentials...", 2: "🏁 Creating your race session...", 3: "📝 Setting up race information...", 4: "🖥️ Configuring server details...", 5: "👥 Managing admin access...", 6: "➕ Adding admin privileges...", 7: "⏰ Setting time limits...", 8: "🚗 Selecting your cars...", 9: "🏎️ Adding your car to the grid...", 10: "🎨 Configuring car classes...", 11: "🗺️ Choosing your track...", 12: "🏟️ Adding track to session...", 13: "⚙️ Setting track options...", 14: "🌅 Configuring time of day...", 15: "🌤️ Setting weather conditions...", 16: "🌦️ Configuring track conditions...", 17: "✅ Done! Review your settings and click 'Host Race' to create your session!", }; /** * CSS styles for the automation overlay. * Styled to match iRacing's dark theme with racing accents. * Colors extracted from iRacing HTML fixtures: * - Primary dark: #12121B (iRacing brand dark) * - Gray background: #1a1a24 (gray-800 equivalent) * - Text light: rgba(255, 255, 255, 0.92) (whiteAlpha-900) * - Accent: #c8102e (iRacing red) * - Button active: #4e4e57 * - Border: rgba(183, 183, 187, 0.3) */ const OVERLAY_CSS = ` @keyframes gridpilot-pulse { 0%, 100% { opacity: 1; transform: scale(1); } 50% { opacity: 0.85; transform: scale(1.03); } } @keyframes gridpilot-spin { from { transform: rotate(0deg); } to { transform: rotate(360deg); } } @keyframes gridpilot-slide-in { from { transform: translateX(100%); opacity: 0; } to { transform: translateX(0); opacity: 1; } } @keyframes gridpilot-checkered { 0% { background-position: 0 0; } 100% { background-position: 20px 20px; } } @keyframes gridpilot-progress { 0% { background-position: 0% 50%; } 100% { background-position: 100% 50%; } } #gridpilot-overlay { position: fixed; bottom: 20px; right: 20px; width: 340px; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; z-index: 2147483647; animation: gridpilot-slide-in 0.4s ease-out; pointer-events: auto; } #gridpilot-overlay * { box-sizing: border-box; } .gridpilot-card { background: #12121B; border-radius: 4px; border: 1px solid rgba(183, 183, 187, 0.2); box-shadow: 0 8px 32px rgba(0, 0, 0, 0.6); overflow: hidden; } .gridpilot-header { background: linear-gradient(90deg, #c8102e 0%, #a00d25 100%); padding: 10px 14px; display: flex; align-items: center; gap: 10px; position: relative; overflow: hidden; } .gridpilot-header::before { content: ''; position: absolute; top: 0; left: 0; right: 0; bottom: 0; background: linear-gradient(45deg, transparent 48%, rgba(255,255,255,0.05) 49%, rgba(255,255,255,0.05) 51%, transparent 52%), linear-gradient(-45deg, transparent 48%, rgba(255,255,255,0.05) 49%, rgba(255,255,255,0.05) 51%, transparent 52%); background-size: 8px 8px; animation: gridpilot-checkered 1.5s linear infinite; opacity: 0.5; } .gridpilot-logo { font-size: 22px; animation: gridpilot-pulse 2s ease-in-out infinite; position: relative; z-index: 1; } .gridpilot-title { color: #ffffff; font-size: 13px; font-weight: 700; letter-spacing: 1.5px; text-transform: uppercase; position: relative; z-index: 1; text-shadow: 0 1px 2px rgba(0,0,0,0.3); flex: 1; } .gridpilot-btn { background: rgba(255, 255, 255, 0.15); color: #ffffff; border: 1px solid rgba(255, 255, 255, 0.3); border-radius: 3px; padding: 4px 10px; font-size: 11px; font-weight: 600; letter-spacing: 0.5px; text-transform: uppercase; cursor: pointer; position: relative; z-index: 1; transition: all 0.15s ease; } .gridpilot-btn:hover { background: rgba(255, 255, 255, 0.25); border-color: rgba(255, 255, 255, 0.5); } .gridpilot-btn:active { background: rgba(255, 255, 255, 0.35); transform: scale(0.97); } .gridpilot-btn.paused { background: #4e4e57; border-color: #ffffff; color: #ffffff; animation: gridpilot-pulse 1s ease-in-out infinite; } .gridpilot-close-btn { background: rgba(200, 16, 46, 0.6); border-color: rgba(200, 16, 46, 0.8); } .gridpilot-close-btn:hover { background: rgba(200, 16, 46, 0.8); border-color: #c8102e; } .gridpilot-close-btn:active { background: #c8102e; } .gridpilot-header-buttons { display: flex; gap: 6px; position: relative; z-index: 1; } .gridpilot-body { padding: 14px; background: #1a1a24; } .gridpilot-status { display: flex; align-items: center; gap: 12px; margin-bottom: 12px; } .gridpilot-spinner { width: 22px; height: 22px; border: 2px solid rgba(200, 16, 46, 0.3); border-top-color: #c8102e; border-radius: 50%; animation: gridpilot-spin 0.8s linear infinite; flex-shrink: 0; } .gridpilot-spinner.paused { animation-play-state: paused; border-top-color: #777880; border-color: rgba(119, 120, 128, 0.3); } .gridpilot-action-text { color: rgba(255, 255, 255, 0.92); font-size: 14px; font-weight: 500; line-height: 1.4; } .gridpilot-progress-container { margin-bottom: 12px; } .gridpilot-progress-bar { height: 4px; background: rgba(78, 78, 87, 0.5); border-radius: 2px; overflow: hidden; } .gridpilot-progress-fill { height: 100%; background: linear-gradient(90deg, #c8102e, #e8304a, #c8102e); background-size: 200% 100%; animation: gridpilot-progress 2s linear infinite; border-radius: 2px; transition: width 0.4s ease-out; } .gridpilot-progress-fill.paused { animation-play-state: paused; background: #777880; } .gridpilot-step-info { display: flex; justify-content: space-between; align-items: center; margin-top: 6px; } .gridpilot-step-text { color: rgba(255, 255, 255, 0.6); font-size: 11px; } .gridpilot-step-count { color: #c8102e; font-size: 11px; font-weight: 600; } .gridpilot-personality { color: rgba(255, 255, 255, 0.5); font-size: 11px; font-style: italic; text-align: center; padding-top: 10px; border-top: 1px solid rgba(183, 183, 187, 0.15); } .gridpilot-footer { background: #12121B; padding: 8px 14px; display: flex; align-items: center; justify-content: center; gap: 6px; border-top: 1px solid rgba(183, 183, 187, 0.1); } .gridpilot-footer-text { color: rgba(255, 255, 255, 0.4); font-size: 10px; letter-spacing: 0.5px; } .gridpilot-footer-dot { width: 4px; height: 4px; background: #c8102e; border-radius: 50%; animation: gridpilot-pulse 1.5s ease-in-out infinite; } .gridpilot-footer-dot.paused { background: #777880; animation: none; } `; /** * HTML template for the automation overlay. * Includes pause/resume button and close button for user control. */ const OVERLAY_HTML = `
GridPilot
Initializing...
Starting up... Step 0 of 17
🏁 Getting ready for the green flag...
`; export interface PlaywrightConfig { headless?: boolean; timeout?: number; baseUrl?: string; mode?: AutomationAdapterMode; /** Path to store persistent browser context (session data). Required for real mode. */ userDataDir?: string; } export class PlaywrightAutomationAdapter implements IBrowserAutomation, IAuthenticationService { private browser: Browser | null = null; private persistentContext: BrowserContext | null = null; private context: BrowserContext | null = null; private page: Page | null = null; private config: Required; private browserSession: PlaywrightBrowserSession; private connected = false; private isConnecting = false; private logger?: ILogger; private cookieStore: SessionCookieStore; private authService: PlaywrightAuthSessionService; private overlayInjected = false; private totalSteps = 17; /** Polling interval for pause check (ms) */ private static readonly PAUSE_CHECK_INTERVAL = 300; /** Checkout confirmation callback - called before clicking checkout button */ private checkoutConfirmationCallback?: (price: CheckoutPrice, state: CheckoutState) => Promise; /** Page state validator instance */ private pageStateValidator: PageStateValidator; private navigator!: IRacingDomNavigator; private safeClickService!: SafeClickService; private domInteractor!: IRacingDomInteractor; private readonly stepOrchestrator: WizardStepOrchestrator; constructor(config: PlaywrightConfig = {}, logger?: ILogger, browserModeLoader?: BrowserModeConfigLoader) { this.config = { headless: true, timeout: 10000, baseUrl: '', mode: 'mock', userDataDir: '', ...config, }; this.logger = logger; this.cookieStore = new SessionCookieStore(this.config.userDataDir, logger); this.pageStateValidator = new PageStateValidator(); this.browserSession = new PlaywrightBrowserSession(this.config, logger, browserModeLoader); const authFlow = new IRacingPlaywrightAuthFlow(logger); this.authService = new PlaywrightAuthSessionService( this.browserSession, this.cookieStore, authFlow, logger, { navigationTimeoutMs: IRACING_TIMEOUTS.navigation, loginWaitTimeoutMs: IRACING_TIMEOUTS.loginWait, }, ); this.safeClickService = new SafeClickService(this.config, this.browserSession, logger); this.navigator = new IRacingDomNavigator(this.config, this.browserSession, logger, async () => { await this.closeBrowserContext(); }); this.domInteractor = new IRacingDomInteractor(this.config, this.browserSession, this.safeClickService, logger); this.stepOrchestrator = new WizardStepOrchestrator({ config: this.config, browserSession: this.browserSession, navigator: this.navigator, interactor: this.domInteractor, authService: this.authService, logger: this.logger, totalSteps: this.totalSteps, getCheckoutConfirmationCallback: () => this.checkoutConfirmationCallback, overlay: { updateOverlay: (step, customMessage) => this.updateOverlay(step, customMessage), showOverlayComplete: (success, message) => this.showOverlayComplete(success, message), }, debug: { saveProactiveDebugInfo: (step) => this.saveProactiveDebugInfo(step), saveDebugInfo: (stepName, error) => this.saveDebugInfo(stepName, error), }, guards: { waitIfPaused: () => this.waitIfPaused(), checkAndHandleClose: () => this.checkAndHandleClose(), dismissModals: () => this.dismissModals(), dismissDatetimePickers: () => this.dismissDatetimePickers(), }, helpers: { handleLogin: () => this.handleLogin(), validatePageState: (validation) => this.validatePageState(validation), handleCheckoutConfirmation: () => this.handleCheckoutConfirmation(), }, }); } // Lifecycle emitter support (minimal, deterministic events) private lifecycleCallbacks: Set = new Set() onLifecycle(cb: any): void { this.lifecycleCallbacks.add(cb) } offLifecycle(cb: any): void { this.lifecycleCallbacks.delete(cb) } private async emitLifecycle(event: any): Promise { try { for (const cb of Array.from(this.lifecycleCallbacks)) { try { await cb(event) } catch (e) { this.log('debug', 'Lifecycle callback error', { error: String(e) }) } } } catch (e) { this.log('debug', 'emitLifecycle failed', { error: String(e) }) } } /** * Minimal attachPanel helper for tests that simulates deterministic lifecycle events. * Emits 'panel-attached' and then 'action-started' immediately for deterministic tests. */ async attachPanel(page?: Page, actionId?: string): Promise { const selector = '#gridpilot-overlay' await this.emitLifecycle({ type: 'panel-attached', actionId, timestamp: Date.now(), payload: { selector } }) await this.emitLifecycle({ type: 'action-started', actionId, timestamp: Date.now() }) } private isRealMode(): boolean { return this.config.mode === 'real'; } /** * Validate that the current page state matches expected conditions. * Uses the PageStateValidator domain service to check selector presence/absence. * * @param validation Expected page state configuration * @returns Result with validation outcome */ async validatePageState(validation: PageStateValidation): Promise> { if (!this.page) { return Result.err(new Error('Browser not connected')); } try { // Create a function that checks if selectors exist on the page const checkSelector = (selector: string): boolean => { // Synchronously check if selector exists (count > 0) // We'll need to make this sync-compatible, so we check in the validator call return false; // Placeholder - will be resolved in evaluate }; // Use page.evaluate to check all selectors at once in the browser context const selectorChecks = await this.page.evaluate( ({ requiredSelectors, forbiddenSelectors }) => { const results: Record = {}; // Check required selectors for (const selector of requiredSelectors) { try { results[selector] = document.querySelectorAll(selector).length > 0; } catch { results[selector] = false; } } // Check forbidden selectors for (const selector of forbiddenSelectors || []) { try { results[selector] = document.querySelectorAll(selector).length > 0; } catch { results[selector] = false; } } return results; }, { requiredSelectors: validation.requiredSelectors, forbiddenSelectors: validation.forbiddenSelectors || [] } ); // Create actualState function that uses the captured results const actualState = (selector: string): boolean => { return selectorChecks[selector] === true; }; // Validate using domain service return this.pageStateValidator.validateStateEnhanced(actualState, validation, this.isRealMode()); } catch (error) { return Result.err( error instanceof Error ? error : new Error(`Page state validation failed: ${String(error)}`) ); } } /** Maximum number of "before" debug snapshots to keep */ private static readonly MAX_BEFORE_SNAPSHOTS = 5; private log(level: 'debug' | 'info' | 'warn' | 'error', message: string, context?: Record): void { if (!this.logger) { return; } const logger: any = this.logger; logger[level](message, context as any); } private syncSessionStateFromBrowser(): void { this.browser = this.browserSession.getBrowser(); this.persistentContext = this.browserSession.getPersistentContext(); this.context = this.browserSession.getContext(); this.page = this.browserSession.getPage(); this.connected = this.browserSession.isConnected(); } async connect(forceHeaded: boolean = false): Promise { const result = await this.browserSession.connect(forceHeaded); if (!result.success) { return { success: false, error: result.error }; } this.syncSessionStateFromBrowser(); if (this.config.mode === 'mock' && this.config.baseUrl) { try { const fixture = getFixtureForStep(1); if (fixture) { const base = this.config.baseUrl.replace(/\/$/, ''); await this.navigator.navigateToPage(`${base}/${fixture}`); } } catch (error) { this.log('debug', 'Initial fixture navigation failed (mock mode)', { error: String(error), }); } } return { success: true }; } /** * Ensure browser context is ready for automation. * This is a wrapper around connect() that allows forcing headed mode. * * @param forceHeaded If true, override browser mode to 'headed' (visible browser) */ private async ensureBrowserContext(forceHeaded: boolean = false): Promise { await this.browserSession.ensureBrowserContext(forceHeaded); this.syncSessionStateFromBrowser(); } /** * Clean up stale SingletonLock file if it exists and the owning process is not running. * On Unix systems, SingletonLock is a symbolic link pointing to a socket file. * If the browser crashed or was force quit, this file remains and blocks new launches. */ private async cleanupStaleLockFile(userDataDir: string): Promise { const singletonLockPath = path.join(userDataDir, 'SingletonLock'); try { // Check if lock file exists if (!fs.existsSync(singletonLockPath)) { return; // No lock file, we're good } this.log('info', 'Found existing SingletonLock, attempting cleanup', { path: singletonLockPath }); // Try to remove the lock file // On Unix, SingletonLock is typically a symlink, so unlink works for both files and symlinks fs.unlinkSync(singletonLockPath); this.log('info', 'Cleaned up stale SingletonLock file'); } catch (error) { // If we can't remove it, the browser might actually be running // Log warning but continue - Playwright will give us a proper error if it's actually locked this.log('warn', 'Could not clean up SingletonLock', { error: String(error) }); } } async disconnect(): Promise { await this.browserSession.disconnect(); this.browser = null; this.context = null; this.persistentContext = null; this.page = null; this.connected = false; } isConnected(): boolean { this.connected = this.browserSession.isConnected(); this.page = this.browserSession.getPage(); return this.connected && this.page !== null; } async navigateToPage(url: string): Promise { const result = await this.navigator.navigateToPage(url); if (result.success) { // Reset overlay state after successful navigation (page context changed) this.resetOverlayState(); } return result; } async fillFormField(fieldName: string, value: string): Promise { return this.domInteractor.fillFormField(fieldName, value); } private getFieldSelector(fieldName: string): string { const fieldMap: Record = { sessionName: `${IRACING_SELECTORS.steps.sessionName}, ${IRACING_SELECTORS.steps.sessionNameAlt}`, password: `${IRACING_SELECTORS.steps.password}, ${IRACING_SELECTORS.steps.passwordAlt}`, description: `${IRACING_SELECTORS.steps.description}, ${IRACING_SELECTORS.steps.descriptionAlt}`, adminSearch: IRACING_SELECTORS.steps.adminSearch, carSearch: IRACING_SELECTORS.steps.carSearch, trackSearch: IRACING_SELECTORS.steps.trackSearch, maxDrivers: IRACING_SELECTORS.steps.maxDrivers, }; return fieldMap[fieldName] || IRACING_SELECTORS.fields.textInput; } async clickElement(target: string): Promise { return this.domInteractor.clickElement(target); } private getActionSelector(action: string): string { // If already a selector, return as-is if (action.startsWith('[') || action.startsWith('button') || action.startsWith('#')) { return action; } const actionMap: Record = { next: IRACING_SELECTORS.wizard.nextButton, back: IRACING_SELECTORS.wizard.backButton, confirm: IRACING_SELECTORS.wizard.confirmButton, cancel: IRACING_SELECTORS.wizard.cancelButton, create: IRACING_SELECTORS.hostedRacing.createRaceButton, close: IRACING_SELECTORS.wizard.closeButton, }; return actionMap[action] || `button:has-text("${action}")`; } async waitForElement(target: string, maxWaitMs?: number): Promise { return this.navigator.waitForElement(target, maxWaitMs); } async handleModal(stepId: StepId, action: string): Promise { return this.domInteractor.handleModal(stepId, action); } async executeStep(stepId: StepId, config: Record): Promise { const stepNumber = stepId.value; if (!this.isRealMode() && this.config.baseUrl) { if (stepNumber >= 2 && stepNumber <= this.totalSteps) { try { const fixture = getFixtureForStep(stepNumber); if (fixture) { const base = this.config.baseUrl.replace(/\/$/, ''); const url = `${base}/${fixture}`; this.log('debug', 'Mock mode: navigating to fixture for step', { step: stepNumber, url, }); await this.navigator.navigateToPage(url); } } catch (error) { this.log('debug', 'Mock mode fixture navigation failed (non-fatal)', { step: stepNumber, error: String(error), }); } } } return this.stepOrchestrator.executeStep(stepId, config); } /** * Step-to-Page mapping for wizard auto-skip detection. * Maps step numbers to their corresponding wizard page names. */ private static readonly STEP_TO_PAGE_MAP: Record = { 7: 'timeLimit', 8: 'cars', 9: 'cars', 10: 'carClasses', 11: 'track', 12: 'track', 13: 'trackOptions', 14: 'timeOfDay', 15: 'weather', 16: 'raceOptions', 17: 'trackConditions', }; /** * Detect which wizard page is currently displayed by checking container existence. * Returns the page name (e.g., 'cars', 'track') or null if no page is detected. * * This method checks each step container from IRACING_SELECTORS.wizard.stepContainers * and returns the first one that exists in the DOM. * * @returns Page name or null if unknown */ private async detectCurrentWizardPage(): Promise { if (!this.page) { return null; } try { // Check each container in stepContainers map const containers = IRACING_SELECTORS.wizard.stepContainers; for (const [pageName, selector] of Object.entries(containers)) { const count = await this.page.locator(selector).count(); if (count > 0) { this.log('debug', 'Detected wizard page', { pageName, selector }); return pageName; } } // No container found this.log('debug', 'No wizard page detected'); return null; } catch (error) { this.log('debug', 'Error detecting wizard page', { error: String(error) }); return null; } } /** * Synchronize step counter with actual wizard state. * Calculates the skip offset when wizard auto-skips steps (e.g., 8→11). * * @param expectedStep The step number we're trying to execute * @param actualPage The actual wizard page detected (from detectCurrentWizardPage) * @returns Skip offset (0 if no skip, >0 if steps were skipped) */ private synchronizeStepCounter(expectedStep: number, actualPage: string | null): number { if (!actualPage) { return 0; // Unknown state, no skip } // Find which step number corresponds to the actual page let actualStep: number | null = null; for (const [step, pageName] of Object.entries(PlaywrightAutomationAdapter.STEP_TO_PAGE_MAP)) { if (pageName === actualPage) { actualStep = parseInt(step, 10); break; // Use first match } } if (actualStep === null) { return 0; // Unknown page, no skip } // Calculate skip offset const skipOffset = actualStep - expectedStep; if (skipOffset > 0) { // Wizard skipped ahead - log warning with skipped step numbers const skippedSteps: number[] = []; for (let i = expectedStep; i < actualStep; i++) { skippedSteps.push(i); } this.log('warn', 'Wizard auto-skip detected', { expectedStep, actualStep, skipOffset, skippedSteps, }); return skipOffset; } // No skip or backward navigation return 0; } /** * Save debug information (screenshot and HTML) when a step fails. * Files are saved to debug-screenshots/ directory with timestamp. * Returns the paths of saved files for inclusion in error messages. * * Error dumps are always kept and not subject to cleanup. */ private async saveDebugInfo(stepName: string, error: Error): Promise<{ screenshotPath?: string; htmlPath?: string }> { if (!this.page) return {}; const timestamp = new Date().toISOString().replace(/[:.]/g, '-'); const baseName = `debug-error-${stepName}-${timestamp}`; const debugDir = path.join(process.cwd(), 'debug-screenshots'); const result: { screenshotPath?: string; htmlPath?: string } = {}; try { await fs.promises.mkdir(debugDir, { recursive: true }); const screenshotPath = path.join(debugDir, `${baseName}.png`); await this.page.screenshot({ path: screenshotPath, fullPage: true }); result.screenshotPath = screenshotPath; this.log('error', `Error debug screenshot saved: ${screenshotPath}`, { path: screenshotPath, error: error.message, }); const htmlPath = path.join(debugDir, `${baseName}.html`); const html = await this.page.evaluate(() => { const root = document.documentElement.cloneNode(true) as HTMLElement; ['script', 'noscript', 'meta', 'base', 'style', 'link', 'iframe', 'picture', 'source', 'svg', 'path', 'img', 'canvas', 'video', 'audio'] .forEach((sel) => root.querySelectorAll(sel).forEach((n) => n.remove())); root.querySelectorAll('*').forEach((n) => { const text = (n.textContent || '').trim(); const interactive = n.matches('a,button,input,select,textarea,option,label'); if (!interactive && text === '' && n.children.length === 0) { n.remove(); } }); return '\n' + root.outerHTML; }); await fs.promises.writeFile(htmlPath, html); result.htmlPath = htmlPath; this.log('error', `Error debug HTML saved: ${htmlPath}`, { path: htmlPath }); } catch (e) { this.log('warn', 'Failed to save error debug info', { error: String(e) }); } return result; } /** * Save proactive debug information BEFORE step execution. * This ensures we always have the most recent state even if the browser is closed via Ctrl+C. * * Files are named with "before-step-N" prefix and old snapshots are cleaned up * to avoid disk bloat (keeps only last MAX_BEFORE_SNAPSHOTS). */ private async saveProactiveDebugInfo(step: number): Promise<{ screenshotPath?: string; htmlPath?: string }> { if (!this.page) return {}; const timestamp = new Date().toISOString().replace(/[:.]/g, '-'); const baseName = `debug-before-step-${step}-${timestamp}`; const debugDir = path.join(process.cwd(), 'debug-screenshots'); const result: { screenshotPath?: string; htmlPath?: string } = {}; try { await fs.promises.mkdir(debugDir, { recursive: true }); await this.cleanupOldBeforeSnapshots(debugDir); const screenshotPath = path.join(debugDir, `${baseName}.png`); await this.page.screenshot({ path: screenshotPath, fullPage: true }); result.screenshotPath = screenshotPath; this.log('info', `Pre-step screenshot saved: ${screenshotPath}`, { path: screenshotPath, step, }); const htmlPath = path.join(debugDir, `${baseName}.html`); const html = await this.page.evaluate(() => { const root = document.documentElement.cloneNode(true) as HTMLElement; ['script', 'noscript', 'meta', 'base', 'style', 'link', 'iframe', 'picture', 'source', 'svg', 'path', 'img', 'canvas', 'video', 'audio'] .forEach((sel) => root.querySelectorAll(sel).forEach((n) => n.remove())); root.querySelectorAll('*').forEach((n) => { const text = (n.textContent || '').trim(); const interactive = n.matches('a,button,input,select,textarea,option,label'); if (!interactive && text === '' && n.children.length === 0) { n.remove(); } }); return '\n' + root.outerHTML; }); await fs.promises.writeFile(htmlPath, html); result.htmlPath = htmlPath; this.log('info', `Pre-step HTML saved: ${htmlPath}`, { path: htmlPath, step }); } catch (e) { this.log('warn', 'Failed to save proactive debug info', { error: String(e), step }); } return result; } /** * Clean up old "before" debug snapshots to avoid disk bloat. * Keeps only the last MAX_BEFORE_SNAPSHOTS files (pairs of .png and .html). * * Error dumps (prefixed with "debug-error-") are never deleted. */ private async cleanupOldBeforeSnapshots(debugDir: string): Promise { try { const files = await fs.promises.readdir(debugDir); // Filter to only "before" snapshot files (not error dumps) const beforeFiles = files.filter(f => f.startsWith('debug-before-step-')); // Group by base name (without extension) to handle .png/.html pairs const baseNames = new Set(); for (const file of beforeFiles) { // Remove extension to get base name const baseName = file.replace(/\.(png|html)$/, ''); baseNames.add(baseName); } // Sort by timestamp (embedded in filename) - oldest first const sortedBaseNames = Array.from(baseNames).sort(); // Calculate how many pairs to delete const pairsToDelete = sortedBaseNames.length - PlaywrightAutomationAdapter.MAX_BEFORE_SNAPSHOTS; if (pairsToDelete > 0) { const baseNamesToDelete = sortedBaseNames.slice(0, pairsToDelete); for (const baseName of baseNamesToDelete) { // Delete both .png and .html files const pngPath = path.join(debugDir, `${baseName}.png`); const htmlPath = path.join(debugDir, `${baseName}.html`); try { if (fs.existsSync(pngPath)) { await fs.promises.unlink(pngPath); this.log('debug', `Cleaned up old snapshot: ${pngPath}`); } } catch { // Ignore deletion errors } try { if (fs.existsSync(htmlPath)) { await fs.promises.unlink(htmlPath); this.log('debug', `Cleaned up old snapshot: ${htmlPath}`); } } catch { // Ignore deletion errors } } this.log('debug', `Cleaned up ${pairsToDelete} old before-step snapshot pairs`); } } catch (e) { // Don't fail if cleanup fails this.log('debug', 'Failed to cleanup old snapshots', { error: String(e) }); } } /** * Dismiss any visible Chakra UI modal popups or datetime pickers that might block interactions. * This handles various modal dismiss patterns including close buttons and overlay clicks. * Also handles React DateTime picker (rdt) popups that can intercept pointer events. * Optimized for speed - uses instant visibility checks and minimal waits. */ private async dismissModals(): Promise { if (!this.page) return; try { // Check for Chakra UI modals (do NOT use this for datetime pickers - see dismissDatetimePickers) const modalContainer = this.page.locator('.chakra-modal__content-container, .modal-content'); const isModalVisible = await modalContainer.isVisible().catch(() => false); if (!isModalVisible) { this.log('debug', 'No modal visible, continuing'); return; } this.log('info', 'Modal detected, dismissing immediately'); // Try clicking Continue/Close/OK button with very short timeout const dismissButton = this.page.locator( '.chakra-modal__content-container button[aria-label="Continue"], ' + '.chakra-modal__content-container button:has-text("Continue"), ' + '.chakra-modal__content-container button:has-text("Close"), ' + '.chakra-modal__content-container button:has-text("OK"), ' + '.chakra-modal__close-btn, ' + '[aria-label="Close"]' ).first(); // Instant visibility check if (await dismissButton.isVisible().catch(() => false)) { this.log('info', 'Clicking modal dismiss button'); await dismissButton.click({ force: true, timeout: 1000 }); // Brief wait for modal to close (100ms) await this.page.waitForTimeout(100); return; } // No dismiss button found — do NOT press Escape because ESC commonly closes the entire wizard. // To avoid accidentally dismissing the race creation modal, log and return instead. this.log('debug', 'No dismiss button found, skipping Escape to avoid closing wizard'); await this.page.waitForTimeout(100); return; } catch (error) { this.log('debug', 'Modal dismiss error (non-critical)', { error: String(error) }); } } /** * Dismiss any open React DateTime pickers (rdt component). * These pickers can intercept pointer events and block clicks on other elements. * Used specifically before navigating away from steps that have datetime pickers. * * IMPORTANT: Do NOT use Escape key as it closes the entire wizard modal in iRacing. * * Strategy (in order of aggressiveness): * 1. Use JavaScript to remove 'rdtOpen' class directly (most reliable) * 2. Click outside the picker on the modal body * 3. Blur the active element */ private async dismissDatetimePickers(): Promise { if (!this.page) return; try { // Check for open datetime pickers (rdt component with class 'rdtOpen') const initialCount = await this.page.locator('.rdt.rdtOpen').count(); if (initialCount === 0) { this.log('debug', 'No datetime picker open'); return; } this.log('info', `Closing ${initialCount} open datetime picker(s)`); // Strategy 1: Use JavaScript to directly remove rdtOpen class // This is the most reliable method as it doesn't require clicking await this.page.evaluate(() => { const openPickers = document.querySelectorAll('.rdt.rdtOpen'); openPickers.forEach((picker) => { picker.classList.remove('rdtOpen'); }); // Also blur any focused inputs to prevent re-opening const activeEl = document.activeElement as HTMLElement; if (activeEl && activeEl.blur && activeEl.closest('.rdt')) { activeEl.blur(); } }); await this.page.waitForTimeout(50); // Verify pickers are closed let stillOpenCount = await this.page.locator('.rdt.rdtOpen').count(); if (stillOpenCount === 0) { this.log('debug', 'Datetime pickers closed via JavaScript'); return; } // Strategy 2: Click on the modal body outside the picker // This simulates clicking elsewhere to close the dropdown this.log('debug', `${stillOpenCount} picker(s) still open, clicking outside`); const modalBody = this.page.locator(IRACING_SELECTORS.wizard.modalContent).first(); if (await modalBody.isVisible().catch(() => false)) { // Click at a safe spot - the header area of the card const cardHeader = this.page.locator(`${IRACING_SELECTORS.wizard.stepContainers.timeOfDay} .card-header`).first(); if (await cardHeader.isVisible().catch(() => false)) { await cardHeader.click({ force: true, timeout: 1000 }).catch(() => { }); await this.page.waitForTimeout(100); } } // Check again stillOpenCount = await this.page.locator('.rdt.rdtOpen').count(); if (stillOpenCount === 0) { this.log('debug', 'Datetime pickers closed via click outside'); return; } // Strategy 3: Force blur on all datetime inputs this.log('debug', `${stillOpenCount} picker(s) still open, force blur`); await this.page.evaluate(() => { // Blur all inputs inside rdt containers const rtdInputs = document.querySelectorAll('.rdt input'); rtdInputs.forEach((input) => { (input as HTMLElement).blur(); }); // Also force remove rdtOpen class again (in case React re-added it) const openPickers = document.querySelectorAll('.rdt.rdtOpen'); openPickers.forEach((picker) => { picker.classList.remove('rdtOpen'); // Also hide the picker element directly const pickerDropdown = picker.querySelector('.rdtPicker') as HTMLElement; if (pickerDropdown) { pickerDropdown.style.display = 'none'; } }); }); await this.page.waitForTimeout(50); // Final check const finalCount = await this.page.locator('.rdt.rdtOpen').count(); if (finalCount > 0) { this.log('warn', `Could not close ${finalCount} datetime picker(s), will attempt click with force`); } else { this.log('debug', 'Datetime picker dismiss complete'); } } catch (error) { this.log('debug', 'Datetime picker dismiss error (non-critical)', { error: String(error) }); } } /** * Check if a selector or element text matches blocked patterns (checkout/payment buttons). * SAFETY CRITICAL: This prevents accidental purchases during automation. * * @param selector The CSS selector being clicked * @param elementText Optional text content of the element (should be direct text only) * @returns true if the selector/text matches a blocked pattern */ private isBlockedSelector(selector: string, elementText?: string): boolean { const selectorLower = selector.toLowerCase(); const textLower = elementText?.toLowerCase().trim() ?? ''; // Check if selector contains any blocked keywords for (const keyword of BLOCKED_KEYWORDS) { if (selectorLower.includes(keyword) || textLower.includes(keyword)) { return true; } } // Check for price indicators (e.g., "$0.50", "$19.99") // IMPORTANT: Only block if the price is combined with a checkout-related action word // This prevents false positives when price is merely displayed on the page const pricePattern = /\$\d+\.\d{2}/; const hasPrice = pricePattern.test(textLower) || pricePattern.test(selector); if (hasPrice) { // Only block if text also contains checkout-related words const checkoutActionWords = ['check', 'out', 'buy', 'purchase', 'pay', 'cart']; const hasCheckoutWord = checkoutActionWords.some(word => textLower.includes(word)); if (hasCheckoutWord) { return true; } } // Check for cart icon class if (selectorLower.includes('icon-cart') || selectorLower.includes('cart-icon')) { return true; } return false; } /** * Verify an element is not a blocked checkout/payment button before clicking. * SAFETY CRITICAL: Throws error if element matches blocked patterns. * * This method checks: * 1. The selector string itself for blocked patterns * 2. The element's DIRECT text content (not children/siblings) * 3. The element's class, id, and href attributes for checkout indicators * 4. Whether the element matches any blocked CSS selectors * * @param selector The CSS selector of the element to verify * @throws Error if element is a blocked checkout/payment button */ private async verifyNotBlockedElement(selector: string): Promise { if (!this.page) return; // In mock mode we bypass safety blocking to allow tests to exercise checkout flows // without risking real-world purchases. Safety checks remain active in 'real' mode. if (!this.isRealMode()) { this.log('debug', 'Mock mode detected - skipping checkout blocking checks', { selector }); return; } // First check the selector itself if (this.isBlockedSelector(selector)) { const errorMsg = `🚫 BLOCKED: Selector "${selector}" matches checkout/payment pattern. Automation stopped for safety.`; this.log('error', errorMsg); throw new Error(errorMsg); } // Try to get the element's attributes and direct text for verification try { const element = this.page.locator(selector).first(); const isVisible = await element.isVisible().catch(() => false); if (isVisible) { // Get element attributes for checking const elementClass = await element.getAttribute('class').catch(() => '') ?? ''; const elementId = await element.getAttribute('id').catch(() => '') ?? ''; const elementHref = await element.getAttribute('href').catch(() => '') ?? ''; // Check class/id/href for checkout indicators const attributeText = `${elementClass} ${elementId} ${elementHref}`.toLowerCase(); if (attributeText.includes('checkout') || attributeText.includes('cart') || attributeText.includes('purchase') || attributeText.includes('payment')) { const errorMsg = `🚫 BLOCKED: Element attributes contain checkout pattern. Class="${elementClass}", ID="${elementId}", Href="${elementHref}". Automation stopped for safety.`; this.log('error', errorMsg); throw new Error(errorMsg); } // Get ONLY the direct text of this element, excluding child element text // This prevents false positives when a checkout button exists elsewhere on the page const directText = await element.evaluate((el) => { // Get only direct text nodes, not text from child elements let text = ''; const childNodes = Array.from(el.childNodes); for (let i = 0; i < childNodes.length; i++) { const node = childNodes[i]; if (node.nodeType === Node.TEXT_NODE) { text += node.textContent || ''; } } return text.trim(); }).catch(() => ''); // Also get innerText as fallback (for buttons with icon + text structure) // But only check if directText is empty or very short let textToCheck = directText; if (directText.length < 3) { // For elements like // We need innerText but should be careful about what we capture const innerText = await element.innerText().catch(() => ''); // Only use innerText if it's reasonably short (not entire page sections) if (innerText.length < 100) { textToCheck = innerText.trim(); } } this.log('debug', 'Checking element text for blocked patterns', { selector, directText, textToCheck, elementClass, }); if (textToCheck && this.isBlockedSelector('', textToCheck)) { const errorMsg = `🚫 BLOCKED: Element text "${textToCheck}" matches checkout/payment pattern. Automation stopped for safety.`; this.log('error', errorMsg); throw new Error(errorMsg); } // Check if element matches any of the blocked selectors directly for (const blockedSelector of Object.values(IRACING_SELECTORS.BLOCKED_SELECTORS)) { const matchesBlocked = await element.evaluate((el, sel) => { try { return el.matches(sel) || el.closest(sel) !== null; } catch { return false; } }, blockedSelector).catch(() => false); if (matchesBlocked) { const errorMsg = `🚫 BLOCKED: Element matches blocked selector "${blockedSelector}". Automation stopped for safety.`; this.log('error', errorMsg); throw new Error(errorMsg); } } } } catch (error) { // If error is our blocked error, re-throw it if (error instanceof Error && error.message.includes('BLOCKED')) { throw error; } // Otherwise ignore - element might not exist yet, safeClick will handle that this.log('debug', 'Could not verify element (may not exist yet)', { selector, error: String(error) }); } } /** * Safe click wrapper that handles modal interception errors with auto-retry. * If a click fails because a modal is intercepting pointer events, this method * will dismiss the modal and retry the click operation. * * SAFETY: Before any click, verifies the target is not a checkout/payment button. * * @param selector The CSS selector of the element to click * @param options Click options including timeout and force * @returns Promise that resolves when click succeeds or throws after max retries */ private async safeClick(selector: string, options?: { timeout?: number; force?: boolean }): Promise { if (!this.page) { throw new Error('Browser not connected'); } // In mock mode, ensure mock fixtures are visible (remove 'hidden' flags) if (!this.isRealMode()) { try { await this.page.evaluate(() => { document.querySelectorAll('.wizard-step.hidden, .modal.hidden, .wizard-step[hidden]').forEach(el => { el.classList.remove('hidden'); el.removeAttribute('hidden'); }); }); } catch { // ignore any evaluation errors in test environments } } // SAFETY CHECK: Verify this is not a checkout/payment button await this.verifyNotBlockedElement(selector); const maxRetries = 3; const timeout = options?.timeout ?? this.config.timeout; for (let attempt = 1; attempt <= maxRetries; attempt++) { try { // On final attempt, use force: true if datetime picker issues detected const useForce = options?.force || attempt === maxRetries; await this.page.click(selector, { timeout, force: useForce }); return; // Success } catch (error) { // Re-throw blocked errors immediately if (error instanceof Error && error.message.includes('BLOCKED')) { throw error; } const errorMessage = String(error); // Check if a modal or datetime picker is intercepting the click if ( errorMessage.includes('intercepts pointer events') || errorMessage.includes('chakra-modal') || errorMessage.includes('chakra-portal') || errorMessage.includes('rdtDay') || errorMessage.includes('rdtPicker') || errorMessage.includes('rdt') ) { this.log('info', `Element intercepting click (attempt ${attempt}/${maxRetries}), dismissing...`, { selector, attempt, maxRetries, }); // Try dismissing datetime pickers first (common cause of interception) await this.dismissDatetimePickers(); // Then try dismissing modals await this.dismissModals(); await this.page.waitForTimeout(200); // Brief wait for DOM to settle if (attempt === maxRetries) { // Last attempt already tried with force: true, so if we're here it really failed this.log('warn', 'Max retries reached, attempting JS click fallback', { selector }); try { // Attempt a direct DOM click as a final fallback. This bypasses Playwright visibility checks. const clicked = await this.page.evaluate((sel) => { try { const el = document.querySelector(sel) as HTMLElement | null; if (!el) return false; // Scroll into view and click el.scrollIntoView({ block: 'center', inline: 'center' }); // Some anchors/buttons may require triggering pointer events el.click(); return true; } catch { return false; } }, selector); if (clicked) { this.log('info', 'JS fallback click succeeded', { selector }); return; } else { this.log('debug', 'JS fallback click did not find element or failed', { selector }); } } catch (e) { this.log('debug', 'JS fallback click error', { selector, error: String(e) }); } this.log('error', 'Max retries reached, click still blocked', { selector }); throw error; // Give up after max retries } // Continue to retry } else { // Different error, don't retry throw error; } } } } /** * Select weather type via Chakra UI radio button. * iRacing's modern UI uses a radio group with options: * - "Static Weather" (value: 2, checked by default) * - "Forecasted weather" (value: 1) * - "Timeline editor" (value: 3) * * @param weatherType The weather type to select (e.g., "static", "forecasted", "timeline", or the value) */ private async selectWeatherType(weatherType: string): Promise { if (!this.page) { throw new Error('Browser not connected'); } try { this.log('info', 'Selecting weather type via radio button', { weatherType }); // Map common weather type names to selectors const weatherTypeLower = weatherType.toLowerCase(); let labelSelector: string; if (weatherTypeLower.includes('static') || weatherType === '2') { labelSelector = 'label.chakra-radio:has-text("Static Weather")'; } else if (weatherTypeLower.includes('forecast') || weatherType === '1') { labelSelector = 'label.chakra-radio:has-text("Forecasted weather")'; } else if (weatherTypeLower.includes('timeline') || weatherTypeLower.includes('custom') || weatherType === '3') { labelSelector = 'label.chakra-radio:has-text("Timeline editor")'; } else { // Default to static weather labelSelector = 'label.chakra-radio:has-text("Static Weather")'; this.log('warn', `Unknown weather type "${weatherType}", defaulting to Static Weather`); } // Check if radio group exists (weather step might be optional) const radioGroup = this.page.locator('[role="radiogroup"]').first(); const exists = await radioGroup.count() > 0; if (!exists) { this.log('debug', 'Weather radio group not found, step may be optional'); return; } // Click the radio button label const radioLabel = this.page.locator(labelSelector).first(); const isVisible = await radioLabel.isVisible().catch(() => false); if (isVisible) { await radioLabel.click({ timeout: IRACING_TIMEOUTS.elementWait }); this.log('info', 'Selected weather type', { weatherType, selector: labelSelector }); } else { this.log('debug', 'Weather type radio not visible, may already be selected or step is different'); } } catch (error) { const message = error instanceof Error ? error.message : String(error); this.log('warn', 'Could not select weather type (non-critical)', { error: message, weatherType }); // Don't throw - weather type selection is optional } } /** * Check if the "Add Admin" modal is currently visible. * This modal appears when the user clicks "Add Admin" on the Admins step. * @returns true if the admin modal is visible, false otherwise */ private async isAdminModalVisible(): Promise { if (!this.page) return false; try { // Look for a modal with admin-related content // The admin modal should have a search input and be separate from the main wizard modal const adminModalSelector = '#set-admins .modal, .modal:has(input[placeholder*="Search"]):has-text("Admin")'; const isVisible = await this.page.locator(adminModalSelector).first().isVisible().catch(() => false); return isVisible; } catch { return false; } } /** * Check if the "Add Car" modal is currently visible. * This modal appears when the user clicks "Add Car" on the Cars step. * @returns true if the car modal is visible, false otherwise */ private async isCarModalVisible(): Promise { if (!this.page) return false; try { // Look for a modal with car-related content // The car modal should have a search input and be part of the set-cars step const carModalSelector = '#set-cars .modal, .modal:has(input[placeholder*=\"Search\"]):has-text(\"Car\")'; const isVisible = await this.page.locator(carModalSelector).first().isVisible().catch(() => false); return isVisible; } catch { return false; } } /** * Check if the "Add Track" modal is currently visible. * This modal appears when the user clicks "Add Track" on the Track step. * @returns true if the track modal is visible, false otherwise */ private async isTrackModalVisible(): Promise { if (!this.page) return false; try { // Look for a modal with track-related content // The track modal should have a search input and be part of the set-track step const trackModalSelector = '#set-track .modal, .modal:has(input[placeholder*=\"Search\"]):has-text(\"Track\")'; const isVisible = await this.page.locator(trackModalSelector).first().isVisible().catch(() => false); return isVisible; } catch { return false; } } /** * Click the "Add Car" button to open the Add Car modal. * This button is located on the Set Cars step (Step 8). */ private async clickAddCarButton(): Promise { if (!this.page) { throw new Error('Browser not connected'); } const addCarButtonSelector = this.isRealMode() ? IRACING_SELECTORS.steps.addCarButton : '[data-action="add-car"]'; try { this.log('info', 'Clicking Add Car button to open modal'); // Use 'attached' instead of 'visible' because iRacing wizard steps have class="hidden" await this.page.waitForSelector(addCarButtonSelector, { state: 'attached', timeout: this.isRealMode() ? IRACING_TIMEOUTS.elementWait : this.config.timeout, }); await this.safeClick(addCarButtonSelector); this.log('info', 'Clicked Add Car button'); } catch (error) { const message = error instanceof Error ? error.message : String(error); this.log('error', 'Could not click Add Car button', { error: message }); throw new Error(`Failed to click Add Car button: ${message}`); } } /** * Wait for the Add Car modal to appear. */ private async waitForAddCarModal(): Promise { if (!this.page) { throw new Error('Browser not connected'); } try { this.log('debug', 'Waiting for Add Car modal to appear (primary selector)'); // Wait for modal container - expanded selector list to tolerate UI variants const modalSelector = IRACING_SELECTORS.steps.addCarModal; await this.page.waitForSelector(modalSelector, { state: 'attached', timeout: this.isRealMode() ? IRACING_TIMEOUTS.elementWait : this.config.timeout, }); // Brief pause for modal animation (reduced from 300ms) await this.page.waitForTimeout(150); this.log('info', 'Add Car modal is visible', { selector: modalSelector }); } catch (error) { const message = error instanceof Error ? error.message : String(error); this.log('warn', 'Add Car modal not found with primary selector, dumping #create-race-wizard innerHTML and retrying', { error: message }); const html = await this.page!.innerHTML('#create-race-wizard').catch(() => ''); this.log('debug', 'create-race-wizard innerHTML (truncated)', { html: html ? html.slice(0, 2000) : '' }); this.log('info', 'Retrying wait for Add Car modal with extended timeout'); try { const modalSelectorRetry = IRACING_SELECTORS.steps.addCarModal; await this.page.waitForSelector(modalSelectorRetry, { state: 'attached', timeout: 10000, }); await this.page.waitForTimeout(150); this.log('info', 'Add Car modal found after retry', { selector: modalSelectorRetry }); } catch { this.log('warn', 'Add Car modal still not found after retry'); } // Don't throw - modal might appear differently in real iRacing } } /** * Click the "Add Track" / "Select Track" button to open the Add Track modal. * This button is located on the Set Track step (Step 11). */ private async clickAddTrackButton(): Promise { if (!this.page) { throw new Error('Browser not connected'); } const addTrackButtonSelector = IRACING_SELECTORS.steps.addTrackButton; try { this.log('info', 'Clicking Add Track button to open modal'); // Use 'attached' instead of 'visible' because iRacing wizard steps have class="hidden" await this.page.waitForSelector(addTrackButtonSelector, { state: 'attached', timeout: this.isRealMode() ? IRACING_TIMEOUTS.elementWait : this.config.timeout, }); await this.safeClick(addTrackButtonSelector); this.log('info', 'Clicked Add Track button'); } catch (error) { const message = error instanceof Error ? error.message : String(error); this.log('error', 'Could not click Add Track button', { error: message }); throw new Error(`Failed to click Add Track button: ${message}`); } } /** * Wait for the Add Track modal to appear. */ private async waitForAddTrackModal(): Promise { if (!this.page) { throw new Error('Browser not connected'); } try { this.log('debug', 'Waiting for Add Track modal to appear'); // Wait for modal container - use 'attached' because iRacing wizard steps have class="hidden" const modalSelector = IRACING_SELECTORS.steps.addTrackModal; await this.page.waitForSelector(modalSelector, { state: 'attached', timeout: this.isRealMode() ? IRACING_TIMEOUTS.elementWait : this.config.timeout, }); // Brief pause for modal animation (reduced from 300ms) await this.page.waitForTimeout(150); this.log('info', 'Add Track modal is visible'); } catch (error) { const message = error instanceof Error ? error.message : String(error); this.log('warn', 'Add Track modal did not appear', { error: message }); // Don't throw - modal might appear differently in real iRacing } } /** * Select the first search result in the current modal by clicking its "Select" button. * In iRacing's Add Car/Track modals, search results are displayed in a table, * and each row has a "Select" button (a.btn.btn-block.btn-primary.btn-xs). * * Two button patterns exist: * 1. Direct select (single-config tracks): a.btn.btn-primary.btn-xs:not(.dropdown-toggle) * 2. Dropdown (multi-config tracks): a.btn.btn-primary.btn-xs.dropdown-toggle → opens menu → click .dropdown-item * * Clicking "Select" immediately adds the item - there is no confirm step. */ private async selectFirstSearchResult(): Promise { if (!this.page) { throw new Error('Browser not connected'); } // First try direct select button (non-dropdown) - using verified selectors // Try both track and car select buttons as this method is shared const directSelectors = [ IRACING_SELECTORS.steps.trackSelectButton, IRACING_SELECTORS.steps.carSelectButton ]; for (const selector of directSelectors) { const button = this.page.locator(selector).first(); if (await button.count() > 0 && await button.isVisible()) { await this.safeClick(selector, { timeout: IRACING_TIMEOUTS.elementWait }); this.log('info', 'Clicked direct Select button for first search result', { selector }); return; } } // Fallback: dropdown toggle pattern (for multi-config tracks) const dropdownSelector = IRACING_SELECTORS.steps.trackSelectDropdown; const dropdownButton = this.page.locator(dropdownSelector).first(); if (await dropdownButton.count() > 0 && await dropdownButton.isVisible()) { // Click dropdown to open menu await this.safeClick(dropdownSelector, { timeout: IRACING_TIMEOUTS.elementWait }); this.log('debug', 'Clicked dropdown toggle, waiting for menu', { selector: dropdownSelector }); // Wait for dropdown menu to appear await this.page.waitForSelector('.dropdown-menu.show', { timeout: 3000 }).catch(() => { }); // Click first item in dropdown (first track config) const itemSelector = IRACING_SELECTORS.steps.trackSelectDropdownItem; await this.page.waitForTimeout(200); await this.safeClick(itemSelector, { timeout: IRACING_TIMEOUTS.elementWait }); this.log('info', 'Clicked first dropdown item to select track config', { selector: itemSelector }); return; } // Final fallback: try tolerant car row selectors (various UI variants) const carRowSelector = '.car-row, .car-item, [data-testid*="car"], [id*="favorite_cars"], [id*="select-car"]'; const carRow = this.page.locator(carRowSelector).first(); if (await carRow.count() > 0) { this.log('info', 'Fallback: clicking car row/item to select', { selector: carRowSelector }); // Click the row itself (or its first clickable descendant) try { await this.safeClick(carRowSelector, { timeout: IRACING_TIMEOUTS.elementWait }); this.log('info', 'Clicked car row fallback selector'); return; } catch (e) { this.log('debug', 'Car row fallback click failed, attempting to click first link inside row', { error: String(e) }); const linkInside = this.page.locator(`${carRowSelector} a, ${carRowSelector} button`).first(); if (await linkInside.count() > 0 && await linkInside.isVisible()) { await this.safeClick(`${carRowSelector} a, ${carRowSelector} button`, { timeout: IRACING_TIMEOUTS.elementWait }); this.log('info', 'Clicked link/button inside car row fallback'); return; } } } // If none found, throw error throw new Error('No Select button found in modal table and no fallback car row found'); } // NOTE: clickCarModalConfirm() and clickTrackModalConfirm() have been removed. // The Add Car/Track modals use a table with "Select" buttons that immediately add the item. // There is no separate confirm step - clicking "Select" closes the modal and adds the car/track. // The selectFirstSearchResult() method now handles clicking the "Select" button directly. /** * Click the confirm/select button in the "Add Admin" modal. * Uses a specific selector that avoids the checkout button. */ private async clickAdminModalConfirm(): Promise { if (!this.page) { throw new Error('Browser not connected'); } // Use a selector specific to the admin modal, NOT the main wizard modal footer // The admin modal confirm button should be inside the admin modal content const adminConfirmSelector = '#set-admins .modal .btn-primary, #set-admins .modal button:has-text("Add"), #set-admins .modal button:has-text("Select")'; try { // Use 'attached' instead of 'visible' because iRacing wizard steps have class="hidden" await this.page.waitForSelector(adminConfirmSelector, { state: 'attached', timeout: IRACING_TIMEOUTS.elementWait, }); await this.safeClick(adminConfirmSelector, { timeout: IRACING_TIMEOUTS.elementWait }); this.log('info', 'Clicked admin modal confirm button'); } catch (error) { const message = error instanceof Error ? error.message : String(error); this.log('warn', 'Could not click admin modal confirm button', { error: message }); throw new Error(`Failed to confirm admin selection: ${message}`); } } /** * Click the "New Race" button in the modal that appears after clicking "Create a Race". * This modal asks whether to use "Last Settings" or "New Race". */ private async clickNewRaceInModal(): Promise { if (!this.page) { throw new Error('Browser not connected'); } try { this.log('info', 'Waiting for Create Race modal to appear'); // Wait for the modal - use 'attached' because iRacing elements may have class="hidden" const modalSelector = IRACING_SELECTORS.hostedRacing.createRaceModal; await this.page.waitForSelector(modalSelector, { state: 'attached', timeout: IRACING_TIMEOUTS.elementWait, }); this.log('info', 'Create Race modal attached, clicking New Race button'); // Click the "New Race" button - use 'attached' for consistency const newRaceSelector = IRACING_SELECTORS.hostedRacing.newRaceButton; await this.page.waitForSelector(newRaceSelector, { state: 'attached', timeout: IRACING_TIMEOUTS.elementWait, }); await this.safeClick(newRaceSelector, { timeout: IRACING_TIMEOUTS.elementWait }); this.log('info', 'Clicked New Race button, waiting for form to load'); // Wait a moment for the form to load await this.page.waitForTimeout(500); } catch (error) { const message = error instanceof Error ? error.message : String(error); this.log('error', 'Failed to click New Race in modal', { error: message }); throw new Error(`Failed to click New Race button: ${message}`); } } /** * Handle login for real iRacing website. * First checks if user is already authenticated - if so, navigates directly to hosted sessions. */ private async injectCookiesBeforeNavigation(targetUrl: string): Promise> { if (!this.persistentContext && !this.context) { return Result.err(new Error('No browser context available')); } try { // Read cookies from store const state = await this.cookieStore.read(); if (!state || state.cookies.length === 0) { return Result.err(new Error('No cookies found in session store')); } // Get only cookies that are valid for target URL // This filters out cookies from other domains (e.g., oauth.iracing.com, members.iracing.com) // and only injects cookies that match the target domain const validCookies = this.cookieStore.getValidCookiesForUrl(targetUrl); if (validCookies.length === 0) { this.log('warn', 'No valid cookies found for target URL', { targetUrl, totalCookies: state.cookies.length, }); return Result.err(new Error('No valid cookies found for target URL')); } // Inject cookies into context BEFORE navigation const context = this.persistentContext || this.context; await context!.addCookies(validCookies); this.log('info', 'Cookies injected successfully', { count: validCookies.length, targetUrl, cookieNames: validCookies.map((c) => c.name), }); return Result.ok(undefined); } catch (error) { const message = error instanceof Error ? error.message : String(error); return Result.err(new Error(`Cookie injection failed: ${message}`)); } } async verifyPageAuthentication(): Promise> { this.syncSessionStateFromBrowser(); return this.authService.verifyPageAuthentication(); } /** * Handle login for real iRacing website. * First checks if user is already authenticated - if so, navigates directly to hosted sessions. * Otherwise navigates to login page and waits for user to complete manual login. */ private async handleLogin(): Promise { try { // Check session cookies FIRST before launching browser const sessionResult = await this.checkSession(); if ( sessionResult.isOk() && sessionResult.unwrap() === AuthenticationState.AUTHENTICATED ) { // Valid cookies exist - use configured browser mode (headless/headed) this.log('info', 'Session cookies found, launching in configured browser mode'); await this.ensureBrowserContext(false); // Use configured mode if (!this.page) { return { success: false, error: 'Browser not connected' }; } // Inject cookies BEFORE navigation const injectResult = await this.injectCookiesBeforeNavigation( IRACING_URLS.hostedSessions ); if (injectResult.isErr()) { this.log('warn', 'Cookie injection failed, switching to manual login', { error: injectResult.error?.message ?? 'unknown error', }); // Fall through to manual login flow below } else { // Navigate with cookies injected await this.page.goto(IRACING_URLS.hostedSessions, { waitUntil: 'domcontentloaded', timeout: IRACING_TIMEOUTS.navigation, }); // Verify page shows authenticated state const verifyResult = await this.verifyPageAuthentication(); if (verifyResult.isOk()) { const browserState = verifyResult.unwrap(); if (browserState.isFullyAuthenticated()) { this.log('info', 'Authentication verified successfully'); return { success: true }; } else { this.log('warn', 'Page shows unauthenticated state despite cookies'); // Fall through to manual login flow below } } } } // No valid cookies or cookie injection failed - need manual login // Close existing browser if running in headless mode // Must restart in headed mode so user can see and interact with login page if (this.browserSession.getBrowserMode() === 'headless' && (this.browser || this.persistentContext)) { this.log('info', '[Auth] Closing headless browser to restart in headed mode for manual login'); await this.closeBrowserContext(); } // Ensure browser context is ready, forcing headed mode for manual login await this.ensureBrowserContext(true); if (!this.page) { return { success: false, error: 'Browser not connected after restart' }; } // Not authenticated - proceed with login flow this.log('info', 'Not authenticated, navigating to iRacing login page'); await this.page.goto(IRACING_URLS.login, { waitUntil: 'domcontentloaded', timeout: IRACING_TIMEOUTS.navigation, }); this.log('info', 'Waiting for user to complete login (max 2 minutes)...'); // Wait for navigation to hosted sessions page await this.page.waitForURL('**/hostedsessions**', { timeout: IRACING_TIMEOUTS.loginWait, }); this.log('info', 'Login successful, now on hosted sessions page'); return { success: true }; } catch (error) { const message = error instanceof Error ? error.message : String(error); this.log('error', 'Login failed or timed out', { error: message }); return { success: false, error: `Login failed: ${message}` }; } } async waitForStep(stepNumber: number): Promise { await this.navigator.waitForStep(stepNumber); } /** * Wait for a specific wizard step to be visible in real mode. * Uses the step container IDs from IRACING_SELECTORS.wizard.stepContainers */ private async waitForWizardStep(stepName: keyof typeof IRACING_SELECTORS.wizard.stepContainers): Promise { if (!this.page || !this.isRealMode()) return; const containerSelector = IRACING_SELECTORS.wizard.stepContainers[stepName]; if (!containerSelector) { this.log('warn', `Unknown wizard step: ${stepName}`); return; } try { this.log('debug', `Waiting for wizard step: ${stepName}`, { selector: containerSelector }); // Use 'attached' instead of 'visible' because iRacing wizard steps are marked as // 'active hidden' in the DOM - they exist but are hidden via CSS class await this.page.waitForSelector(containerSelector, { state: 'attached', timeout: 15000, }); // Brief pause to ensure DOM is settled await this.page.waitForTimeout(100); } catch (error) { this.log('warn', `Wizard step not attached: ${stepName}`, { error: String(error) }); // Don't throw - step might be combined with another or skipped } } /** * Fill a form field with fallback selector support. * Tries the primary selector first, then falls back to alternative selectors. * This is needed because iRacing's form structure can vary slightly. */ private async fillFieldWithFallback(fieldName: string, value: string): Promise { if (!this.page) { return { success: false, fieldName, valueSet: value, error: 'Browser not connected' }; } const selector = this.getFieldSelector(fieldName); const timeout = this.isRealMode() ? IRACING_TIMEOUTS.elementWait : this.config.timeout; // Split combined selectors and try each one const selectors = selector.split(', ').map(s => s.trim()); for (const sel of selectors) { try { this.log('debug', `Trying selector for ${fieldName}`, { selector: sel }); // Check if element exists and is visible const element = this.page.locator(sel).first(); const isVisible = await element.isVisible().catch(() => false); if (isVisible) { // Use 'attached' instead of 'visible' because iRacing wizard steps have class="hidden" await element.waitFor({ state: 'attached', timeout }); await element.fill(value); this.log('info', `Successfully filled ${fieldName}`, { selector: sel, value }); return { success: true, fieldName, valueSet: value }; } } catch (error) { this.log('debug', `Selector failed for ${fieldName}`, { selector: sel, error: String(error) }); } } // If none worked, try the original combined selector (Playwright handles comma-separated) try { this.log('debug', `Trying combined selector for ${fieldName}`, { selector }); // Use 'attached' instead of 'visible' because iRacing wizard steps have class="hidden" await this.page.waitForSelector(selector, { state: 'attached', timeout }); await this.page.fill(selector, value); return { success: true, fieldName, valueSet: value }; } catch (error) { const message = error instanceof Error ? error.message : String(error); this.log('error', `Failed to fill ${fieldName}`, { selector, error: message }); return { success: false, fieldName, valueSet: value, error: message }; } } /** * Click the "Next" button in the wizard footer. * In real iRacing, the next button text shows the next step name (e.g., "Server Details"). * @param nextStepName The name of the next step (for logging and fallback) */ private async clickNextButton(nextStepName: string): Promise { if (!this.page) { throw new Error('Browser not connected'); } if (!this.isRealMode()) { // Mock mode uses data-action="next" await this.clickAction('next'); return; } const timeout = IRACING_TIMEOUTS.elementWait; // Primary: Look for the next button with caret icon (it points to next step) const nextButtonSelector = IRACING_SELECTORS.wizard.nextButton; // Fallback: Look for button with the next step name const fallbackSelector = `.wizard-footer a.btn:has-text("${nextStepName}")`; try { // Attempt primary selector first using a forced safe click. // Some wizard footer buttons are present/attached but not considered "visible" by Playwright // (offscreen, overlapped by overlays, or transitional). Use a forced safe click first, // then fall back to name-based or last-resort selectors if that fails. this.log('debug', 'Attempting next button (primary) with forced click', { selector: nextButtonSelector }); try { await this.safeClick(nextButtonSelector, { timeout, force: true }); this.log('info', `Clicked next button to ${nextStepName} (primary forced)`); return; } catch (e) { this.log('debug', 'Primary forced click failed, falling back', { error: String(e) }); } // Try fallback with step name (also attempt forced click) this.log('debug', 'Trying fallback next button (forced)', { selector: fallbackSelector }); try { await this.safeClick(fallbackSelector, { timeout, force: true }); this.log('info', `Clicked next button (fallback) to ${nextStepName}`); return; } catch (e) { this.log('debug', 'Fallback forced click failed, trying last resort', { error: String(e) }); } // Last resort: any non-disabled button in wizard footer (use forced click) const lastResort = '.wizard-footer a.btn:not(.disabled):last-child'; await this.safeClick(lastResort, { timeout, force: true }); this.log('info', `Clicked next button (last resort) to ${nextStepName}`); } catch (error) { const message = error instanceof Error ? error.message : String(error); this.log('error', `Failed to click next button to ${nextStepName}`, { error: message }); throw new Error(`Failed to navigate to ${nextStepName}: ${message}`); } } async clickAction(action: string): Promise { if (!this.page) { return { success: false, target: action, error: 'Browser not connected' }; } const timeout = this.isRealMode() ? IRACING_TIMEOUTS.elementWait : this.config.timeout; let selector: string; if (!this.isRealMode()) { // Mock-mode shortcut selectors to match the lightweight fixtures used in tests. const mockMap: Record = { create: '#create-race-btn, [data-action="create"], button:has-text("Create a Race")', next: '.wizard-footer a.btn.btn-primary, .wizard-footer a:has(.icon-caret-right), [data-action="next"], button:has-text("Next")', back: '.wizard-footer a.btn.btn-secondary, .wizard-footer a:has(.icon-caret-left):has-text("Back"), [data-action="back"], button:has-text("Back")', confirm: '.modal-footer a.btn-success, button:has-text("Confirm"), [data-action="confirm"]', cancel: '.modal-footer a.btn-secondary, button:has-text("Cancel"), [data-action="cancel"]', close: '[aria-label="Close"], #gridpilot-close-btn' }; selector = mockMap[action] || `[data-action="${action}"], button:has-text("${action}")`; } else { selector = this.getActionSelector(action); } // Use 'attached' instead of 'visible' because mock fixtures/wizard steps may be present but hidden await this.page.waitForSelector(selector, { state: 'attached', timeout }); await this.safeClick(selector, { timeout }); return { success: true, target: selector }; } async fillField(fieldName: string, value: string): Promise { if (!this.page) { return { success: false, fieldName, valueSet: value, error: 'Browser not connected' }; } const selector = this.getFieldSelector(fieldName); const timeout = this.isRealMode() ? IRACING_TIMEOUTS.elementWait : this.config.timeout; this.log('debug', 'fillField', { fieldName, selector, mode: this.config.mode }); // In mock mode, reveal typical fixture-hidden containers to allow Playwright to interact. if (!this.isRealMode()) { try { await this.page.evaluate(() => { document.querySelectorAll('.wizard-step.hidden, .modal.hidden, .wizard-step[hidden]').forEach(el => { el.classList.remove('hidden'); el.removeAttribute('hidden'); }); }); } catch { // Ignore errors in test environment } } // Wait for the element to be attached to the DOM await this.page.waitForSelector(selector, { state: 'attached', timeout }); // Try normal Playwright fill first; fall back to JS injection in mock mode if Playwright refuses due to visibility. try { await this.page.fill(selector, value); return { success: true, fieldName, valueSet: value }; } catch (fillErr) { if (this.isRealMode()) { const message = fillErr instanceof Error ? fillErr.message : String(fillErr); return { success: false, fieldName, valueSet: value, error: message }; } // Mock-mode JS fallback: set value directly and dispatch events try { await this.page.evaluate(({ sel, val }) => { const el = document.querySelector(sel) as HTMLInputElement | HTMLTextAreaElement | null; if (!el) return; (el as any).value = val; (el as any).dispatchEvent(new Event('input', { bubbles: true })); (el as any).dispatchEvent(new Event('change', { bubbles: true })); }, { sel: selector, val: value }); return { success: true, fieldName, valueSet: value }; } catch (evalErr) { const message = evalErr instanceof Error ? evalErr.message : String(evalErr); return { success: false, fieldName, valueSet: value, error: message }; } } } async selectDropdown(name: string, value: string): Promise { if (!this.page) { throw new Error('Browser not connected'); } const selector = this.getDropdownSelector(name); const timeout = this.isRealMode() ? IRACING_TIMEOUTS.elementWait : this.config.timeout; // Try to wait for the canonical selector first try { await this.page.waitForSelector(selector, { state: 'attached', timeout }); await this.page.selectOption(selector, value); return; } catch { // fallthrough to tolerant fallback below } // Fallback strategy: // 1) Look for any or input elements const heuristics = [ `select[id*="${name}"]`, `select[name*="${name}"]`, `select[data-dropdown*="${name}"]`, `select`, `[data-dropdown="${name}"]`, `[data-dropdown*="${name}"]`, `[role="listbox"] select`, `[role="listbox"]`, ]; for (const h of heuristics) { try { const count = await this.page.locator(h).first().count().catch(() => 0); if (count > 0) { // Prefer selectOption on real