refactor(automation): remove browser automation, use OS-level automation only
This commit is contained in:
@@ -1,50 +1,17 @@
|
||||
import { app } from 'electron';
|
||||
import * as path from 'path';
|
||||
import { InMemorySessionRepository } from '@/packages/infrastructure/repositories/InMemorySessionRepository';
|
||||
import { MockBrowserAutomationAdapter } from '@/packages/infrastructure/adapters/automation/MockBrowserAutomationAdapter';
|
||||
import { BrowserDevToolsAdapter } from '@/packages/infrastructure/adapters/automation/BrowserDevToolsAdapter';
|
||||
import { NutJsAutomationAdapter } from '@/packages/infrastructure/adapters/automation/NutJsAutomationAdapter';
|
||||
import { MockAutomationEngineAdapter } from '@/packages/infrastructure/adapters/automation/MockAutomationEngineAdapter';
|
||||
import { PermissionService } from '@/packages/infrastructure/adapters/automation/PermissionService';
|
||||
import { FixtureServerService } from '@/packages/infrastructure/adapters/automation/FixtureServerService';
|
||||
import { StartAutomationSessionUseCase } from '@/packages/application/use-cases/StartAutomationSessionUseCase';
|
||||
import { loadAutomationConfig, getAutomationMode, AutomationMode } from '@/packages/infrastructure/config';
|
||||
import { PinoLogAdapter } from '@/packages/infrastructure/adapters/logging/PinoLogAdapter';
|
||||
import { NoOpLogAdapter } from '@/packages/infrastructure/adapters/logging/NoOpLogAdapter';
|
||||
import { loadLoggingConfig } from '@/packages/infrastructure/config/LoggingConfig';
|
||||
import type { ISessionRepository } from '@/packages/application/ports/ISessionRepository';
|
||||
import type { IBrowserAutomation } from '@/packages/application/ports/IBrowserAutomation';
|
||||
import type { IScreenAutomation } from '@/packages/application/ports/IScreenAutomation';
|
||||
import type { IAutomationEngine } from '@/packages/application/ports/IAutomationEngine';
|
||||
import type { ILogger } from '@/packages/application/ports/ILogger';
|
||||
import type { IFixtureServerService } from '@/packages/infrastructure/adapters/automation/FixtureServerService';
|
||||
|
||||
/**
|
||||
* Resolve the fixtures path relative to the monorepo root.
|
||||
* Uses __dirname to determine the navigation depth based on whether we're
|
||||
* running from source (dev mode) or built output (dist mode).
|
||||
*
|
||||
* Path breakdown:
|
||||
* - Dev mode: apps/companion/main → 3 levels to root
|
||||
* - Built mode: apps/companion/dist/main → 4 levels to root
|
||||
*
|
||||
* @param configuredPath - The path configured (may be relative or absolute)
|
||||
* @param dirname - The directory name (__dirname) of the calling module
|
||||
* @returns Resolved absolute path
|
||||
*/
|
||||
export function resolveFixturesPath(configuredPath: string, dirname: string): string {
|
||||
if (path.isAbsolute(configuredPath)) {
|
||||
return configuredPath;
|
||||
}
|
||||
|
||||
// Determine navigation depth based on whether we're in dist/ or source
|
||||
// Dev mode: apps/companion/main → 3 levels to root
|
||||
// Built mode: apps/companion/dist/main → 4 levels to root
|
||||
const isBuiltMode = dirname.includes(`${path.sep}dist${path.sep}`);
|
||||
const levelsUp = isBuiltMode ? '../../../../' : '../../../';
|
||||
const projectRoot = path.resolve(dirname, levelsUp);
|
||||
|
||||
return path.join(projectRoot, configuredPath);
|
||||
}
|
||||
|
||||
export interface BrowserConnectionResult {
|
||||
success: boolean;
|
||||
@@ -66,37 +33,22 @@ function createLogger(): ILogger {
|
||||
}
|
||||
|
||||
/**
|
||||
* Create browser automation adapter based on configuration mode.
|
||||
* Create screen automation adapter based on configuration mode.
|
||||
*
|
||||
* Mode mapping:
|
||||
* - 'development' → BrowserDevToolsAdapter with fixture server URL
|
||||
* - 'production' → NutJsAutomationAdapter with iRacing window
|
||||
* - 'test' → MockBrowserAutomationAdapter
|
||||
* - 'test'/'development' → MockBrowserAutomationAdapter
|
||||
*
|
||||
* @param mode - The automation mode from configuration
|
||||
* @param logger - Logger instance for the adapter
|
||||
* @returns IBrowserAutomation adapter instance
|
||||
* @returns IScreenAutomation adapter instance
|
||||
*/
|
||||
function createBrowserAutomationAdapter(mode: AutomationMode, logger: ILogger): IBrowserAutomation {
|
||||
function createBrowserAutomationAdapter(mode: AutomationMode, logger: ILogger): IScreenAutomation {
|
||||
const config = loadAutomationConfig();
|
||||
|
||||
switch (mode) {
|
||||
case 'development':
|
||||
return new BrowserDevToolsAdapter({
|
||||
debuggingPort: config.devTools?.debuggingPort ?? 9222,
|
||||
browserWSEndpoint: config.devTools?.browserWSEndpoint,
|
||||
defaultTimeout: config.defaultTimeout,
|
||||
launchBrowser: true,
|
||||
headless: false,
|
||||
startUrl: `http://localhost:${config.fixtureServer?.port ?? 3456}/01-hosted-racing.html`,
|
||||
}, logger.child({ adapter: 'BrowserDevTools' }));
|
||||
|
||||
case 'production':
|
||||
return new NutJsAutomationAdapter({
|
||||
mouseSpeed: config.nutJs?.mouseSpeed,
|
||||
keyboardDelay: config.nutJs?.keyboardDelay,
|
||||
defaultTimeout: config.defaultTimeout,
|
||||
}, logger.child({ adapter: 'NutJs' }));
|
||||
return new NutJsAutomationAdapter(config.nutJs, logger.child({ adapter: 'NutJs' }));
|
||||
|
||||
case 'test':
|
||||
default:
|
||||
@@ -109,13 +61,11 @@ export class DIContainer {
|
||||
|
||||
private logger: ILogger;
|
||||
private sessionRepository: ISessionRepository;
|
||||
private browserAutomation: IBrowserAutomation;
|
||||
private browserAutomation: IScreenAutomation;
|
||||
private automationEngine: IAutomationEngine;
|
||||
private startAutomationUseCase: StartAutomationSessionUseCase;
|
||||
private automationMode: AutomationMode;
|
||||
private permissionService: PermissionService;
|
||||
private fixtureServer: IFixtureServerService | null = null;
|
||||
private fixtureServerInitialized: boolean = false;
|
||||
|
||||
private constructor() {
|
||||
// Initialize logger first - it's needed by other components
|
||||
@@ -153,7 +103,6 @@ export class DIContainer {
|
||||
|
||||
private getBrowserAutomationType(mode: AutomationMode): string {
|
||||
switch (mode) {
|
||||
case 'development': return 'BrowserDevToolsAdapter';
|
||||
case 'production': return 'NutJsAutomationAdapter';
|
||||
case 'test':
|
||||
default: return 'MockBrowserAutomationAdapter';
|
||||
@@ -183,7 +132,7 @@ export class DIContainer {
|
||||
return this.automationMode;
|
||||
}
|
||||
|
||||
public getBrowserAutomation(): IBrowserAutomation {
|
||||
public getBrowserAutomation(): IScreenAutomation {
|
||||
return this.browserAutomation;
|
||||
}
|
||||
|
||||
@@ -196,121 +145,35 @@ export class DIContainer {
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve the fixtures path relative to the monorepo root.
|
||||
* Uses the exported resolveFixturesPath function with __dirname.
|
||||
*/
|
||||
private resolveFixturesPathInternal(configuredPath: string): string {
|
||||
return resolveFixturesPath(configuredPath, __dirname);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize fixture server for development mode.
|
||||
* Starts an embedded HTTP server serving static HTML fixtures.
|
||||
* This should be called before initializing browser connection.
|
||||
*/
|
||||
private async initializeFixtureServer(): Promise<BrowserConnectionResult> {
|
||||
const config = loadAutomationConfig();
|
||||
|
||||
if (!config.fixtureServer?.autoStart) {
|
||||
this.logger.debug('Fixture server auto-start disabled');
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
if (this.fixtureServerInitialized) {
|
||||
this.logger.debug('Fixture server already initialized');
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
this.fixtureServer = new FixtureServerService();
|
||||
const port = config.fixtureServer.port;
|
||||
const fixturesPath = this.resolveFixturesPathInternal(config.fixtureServer.fixturesPath);
|
||||
|
||||
this.logger.debug('Fixture server path resolution', {
|
||||
configuredPath: config.fixtureServer.fixturesPath,
|
||||
dirname: __dirname,
|
||||
resolvedPath: fixturesPath
|
||||
});
|
||||
|
||||
try {
|
||||
await this.fixtureServer.start(port, fixturesPath);
|
||||
const isReady = await this.fixtureServer.waitForReady(5000);
|
||||
|
||||
if (!isReady) {
|
||||
throw new Error('Fixture server failed to become ready within timeout');
|
||||
}
|
||||
|
||||
this.fixtureServerInitialized = true;
|
||||
this.logger.info(`Fixture server started on port ${port}`, {
|
||||
port,
|
||||
fixturesPath,
|
||||
baseUrl: this.fixtureServer.getBaseUrl()
|
||||
});
|
||||
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
const errorMsg = error instanceof Error ? error.message : 'Failed to start fixture server';
|
||||
this.logger.error('Fixture server initialization failed', error instanceof Error ? error : new Error(errorMsg));
|
||||
return { success: false, error: errorMsg };
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize browser connection based on mode.
|
||||
* In development mode, starts fixture server (if configured) then connects to browser via CDP.
|
||||
* Initialize automation connection based on mode.
|
||||
* In production mode, connects to iRacing window via nut.js.
|
||||
* In test mode, returns success immediately (no connection needed).
|
||||
* In test/development mode, returns success immediately (no connection needed).
|
||||
*/
|
||||
public async initializeBrowserConnection(): Promise<BrowserConnectionResult> {
|
||||
this.logger.info('Initializing browser connection', { mode: this.automationMode });
|
||||
this.logger.info('Initializing automation connection', { mode: this.automationMode });
|
||||
|
||||
if (this.automationMode === 'development') {
|
||||
const fixtureResult = await this.initializeFixtureServer();
|
||||
if (!fixtureResult.success) {
|
||||
return fixtureResult;
|
||||
}
|
||||
|
||||
try {
|
||||
const devToolsAdapter = this.browserAutomation as BrowserDevToolsAdapter;
|
||||
await devToolsAdapter.connect();
|
||||
this.logger.info('Browser connection established', { mode: 'development', adapter: 'BrowserDevTools' });
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
const errorMsg = error instanceof Error ? error.message : 'Failed to connect to browser';
|
||||
this.logger.error('Browser connection failed', error instanceof Error ? error : new Error(errorMsg), { mode: 'development' });
|
||||
return {
|
||||
success: false,
|
||||
error: errorMsg
|
||||
};
|
||||
}
|
||||
}
|
||||
if (this.automationMode === 'production') {
|
||||
try {
|
||||
const nutJsAdapter = this.browserAutomation as NutJsAutomationAdapter;
|
||||
const result = await nutJsAdapter.connect();
|
||||
if (!result.success) {
|
||||
this.logger.error('Browser connection failed', new Error(result.error || 'Unknown error'), { mode: 'production' });
|
||||
this.logger.error('Automation connection failed', new Error(result.error || 'Unknown error'), { mode: 'production' });
|
||||
return { success: false, error: result.error };
|
||||
}
|
||||
this.logger.info('Browser connection established', { mode: 'production', adapter: 'NutJs' });
|
||||
this.logger.info('Automation connection established', { mode: 'production', adapter: 'NutJs' });
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
const errorMsg = error instanceof Error ? error.message : 'Failed to initialize nut.js';
|
||||
this.logger.error('Browser connection failed', error instanceof Error ? error : new Error(errorMsg), { mode: 'production' });
|
||||
this.logger.error('Automation connection failed', error instanceof Error ? error : new Error(errorMsg), { mode: 'production' });
|
||||
return {
|
||||
success: false,
|
||||
error: errorMsg
|
||||
};
|
||||
}
|
||||
}
|
||||
this.logger.debug('Test mode - no browser connection needed');
|
||||
return { success: true }; // Test mode doesn't need connection
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the fixture server instance (may be null if not in development mode or not auto-started).
|
||||
*/
|
||||
public getFixtureServer(): IFixtureServerService | null {
|
||||
return this.fixtureServer;
|
||||
|
||||
this.logger.debug('Test/development mode - no automation connection needed');
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -320,21 +183,12 @@ export class DIContainer {
|
||||
public async shutdown(): Promise<void> {
|
||||
this.logger.info('DIContainer shutting down');
|
||||
|
||||
if (this.fixtureServer?.isRunning()) {
|
||||
try {
|
||||
await this.fixtureServer.stop();
|
||||
this.logger.info('Fixture server stopped');
|
||||
} catch (error) {
|
||||
this.logger.error('Error stopping fixture server', error instanceof Error ? error : new Error('Unknown error'));
|
||||
}
|
||||
}
|
||||
|
||||
if (this.browserAutomation && 'disconnect' in this.browserAutomation) {
|
||||
try {
|
||||
await (this.browserAutomation as BrowserDevToolsAdapter).disconnect();
|
||||
this.logger.info('Browser automation disconnected');
|
||||
await (this.browserAutomation as NutJsAutomationAdapter).disconnect();
|
||||
this.logger.info('Automation adapter disconnected');
|
||||
} catch (error) {
|
||||
this.logger.error('Error disconnecting browser automation', error instanceof Error ? error : new Error('Unknown error'));
|
||||
this.logger.error('Error disconnecting automation adapter', error instanceof Error ? error : new Error('Unknown error'));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user