Skip to content

Commit

Permalink
feat: テストコード追加
Browse files Browse the repository at this point in the history
  • Loading branch information
nana4rider committed Jan 18, 2025
1 parent 54bddc0 commit 98f8936
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 8 deletions.
34 changes: 34 additions & 0 deletions __tests__/connector/WiSunConnector.ts.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { BP35Connector } from "@/connector/BP35Connector";
import createWiSunConnector from "@/connector/WiSunConnector";
import { WiSunConnectorModel } from "@/connector/WiSunConnectorModel";

jest.mock("@/connector/BP35Connector");

describe("createWiSunConnector", () => {
test("side指定ありモデルのインスタンスを取得できる", () => {
const MockBP35Connector = jest.mocked(BP35Connector, { shallow: true });

const wiSunConnector = createWiSunConnector("BP35C2", "devicePath");

expect(MockBP35Connector).toHaveBeenCalledWith("devicePath", 0);
expect(wiSunConnector).toBeInstanceOf(BP35Connector);
});

test("side指定なしモデルのインスタンスを取得できる", () => {
const MockBP35Connector = jest.mocked(BP35Connector, { shallow: true });

const wiSunConnector = createWiSunConnector("BP35A1", "devicePath");

expect(MockBP35Connector).toHaveBeenCalledWith("devicePath");
expect(wiSunConnector).toBeInstanceOf(BP35Connector);
});

test("不正なモデルの場合例外をすろーする", () => {
jest.mocked(BP35Connector, { shallow: true });

const actual = () =>
createWiSunConnector("unknown" as WiSunConnectorModel, "devicePath");

expect(actual).toThrow();
});
});
54 changes: 54 additions & 0 deletions __tests__/payload/builder.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Entity } from "@/entity";
import { buildDevice, buildEntity, buildOrigin } from "@/payload/builder";
import { TopicType } from "@/payload/topic";

describe("buildEntity", () => {
test("必要な属性が揃っている", () => {
const mockEntity = {
id: "entity1",
name: "Test Entity",
deviceClass: "energy",
stateClass: "total_increasing",
unit: "kWh",
nativeValue: "float",
unitPrecision: 3,
} as Entity;

const entity = buildEntity("deviceId1", mockEntity);

expect(entity).toHaveProperty("unique_id", "wisun2mqtt_deviceId1_entity1");
expect(entity).toHaveProperty("name", "Test Entity");
expect(entity).toHaveProperty(
"state_topic",
`wisun2mqtt/deviceId1/entity1/${TopicType.STATE}`,
);
expect(entity).toHaveProperty(
"availability_topic",
`wisun2mqtt/deviceId1/entity1/${TopicType.AVAILABILITY}`,
);
expect(entity).toHaveProperty("device_class", "energy");
expect(entity).toHaveProperty("state_class", "total_increasing");
expect(entity).toHaveProperty("unit_of_measurement", "kWh");
expect(entity).toHaveProperty("native_value", "float");
expect(entity).toHaveProperty("suggested_display_precision", 3);
expect(entity).toHaveProperty("qos");
});
});

describe("buildDevice", () => {
test("必要な属性が揃っている", () => {
const device = buildDevice("deviceId1", "manufacturer");
expect(device).toHaveProperty("device.identifiers");
expect(device).toHaveProperty("device.name");
expect(device).toHaveProperty("device.manufacturer");
});
});

describe("buildOrigin", () => {
test("必要な属性が揃っている", async () => {
const origin = await buildOrigin();
expect(origin).toHaveProperty("origin.name");
expect(origin).toHaveProperty("origin.sw_version");
expect(origin).toHaveProperty("origin.support_url");
});
});
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ export default {
transform: {
"^.+\\.(t|j)sx?$": "@swc/jest",
},
transformIgnorePatterns: ["node_modules/(?!(p-event|p-timeout)/)"],
};
6 changes: 3 additions & 3 deletions src/connector/BP35Connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ export class BP35Connector extends Emitter<Events> implements WiSunConnector {
/**
* BP35C2Connector クラスのインスタンスを初期化します。
*
* @param device シリアルポートのパス
* @param devicePath シリアルポートのパス
* @param side B面:0 HAN面:1
* @param
*/
constructor(device: string, side: 0 | 1 | undefined = undefined) {
constructor(devicePath: string, side: 0 | 1 | undefined = undefined) {
super();

this.extendArg = side !== undefined ? ` ${side}` : "";
this.serialPort = new SerialPort({ path: device, baudRate: BAUDRATE });
this.serialPort = new SerialPort({ path: devicePath, baudRate: BAUDRATE });
this.parser = this.serialPort.pipe(
new DelimiterParser({ delimiter: Buffer.from(CRLF, "utf-8") }),
);
Expand Down
10 changes: 5 additions & 5 deletions src/connector/WiSunConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,21 @@ export interface WiSunConnector {
* 指定したモデルのWi-SUNコネクタのインスタンスを取得します
*
* @param model Wi-SUNコネクタのモデル
* @param device シリアルポートのパス
* @param devicePath シリアルポートのパス
* @returns
*/
export default function createWiSunConnector(
model: WiSunConnectorModel,
device: string,
devicePath: string,
): WiSunConnector {
switch (model) {
case "BP35C0":
case "BP35C2":
case "BP35C0":
case "RS-WSUHA-P":
return new BP35Connector(device, 0);
return new BP35Connector(devicePath, 0);
case "BP35A1":
case "WSR35A1-00":
return new BP35Connector(device);
return new BP35Connector(devicePath);
default:
throw new Error(`Unsupported model: ${String(model)}`);
}
Expand Down

0 comments on commit 98f8936

Please sign in to comment.