Skip to content

Commit

Permalink
chore: fix switches tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mdvanes committed May 23, 2024
1 parent 8d61cc8 commit ee8ab35
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const mockRootState: MockRootState = {
dimLevel: null,
readOnly: false,
children: false,
origin: "domoticz",
},
{
idx: "4",
Expand All @@ -29,6 +30,7 @@ const mockRootState: MockRootState = {
status: "On",
dimLevel: null,
readOnly: false,
origin: "domoticz",
children: [
{
idx: "5",
Expand All @@ -38,6 +40,7 @@ const mockRootState: MockRootState = {
dimLevel: null,
readOnly: false,
children: false,
origin: "domoticz",
},
],
},
Expand All @@ -49,6 +52,7 @@ const mockRootState: MockRootState = {
dimLevel: 30,
readOnly: true,
children: false,
origin: "domoticz",
},
{
idx: "7",
Expand All @@ -58,6 +62,7 @@ const mockRootState: MockRootState = {
dimLevel: 30,
readOnly: false,
children: false,
origin: "domoticz",
},
{
idx: "8",
Expand All @@ -67,6 +72,7 @@ const mockRootState: MockRootState = {
dimLevel: null,
readOnly: false,
children: false,
origin: "domoticz",
},
],
expanded: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const mockRootState: MockRootState = {
dimLevel: null,
readOnly: false,
children: false,
origin: "domoticz",
},
{
idx: "4",
Expand All @@ -28,6 +29,7 @@ const mockRootState: MockRootState = {
status: "On",
dimLevel: null,
readOnly: false,
origin: "domoticz",
children: [
{
idx: "5",
Expand All @@ -37,6 +39,7 @@ const mockRootState: MockRootState = {
dimLevel: null,
readOnly: false,
children: false,
origin: "domoticz",
},
],
},
Expand All @@ -48,6 +51,7 @@ const mockRootState: MockRootState = {
dimLevel: 30,
readOnly: true,
children: false,
origin: "domoticz",
},
{
idx: "7",
Expand All @@ -57,6 +61,7 @@ const mockRootState: MockRootState = {
dimLevel: 30,
readOnly: false,
children: false,
origin: "domoticz",
},
{
idx: "8",
Expand All @@ -66,6 +71,7 @@ const mockRootState: MockRootState = {
dimLevel: null,
readOnly: false,
children: false,
origin: "domoticz",
},
],
expanded: [],
Expand Down
14 changes: 10 additions & 4 deletions apps/server/src/switches/switches.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ import { DomoticzTypeOptions } from "@homeremote/types";
import { ConfigService } from "@nestjs/config";
import { Test, TestingModule } from "@nestjs/testing";
import * as Got from "got";
import { AuthenticatedRequest } from "../login/LoginRequest.types";
import { DomoticzSwitch, SwitchesController } from "./switches.controller";

jest.mock("got");
const gotSpy = jest.spyOn(Got, "default");

const mockAuthenticatedRequest = {
user: { name: "someuser", id: 1 },
} as AuthenticatedRequest;

describe("Switches Controller", () => {
let controller: SwitchesController;
let configService: ConfigService;
Expand Down Expand Up @@ -42,7 +47,7 @@ describe("Switches Controller", () => {
gotSpy.mockResolvedValue({
body: JSON.stringify({ status: "OK", result: [mockSwitch] }),
});
expect(await controller.getSwitches()).toEqual({
expect(await controller.getSwitches(mockAuthenticatedRequest)).toEqual({
status: "received",
switches: [
{
Expand All @@ -53,14 +58,15 @@ describe("Switches Controller", () => {
dimLevel: null,
readOnly: false,
children: false,
origin: "domoticz",
},
],
});
});

it("returns error status on failed /GET ", async () => {
gotSpy.mockRejectedValue("Mock Server Down");
expect(await controller.getSwitches()).toEqual({
expect(await controller.getSwitches(mockAuthenticatedRequest)).toEqual({
status: "error",
});
});
Expand All @@ -69,7 +75,7 @@ describe("Switches Controller", () => {
gotSpy.mockResolvedValue({
body: JSON.stringify({ status: "error" }),
});
expect(await controller.getSwitches()).toEqual({
expect(await controller.getSwitches(mockAuthenticatedRequest)).toEqual({
status: "error",
});
});
Expand All @@ -78,7 +84,7 @@ describe("Switches Controller", () => {
gotSpy.mockResolvedValue({
body: "not json",
});
expect(await controller.getSwitches()).toEqual({
expect(await controller.getSwitches(mockAuthenticatedRequest)).toEqual({
status: "error",
});
});
Expand Down
78 changes: 42 additions & 36 deletions apps/server/src/switches/switches.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ type GetChildren = (
SceneType: DomoticzType,
SceneStatus: DomoticzStatus
) => Promise<HomeRemoteSwitch[] | false>;

const getChildren: GetChildren = async (
domoticzuri,
SceneIdx,
Expand Down Expand Up @@ -149,45 +150,50 @@ export class SwitchesController {
}

getHaLightFavoritesAsSwitches = async (): Promise<HomeRemoteHaSwitch[]> => {
const haFavoritesResponse = await fetch(
`${this.haApiConfig.baseUrl}/api/states/light.favorites`,
{
headers: {
Authorization: `Bearer ${this.haApiConfig.token}`,
},
}
);
const haFavoriteIds =
(await haFavoritesResponse.json()) as GetHaStatesResponse;
try {
const haFavoritesResponse = await fetch(
`${this.haApiConfig.baseUrl}/api/states/light.favorites`,
{
headers: {
Authorization: `Bearer ${this.haApiConfig.token}`,
},
}
);
const haFavoriteIds =
(await haFavoritesResponse.json()) as GetHaStatesResponse;

const haStatesPromises = haFavoriteIds.attributes.entity_id.map(
async (entity) => {
const haStateResponse = await fetch(
`${this.haApiConfig.baseUrl}/api/states/${entity}`,
{
headers: {
Authorization: `Bearer ${this.haApiConfig.token}`,
},
}
);
return (await haStateResponse.json()) as GetHaStatesResponse;
}
);
const haStates = await Promise.all(haStatesPromises);
const haStatesPromises = haFavoriteIds.attributes.entity_id.map(
async (entity) => {
const haStateResponse = await fetch(
`${this.haApiConfig.baseUrl}/api/states/${entity}`,
{
headers: {
Authorization: `Bearer ${this.haApiConfig.token}`,
},
}
);
return (await haStateResponse.json()) as GetHaStatesResponse;
}
);
const haStates = await Promise.all(haStatesPromises);

const haSwitches: HomeRemoteHaSwitch[] =
haStates.map<HomeRemoteHaSwitch>((entity) => ({
origin: "home-assistant",
dimLevel: null,
idx: entity.entity_id,
name: entity.attributes.friendly_name,
readOnly: false,
status: entity.state === "off" ? "Off" : "On",
type: "Light/Switch",
children: false,
}));
const haSwitches: HomeRemoteHaSwitch[] =
haStates.map<HomeRemoteHaSwitch>((entity) => ({
origin: "home-assistant",
dimLevel: null,
idx: entity.entity_id,
name: entity.attributes.friendly_name,
readOnly: false,
status: entity.state === "off" ? "Off" : "On",
type: "Light/Switch",
children: false,
}));

return haSwitches;
return haSwitches;
} catch (err) {
this.logger.error(`Can't get HaLightFavorites ${err}`);
return [];
}
};

@UseGuards(JwtAuthGuard)
Expand Down

0 comments on commit ee8ab35

Please sign in to comment.