Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(mock-server): add endpoint support without test-specific behaviors #7005

Merged
merged 6 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
}
],
"dprint.experimentalLsp": true,
"dprint.path": "./node_modules/dprint/dprint",
"dprint.path": "./node_modules/dprint/bin.js",
"editor.defaultFormatter": "dprint.dprint",
"[jsonc]": {
"editor.formatOnSave": true,
Expand Down
1 change: 0 additions & 1 deletion packages/cc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@
"@types/fs-extra": "^11.0.4",
"@types/node": "^18.19.31",
"@zwave-js/maintenance": "workspace:*",
"@zwave-js/testing": "workspace:*",
"@zwave-js/transformers": "workspace:*",
"ava": "^6.1.2",
"del-cli": "^5.1.0",
Expand Down
3 changes: 0 additions & 3 deletions packages/cc/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@
{
"path": "../maintenance/tsconfig.build.json"
},
{
"path": "../testing/tsconfig.build.json"
},
{
"path": "../transformers/tsconfig.build.json"
}
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/values/Primitive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,8 @@ export function encodeBitMask(
maxValue: number = Math.max(...values),
startValue: number = 1,
): Buffer {
if (values.length === 0) return Buffer.from([0]);

const numBytes = Math.ceil((maxValue - startValue + 1) / 8);
const ret = Buffer.alloc(numBytes, 0);
for (let val = startValue; val <= maxValue; val++) {
Expand Down
2 changes: 1 addition & 1 deletion packages/serial/src/message/ZnifferMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ export class ZnifferDataMessage extends ZnifferMessage
this.checksumOK = true;
this.payload = Buffer.alloc(0);
} else {
validatePayload.fail(
throw validatePayload.fail(
`Unsupported frame type ${
getEnumMemberName(ZnifferFrameType, this.frameType)
}`,
Expand Down
1 change: 1 addition & 0 deletions packages/testing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"@microsoft/api-extractor": "^7.47.0",
"@types/node": "^18.19.31",
"@types/triple-beam": "^1.3.5",
"@zwave-js/cc": "workspace:*",
"del-cli": "^5.1.0",
"esbuild": "0.21.5",
"esbuild-register": "^3.5.0",
Expand Down
98 changes: 87 additions & 11 deletions packages/testing/src/MockNode.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { CommandClass } from "@zwave-js/cc";
import {
type CommandClassInfo,
type CommandClasses,
Expand Down Expand Up @@ -26,6 +27,7 @@ import {
MockZWaveFrameType,
type MockZWaveRequestFrame,
createMockZWaveAckFrame,
createMockZWaveRequestFrame,
} from "./MockZWaveFrame";

const defaultCCInfo: CommandClassInfo = {
Expand Down Expand Up @@ -293,18 +295,57 @@ export class MockNode {
);
if (handler) {
handler.resolve(frame);
} else {
} else if (frame.type === MockZWaveFrameType.Request) {
let cc = frame.payload;
let response: MockNodeResponse | undefined;

// Transform incoming frames with hooks, e.g. to support unwrapping encapsulated CCs
for (const behavior of this.behaviors) {
if (behavior.transformIncomingCC) {
cc = await behavior.transformIncomingCC(
this.controller,
this,
cc,
);
}
}

// Figure out what to do with the frame
for (const behavior of this.behaviors) {
response = await behavior.handleCC?.(
this.controller,
this,
cc,
);
if (response) break;
}

// If no behavior handled the frame, or we're supposed to stop, stop
if (!response || response.action === "stop") return;

// Transform responses with hooks, e.g. to support Supervision or other encapsulation
for (const behavior of this.behaviors) {
if (
await behavior.onControllerFrame?.(
if (behavior.transformResponse) {
response = await behavior.transformResponse(
this.controller,
this,
frame,
)
) {
return;
cc,
response,
);
}
}

// Finally send a CC to the controller if we're supposed to
if (response.action === "sendCC") {
await this.sendToController(
createMockZWaveRequestFrame(response.cc, {
ackRequested: response.ackRequested,
}),
);
} else if (response.action === "ack") {
// Or ack the frame
await this.ackControllerRequestFrame(frame);
}
}
}

Expand Down Expand Up @@ -419,11 +460,46 @@ export class MockNode {
}
}

/** What the mock node should do after receiving a controller frame */
export type MockNodeResponse = {
// Send a CC
action: "sendCC";
cc: CommandClass;
ackRequested?: boolean; // Defaults to false
} | {
// Acknowledge the incoming frame
action: "ack";
} | {
// do nothing
action: "stop";
} | {
// indicate success to the sending node
action: "ok";
} | {
// indicate failure to the sending node
action: "fail";
};

export interface MockNodeBehavior {
/** Gets called when a message from the controller is received. Return `true` to indicate that the message has been handled. */
onControllerFrame?: (
/** Gets called before the `handleCC` handlers and can transform an incoming `CommandClass` into another */
transformIncomingCC?: (
controller: MockController,
self: MockNode,
cc: CommandClass,
) => Promise<CommandClass> | CommandClass;

/** Gets called when a CC from the controller is received. Returns an action to be performed in response, or `undefined` if there is nothing to do. */
handleCC?: (
controller: MockController,
self: MockNode,
receivedCC: CommandClass,
) => Promise<MockNodeResponse | undefined> | MockNodeResponse | undefined;

/** Gets called after the `onControllerFrame` handlers and can transform one `MockNodeResponse` into another */
transformResponse?: (
controller: MockController,
self: MockNode,
frame: MockZWaveFrame,
) => Promise<boolean | undefined> | boolean | undefined;
receivedCC: CommandClass,
response: MockNodeResponse,
) => Promise<MockNodeResponse> | MockNodeResponse;
}
8 changes: 4 additions & 4 deletions packages/testing/src/MockZWaveFrame.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ICommandClass } from "@zwave-js/core";
import { type CommandClass } from "@zwave-js/cc";

/**
* Is used to simulate communication between a {@link MockController} and a {@link MockNode}.
Expand All @@ -15,7 +15,7 @@ export interface MockZWaveRequestFrame {
/** Whether an ACK is requested from the destination */
ackRequested: boolean;
/** The Command Class contained in the frame */
payload: ICommandClass;
payload: CommandClass;
}

export interface LazyMockZWaveRequestFrame {
Expand All @@ -25,7 +25,7 @@ export interface LazyMockZWaveRequestFrame {
/** Whether an ACK is requested from the destination */
ackRequested: boolean;
/** The Command Class contained in the frame */
payload: ICommandClass | (() => ICommandClass);
payload: CommandClass | (() => CommandClass);
}

export interface MockZWaveAckFrame {
Expand All @@ -44,7 +44,7 @@ export enum MockZWaveFrameType {
}

export function createMockZWaveRequestFrame(
payload: ICommandClass | (() => ICommandClass),
payload: CommandClass | (() => CommandClass),
options: Partial<Omit<MockZWaveRequestFrame, "direction" | "payload">> = {},
): LazyMockZWaveRequestFrame {
const { repeaters = [], ackRequested = true } = options;
Expand Down
11 changes: 9 additions & 2 deletions packages/testing/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,15 @@
},
{
"path": "../shared/tsconfig.build.json"
},
{
"path": "../cc/tsconfig.build.json"
}
],
"include": ["src/**/*.ts"],
"exclude": ["src/**/*.test.ts"]
"include": [
"src/**/*.ts"
],
"exclude": [
"src/**/*.test.ts"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
ZWaveProtocolCCRequestNodeInformationFrame,
} from "@zwave-js/cc/ZWaveProtocolCC";
import {
type ICommandClass,
NodeType,
TransmitOptions,
TransmitStatus,
Expand Down Expand Up @@ -92,7 +91,7 @@ function createLazySendDataPayload(
controller: MockController,
node: MockNode,
msg: SendDataRequest | SendDataMulticastRequest,
): () => ICommandClass {
): () => CommandClass {
return () => {
try {
const cmd = CommandClass.from(node.host, {
Expand Down
Loading