import { KasaLightState } from '../src/lightstate/kasa-lightstate'; import { UnifiedDeviceFactory, DeviceProtocol } from '../src/unified-device-factory'; import { encrypt, decrypt } from '../src/utils/kasa-utils'; import { describe, it, expect } from '@jest/globals'; describe('Kasa Device Tests', () => { describe('Kasa Encryption/Decryption', () => { it('should encrypt and decrypt Kasa commands correctly', () => { const command = '{"system":{"get_sysinfo":null}}'; const encrypted = encrypt(command); const decrypted = decrypt(encrypted); expect(decrypted.toString()).toBe(command); }); it('should handle complex Kasa commands', () => { const command = JSON.stringify({ 'smartlife.iot.smartbulb.lightingservice': { transition_light_state: { on_off: 1, brightness: 75, hue: 120, saturation: 100, }, }, }); const encrypted = encrypt(command); const decrypted = decrypt(encrypted); expect(decrypted.toString()).toBe(command); }); }); describe('Kasa LightState', () => { it('should create basic on/off state', () => { const state = new KasaLightState(); state.on(); const values = state.getValues(); expect(values.on_off).toBe(true); }); it('should create state with brightness', () => { const state = new KasaLightState(); state.on().brightness(50); const values = state.getValues(); expect(values.on_off).toBe(true); expect(values.brightness).toBe(50); }); it('should create state with RGB color', () => { const state = new KasaLightState(); state.on().rgb([255, 0, 0]); const values = state.getValues(); expect(values.on_off).toBe(true); expect(values.hue).toBeDefined(); expect(values.saturation).toBeDefined(); expect(values.brightness).toBeDefined(); }); it('should create state with HSV color', () => { const state = new KasaLightState(); state.on().hsv([180, 100, 75]); const values = state.getValues(); expect(values.on_off).toBe(true); expect(values.hue).toBe(180); expect(values.saturation).toBe(100); expect(values.brightness).toBe(75); }); it('should create state with color temperature', () => { const state = new KasaLightState(); state.on().colorTemperature(4000); const values = state.getValues(); expect(values.on_off).toBe(true); expect(values.color_temp).toBe(4000); }); it('should support transition duration', () => { const state = new KasaLightState(); state.on().brightness(100).transition(2000); const values = state.getValues(); expect(values.transition_period).toBe(2000); }); it('should handle mode changes', () => { const state = new KasaLightState(); state.on().mode('circadian'); const values = state.getValues(); expect(values.mode).toBe('circadian'); }); }); describe('Kasa Device Factory', () => { it('should detect Kasa device models', () => { const models = ['HS100', 'HS110', 'KP115', 'KL130', 'LB130', 'EP40']; models.forEach((model) => { const protocol = UnifiedDeviceFactory.detectProtocol({ model }); expect(protocol).toBe(DeviceProtocol.KASA); }); }); it('should create Kasa bulb device', () => { const deviceInfo = { deviceId: 'test123', alias: 'Test Bulb', model: 'KL130', address: '192.168.1.10', mic_type: 'IOT.SMARTBULB', }; const device = UnifiedDeviceFactory.createUnifiedDevice(deviceInfo); expect(device.id).toBe('test123'); expect(device.name).toBe('Test Bulb'); expect(device.protocol).toBe(DeviceProtocol.KASA); }); it('should create Kasa plug device', () => { const deviceInfo = { deviceId: 'plug456', alias: 'Test Plug', model: 'HS110', address: '192.168.1.11', mic_type: 'IOT.SMARTPLUGSWITCH', }; const device = UnifiedDeviceFactory.createUnifiedDevice(deviceInfo); expect(device.id).toBe('plug456'); expect(device.name).toBe('Test Plug'); expect(device.protocol).toBe(DeviceProtocol.KASA); }); }); });