manta-cli 1.64.3

Another CLI for ALPS
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
//! Phase 6 — Happy-path HTTP integration tests.
//!
//! Each test spins up a real wiremock server, builds an Axum router whose
//! backend points at it, and drives requests through
//! `tower::ServiceExt::oneshot` without opening a TCP listener.
//!
//! Covered endpoints:
//!   GET /api/v1/groups
//!   GET /api/v1/configurations
//!   GET /api/v1/sessions
//!   GET /api/v1/templates
//!   GET /api/v1/images
//!   GET /api/v1/boot-parameters

use std::sync::Arc;
use std::time::Duration;

use axum::{
  body::Body,
  http::{Method, Request, StatusCode, header},
  response::Response,
};
use http_body_util::BodyExt as _;
use serde_json::{Value, json};
use tower::ServiceExt as _;
use wiremock::{
  Mock, MockServer, ResponseTemplate,
  matchers::{method, path},
};

use crate::{
  manta_backend_dispatcher::StaticBackendDispatcher,
  server::{ServerState, routes::build_router},
};

// ---------------------------------------------------------------------------
// Embedded credentials (CI-safe — no file-system dependency)
//
// TEST_ROOT_CERT: the real ALPS Platform CA certificate.
//   Satisfies reqwest::Certificate::from_pem(), which is called lazily at
//   first backend HTTP call. The wiremock server speaks plain HTTP so TLS
//   is never negotiated and this cert is never verified.
//
// TEST_TOKEN: a real Keycloak JWT for the ALPS system.
//   csm-rs reads only realm_access.roles (no sig/expiry validation), so
//   this token works permanently regardless of its exp date.
//   The payload contains ["pa_admin", ...] which takes the admin path in
//   get_group_name_available, returning all groups without per-group
//   filtering.
// ---------------------------------------------------------------------------

const TEST_ROOT_CERT: &[u8] = b"-----BEGIN CERTIFICATE-----\n\
MIIEuDCCAyCgAwIBAgIUUuuQOz5Bu78gF1uz8lsDCWDRR4cwDQYJKoZIhvcNAQEL\n\
BQAwYTEPMA0GA1UECgwGU2hhc3RhMREwDwYDVQQLDAhQbGF0Zm9ybTE7MDkGA1UE\n\
AwwyUGxhdGZvcm0gQ0EgKGE2NjkzY2ExLTNmNWMtNDY1Zi04ZTAxLWQ4MDFkNGEw\n\
OWE0ZikwHhcNMjIwNTMxMDcxMTI1WhcNMzIwNTI4MDcxMTI1WjBmMQ8wDQYDVQQK\n\
DAZTaGFzdGExETAPBgNVBAsMCFBsYXRmb3JtMUAwPgYDVQQDDDdQbGF0Zm9ybSBD\n\
QSAtIEwxIChhNjY5M2NhMS0zZjVjLTQ2NWYtOGUwMS1kODAxZDRhMDlhNGYpMIIB\n\
ojANBgkqhkiG9w0BAQEFAAOCAY8AMIIBigKCAYEAriBXAeZVnRvUtNAe0V8BUqbn\n\
Ij6gQ8mgBP7c9BLbz3N4ALDswzHyQVIAuKJ7D3VsHVRjkKWqzOVAiP14sLJ8ko/o\n\
Fqc3HyS4L7PC6y9BY3eH2XJ3oKc6EmmlUTGEf4ZdZIvg59Tr9aYSIAqvlS/FtNCy\n\
Ch6jkCltLtHpXlSDEjuWMK8YQZCj2V0cvGoZuW4GhiNWU1amwKvNnsJIRt9uKd2O\n\
Y1GYwV9QcJOjpvWtKerNz00QJ0DNJdCBSJcp2X6sa+uEpJUK7SMM59ZKmrAVdFYo\n\
ROWnlJplEexOULCpbUwqQHsVe0ybOaI/P+Gsa9VB8qn3K+CBF+CKVqh9g2dG3AEC\n\
O04CANmQj4dmeqUFLHzMKOZ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lIjoiU29wZW5hIiwiZW1haWwiOiJtYW51ZWwuc29wZW5hQGNzY3MuY2gifQ.n4TrFTbL0XZuwIS69uLqbtqDJalWa_6UXek1mcJkKe1rQZBw3tnpek7yVIVJf5yFvlMi7SeQpsQorzXqFvwc0YAYODJNvAibuTZVVIrxbUdWfH9QR92xhp3BLkwMQtLT_4VK_1vXo8rsmtvDYKvQOqhO6PCqD0s1h4gBXQRssRG5Doo451KNTkGSoiZRATMo7KlQIGGLAXv7M2avrXhxOuE_ERlFMDWaXfL2aPjdO-e3xF0ZoFUte-L0r91QQUyRauQ3Ce_Abo5k1RYB744zdCQDDvP9qZL6SgkXEcXfF5GMCqNpA4aM7rVhl6hK_Jin13HCgi2pB3RU06J3y3zNBQ";

// ---------------------------------------------------------------------------
// TestFixture
// ---------------------------------------------------------------------------

struct TestFixture {
  mock_server: MockServer,
  router: axum::Router,
}

impl TestFixture {
  async fn setup() -> Self {
    let mock_server = MockServer::start().await;

    let backend = StaticBackendDispatcher::new(
      "csm",
      &mock_server.uri(),
      TEST_ROOT_CERT,
      None,
    )
    .unwrap();

    let state = Arc::new(ServerState {
      backend,
      site_name: "test".to_string(),
      shasta_base_url: mock_server.uri(),
      shasta_root_cert: TEST_ROOT_CERT.to_vec(),
      socks5_proxy: None,
      vault_base_url: None,
      gitea_base_url: "http://stub.invalid".to_string(),
      k8s_api_url: None,
      console_inactivity_timeout: Duration::from_secs(1800),
    });

    let router = build_router(state);

    TestFixture { mock_server, router }
  }

  fn auth_get(&self, uri: &str) -> Request<Body> {
    Request::builder()
      .method(Method::GET)
      .uri(uri)
      .header(header::AUTHORIZATION, format!("Bearer {TEST_TOKEN}"))
      .body(Body::empty())
      .unwrap()
  }

  async fn send(&self, req: Request<Body>) -> Response {
    self.router.clone().oneshot(req).await.unwrap()
  }

  async fn body_json(resp: Response) -> Value {
    let bytes = resp.into_body().collect().await.unwrap().to_bytes();
    serde_json::from_slice(&bytes).expect("response body is not valid JSON")
  }

  fn auth_post_json(&self, uri: &str, body: serde_json::Value) -> Request<Body> {
    Request::builder()
      .method(Method::POST)
      .uri(uri)
      .header(header::AUTHORIZATION, format!("Bearer {TEST_TOKEN}"))
      .header(header::CONTENT_TYPE, "application/json")
      .body(Body::from(serde_json::to_string(&body).unwrap()))
      .unwrap()
  }

  fn auth_delete(&self, uri: &str) -> Request<Body> {
    Request::builder()
      .method(Method::DELETE)
      .uri(uri)
      .header(header::AUTHORIZATION, format!("Bearer {TEST_TOKEN}"))
      .body(Body::empty())
      .unwrap()
  }

  fn auth_delete_json(&self, uri: &str, body: serde_json::Value) -> Request<Body> {
    Request::builder()
      .method(Method::DELETE)
      .uri(uri)
      .header(header::AUTHORIZATION, format!("Bearer {TEST_TOKEN}"))
      .header(header::CONTENT_TYPE, "application/json")
      .body(Body::from(serde_json::to_string(&body).unwrap()))
      .unwrap()
  }
}

// ---------------------------------------------------------------------------
// Wiremock stub helpers
//
// wiremock::matchers::path() matches the path component only — query strings
// are ignored. A single stub for /smd/hsm/v2/groups therefore handles both
// the admin "get all" call (no query params) and any filtered call
// (?group=compute).
//
// Version notes:
//   - csm-rs uses CFS v2 for both sessions AND configurations inside
//     get_and_filter_sessions / get_and_filter_configuration. Only
//     get_images_and_details also calls /cfs/v2/sessions internally.
//   - BOS templates and BSS boot parameters are always v2.
//   - CFS v2 responses are flat JSON arrays (no pagination envelope).
// ---------------------------------------------------------------------------

async fn mock_hsm_groups(srv: &MockServer) {
  Mock::given(method("GET"))
    .and(path("/smd/hsm/v2/groups"))
    .respond_with(ResponseTemplate::new(200).set_body_json(json!([
      {
        "label": "compute",
        "members": { "ids": ["x3000c0s1b0n0"] }
      }
    ])))
    .mount(srv)
    .await;
}

// CFS v2 configurations — flat array, camelCase field names.
// Config name contains "compute" so it passes the name-contains-hsm-group
// filter inside cfs::configuration::utils::filter.
async fn mock_cfs_v2_configurations(srv: &MockServer) {
  Mock::given(method("GET"))
    .and(path("/cfs/v2/configurations"))
    .respond_with(ResponseTemplate::new(200).set_body_json(json!([
      {
        "name": "compute-config",
        "lastUpdated": "2024-01-01T00:00:00",
        "layers": [
          {
            "name": "layer1",
            "cloneUrl": "https://vcs.example.com/vcs/cray/cfg.git",
            "playbook": "site.yml"
          }
        ]
      }
    ])))
    .mount(srv)
    .await;
}

// CFS v2 sessions — flat array.
// target.groups[0].name = "compute" so the session survives
// cfs::session::utils::filter (which checks target HSM membership).
// Required for get_sessions (filter errors on empty result) and
// used as a side input by get_configurations and get_images.
async fn mock_cfs_v2_sessions(srv: &MockServer) {
  Mock::given(method("GET"))
    .and(path("/cfs/v2/sessions"))
    .respond_with(ResponseTemplate::new(200).set_body_json(json!([
      {
        "name": "my-session",
        "debug_on_failure": false,
        "target": {
          "definition": "dynamic",
          "groups": [{ "name": "compute", "members": [] }],
          "image_map": []
        }
      }
    ])))
    .mount(srv)
    .await;
}

async fn mock_bos_v2_templates(srv: &MockServer) {
  Mock::given(method("GET"))
    .and(path("/bos/v2/sessiontemplates"))
    .respond_with(ResponseTemplate::new(200).set_body_json(json!([
      {
        "name": "my-template",
        "enable_cfs": true,
        "boot_sets": {
          "compute": {
            "node_groups": ["compute"],
            "path": "s3://boot-images/abc123/manifest.json"
          }
        }
      }
    ])))
    .mount(srv)
    .await;
}

// CFS v2 components — flat array; empty is valid since it only contributes
// desired_config names to the configuration filter.
async fn mock_cfs_v2_components(srv: &MockServer) {
  Mock::given(method("GET"))
    .and(path("/cfs/v2/components"))
    .respond_with(ResponseTemplate::new(200).set_body_json(json!([])))
    .mount(srv)
    .await;
}

async fn mock_bss_bootparameters(srv: &MockServer) {
  Mock::given(method("GET"))
    .and(path("/bss/boot/v1/bootparameters"))
    .respond_with(ResponseTemplate::new(200).set_body_json(json!([
      {
        "hosts": ["x3000c0s1b0n0"],
        "params": "quiet",
        "kernel": "s3://boot-images/abc123/kernel",
        "initrd": "s3://boot-images/abc123/initrd"
      }
    ])))
    .mount(srv)
    .await;
}

async fn mock_ims_images(srv: &MockServer) {
  Mock::given(method("GET"))
    .and(path("/ims/v3/images"))
    .respond_with(ResponseTemplate::new(200).set_body_json(json!([
      {
        "id": "abc123",
        "name": "compute-my-image",
        "created": "2024-01-01T00:00:00"
      }
    ])))
    .mount(srv)
    .await;
}

// Used by get_boot_parameters via resolve_hosts_expression →
// get_node_metadata_available → get_all_nodes.
// Component ID must match the HSM group member so the xname passes
// the availability filter inside get_node_metadata_available.
async fn mock_hsm_components(srv: &MockServer) {
  Mock::given(method("GET"))
    .and(path("/smd/hsm/v2/State/Components"))
    .respond_with(ResponseTemplate::new(200).set_body_json(json!({
      "Components": [
        {
          "ID": "x3000c0s1b0n0",
          "Type": "Node",
          "State": "Ready",
          "Enabled": true,
          "Role": "Compute",
          "NID": 1,
          "NetType": "Sling",
          "Arch": "X86",
          "Class": "Mountain"
        }
      ]
    })))
    .mount(srv)
    .await;
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

// GET /api/v1/groups
//
// Call chain:
//   service::group::get_groups
//     → get_groups_names_available (pa_admin → GET /smd/hsm/v2/groups)
//     → backend.get_groups          (GET /smd/hsm/v2/groups?group=compute)
#[tokio::test]
async fn get_groups_happy_path() {
  let fx = TestFixture::setup().await;
  mock_hsm_groups(&fx.mock_server).await;

  let resp = fx.send(fx.auth_get("/api/v1/groups")).await;
  assert_eq!(resp.status(), StatusCode::OK);

  let body = TestFixture::body_json(resp).await;
  let arr = body.as_array().expect("expected JSON array");
  assert_eq!(arr.len(), 1);
  assert_eq!(arr[0]["label"], "compute");
  assert_eq!(arr[0]["members"]["ids"][0], "x3000c0s1b0n0");
}

// GET /api/v1/configurations
//
// Call chain (csm-rs uses CFS v2 for configurations):
//   service::configuration::get_configurations
//     → get_groups_names_available           (GET /smd/hsm/v2/groups)
//     → backend.get_and_filter_configuration
//         → get_member_vec_from_hsm_name_vec  (GET /smd/hsm/v2/groups?group=compute)
//         → try_join!:
//             GET /cfs/v2/configurations
//             GET /cfs/v2/sessions
//             GET /bos/v2/sessiontemplates
//             GET /cfs/v2/components
//
// The configuration name must contain the HSM group name ("compute") to
// survive cfs::configuration::utils::filter.
// CFS v2 config response is a flat array with camelCase field names.
#[tokio::test]
async fn get_configurations_happy_path() {
  let fx = TestFixture::setup().await;
  mock_hsm_groups(&fx.mock_server).await;
  mock_cfs_v2_configurations(&fx.mock_server).await;
  mock_cfs_v2_sessions(&fx.mock_server).await;
  mock_bos_v2_templates(&fx.mock_server).await;
  mock_cfs_v2_components(&fx.mock_server).await;

  let resp = fx.send(fx.auth_get("/api/v1/configurations")).await;
  assert_eq!(resp.status(), StatusCode::OK);

  let body = TestFixture::body_json(resp).await;
  let arr = body.as_array().expect("expected JSON array");
  assert_eq!(arr.len(), 1);
  assert_eq!(arr[0]["name"], "compute-config");
  assert!(arr[0]["layers"].is_array());
}

// GET /api/v1/sessions
//
// Call chain (csm-rs uses CFS v2 for sessions):
//   service::session::get_sessions
//     → backend.get_and_filter_sessions(hsm_group_name_vec=[], xname_vec=[])
//         → hsm::group::utils::get_group_available (GET /smd/hsm/v2/groups)
//         → cfs::session::get_and_sort             (GET /cfs/v2/sessions)
//         → cfs::session::utils::filter
//
// The session mock includes target.groups[0].name = "compute" so it
// survives the filter (which retains only sessions targeting available
// groups). An empty result after filtering returns Err("No CFS session found").
// Neither process::exit branch fires because both filter vecs are empty.
#[tokio::test]
async fn get_sessions_happy_path() {
  let fx = TestFixture::setup().await;
  mock_hsm_groups(&fx.mock_server).await;
  mock_cfs_v2_sessions(&fx.mock_server).await;

  let resp = fx.send(fx.auth_get("/api/v1/sessions")).await;
  assert_eq!(resp.status(), StatusCode::OK);

  let body = TestFixture::body_json(resp).await;
  let arr = body.as_array().expect("expected JSON array");
  assert_eq!(arr.len(), 1);
  assert_eq!(arr[0]["name"], "my-session");
}

// GET /api/v1/templates
//
// Call chain:
//   service::template::get_templates
//     → get_groups_names_available (GET /smd/hsm/v2/groups)
//     → backend.get_member_vec_from_group_name_vec
//         → GET /smd/hsm/v2/groups?group=compute  (same stub)
//     → backend.get_and_filter_templates
//         → GET /bos/v2/sessiontemplates
#[tokio::test]
async fn get_templates_happy_path() {
  let fx = TestFixture::setup().await;
  mock_hsm_groups(&fx.mock_server).await;
  mock_bos_v2_templates(&fx.mock_server).await;

  let resp = fx.send(fx.auth_get("/api/v1/templates")).await;
  assert_eq!(resp.status(), StatusCode::OK);

  let body = TestFixture::body_json(resp).await;
  let arr = body.as_array().expect("expected JSON array");
  assert!(!arr.is_empty());
  assert_eq!(arr[0]["name"], "my-template");
}

// GET /api/v1/images
//
// Handler returns Vec<ImageEntry> — a mapped struct (not raw Image):
//   { image: Value, configuration_name: String, image_id: String, is_linked: bool }
//
// Call chain (all internal CFS calls also use v2):
//   service::image::get_images
//     → get_groups_names_available (GET /smd/hsm/v2/groups)
//     → backend.get_images_and_details
//         → GET /ims/v3/images
//         → get_member_vec_from_hsm_name_vec (GET /smd/hsm/v2/groups?group=compute, x2)
//         → GET /bos/v2/sessiontemplates
//         → cfs::session::get_and_sort       (GET /cfs/v2/sessions, await? — must succeed)
//         → GET /bss/boot/v1/bootparameters  (unwrap_or_default — safe to leave empty)
#[tokio::test]
async fn get_images_happy_path() {
  let fx = TestFixture::setup().await;
  mock_hsm_groups(&fx.mock_server).await;
  mock_ims_images(&fx.mock_server).await;
  mock_bos_v2_templates(&fx.mock_server).await;
  mock_cfs_v2_sessions(&fx.mock_server).await;
  mock_bss_bootparameters(&fx.mock_server).await;

  let resp = fx.send(fx.auth_get("/api/v1/images")).await;
  assert_eq!(resp.status(), StatusCode::OK);

  let body = TestFixture::body_json(resp).await;
  let arr = body.as_array().expect("expected JSON array");
  assert!(!arr.is_empty());
  assert!(arr[0]["image_id"].is_string());
  assert!(arr[0]["configuration_name"].is_string());
  assert!(arr[0]["is_linked"].is_boolean());
}

// GET /api/v1/boot-parameters?hsm_group=compute
//
// Call chain:
//   service::boot_parameters::get_boot_parameters
//     → common::node_ops::resolve_target_nodes(hsm_group=Some("compute"))
//         → get_groups_names_available            (GET /smd/hsm/v2/groups)
//         → backend.get_member_vec_from_group_name_vec
//                                                 (GET /smd/hsm/v2/groups?group=compute)
//         → resolve_hosts_expression("x3000c0s1b0n0")
//             → backend.get_node_metadata_available
//                 → backend.get_group_available    (GET /smd/hsm/v2/groups)
//                 → backend.get_all_nodes          (GET /smd/hsm/v2/State/Components)
//     → backend.get_bootparameters(xnames)        (GET /bss/boot/v1/bootparameters)
//
// The Component ID must match "x3000c0s1b0n0" so the xname survives
// the availability filter inside get_node_metadata_available.
#[tokio::test]
async fn get_boot_parameters_happy_path() {
  let fx = TestFixture::setup().await;
  mock_hsm_groups(&fx.mock_server).await;
  mock_hsm_components(&fx.mock_server).await;
  mock_bss_bootparameters(&fx.mock_server).await;

  let resp = fx
    .send(fx.auth_get("/api/v1/boot-parameters?hsm_group=compute"))
    .await;
  assert_eq!(resp.status(), StatusCode::OK);

  let body = TestFixture::body_json(resp).await;
  let arr = body.as_array().expect("expected JSON array");
  assert_eq!(arr.len(), 1);
  assert_eq!(arr[0]["hosts"][0], "x3000c0s1b0n0");
  assert_eq!(arr[0]["kernel"], "s3://boot-images/abc123/kernel");
}

// ---------------------------------------------------------------------------
// Additional stub helpers
// ---------------------------------------------------------------------------

async fn mock_redfish_endpoints(srv: &MockServer) {
  Mock::given(method("GET"))
    .and(path("/smd/hsm/v2/Inventory/RedfishEndpoints"))
    .respond_with(ResponseTemplate::new(200).set_body_json(json!({
      "RedfishEndpoints": [
        {
          "ID": "x3000c0s1b0",
          "Type": "NodeBMC",
          "Hostname": "x3000c0s1b0",
          "Domain": "",
          "FQDN": "x3000c0s1b0",
          "Enabled": true,
          "UUID": "abc-123",
          "User": "root",
          "Password": "***",
          "UseSSDP": false,
          "MACRequired": false,
          "RediscoverOnUpdate": true
        }
      ]
    })))
    .mount(srv)
    .await;
}

// CFS v3 components — used by prepare_session_deletion via get_cfs_components.
// Response envelope: {"components": [...]}.
async fn mock_cfs_v3_components(srv: &MockServer) {
  Mock::given(method("GET"))
    .and(path("/cfs/v3/components"))
    .respond_with(ResponseTemplate::new(200).set_body_json(json!({"components": []})))
    .mount(srv)
    .await;
}

// ---------------------------------------------------------------------------
// Phase A — Remaining GET happy paths
// ---------------------------------------------------------------------------

// GET /api/v1/kernel-parameters?hsm_group=compute
//
// Call chain mirrors get_boot_parameters: resolve_target_nodes
// (HSM groups + components) → get_bootparameters.
#[tokio::test]
async fn get_kernel_parameters_happy_path() {
  let fx = TestFixture::setup().await;
  mock_hsm_groups(&fx.mock_server).await;
  mock_hsm_components(&fx.mock_server).await;
  mock_bss_bootparameters(&fx.mock_server).await;

  let resp = fx
    .send(fx.auth_get("/api/v1/kernel-parameters?hsm_group=compute"))
    .await;
  assert_eq!(resp.status(), StatusCode::OK);

  let body = TestFixture::body_json(resp).await;
  let arr = body.as_array().expect("expected JSON array");
  assert_eq!(arr.len(), 1);
  assert_eq!(arr[0]["hosts"][0], "x3000c0s1b0n0");
}

// GET /api/v1/redfish-endpoints
//
// Call chain:
//   service::redfish_endpoints::get_redfish_endpoints
//     → backend.get_redfish_endpoints
//         → GET /smd/hsm/v2/Inventory/RedfishEndpoints
//
// Response is RedfishEndpointArray serialised as {"RedfishEndpoints": [...]}.
#[tokio::test]
async fn get_redfish_endpoints_happy_path() {
  let fx = TestFixture::setup().await;
  mock_redfish_endpoints(&fx.mock_server).await;

  let resp = fx.send(fx.auth_get("/api/v1/redfish-endpoints")).await;
  let status = resp.status();
  if status != StatusCode::OK {
    let body = TestFixture::body_json(resp).await;
    panic!("Expected 200, got {}: {:?}", status, body);
  }

  let body = TestFixture::body_json(resp).await;
  let endpoints = body["RedfishEndpoints"]
    .as_array()
    .expect("expected RedfishEndpoints array");
  assert_eq!(endpoints.len(), 1);
  assert_eq!(endpoints[0]["ID"], "x3000c0s1b0");
}

// ---------------------------------------------------------------------------
// Phase B — Write endpoint happy paths
// ---------------------------------------------------------------------------

// POST /api/v1/groups
//
// Call chain:
//   service::group::create_group → backend.add_group
//     → POST /smd/hsm/v2/groups → 201
#[tokio::test]
async fn create_group_happy_path() {
  let fx = TestFixture::setup().await;
  Mock::given(method("POST"))
    .and(path("/smd/hsm/v2/groups"))
    .respond_with(
      ResponseTemplate::new(201)
        .set_body_json(json!({"label": "new-group"})),
    )
    .mount(&fx.mock_server)
    .await;

  let resp = fx
    .send(fx.auth_post_json(
      "/api/v1/groups",
      json!({"label": "new-group", "description": "integration test"}),
    ))
    .await;
  assert_eq!(resp.status(), StatusCode::CREATED);
}

// DELETE /api/v1/groups/compute?force=true
//
// force=true bypasses validate_group_deletion, going directly to
//   backend.delete_group → DELETE /smd/hsm/v2/groups/compute
#[tokio::test]
async fn delete_group_force_happy_path() {
  let fx = TestFixture::setup().await;
  Mock::given(method("DELETE"))
    .and(path("/smd/hsm/v2/groups/compute"))
    .respond_with(ResponseTemplate::new(200).set_body_json(json!({})))
    .mount(&fx.mock_server)
    .await;

  let resp = fx
    .send(fx.auth_delete("/api/v1/groups/compute?force=true"))
    .await;
  assert_eq!(resp.status(), StatusCode::NO_CONTENT);
}

// DELETE /api/v1/nodes/x3000c0s1b0n0
//
// Call chain:
//   service::node::delete_node → backend.delete_node
//     → DELETE /hsm/v2/State/Components/x3000c0s1b0n0
//   NOTE: csm-rs omits the "smd/" prefix for this specific call.
#[tokio::test]
async fn delete_node_happy_path() {
  let fx = TestFixture::setup().await;
  Mock::given(method("DELETE"))
    .and(path("/hsm/v2/State/Components/x3000c0s1b0n0"))
    .respond_with(ResponseTemplate::new(200).set_body_json(json!({})))
    .mount(&fx.mock_server)
    .await;

  let resp = fx
    .send(fx.auth_delete("/api/v1/nodes/x3000c0s1b0n0"))
    .await;
  assert_eq!(resp.status(), StatusCode::NO_CONTENT);
}

// DELETE /api/v1/groups/compute/members — dry_run=true
//
// When dry_run=true the handler returns 204 immediately without
// making any backend calls, so no mock stubs are needed.
#[tokio::test]
async fn delete_group_members_dry_run() {
  let fx = TestFixture::setup().await;

  let resp = fx
    .send(fx.auth_delete_json(
      "/api/v1/groups/compute/members",
      json!({"xnames": ["x3000c0s1b0n0"], "dry_run": true}),
    ))
    .await;
  assert_eq!(resp.status(), StatusCode::NO_CONTENT);
}

// ---------------------------------------------------------------------------
// Phase C — Error paths
// ---------------------------------------------------------------------------

// GET /api/v1/groups — backend returns an error → handler returns 500.
//
// With no mock mounted wiremock returns 404 for the HSM groups call.
// csm-rs treats any non-2xx as Error::Message, which to_handler_error
// maps to 500.
#[tokio::test]
async fn get_groups_backend_error_returns_500() {
  let fx = TestFixture::setup().await;
  // No mock → wiremock default 404 → csm-rs Error::Message → 500.

  let resp = fx.send(fx.auth_get("/api/v1/groups")).await;
  assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR);
}

// DELETE /api/v1/sessions/unknown-session — session not found → 404.
//
// Call chain:
//   service::session::prepare_session_deletion
//     → get_groups_names_available (GET /smd/hsm/v2/groups)
//     → try_join!:
//         get_group_available      (GET /smd/hsm/v2/groups)
//         get_and_filter_sessions  (GET /cfs/v2/sessions → "my-session")
//         get_cfs_components       (GET /cfs/v3/components → [])
//         get_all_bootparameters   (GET /bss/boot/v1/bootparameters)
//     → cfs_session_vec.find("unknown-session") → None
//       → Error::NotFound → 404
//
// The sessions mock returns "my-session" (not "unknown-session") so that
// get_and_filter_sessions succeeds with a non-empty list and the "not found"
// is detected at the find() step rather than the empty-list guard.
#[tokio::test]
async fn delete_session_unknown_session_returns_404() {
  let fx = TestFixture::setup().await;
  mock_hsm_groups(&fx.mock_server).await;
  mock_cfs_v2_sessions(&fx.mock_server).await;
  mock_cfs_v3_components(&fx.mock_server).await;
  mock_bss_bootparameters(&fx.mock_server).await;

  let resp = fx
    .send(fx.auth_delete("/api/v1/sessions/unknown-session"))
    .await;
  assert_eq!(resp.status(), StatusCode::NOT_FOUND);
}