@aj-archipelago/cortex
Version:
Cortex is a GraphQL API for AI. It provides a simple, extensible interface for using AI services from OpenAI, Azure and others.
126 lines (111 loc) ⢠4.19 kB
JavaScript
/**
* Environment Variable Validation Script
*
* This script validates that all required environment variables are present
* and properly configured before running tests. It fails fast if any critical
* environment variables are missing or misconfigured.
*/
import { existsSync } from 'fs';
import { readFileSync } from 'fs';
import { execSync } from 'child_process';
// Required environment variables for Azure tests
const REQUIRED_ENV_VARS = {
NODE_ENV: {
required: true,
expectedValue: 'test',
description: 'Must be set to "test" for test environment'
},
AZURE_STORAGE_CONNECTION_STRING: {
required: true,
expectedValue: 'UseDevelopmentStorage=true',
description: 'Must be set to use Azurite development storage'
},
AZURE_STORAGE_CONTAINER_NAME: {
required: true,
description: 'Must specify a single container name'
},
REDIS_CONNECTION_STRING: {
required: false,
description: 'Redis connection string (optional for tests)'
},
PORT: {
required: false,
description: 'Port for test server'
}
};
// Validate environment variables
function validateEnvironment() {
console.log('š Validating environment variables for Azure tests...\n');
let hasErrors = false;
const errors = [];
const warnings = [];
// Validate each required environment variable
for (const [varName, config] of Object.entries(REQUIRED_ENV_VARS)) {
const value = process.env[varName];
if (config.required && (!value || value.trim() === '')) {
errors.push(`ā Missing required environment variable: ${varName}`);
if (config.description) {
errors.push(` Description: ${config.description}`);
}
hasErrors = true;
} else if (config.expectedValue && value !== config.expectedValue) {
errors.push(`ā Invalid value for ${varName}: "${value}"`);
errors.push(` Expected: "${config.expectedValue}"`);
errors.push(` Description: ${config.description}`);
hasErrors = true;
} else if (value) {
console.log(`ā
${varName}: ${value}`);
} else if (!config.required) {
warnings.push(`ā ļø Optional variable ${varName} not set`);
}
}
// Validate container name
const containerName = process.env.AZURE_STORAGE_CONTAINER_NAME;
if (containerName) {
// Handle legacy comma-separated values (take the last one)
const containers = containerName.split(',').map(name => name.trim()).filter(name => name.length > 0);
const actualContainer = containers[containers.length - 1];
console.log(`ā
Container name: ${actualContainer}`);
// Warn if comma-separated (legacy format)
if (containers.length > 1) {
warnings.push(`ā ļø AZURE_STORAGE_CONTAINER_NAME contains comma-separated values (legacy format). Using: "${actualContainer}"`);
}
}
// Check if Azurite is available
try {
execSync('which azurite', { stdio: 'ignore' });
console.log('ā
Azurite is installed and available');
} catch (error) {
errors.push('ā Azurite is not installed or not in PATH');
errors.push(' Install with: npm install -g azurite');
hasErrors = true;
}
// Print warnings
if (warnings.length > 0) {
console.log('\nā ļø Warnings:');
warnings.forEach(warning => console.log(warning));
}
// Print errors and exit if any
if (hasErrors) {
console.log('\nā Environment validation failed:');
errors.forEach(error => console.log(error));
console.log('\nš” To fix these issues:');
console.log('1. Ensure .env.test.azure exists and contains all required variables');
console.log('2. Install Azurite: npm install -g azurite');
console.log('3. Check that DOTENV_CONFIG_PATH=.env.test.azure is set when running tests');
process.exit(1);
}
console.log('\nā
Environment validation passed! Ready to run Azure tests.');
return true;
}
// Run validation if called directly
if (import.meta.url === `file://${process.argv[1]}`) {
try {
validateEnvironment();
} catch (error) {
console.error('ā Environment validation failed:', error.message);
process.exit(1);
}
}
export { validateEnvironment };