Skip to content

Commit c0eadc1

Browse files
authored
test(e2e): add CLI tools E2E tests (#285)
Add comprehensive BATS tests for ya-modbus CLI commands (read, write, list-devices) using emulated devices. Changes: - Created tests/e2e/tests/04-cli.bats with 17 test cases covering help text, error handling, read/write operations, and list-devices - Refactored to use CLI_BIN variable with get_project_root() helper - Strengthened test assertions with specific error message validation and JSON structure verification - Fixed assertions to match actual driver output (capitalized names, correct values) All tests use existing helper functions and properly clean up resources in teardown. Closes #238 Fixes #286 Fixes #287
1 parent cca73cd commit c0eadc1

1 file changed

Lines changed: 240 additions & 0 deletions

File tree

tests/e2e/tests/04-cli.bats

Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
#!/usr/bin/env bats
2+
#
3+
# CLI tools tests
4+
# These tests verify that the ya-modbus CLI commands work correctly
5+
#
6+
7+
load helpers
8+
9+
setup() {
10+
EMULATOR_PID=""
11+
CLI_BIN="$(get_project_root)/packages/cli/dist/esm/bin/ya-modbus.js"
12+
clean_test_artifacts
13+
}
14+
15+
teardown() {
16+
if [ -n "$EMULATOR_PID" ]; then
17+
stop_test_emulator "$EMULATOR_PID"
18+
fi
19+
clean_test_artifacts
20+
}
21+
22+
@test "CLI binary exists" {
23+
[ -f "$CLI_BIN" ]
24+
}
25+
26+
@test "CLI shows help text" {
27+
run node "$CLI_BIN" --help
28+
assert_success
29+
assert_output_contains "Usage: ya-modbus"
30+
assert_output_contains "[options] [command]"
31+
assert_output_contains "Device Operations:"
32+
}
33+
34+
@test "read command shows help text" {
35+
run node "$CLI_BIN" read --help
36+
assert_success
37+
assert_output_contains "Read data points from device"
38+
assert_output_contains "--data-point"
39+
assert_output_contains "--all"
40+
}
41+
42+
@test "write command shows help text" {
43+
run node "$CLI_BIN" write --help
44+
assert_success
45+
assert_output_contains "Write data point to device"
46+
assert_output_contains "--data-point"
47+
assert_output_contains "--value"
48+
assert_output_contains "--verify"
49+
}
50+
51+
@test "list-devices command shows help text" {
52+
run node "$CLI_BIN" list-devices --help
53+
assert_success
54+
assert_output_contains "List supported devices"
55+
assert_output_contains "--driver"
56+
}
57+
58+
@test "read command fails with invalid slave ID" {
59+
run node "$CLI_BIN" read \
60+
--port /tmp/ttyV1 \
61+
--slave-id 999 \
62+
--driver @ya-modbus/driver-ex9em \
63+
--data-point voltage
64+
assert_failure
65+
assert_output_contains "Invalid slave ID"
66+
}
67+
68+
@test "read command fails without port or host" {
69+
run node "$CLI_BIN" read \
70+
--slave-id 1 \
71+
--driver @ya-modbus/driver-ex9em \
72+
--data-point voltage
73+
assert_failure
74+
assert_output_contains "cannot open"
75+
}
76+
77+
@test "read command fails without data point" {
78+
run node "$CLI_BIN" read \
79+
--port /tmp/ttyV1 \
80+
--slave-id 1 \
81+
--driver @ya-modbus/driver-ex9em
82+
assert_failure
83+
assert_output_contains "--data-point"
84+
}
85+
86+
@test "can read voltage from ex9em device" {
87+
# Start emulator
88+
run start_test_emulator "fixtures/emulators/port1-single-device.json"
89+
assert_success
90+
EMULATOR_PID="$output"
91+
92+
# Read voltage data point
93+
run node "$CLI_BIN" read \
94+
--port /tmp/ttyV1 \
95+
--slave-id 1 \
96+
--driver @ya-modbus/driver-ex9em \
97+
--data-point voltage
98+
99+
assert_success
100+
assert_output_contains "Voltage"
101+
assert_output_contains "230.0"
102+
assert_output_contains "V"
103+
assert_output_contains "Performance:"
104+
}
105+
106+
@test "can read multiple data points from ex9em device" {
107+
# Start emulator
108+
run start_test_emulator "fixtures/emulators/port1-single-device.json"
109+
assert_success
110+
EMULATOR_PID="$output"
111+
112+
# Read multiple data points
113+
run node "$CLI_BIN" read \
114+
--port /tmp/ttyV1 \
115+
--slave-id 1 \
116+
--driver @ya-modbus/driver-ex9em \
117+
--data-point voltage current frequency
118+
119+
assert_success
120+
# Verify all three data points present with values
121+
assert_output_contains "Voltage"
122+
assert_output_contains "230.0"
123+
assert_output_contains "Current"
124+
assert_output_contains "52.0"
125+
assert_output_contains "Frequency"
126+
assert_output_contains "50.0"
127+
}
128+
129+
@test "can read data in JSON format" {
130+
# Start emulator
131+
run start_test_emulator "fixtures/emulators/port1-single-device.json"
132+
assert_success
133+
EMULATOR_PID="$output"
134+
135+
# Read with JSON output
136+
run node "$CLI_BIN" read \
137+
--port /tmp/ttyV1 \
138+
--slave-id 1 \
139+
--driver @ya-modbus/driver-ex9em \
140+
--data-point voltage \
141+
--format json
142+
143+
assert_success
144+
# Validate JSON structure
145+
assert_output_contains '"dataPoints"'
146+
assert_output_contains '"voltage"'
147+
assert_output_contains '"driver"'
148+
assert_output_contains '"performance"'
149+
assert_output_contains "230"
150+
}
151+
152+
@test "can write device_address to ex9em device" {
153+
# Start emulator
154+
run start_test_emulator "fixtures/emulators/port1-single-device.json"
155+
assert_success
156+
EMULATOR_PID="$output"
157+
158+
# Write device_address (writable configuration parameter)
159+
run node "$CLI_BIN" write \
160+
--port /tmp/ttyV1 \
161+
--slave-id 1 \
162+
--driver @ya-modbus/driver-ex9em \
163+
--data-point device_address \
164+
--value 5 \
165+
--yes
166+
167+
assert_success
168+
assert_output_contains "Successfully wrote"
169+
assert_output_contains "device_address"
170+
}
171+
172+
@test "can write and verify device_address" {
173+
# Start emulator
174+
run start_test_emulator "fixtures/emulators/port1-single-device.json"
175+
assert_success
176+
EMULATOR_PID="$output"
177+
178+
# Write with verification
179+
run node "$CLI_BIN" write \
180+
--port /tmp/ttyV1 \
181+
--slave-id 1 \
182+
--driver @ya-modbus/driver-ex9em \
183+
--data-point device_address \
184+
--value 10 \
185+
--yes \
186+
--verify
187+
188+
assert_success
189+
assert_output_contains "Successfully wrote"
190+
assert_output_contains "Verification:"
191+
}
192+
193+
@test "write command fails for non-writable data point" {
194+
# Start emulator
195+
run start_test_emulator "fixtures/emulators/port1-single-device.json"
196+
assert_success
197+
EMULATOR_PID="$output"
198+
199+
# Try to write to a read-only data point
200+
run node "$CLI_BIN" write \
201+
--port /tmp/ttyV1 \
202+
--slave-id 1 \
203+
--driver @ya-modbus/driver-ex9em \
204+
--data-point voltage \
205+
--value 240 \
206+
--yes
207+
208+
assert_failure
209+
assert_output_contains "read-only"
210+
}
211+
212+
@test "list-devices command shows ex9em driver" {
213+
run node "$CLI_BIN" list-devices \
214+
--driver @ya-modbus/driver-ex9em
215+
216+
assert_success
217+
assert_output_contains "driver does not export"
218+
assert_output_contains "single-device driver"
219+
}
220+
221+
@test "list-devices shows message for single-device driver" {
222+
run node "$CLI_BIN" list-devices \
223+
--driver @ya-modbus/driver-or-we-516
224+
225+
assert_success
226+
assert_output_contains "single-device driver"
227+
}
228+
229+
@test "list-devices with JSON format for single-device driver" {
230+
run node "$CLI_BIN" list-devices \
231+
--driver @ya-modbus/driver-or-we-516 \
232+
--format json
233+
234+
assert_success
235+
# Validate JSON structure for single-device driver
236+
assert_output_contains '"devices": null'
237+
assert_output_contains '"defaultConfig"'
238+
assert_output_contains '"baudRate"'
239+
assert_output_contains '"parity"'
240+
}

0 commit comments

Comments
 (0)