bairelay 1.1.1

RTSP Relay for Reolink Baichuan cameras
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
//! CLI → oneshot-action converters and log-directive helpers.
//!
//! Pure glue lifted out of `main.rs` so unit tests can cover every
//! branch without spawning the full binary. The functions here bridge
//! the clap-derived enums in [`crate::cli`] with the action / type
//! enums the `oneshot` modules consume. Living in the library (rather
//! than as `From` impls on either side) keeps `crate::oneshot` free of
//! CLI concerns and `crate::cli` free of `bairelay_neolink_core` concerns —
//! each enum stays focused on its own boundary.

use tracing_subscriber::EnvFilter;

use crate::cli;
use crate::oneshot;

/// Decide whether to emit ANSI colour codes on the log stream. Off
/// when stderr is not a TTY (redirected to a file, piped to another
/// program, captured by a process supervisor) or when `NO_COLOR` is
/// set per <https://no-color.org>. Keeps the four-combination
/// decision table testable; the live TTY probe happens in `main.rs`.
pub fn should_emit_ansi(stderr_is_tty: bool, no_color_set: bool) -> bool {
	stderr_is_tty && !no_color_set
}

/// Translate a `-v` count into a `RUST_LOG` directive. `RUST_LOG`
/// always wins if set in the environment (explicit beats implicit —
/// operator-visible).
pub fn verbosity_env_filter(verbose: u8) -> EnvFilter {
	if let Ok(explicit) = EnvFilter::try_from_default_env() {
		return explicit;
	}
	let directive = match verbose {
		0 => "warn,bairelay=info,rumqttc=warn",
		1 => "info,bairelay=debug",
		2 => "debug,bairelay=trace,bairelay_neolink_core=debug",
		_ => "trace",
	};
	EnvFilter::new(directive)
}

/// Clone / normalise a `bairelay::cli::PtzCommand`. An absent
/// sub-command defaults to listing presets (`Preset { preset_id:
/// None }`) — the same shape as an explicit `ptz <cam> preset` with
/// no id.
pub fn clone_ptz_cmd(cmd: &Option<cli::PtzCommand>) -> cli::PtzCommand {
	use cli::PtzCommand::*;
	match cmd {
		Some(Preset { preset_id }) => Preset {
			preset_id: *preset_id,
		},
		Some(Assign { preset_id, name }) => Assign {
			preset_id: *preset_id,
			name: name.clone(),
		},
		Some(Control {
			amount,
			direction,
			speed,
		}) => Control {
			amount: *amount,
			direction: *direction,
			speed: *speed,
		},
		Some(Zoom { amount }) => Zoom { amount: *amount },
		None => Preset { preset_id: None },
	}
}

/// Map a CLI-level PTZ direction onto the bairelay_neolink_core value.
pub fn ptz_direction_to_core(
	d: cli::PtzDirection,
) -> bairelay_neolink_core::bc_protocol::Direction {
	use bairelay_neolink_core::bc_protocol::Direction;
	use cli::PtzDirection;
	match d {
		PtzDirection::Up => Direction::Up,
		PtzDirection::Down => Direction::Down,
		PtzDirection::Left => Direction::Left,
		PtzDirection::Right => Direction::Right,
		PtzDirection::Stop => Direction::Stop,
	}
}

/// Map a CLI-level service-name onto the oneshot `Service` enum.
pub fn service_name_to_core(s: cli::ServiceName) -> oneshot::services::Service {
	use cli::ServiceName;
	use oneshot::services::Service;
	match s {
		ServiceName::Baichuan => Service::Baichuan,
		ServiceName::Http => Service::Http,
		ServiceName::Https => Service::Https,
		ServiceName::Rtmp => Service::Rtmp,
		ServiceName::Rtsp => Service::Rtsp,
		ServiceName::Onvif => Service::Onvif,
	}
}

/// Clone / normalise a `cli::ServiceAction`. An absent action
/// defaults to `get` (read-mode).
pub fn clone_service_action(a: &Option<cli::ServiceAction>) -> oneshot::services::Action {
	use cli::{OnOff, ServiceAction};
	use oneshot::services::Action;
	match a {
		None | Some(ServiceAction::Get) => Action::Get,
		Some(ServiceAction::On) => Action::On,
		Some(ServiceAction::Off) => Action::Off,
		Some(ServiceAction::Port { port }) => Action::Port(*port),
		Some(ServiceAction::Set { port, enabled }) => Action::Set {
			port: *port,
			enabled: matches!(enabled, OnOff::On),
		},
	}
}

/// Clone / normalise a `cli::UserAction`. An absent action defaults
/// to `list`.
/// Translate a CLI [`cli::UserAction`] into the oneshot
/// [`oneshot::users::Action`].
///
/// `Add` / `Password` carry the password as `Option<String>` to allow
/// the operator to omit it on the command line; this function calls
/// [`oneshot::password_source::resolve`] to fill it in via TTY prompt
/// or stdin read. That path performs blocking I/O — fine for a one-
/// shot CLI but worth noting for callers in test contexts (tests
/// always pass `Some(_)` to bypass the prompt).
pub fn clone_user_action(a: &Option<cli::UserAction>) -> anyhow::Result<oneshot::users::Action> {
	use cli::{UserAction, UserTypeArg};
	use oneshot::password_source::{self, PROMPT_ADD, PROMPT_PASSWORD};
	use oneshot::users::{Action, UserType};
	let action = match a {
		None | Some(UserAction::List) => Action::List,
		Some(UserAction::Add {
			name,
			password,
			user_type,
		}) => {
			let password = password_source::resolve(password.clone(), PROMPT_ADD)?;
			Action::Add {
				name: name.clone(),
				password,
				user_type: match user_type {
					UserTypeArg::User => UserType::User,
					UserTypeArg::Administrator => UserType::Administrator,
				},
			}
		}
		Some(UserAction::Password { name, password }) => Action::Password {
			name: name.clone(),
			password: password_source::resolve(password.clone(), PROMPT_PASSWORD)?,
		},
		Some(UserAction::Delete { name }) => Action::Delete { name: name.clone() },
	};
	Ok(action)
}

/// Deep-clone a [`cli::Command`] value.
///
/// `Command` isn't `Clone` (the clap `Subcommand` variants hold
/// sub-commands that aren't `Clone` either), but tests and the
/// async-dispatch bridge need an owned copy so the `'static`-ish
/// closure bound on `runner::run` is satisfiable. This helper does
/// the hand-rolled deep copy — service-mode variants round-trip
/// through their payload fields; every one-shot variant preserves
/// `OneShotArgs` + any sub-flags.
pub fn clone_command(cmd: &cli::Command) -> cli::Command {
	use cli::Command;
	match cmd {
		Command::Mqtt => Command::Mqtt,
		Command::Rtsp { dump_bcmedia } => Command::Rtsp {
			dump_bcmedia: dump_bcmedia.clone(),
		},
		Command::MqttRtsp { dump_bcmedia } => Command::MqttRtsp {
			dump_bcmedia: dump_bcmedia.clone(),
		},
		Command::Reboot(a) => Command::Reboot(clone_oneshot_args(a)),
		Command::Battery(a) => Command::Battery(clone_oneshot_args(a)),
		Command::Presets(a) => Command::Presets(clone_oneshot_args(a)),
		Command::SetTime(a) => Command::SetTime(clone_oneshot_args(a)),
		Command::Version(a) => Command::Version(clone_oneshot_args(a)),
		Command::Siren(a) => Command::Siren(clone_oneshot_args(a)),
		Command::Abilities(a) => Command::Abilities(clone_oneshot_args(a)),
		Command::Snapshot {
			common,
			output,
			use_stream,
			use_stream_raw,
		} => Command::Snapshot {
			common: clone_oneshot_args(common),
			output: output.clone(),
			use_stream: *use_stream,
			use_stream_raw: *use_stream_raw,
		},
		Command::Floodlight { common, state } => Command::Floodlight {
			common: clone_oneshot_args(common),
			state: *state,
		},
		Command::Pir { common, state } => Command::Pir {
			common: clone_oneshot_args(common),
			state: *state,
		},
		Command::StatusLight { common, state } => Command::StatusLight {
			common: clone_oneshot_args(common),
			state: *state,
		},
		Command::Ptz { common, cmd } => Command::Ptz {
			common: clone_oneshot_args(common),
			cmd: cmd.as_ref().map(clone_ptz_sub),
		},
		Command::Services {
			common,
			service,
			action,
		} => Command::Services {
			common: clone_oneshot_args(common),
			service: *service,
			action: action.as_ref().map(clone_service_sub),
		},
		Command::Users { common, action } => Command::Users {
			common: clone_oneshot_args(common),
			action: action.as_ref().map(clone_user_sub),
		},
		Command::CheckConfig => Command::CheckConfig,
		Command::RenderHassioConfig {
			options_json,
			overlay,
			mqtt_host,
			mqtt_port,
			mqtt_user,
			mqtt_pass,
			mqtt_ssl,
			output,
		} => Command::RenderHassioConfig {
			options_json: options_json.clone(),
			overlay: overlay.clone(),
			mqtt_host: mqtt_host.clone(),
			mqtt_port: *mqtt_port,
			mqtt_user: mqtt_user.clone(),
			mqtt_pass: mqtt_pass.clone(),
			mqtt_ssl: *mqtt_ssl,
			output: output.clone(),
		},
	}
}

fn clone_oneshot_args(a: &cli::OneShotArgs) -> cli::OneShotArgs {
	cli::OneShotArgs {
		camera: a.camera.clone(),
	}
}

/// Sub-helper for `clone_command`: deep-clone a `cli::PtzCommand`
/// variant without normalising `None` the way `clone_ptz_cmd` does.
fn clone_ptz_sub(c: &cli::PtzCommand) -> cli::PtzCommand {
	use cli::PtzCommand::*;
	match c {
		Preset { preset_id } => Preset {
			preset_id: *preset_id,
		},
		Assign { preset_id, name } => Assign {
			preset_id: *preset_id,
			name: name.clone(),
		},
		Control {
			amount,
			direction,
			speed,
		} => Control {
			amount: *amount,
			direction: *direction,
			speed: *speed,
		},
		Zoom { amount } => Zoom { amount: *amount },
	}
}

/// Sub-helper: deep-clone a `cli::ServiceAction`.
fn clone_service_sub(a: &cli::ServiceAction) -> cli::ServiceAction {
	use cli::ServiceAction::*;
	match a {
		Get => Get,
		On => On,
		Off => Off,
		Port { port } => Port { port: *port },
		Set { port, enabled } => Set {
			port: *port,
			enabled: *enabled,
		},
	}
}

/// Sub-helper: deep-clone a `cli::UserAction`.
fn clone_user_sub(a: &cli::UserAction) -> cli::UserAction {
	use cli::UserAction::*;
	match a {
		List => List,
		Add {
			name,
			password,
			user_type,
		} => Add {
			name: name.clone(),
			password: password.clone(),
			user_type: *user_type,
		},
		Password { name, password } => Password {
			name: name.clone(),
			password: password.clone(),
		},
		Delete { name } => Delete { name: name.clone() },
	}
}

/// Coarse-grained label for a one-shot exit code, for error reporting
/// to operators.
pub fn exit_code_to_kind(code: i32) -> &'static str {
	use crate::oneshot::classify::{
		EXIT_CONFIG, EXIT_CONNECTION, EXIT_INTERRUPTED, EXIT_PROTOCOL, EXIT_UNSUPPORTED, EXIT_USAGE,
	};
	match code {
		EXIT_USAGE => "usage",
		EXIT_CONFIG => "config",
		EXIT_CONNECTION => "connection",
		EXIT_PROTOCOL => "protocol",
		EXIT_UNSUPPORTED => "unsupported",
		EXIT_INTERRUPTED => "interrupted",
		_ => "internal",
	}
}

#[cfg(test)]
mod tests {
	use super::*;

	#[test]
	fn should_emit_ansi_only_when_tty_and_no_color_unset() {
		// TTY + no NO_COLOR → colour on (interactive terminal).
		assert!(should_emit_ansi(true, false));
		// Not a TTY (file redirect, pipe) → colour off regardless.
		assert!(!should_emit_ansi(false, false));
		// NO_COLOR set → colour off even on a TTY.
		assert!(!should_emit_ansi(true, true));
		// Both off-conditions → colour off.
		assert!(!should_emit_ansi(false, true));
	}

	#[test]
	fn verbosity_env_filter_maps_each_level() {
		// Make sure RUST_LOG isn't set in the test env so the
		// implicit mapping path runs (don't touch global env here —
		// we just verify the fn doesn't panic and builds an EnvFilter
		// for each level).
		// SAFETY (single-threaded test, scoped flip): ensure the
		// explicit env-var branch is not taken.
		let previous = std::env::var("RUST_LOG").ok();
		unsafe {
			std::env::remove_var("RUST_LOG");
		}
		for v in [0u8, 1, 2, 3, 255] {
			let _f = verbosity_env_filter(v);
		}
		if let Some(v) = previous {
			unsafe {
				std::env::set_var("RUST_LOG", v);
			}
		}
	}

	#[test]
	fn clone_ptz_cmd_none_defaults_to_preset_list() {
		match clone_ptz_cmd(&None) {
			cli::PtzCommand::Preset { preset_id: None } => {}
			other => panic!("expected Preset{{None}}, got {other:?}"),
		}
	}

	#[test]
	fn clone_ptz_cmd_preserves_preset_id() {
		let got = clone_ptz_cmd(&Some(cli::PtzCommand::Preset { preset_id: Some(3) }));
		match got {
			cli::PtzCommand::Preset { preset_id: Some(3) } => {}
			other => panic!("expected Preset{{Some(3)}}, got {other:?}"),
		}
	}

	#[test]
	fn clone_ptz_cmd_preserves_assign() {
		let got = clone_ptz_cmd(&Some(cli::PtzCommand::Assign {
			preset_id: 5,
			name: "home".into(),
		}));
		match got {
			cli::PtzCommand::Assign { preset_id: 5, name } if name == "home" => {}
			other => panic!("expected Assign{{5,\"home\"}}, got {other:?}"),
		}
	}

	#[test]
	fn clone_ptz_cmd_preserves_control() {
		let got = clone_ptz_cmd(&Some(cli::PtzCommand::Control {
			amount: 3,
			direction: cli::PtzDirection::Up,
			speed: Some(32),
		}));
		match got {
			cli::PtzCommand::Control {
				amount,
				direction: cli::PtzDirection::Up,
				speed,
			} => {
				assert_eq!(amount, 3);
				assert_eq!(speed, Some(32));
			}
			other => panic!("expected Control{{..}}, got {other:?}"),
		}
	}

	#[test]
	fn clone_ptz_cmd_preserves_zoom() {
		let got = clone_ptz_cmd(&Some(cli::PtzCommand::Zoom { amount: 0.75 }));
		match got {
			cli::PtzCommand::Zoom { amount } => assert_eq!(amount, 0.75),
			other => panic!("expected Zoom, got {other:?}"),
		}
	}

	#[test]
	fn ptz_direction_to_core_maps_every_variant() {
		use bairelay_neolink_core::bc_protocol::Direction;
		assert!(matches!(
			ptz_direction_to_core(cli::PtzDirection::Up),
			Direction::Up
		));
		assert!(matches!(
			ptz_direction_to_core(cli::PtzDirection::Down),
			Direction::Down
		));
		assert!(matches!(
			ptz_direction_to_core(cli::PtzDirection::Left),
			Direction::Left
		));
		assert!(matches!(
			ptz_direction_to_core(cli::PtzDirection::Right),
			Direction::Right
		));
		assert!(matches!(
			ptz_direction_to_core(cli::PtzDirection::Stop),
			Direction::Stop
		));
	}

	#[test]
	fn service_name_to_core_maps_every_variant() {
		use oneshot::services::Service;
		assert!(matches!(
			service_name_to_core(cli::ServiceName::Baichuan),
			Service::Baichuan
		));
		assert!(matches!(
			service_name_to_core(cli::ServiceName::Http),
			Service::Http
		));
		assert!(matches!(
			service_name_to_core(cli::ServiceName::Https),
			Service::Https
		));
		assert!(matches!(
			service_name_to_core(cli::ServiceName::Rtmp),
			Service::Rtmp
		));
		assert!(matches!(
			service_name_to_core(cli::ServiceName::Rtsp),
			Service::Rtsp
		));
		assert!(matches!(
			service_name_to_core(cli::ServiceName::Onvif),
			Service::Onvif
		));
	}

	#[test]
	fn clone_service_action_defaults_to_get_when_absent() {
		use oneshot::services::Action;
		assert!(matches!(clone_service_action(&None), Action::Get));
	}

	#[test]
	fn clone_service_action_maps_each_variant() {
		use cli::{OnOff, ServiceAction};
		use oneshot::services::Action;
		assert!(matches!(
			clone_service_action(&Some(ServiceAction::Get)),
			Action::Get
		));
		assert!(matches!(
			clone_service_action(&Some(ServiceAction::On)),
			Action::On
		));
		assert!(matches!(
			clone_service_action(&Some(ServiceAction::Off)),
			Action::Off
		));
		assert!(matches!(
			clone_service_action(&Some(ServiceAction::Port { port: 8080 })),
			Action::Port(8080)
		));
		let set_on = clone_service_action(&Some(ServiceAction::Set {
			port: 443,
			enabled: OnOff::On,
		}));
		match set_on {
			Action::Set { port, enabled } => {
				assert_eq!(port, 443);
				assert!(enabled);
			}
			other => panic!("expected Set, got {other:?}"),
		}
		let set_off = clone_service_action(&Some(ServiceAction::Set {
			port: 80,
			enabled: OnOff::Off,
		}));
		match set_off {
			Action::Set { port, enabled } => {
				assert_eq!(port, 80);
				assert!(!enabled);
			}
			other => panic!("expected Set(off), got {other:?}"),
		}
	}

	#[test]
	fn clone_user_action_defaults_to_list_when_absent() {
		use oneshot::users::Action;
		assert!(matches!(
			clone_user_action(&None).expect("ok"),
			Action::List
		));
	}

	#[test]
	fn clone_user_action_maps_each_variant() {
		use cli::{UserAction, UserTypeArg};
		use oneshot::users::{Action, UserType};
		assert!(matches!(
			clone_user_action(&Some(UserAction::List)).expect("ok"),
			Action::List
		));
		let add = clone_user_action(&Some(UserAction::Add {
			name: "bob".into(),
			password: Some("pw".into()),
			user_type: UserTypeArg::Administrator,
		}))
		.expect("ok");
		match add {
			Action::Add {
				name,
				password,
				user_type: UserType::Administrator,
			} => {
				assert_eq!(name, "bob");
				assert_eq!(password, "pw");
			}
			other => panic!("expected Add Administrator, got {other:?}"),
		}
		let add_user = clone_user_action(&Some(UserAction::Add {
			name: "a".into(),
			password: Some("b".into()),
			user_type: UserTypeArg::User,
		}))
		.expect("ok");
		assert!(matches!(
			add_user,
			Action::Add {
				user_type: UserType::User,
				..
			}
		));

		let pw = clone_user_action(&Some(UserAction::Password {
			name: "x".into(),
			password: Some("y".into()),
		}))
		.expect("ok");
		match pw {
			Action::Password { name, password } => {
				assert_eq!(name, "x");
				assert_eq!(password, "y");
			}
			other => panic!("expected Password, got {other:?}"),
		}
		let del = clone_user_action(&Some(UserAction::Delete { name: "z".into() })).expect("ok");
		match del {
			Action::Delete { name } => assert_eq!(name, "z"),
			other => panic!("expected Delete, got {other:?}"),
		}
	}

	#[test]
	fn clone_user_action_empty_provided_password_rejected() {
		use cli::{UserAction, UserTypeArg};
		// Empty positional should classify as a usage error (operator
		// pilot error: `bairelay users add bob user ""`). The
		// password resolver enforces non-empty regardless of source.
		let err = clone_user_action(&Some(UserAction::Add {
			name: "bob".into(),
			password: Some(String::new()),
			user_type: UserTypeArg::User,
		}))
		.expect_err("must reject empty password");
		assert!(
			err.chain()
				.any(|c| c.is::<crate::oneshot::errors::UsageError>()),
			"empty password must classify as UsageError; chain: {err:#}"
		);
	}

	fn args(name: &str) -> cli::OneShotArgs {
		cli::OneShotArgs {
			camera: name.into(),
		}
	}

	#[test]
	fn clone_command_service_modes() {
		use std::path::PathBuf;
		assert!(matches!(
			clone_command(&cli::Command::Mqtt),
			cli::Command::Mqtt
		));

		let r = clone_command(&cli::Command::Rtsp {
			dump_bcmedia: Some(PathBuf::from("/tmp/dump")),
		});
		match r {
			cli::Command::Rtsp { dump_bcmedia } => {
				assert_eq!(dump_bcmedia.unwrap(), PathBuf::from("/tmp/dump"))
			}
			_ => panic!(),
		}
		let r = clone_command(&cli::Command::Rtsp { dump_bcmedia: None });
		match r {
			cli::Command::Rtsp { dump_bcmedia: None } => {}
			_ => panic!(),
		}
		let r = clone_command(&cli::Command::MqttRtsp {
			dump_bcmedia: Some(PathBuf::from("/tmp/a")),
		});
		match r {
			cli::Command::MqttRtsp { dump_bcmedia } => {
				assert_eq!(dump_bcmedia.unwrap(), PathBuf::from("/tmp/a"))
			}
			_ => panic!(),
		}
	}

	#[test]
	fn clone_command_oneshot_simple_variants() {
		match clone_command(&cli::Command::Reboot(args("c1"))) {
			cli::Command::Reboot(a) => assert_eq!(a.camera, "c1"),
			_ => panic!(),
		}
		match clone_command(&cli::Command::Battery(args("c2"))) {
			cli::Command::Battery(a) => assert_eq!(a.camera, "c2"),
			_ => panic!(),
		}
		match clone_command(&cli::Command::Presets(args("c3"))) {
			cli::Command::Presets(a) => assert_eq!(a.camera, "c3"),
			_ => panic!(),
		}
		match clone_command(&cli::Command::SetTime(args("c4"))) {
			cli::Command::SetTime(a) => assert_eq!(a.camera, "c4"),
			_ => panic!(),
		}
		match clone_command(&cli::Command::Version(args("c5"))) {
			cli::Command::Version(a) => assert_eq!(a.camera, "c5"),
			_ => panic!(),
		}
		match clone_command(&cli::Command::Siren(args("c6"))) {
			cli::Command::Siren(a) => assert_eq!(a.camera, "c6"),
			_ => panic!(),
		}
		match clone_command(&cli::Command::Abilities(args("c7"))) {
			cli::Command::Abilities(a) => assert_eq!(a.camera, "c7"),
			_ => panic!(),
		}
	}

	#[test]
	fn clone_command_snapshot_preserves_every_field() {
		use std::path::PathBuf;
		let src = cli::Command::Snapshot {
			common: args("cam"),
			output: Some(PathBuf::from("/tmp/out.jpg")),
			use_stream: true,
			use_stream_raw: false,
		};
		match clone_command(&src) {
			cli::Command::Snapshot {
				common,
				output,
				use_stream,
				use_stream_raw,
			} => {
				assert_eq!(common.camera, "cam");
				assert_eq!(output.unwrap(), PathBuf::from("/tmp/out.jpg"));
				assert!(use_stream);
				assert!(!use_stream_raw);
			}
			_ => panic!(),
		}
		// variant with raw=true
		let src = cli::Command::Snapshot {
			common: args("cam"),
			output: None,
			use_stream: false,
			use_stream_raw: true,
		};
		match clone_command(&src) {
			cli::Command::Snapshot {
				output: None,
				use_stream: false,
				use_stream_raw: true,
				..
			} => {}
			_ => panic!(),
		}
	}

	#[test]
	fn clone_command_floodlight_pir_statuslight() {
		use cli::{FloodlightState, OnOff};
		match clone_command(&cli::Command::Floodlight {
			common: args("fc"),
			state: Some(FloodlightState::On),
		}) {
			cli::Command::Floodlight {
				common,
				state: Some(FloodlightState::On),
			} => {
				assert_eq!(common.camera, "fc")
			}
			_ => panic!(),
		}
		match clone_command(&cli::Command::Floodlight {
			common: args("fc"),
			state: None,
		}) {
			cli::Command::Floodlight { state: None, .. } => {}
			_ => panic!(),
		}
		match clone_command(&cli::Command::Pir {
			common: args("p"),
			state: Some(OnOff::Off),
		}) {
			cli::Command::Pir {
				state: Some(OnOff::Off),
				..
			} => {}
			_ => panic!(),
		}
		match clone_command(&cli::Command::StatusLight {
			common: args("s"),
			state: Some(OnOff::On),
		}) {
			cli::Command::StatusLight {
				state: Some(OnOff::On),
				..
			} => {}
			_ => panic!(),
		}
	}

	#[test]
	fn clone_command_ptz_all_subcommands() {
		// None → kept as None (clone_ptz_sub only runs when Some)
		match clone_command(&cli::Command::Ptz {
			common: args("p"),
			cmd: None,
		}) {
			cli::Command::Ptz { cmd: None, .. } => {}
			_ => panic!(),
		}
		// Preset
		match clone_command(&cli::Command::Ptz {
			common: args("p"),
			cmd: Some(cli::PtzCommand::Preset { preset_id: Some(9) }),
		}) {
			cli::Command::Ptz {
				cmd: Some(cli::PtzCommand::Preset { preset_id: Some(9) }),
				..
			} => {}
			_ => panic!(),
		}
		// Assign
		match clone_command(&cli::Command::Ptz {
			common: args("p"),
			cmd: Some(cli::PtzCommand::Assign {
				preset_id: 1,
				name: "n".into(),
			}),
		}) {
			cli::Command::Ptz {
				cmd: Some(cli::PtzCommand::Assign { preset_id: 1, name }),
				..
			} if name == "n" => {}
			_ => panic!(),
		}
		// Control
		match clone_command(&cli::Command::Ptz {
			common: args("p"),
			cmd: Some(cli::PtzCommand::Control {
				amount: 2,
				direction: cli::PtzDirection::Left,
				speed: Some(10),
			}),
		}) {
			cli::Command::Ptz {
				cmd:
					Some(cli::PtzCommand::Control {
						amount: 2,
						direction: cli::PtzDirection::Left,
						speed: Some(10),
					}),
				..
			} => {}
			_ => panic!(),
		}
		// Zoom
		match clone_command(&cli::Command::Ptz {
			common: args("p"),
			cmd: Some(cli::PtzCommand::Zoom { amount: 0.5 }),
		}) {
			cli::Command::Ptz {
				cmd: Some(cli::PtzCommand::Zoom { amount }),
				..
			} => {
				assert_eq!(amount, 0.5)
			}
			_ => panic!(),
		}
	}

	#[test]
	fn clone_command_services_all_subactions() {
		use cli::{OnOff, ServiceAction, ServiceName};
		match clone_command(&cli::Command::Services {
			common: args("s"),
			service: None,
			action: None,
		}) {
			cli::Command::Services {
				service: None,
				action: None,
				..
			} => {}
			_ => panic!(),
		}
		match clone_command(&cli::Command::Services {
			common: args("s"),
			service: Some(ServiceName::Http),
			action: Some(ServiceAction::Get),
		}) {
			cli::Command::Services {
				action: Some(ServiceAction::Get),
				..
			} => {}
			_ => panic!(),
		}
		match clone_command(&cli::Command::Services {
			common: args("s"),
			service: Some(ServiceName::Https),
			action: Some(ServiceAction::On),
		}) {
			cli::Command::Services {
				action: Some(ServiceAction::On),
				..
			} => {}
			_ => panic!(),
		}
		match clone_command(&cli::Command::Services {
			common: args("s"),
			service: Some(ServiceName::Rtsp),
			action: Some(ServiceAction::Off),
		}) {
			cli::Command::Services {
				action: Some(ServiceAction::Off),
				..
			} => {}
			_ => panic!(),
		}
		match clone_command(&cli::Command::Services {
			common: args("s"),
			service: Some(ServiceName::Onvif),
			action: Some(ServiceAction::Port { port: 8080 }),
		}) {
			cli::Command::Services {
				action: Some(ServiceAction::Port { port: 8080 }),
				..
			} => {}
			_ => panic!(),
		}
		match clone_command(&cli::Command::Services {
			common: args("s"),
			service: Some(ServiceName::Rtmp),
			action: Some(ServiceAction::Set {
				port: 443,
				enabled: OnOff::On,
			}),
		}) {
			cli::Command::Services {
				action: Some(ServiceAction::Set {
					port: 443,
					enabled: OnOff::On,
				}),
				..
			} => {}
			_ => panic!(),
		}
	}

	#[test]
	fn clone_command_users_all_subactions() {
		use cli::{UserAction, UserTypeArg};
		match clone_command(&cli::Command::Users {
			common: args("u"),
			action: None,
		}) {
			cli::Command::Users { action: None, .. } => {}
			_ => panic!(),
		}
		match clone_command(&cli::Command::Users {
			common: args("u"),
			action: Some(UserAction::List),
		}) {
			cli::Command::Users {
				action: Some(UserAction::List),
				..
			} => {}
			_ => panic!(),
		}
		match clone_command(&cli::Command::Users {
			common: args("u"),
			action: Some(UserAction::Add {
				name: "a".into(),
				password: Some("b".into()),
				user_type: UserTypeArg::Administrator,
			}),
		}) {
			cli::Command::Users {
				action:
					Some(UserAction::Add {
						name,
						password,
						user_type: UserTypeArg::Administrator,
					}),
				..
			} => {
				assert_eq!(name, "a");
				assert_eq!(password.as_deref(), Some("b"));
			}
			_ => panic!(),
		}
		match clone_command(&cli::Command::Users {
			common: args("u"),
			action: Some(UserAction::Add {
				name: "a".into(),
				password: Some("b".into()),
				user_type: UserTypeArg::User,
			}),
		}) {
			cli::Command::Users {
				action: Some(UserAction::Add {
					user_type: UserTypeArg::User,
					..
				}),
				..
			} => {}
			_ => panic!(),
		}
		match clone_command(&cli::Command::Users {
			common: args("u"),
			action: Some(UserAction::Password {
				name: "a".into(),
				password: Some("b".into()),
			}),
		}) {
			cli::Command::Users {
				action: Some(UserAction::Password { name, password }),
				..
			} => {
				assert_eq!(name, "a");
				assert_eq!(password.as_deref(), Some("b"));
			}
			_ => panic!(),
		}
		match clone_command(&cli::Command::Users {
			common: args("u"),
			action: Some(UserAction::Delete { name: "z".into() }),
		}) {
			cli::Command::Users {
				action: Some(UserAction::Delete { name }),
				..
			} => {
				assert_eq!(name, "z");
			}
			_ => panic!(),
		}
	}

	#[test]
	fn verbosity_honours_explicit_rust_log() {
		// Force the explicit branch by setting RUST_LOG.
		let previous = std::env::var("RUST_LOG").ok();
		unsafe {
			std::env::set_var("RUST_LOG", "warn");
		}
		let _ = verbosity_env_filter(0);
		if let Some(v) = previous {
			unsafe {
				std::env::set_var("RUST_LOG", v);
			}
		} else {
			unsafe {
				std::env::remove_var("RUST_LOG");
			}
		}
	}

	#[test]
	fn exit_code_to_kind_covers_every_label() {
		use crate::oneshot::classify::{
			EXIT_CONFIG, EXIT_CONNECTION, EXIT_INTERRUPTED, EXIT_PROTOCOL, EXIT_UNSUPPORTED,
			EXIT_USAGE,
		};
		assert_eq!(exit_code_to_kind(EXIT_USAGE), "usage");
		assert_eq!(exit_code_to_kind(EXIT_CONFIG), "config");
		assert_eq!(exit_code_to_kind(EXIT_CONNECTION), "connection");
		assert_eq!(exit_code_to_kind(EXIT_PROTOCOL), "protocol");
		assert_eq!(exit_code_to_kind(EXIT_UNSUPPORTED), "unsupported");
		assert_eq!(exit_code_to_kind(EXIT_INTERRUPTED), "interrupted");
		assert_eq!(exit_code_to_kind(0), "internal");
		assert_eq!(exit_code_to_kind(999), "internal");
	}
}