x0x 0.19.47

Agent-to-agent gossip network for AI systems — no winners, no losers, just cooperation
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
//! `x0x group` subcommands.
//!
//! Thin wrappers around the named-groups REST endpoints registered in
//! `src/api/mod.rs::ENDPOINTS`. When you add a handler here, ensure its
//! `cli_name` in the registry matches the clap subcommand in
//! `src/bin/x0x.rs`. The `parity_cli` integration test guards this.

use crate::cli::{print_value, DaemonClient};
use anyhow::{ensure, Context, Result};
use serde_json::{json, Value};

// ── Core CRUD ───────────────────────────────────────────────────────────

/// `x0x group list` — GET /groups.
pub async fn list(client: &DaemonClient) -> Result<()> {
    client.ensure_running().await?;
    let resp = client.get("/groups").await?;
    print_value(client.format(), &resp);
    Ok(())
}

/// `x0x group create` — POST /groups.
pub async fn create(
    client: &DaemonClient,
    name: &str,
    description: Option<&str>,
    display_name: Option<&str>,
    preset: Option<&str>,
) -> Result<()> {
    client.ensure_running().await?;
    let mut body = json!({ "name": name });
    if let Some(desc) = description {
        body["description"] = Value::String(desc.to_string());
    }
    if let Some(dn) = display_name {
        body["display_name"] = Value::String(dn.to_string());
    }
    if let Some(p) = preset {
        body["preset"] = Value::String(p.to_string());
    }
    let resp = client.post("/groups", &body).await?;
    print_value(client.format(), &resp);
    Ok(())
}

/// `x0x group info` — GET /groups/:id.
pub async fn info(client: &DaemonClient, group_id: &str) -> Result<()> {
    client.ensure_running().await?;
    let resp = client.get(&format!("/groups/{group_id}")).await?;
    print_value(client.format(), &resp);
    Ok(())
}

/// `x0x group update` — PATCH /groups/:id.
pub async fn update(
    client: &DaemonClient,
    group_id: &str,
    name: Option<&str>,
    description: Option<&str>,
) -> Result<()> {
    ensure!(
        name.is_some() || description.is_some(),
        "group update requires at least one of: --name, --description"
    );
    client.ensure_running().await?;
    let mut body = json!({});
    if let Some(n) = name {
        body["name"] = Value::String(n.to_string());
    }
    if let Some(d) = description {
        body["description"] = Value::String(d.to_string());
    }
    let resp = client.patch(&format!("/groups/{group_id}"), &body).await?;
    print_value(client.format(), &resp);
    Ok(())
}

/// `x0x group members` — GET /groups/:id/members.
pub async fn members(client: &DaemonClient, group_id: &str) -> Result<()> {
    client.ensure_running().await?;
    let resp = client.get(&format!("/groups/{group_id}/members")).await?;
    print_value(client.format(), &resp);
    Ok(())
}

/// `x0x group add-member` — POST /groups/:id/members.
pub async fn add_member(
    client: &DaemonClient,
    group_id: &str,
    agent_id: &str,
    display_name: Option<&str>,
) -> Result<()> {
    client.ensure_running().await?;
    let mut body = json!({ "agent_id": agent_id });
    if let Some(dn) = display_name {
        body["display_name"] = Value::String(dn.to_string());
    }
    let resp = client
        .post(&format!("/groups/{group_id}/members"), &body)
        .await?;
    print_value(client.format(), &resp);
    Ok(())
}

/// `x0x group remove-member` — DELETE /groups/:id/members/:agent_id.
pub async fn remove_member(client: &DaemonClient, group_id: &str, agent_id: &str) -> Result<()> {
    client.ensure_running().await?;
    let resp = client
        .delete(&format!("/groups/{group_id}/members/{agent_id}"))
        .await?;
    print_value(client.format(), &resp);
    Ok(())
}

/// `x0x group invite` — POST /groups/:id/invite.
pub async fn invite(client: &DaemonClient, group_id: &str, expiry_secs: u64) -> Result<()> {
    client.ensure_running().await?;
    let body = json!({ "expiry_secs": expiry_secs });
    let resp = client
        .post(&format!("/groups/{group_id}/invite"), &body)
        .await?;
    print_value(client.format(), &resp);
    Ok(())
}

/// `x0x group join` — POST /groups/join.
pub async fn join(
    client: &DaemonClient,
    invite_link: &str,
    display_name: Option<&str>,
) -> Result<()> {
    client.ensure_running().await?;
    let mut body = json!({ "invite": invite_link });
    if let Some(dn) = display_name {
        body["display_name"] = Value::String(dn.to_string());
    }
    let resp = client.post("/groups/join", &body).await?;
    print_value(client.format(), &resp);
    Ok(())
}

/// `x0x group set-name` — PUT /groups/:id/display-name.
pub async fn set_name(client: &DaemonClient, group_id: &str, name: &str) -> Result<()> {
    client.ensure_running().await?;
    let body = json!({ "name": name });
    let resp = client
        .put(&format!("/groups/{group_id}/display-name"), &body)
        .await?;
    print_value(client.format(), &resp);
    Ok(())
}

/// `x0x group leave` — DELETE /groups/:id.
pub async fn leave(client: &DaemonClient, group_id: &str) -> Result<()> {
    client.ensure_running().await?;
    let resp = client.delete(&format!("/groups/{group_id}")).await?;
    print_value(client.format(), &resp);
    Ok(())
}

// ── Policy / roles / bans ───────────────────────────────────────────────

/// `x0x group policy` — PATCH /groups/:id/policy.
///
/// Accepts either a preset name via `--preset` or individual axes via the
/// other flags. When both are supplied, the daemon applies the preset
/// first and overlays the individual axes. At least one field must be
/// set; the CLI rejects empty patches before contacting the daemon.
#[allow(clippy::too_many_arguments)]
pub async fn policy(
    client: &DaemonClient,
    group_id: &str,
    preset: Option<&str>,
    discoverability: Option<&str>,
    admission: Option<&str>,
    confidentiality: Option<&str>,
    read_access: Option<&str>,
    write_access: Option<&str>,
) -> Result<()> {
    ensure!(
        preset.is_some()
            || discoverability.is_some()
            || admission.is_some()
            || confidentiality.is_some()
            || read_access.is_some()
            || write_access.is_some(),
        "group policy requires at least one of: --preset, --discoverability, --admission, --confidentiality, --read-access, --write-access"
    );
    client.ensure_running().await?;
    let mut body = json!({});
    if let Some(v) = preset {
        body["preset"] = Value::String(v.to_string());
    }
    if let Some(v) = discoverability {
        body["discoverability"] = Value::String(v.to_string());
    }
    if let Some(v) = admission {
        body["admission"] = Value::String(v.to_string());
    }
    if let Some(v) = confidentiality {
        body["confidentiality"] = Value::String(v.to_string());
    }
    if let Some(v) = read_access {
        body["read_access"] = Value::String(v.to_string());
    }
    if let Some(v) = write_access {
        body["write_access"] = Value::String(v.to_string());
    }
    let resp = client
        .patch(&format!("/groups/{group_id}/policy"), &body)
        .await?;
    print_value(client.format(), &resp);
    Ok(())
}

/// `x0x group set-role` — PATCH /groups/:id/members/:agent_id/role.
pub async fn set_role(
    client: &DaemonClient,
    group_id: &str,
    agent_id: &str,
    role: &str,
) -> Result<()> {
    client.ensure_running().await?;
    let body = json!({ "role": role });
    let resp = client
        .patch(
            &format!("/groups/{group_id}/members/{agent_id}/role"),
            &body,
        )
        .await?;
    print_value(client.format(), &resp);
    Ok(())
}

/// `x0x group ban` — POST /groups/:id/ban/:agent_id.
pub async fn ban(client: &DaemonClient, group_id: &str, agent_id: &str) -> Result<()> {
    client.ensure_running().await?;
    let resp = client
        .post_empty(&format!("/groups/{group_id}/ban/{agent_id}"))
        .await?;
    print_value(client.format(), &resp);
    Ok(())
}

/// `x0x group unban` — DELETE /groups/:id/ban/:agent_id.
pub async fn unban(client: &DaemonClient, group_id: &str, agent_id: &str) -> Result<()> {
    client.ensure_running().await?;
    let resp = client
        .delete(&format!("/groups/{group_id}/ban/{agent_id}"))
        .await?;
    print_value(client.format(), &resp);
    Ok(())
}

// ── Join requests ───────────────────────────────────────────────────────

/// `x0x group requests` — GET /groups/:id/requests.
pub async fn requests(client: &DaemonClient, group_id: &str) -> Result<()> {
    client.ensure_running().await?;
    let resp = client.get(&format!("/groups/{group_id}/requests")).await?;
    print_value(client.format(), &resp);
    Ok(())
}

/// `x0x group request-access` — POST /groups/:id/requests.
pub async fn request_access(
    client: &DaemonClient,
    group_id: &str,
    message: Option<&str>,
) -> Result<()> {
    client.ensure_running().await?;
    let body = match message {
        Some(m) => json!({ "message": m }),
        None => json!({}),
    };
    let resp = client
        .post(&format!("/groups/{group_id}/requests"), &body)
        .await?;
    print_value(client.format(), &resp);
    Ok(())
}

/// `x0x group approve-request` — POST /groups/:id/requests/:request_id/approve.
pub async fn approve_request(
    client: &DaemonClient,
    group_id: &str,
    request_id: &str,
) -> Result<()> {
    client.ensure_running().await?;
    let resp = client
        .post_empty(&format!("/groups/{group_id}/requests/{request_id}/approve"))
        .await?;
    print_value(client.format(), &resp);
    Ok(())
}

/// `x0x group reject-request` — POST /groups/:id/requests/:request_id/reject.
pub async fn reject_request(client: &DaemonClient, group_id: &str, request_id: &str) -> Result<()> {
    client.ensure_running().await?;
    let resp = client
        .post_empty(&format!("/groups/{group_id}/requests/{request_id}/reject"))
        .await?;
    print_value(client.format(), &resp);
    Ok(())
}

/// `x0x group cancel-request` — DELETE /groups/:id/requests/:request_id.
pub async fn cancel_request(client: &DaemonClient, group_id: &str, request_id: &str) -> Result<()> {
    client.ensure_running().await?;
    let resp = client
        .delete(&format!("/groups/{group_id}/requests/{request_id}"))
        .await?;
    print_value(client.format(), &resp);
    Ok(())
}

// ── Discovery ───────────────────────────────────────────────────────────

/// `x0x group discover` — GET /groups/discover?q=...
pub async fn discover(client: &DaemonClient, query: Option<&str>) -> Result<()> {
    client.ensure_running().await?;
    let resp = match query {
        Some(q) if !q.is_empty() => client.get_query("/groups/discover", &[("q", q)]).await?,
        _ => client.get("/groups/discover").await?,
    };
    print_value(client.format(), &resp);
    Ok(())
}

/// `x0x group discover-nearby` — GET /groups/discover/nearby.
pub async fn discover_nearby(client: &DaemonClient) -> Result<()> {
    client.ensure_running().await?;
    let resp = client.get("/groups/discover/nearby").await?;
    print_value(client.format(), &resp);
    Ok(())
}

/// `x0x group discover-subscriptions` — GET /groups/discover/subscriptions.
pub async fn discover_subscriptions(client: &DaemonClient) -> Result<()> {
    client.ensure_running().await?;
    let resp = client.get("/groups/discover/subscriptions").await?;
    print_value(client.format(), &resp);
    Ok(())
}

/// `x0x group discover-subscribe` — POST /groups/discover/subscribe.
pub async fn discover_subscribe(
    client: &DaemonClient,
    kind: &str,
    key: Option<&str>,
    shard: Option<u32>,
) -> Result<()> {
    client.ensure_running().await?;
    let mut body = json!({ "kind": kind });
    if let Some(k) = key {
        body["key"] = Value::String(k.to_string());
    }
    if let Some(s) = shard {
        body["shard"] = Value::Number(s.into());
    }
    let resp = client.post("/groups/discover/subscribe", &body).await?;
    print_value(client.format(), &resp);
    Ok(())
}

/// `x0x group discover-unsubscribe` — DELETE /groups/discover/subscribe/:kind/:shard.
pub async fn discover_unsubscribe(client: &DaemonClient, kind: &str, shard: u32) -> Result<()> {
    client.ensure_running().await?;
    let resp = client
        .delete(&format!("/groups/discover/subscribe/{kind}/{shard}"))
        .await?;
    print_value(client.format(), &resp);
    Ok(())
}

/// `x0x group card` — GET /groups/cards/:id.
pub async fn card(client: &DaemonClient, group_id: &str) -> Result<()> {
    client.ensure_running().await?;
    let resp = client.get(&format!("/groups/cards/{group_id}")).await?;
    print_value(client.format(), &resp);
    Ok(())
}

/// `x0x group card-import` — POST /groups/cards/import.
///
/// Takes a path to a JSON file containing a signed `GroupCard`. Stdin
/// support: pass `-` as the path.
pub async fn card_import(client: &DaemonClient, path: &str) -> Result<()> {
    client.ensure_running().await?;
    let raw = if path == "-" {
        use std::io::Read as _;
        let mut s = String::new();
        std::io::stdin()
            .read_to_string(&mut s)
            .context("read card from stdin")?;
        s
    } else {
        std::fs::read_to_string(path).with_context(|| format!("read card from {path}"))?
    };
    let card: Value = serde_json::from_str(&raw).context("parse card JSON")?;
    let resp = client.post("/groups/cards/import", &card).await?;
    print_value(client.format(), &resp);
    Ok(())
}

// ── Public messaging (Phase E) ──────────────────────────────────────────

/// `x0x group send` — POST /groups/:id/send.
pub async fn send(
    client: &DaemonClient,
    group_id: &str,
    body_text: &str,
    kind: Option<&str>,
) -> Result<()> {
    client.ensure_running().await?;
    let mut req = json!({ "body": body_text });
    if let Some(k) = kind {
        req["kind"] = Value::String(k.to_string());
    }
    let resp = client
        .post(&format!("/groups/{group_id}/send"), &req)
        .await?;
    print_value(client.format(), &resp);
    Ok(())
}

/// `x0x group messages` — GET /groups/:id/messages.
pub async fn messages(client: &DaemonClient, group_id: &str) -> Result<()> {
    client.ensure_running().await?;
    let resp = client.get(&format!("/groups/{group_id}/messages")).await?;
    print_value(client.format(), &resp);
    Ok(())
}

// ── State-commit chain (Phase D.3) ──────────────────────────────────────

/// `x0x group state` — GET /groups/:id/state.
pub async fn state(client: &DaemonClient, group_id: &str) -> Result<()> {
    client.ensure_running().await?;
    let resp = client.get(&format!("/groups/{group_id}/state")).await?;
    print_value(client.format(), &resp);
    Ok(())
}

/// `x0x group state-seal` — POST /groups/:id/state/seal.
pub async fn state_seal(client: &DaemonClient, group_id: &str) -> Result<()> {
    client.ensure_running().await?;
    let resp = client
        .post_empty(&format!("/groups/{group_id}/state/seal"))
        .await?;
    print_value(client.format(), &resp);
    Ok(())
}

/// `x0x group state-withdraw` — POST /groups/:id/state/withdraw.
pub async fn state_withdraw(client: &DaemonClient, group_id: &str) -> Result<()> {
    client.ensure_running().await?;
    let resp = client
        .post_empty(&format!("/groups/{group_id}/state/withdraw"))
        .await?;
    print_value(client.format(), &resp);
    Ok(())
}

// ── Secure plane (Phase D.2) ────────────────────────────────────────────

/// `x0x group secure-encrypt` — POST /groups/:id/secure/encrypt.
///
/// `payload` is encoded as base64 before being sent.
pub async fn secure_encrypt(client: &DaemonClient, group_id: &str, payload: &[u8]) -> Result<()> {
    use base64::Engine as _;
    client.ensure_running().await?;
    let payload_b64 = base64::engine::general_purpose::STANDARD.encode(payload);
    let body = json!({ "payload_b64": payload_b64 });
    let resp = client
        .post(&format!("/groups/{group_id}/secure/encrypt"), &body)
        .await?;
    print_value(client.format(), &resp);
    Ok(())
}

/// `x0x group secure-decrypt` — POST /groups/:id/secure/decrypt.
pub async fn secure_decrypt(
    client: &DaemonClient,
    group_id: &str,
    ciphertext_b64: &str,
    nonce_b64: &str,
    secret_epoch: u64,
) -> Result<()> {
    client.ensure_running().await?;
    let body = json!({
        "ciphertext_b64": ciphertext_b64,
        "nonce_b64": nonce_b64,
        "secret_epoch": secret_epoch,
    });
    let resp = client
        .post(&format!("/groups/{group_id}/secure/decrypt"), &body)
        .await?;
    print_value(client.format(), &resp);
    Ok(())
}

/// `x0x group secure-reseal` — POST /groups/:id/secure/reseal.
pub async fn secure_reseal(client: &DaemonClient, group_id: &str, recipient: &str) -> Result<()> {
    client.ensure_running().await?;
    let body = json!({ "recipient": recipient });
    let resp = client
        .post(&format!("/groups/{group_id}/secure/reseal"), &body)
        .await?;
    print_value(client.format(), &resp);
    Ok(())
}

/// `x0x group secure-open-envelope` — POST /groups/secure/open-envelope.
///
/// Reads the envelope JSON from `path` (or stdin if `path == "-"`).
/// Adversarial test endpoint: attempts to decrypt the envelope with this
/// daemon's KEM private key.
pub async fn secure_open_envelope(client: &DaemonClient, path: &str) -> Result<()> {
    client.ensure_running().await?;
    let raw = if path == "-" {
        use std::io::Read as _;
        let mut s = String::new();
        std::io::stdin()
            .read_to_string(&mut s)
            .context("read envelope from stdin")?;
        s
    } else {
        std::fs::read_to_string(path).with_context(|| format!("read envelope from {path}"))?
    };
    let envelope: Value = serde_json::from_str(&raw).context("parse envelope JSON")?;
    let resp = client
        .post("/groups/secure/open-envelope", &envelope)
        .await?;
    print_value(client.format(), &resp);
    Ok(())
}

#[cfg(test)]
mod tests {
    #![allow(clippy::unwrap_used)]
    use super::*;
    use crate::cli::DaemonClient;

    /// Start a mock axum server that returns the given JSON for any request.
    #[allow(dead_code)]
    async fn start_mock_server(
        response_json: serde_json::Value,
    ) -> (String, tokio::sync::oneshot::Sender<()>) {
        use std::sync::Arc;

        let json = Arc::new(response_json);
        let app = axum::Router::new().fallback(move |_req: axum::extract::Request| {
            let json = Arc::clone(&json);
            async move {
                let body = serde_json::to_vec(&*json).unwrap();
                axum::response::Response::builder()
                    .status(200)
                    .header("content-type", "application/json")
                    .body(axum::body::Body::from(body))
                    .unwrap()
            }
        });

        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();
        let (tx, rx) = tokio::sync::oneshot::channel::<()>();

        tokio::spawn(async move {
            axum::serve(listener, app.into_make_service())
                .with_graceful_shutdown(async {
                    rx.await.ok();
                })
                .await
                .ok();
        });

        tokio::time::sleep(std::time::Duration::from_millis(50)).await;

        (format!("http://{}", addr), tx)
    }

    #[tokio::test]
    async fn list_returns_mock_response() {
        let mock_resp = serde_json::json!({"status": "ok"});
        let (url, _shutdown) = start_mock_server(mock_resp).await;
        let client = DaemonClient::new(None, Some(&url), crate::cli::OutputFormat::Json).unwrap();
        let result = list(&client).await;
        assert!(result.is_ok(), "list should succeed: {:?}", result);
    }
    #[tokio::test]
    async fn info_returns_mock_response() {
        let mock_resp = serde_json::json!({"status": "ok"});
        let (url, _shutdown) = start_mock_server(mock_resp).await;
        let client = DaemonClient::new(None, Some(&url), crate::cli::OutputFormat::Json).unwrap();
        let result = info(&client, "group-123").await;
        assert!(result.is_ok(), "info should succeed: {:?}", result);
    }
    #[tokio::test]
    async fn members_returns_mock_response() {
        let mock_resp = serde_json::json!({"status": "ok"});
        let (url, _shutdown) = start_mock_server(mock_resp).await;
        let client = DaemonClient::new(None, Some(&url), crate::cli::OutputFormat::Json).unwrap();
        let result = members(&client, "group-123").await;
        assert!(result.is_ok(), "members should succeed: {:?}", result);
    }

    #[tokio::test]
    async fn leave_returns_mock_response() {
        let mock_resp = serde_json::json!({"ok": true});
        let (url, _shutdown) = start_mock_server(mock_resp).await;
        let client = DaemonClient::new(None, Some(&url), crate::cli::OutputFormat::Json).unwrap();
        let result = leave(&client, "group-123").await;
        assert!(result.is_ok(), "leave should succeed: {:?}", result);
    }

    #[tokio::test]
    async fn requests_returns_mock_response() {
        let mock_resp = serde_json::json!([{"request_id": "req-1"}]);
        let (url, _shutdown) = start_mock_server(mock_resp).await;
        let client = DaemonClient::new(None, Some(&url), crate::cli::OutputFormat::Json).unwrap();
        let result = requests(&client, "group-123").await;
        assert!(result.is_ok(), "requests should succeed: {:?}", result);
    }

    #[tokio::test]
    async fn cancel_request_returns_mock_response() {
        let mock_resp = serde_json::json!({"ok": true});
        let (url, _shutdown) = start_mock_server(mock_resp).await;
        let client = DaemonClient::new(None, Some(&url), crate::cli::OutputFormat::Json).unwrap();
        let result = cancel_request(&client, "group-123", "req-1").await;
        assert!(
            result.is_ok(),
            "cancel_request should succeed: {:?}",
            result
        );
    }

    #[tokio::test]
    async fn discover_returns_mock_response() {
        let mock_resp = serde_json::json!([{"id": "group-1"}]);
        let (url, _shutdown) = start_mock_server(mock_resp).await;
        let client = DaemonClient::new(None, Some(&url), crate::cli::OutputFormat::Json).unwrap();
        let result = discover(&client, Some("test")).await;
        assert!(result.is_ok(), "discover should succeed: {:?}", result);
    }

    #[tokio::test]
    async fn discover_nearby_returns_mock_response() {
        let mock_resp = serde_json::json!([{"id": "group-1"}]);
        let (url, _shutdown) = start_mock_server(mock_resp).await;
        let client = DaemonClient::new(None, Some(&url), crate::cli::OutputFormat::Json).unwrap();
        let result = discover_nearby(&client).await;
        assert!(
            result.is_ok(),
            "discover_nearby should succeed: {:?}",
            result
        );
    }

    #[tokio::test]
    async fn discover_subscriptions_returns_mock_response() {
        let mock_resp = serde_json::json!([{"kind": "tag", "shard": 42}]);
        let (url, _shutdown) = start_mock_server(mock_resp).await;
        let client = DaemonClient::new(None, Some(&url), crate::cli::OutputFormat::Json).unwrap();
        let result = discover_subscriptions(&client).await;
        assert!(
            result.is_ok(),
            "discover_subscriptions should succeed: {:?}",
            result
        );
    }

    #[tokio::test]
    async fn card_returns_mock_response() {
        let mock_resp = serde_json::json!({"id": "group-1", "name": "test"});
        let (url, _shutdown) = start_mock_server(mock_resp).await;
        let client = DaemonClient::new(None, Some(&url), crate::cli::OutputFormat::Json).unwrap();
        let result = card(&client, "group-123").await;
        assert!(result.is_ok(), "card should succeed: {:?}", result);
    }

    #[tokio::test]
    async fn messages_returns_mock_response() {
        let mock_resp = serde_json::json!([{"id": "msg-1", "text": "hello"}]);
        let (url, _shutdown) = start_mock_server(mock_resp).await;
        let client = DaemonClient::new(None, Some(&url), crate::cli::OutputFormat::Json).unwrap();
        let result = messages(&client, "group-123").await;
        assert!(result.is_ok(), "messages should succeed: {:?}", result);
    }

    #[tokio::test]
    async fn state_returns_mock_response() {
        let mock_resp = serde_json::json!({"version": 1, "sealed": false});
        let (url, _shutdown) = start_mock_server(mock_resp).await;
        let client = DaemonClient::new(None, Some(&url), crate::cli::OutputFormat::Json).unwrap();
        let result = state(&client, "group-123").await;
        assert!(result.is_ok(), "state should succeed: {:?}", result);
    }

    #[tokio::test]
    async fn state_seal_returns_mock_response() {
        let mock_resp = serde_json::json!({"ok": true});
        let (url, _shutdown) = start_mock_server(mock_resp).await;
        let client = DaemonClient::new(None, Some(&url), crate::cli::OutputFormat::Json).unwrap();
        let result = state_seal(&client, "group-123").await;
        assert!(result.is_ok(), "state_seal should succeed: {:?}", result);
    }

    #[tokio::test]
    async fn state_withdraw_returns_mock_response() {
        let mock_resp = serde_json::json!({"ok": true});
        let (url, _shutdown) = start_mock_server(mock_resp).await;
        let client = DaemonClient::new(None, Some(&url), crate::cli::OutputFormat::Json).unwrap();
        let result = state_withdraw(&client, "group-123").await;
        assert!(
            result.is_ok(),
            "state_withdraw should succeed: {:?}",
            result
        );
    }

    #[tokio::test]
    async fn set_name_returns_mock_response() {
        let mock_resp = serde_json::json!({"ok": true});
        let (url, _shutdown) = start_mock_server(mock_resp).await;
        let client = DaemonClient::new(None, Some(&url), crate::cli::OutputFormat::Json).unwrap();
        let result = set_name(&client, "group-123", "new-name").await;
        assert!(result.is_ok(), "set_name should succeed: {:?}", result);
    }

    #[tokio::test]
    async fn ban_returns_mock_response() {
        let mock_resp = serde_json::json!({"ok": true});
        let (url, _shutdown) = start_mock_server(mock_resp).await;
        let client = DaemonClient::new(None, Some(&url), crate::cli::OutputFormat::Json).unwrap();
        let result = ban(&client, "group-123", "agent-456").await;
        assert!(result.is_ok(), "ban should succeed: {:?}", result);
    }

    #[tokio::test]
    async fn unban_returns_mock_response() {
        let mock_resp = serde_json::json!({"ok": true});
        let (url, _shutdown) = start_mock_server(mock_resp).await;
        let client = DaemonClient::new(None, Some(&url), crate::cli::OutputFormat::Json).unwrap();
        let result = unban(&client, "group-123", "agent-456").await;
        assert!(result.is_ok(), "unban should succeed: {:?}", result);
    }

    #[tokio::test]
    async fn reject_request_returns_mock_response() {
        let mock_resp = serde_json::json!({"ok": true});
        let (url, _shutdown) = start_mock_server(mock_resp).await;
        let client = DaemonClient::new(None, Some(&url), crate::cli::OutputFormat::Json).unwrap();
        let result = reject_request(&client, "group-123", "req-1").await;
        assert!(
            result.is_ok(),
            "reject_request should succeed: {:?}",
            result
        );
    }

    #[tokio::test]
    async fn invite_returns_mock_response() {
        let mock_resp = serde_json::json!({"invite_code": "abc123"});
        let (url, _shutdown) = start_mock_server(mock_resp).await;
        let client = DaemonClient::new(None, Some(&url), crate::cli::OutputFormat::Json).unwrap();
        let result = invite(&client, "group-123", 3600).await;
        assert!(result.is_ok(), "invite should succeed: {:?}", result);
    }

    #[tokio::test]
    async fn create_returns_mock_response() {
        let mock_resp = serde_json::json!({"id": "new-group", "name": "test"});
        let (url, _shutdown) = start_mock_server(mock_resp).await;
        let client = DaemonClient::new(None, Some(&url), crate::cli::OutputFormat::Json).unwrap();
        let result = create(&client, "test-group", None, None, None).await;
        assert!(result.is_ok(), "create should succeed: {:?}", result);
    }

    #[tokio::test]
    async fn add_member_returns_mock_response() {
        let mock_resp = serde_json::json!({"ok": true});
        let (url, _shutdown) = start_mock_server(mock_resp).await;
        let client = DaemonClient::new(None, Some(&url), crate::cli::OutputFormat::Json).unwrap();
        let result = add_member(&client, "group-123", "agent-456", None).await;
        assert!(result.is_ok(), "add_member should succeed: {:?}", result);
    }

    #[tokio::test]
    async fn remove_member_returns_mock_response() {
        let mock_resp = serde_json::json!({"ok": true});
        let (url, _shutdown) = start_mock_server(mock_resp).await;
        let client = DaemonClient::new(None, Some(&url), crate::cli::OutputFormat::Json).unwrap();
        let result = remove_member(&client, "group-123", "agent-456").await;
        assert!(result.is_ok(), "remove_member should succeed: {:?}", result);
    }

    #[tokio::test]
    async fn join_returns_mock_response() {
        let mock_resp = serde_json::json!({"id": "joined-group"});
        let (url, _shutdown) = start_mock_server(mock_resp).await;
        let client = DaemonClient::new(None, Some(&url), crate::cli::OutputFormat::Json).unwrap();
        let result = join(&client, "invite-code-123", None).await;
        assert!(result.is_ok(), "join should succeed: {:?}", result);
    }

    #[tokio::test]
    async fn request_access_returns_mock_response() {
        let mock_resp = serde_json::json!({"ok": true});
        let (url, _shutdown) = start_mock_server(mock_resp).await;
        let client = DaemonClient::new(None, Some(&url), crate::cli::OutputFormat::Json).unwrap();
        let result = request_access(&client, "group-123", None).await;
        assert!(
            result.is_ok(),
            "request_access should succeed: {:?}",
            result
        );
    }

    #[tokio::test]
    async fn approve_request_returns_mock_response() {
        let mock_resp = serde_json::json!({"ok": true});
        let (url, _shutdown) = start_mock_server(mock_resp).await;
        let client = DaemonClient::new(None, Some(&url), crate::cli::OutputFormat::Json).unwrap();
        let result = approve_request(&client, "group-123", "req-1").await;
        assert!(
            result.is_ok(),
            "approve_request should succeed: {:?}",
            result
        );
    }

    #[tokio::test]
    async fn discover_subscribe_returns_mock_response() {
        let mock_resp = serde_json::json!({"ok": true});
        let (url, _shutdown) = start_mock_server(mock_resp).await;
        let client = DaemonClient::new(None, Some(&url), crate::cli::OutputFormat::Json).unwrap();
        let result = discover_subscribe(&client, "tag", None, None).await;
        assert!(
            result.is_ok(),
            "discover_subscribe should succeed: {:?}",
            result
        );
    }

    #[tokio::test]
    async fn discover_unsubscribe_returns_mock_response() {
        let mock_resp = serde_json::json!({"ok": true});
        let (url, _shutdown) = start_mock_server(mock_resp).await;
        let client = DaemonClient::new(None, Some(&url), crate::cli::OutputFormat::Json).unwrap();
        let result = discover_unsubscribe(&client, "tag", 42).await;
        assert!(
            result.is_ok(),
            "discover_unsubscribe should succeed: {:?}",
            result
        );
    }

    #[tokio::test]
    async fn secure_encrypt_returns_mock_response() {
        let mock_resp = serde_json::json!({"ciphertext": "encrypted-data"});
        let (url, _shutdown) = start_mock_server(mock_resp).await;
        let client = DaemonClient::new(None, Some(&url), crate::cli::OutputFormat::Json).unwrap();
        let result = secure_encrypt(&client, "group-123", b"hello").await;
        assert!(
            result.is_ok(),
            "secure_encrypt should succeed: {:?}",
            result
        );
    }

    #[tokio::test]
    async fn secure_reseal_returns_mock_response() {
        let mock_resp = serde_json::json!({"ciphertext": "resealed-data"});
        let (url, _shutdown) = start_mock_server(mock_resp).await;
        let client = DaemonClient::new(None, Some(&url), crate::cli::OutputFormat::Json).unwrap();
        let result = secure_reseal(&client, "group-123", "agent-456").await;
        assert!(result.is_ok(), "secure_reseal should succeed: {:?}", result);
    }
}