262 lines
8.2 KiB
TypeScript
262 lines
8.2 KiB
TypeScript
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
|
|
import { loadAutomationConfig, getAutomationMode, AutomationMode } from '../../../packages/automation/infrastructure/config/AutomationConfig';
|
|
|
|
describe('AutomationConfig', () => {
|
|
const originalEnv = process.env;
|
|
|
|
beforeEach(() => {
|
|
// Reset environment before each test
|
|
process.env = { ...originalEnv };
|
|
});
|
|
|
|
afterEach(() => {
|
|
// Restore original environment
|
|
process.env = originalEnv;
|
|
});
|
|
|
|
describe('getAutomationMode', () => {
|
|
describe('NODE_ENV-based mode detection', () => {
|
|
it('should return production mode when NODE_ENV=production', () => {
|
|
(process.env as any).NODE_ENV = 'production';
|
|
delete process.env.AUTOMATION_MODE;
|
|
|
|
const mode = getAutomationMode();
|
|
|
|
expect(mode).toBe('production');
|
|
});
|
|
|
|
it('should return test mode when NODE_ENV=test', () => {
|
|
(process.env as any).NODE_ENV = 'test';
|
|
delete process.env.AUTOMATION_MODE;
|
|
|
|
const mode = getAutomationMode();
|
|
|
|
expect(mode).toBe('test');
|
|
});
|
|
|
|
it('should return test mode when NODE_ENV is not set', () => {
|
|
delete (process.env as any).NODE_ENV;
|
|
delete process.env.AUTOMATION_MODE;
|
|
|
|
const mode = getAutomationMode();
|
|
|
|
expect(mode).toBe('test');
|
|
});
|
|
|
|
it('should return test mode for unknown NODE_ENV values', () => {
|
|
(process.env as any).NODE_ENV = 'staging';
|
|
delete process.env.AUTOMATION_MODE;
|
|
|
|
const mode = getAutomationMode();
|
|
|
|
expect(mode).toBe('test');
|
|
});
|
|
|
|
it('should return development mode when NODE_ENV=development', () => {
|
|
(process.env as any).NODE_ENV = 'development';
|
|
delete process.env.AUTOMATION_MODE;
|
|
|
|
const mode = getAutomationMode();
|
|
|
|
expect(mode).toBe('development');
|
|
});
|
|
});
|
|
|
|
describe('legacy AUTOMATION_MODE support', () => {
|
|
it('should map legacy dev mode to test with deprecation warning', () => {
|
|
process.env.AUTOMATION_MODE = 'dev';
|
|
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
|
|
|
|
const mode = getAutomationMode();
|
|
|
|
expect(mode).toBe('test');
|
|
expect(consoleSpy).toHaveBeenCalledWith(
|
|
expect.stringContaining('[DEPRECATED] AUTOMATION_MODE')
|
|
);
|
|
consoleSpy.mockRestore();
|
|
});
|
|
|
|
it('should map legacy mock mode to test with deprecation warning', () => {
|
|
process.env.AUTOMATION_MODE = 'mock';
|
|
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
|
|
|
|
const mode = getAutomationMode();
|
|
|
|
expect(mode).toBe('test');
|
|
expect(consoleSpy).toHaveBeenCalledWith(
|
|
expect.stringContaining('[DEPRECATED] AUTOMATION_MODE')
|
|
);
|
|
consoleSpy.mockRestore();
|
|
});
|
|
|
|
it('should map legacy production mode to production with deprecation warning', () => {
|
|
process.env.AUTOMATION_MODE = 'production';
|
|
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
|
|
|
|
const mode = getAutomationMode();
|
|
|
|
expect(mode).toBe('production');
|
|
expect(consoleSpy).toHaveBeenCalledWith(
|
|
expect.stringContaining('[DEPRECATED] AUTOMATION_MODE')
|
|
);
|
|
consoleSpy.mockRestore();
|
|
});
|
|
|
|
it('should ignore invalid AUTOMATION_MODE and use NODE_ENV', () => {
|
|
process.env.AUTOMATION_MODE = 'invalid-mode';
|
|
(process.env as any).NODE_ENV = 'production';
|
|
|
|
const mode = getAutomationMode();
|
|
|
|
expect(mode).toBe('production');
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('loadAutomationConfig', () => {
|
|
describe('default configuration', () => {
|
|
it('should return test mode when NODE_ENV is not set', () => {
|
|
delete (process.env as any).NODE_ENV;
|
|
delete process.env.AUTOMATION_MODE;
|
|
|
|
const config = loadAutomationConfig();
|
|
|
|
expect(config.mode).toBe('test');
|
|
});
|
|
|
|
it('should return default nutJs configuration', () => {
|
|
const config = loadAutomationConfig();
|
|
|
|
expect(config.nutJs?.windowTitle).toBe('iRacing');
|
|
expect(config.nutJs?.templatePath).toBe('./resources/templates/iracing');
|
|
expect(config.nutJs?.confidence).toBe(0.9);
|
|
});
|
|
|
|
it('should return default shared settings', () => {
|
|
const config = loadAutomationConfig();
|
|
|
|
expect(config.defaultTimeout).toBe(30000);
|
|
expect(config.retryAttempts).toBe(3);
|
|
expect(config.screenshotOnError).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe('production mode configuration', () => {
|
|
it('should return production mode when NODE_ENV=production', () => {
|
|
(process.env as any).NODE_ENV = 'production';
|
|
delete process.env.AUTOMATION_MODE;
|
|
|
|
const config = loadAutomationConfig();
|
|
|
|
expect(config.mode).toBe('production');
|
|
});
|
|
|
|
it('should parse IRACING_WINDOW_TITLE', () => {
|
|
process.env.IRACING_WINDOW_TITLE = 'iRacing Simulator';
|
|
|
|
const config = loadAutomationConfig();
|
|
|
|
expect(config.nutJs?.windowTitle).toBe('iRacing Simulator');
|
|
});
|
|
|
|
it('should parse TEMPLATE_PATH', () => {
|
|
process.env.TEMPLATE_PATH = '/custom/templates';
|
|
|
|
const config = loadAutomationConfig();
|
|
|
|
expect(config.nutJs?.templatePath).toBe('/custom/templates');
|
|
});
|
|
|
|
it('should parse OCR_CONFIDENCE', () => {
|
|
process.env.OCR_CONFIDENCE = '0.85';
|
|
|
|
const config = loadAutomationConfig();
|
|
|
|
expect(config.nutJs?.confidence).toBe(0.85);
|
|
});
|
|
});
|
|
|
|
describe('environment variable parsing', () => {
|
|
it('should parse AUTOMATION_TIMEOUT', () => {
|
|
process.env.AUTOMATION_TIMEOUT = '60000';
|
|
|
|
const config = loadAutomationConfig();
|
|
|
|
expect(config.defaultTimeout).toBe(60000);
|
|
});
|
|
|
|
it('should parse RETRY_ATTEMPTS', () => {
|
|
process.env.RETRY_ATTEMPTS = '5';
|
|
|
|
const config = loadAutomationConfig();
|
|
|
|
expect(config.retryAttempts).toBe(5);
|
|
});
|
|
|
|
it('should parse SCREENSHOT_ON_ERROR=false', () => {
|
|
process.env.SCREENSHOT_ON_ERROR = 'false';
|
|
|
|
const config = loadAutomationConfig();
|
|
|
|
expect(config.screenshotOnError).toBe(false);
|
|
});
|
|
|
|
it('should parse SCREENSHOT_ON_ERROR=true', () => {
|
|
process.env.SCREENSHOT_ON_ERROR = 'true';
|
|
|
|
const config = loadAutomationConfig();
|
|
|
|
expect(config.screenshotOnError).toBe(true);
|
|
});
|
|
|
|
it('should fallback to defaults for invalid integer values', () => {
|
|
process.env.AUTOMATION_TIMEOUT = 'not-a-number';
|
|
process.env.RETRY_ATTEMPTS = '';
|
|
|
|
const config = loadAutomationConfig();
|
|
|
|
expect(config.defaultTimeout).toBe(30000);
|
|
expect(config.retryAttempts).toBe(3);
|
|
});
|
|
|
|
it('should fallback to defaults for invalid float values', () => {
|
|
process.env.OCR_CONFIDENCE = 'invalid';
|
|
|
|
const config = loadAutomationConfig();
|
|
|
|
expect(config.nutJs?.confidence).toBe(0.9);
|
|
});
|
|
|
|
it('should fallback to test mode for invalid NODE_ENV', () => {
|
|
(process.env as any).NODE_ENV = 'invalid-env';
|
|
delete process.env.AUTOMATION_MODE;
|
|
|
|
const config = loadAutomationConfig();
|
|
|
|
expect(config.mode).toBe('test');
|
|
});
|
|
});
|
|
|
|
describe('full configuration scenario', () => {
|
|
it('should load complete test environment configuration', () => {
|
|
(process.env as any).NODE_ENV = 'test';
|
|
delete process.env.AUTOMATION_MODE;
|
|
|
|
const config = loadAutomationConfig();
|
|
|
|
expect(config.mode).toBe('test');
|
|
expect(config.nutJs).toBeDefined();
|
|
});
|
|
|
|
it('should load complete production environment configuration', () => {
|
|
(process.env as any).NODE_ENV = 'production';
|
|
delete process.env.AUTOMATION_MODE;
|
|
|
|
const config = loadAutomationConfig();
|
|
|
|
expect(config.mode).toBe('production');
|
|
expect(config.nutJs).toBeDefined();
|
|
});
|
|
});
|
|
});
|
|
}); |