import { UnifiedTpLinkApi } from '../../src/unified-api'; export const testUnifiedAPI = async (email?: string, password?: string) => { console.log('Starting Unified API Integration Test...\n'); const api = new UnifiedTpLinkApi({ email: email || process.env.TPLINK_EMAIL, password: password || process.env.TPLINK_PASSWORD, }); try { // Step 1: Discovery console.log('1. Discovering devices...'); const { kasaDevices, tapoDevices, allDevices } = await api.discover(); console.log(` Found ${allDevices.length} devices\n`); if (allDevices.length === 0) { console.log('No devices found. Please ensure devices are on the network.'); return false; } // Select first device for testing const testDevice = allDevices[0]; console.log(`2. Testing device: ${testDevice.name} (${testDevice.model})`); console.log(` Protocol: ${testDevice.protocol}`); console.log(` IP: ${testDevice.host}\n`); // Step 3: Get device info console.log('3. Getting device information...'); const deviceInfo = await api.getDeviceInfo(testDevice.id); console.log(' Device info retrieved successfully\n'); // Step 4: Test power control console.log('4. Testing power control...'); await api.turnOn(testDevice.id); console.log(' Device turned ON'); await new Promise((resolve) => setTimeout(resolve, 2000)); await api.turnOff(testDevice.id); console.log(' Device turned OFF'); await new Promise((resolve) => setTimeout(resolve, 2000)); await api.turnOn(testDevice.id); console.log(' Device turned back ON\n'); // Step 5: Test brightness (if supported) if (testDevice.deviceType.includes('bulb') || testDevice.deviceType.includes('BULB')) { console.log('5. Testing brightness control...'); await api.setBrightness(testDevice.id, 30); console.log(' Brightness set to 30%'); await new Promise((resolve) => setTimeout(resolve, 2000)); await api.setBrightness(testDevice.id, 70); console.log(' Brightness set to 70%\n'); // Step 6: Test color (if supported) console.log('6. Testing color control...'); // Red await api.setRgb(testDevice.id, 255, 0, 0); console.log(' Color set to RED'); await new Promise((resolve) => setTimeout(resolve, 2000)); // Green await api.setRgb(testDevice.id, 0, 255, 0); console.log(' Color set to GREEN'); await new Promise((resolve) => setTimeout(resolve, 2000)); // Blue await api.setRgb(testDevice.id, 0, 0, 255); console.log(' Color set to BLUE'); await new Promise((resolve) => setTimeout(resolve, 2000)); // HSL test await api.setHsl(testDevice.id, 60, 100, 50); // Yellow console.log(' Color set to YELLOW (via HSL)\n'); } // Step 7: Test energy monitoring (if supported) try { console.log('7. Testing energy monitoring...'); const energy = await api.getEnergyUsage(testDevice.id); console.log(` Current power: ${energy.power_mw / 1000}W`); console.log(` Total energy: ${energy.total_wh}Wh\n`); } catch { console.log('7. Energy monitoring not supported on this device\n'); } console.log('\n✅ Integration test completed successfully!'); return true; } catch (error) { console.error('\n❌ Integration test failed:', error); return false; } };