Skip to content

Commit 9627902

Browse files
qdotclaude
andcommitted
test: verify DeviceListV4 message id is correct for system events vs responses
DeviceList events from device added/removed must have id=0 (system message), while DeviceList responses to RequestDeviceList must echo the request's id. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
1 parent 2fca8db commit 9627902

1 file changed

Lines changed: 81 additions & 0 deletions

File tree

crates/buttplug_tests/tests/test_server.rs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@ use buttplug_core::{
2525
BUTTPLUG_CURRENT_API_MAJOR_VERSION,
2626
BUTTPLUG_CURRENT_API_MINOR_VERSION,
2727
ButtplugClientMessageV4,
28+
ButtplugMessage,
2829
ButtplugMessageSpecVersion,
2930
ButtplugServerMessageV4,
3031
ErrorCode,
3132
OutputCmdV4,
3233
OutputCommand,
3334
OutputValue,
3435
PingV0,
36+
RequestDeviceListV0,
3537
RequestServerInfoV4,
3638
ServerInfoV4,
3739
StartScanningV0,
@@ -447,6 +449,85 @@ async fn test_server_scanning_finished() {
447449
assert!(finish_received);
448450
}
449451

452+
/// Tests that DeviceListV4 has the correct message id depending on context:
453+
/// - When sent as a system event (device added/removed), id must be 0.
454+
/// - When sent as a response to RequestDeviceList, id must match the request's id.
455+
#[tokio::test]
456+
async fn test_device_list_message_id_on_device_event_vs_request() {
457+
let mut builder = TestDeviceCommunicationManagerBuilder::default();
458+
let mut _device = builder.add_test_device(&TestDeviceIdentifier::new("Massage Demo", None));
459+
460+
let server = test_server_with_comm_manager(builder);
461+
462+
let recv = server.server_version_event_stream();
463+
pin_mut!(recv);
464+
465+
// Handshake
466+
assert!(
467+
server
468+
.parse_checked_message(
469+
RequestServerInfoV4::new(
470+
"Test Client",
471+
BUTTPLUG_CURRENT_API_MAJOR_VERSION,
472+
BUTTPLUG_CURRENT_API_MINOR_VERSION
473+
)
474+
.into()
475+
)
476+
.await
477+
.is_ok()
478+
);
479+
480+
// Start scanning — this will discover the device and emit a DeviceList event
481+
assert!(
482+
server
483+
.parse_checked_message(StartScanningV0::default().into())
484+
.await
485+
.is_ok()
486+
);
487+
488+
// Collect the DeviceList event from the event stream (device added notification).
489+
// This is a system message, so its id must be 0.
490+
while let Some(msg) = recv.next().await {
491+
if let ButtplugServerMessageV4::ScanningFinished(_) = msg {
492+
continue;
493+
} else if let ButtplugServerMessageV4::DeviceList(device_list) = msg {
494+
assert_eq!(
495+
device_list.id(),
496+
0,
497+
"DeviceList from device-added event should be a system message (id=0), got id={}",
498+
device_list.id()
499+
);
500+
break;
501+
} else {
502+
panic!("Unexpected message: {:?}", msg);
503+
}
504+
}
505+
506+
// Now send a RequestDeviceList with a specific id and verify the response echoes it.
507+
let request_id = 42;
508+
let mut request = RequestDeviceListV0::default();
509+
request.set_id(request_id);
510+
511+
let response = server
512+
.parse_checked_message(ButtplugCheckedClientMessageV4::RequestDeviceList(request))
513+
.await
514+
.expect("RequestDeviceList should succeed");
515+
if let ButtplugServerMessageV4::DeviceList(device_list) = response {
516+
assert_eq!(
517+
device_list.id(),
518+
request_id,
519+
"DeviceList response to RequestDeviceList should echo request id={}, got id={}",
520+
request_id,
521+
device_list.id()
522+
);
523+
} else {
524+
panic!(
525+
"Expected DeviceList response, got: {:?}",
526+
response
527+
);
528+
}
529+
}
530+
450531
// TODO Test sending system message (Id 0)
451532
// TODO Test sending system message (Ok but Id > 0)
452533
// TODO Test scan with no comm managers

0 commit comments

Comments
 (0)