-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3bec1ac
commit d8ec68d
Showing
7 changed files
with
377 additions
and
217 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import type { API } from 'homebridge' | ||
|
||
import { describe, expect, it, vi } from 'vitest' | ||
|
||
import registerPlatform from './index.js' | ||
import { ResideoPlatform } from './platform.js' | ||
import { PLATFORM_NAME, PLUGIN_NAME } from './settings.js' | ||
|
||
describe('index.ts', () => { | ||
it('should register the platform with homebridge', () => { | ||
const apiMock: API = { | ||
registerPlatform: vi.fn(), | ||
} as unknown as API | ||
|
||
registerPlatform(apiMock) | ||
|
||
expect(apiMock.registerPlatform).toHaveBeenCalledWith(PLUGIN_NAME, PLATFORM_NAME, ResideoPlatform) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import type { API, Logging } from 'homebridge' | ||
import type { MockedFunction } from 'vitest' | ||
|
||
import type { ResideoPlatformConfig } from './settings.js' | ||
|
||
import axios from 'axios' | ||
import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
||
import { ResideoPlatform } from './platform.js' | ||
|
||
vi.mock('axios') | ||
|
||
describe('resideoPlatform', () => { | ||
let platform: ResideoPlatform | ||
let log: Logging | ||
let config: ResideoPlatformConfig | ||
let api: API | ||
|
||
beforeEach(() => { | ||
log = { | ||
info: vi.fn(), | ||
warn: vi.fn(), | ||
error: vi.fn(), | ||
debug: vi.fn(), | ||
} as unknown as Logging | ||
|
||
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(), | ||
} as unknown as API | ||
|
||
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 as MockedFunction<typeof 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 as MockedFunction<typeof axios.get>).mockResolvedValue({ data: mockLocations }) | ||
|
||
const locations = await platform.discoverlocations() | ||
|
||
expect(locations).toEqual(mockLocations) | ||
}) | ||
|
||
it('should handle error during location discovery', async () => { | ||
(axios.get as MockedFunction<typeof axios.get>).mockRejectedValue(new Error('Network Error')) | ||
|
||
await expect(platform.discoverlocations()).rejects.toThrow('Network Error') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { describe, expect, it } from 'vitest' | ||
|
||
import { PLATFORM_NAME } from './settings.js' | ||
|
||
describe('settings', () => { | ||
it('should have the correct PLATFORM_NAME', () => { | ||
expect(PLATFORM_NAME).toBe('Resideo') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { describe, expect, it } from 'vitest' | ||
|
||
import { toCelsius } from './utils.js' | ||
|
||
describe('toCelsius', () => { | ||
it('should return the same value if the unit is 0 (Celsius)', () => { | ||
expect(toCelsius(25, 0)).toBe(25) | ||
}) | ||
|
||
it('should convert Fahrenheit to Celsius correctly', () => { | ||
expect(toCelsius(32, 1)).toBe(0) // 32°F is 0°C | ||
expect(toCelsius(68, 1)).toBe(20) // 68°F is 20°C | ||
expect(toCelsius(100, 1)).toBe(37.5) // 100°F is 37.5°C | ||
}) | ||
|
||
it('should round to the nearest 0.5 degree', () => { | ||
expect(toCelsius(33, 1)).toBe(0.5) // 33°F is 0.5°C | ||
expect(toCelsius(34, 1)).toBe(1) // 34°F is 1°C | ||
}) | ||
}) |