feat(logging): add professional logging with pino and headless E2E tests - Add ILogger port interface in application layer - Implement PinoLogAdapter with Electron-compatible structured logging - Add NoOpLogAdapter for testing - Wire logging into DI container and all adapters - Create 32 E2E tests for automation workflow (headless-only) - Add vitest.e2e.config.ts for E2E test configuration - All tests enforce HEADLESS mode (no headed browser allowed)
This commit is contained in:
@@ -5,22 +5,41 @@ import { NutJsAutomationAdapter } from '@/packages/infrastructure/adapters/autom
|
||||
import { MockAutomationEngineAdapter } from '@/packages/infrastructure/adapters/automation/MockAutomationEngineAdapter';
|
||||
import { StartAutomationSessionUseCase } from '@/packages/application/use-cases/StartAutomationSessionUseCase';
|
||||
import { loadAutomationConfig, 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 { IAutomationEngine } from '@/packages/application/ports/IAutomationEngine';
|
||||
import type { ILogger } from '@/packages/application/ports/ILogger';
|
||||
|
||||
export interface BrowserConnectionResult {
|
||||
success: boolean;
|
||||
error?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create logger based on environment configuration.
|
||||
* In test environment, returns NoOpLogAdapter for silent logging.
|
||||
*/
|
||||
function createLogger(): ILogger {
|
||||
const config = loadLoggingConfig();
|
||||
|
||||
if (process.env.NODE_ENV === 'test') {
|
||||
return new NoOpLogAdapter();
|
||||
}
|
||||
|
||||
return new PinoLogAdapter(config);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create browser automation adapter based on configuration mode.
|
||||
*
|
||||
* @param mode - The automation mode from configuration
|
||||
* @param logger - Logger instance for the adapter
|
||||
* @returns IBrowserAutomation adapter instance
|
||||
*/
|
||||
function createBrowserAutomationAdapter(mode: AutomationMode): IBrowserAutomation {
|
||||
function createBrowserAutomationAdapter(mode: AutomationMode, logger: ILogger): IBrowserAutomation {
|
||||
const config = loadAutomationConfig();
|
||||
|
||||
switch (mode) {
|
||||
@@ -29,14 +48,14 @@ function createBrowserAutomationAdapter(mode: AutomationMode): IBrowserAutomatio
|
||||
debuggingPort: config.devTools?.debuggingPort,
|
||||
browserWSEndpoint: config.devTools?.browserWSEndpoint,
|
||||
defaultTimeout: config.defaultTimeout,
|
||||
});
|
||||
}, logger.child({ adapter: 'BrowserDevTools' }));
|
||||
|
||||
case 'production':
|
||||
return new NutJsAutomationAdapter({
|
||||
mouseSpeed: config.nutJs?.mouseSpeed,
|
||||
keyboardDelay: config.nutJs?.keyboardDelay,
|
||||
defaultTimeout: config.defaultTimeout,
|
||||
});
|
||||
}, logger.child({ adapter: 'NutJs' }));
|
||||
|
||||
case 'mock':
|
||||
default:
|
||||
@@ -47,6 +66,7 @@ function createBrowserAutomationAdapter(mode: AutomationMode): IBrowserAutomatio
|
||||
export class DIContainer {
|
||||
private static instance: DIContainer;
|
||||
|
||||
private logger: ILogger;
|
||||
private sessionRepository: ISessionRepository;
|
||||
private browserAutomation: IBrowserAutomation;
|
||||
private automationEngine: IAutomationEngine;
|
||||
@@ -54,11 +74,15 @@ export class DIContainer {
|
||||
private automationMode: AutomationMode;
|
||||
|
||||
private constructor() {
|
||||
// Initialize logger first - it's needed by other components
|
||||
this.logger = createLogger();
|
||||
this.logger.info('DIContainer initializing', { automationMode: process.env.AUTOMATION_MODE });
|
||||
|
||||
const config = loadAutomationConfig();
|
||||
this.automationMode = config.mode;
|
||||
|
||||
this.sessionRepository = new InMemorySessionRepository();
|
||||
this.browserAutomation = createBrowserAutomationAdapter(config.mode);
|
||||
this.browserAutomation = createBrowserAutomationAdapter(config.mode, this.logger);
|
||||
this.automationEngine = new MockAutomationEngineAdapter(
|
||||
this.browserAutomation,
|
||||
this.sessionRepository
|
||||
@@ -68,6 +92,20 @@ export class DIContainer {
|
||||
this.browserAutomation,
|
||||
this.sessionRepository
|
||||
);
|
||||
|
||||
this.logger.info('DIContainer initialized', {
|
||||
automationMode: config.mode,
|
||||
sessionRepositoryType: 'InMemorySessionRepository',
|
||||
browserAutomationType: this.getBrowserAutomationType(config.mode)
|
||||
});
|
||||
}
|
||||
|
||||
private getBrowserAutomationType(mode: AutomationMode): string {
|
||||
switch (mode) {
|
||||
case 'dev': return 'BrowserDevToolsAdapter';
|
||||
case 'production': return 'NutJsAutomationAdapter';
|
||||
default: return 'MockBrowserAutomationAdapter';
|
||||
}
|
||||
}
|
||||
|
||||
public static getInstance(): DIContainer {
|
||||
@@ -97,21 +135,30 @@ export class DIContainer {
|
||||
return this.browserAutomation;
|
||||
}
|
||||
|
||||
public getLogger(): ILogger {
|
||||
return this.logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize browser connection for dev mode.
|
||||
* In dev mode, connects to the browser via Chrome DevTools Protocol.
|
||||
* In mock mode, returns success immediately (no connection needed).
|
||||
*/
|
||||
public async initializeBrowserConnection(): Promise<BrowserConnectionResult> {
|
||||
this.logger.info('Initializing browser connection', { mode: this.automationMode });
|
||||
|
||||
if (this.automationMode === 'dev') {
|
||||
try {
|
||||
const devToolsAdapter = this.browserAutomation as BrowserDevToolsAdapter;
|
||||
await devToolsAdapter.connect();
|
||||
this.logger.info('Browser connection established', { mode: 'dev', 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: 'dev' });
|
||||
return {
|
||||
success: false,
|
||||
error: error instanceof Error ? error.message : 'Failed to connect to browser'
|
||||
error: errorMsg
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -120,16 +167,21 @@ export class DIContainer {
|
||||
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' });
|
||||
return { success: false, error: result.error };
|
||||
}
|
||||
this.logger.info('Browser 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' });
|
||||
return {
|
||||
success: false,
|
||||
error: error instanceof Error ? error.message : 'Failed to initialize nut.js'
|
||||
error: errorMsg
|
||||
};
|
||||
}
|
||||
}
|
||||
this.logger.debug('Mock mode - no browser connection needed');
|
||||
return { success: true }; // Mock mode doesn't need connection
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user