import {expect} from 'chai'; import * as bridgeValidation from './bridge-validation'; import { mDNSSearch } from "./mDNS"; describe('bridge-validation', () => { describe('#parseXmlDescription()', () => { it('should extract description from XML', () => { const data = '\n' + '\n' + '1\n' + '0\n' + '\n' + 'https://192.168.1.130:80/\n' + '\n' + 'urn:schemas-upnp-org:device:Basic:1\n' + 'Philips hue (192.168.1.130)\n' + 'Royal Philips Electronicshttps://www.philips.com\n' + 'Philips hue Personal Wireless Lighting\n' + 'Philips hue bridge 2012\n' + '929000226503\n' + 'https://www.meethue.com\n' + '001788102201\n' + 'uuid:2f402f80-da50-11e1-9b23-001788102201\n' + 'index.html\n' + '\n' + '\n' + 'image/png\n' + '48\n' + '48\n' + '24\n' + 'hue_logo_0.png\n' + '\n' + '\n' + 'image/png\n' + '120\n' + '120\n' + '24\n' + 'hue_logo_3.png\n' + '\n' + '\n' + '\n' + ''; const result = bridgeValidation.parseXmlDescription(data); expect(result).to.have.property('name').to.equal('Philips hue (192.168.1.130)'); expect(result).to.have.property('manufacturer').to.equal('Royal Philips Electronics'); expect(result).to.have.property('ipaddress').to.equal('192.168.1.130'); expect(result).to.have.property('model'); expect(result.model).to.have.property('number').to.equal('929000226503'); expect(result.model).to.have.property('description').to.equal('Philips hue Personal Wireless Lighting'); expect(result.model).to.have.property('name').to.equal('Philips hue bridge 2012'); expect(result.model).to.have.property('serial').to.equal('001788102201'); expect(result).to.have.property('icons'); expect(result.icons).to.be.instanceOf(Array).to.have.length.greaterThan(0); // @ts-ignore let icon = result.icons[0]; expect(icon).to.have.property('mimetype').to.equal('image/png'); expect(icon).to.have.property('height').to.equal('48'); expect(icon).to.have.property('width').to.equal('48'); expect(icon).to.have.property('depth').to.equal('24'); expect(icon).to.have.property('url').to.equal('hue_logo_0.png'); // @ts-ignore icon = result.icons[1]; expect(icon).to.have.property('mimetype').to.equal('image/png'); expect(icon).to.have.property('height').to.equal('120'); expect(icon).to.have.property('width').to.equal('120'); expect(icon).to.have.property('depth').to.equal('24'); expect(icon).to.have.property('url').to.equal('hue_logo_3.png'); }); }); describe('#getBridgeConfig()', () => { it('should get config on a valid bridge', async function(){ this.timeout(10000); const bridges = await new mDNSSearch().search(); expect(bridges).to.have.length.greaterThan(0); const bridge = bridges[0]; const config = await bridgeValidation.getBridgeConfig(bridge, 5000); expect(config).to.have.property('name'); expect(config).to.have.property('ipaddress').to.equal(bridge.internalipaddress); expect(config).to.have.property('modelid'); expect(config).to.have.property('swversion'); }); it('should fail for invalid ip address', async function() { const ipAddress = '10.0.0.1' , timeout = 2000 ; // Make test time out double that of the actual timeout on the request this.timeout(timeout * 2); try { await bridgeValidation.getBridgeConfig({internalipaddress: ipAddress}, timeout); expect.fail('Should have failed'); } catch (err: any) { expect(err.message).to.contain('network timeout'); } }); }); describe('#getBridgeDescription()', () => { it('should get config on a valid bridge', async function(){ this.timeout(10000); const bridges = await new mDNSSearch().search(); expect(bridges).to.have.length.greaterThan(0); const bridge = bridges[0]; const config = await bridgeValidation.getBridgeDescription(bridge, 5000); expect(config).to.have.property('name').to.include('Philips hue'); expect(config).to.have.property('ipaddress').to.equal(bridge.internalipaddress); expect(config).to.have.property('model'); expect(config.model).to.have.property('number').to.equal('BSB002'); expect(config.model).to.have.property('name'); expect(config.model).to.have.property('description'); }); it('should fail for invalid ip address', async function() { const ipAddress = '10.0.0.1' , timeout = 2000 ; // Make test time out double that of the actual timeout on the request this.timeout(timeout * 2); try { await bridgeValidation.getBridgeDescription({internalipaddress: ipAddress}, timeout); expect.fail('Should have failed'); } catch (err: any) { expect(err.message).to.contain('network timeout'); } }); }); });