ocppsim 0.1.0

A terminal OCPP-J charge point simulator.
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
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
use std::env;
use std::path::{Path, PathBuf};

use anyhow::{Result, bail};
use clap::{ArgAction, Parser, ValueEnum};

use crate::config::{ProfileDefaults, resolve_profile};
use crate::ocpp::OcppVersion;

const DEFAULT_CONNECTORS: u16 = 1;
const DEFAULT_PROTOCOL: OcppVersion = OcppVersion::V1_6;
const DEFAULT_VENDOR: &str = "ocppsim";
const DEFAULT_MODEL: &str = "ocppsim";
const DEFAULT_FIRMWARE: &str = env!("CARGO_PKG_VERSION");
const DEFAULT_REQUEST_TIMEOUT_SECONDS: u64 = 30;
const DEFAULT_CONFIG_PATH_HINT: &str = "~/.config/ocppsim/ocppsim.toml";
const CLI_LONG_ABOUT: &str =
  "Command-line OCPP charge point simulator for OCPP-J.
Run in direct mode with --ws-url and --cp-id,
or pass a profile name from a TOML config file.";
const CLI_AFTER_HELP: &str = r#"Config file format:
  # Optional global defaults that can be overridden by charge point configs.
  # CLI options take precedence over config file items.
  protocol = "2.1"
  vendor = "ocppsim"
  model = "ocppsim"
  firmware = "0.1.0"
  log-path = "./ocppsim.log"
  trace-frames = false
  strict = false
  request-timeout-seconds = 30
  heartbeat-seconds = 0

  [charge-points.example]
  ws-url = "wss://csms.example.com/ocpp"
  id = "CP-001"
  append-cp-id = true
  connectors = 1

Examples:
  ocppsim --ws-url ws://csms.local/ocpp --cp-id CP-001
  ocppsim --ws-url ws://csms.local/ocpp --cp-id CP-001 --log-path ./ocpp.log
  ocppsim example
  ocppsim example --config-path ./config.toml
"#;

/// Command-line OCPP charge point simulator for OCPP-J
#[derive(Debug, Clone, Parser)]
#[command(
  name = "ocppsim",
  long_about = CLI_LONG_ABOUT,
  after_long_help = CLI_AFTER_HELP,
  version,
  long_version = crate::version_string(),
)]
pub struct CliArgs {
  /// Profile name from config file
  #[arg(value_name = "PROFILE")]
  pub profile: Option<String>,

  /// Config file path. Defaults to ~/.config/ocppsim/ocppsim.toml
  #[arg(long, value_name = "PATH")]
  pub config_path: Option<PathBuf>,

  /// CSMS WebSocket URL. Required without PROFILE
  #[arg(long, value_name = "URL")]
  pub ws_url: Option<String>,

  /// Charge point ID. Required without PROFILE
  #[arg(long, value_name = "ID")]
  pub cp_id: Option<String>,

  /// Do not append cp-id to ws-url
  #[arg(
    long,
    default_value_t = false,
    action = ArgAction::SetTrue,
  )]
  pub no_append_cp_id: bool,

  /// Connector count (must be positive)
  #[arg(long)]
  pub connectors: Option<u16>,

  /// OCPP protocol version (overrides profile value)
  #[arg(long, value_enum)]
  pub protocol: Option<ProtocolArg>,

  /// Charge point vendor string
  #[arg(long)]
  pub vendor: Option<String>,

  /// Charge point model string
  #[arg(long)]
  pub model: Option<String>,

  /// Firmware version string
  #[arg(long)]
  pub firmware: Option<String>,

  /// Append logs to file at PATH
  #[arg(long, value_name = "PATH")]
  pub log_path: Option<PathBuf>,

  /// Log raw OCPP JSON frames
  #[arg(
    long,
    default_value_t = false,
    action = ArgAction::SetTrue,
  )]
  pub trace_frames: bool,

  /// Validate inbound CALL payloads against checked-in JSON schemas
  #[arg(
    long,
    default_value_t = false,
    action = ArgAction::SetTrue,
  )]
  pub strict: bool,

  /// Request timeout in seconds
  #[arg(long)]
  pub request_timeout_seconds: Option<u64>,

  /// Startup periodic heartbeat interval in seconds (0 disables periodic
  /// heartbeats)
  #[arg(long)]
  pub heartbeat_seconds: Option<u64>,
}

/// Fully resolved runtime arguments after merging CLI and config profile data.
#[derive(Debug, Clone)]
pub struct ResolvedCliArgs {
  pub profile: Option<String>,
  pub config_path: Option<PathBuf>,
  pub ws_url: String,
  pub cp_id: String,
  pub append_cp_id: bool,
  pub connectors: u16,
  pub protocol: OcppVersion,
  pub vendor: String,
  pub model: String,
  pub firmware: String,
  pub log_path: Option<PathBuf>,
  pub trace_frames: bool,
  pub strict: bool,
  pub request_timeout_seconds: u64,
  pub heartbeat_seconds: Option<u64>,
}

impl CliArgs {
  /// Resolves CLI arguments into effective simulator settings.
  ///
  /// This method merges, in order of precedence:
  /// 1. Built-in defaults.
  /// 2. Global profile defaults from config.
  /// 3. Charge-point profile values.
  /// 4. Explicit CLI overrides.
  ///
  /// Returns an error when required direct-mode flags are missing, when
  /// profile constraints are violated, or when overrides are invalid.
  pub fn resolve(self) -> Result<ResolvedCliArgs> {
    if self.profile.is_some() && (self.ws_url.is_some() || self.cp_id.is_some())
    {
      bail!("When a profile is used, --ws-url and --cp-id must not be set.");
    }

    let mut resolved = if let Some(profile_name) = &self.profile {
      let config_path = self
        .config_path
        .clone()
        .map(|path| expand_tilde_path(&path))
        .unwrap_or_else(default_config_path);
      let defaults = ProfileDefaults {
        connectors: DEFAULT_CONNECTORS,
        protocol: DEFAULT_PROTOCOL,
        vendor: DEFAULT_VENDOR.to_string(),
        model: DEFAULT_MODEL.to_string(),
        firmware: DEFAULT_FIRMWARE.to_string(),
        request_timeout_seconds: DEFAULT_REQUEST_TIMEOUT_SECONDS,
      };
      let profile = resolve_profile(&config_path, profile_name, &defaults)?;
      ResolvedCliArgs {
        profile: Some(profile_name.clone()),
        config_path: Some(config_path),
        ws_url: profile.ws_url,
        cp_id: profile.cp_id,
        append_cp_id: profile.append_cp_id,
        connectors: profile.connectors,
        protocol: profile.protocol,
        vendor: profile.vendor,
        model: profile.model,
        firmware: profile.firmware,
        log_path: profile.log_path.map(|path| expand_tilde_path(&path)),
        trace_frames: profile.trace_frames,
        strict: profile.strict,
        request_timeout_seconds: profile.request_timeout_seconds,
        heartbeat_seconds: profile.heartbeat_seconds,
      }
    } else {
      resolve_with_direct_args(&self)?
    };

    apply_cli_overrides(&mut resolved, &self)?;
    Ok(resolved)
  }
}

/// CLI-facing protocol selector accepted by clap value parsing.
#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
pub enum ProtocolArg {
  #[value(name = "1.6")]
  V1_6,
  #[value(name = "2.0.1")]
  V2_0_1,
  #[value(name = "2.1")]
  V2_1,
}

impl ProtocolArg {
  /// Maps the CLI enum variant to the internal OCPP version enum.
  pub fn to_version(self) -> OcppVersion {
    match self {
      Self::V1_6 => OcppVersion::V1_6,
      Self::V2_0_1 => OcppVersion::V2_0_1,
      Self::V2_1 => OcppVersion::V2_1,
    }
  }
}

/// Resolves configuration in direct mode, without profile loading.
///
/// Inputs must include both `--ws-url` and `--cp-id`. The function returns
/// baseline defaults plus direct CLI protocol selection.
fn resolve_with_direct_args(cli: &CliArgs) -> Result<ResolvedCliArgs> {
  let (ws_url, cp_id) = match (&cli.ws_url, &cli.cp_id) {
    (None, None) => {
      bail!("--ws-url and --cp-id are required when no profile is used.");
    }
    (None, Some(_)) => {
      bail!("--ws-url is required when no profile is used.");
    }
    (Some(_), None) => {
      bail!("--cp-id is required when no profile is used.");
    }
    (Some(ws_url), Some(cp_id)) => (ws_url.clone(), cp_id.clone()),
  };

  Ok(ResolvedCliArgs {
    profile: None,
    config_path: None,
    ws_url,
    cp_id,
    append_cp_id: true,
    connectors: DEFAULT_CONNECTORS,
    protocol: cli
      .protocol
      .map(ProtocolArg::to_version)
      .unwrap_or(DEFAULT_PROTOCOL),
    vendor: DEFAULT_VENDOR.to_string(),
    model: DEFAULT_MODEL.to_string(),
    firmware: DEFAULT_FIRMWARE.to_string(),
    log_path: None,
    trace_frames: false,
    strict: false,
    request_timeout_seconds: DEFAULT_REQUEST_TIMEOUT_SECONDS,
    heartbeat_seconds: None,
  })
}

/// Applies optional CLI overrides on top of already-resolved settings.
///
/// This function mutates `resolved` and validates override-specific
/// constraints, such as positive connector counts.
fn apply_cli_overrides(
  resolved: &mut ResolvedCliArgs,
  cli: &CliArgs,
) -> Result<()> {
  if cli.no_append_cp_id {
    resolved.append_cp_id = false;
  }
  if let Some(connectors) = cli.connectors {
    if connectors == 0 {
      bail!("--connectors must be positive.");
    }
    resolved.connectors = connectors;
  }
  if let Some(protocol) = cli.protocol {
    resolved.protocol = protocol.to_version();
  }
  if let Some(vendor) = &cli.vendor {
    resolved.vendor = vendor.clone();
  }
  if let Some(model) = &cli.model {
    resolved.model = model.clone();
  }
  if let Some(firmware) = &cli.firmware {
    resolved.firmware = firmware.clone();
  }
  if let Some(path) = &cli.log_path {
    resolved.log_path = Some(expand_tilde_path(path));
  }
  if cli.trace_frames {
    resolved.trace_frames = true;
  }
  if cli.strict {
    resolved.strict = true;
  }
  if let Some(seconds) = cli.request_timeout_seconds {
    resolved.request_timeout_seconds = seconds;
  }
  if let Some(seconds) = cli.heartbeat_seconds {
    resolved.heartbeat_seconds = normalize_heartbeat_seconds(Some(seconds));
  }
  Ok(())
}

/// Converts heartbeat seconds into an optional interval.
///
/// A value of `0` disables periodic heartbeats and becomes `None`.
fn normalize_heartbeat_seconds(value: Option<u64>) -> Option<u64> {
  value.and_then(|seconds| if seconds == 0 { None } else { Some(seconds) })
}

/// Expands `~` and `~/...` paths against the current `HOME` directory.
///
/// If no `HOME` is present, or if the path does not start with `~`, the
/// original path is returned unchanged.
fn expand_tilde_path(path: &Path) -> PathBuf {
  let raw = path.to_string_lossy();
  if (raw == "~" || raw.starts_with("~/"))
    && let Some(home) = env::var_os("HOME")
  {
    let mut expanded = PathBuf::from(home);
    if raw.len() > 2 {
      expanded.push(raw.trim_start_matches("~/"));
    }
    return expanded;
  }

  path.to_path_buf()
}

/// Returns the default config file location for profile mode.
///
/// The path is `$HOME/.config/ocppsim/ocppsim.toml` when `HOME` exists,
/// otherwise a relative fallback of `.config/ocppsim/ocppsim.toml`.
fn default_config_path() -> PathBuf {
  if let Some(home) = env::var_os("HOME") {
    let mut path = PathBuf::from(home);
    path.push(DEFAULT_CONFIG_PATH_HINT.trim_start_matches("~/"));
    return path;
  }

  PathBuf::from(DEFAULT_CONFIG_PATH_HINT.trim_start_matches("~/"))
}

#[cfg(test)]
mod tests {
  use std::fs;
  use std::path::{Path, PathBuf};
  use std::sync::atomic::{AtomicU64, Ordering};
  use std::time::{SystemTime, UNIX_EPOCH};

  use super::{CliArgs, OcppVersion, default_config_path, expand_tilde_path};

  static TEMP_CONFIG_COUNTER: AtomicU64 = AtomicU64::new(0);

  /// Builds baseline profile-mode args used by test cases.
  fn base_profile_args() -> CliArgs {
    CliArgs {
      profile: Some("test".to_string()),
      config_path: None,
      ws_url: None,
      cp_id: None,
      no_append_cp_id: false,
      connectors: None,
      protocol: None,
      vendor: None,
      model: None,
      firmware: None,
      log_path: None,
      trace_frames: false,
      strict: false,
      request_timeout_seconds: None,
      heartbeat_seconds: None,
    }
  }

  #[test]
  /// Verifies profile mode rejects simultaneous direct connection flags.
  fn rejects_profile_with_ws_url_or_cp_id() {
    let mut args = base_profile_args();
    args.ws_url = Some("ws://example".to_string());
    assert!(args.resolve().is_err());
  }

  #[test]
  /// Verifies direct mode requires both `--ws-url` and `--cp-id`.
  fn direct_args_require_ws_url_and_cp_id() {
    let args = CliArgs {
      profile: None,
      config_path: None,
      ws_url: None,
      cp_id: None,
      no_append_cp_id: true,
      connectors: Some(2),
      protocol: Some(super::ProtocolArg::V1_6),
      vendor: Some("vendor".to_string()),
      model: Some("model".to_string()),
      firmware: Some("1.0.0".to_string()),
      log_path: None,
      trace_frames: true,
      strict: false,
      request_timeout_seconds: Some(20),
      heartbeat_seconds: Some(5),
    };
    let error = args.resolve().expect_err("resolution should fail");
    assert_eq!(
      error.to_string(),
      "--ws-url and --cp-id are required when no profile is used."
    );
  }

  #[test]
  /// Verifies direct mode fails when `--cp-id` is set without `--ws-url`.
  fn direct_args_require_ws_url_when_only_cp_id_is_set() {
    let args = CliArgs {
      profile: None,
      config_path: None,
      ws_url: None,
      cp_id: Some("CP-DEMO".to_string()),
      no_append_cp_id: false,
      connectors: None,
      protocol: None,
      vendor: None,
      model: None,
      firmware: None,
      log_path: None,
      trace_frames: false,
      strict: false,
      request_timeout_seconds: None,
      heartbeat_seconds: None,
    };
    let error = args.resolve().expect_err("resolution should fail");
    assert_eq!(
      error.to_string(),
      "--ws-url is required when no profile is used."
    );
  }

  #[test]
  /// Verifies direct mode fails when `--ws-url` is set without `--cp-id`.
  fn direct_args_require_cp_id_when_only_ws_url_is_set() {
    let args = CliArgs {
      profile: None,
      config_path: None,
      ws_url: Some("ws://localhost:9000/ocpp".to_string()),
      cp_id: None,
      no_append_cp_id: false,
      connectors: None,
      protocol: None,
      vendor: None,
      model: None,
      firmware: None,
      log_path: None,
      trace_frames: false,
      strict: false,
      request_timeout_seconds: None,
      heartbeat_seconds: None,
    };
    let error = args.resolve().expect_err("resolution should fail");
    assert_eq!(
      error.to_string(),
      "--cp-id is required when no profile is used."
    );
  }

  #[test]
  /// Verifies protocol CLI enum converts to the expected internal versions.
  fn protocol_enum_maps_to_version() {
    assert_eq!(super::ProtocolArg::V1_6.to_version(), OcppVersion::V1_6);
    assert_eq!(super::ProtocolArg::V2_0_1.to_version(), OcppVersion::V2_0_1);
    assert_eq!(super::ProtocolArg::V2_1.to_version(), OcppVersion::V2_1);
  }

  #[test]
  /// Verifies profile resolution loads required fields from a config file.
  fn resolves_profile_from_config_path() {
    let path = write_temp_config(
      r#"
[charge-points.demo]
ws-url = "wss://example.com/ocpp"
id = "CP-DEMO"
"#,
    );

    let args = CliArgs {
      profile: Some("demo".to_string()),
      config_path: Some(path.clone()),
      ws_url: None,
      cp_id: None,
      no_append_cp_id: false,
      connectors: None,
      protocol: None,
      vendor: None,
      model: None,
      firmware: None,
      log_path: None,
      trace_frames: false,
      strict: false,
      request_timeout_seconds: None,
      heartbeat_seconds: None,
    };

    let resolved = args.resolve().expect("profile should resolve");
    assert_eq!(resolved.cp_id, "CP-DEMO");
    assert_eq!(resolved.ws_url, "wss://example.com/ocpp");
    assert_eq!(resolved.protocol, OcppVersion::V1_6);
    assert_eq!(resolved.connectors, 1);
    assert_eq!(resolved.vendor, "ocppsim");
    assert_eq!(resolved.model, "ocppsim");
    assert!(resolved.append_cp_id);

    let _ = fs::remove_file(path);
  }

  #[test]
  /// Verifies profiles must include both `ws-url` and `id`.
  fn profile_requires_ws_url_and_id() {
    let path = write_temp_config(
      r#"
[charge-points.demo]
id = "CP-DEMO"
"#,
    );

    let args = CliArgs {
      profile: Some("demo".to_string()),
      config_path: Some(path.clone()),
      ws_url: None,
      cp_id: None,
      no_append_cp_id: false,
      connectors: None,
      protocol: None,
      vendor: None,
      model: None,
      firmware: None,
      log_path: None,
      trace_frames: false,
      strict: false,
      request_timeout_seconds: None,
      heartbeat_seconds: None,
    };
    assert!(args.resolve().is_err());

    let _ = fs::remove_file(path);
  }

  /// Writes temporary TOML config content and returns its path.
  fn write_temp_config(content: &str) -> PathBuf {
    let base = std::env::current_dir().expect("cwd");
    let timestamp = SystemTime::now()
      .duration_since(UNIX_EPOCH)
      .expect("time")
      .as_nanos();
    let sequence = TEMP_CONFIG_COUNTER.fetch_add(1, Ordering::Relaxed);
    let pid = std::process::id();
    let path =
      base.join(format!(".tmp-ocppsim-{pid}-{timestamp}-{sequence}.toml"));
    fs::write(&path, content).expect("write temp config");
    path
  }

  #[test]
  /// Verifies CLI `--log-path` overrides resolved log file destination.
  fn applies_log_path_override() {
    let args = CliArgs {
      profile: None,
      config_path: None,
      ws_url: Some("ws://localhost:9000/ocpp".to_string()),
      cp_id: Some("CP-TEST".to_string()),
      no_append_cp_id: false,
      connectors: None,
      protocol: None,
      vendor: None,
      model: None,
      firmware: None,
      log_path: Some(PathBuf::from("./sim.log")),
      trace_frames: false,
      strict: false,
      request_timeout_seconds: None,
      heartbeat_seconds: None,
    };

    let resolved = args.resolve().expect("arguments should resolve");
    assert_eq!(resolved.log_path, Some(PathBuf::from("./sim.log")));
  }

  #[test]
  /// Verifies profile-level `log-path` is resolved when present.
  fn resolves_profile_log_path() {
    let path = write_temp_config(
      r#"
[charge-points.demo]
ws-url = "wss://example.com/ocpp"
id = "CP-DEMO"
log-path = "./profile.log"
"#,
    );

    let args = CliArgs {
      profile: Some("demo".to_string()),
      config_path: Some(path.clone()),
      ws_url: None,
      cp_id: None,
      no_append_cp_id: false,
      connectors: None,
      protocol: None,
      vendor: None,
      model: None,
      firmware: None,
      log_path: None,
      trace_frames: false,
      strict: false,
      request_timeout_seconds: None,
      heartbeat_seconds: None,
    };

    let resolved = args.resolve().expect("profile should resolve");
    assert_eq!(resolved.log_path, Some(PathBuf::from("./profile.log")));

    let _ = fs::remove_file(path);
  }

  #[test]
  /// Verifies profile resolution inherits global defaults when omitted.
  fn resolves_global_defaults_for_profile() {
    let path = write_temp_config(
      r#"
protocol = "1.6"
vendor = "global-vendor"
model = "global-model"
firmware = "global-fw"
log-path = "./global.log"
trace-frames = true
strict = true
request-timeout-seconds = 42
heartbeat-seconds = 12

[charge-points.demo]
ws-url = "wss://example.com/ocpp"
id = "CP-DEMO"
"#,
    );

    let args = CliArgs {
      profile: Some("demo".to_string()),
      config_path: Some(path.clone()),
      ws_url: None,
      cp_id: None,
      no_append_cp_id: false,
      connectors: None,
      protocol: None,
      vendor: None,
      model: None,
      firmware: None,
      log_path: None,
      trace_frames: false,
      strict: false,
      request_timeout_seconds: None,
      heartbeat_seconds: None,
    };

    let resolved = args.resolve().expect("profile should resolve");
    assert_eq!(resolved.protocol, OcppVersion::V1_6);
    assert_eq!(resolved.vendor, "global-vendor");
    assert_eq!(resolved.model, "global-model");
    assert_eq!(resolved.firmware, "global-fw");
    assert_eq!(resolved.log_path, Some(PathBuf::from("./global.log")));
    assert!(resolved.trace_frames);
    assert!(resolved.strict);
    assert_eq!(resolved.request_timeout_seconds, 42);
    assert_eq!(resolved.heartbeat_seconds, Some(12));

    let _ = fs::remove_file(path);
  }

  #[test]
  /// Verifies charge-point config takes precedence over global defaults.
  fn profile_overrides_global_defaults() {
    let path = write_temp_config(
      r#"
protocol = "2.0.1"
vendor = "global-vendor"
model = "global-model"
firmware = "global-fw"
log-path = "./global.log"
trace-frames = true
strict = true
request-timeout-seconds = 60
heartbeat-seconds = 20

[charge-points.demo]
ws-url = "wss://example.com/ocpp"
id = "CP-DEMO"
protocol = "2.1"
vendor = "profile-vendor"
model = "profile-model"
firmware = "profile-fw"
log-path = "./profile.log"
trace-frames = false
strict = false
request-timeout-seconds = 15
heartbeat-seconds = 0
"#,
    );

    let args = CliArgs {
      profile: Some("demo".to_string()),
      config_path: Some(path.clone()),
      ws_url: None,
      cp_id: None,
      no_append_cp_id: false,
      connectors: None,
      protocol: None,
      vendor: None,
      model: None,
      firmware: None,
      log_path: None,
      trace_frames: false,
      strict: false,
      request_timeout_seconds: None,
      heartbeat_seconds: None,
    };

    let resolved = args.resolve().expect("profile should resolve");
    assert_eq!(resolved.protocol, OcppVersion::V2_1);
    assert_eq!(resolved.vendor, "profile-vendor");
    assert_eq!(resolved.model, "profile-model");
    assert_eq!(resolved.firmware, "profile-fw");
    assert_eq!(resolved.log_path, Some(PathBuf::from("./profile.log")));
    assert!(!resolved.trace_frames);
    assert!(!resolved.strict);
    assert_eq!(resolved.request_timeout_seconds, 15);
    assert_eq!(resolved.heartbeat_seconds, None);

    let _ = fs::remove_file(path);
  }

  #[test]
  /// Verifies CLI flags override both profile and global config values.
  fn cli_overrides_profile_and_global_values() {
    let path = write_temp_config(
      r#"
protocol = "1.6"
vendor = "global-vendor"
model = "global-model"
firmware = "global-fw"
log-path = "./global.log"
trace-frames = false
request-timeout-seconds = 60
heartbeat-seconds = 20

[charge-points.demo]
ws-url = "wss://example.com/ocpp"
id = "CP-DEMO"
protocol = "2.0.1"
vendor = "profile-vendor"
model = "profile-model"
firmware = "profile-fw"
log-path = "./profile.log"
trace-frames = false
request-timeout-seconds = 15
heartbeat-seconds = 12
"#,
    );

    let args = CliArgs {
      profile: Some("demo".to_string()),
      config_path: Some(path.clone()),
      ws_url: None,
      cp_id: None,
      no_append_cp_id: false,
      connectors: None,
      protocol: Some(super::ProtocolArg::V2_1),
      vendor: Some("cli-vendor".to_string()),
      model: Some("cli-model".to_string()),
      firmware: Some("cli-fw".to_string()),
      log_path: Some(PathBuf::from("./cli.log")),
      trace_frames: true,
      strict: true,
      request_timeout_seconds: Some(99),
      heartbeat_seconds: Some(0),
    };

    let resolved = args.resolve().expect("profile should resolve");
    assert_eq!(resolved.protocol, OcppVersion::V2_1);
    assert_eq!(resolved.vendor, "cli-vendor");
    assert_eq!(resolved.model, "cli-model");
    assert_eq!(resolved.firmware, "cli-fw");
    assert_eq!(resolved.log_path, Some(PathBuf::from("./cli.log")));
    assert!(resolved.trace_frames);
    assert!(resolved.strict);
    assert_eq!(resolved.request_timeout_seconds, 99);
    assert_eq!(resolved.heartbeat_seconds, None);

    let _ = fs::remove_file(path);
  }

  #[test]
  /// Verifies global heartbeat value `0` disables startup heartbeats.
  fn global_heartbeat_zero_disables_periodic_heartbeat() {
    let path = write_temp_config(
      r#"
heartbeat-seconds = 0

[charge-points.demo]
ws-url = "wss://example.com/ocpp"
id = "CP-DEMO"
"#,
    );

    let args = CliArgs {
      profile: Some("demo".to_string()),
      config_path: Some(path.clone()),
      ws_url: None,
      cp_id: None,
      no_append_cp_id: false,
      connectors: None,
      protocol: None,
      vendor: None,
      model: None,
      firmware: None,
      log_path: None,
      trace_frames: false,
      strict: false,
      request_timeout_seconds: None,
      heartbeat_seconds: None,
    };

    let resolved = args.resolve().expect("profile should resolve");
    assert_eq!(resolved.heartbeat_seconds, None);

    let _ = fs::remove_file(path);
  }

  #[test]
  /// Verifies CLI heartbeat value `0` disables startup heartbeats.
  fn cli_heartbeat_zero_disables_periodic_heartbeat() {
    let args = CliArgs {
      profile: None,
      config_path: None,
      ws_url: Some("ws://localhost:9000/ocpp".to_string()),
      cp_id: Some("CP-TEST".to_string()),
      no_append_cp_id: false,
      connectors: None,
      protocol: None,
      vendor: None,
      model: None,
      firmware: None,
      log_path: None,
      trace_frames: false,
      strict: false,
      request_timeout_seconds: None,
      heartbeat_seconds: Some(0),
    };

    let resolved = args.resolve().expect("arguments should resolve");
    assert_eq!(resolved.heartbeat_seconds, None);
  }

  #[test]
  /// Verifies tilde expansion for CLI paths and helper behavior.
  fn expands_tilde_in_cli_paths() {
    let args = CliArgs {
      profile: None,
      config_path: None,
      ws_url: Some("ws://localhost:9000/ocpp".to_string()),
      cp_id: Some("CP-TEST".to_string()),
      no_append_cp_id: false,
      connectors: None,
      protocol: None,
      vendor: None,
      model: None,
      firmware: None,
      log_path: Some(PathBuf::from("~/sim.log")),
      trace_frames: false,
      strict: false,
      request_timeout_seconds: None,
      heartbeat_seconds: None,
    };

    let resolved = args.resolve().expect("arguments should resolve");
    assert_eq!(
      resolved.log_path,
      Some(expand_tilde_path(Path::new("~/sim.log")))
    );
    assert_eq!(
      expand_tilde_path(Path::new("~/.config/ocppsim/config.toml")),
      default_config_path().with_file_name("config.toml")
    );
  }

  #[test]
  /// Verifies that a nonexistent config file path produces an error.
  fn missing_config_file_produces_error() {
    let args = CliArgs {
      profile: Some("demo".to_string()),
      config_path: Some(PathBuf::from("/nonexistent/path/ocppsim.toml")),
      ws_url: None,
      cp_id: None,
      no_append_cp_id: false,
      connectors: None,
      protocol: None,
      vendor: None,
      model: None,
      firmware: None,
      log_path: None,
      trace_frames: false,
      strict: false,
      request_timeout_seconds: None,
      heartbeat_seconds: None,
    };
    let error = args.resolve().expect_err("should fail");
    assert!(
      error.to_string().contains("Failed to read"),
      "unexpected error: {error}"
    );
  }

  #[test]
  /// Verifies that referencing a nonexistent profile produces an error.
  fn nonexistent_profile_produces_error() {
    let path = write_temp_config(
      r#"
[charge-points.demo]
ws-url = "wss://example.com/ocpp"
id = "CP-DEMO"
"#,
    );

    let args = CliArgs {
      profile: Some("no-such-profile".to_string()),
      config_path: Some(path.clone()),
      ws_url: None,
      cp_id: None,
      no_append_cp_id: false,
      connectors: None,
      protocol: None,
      vendor: None,
      model: None,
      firmware: None,
      log_path: None,
      trace_frames: false,
      strict: false,
      request_timeout_seconds: None,
      heartbeat_seconds: None,
    };
    let error = args.resolve().expect_err("should fail");
    assert!(
      error.to_string().contains("was not found"),
      "unexpected error: {error}"
    );

    let _ = fs::remove_file(path);
  }

  #[test]
  /// Verifies that an invalid protocol string in a profile rejects.
  fn invalid_protocol_in_profile_rejects() {
    let path = write_temp_config(
      r#"
[charge-points.demo]
ws-url = "wss://example.com/ocpp"
id = "CP-DEMO"
protocol = "3.0"
"#,
    );

    let args = CliArgs {
      profile: Some("demo".to_string()),
      config_path: Some(path.clone()),
      ws_url: None,
      cp_id: None,
      no_append_cp_id: false,
      connectors: None,
      protocol: None,
      vendor: None,
      model: None,
      firmware: None,
      log_path: None,
      trace_frames: false,
      strict: false,
      request_timeout_seconds: None,
      heartbeat_seconds: None,
    };
    let error = args.resolve().expect_err("should fail");
    assert!(
      error.to_string().contains("protocol"),
      "unexpected error: {error}"
    );

    let _ = fs::remove_file(path);
  }

  #[test]
  /// Verifies that an invalid protocol in global config rejects.
  fn invalid_global_protocol_rejects() {
    let path = write_temp_config(
      r#"
protocol = "invalid"

[charge-points.demo]
ws-url = "wss://example.com/ocpp"
id = "CP-DEMO"
"#,
    );

    let args = CliArgs {
      profile: Some("demo".to_string()),
      config_path: Some(path.clone()),
      ws_url: None,
      cp_id: None,
      no_append_cp_id: false,
      connectors: None,
      protocol: None,
      vendor: None,
      model: None,
      firmware: None,
      log_path: None,
      trace_frames: false,
      strict: false,
      request_timeout_seconds: None,
      heartbeat_seconds: None,
    };
    let error = args.resolve().expect_err("should fail");
    assert!(
      error.to_string().contains("protocol"),
      "unexpected error: {error}"
    );

    let _ = fs::remove_file(path);
  }

  #[test]
  /// Verifies that `connectors = 0` in a profile is rejected.
  fn profile_connectors_zero_rejects() {
    let path = write_temp_config(
      r#"
[charge-points.demo]
ws-url = "wss://example.com/ocpp"
id = "CP-DEMO"
connectors = 0
"#,
    );

    let args = CliArgs {
      profile: Some("demo".to_string()),
      config_path: Some(path.clone()),
      ws_url: None,
      cp_id: None,
      no_append_cp_id: false,
      connectors: None,
      protocol: None,
      vendor: None,
      model: None,
      firmware: None,
      log_path: None,
      trace_frames: false,
      strict: false,
      request_timeout_seconds: None,
      heartbeat_seconds: None,
    };
    let error = args.resolve().expect_err("should fail");
    assert!(
      error.to_string().contains("connectors"),
      "unexpected error: {error}"
    );

    let _ = fs::remove_file(path);
  }

  #[test]
  /// Verifies that `--connectors 0` on the CLI is rejected.
  fn cli_connectors_zero_rejects() {
    let args = CliArgs {
      profile: None,
      config_path: None,
      ws_url: Some("ws://localhost:9000/ocpp".to_string()),
      cp_id: Some("CP-TEST".to_string()),
      no_append_cp_id: false,
      connectors: Some(0),
      protocol: None,
      vendor: None,
      model: None,
      firmware: None,
      log_path: None,
      trace_frames: false,
      strict: false,
      request_timeout_seconds: None,
      heartbeat_seconds: None,
    };
    let error = args.resolve().expect_err("should fail");
    assert!(
      error.to_string().contains("--connectors must be positive"),
      "unexpected error: {error}"
    );
  }

  #[test]
  /// Verifies that `append-cp-id = false` in profile disables appending.
  fn profile_append_cp_id_false() {
    let path = write_temp_config(
      r#"
[charge-points.demo]
ws-url = "wss://example.com/ocpp"
id = "CP-DEMO"
append-cp-id = false
"#,
    );

    let args = CliArgs {
      profile: Some("demo".to_string()),
      config_path: Some(path.clone()),
      ws_url: None,
      cp_id: None,
      no_append_cp_id: false,
      connectors: None,
      protocol: None,
      vendor: None,
      model: None,
      firmware: None,
      log_path: None,
      trace_frames: false,
      strict: false,
      request_timeout_seconds: None,
      heartbeat_seconds: None,
    };

    let resolved = args.resolve().expect("profile should resolve");
    assert!(!resolved.append_cp_id);

    let _ = fs::remove_file(path);
  }

  #[test]
  /// Verifies `--no-append-cp-id` overrides profile `append-cp-id = true`.
  fn cli_no_append_cp_id_overrides_profile() {
    let path = write_temp_config(
      r#"
[charge-points.demo]
ws-url = "wss://example.com/ocpp"
id = "CP-DEMO"
append-cp-id = true
"#,
    );

    let args = CliArgs {
      profile: Some("demo".to_string()),
      config_path: Some(path.clone()),
      ws_url: None,
      cp_id: None,
      no_append_cp_id: true,
      connectors: None,
      protocol: None,
      vendor: None,
      model: None,
      firmware: None,
      log_path: None,
      trace_frames: false,
      strict: false,
      request_timeout_seconds: None,
      heartbeat_seconds: None,
    };

    let resolved = args.resolve().expect("profile should resolve");
    assert!(!resolved.append_cp_id);

    let _ = fs::remove_file(path);
  }

  #[test]
  /// Verifies direct mode applies expected default values correctly.
  fn direct_mode_applies_defaults() {
    let args = CliArgs {
      profile: None,
      config_path: None,
      ws_url: Some("ws://localhost:9000/ocpp".to_string()),
      cp_id: Some("CP-TEST".to_string()),
      no_append_cp_id: false,
      connectors: None,
      protocol: None,
      vendor: None,
      model: None,
      firmware: None,
      log_path: None,
      trace_frames: false,
      strict: false,
      request_timeout_seconds: None,
      heartbeat_seconds: None,
    };

    let resolved = args.resolve().expect("arguments should resolve");
    assert_eq!(resolved.ws_url, "ws://localhost:9000/ocpp");
    assert_eq!(resolved.cp_id, "CP-TEST");
    assert!(resolved.append_cp_id);
    assert_eq!(resolved.connectors, 1);
    assert_eq!(resolved.protocol, OcppVersion::V1_6);
    assert_eq!(resolved.vendor, "ocppsim");
    assert_eq!(resolved.model, "ocppsim");
    assert_eq!(resolved.log_path, None);
    assert!(!resolved.trace_frames);
    assert_eq!(resolved.request_timeout_seconds, 30);
    assert_eq!(resolved.heartbeat_seconds, None);
  }

  #[test]
  /// Verifies that profile connectors setting is respected.
  fn profile_connectors_override() {
    let path = write_temp_config(
      r#"
[charge-points.demo]
ws-url = "wss://example.com/ocpp"
id = "CP-DEMO"
connectors = 3
"#,
    );

    let args = CliArgs {
      profile: Some("demo".to_string()),
      config_path: Some(path.clone()),
      ws_url: None,
      cp_id: None,
      no_append_cp_id: false,
      connectors: None,
      protocol: None,
      vendor: None,
      model: None,
      firmware: None,
      log_path: None,
      trace_frames: false,
      strict: false,
      request_timeout_seconds: None,
      heartbeat_seconds: None,
    };

    let resolved = args.resolve().expect("profile should resolve");
    assert_eq!(resolved.connectors, 3);

    let _ = fs::remove_file(path);
  }

  #[test]
  /// Verifies CLI `--connectors` overrides profile value.
  fn cli_connectors_overrides_profile() {
    let path = write_temp_config(
      r#"
[charge-points.demo]
ws-url = "wss://example.com/ocpp"
id = "CP-DEMO"
connectors = 3
"#,
    );

    let args = CliArgs {
      profile: Some("demo".to_string()),
      config_path: Some(path.clone()),
      ws_url: None,
      cp_id: None,
      no_append_cp_id: false,
      connectors: Some(5),
      protocol: None,
      vendor: None,
      model: None,
      firmware: None,
      log_path: None,
      trace_frames: false,
      strict: false,
      request_timeout_seconds: None,
      heartbeat_seconds: None,
    };

    let resolved = args.resolve().expect("profile should resolve");
    assert_eq!(resolved.connectors, 5);

    let _ = fs::remove_file(path);
  }

  #[test]
  /// Verifies malformed TOML config file produces a parse error.
  fn malformed_toml_produces_error() {
    let path = write_temp_config("this is not valid toml [[[");

    let args = CliArgs {
      profile: Some("demo".to_string()),
      config_path: Some(path.clone()),
      ws_url: None,
      cp_id: None,
      no_append_cp_id: false,
      connectors: None,
      protocol: None,
      vendor: None,
      model: None,
      firmware: None,
      log_path: None,
      trace_frames: false,
      strict: false,
      request_timeout_seconds: None,
      heartbeat_seconds: None,
    };
    let error = args.resolve().expect_err("should fail");
    assert!(
      error.to_string().contains("Failed to parse"),
      "unexpected error: {error}"
    );

    let _ = fs::remove_file(path);
  }

  #[test]
  /// Verifies that expand_tilde_path leaves non-tilde paths unchanged.
  fn expand_tilde_path_leaves_absolute_unchanged() {
    let path = Path::new("/usr/local/bin/ocppsim");
    assert_eq!(expand_tilde_path(path), PathBuf::from(path));
  }

  #[test]
  /// Verifies that expand_tilde_path leaves relative paths unchanged.
  fn expand_tilde_path_leaves_relative_unchanged() {
    let path = Path::new("relative/path.toml");
    assert_eq!(expand_tilde_path(path), PathBuf::from(path));
  }
}