Skip to content

Commit 03d1e0c

Browse files
authored
test(e2e): add discover and show-defaults command tests (#315)
Adds comprehensive E2E test coverage for two CLI commands that were previously untested: - `discover` - Modbus device discovery with parameter scanning - `show-defaults` - Display driver DEFAULT_CONFIG and SUPPORTED_CONFIG Both commands are fully implemented and functional but lacked integration testing. ## Test Coverage ### show-defaults Command (7 tests) - Help text display - Default configuration display - Supported configuration display - JSON format output with structure validation - Support for both multi-device and single-device drivers - Error handling for missing --driver option - Error handling for non-existent drivers ### discover Command (7 tests) - Help text display - Quick strategy discovery with single device - Multi-device discovery test - JSON format output with structure validation - Verbose mode showing progress messages - Silent mode suppressing non-JSON output - Error handling for missing --port option ## Implementation Notes ### Performance Optimization All discover tests use restrictive parameters to minimize test execution time: - `--max-devices 1` - Stop after finding first device (single-device tests) - `--max-devices 2` - Find both devices (multi-device test) - `--driver` flag - Use driver SUPPORTED_CONFIG to narrow parameter space - `--strategy quick` - Test only common parameters ### Test Structure Tests follow existing patterns from PR #285: - Use emulator fixtures (port1-single-device, port2-multi-device) - Validate both table and JSON output formats - Include error handling and edge cases ## Verification All 55 E2E tests pass: ```bash cd tests/e2e ./run-tests.sh # 55 tests, 0 failures ``` Closes #289
1 parent c0eadc1 commit 03d1e0c

2 files changed

Lines changed: 201 additions & 1 deletion

File tree

tests/e2e/fixtures/emulators/port2-multi-device.json

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"description": "XYMD1 temperature controller on port 2 (OR-WE-516 removed due to parity incompatibility)",
2+
"description": "Two devices on port 2: XYMD1 and EX9EM (both use even parity)",
33
"transport": {
44
"type": "rtu",
55
"port": "/tmp/ttyV2",
@@ -24,6 +24,33 @@
2424
"processingDelay": [1, 3],
2525
"perRegisterDelay": 0.1
2626
}
27+
},
28+
{
29+
"slaveId": 2,
30+
"deviceType": "EX9EM",
31+
"registers": {
32+
"holding": {
33+
"0": 2200,
34+
"1": 480,
35+
"2": 500,
36+
"3": 1050,
37+
"4": 95,
38+
"5": 1150,
39+
"6": 950,
40+
"7": 1111,
41+
"8": 2222,
42+
"9": 3333,
43+
"10": 4444,
44+
"42": 4,
45+
"43": 2
46+
}
47+
},
48+
"timing": {
49+
"pollingInterval": 10,
50+
"commandDetectionDelay": [2, 5],
51+
"processingDelay": [1, 3],
52+
"perRegisterDelay": 0.1
53+
}
2754
}
2855
]
2956
}

tests/e2e/tests/04-cli.bats

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,3 +238,176 @@ teardown() {
238238
assert_output_contains '"baudRate"'
239239
assert_output_contains '"parity"'
240240
}
241+
242+
@test "show-defaults command shows help text" {
243+
run node "$CLI_BIN" show-defaults --help
244+
assert_success
245+
assert_output_contains "show-defaults"
246+
assert_output_contains "--driver"
247+
assert_output_contains "--format"
248+
}
249+
250+
@test "show-defaults displays driver configuration" {
251+
run node "$CLI_BIN" show-defaults \
252+
--driver @ya-modbus/driver-ex9em
253+
254+
assert_success
255+
assert_output_contains "DEFAULT_CONFIG"
256+
assert_output_contains "baudRate"
257+
assert_output_contains "9600"
258+
assert_output_contains "parity"
259+
assert_output_contains "even"
260+
}
261+
262+
@test "show-defaults displays supported configuration" {
263+
run node "$CLI_BIN" show-defaults \
264+
--driver @ya-modbus/driver-ex9em
265+
266+
assert_success
267+
assert_output_contains "SUPPORTED_CONFIG"
268+
assert_output_contains "validBaudRates"
269+
assert_output_contains "validParity"
270+
}
271+
272+
@test "show-defaults outputs JSON format" {
273+
run node "$CLI_BIN" show-defaults \
274+
--driver @ya-modbus/driver-ex9em \
275+
--format json
276+
277+
assert_success
278+
279+
# Validate JSON structure
280+
echo "$output" | jq -e 'has("defaultConfig")'
281+
echo "$output" | jq -e 'has("supportedConfig")'
282+
echo "$output" | jq -e '.defaultConfig.baudRate == 9600'
283+
echo "$output" | jq -e '.defaultConfig.parity == "even"'
284+
}
285+
286+
@test "show-defaults works with single-device driver" {
287+
run node "$CLI_BIN" show-defaults \
288+
--driver @ya-modbus/driver-or-we-516
289+
290+
assert_success
291+
assert_output_contains "DEFAULT_CONFIG"
292+
assert_output_contains "baudRate"
293+
assert_output_contains "9600"
294+
assert_output_contains "parity"
295+
assert_output_contains "odd"
296+
}
297+
298+
@test "show-defaults fails without --driver" {
299+
run node "$CLI_BIN" show-defaults
300+
301+
assert_failure
302+
assert_output_contains "driver"
303+
}
304+
305+
@test "show-defaults fails with non-existent driver" {
306+
run node "$CLI_BIN" show-defaults \
307+
--driver @ya-modbus/driver-nonexistent
308+
309+
assert_failure
310+
assert_output_contains "Driver package not found"
311+
}
312+
313+
@test "discover command shows help text" {
314+
run node "$CLI_BIN" discover --help
315+
assert_success
316+
assert_output_contains "discover"
317+
assert_output_contains "--port"
318+
assert_output_contains "--strategy"
319+
assert_output_contains "--driver"
320+
}
321+
322+
@test "discover finds single device with quick strategy" {
323+
run start_test_emulator "fixtures/emulators/port1-single-device.json"
324+
assert_success
325+
EMULATOR_PID="$output"
326+
327+
run node "$CLI_BIN" discover \
328+
--port /tmp/ttyV1 \
329+
--driver @ya-modbus/driver-ex9em \
330+
--strategy quick \
331+
--max-devices 1
332+
333+
assert_success
334+
assert_output_contains "Found 1 device"
335+
assert_output_contains "Slave ID 1"
336+
assert_output_contains "9600"
337+
}
338+
339+
@test "discover finds multiple devices" {
340+
run start_test_emulator "fixtures/emulators/port2-multi-device.json"
341+
assert_success
342+
EMULATOR_PID="$output"
343+
344+
run node "$CLI_BIN" discover \
345+
--port /tmp/ttyV3 \
346+
--strategy quick \
347+
--max-devices 2
348+
349+
assert_success
350+
assert_output_contains "Found 2 device"
351+
assert_output_contains "Slave ID 1"
352+
assert_output_contains "Slave ID 2"
353+
}
354+
355+
@test "discover outputs JSON format" {
356+
run start_test_emulator "fixtures/emulators/port1-single-device.json"
357+
assert_success
358+
EMULATOR_PID="$output"
359+
360+
run node "$CLI_BIN" discover \
361+
--port /tmp/ttyV1 \
362+
--driver @ya-modbus/driver-ex9em \
363+
--strategy quick \
364+
--max-devices 1 \
365+
--silent \
366+
--format json
367+
368+
assert_success
369+
echo "$output" | jq -e '.[0].slaveId == 1'
370+
echo "$output" | jq -e '.[0].baudRate == 9600'
371+
}
372+
373+
@test "discover with verbose shows progress" {
374+
run start_test_emulator "fixtures/emulators/port1-single-device.json"
375+
assert_success
376+
EMULATOR_PID="$output"
377+
378+
run node "$CLI_BIN" discover \
379+
--port /tmp/ttyV1 \
380+
--driver @ya-modbus/driver-ex9em \
381+
--strategy quick \
382+
--max-devices 1 \
383+
--verbose
384+
385+
assert_success
386+
assert_output_contains "Testing"
387+
}
388+
389+
@test "discover with silent suppresses progress" {
390+
run start_test_emulator "fixtures/emulators/port1-single-device.json"
391+
assert_success
392+
EMULATOR_PID="$output"
393+
394+
run node "$CLI_BIN" discover \
395+
--port /tmp/ttyV1 \
396+
--driver @ya-modbus/driver-ex9em \
397+
--strategy quick \
398+
--max-devices 1 \
399+
--silent \
400+
--format json
401+
402+
assert_success
403+
# Output should only be JSON, no progress messages
404+
echo "$output" | jq -e 'type == "array"'
405+
}
406+
407+
@test "discover fails without --port" {
408+
run node "$CLI_BIN" discover \
409+
--driver @ya-modbus/driver-ex9em
410+
411+
assert_failure
412+
assert_output_contains "port"
413+
}

0 commit comments

Comments
 (0)