UNPKG

@homebridge-plugins/homebridge-resideo

Version:

The Resideo plugin allows you to access your Resideo device(s) from HomeKit.

86 lines 3.15 kB
import axios from 'axios'; import { beforeEach, describe, expect, it, vi } from 'vitest'; import { ResideoPlatform } from './platform.js'; vi.mock('axios'); describe('resideoPlatform', () => { let platform; let log; let config; let api; beforeEach(() => { log = { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn(), }; config = { platform: 'Resideo', name: 'Test Platform', credentials: { accessToken: 'testAccessToken', consumerKey: 'testConsumerKey', consumerSecret: 'testConsumerSecret', refreshToken: 'testRefreshToken', }, options: { refreshRate: 120, pushRate: 0.1, devices: [], }, }; api = { hap: { uuid: { generate: vi.fn().mockReturnValue('test-uuid'), }, }, platformAccessory: vi.fn().mockImplementation((name, uuid) => ({ displayName: name, UUID: uuid, context: {}, })), user: { configPath: vi.fn().mockReturnValue('/path/to/config.json'), }, on: vi.fn(), updatePlatformAccessories: vi.fn(), }; platform = new ResideoPlatform(log, config, api); }); it('should initialize platform with given config', () => { expect(platform.config.name).toBe('Test Platform'); expect(platform.config.credentials?.accessToken).toBe('testAccessToken'); }); it('should verify config correctly', () => { expect(() => platform.verifyConfig()).not.toThrow(); }); it('should throw error if refresh rate is less than 30', () => { if (platform.config.options) { platform.config.options.refreshRate = 20; } expect(() => platform.verifyConfig()).toThrow('Refresh Rate must be above 30 seconds.'); }); it('should refresh access token', async () => { axios.post.mockResolvedValue({ data: { access_token: 'newAccessToken', refresh_token: 'newRefreshToken', }, }); await platform.refreshAccessToken(); expect(platform.config.credentials?.accessToken).toBe('newAccessToken'); expect(platform.config.credentials?.refreshToken).toBe('newRefreshToken'); }); it('should discover locations', async () => { const mockLocations = [{ locationID: '1', name: 'Location 1', devices: [] }]; axios.get.mockResolvedValue({ data: mockLocations }); const locations = await platform.discoverlocations(); expect(locations).toEqual(mockLocations); }); it('should handle error during location discovery', async () => { axios.get.mockRejectedValue(new Error('Network Error')); await expect(platform.discoverlocations()).rejects.toThrow('Network Error'); }); }); //# sourceMappingURL=platform.test.js.map