Skip to content

Commit

Permalink
test: fix several tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AlCalzone committed Oct 21, 2024
1 parent 48590d2 commit 678c476
Show file tree
Hide file tree
Showing 31 changed files with 474 additions and 479 deletions.
2 changes: 1 addition & 1 deletion packages/cc/src/lib/CommandClass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ export class CommandClass implements CCId {
public static from(raw: CCRaw, ctx: CCParsingContext): CommandClass {
// FIXME: Propagate frame type etc.
// FIXME: Refactor subclasses' parse() to override this
return new CommandClass({
return new this({
nodeId: ctx.ownNodeId,
ccId: raw.ccId,
ccCommand: raw.ccCommand,
Expand Down
21 changes: 11 additions & 10 deletions packages/zwave-js/src/lib/test/cc/AssociationCC.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
AssociationCCSupportedGroupingsGet,
AssociationCCSupportedGroupingsReport,
AssociationCommand,
CommandClass,
} from "@zwave-js/cc";
import { CommandClasses } from "@zwave-js/core";
import test from "ava";
Expand Down Expand Up @@ -38,11 +39,11 @@ test("the SupportedGroupingsReport command should be deserialized correctly", (t
7, // # of groups
]),
);
const cc = new AssociationCCSupportedGroupingsReport({
nodeId: 2,
data: ccData,
context: {} as any,
});
const cc = CommandClass.parse(
ccData,
{ sourceNodeId: 2 } as any,
) as AssociationCCSupportedGroupingsReport;
t.is(cc.constructor, AssociationCCSupportedGroupingsReport);

t.is(cc.groupCount, 7);
});
Expand Down Expand Up @@ -92,11 +93,11 @@ test("the Report command should be deserialized correctly", (t) => {
5,
]),
);
const cc = new AssociationCCReport({
nodeId: 1,
data: ccData,
context: {} as any,
});
const cc = CommandClass.parse(
ccData,
{ sourceNodeId: 1 } as any,
) as AssociationCCReport;
t.is(cc.constructor, AssociationCCReport);

t.is(cc.groupId, 5);
t.is(cc.maxNodes, 9);
Expand Down
40 changes: 20 additions & 20 deletions packages/zwave-js/src/lib/test/cc/AssociationGroupInfoCC.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
AssociationGroupInfoCommand,
AssociationGroupInfoProfile,
BasicCommand,
CommandClass,
} from "@zwave-js/cc";
import { CommandClasses } from "@zwave-js/core";
import test from "ava";
Expand Down Expand Up @@ -51,11 +52,11 @@ test("the NameReport command should be deserialized correctly", (t) => {
0x72,
]),
);
const cc = new AssociationGroupInfoCCNameReport({
nodeId: 1,
data: ccData,
context: {} as any,
});
const cc = CommandClass.parse(
ccData,
{ sourceNodeId: 1 } as any,
) as AssociationGroupInfoCCNameReport;
t.is(cc.constructor, AssociationGroupInfoCCNameReport);

t.is(cc.groupId, 7);
t.is(cc.name, "foobar");
Expand Down Expand Up @@ -138,11 +139,11 @@ test("the Info Report command should be deserialized correctly", (t) => {
0,
]),
);
const cc = new AssociationGroupInfoCCInfoReport({
nodeId: 1,
data: ccData,
context: {} as any,
});
const cc = CommandClass.parse(
ccData,
{ sourceNodeId: 1 } as any,
) as AssociationGroupInfoCCInfoReport;
t.is(cc.constructor, AssociationGroupInfoCCInfoReport);

t.is(cc.groups.length, 2);
t.is(cc.groups[0].groupId, 1);
Expand Down Expand Up @@ -184,11 +185,11 @@ test("the CommandListReport command should be deserialized correctly", (t) => {
0x05,
]),
);
const cc = new AssociationGroupInfoCCCommandListReport({
nodeId: 1,
data: ccData,
context: {} as any,
});
const cc = CommandClass.parse(
ccData,
{ sourceNodeId: 1 } as any,
) as AssociationGroupInfoCCCommandListReport;
t.is(cc.constructor, AssociationGroupInfoCCCommandListReport);

t.is(cc.groupId, 7);
t.is(cc.commands.size, 2);
Expand All @@ -203,11 +204,10 @@ test("deserializing an unsupported command should return an unspecified version
const serializedCC = buildCCBuffer(
Buffer.from([255]), // not a valid command
);
const cc: any = new AssociationGroupInfoCC({
nodeId: 1,
data: serializedCC,
context: {} as any,
});
const cc = CommandClass.parse(
serializedCC,
{ sourceNodeId: 1 } as any,
) as AssociationGroupInfoCC;
t.is(cc.constructor, AssociationGroupInfoCC);
});

Expand Down
34 changes: 17 additions & 17 deletions packages/zwave-js/src/lib/test/cc/BasicCC.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
BasicCCSet,
type BasicCCValues,
BasicCommand,
CommandClass,
getCCValues,
} from "@zwave-js/cc";
import { CommandClasses } from "@zwave-js/core";
Expand Down Expand Up @@ -55,11 +56,11 @@ test("the Report command (v1) should be deserialized correctly", (t) => {
55, // current value
]),
);
const basicCC = new BasicCCReport({
nodeId: 2,
data: ccData,
context: {} as any,
});
const basicCC = CommandClass.parse(
ccData,
{ sourceNodeId: 2 } as any,
) as BasicCCReport;
t.is(basicCC.constructor, BasicCCReport);

t.is(basicCC.currentValue, 55);
t.is(basicCC.targetValue, undefined);
Expand All @@ -75,11 +76,11 @@ test("the Report command (v2) should be deserialized correctly", (t) => {
1, // duration
]),
);
const basicCC = new BasicCCReport({
nodeId: 2,
data: ccData,
context: {} as any,
});
const basicCC = CommandClass.parse(
ccData,
{ sourceNodeId: 2 } as any,
) as BasicCCReport;
t.is(basicCC.constructor, BasicCCReport);

t.is(basicCC.currentValue, 55);
t.is(basicCC.targetValue, 66);
Expand All @@ -91,11 +92,10 @@ test("deserializing an unsupported command should return an unspecified version
const serializedCC = buildCCBuffer(
Buffer.from([255]), // not a valid command
);
const basicCC: any = new BasicCC({
nodeId: 2,
data: serializedCC,
context: {} as any,
});
const basicCC = CommandClass.parse(
serializedCC,
{ sourceNodeId: 2 } as any,
) as BasicCCReport;
t.is(basicCC.constructor, BasicCC);
});

Expand Down Expand Up @@ -133,7 +133,7 @@ test.only("getDefinedValueIDs() should include the target value for all endpoint
test("BasicCCSet should expect no response", (t) => {
const cc = new BasicCCSet({
nodeId: 2,
endpoint: 2,
endpointIndex: 2,
targetValue: 7,
});
t.false(cc.expectsCCResponse());
Expand All @@ -142,7 +142,7 @@ test("BasicCCSet should expect no response", (t) => {
test("BasicCCSet => BasicCCReport = unexpected", (t) => {
const ccRequest = new BasicCCSet({
nodeId: 2,
endpoint: 2,
endpointIndex: 2,
targetValue: 7,
});
const ccResponse = new BasicCCReport({
Expand Down
64 changes: 32 additions & 32 deletions packages/zwave-js/src/lib/test/cc/BatteryCC.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import {
BatteryCC,
BatteryCCGet,
type BatteryCCReport,
BatteryCCReport,
BatteryChargingStatus,
BatteryCommand,
BatteryReplacementStatus,
CommandClass,
} from "@zwave-js/cc";
import { CommandClasses } from "@zwave-js/core";
import test from "ava";
Expand All @@ -24,11 +25,11 @@ test("the Report command (v1) should be deserialized correctly: when the battery
BatteryCommand.Report, // CC Command
55, // current value
]);
const batteryCC = new BatteryCC({
nodeId: 7,
data: ccData,
context: {} as any,
}) as BatteryCCReport;
const batteryCC = CommandClass.parse(
ccData,
{ sourceNodeId: 7 } as any,
) as BatteryCCReport;
t.is(batteryCC.constructor, BatteryCCReport);

t.is(batteryCC.level, 55);
t.false(batteryCC.isLow);
Expand All @@ -40,11 +41,11 @@ test("the Report command (v1) should be deserialized correctly: when the battery
BatteryCommand.Report, // CC Command
0xff, // current value
]);
const batteryCC = new BatteryCC({
nodeId: 7,
data: ccData,
context: {} as any,
}) as BatteryCCReport;
const batteryCC = CommandClass.parse(
ccData,
{ sourceNodeId: 7 } as any,
) as BatteryCCReport;
t.is(batteryCC.constructor, BatteryCCReport);

t.is(batteryCC.level, 0);
t.true(batteryCC.isLow);
Expand All @@ -58,11 +59,11 @@ test("the Report command (v2) should be deserialized correctly: all flags set",
0b00_1111_00,
1, // disconnected
]);
const batteryCC = new BatteryCC({
nodeId: 7,
data: ccData,
context: {} as any,
}) as BatteryCCReport;
const batteryCC = CommandClass.parse(
ccData,
{ sourceNodeId: 7 } as any,
) as BatteryCCReport;
t.is(batteryCC.constructor, BatteryCCReport);

t.true(batteryCC.rechargeable);
t.true(batteryCC.backup);
Expand All @@ -79,11 +80,11 @@ test("the Report command (v2) should be deserialized correctly: charging status"
0b10_000000, // Maintaining
0,
]);
const batteryCC = new BatteryCC({
nodeId: 7,
data: ccData,
context: {} as any,
}) as BatteryCCReport;
const batteryCC = CommandClass.parse(
ccData,
{ sourceNodeId: 7 } as any,
) as BatteryCCReport;
t.is(batteryCC.constructor, BatteryCCReport);

t.is(batteryCC.chargingStatus, BatteryChargingStatus.Maintaining);
});
Expand All @@ -96,11 +97,11 @@ test("the Report command (v2) should be deserialized correctly: recharge or repl
0b11, // Maintaining
0,
]);
const batteryCC = new BatteryCC({
nodeId: 7,
data: ccData,
context: {} as any,
}) as BatteryCCReport;
const batteryCC = CommandClass.parse(
ccData,
{ sourceNodeId: 7 } as any,
) as BatteryCCReport;
t.is(batteryCC.constructor, BatteryCCReport);

t.is(batteryCC.rechargeOrReplace, BatteryReplacementStatus.Now);
});
Expand All @@ -110,12 +111,11 @@ test("deserializing an unsupported command should return an unspecified version
CommandClasses.Battery, // CC
255, // not a valid command
]);
const basicCC: any = new BatteryCC({
nodeId: 7,
data: serializedCC,
context: {} as any,
});
t.is(basicCC.constructor, BatteryCC);
const batteryCC = CommandClass.parse(
serializedCC,
{ sourceNodeId: 7 } as any,
) as BatteryCCReport;
t.is(batteryCC.constructor, BatteryCC);
});

// describe.skip(`interview()`, () => {
Expand Down
40 changes: 20 additions & 20 deletions packages/zwave-js/src/lib/test/cc/BinarySensorCC.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
BinarySensorCCSupportedReport,
BinarySensorCommand,
BinarySensorType,
CommandClass,
} from "@zwave-js/cc";
import { CommandClasses } from "@zwave-js/core";
import test from "ava";
Expand Down Expand Up @@ -48,11 +49,11 @@ test("the Report command (v1) should be deserialized correctly", (t) => {
0xff, // current value
]),
);
const cc = new BinarySensorCCReport({
nodeId: 1,
data: ccData,
context: {} as any,
});
const cc = CommandClass.parse(
ccData,
{ sourceNodeId: 1 } as any,
) as BinarySensorCCReport;
t.is(cc.constructor, BinarySensorCCReport);

t.is(cc.value, true);
});
Expand All @@ -65,11 +66,11 @@ test("the Report command (v2) should be deserialized correctly", (t) => {
BinarySensorType.CO2,
]),
);
const cc = new BinarySensorCCReport({
nodeId: 1,
data: ccData,
context: {} as any,
});
const cc = CommandClass.parse(
ccData,
{ sourceNodeId: 1 } as any,
) as BinarySensorCCReport;
t.is(cc.constructor, BinarySensorCCReport);

t.is(cc.value, false);
t.is(cc.type, BinarySensorType.CO2);
Expand All @@ -93,11 +94,11 @@ test("the SupportedReport command should be deserialized correctly", (t) => {
0b10,
]),
);
const cc = new BinarySensorCCSupportedReport({
nodeId: 1,
data: ccData,
context: {} as any,
});
const cc = CommandClass.parse(
ccData,
{ sourceNodeId: 1 } as any,
) as BinarySensorCCSupportedReport;
t.is(cc.constructor, BinarySensorCCSupportedReport);

t.deepEqual(cc.supportedSensorTypes, [
BinarySensorType["General Purpose"],
Expand All @@ -112,11 +113,10 @@ test("deserializing an unsupported command should return an unspecified version
const serializedCC = buildCCBuffer(
Buffer.from([255]), // not a valid command
);
const cc: any = new BinarySensorCC({
nodeId: 1,
data: serializedCC,
context: {} as any,
});
const cc = CommandClass.parse(
serializedCC,
{ sourceNodeId: 1 } as any,
) as BinarySensorCC;
t.is(cc.constructor, BinarySensorCC);
});

Expand Down
Loading

0 comments on commit 678c476

Please sign in to comment.