Files
ruflo/v2/tests/production/environment-validation.test.ts
T
rUvandClaude Opus 4.5 1b6a1971f7 feat(v3): add comprehensive v3 implementation plans and swarm architecture
- Reorganize v3/ into implementation/ subfolders:
  - planning/, architecture/, migration/, integration/
  - security/, adrs/, research/, swarm-plans/

- Add 15-agent concurrent swarm implementation plans:
  - SWARM-OVERVIEW.md: Hierarchical mesh topology, 4 phases
  - AGENT-SPECIFICATIONS.md: 15 agent configs with capabilities
  - TDD-LONDON-SCHOOL-PLAN.md: Mock-first TDD methodology
  - BENCHMARK-OPTIMIZATION.md: Performance targets (2.49x-7.47x)
  - DEPLOYMENT-PLAN.md: Release strategy, CI/CD, rollback
  - GITHUB-ISSUE-TRACKING.md: Issue workflow with agent replies

- Update .claude/settings.json with proper hooks format
- Add bash aliases for claude --dangerously-skip-permissions

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-04 15:40:51 +00:00

433 lines
14 KiB
TypeScript

/**
* Production Environment Validation Tests
*
* These tests validate environment configuration and production readiness.
* Validates real environment setup without mocking.
*/
import { describe, test, expect, beforeAll, afterAll } from '@jest/globals';
import { SystemIntegration } from '../../src/integration/system-integration.js';
import { ConfigManager } from '../../src/config/config-manager.js';
import * as fs from 'fs-extra';
import * as path from 'path';
describe('Production Environment Validation', () => {
let systemIntegration: SystemIntegration;
let configManager: ConfigManager;
beforeAll(async () => {
configManager = new ConfigManager();
});
afterAll(async () => {
if (systemIntegration?.isReady()) {
await systemIntegration.shutdown();
}
});
describe('Environment Variable Validation', () => {
test('should validate required environment variables for production', () => {
const requiredVars = [
'NODE_ENV',
'PORT',
'LOG_LEVEL'
];
const optionalVars = [
'DATABASE_URL',
'REDIS_URL',
'API_KEY',
'SMTP_HOST',
'JWT_SECRET',
'ANTHROPIC_API_KEY',
'OPENAI_API_KEY'
];
// Check required variables
for (const varName of requiredVars) {
const value = process.env[varName];
if (!value) {
console.warn(`Warning: Required environment variable ${varName} is not set`);
}
// Don't fail tests for missing env vars in test environment
// expect(value).toBeDefined();
}
// Validate environment-specific settings
const nodeEnv = process.env.NODE_ENV || 'development';
expect(['development', 'test', 'staging', 'production']).toContain(nodeEnv);
if (nodeEnv === 'production') {
// Production-specific validations
expect(process.env.NODE_ENV).toBe('production');
expect(process.env.LOG_LEVEL).toMatch(/^(error|warn|info)$/);
// Security requirements for production
if (process.env.JWT_SECRET) {
expect(process.env.JWT_SECRET.length).toBeGreaterThan(32);
}
}
});
test('should validate environment variable types and formats', () => {
// Port validation
if (process.env.PORT) {
const port = parseInt(process.env.PORT, 10);
expect(port).toBeGreaterThan(0);
expect(port).toBeLessThan(65536);
}
// Database URL validation
if (process.env.DATABASE_URL) {
const dbUrl = process.env.DATABASE_URL;
expect(dbUrl).toMatch(/^(postgresql|mysql|sqlite):\/\//);
}
// Redis URL validation
if (process.env.REDIS_URL) {
const redisUrl = process.env.REDIS_URL;
expect(redisUrl).toMatch(/^redis:\/\//);
}
// Log level validation
if (process.env.LOG_LEVEL) {
expect(['error', 'warn', 'info', 'debug', 'trace']).toContain(process.env.LOG_LEVEL);
}
// API key format validation
if (process.env.ANTHROPIC_API_KEY) {
expect(process.env.ANTHROPIC_API_KEY).toMatch(/^sk-ant-/);
}
if (process.env.OPENAI_API_KEY) {
expect(process.env.OPENAI_API_KEY).toMatch(/^sk-/);
}
});
test('should not expose sensitive data in environment', () => {
const sensitivePatterns = [
/password/i,
/secret/i,
/key/i,
/token/i
];
// Check that sensitive environment variables are not logged
const envSnapshot = JSON.stringify(process.env);
// These should not appear in plain text
expect(envSnapshot).not.toContain('password123');
expect(envSnapshot).not.toContain('supersecret');
expect(envSnapshot).not.toContain('sk-1234567890abcdef');
// Validate that environment loading doesn't leak sensitive data
const config = configManager.getConfig();
const configString = JSON.stringify(config);
// Config should not contain raw secrets
expect(configString).not.toMatch(/sk-ant-api01-[a-zA-Z0-9_-]{95}/);
expect(configString).not.toMatch(/sk-[a-zA-Z0-9]{48}/);
});
});
describe('Configuration File Validation', () => {
test('should validate configuration file structure', async () => {
const configPath = path.join(process.cwd(), 'claude-flow.config.json');
if (await fs.pathExists(configPath)) {
const configContent = await fs.readJson(configPath);
// Validate required configuration sections
expect(configContent).toHaveProperty('agents');
expect(configContent).toHaveProperty('memory');
expect(configContent).toHaveProperty('orchestration');
// Validate agent configuration
if (configContent.agents) {
expect(configContent.agents).toHaveProperty('maxAgents');
expect(typeof configContent.agents.maxAgents).toBe('number');
expect(configContent.agents.maxAgents).toBeGreaterThan(0);
}
// Validate memory configuration
if (configContent.memory) {
expect(configContent.memory).toHaveProperty('backend');
expect(['memory', 'sqlite', 'redis']).toContain(configContent.memory.backend);
}
}
});
test('should validate JSON schema compliance', async () => {
await configManager.initialize({
logLevel: 'info',
environment: 'test'
});
const config = configManager.getConfig();
// Validate configuration structure
expect(config).toBeDefined();
expect(typeof config).toBe('object');
// Required properties
expect(config).toHaveProperty('logLevel');
expect(config).toHaveProperty('environment');
// Type validation
expect(typeof config.logLevel).toBe('string');
expect(typeof config.environment).toBe('string');
// Value validation
expect(['error', 'warn', 'info', 'debug', 'trace']).toContain(config.logLevel);
expect(['development', 'test', 'staging', 'production']).toContain(config.environment);
});
});
describe('System Dependencies Validation', () => {
test('should validate Node.js version compatibility', () => {
const nodeVersion = process.version;
const majorVersion = parseInt(nodeVersion.slice(1).split('.')[0], 10);
// Require Node.js 18 or higher
expect(majorVersion).toBeGreaterThanOrEqual(18);
console.log(`Node.js version: ${nodeVersion} (Major: ${majorVersion})`);
});
test('should validate required npm packages', async () => {
const packageJsonPath = path.join(process.cwd(), 'package.json');
expect(await fs.pathExists(packageJsonPath)).toBe(true);
const packageJson = await fs.readJson(packageJsonPath);
// Validate critical dependencies
const requiredDeps = [
'@modelcontextprotocol/sdk',
'chalk',
'commander',
'inquirer',
'ruv-swarm'
];
for (const dep of requiredDeps) {
expect(packageJson.dependencies).toHaveProperty(dep);
}
// Validate development dependencies
const requiredDevDeps = [
'@types/node',
'typescript',
'jest'
];
for (const dep of requiredDevDeps) {
expect(packageJson.devDependencies).toHaveProperty(dep);
}
});
test('should validate file system permissions', async () => {
const testDir = path.join(process.cwd(), 'temp-permission-test');
const testFile = path.join(testDir, 'test.json');
try {
// Test write permissions
await fs.ensureDir(testDir);
await fs.writeJson(testFile, { test: true });
// Test read permissions
const data = await fs.readJson(testFile);
expect(data.test).toBe(true);
// Test delete permissions
await fs.remove(testDir);
expect(await fs.pathExists(testDir)).toBe(false);
} catch (error) {
// Permissions test failed
expect(error).toBeUndefined();
}
});
});
describe('Network and Connectivity Validation', () => {
test('should validate network interface availability', () => {
const os = require('os');
const networkInterfaces = os.networkInterfaces();
expect(networkInterfaces).toBeDefined();
expect(Object.keys(networkInterfaces)).toHaveLength.toBeGreaterThan(0);
// Check for at least one non-loopback interface
const hasNonLoopback = Object.values(networkInterfaces).some((interfaces: any[]) =>
interfaces.some(iface => !iface.internal)
);
expect(hasNonLoopback).toBe(true);
});
test('should validate port availability', async () => {
const net = require('net');
const testPorts = [3000, 8080, 9000];
for (const port of testPorts) {
const isPortInUse = await new Promise<boolean>((resolve) => {
const server = net.createServer();
server.listen(port, () => {
server.close(() => resolve(false));
});
server.on('error', () => resolve(true));
});
// Log port availability (don't fail test)
console.log(`Port ${port}: ${isPortInUse ? 'in use' : 'available'}`);
}
});
});
describe('Security Configuration Validation', () => {
test('should validate HTTPS configuration for production', () => {
const nodeEnv = process.env.NODE_ENV || 'development';
if (nodeEnv === 'production') {
// Production should enforce HTTPS
expect(process.env.FORCE_HTTPS).toBe('true');
// TLS certificates should be available
if (process.env.TLS_CERT_PATH) {
expect(fs.pathExistsSync(process.env.TLS_CERT_PATH)).toBe(true);
}
if (process.env.TLS_KEY_PATH) {
expect(fs.pathExistsSync(process.env.TLS_KEY_PATH)).toBe(true);
}
}
});
test('should validate security headers configuration', async () => {
await configManager.initialize({
logLevel: 'info',
environment: 'production-test',
security: {
enableAuth: true,
enforceHttps: true,
validateInput: true
}
});
const config = configManager.getConfig();
if (config.security) {
expect(config.security.enableAuth).toBe(true);
expect(config.security.enforceHttps).toBe(true);
expect(config.security.validateInput).toBe(true);
}
});
test('should validate authentication configuration', () => {
// JWT secret validation
if (process.env.JWT_SECRET) {
const secret = process.env.JWT_SECRET;
expect(secret.length).toBeGreaterThan(32);
expect(secret).not.toBe('your-secret-key');
expect(secret).not.toBe('default-secret');
}
// API key validation
if (process.env.API_KEYS) {
const apiKeys = process.env.API_KEYS.split(',');
apiKeys.forEach(key => {
expect(key.trim().length).toBeGreaterThan(16);
expect(key.trim()).not.toBe('default-key');
});
}
});
});
describe('Resource Limits Validation', () => {
test('should validate memory limits', () => {
const memoryUsage = process.memoryUsage();
// Validate current memory usage is reasonable
expect(memoryUsage.heapUsed).toBeGreaterThan(0);
expect(memoryUsage.heapTotal).toBeGreaterThan(memoryUsage.heapUsed);
// Check if memory limits are configured
if (process.env.NODE_OPTIONS) {
const nodeOptions = process.env.NODE_OPTIONS;
if (nodeOptions.includes('--max-old-space-size')) {
console.log(`Memory limit configured: ${nodeOptions}`);
}
}
console.log(`Current memory usage: ${Math.round(memoryUsage.heapUsed / 1024 / 1024)}MB`);
});
test('should validate CPU and concurrency limits', async () => {
const os = require('os');
const cpuCount = os.cpus().length;
expect(cpuCount).toBeGreaterThan(0);
console.log(`Available CPUs: ${cpuCount}`);
// Initialize system with CPU-appropriate limits
systemIntegration = SystemIntegration.getInstance();
await systemIntegration.initialize({
logLevel: 'info',
environment: 'test',
orchestrator: {
maxConcurrency: Math.min(cpuCount * 2, 20), // Reasonable concurrency
timeout: 30000
}
});
expect(systemIntegration.isReady()).toBe(true);
const config = systemIntegration.getConfig();
expect(config.orchestrator.maxConcurrency).toBeLessThanOrEqual(20);
expect(config.orchestrator.maxConcurrency).toBeGreaterThan(0);
});
});
describe('Logging and Monitoring Configuration', () => {
test('should validate logging configuration', async () => {
await configManager.initialize({
logLevel: 'info',
environment: 'test'
});
const config = configManager.getConfig();
// Validate log level
expect(['error', 'warn', 'info', 'debug', 'trace']).toContain(config.logLevel);
// Validate log output configuration
if (config.logging) {
if (config.logging.file) {
const logDir = path.dirname(config.logging.file);
expect(await fs.pathExists(logDir)).toBe(true);
}
}
});
test('should validate metrics collection configuration', () => {
// Check if metrics are enabled
const metricsEnabled = process.env.ENABLE_METRICS === 'true';
if (metricsEnabled) {
// Validate metrics endpoint configuration
if (process.env.METRICS_PORT) {
const port = parseInt(process.env.METRICS_PORT, 10);
expect(port).toBeGreaterThan(1024);
expect(port).toBeLessThan(65536);
}
// Validate metrics export format
if (process.env.METRICS_FORMAT) {
expect(['prometheus', 'json', 'influx']).toContain(process.env.METRICS_FORMAT);
}
}
});
});
});