UNPKG

@homebridge-plugins/homebridge-air

Version:

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

76 lines 3.66 kB
import { describe, expect, it } from 'vitest'; import { AirNowUrl, AqicnUrl } from '../settings.js'; describe('aPI Performance and Security Improvements', () => { it('should use HTTPS for both API endpoints for security', () => { // Verify both APIs use HTTPS for secure connections expect(AirNowUrl).toMatch(/^https:\/\//); expect(AqicnUrl).toMatch(/^https:\/\//); // Verify specific URLs match expected endpoints expect(AirNowUrl).toBe('https://www.airnowapi.org/aq/observation/'); expect(AqicnUrl).toBe('https://api.waqi.info/feed/'); }); it('should have proper API URL structures for both providers', () => { // AirNow URL should include the observation endpoint expect(AirNowUrl).toContain('airnowapi.org'); expect(AirNowUrl).toContain('/aq/observation/'); // AQICN URL should include the feed endpoint expect(AqicnUrl).toContain('api.waqi.info'); expect(AqicnUrl).toContain('/feed/'); }); it('should validate request timeout configuration', () => { // Test that timeout values are reasonable (10 seconds) const expectedTimeout = 10000; expect(expectedTimeout).toBeGreaterThan(5000); // At least 5 seconds expect(expectedTimeout).toBeLessThanOrEqual(30000); // No more than 30 seconds }); it('should validate cache configuration for performance', () => { // Test cache max age is reasonable (1 minute) const expectedCacheAge = 60000; expect(expectedCacheAge).toBeGreaterThan(30000); // At least 30 seconds expect(expectedCacheAge).toBeLessThanOrEqual(300000); // No more than 5 minutes }); it('should handle different error types appropriately', () => { // Test error handling for different network error scenarios const timeoutErrors = ['UND_ERR_CONNECT_TIMEOUT', 'ETIMEDOUT']; const networkErrors = ['ENOTFOUND', 'ECONNREFUSED']; // Verify timeout errors are recognized expect(timeoutErrors.includes('UND_ERR_CONNECT_TIMEOUT')).toBe(true); expect(timeoutErrors.includes('ETIMEDOUT')).toBe(true); // Verify network errors are recognized expect(networkErrors.includes('ENOTFOUND')).toBe(true); expect(networkErrors.includes('ECONNREFUSED')).toBe(true); }); it('should validate API response structure requirements', () => { // Test AQICN response validation requirements const validAqicnResponse = { status: 'ok', data: { aqi: 50, iaqi: { pm25: { v: 45 }, }, }, }; expect(validAqicnResponse.status).toBe('ok'); expect(validAqicnResponse.data.aqi).toBeDefined(); expect(typeof validAqicnResponse.data.aqi).toBe('number'); // Test AirNow response validation requirements const validAirNowResponse = [ { ParameterName: 'PM2.5', AQI: 45, }, ]; expect(Array.isArray(validAirNowResponse)).toBe(true); expect(validAirNowResponse.length).toBeGreaterThan(0); expect(validAirNowResponse[0].ParameterName).toBeDefined(); expect(validAirNowResponse[0].AQI).toBeDefined(); }); it('should validate user agent format for API requests', () => { const userAgent = 'homebridge-air/1.0.5'; expect(userAgent).toMatch(/^homebridge-air\/\d+\.\d+\.\d+$/); expect(userAgent).toContain('homebridge-air'); expect(userAgent).toContain('1.0.5'); }); }); //# sourceMappingURL=api-performance.test.js.map