UNPKG

@homebridge-plugins/homebridge-air

Version:

The AirNow plugin allows you to monitor the current AirQuality for your Zip Code from HomeKit and Siri.

141 lines 6.56 kB
import { describe, expect, it } from 'vitest'; import { AqicnUrl, resolveAqicnLocationSegment } from '../settings.js'; /** * Test the AQICN URL construction logic to ensure it supports * various URL patterns as requested in issue #45 */ describe('aQICN URL Construction', () => { /** * Mock the URL construction logic from AirQualitySensor.refreshStatus() * This simulates lines 188-199 in airqualitysensor.ts */ function constructAqicnUrl(device) { const AqicnCurrentObservationBy = resolveAqicnLocationSegment({ city: device.city, latitude: device.latitude, longitude: device.longitude, }); return `${AqicnUrl}${AqicnCurrentObservationBy}${AqicnCurrentObservationBy ? '/' : ''}?token=${device.apiKey}`; } it('should support geo coordinates (existing functionality)', () => { const device = { latitude: 47.5, longitude: 8.7, apiKey: 'test-api-key', }; const url = constructAqicnUrl(device); expect(url).toBe('https://api.waqi.info/feed/geo:47.5;8.7/?token=test-api-key'); }); it('should support simple city names (existing functionality)', () => { const device = { city: 'winterthur', apiKey: 'test-api-key', }; const url = constructAqicnUrl(device); expect(url).toBe('https://api.waqi.info/feed/winterthur/?token=test-api-key'); }); it('should support /city/country/cityname syntax', () => { const device = { city: '/city/switzerland/winterthur-veltheim', apiKey: 'test-api-key', }; const url = constructAqicnUrl(device); expect(url).toBe('https://api.waqi.info/feed/city/switzerland/winterthur-veltheim/?token=test-api-key'); }); it('should support city/country/cityname syntax without leading slash', () => { const device = { city: 'city/switzerland/tanikon', apiKey: 'test-api-key', }; const url = constructAqicnUrl(device); expect(url).toBe('https://api.waqi.info/feed/city/switzerland/tanikon/?token=test-api-key'); }); it('should support /station/@stationid syntax', () => { const device = { city: '/station/@92323', apiKey: 'test-api-key', }; const url = constructAqicnUrl(device); expect(url).toBe('https://api.waqi.info/feed/station/@92323/?token=test-api-key'); }); it('should support station/@stationid syntax without leading slash', () => { const device = { city: 'station/@231133', apiKey: 'test-api-key', }; const url = constructAqicnUrl(device); expect(url).toBe('https://api.waqi.info/feed/station/@231133/?token=test-api-key'); }); it('should support /station/station-name/locale syntax', () => { const device = { city: '/station/bielsko-bia%C5%82a-poland-bielsko-biala-urodzajna/pl', apiKey: 'test-api-key', }; const url = constructAqicnUrl(device); expect(url).toBe('https://api.waqi.info/feed/station/bielsko-bia%C5%82a-poland-bielsko-biala-urodzajna/pl/?token=test-api-key'); }); it('should support station/station-name/locale syntax without leading slash', () => { const device = { city: 'station/bielsko-bia%C5%82a-poland-bielsko-biala-urodzajna/pl', apiKey: 'test-api-key', }; const url = constructAqicnUrl(device); expect(url).toBe('https://api.waqi.info/feed/station/bielsko-bia%C5%82a-poland-bielsko-biala-urodzajna/pl/?token=test-api-key'); }); it('should prioritize explicit station/city paths over geo coordinates', () => { const device = { latitude: 47.5, longitude: 8.7, city: '/city/switzerland/winterthur-veltheim', // Should be ignored when coordinates are present apiKey: 'test-api-key', }; const url = constructAqicnUrl(device); expect(url).toBe('https://api.waqi.info/feed/city/switzerland/winterthur-veltheim/?token=test-api-key'); }); it('should support full AQICN station URL input', () => { const device = { city: 'https://aqicn.org/station/@92323/', apiKey: 'test-api-key', }; const url = constructAqicnUrl(device); expect(url).toBe('https://api.waqi.info/feed/station/@92323/?token=test-api-key'); }); it('should handle empty city gracefully', () => { const device = { city: '', apiKey: 'test-api-key', }; const url = constructAqicnUrl(device); expect(url).toBe('https://api.waqi.info/feed/?token=test-api-key'); }); it('should handle undefined city gracefully', () => { const device = { apiKey: 'test-api-key', }; const url = constructAqicnUrl(device); expect(url).toBe('https://api.waqi.info/feed/?token=test-api-key'); }); // Integration test with real-world examples from issue #45 it('should support all example URLs from issue #45', () => { const examples = [ // Original examples from the issue { input: '/city/switzerland/winterthur-veltheim', expected: 'https://api.waqi.info/feed/city/switzerland/winterthur-veltheim/?token=test-key' }, { input: '/city/switzerland/tanikon', expected: 'https://api.waqi.info/feed/city/switzerland/tanikon/?token=test-key' }, { input: '/station/@92323', expected: 'https://api.waqi.info/feed/station/@92323/?token=test-key' }, { input: '/station/@231133', expected: 'https://api.waqi.info/feed/station/@231133/?token=test-key' }, { input: '/station/bielsko-bia%C5%82a-poland-bielsko-biala-urodzajna/pl', expected: 'https://api.waqi.info/feed/station/bielsko-bia%C5%82a-poland-bielsko-biala-urodzajna/pl/?token=test-key' }, // Backward compatibility examples { input: 'winterthur', expected: 'https://api.waqi.info/feed/winterthur/?token=test-key' }, { input: 'beijing', expected: 'https://api.waqi.info/feed/beijing/?token=test-key' }, ]; examples.forEach(({ input, expected }) => { const device = { city: input, apiKey: 'test-key', }; const url = constructAqicnUrl(device); expect(url).toBe(expected); }); }); }); //# sourceMappingURL=aqicn-url.test.js.map