Files
nupst/test/test.ts

94 lines
3.5 KiB
TypeScript
Raw Normal View History

import { assert, assertEquals, assertExists } from 'jsr:@std/assert@^1.0.0';
import { NupstSnmp } from '../ts/snmp/manager.ts';
import type { ISnmpConfig } from '../ts/snmp/types.ts';
2025-03-25 09:06:23 +00:00
import * as qenv from 'npm:@push.rocks/qenv@^6.0.0';
2025-03-25 09:06:23 +00:00
const testQenv = new qenv.Qenv('./', '.nogit/');
// Create an SNMP instance with debug enabled
const snmp = new NupstSnmp(true);
// Load the test configuration from .nogit/env.json
2025-03-26 13:13:01 +00:00
const testConfigV1 = await testQenv.getEnvVarOnDemandAsObject('testConfigV1');
const testConfigV3 = await testQenv.getEnvVarOnDemandAsObject('testConfigV3');
2025-03-25 09:06:23 +00:00
Deno.test('should log config', () => {
2025-03-26 13:13:01 +00:00
console.log(testConfigV1);
assert(true);
2025-03-25 09:06:23 +00:00
});
2025-03-26 13:13:01 +00:00
// Test with real UPS using the configuration from .nogit/env.json
Deno.test('Real UPS test v1', async () => {
2025-03-26 13:13:01 +00:00
try {
console.log('Testing with real UPS configuration...');
2025-03-26 13:13:01 +00:00
// Extract the correct SNMP config from the test configuration
const snmpConfig = testConfigV1.snmp as ISnmpConfig;
2025-03-26 13:13:01 +00:00
console.log('SNMP Config:');
console.log(` Host: ${snmpConfig.host}:${snmpConfig.port}`);
console.log(` Version: SNMPv${snmpConfig.version}`);
console.log(` UPS Model: ${snmpConfig.upsModel}`);
2025-03-26 13:13:01 +00:00
// Use a short timeout for testing
const testSnmpConfig = {
2025-03-26 13:13:01 +00:00
...snmpConfig,
timeout: Math.min(snmpConfig.timeout, 10000), // Use at most 10 seconds for testing
2025-03-25 09:06:23 +00:00
};
2025-03-26 13:13:01 +00:00
// Try to get the UPS status
const status = await snmp.getUpsStatus(testSnmpConfig);
2025-03-26 13:13:01 +00:00
console.log('UPS Status:');
console.log(` Power Status: ${status.powerStatus}`);
console.log(` Battery Capacity: ${status.batteryCapacity}%`);
console.log(` Runtime Remaining: ${status.batteryRuntime} minutes`);
2025-03-26 13:13:01 +00:00
// Just make sure we got valid data types back
assertExists(status);
assert(['online', 'onBattery', 'unknown'].includes(status.powerStatus));
assertEquals(typeof status.batteryCapacity, 'number');
assertEquals(typeof status.batteryRuntime, 'number');
2025-03-26 13:13:01 +00:00
} catch (error) {
console.log('Real UPS test failed:', error);
// Skip the test if we can't connect to the real UPS
console.log('Skipping this test since the UPS might not be available');
}
2025-03-25 09:06:23 +00:00
});
Deno.test('Real UPS test v3', async () => {
2025-03-25 09:06:23 +00:00
try {
console.log('Testing with real UPS configuration...');
2025-03-25 09:06:23 +00:00
// Extract the correct SNMP config from the test configuration
const snmpConfig = testConfigV3.snmp as ISnmpConfig;
2025-03-25 09:06:23 +00:00
console.log('SNMP Config:');
console.log(` Host: ${snmpConfig.host}:${snmpConfig.port}`);
console.log(` Version: SNMPv${snmpConfig.version}`);
console.log(` UPS Model: ${snmpConfig.upsModel}`);
2025-03-25 09:06:23 +00:00
// Use a short timeout for testing
const testSnmpConfig = {
2025-03-25 09:06:23 +00:00
...snmpConfig,
timeout: Math.min(snmpConfig.timeout, 10000), // Use at most 10 seconds for testing
2025-03-25 09:06:23 +00:00
};
2025-03-25 09:06:23 +00:00
// Try to get the UPS status
const status = await snmp.getUpsStatus(testSnmpConfig);
2025-03-25 09:06:23 +00:00
console.log('UPS Status:');
console.log(` Power Status: ${status.powerStatus}`);
console.log(` Battery Capacity: ${status.batteryCapacity}%`);
console.log(` Runtime Remaining: ${status.batteryRuntime} minutes`);
2025-03-25 09:06:23 +00:00
// Just make sure we got valid data types back
assertExists(status);
assert(['online', 'onBattery', 'unknown'].includes(status.powerStatus));
assertEquals(typeof status.batteryCapacity, 'number');
assertEquals(typeof status.batteryRuntime, 'number');
2025-03-25 09:06:23 +00:00
} catch (error) {
console.log('Real UPS test failed:', error);
// Skip the test if we can't connect to the real UPS
console.log('Skipping this test since the UPS might not be available');
}
});