Skip to content

Commit

Permalink
feat: basic setup extra accessory for just using power consumption
Browse files Browse the repository at this point in the history
  • Loading branch information
jvandenaardweg committed Nov 7, 2023
1 parent 0f0fc76 commit 1bacfa5
Show file tree
Hide file tree
Showing 3 changed files with 639 additions and 0 deletions.
133 changes: 133 additions & 0 deletions src/energy-socket-accessory-power-status.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import { EnergySocketAccessoryPowerStatus } from './energy-socket-accessory-power-status';
import { Interceptable, MockAgent, setGlobalDispatcher } from 'undici';
import { mockApiUrl, mockFirmwareVersion } from './api/mocks/api';
import { mockIdentifyResponse } from './api/mocks/data/identify';
import { mockBasicInformationResponse } from './api/mocks/data/basic';
import { accessoryMock, platformMock } from './mocks/platform';
import { mockStateResponse } from './api/mocks/data/state';
import { EnergySocketApi } from 'homewizard-energy-api';

let mockApiAgent: MockAgent;
let mockApiPool: Interceptable;

let mockApi: EnergySocketApi;

describe('EnergySocketAccessoryPowerStatus', () => {
beforeEach(() => {
mockApiAgent = new MockAgent({
bodyTimeout: 10,
keepAliveTimeout: 10,
keepAliveMaxTimeout: 10,
});

mockApiAgent.disableNetConnect();

setGlobalDispatcher(mockApiAgent);

mockApiPool = mockApiAgent.get(mockApiUrl);

mockApi = new EnergySocketApi(mockApiUrl);
});

afterEach(async () => {
await mockApiAgent.close();
vi.restoreAllMocks();
});

it('should create an instance', () => {
const energySocketAccessoryPowerStatus = new EnergySocketAccessoryPowerStatus(platformMock, accessoryMock, mockApi);

expect(energySocketAccessoryPowerStatus).toBeTruthy();
});

it('should get the firmware version as a number value', () => {
const firmwareVersion = new EnergySocketAccessoryPowerStatus(platformMock, accessoryMock, mockApi)
.firmwareVersion;

expect(firmwareVersion).toBe(Number(mockFirmwareVersion));
});

it('should get a response from the identify endpoint', async () => {
mockApiPool
.intercept({
path: `/api/v1/identify`,
method: 'PUT',
})
.reply(() => ({
data: mockIdentifyResponse,
statusCode: 200,
}));

const energySocketAccessoryPowerStatus = new EnergySocketAccessoryPowerStatus(platformMock, accessoryMock, mockApi);

expect(energySocketAccessoryPowerStatus.handleIdentify()).resolves.toStrictEqual(mockIdentifyResponse);
});

it('should call handleAccessoryApiError when an error happens on handleIdentify', async () => {
mockApiPool
.intercept({
path: `/api/v1/identify`,
method: 'PUT',
})
.reply(() => ({
data: 'Server error!',
statusCode: 500,
}));

const handleAccessoryApiErrorSpy = vi.spyOn(
EnergySocketAccessoryPowerStatus.prototype,
'handleAccessoryApiError',
);

const energySocketAccessoryPowerStatus = new EnergySocketAccessoryPowerStatus(platformMock, accessoryMock, mockApi);

try {
await energySocketAccessoryPowerStatus.handleIdentify();
} catch (err) {
expect(handleAccessoryApiErrorSpy).toHaveBeenCalledTimes(1);
}
});

it('should resolve when handleSetOn is invoked with a boolean value and switch_lock = false', async () => {
const mockPowerOn = true;

const energySocketAccessoryPowerStatus = new EnergySocketAccessoryPowerStatus(platformMock, accessoryMock, mockApi);

expect(energySocketAccessoryPowerStatus.handleSetOn(mockPowerOn)).resolves.toBeUndefined();
});

it('should throw an error when handleSetOn is invoked when switch_lock = true', async () => {
const mockPowerOn = true;

mockApiPool
.intercept({
path: `/api`,
method: 'GET',
})
.reply(() => ({
data: mockBasicInformationResponse,
statusCode: 200,
}));

const handleAccessoryApiErrorSpy = vi.spyOn(
EnergySocketAccessoryPowerStatus.prototype,
'handleAccessoryApiError',
);

const energySocketAccessoryPowerStatus = new EnergySocketAccessoryPowerStatus(platformMock, accessoryMock, mockApi);

try {
await energySocketAccessoryPowerStatus.handleSetOn(mockPowerOn);
} catch (err) {
expect(handleAccessoryApiErrorSpy).toHaveBeenCalledOnce();
}
});

it('should return the power_on value when handleGetOn is invoked', async () => {
const energySocketAccessoryPowerStatus = new EnergySocketAccessoryPowerStatus(platformMock, accessoryMock, mockApi);

const response = await energySocketAccessoryPowerStatus.handleGetOn();

expect(response).toStrictEqual(mockStateResponse.power_on);
});
});
Loading

0 comments on commit 1bacfa5

Please sign in to comment.