@homebridge-plugins/homebridge-air
Version:
The AirNow plugin allows you to monitor the current AirQuality for your Zip Code from HomeKit and Siri.
127 lines • 6.3 kB
JavaScript
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { createPlatformProxy } from '../utils.js';
// Dummy platform constructors – must use regular functions (not arrow functions) so they can be
// called with `new` inside the PlatformProxy constructor.
// eslint-disable-next-line prefer-arrow-callback
const MockHapPlatform = vi.fn().mockImplementation(function () { });
// eslint-disable-next-line prefer-arrow-callback
const MockMatterPlatform = vi.fn().mockImplementation(function () { });
const mockLog = {
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
debug: vi.fn(),
success: vi.fn(),
prefix: 'test',
};
function makeConfig(enableMatter, preferMatter) {
return {
platform: 'Air',
name: 'Air',
options: { enableMatter, preferMatter },
};
}
function makeApi(matterAvailable, matterEnabled) {
return {
isMatterAvailable: vi.fn().mockReturnValue(matterAvailable),
isMatterEnabled: vi.fn().mockReturnValue(matterEnabled),
};
}
describe('createPlatformProxy', () => {
beforeEach(() => {
MockHapPlatform.mockClear();
MockMatterPlatform.mockClear();
vi.clearAllMocks();
});
describe('hAP selection (default)', () => {
it('uses HAP when neither enableMatter nor preferMatter is set', () => {
const Proxy = createPlatformProxy(MockHapPlatform, MockMatterPlatform);
const api = makeApi(true, true);
new Proxy(mockLog, makeConfig(), api);
expect(MockHapPlatform).toHaveBeenCalledOnce();
expect(MockMatterPlatform).not.toHaveBeenCalled();
});
it('uses HAP when config is undefined/null', () => {
const Proxy = createPlatformProxy(MockHapPlatform, MockMatterPlatform);
const api = makeApi(true, true);
new Proxy(mockLog, undefined, api);
expect(MockHapPlatform).toHaveBeenCalledOnce();
expect(MockMatterPlatform).not.toHaveBeenCalled();
});
});
describe('matter selection', () => {
it('uses Matter when enableMatter=true and Matter is available+enabled', () => {
const Proxy = createPlatformProxy(MockHapPlatform, MockMatterPlatform);
const api = makeApi(true, true);
new Proxy(mockLog, makeConfig(true), api);
expect(MockMatterPlatform).toHaveBeenCalledOnce();
expect(MockHapPlatform).not.toHaveBeenCalled();
});
it('uses Matter when preferMatter=true and Matter is available+enabled', () => {
const Proxy = createPlatformProxy(MockHapPlatform, MockMatterPlatform);
const api = makeApi(true, true);
new Proxy(mockLog, makeConfig(false, true), api);
expect(MockMatterPlatform).toHaveBeenCalledOnce();
expect(MockHapPlatform).not.toHaveBeenCalled();
});
});
describe('hAP fallback when Matter is unavailable', () => {
it('falls back to HAP when enableMatter=true but Matter is not available', () => {
const Proxy = createPlatformProxy(MockHapPlatform, MockMatterPlatform);
const api = makeApi(false, false);
new Proxy(mockLog, makeConfig(true), api);
expect(MockHapPlatform).toHaveBeenCalledOnce();
expect(MockMatterPlatform).not.toHaveBeenCalled();
});
it('falls back to HAP when enableMatter=true but Matter is available but not enabled', () => {
const Proxy = createPlatformProxy(MockHapPlatform, MockMatterPlatform);
const api = makeApi(true, false);
new Proxy(mockLog, makeConfig(true), api);
expect(MockHapPlatform).toHaveBeenCalledOnce();
expect(MockMatterPlatform).not.toHaveBeenCalled();
});
it('falls back to HAP when preferMatter=true but Matter is not available', () => {
const Proxy = createPlatformProxy(MockHapPlatform, MockMatterPlatform);
const api = makeApi(false, false);
new Proxy(mockLog, makeConfig(false, true), api);
expect(MockHapPlatform).toHaveBeenCalledOnce();
expect(MockMatterPlatform).not.toHaveBeenCalled();
});
});
describe('logging semantics: enableMatter vs preferMatter', () => {
it('logs a warning when enableMatter=true but Matter is not available', () => {
const Proxy = createPlatformProxy(MockHapPlatform, MockMatterPlatform);
const api = makeApi(false, false);
new Proxy(mockLog, makeConfig(true), api);
expect(mockLog.warn).toHaveBeenCalledWith(expect.stringContaining('Matter'));
});
it('logs a warning when enableMatter=true but Matter is available but not enabled', () => {
const Proxy = createPlatformProxy(MockHapPlatform, MockMatterPlatform);
const api = makeApi(true, false);
new Proxy(mockLog, makeConfig(true), api);
expect(mockLog.warn).toHaveBeenCalledWith(expect.stringContaining('Matter'));
});
it('does NOT log a warning when preferMatter=true and Matter is not available (silent fallback)', () => {
const Proxy = createPlatformProxy(MockHapPlatform, MockMatterPlatform);
const api = makeApi(false, false);
new Proxy(mockLog, makeConfig(false, true), api);
expect(mockLog.warn).not.toHaveBeenCalled();
});
it('does NOT log a warning when neither flag is set and Matter is not available', () => {
const Proxy = createPlatformProxy(MockHapPlatform, MockMatterPlatform);
const api = makeApi(false, false);
new Proxy(mockLog, makeConfig(), api);
expect(mockLog.warn).not.toHaveBeenCalled();
});
});
describe('proxy returns correct constructor args', () => {
it('passes log, config, and api through to the selected platform', () => {
const Proxy = createPlatformProxy(MockHapPlatform, MockMatterPlatform);
const api = makeApi(false, false);
const config = makeConfig();
new Proxy(mockLog, config, api);
expect(MockHapPlatform).toHaveBeenCalledWith(mockLog, config, api);
});
});
});
//# sourceMappingURL=utils.test.js.map