import { TapoLightState } from '../src/lightstate/tapo-lightstate'; import { TapoApi } from '../src/protocols/tapo-api'; import { UnifiedDeviceFactory, DeviceProtocol } from '../src/unified-device-factory'; import { base64Encode, base64Decode } from '../src/utils/cipher'; import { describe, it, expect } from '@jest/globals'; describe('Tapo Device Tests', () => { describe('Tapo Base64 Encoding', () => { it('should encode and decode base64 correctly', () => { const original = 'Test Device Name'; const encoded = base64Encode(original); const decoded = base64Decode(encoded); expect(decoded).toBe(original); }); it('should handle special characters', () => { const original = 'Device-Name_123 #Special!'; const encoded = base64Encode(original); const decoded = base64Decode(encoded); expect(decoded).toBe(original); }); }); describe('Tapo LightState', () => { it('should create basic on/off state', () => { const state = new TapoLightState(); state.on(); const values = state.getValues(); expect(values.device_on).toBe(true); }); it('should create state with brightness', () => { const state = new TapoLightState(); state.on().brightness(75); const values = state.getValues(); expect(values.device_on).toBe(true); expect(values.brightness).toBe(75); }); it('should create state with RGB color', () => { const state = new TapoLightState(); state.on().rgb([0, 255, 0]); // Green const values = state.getValues(); expect(values.device_on).toBe(true); expect(values.hue).toBeDefined(); expect(values.saturation).toBeDefined(); }); it('should create state with HSL color', () => { const state = new TapoLightState(); state.on().hsl(240, 100, 50); // Blue const values = state.getValues(); expect(values.device_on).toBe(true); expect(values.hue).toBe(240); expect(values.saturation).toBe(100); }); it('should create state with color temperature', () => { const state = new TapoLightState(); state.on().colorTemperature(3000); const values = state.getValues(); expect(values.device_on).toBe(true); expect(values.color_temp).toBe(3000); }); it('should support transition duration', () => { const state = new TapoLightState(); state.on().brightness(50).transition(3000); const values = state.getValues(); expect(values.transition_period).toBe(3000); }); it('should handle preset patterns', () => { const state = new TapoLightState(); state.on().brightness(80); const values = state.getValues(); expect(values.device_on).toBe(true); expect(values.brightness).toBe(80); }); }); describe('Tapo Device Factory', () => { it('should detect Tapo device types', () => { const tapoTypes = [{ deviceType: 'SMART.TAPOBULB' }, { device_type: 'SMART.TAPOSTRIP' }, { deviceType: 'SMART.TAPOPLUG' }, { model: 'Tapo L530' }]; tapoTypes.forEach((device) => { const protocol = UnifiedDeviceFactory.detectProtocol(device); expect(protocol).toBe(DeviceProtocol.TAPO); }); }); it('should create Tapo bulb device', () => { const deviceInfo = { deviceMac: 'AA:BB:CC:DD:EE:FF', deviceName: 'Tapo Bulb', deviceModel: 'L530', ip: '192.168.1.20', deviceType: 'SMART.TAPOBULB', }; const device = UnifiedDeviceFactory.createUnifiedDevice(deviceInfo); expect(device.id).toBe('AA:BB:CC:DD:EE:FF'); expect(device.name).toBe('Tapo Bulb'); expect(device.protocol).toBe(DeviceProtocol.TAPO); expect(device.model).toBe('L530'); }); it('should create Tapo strip device', () => { const deviceInfo = { deviceMac: 'FF:EE:DD:CC:BB:AA', deviceName: 'Tapo Strip', deviceModel: 'L900', ip: '192.168.1.21', deviceType: 'SMART.TAPOSTRIP', }; const device = UnifiedDeviceFactory.createUnifiedDevice(deviceInfo); expect(device.id).toBe('FF:EE:DD:CC:BB:AA'); expect(device.name).toBe('Tapo Strip'); expect(device.protocol).toBe(DeviceProtocol.TAPO); expect(device.model).toBe('L900'); }); it('should create Tapo plug device', () => { const deviceInfo = { deviceMac: '11:22:33:44:55:66', deviceName: 'Tapo Plug', deviceModel: 'P110', ip: '192.168.1.22', deviceType: 'SMART.TAPOPLUG', }; const device = UnifiedDeviceFactory.createUnifiedDevice(deviceInfo); expect(device.id).toBe('11:22:33:44:55:66'); expect(device.name).toBe('Tapo Plug'); expect(device.protocol).toBe(DeviceProtocol.TAPO); expect(device.model).toBe('P110'); }); }); describe('Tapo API Authentication', () => { it('should initialize Tapo API with email and password', () => { const api = new TapoApi({ email: 'test@example.com', password: 'testpass', }); expect(api).toBeDefined(); }); it('should initialize Tapo API with token', () => { const api = new TapoApi({ email: 'test@example.com', password: 'testpass', token: 'existing-token', }); expect(api).toBeDefined(); }); }); });