|
| 1 | +--- |
| 2 | +name: implement-test-handler |
| 3 | +description: Implements Z-Wave CTT (Certification Test Tool) test handlers for automating certification tests. Use when asked to create handlers for a given test, which includes automating CTT log parsing and prompt responses. |
| 4 | +--- |
| 5 | + |
| 6 | +# CTT Test Handler Implementation |
| 7 | + |
| 8 | +## Overview |
| 9 | + |
| 10 | +CTT handlers automate Z-Wave certification tests by: |
| 11 | +1. Parsing CTT log messages and executing corresponding Z-Wave commands |
| 12 | +2. Responding to CTT prompts with Yes/No/Ok based on device state |
| 13 | + |
| 14 | +## File Structure |
| 15 | + |
| 16 | +``` |
| 17 | +dut/zwave-js/handlers/ |
| 18 | +├── tests/ # Test-specific handlers (CCR_*, CDR_*, RT_*, ...) |
| 19 | +├── behaviors/ # Reusable handlers (capabilities, addMode, etc.) |
| 20 | +├── utils.ts # Utility functions |
| 21 | +└── index.ts # Auto-imports all handlers |
| 22 | +``` |
| 23 | + |
| 24 | +## Handler Template |
| 25 | + |
| 26 | +```typescript |
| 27 | +import { <CCValues>, <CCEnums> } from "zwave-js"; |
| 28 | +import { registerHandler } from "../../prompt-handlers.ts"; |
| 29 | + |
| 30 | +// Map CTT names to zwave-js enum values |
| 31 | +const nameToEnum: Record<string, EnumType> = { |
| 32 | + CTTName: EnumType["ZWave JS Name"], |
| 33 | +}; |
| 34 | + |
| 35 | +registerHandler("CCR_<TestName>_Rev##", { |
| 36 | + async onLog(ctx) { |
| 37 | + const node = ctx.includedNodes.at(-1); |
| 38 | + if (!node) return; |
| 39 | + |
| 40 | + // Parse log and execute command |
| 41 | + const match = /<PATTERN>/i.exec(ctx.logText); |
| 42 | + if (match?.groups) { |
| 43 | + // Execute Z-Wave command |
| 44 | + return true; // Mark as handled |
| 45 | + } |
| 46 | + }, |
| 47 | + |
| 48 | + async onPrompt(ctx) { |
| 49 | + const node = ctx.includedNodes.at(-1); |
| 50 | + if (!node) return; |
| 51 | + |
| 52 | + // Check condition and respond (prefer regex over .includes()) |
| 53 | + if (/some pattern/i.test(ctx.promptText)) { |
| 54 | + const actual = node.getValue(CCValues.property.id); |
| 55 | + return actual === expected ? "Yes" : "No"; |
| 56 | + } |
| 57 | + }, |
| 58 | +}); |
| 59 | +``` |
| 60 | + |
| 61 | +## Key Patterns |
| 62 | + |
| 63 | +### Executing Commands |
| 64 | + |
| 65 | +Prefer `setValue` for simple single-value commands. Use `commandClasses` methods for complex operations with multiple parameters. |
| 66 | + |
| 67 | +**Simple value set (preferred):** |
| 68 | +```typescript |
| 69 | +node.setValue(DoorLockCCValues.targetMode.id, DoorLockMode.Secured); |
| 70 | +``` |
| 71 | + |
| 72 | +**Command class method (for complex operations):** |
| 73 | +```typescript |
| 74 | +await node.commandClasses["Thermostat Setpoint"].set(type, value, scale); |
| 75 | +await node.commandClasses["Door Lock"].setConfiguration(config); |
| 76 | +``` |
| 77 | + |
| 78 | +### Reading Values |
| 79 | + |
| 80 | +```typescript |
| 81 | +const current = node.getValue(ThermostatModeCCValues.thermostatMode.id); |
| 82 | +``` |
| 83 | + |
| 84 | +### State Management |
| 85 | + |
| 86 | +Store values for later verification: |
| 87 | +```typescript |
| 88 | +// In onLog: |
| 89 | +ctx.state.set("lastValue", value); |
| 90 | + |
| 91 | +// In onPrompt: |
| 92 | +const stored = ctx.state.get("lastValue") as number; |
| 93 | +``` |
| 94 | + |
| 95 | +### Enum Mapping Conventions |
| 96 | + |
| 97 | +CTT typically uses Z-Wave JS enum names without spaces: |
| 98 | +- `FullPower` → `ThermostatSetpointType["Full Power"]` |
| 99 | +- `AutoChangeover` → `ThermostatSetpointType["Auto Changeover"]` |
| 100 | + |
| 101 | +For UPPER_CASE log formats (like `THERMOSTAT_MODE_SET`): |
| 102 | +- `HEAT` → `ThermostatMode["Heat"]` |
| 103 | +- `MANUFACTURER_SPECIFIC` → `ThermostatMode["Manufacturer specific"]` |
| 104 | + |
| 105 | +### Pattern Matching |
| 106 | + |
| 107 | +Prefer regex over `.includes()` due to case variations and potential typos in CTT output: |
| 108 | + |
| 109 | +```typescript |
| 110 | +// Good: case-insensitive, handles typos |
| 111 | +if (/setpoint.+set succ?essfully/i.test(ctx.promptText)) { ... } |
| 112 | + |
| 113 | +// Avoid: brittle, case-sensitive |
| 114 | +if (ctx.promptText.includes("Setpoint been set successfully")) { ... } |
| 115 | +``` |
| 116 | + |
| 117 | +## Common Log Patterns |
| 118 | + |
| 119 | +```typescript |
| 120 | +// Value with mode/type |
| 121 | +/COMMAND_SET to mode = '(?<mode>\w+)'/i |
| 122 | + |
| 123 | +// Value with numeric parameter |
| 124 | +/COMMAND_SET.+value=(?<value>[\d.]+)/i |
| 125 | + |
| 126 | +// Hex value in parentheses |
| 127 | +/'(?<name>[^']+)' \((?<hex>0x[0-9a-fA-F]+)\)/i |
| 128 | + |
| 129 | +// Multi-line configuration |
| 130 | +if (/Set Configuration:/i.test(ctx.logText)) { |
| 131 | + const field1 = /Field 1:\s+'(\w+)'/i.exec(ctx.logText)?.[1]; |
| 132 | +} |
| 133 | +``` |
| 134 | + |
| 135 | +## Common Prompt Patterns |
| 136 | + |
| 137 | +```typescript |
| 138 | +// Confirmation with expected value |
| 139 | +/current mode is set to '(?<mode>\w+)'/i |
| 140 | +/last known .+ is '(?<value>[^']+)' \((?<hex>0x[0-9a-fA-F]+)\)/i |
| 141 | + |
| 142 | +// Simple yes/no capability questions → add to behaviors/capabilities.ts |
| 143 | +{ pattern: /capable to do something/i, answer: "Yes" } |
| 144 | +``` |
| 145 | + |
| 146 | +## Reference Files |
| 147 | + |
| 148 | +- [prompt-handlers.ts](../../../dut/zwave-js/prompt-handlers.ts) - Handler registration |
| 149 | +- [utils.ts](../../../dut/zwave-js/handlers/utils.ts) - Utilities like `parseDurationFromLog` |
| 150 | +- [capabilities.ts](../../../dut/zwave-js/handlers/behaviors/capabilities.ts) - Capability prompts |
| 151 | +- [zwave-js CC Documentation](https://zwave-js.github.io/zwave-js/#/api/CCs/index) - Official CC API docs |
| 152 | + |
| 153 | +## Existing Handler Examples |
| 154 | + |
| 155 | +- [CCR_DoorLockCC_Rev02.ts](../../../dut/zwave-js/handlers/tests/CCR_DoorLockCC_Rev02.ts) - Complex config |
| 156 | +- [CCR_ThermostatModeCC_Rev02.ts](../../../dut/zwave-js/handlers/tests/CCR_ThermostatModeCC_Rev02.ts) - Mode with manufacturer data |
| 157 | +- [CCR_ThermostatSetpointCC_Rev03.ts](../../../dut/zwave-js/handlers/tests/CCR_ThermostatSetpointCC_Rev03.ts) - State verification |
| 158 | +- [CCR_WindowCoveringCC_Rev02.ts](../../../dut/zwave-js/handlers/tests/CCR_WindowCoveringCC_Rev02.ts) - Level change with setTimeout |
| 159 | + |
| 160 | +## Workflow |
| 161 | + |
| 162 | +1. **Identify the test name** (e.g., `CCR_DoorLockCC_Rev02`) |
| 163 | +2. **Get log/prompt patterns** from user |
| 164 | +3. **Find the CC API** at https://zwave-js.github.io/zwave-js/#/api/CCs/index - only if this leaves questions open, look in `node_modules/@zwave-js/cc/build/esm/cc/<CC>CC.d.ts` |
| 165 | +4. **Find enum values** in `node_modules/@zwave-js/cc/build/esm/lib/_Types.d.ts` |
| 166 | +5. **Create handler** in `dut/zwave-js/handlers/tests/CCR_<Name>.ts` |
| 167 | +6. **Add capability questions** to `behaviors/capabilities.ts` if needed |
0 commit comments