gemini-cost-tracker
Version:
CLI tool to display token usage and costs for Gemini and Vertex AI
80 lines ⢠3.65 kB
JavaScript
import chalk from 'chalk';
import ora from 'ora';
import { AppError } from '../types/index.js';
import { AuthManager } from '../services/auth/authManager.js';
import { RealUsageClient } from '../services/api/realUsageClient.js';
export async function testCommand() {
const spinner = ora('Testing connections...').start();
try {
// Initialize authentication
const authManager = new AuthManager();
await authManager.initialize();
// Validate credentials
spinner.text = 'Validating credentials...';
const isValid = await authManager.validateCredentials();
if (!isValid) {
spinner.stop();
console.log(chalk.red('ā Credentials validation failed'));
console.log(chalk.yellow('Run "gemini-cost-demo config" to set up authentication'));
return;
}
console.log(chalk.green('ā
Credentials validation passed'));
// Test real usage client connections
spinner.text = 'Testing Google Cloud API connections...';
const realUsageClient = new RealUsageClient(authManager);
const connections = await realUsageClient.testConnections();
spinner.stop();
console.log(chalk.bold.blue('\nš Connection Test Results:'));
// Logging API test
if (connections.logging) {
console.log(chalk.green('ā
Cloud Logging API: Connected'));
console.log(chalk.gray(' Can access API usage logs'));
}
else {
console.log(chalk.yellow('ā ļø Cloud Logging API: Failed'));
console.log(chalk.gray(' May need to enable Cloud Logging API'));
}
// Monitoring API test
if (connections.monitoring) {
console.log(chalk.green('ā
Cloud Monitoring API: Connected'));
console.log(chalk.gray(' Can access usage metrics'));
}
else {
console.log(chalk.yellow('ā ļø Cloud Monitoring API: Failed'));
console.log(chalk.gray(' May need to enable Cloud Monitoring API'));
}
// Overall status
console.log(chalk.bold('\nš Overall Status:'));
if (connections.logging || connections.monitoring) {
console.log(chalk.green('ā
Real data collection partially available'));
console.log(chalk.gray(' Use --real-data flag to enable real data collection'));
}
else {
console.log(chalk.yellow('ā ļø Real data collection not available'));
console.log(chalk.gray(' Will fall back to mock data'));
}
// Recommendations
console.log(chalk.bold('\nš” Recommendations:'));
if (!connections.logging) {
console.log(chalk.gray('⢠Enable Cloud Logging API:'));
console.log(chalk.cyan(' gcloud services enable logging.googleapis.com'));
}
if (!connections.monitoring) {
console.log(chalk.gray('⢠Enable Cloud Monitoring API:'));
console.log(chalk.cyan(' gcloud services enable monitoring.googleapis.com'));
}
console.log(chalk.gray('⢠Ensure proper IAM permissions for:'));
console.log(chalk.cyan(' - Logging Viewer'));
console.log(chalk.cyan(' - Monitoring Viewer'));
}
catch (error) {
spinner.stop();
if (error instanceof AppError) {
console.log(chalk.red(`ā Test failed: ${error.message}`));
}
else {
console.log(chalk.red(`ā Unexpected error: ${error instanceof Error ? error.message : 'Unknown error'}`));
}
}
}
//# sourceMappingURL=test.js.map