-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-sdk.js
More file actions
92 lines (81 loc) · 3.4 KB
/
test-sdk.js
File metadata and controls
92 lines (81 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
* Test file for ZeroCarbon Node.js SDK
*
* Usage:
* 1. Make sure dev server is running: npm run dev
* 2. Set API key: $env:ZEROCARBON_API_KEY = "your_key_here"
* 3. Run: node test-sdk.js
*/
const { ZeroCarbon } = require('./dist/index.js');
const API_KEY = process.env.ZEROCARBON_API_KEY || 'YOUR_API_KEY_HERE';
const client = new ZeroCarbon({
apiKey: API_KEY,
baseUrl: 'http://localhost:3000/api/v1', // Local development
testMode: true // Set to false for production
});
async function testSDK() {
console.log('🧪 Testing ZeroCarbon Node.js SDK...\n');
console.log(`✓ API Key: ${API_KEY.substring(0, 15)}...`);
console.log(`✓ Base URL: http://localhost:3000/api/v1`);
console.log(`✓ Test Mode: enabled\n`);
try {
// Test 1: Flight Emissions Calculation
console.log('1️⃣ Testing flight emissions calculation...');
const flightResult = await client.calculate.flight({
origin: 'DEL',
destination: 'BOM',
cabin_class: 'economy',
passengers: 1,
round_trip: false
});
console.log('✅ Flight emissions:', flightResult.emissions_kg_co2e, 'kg CO2e');
console.log(' Distance:', flightResult.distance_km, 'km');
console.log('');
// Test 2: Electricity Emissions Calculation
console.log('2️⃣ Testing electricity emissions calculation...');
const electricityResult = await client.calculate.electricity({
amountKwh: 1000,
country: 'IN',
state: 'MH'
});
console.log('✅ Electricity emissions:', electricityResult.emissions_kg_co2e, 'kg CO2e');
console.log(' Emission factor:', electricityResult.emission_factor, 'kg/kWh');
console.log('');
// Test 3: Fuel Combustion Calculation
console.log('3️⃣ Testing fuel combustion calculation...');
const fuelResult = await client.calculate.fuel({
fuelType: 'diesel',
amount: 100,
unit: 'liters'
});
console.log('✅ Fuel emissions:', fuelResult.emissions_kg_co2e, 'kg CO2e');
console.log('');
// Test 4: Get Offset Recommendations
console.log('4️⃣ Testing offset recommendations...');
const offsetsResult = await client.offsets.getRecommendations({
emissionsKgCo2e: 5000,
budgetUsd: 100,
location: 'India'
});
console.log('✅ Found', offsetsResult.recommendations?.length || 0, 'recommendations');
if (offsetsResult.recommendations && offsetsResult.recommendations.length > 0) {
const top = offsetsResult.recommendations[0];
console.log(` Top: ${top.name} - $${top.price_per_tonne}/tonne (Score: ${top.ai_score})`);
}
console.log('');
console.log('🎉 All tests passed! SDK is working perfectly!\n');
console.log('📊 Summary:');
console.log(` ✓ Flight calculation: ${flightResult.emissions_kg_co2e} kg CO2e`);
console.log(` ✓ Electricity calculation: ${electricityResult.emissions_kg_co2e} kg CO2e`);
console.log(` ✓ Fuel calculation: ${fuelResult.emissions_kg_co2e} kg CO2e`);
console.log(` ✓ Offset recommendations: ${offsetsResult.recommendations?.length || 0} projects`);
} catch (error) {
console.error('❌ Error:', error.message);
console.error('\n💡 Make sure:');
console.error(' 1. Dev server is running: npm run dev');
console.error(' 2. Your API key is valid (get from /company/api-keys)');
console.error(' 3. Database is connected and seeded with projects');
}
}
// Run tests
testSDK();