purple-ssh 3.12.3

Open-source terminal SSH manager that keeps ~/.ssh/config in sync with your cloud infra. Spin up a VM on AWS, GCP, Azure, Hetzner or 12 other cloud providers and it appears in your host list. Destroy it and the entry dims. Search hundreds of hosts, transfer files, manage Docker and Podman over SSH, sign Vault SSH certs. Rust TUI, MIT licensed.
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
//! Property-based tests for combined mutation sequences.
//!
//! Simulates realistic purple usage: add hosts, set tags/provider/meta/askpass,
//! add tunnels, update, delete, undo, sync, import -- all interleaved -- and
//! verifies that the config never corrupts, panics, or loses structural integrity.

use std::path::PathBuf;

use proptest::prelude::*;
use purple_ssh::providers::config::ProviderConfigId;
use purple_ssh::ssh_config::model::{HostEntry, SshConfigFile};

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

fn parse_str(content: &str) -> SshConfigFile {
    SshConfigFile {
        elements: SshConfigFile::parse_content(content),
        path: PathBuf::from("/tmp/proptest_mutations"),
        crlf: content.contains("\r\n"),
        bom: content.starts_with('\u{FEFF}'),
    }
}

/// Assert round-trip idempotency: serialize -> reparse -> serialize must match.
fn assert_idempotent(config: &SshConfigFile) {
    let s1 = config.serialize();
    let reparsed = parse_str(&s1);
    let s2 = reparsed.serialize();
    assert_eq!(
        s1,
        s2,
        "Round-trip idempotency broken (len {} vs {})",
        s1.len(),
        s2.len()
    );
}

/// Assert no host block leaks directives into another block.
fn assert_no_directive_leak(config: &SshConfigFile) {
    let serialized = config.serialize();
    let reparsed = parse_str(&serialized);
    let entries_before = config.host_entries();
    let entries_after = reparsed.host_entries();
    assert_eq!(
        entries_before.len(),
        entries_after.len(),
        "Host count changed after reparse ({} -> {})",
        entries_before.len(),
        entries_after.len(),
    );
    for (a, b) in entries_before.iter().zip(entries_after.iter()) {
        assert_eq!(a.alias, b.alias, "Alias mismatch after reparse");
        assert_eq!(
            a.hostname, b.hostname,
            "Hostname mismatch for '{}'",
            a.alias
        );
        assert_eq!(a.user, b.user, "User mismatch for '{}'", a.alias);
        assert_eq!(a.port, b.port, "Port mismatch for '{}'", a.alias);
        assert_eq!(a.tags, b.tags, "Tags mismatch for '{}'", a.alias);
        assert_eq!(
            a.provider, b.provider,
            "Provider mismatch for '{}'",
            a.alias
        );
        assert_eq!(a.askpass, b.askpass, "Askpass mismatch for '{}'", a.alias);
        assert_eq!(
            a.tunnel_count, b.tunnel_count,
            "Tunnel count mismatch for '{}'",
            a.alias
        );
        assert_eq!(a.stale, b.stale, "Stale mismatch for '{}'", a.alias);
    }
}

// ---------------------------------------------------------------------------
// Strategies
// ---------------------------------------------------------------------------

fn alias_strategy() -> impl Strategy<Value = String> {
    "[a-z][a-z0-9-]{1,15}"
}

fn hostname_strategy() -> impl Strategy<Value = String> {
    (1u8..=254, 0u8..=255, 0u8..=255, 1u8..=254).prop_map(|(a, b, c, d)| format!("{a}.{b}.{c}.{d}"))
}

fn user_strategy() -> impl Strategy<Value = String> {
    prop_oneof![
        Just("root".to_string()),
        Just("admin".to_string()),
        Just("deploy".to_string()),
        "[a-z]{3,8}",
    ]
}

fn tags_strategy() -> impl Strategy<Value = Vec<String>> {
    prop::collection::vec("[a-z]{2,8}", 0..=5)
}

fn meta_strategy() -> impl Strategy<Value = Vec<(String, String)>> {
    prop::collection::vec(("[a-z]{3,8}", "[a-z0-9-]{2,12}"), 0..=4)
        .prop_map(|v| v.into_iter().collect())
}

fn askpass_strategy() -> impl Strategy<Value = String> {
    prop_oneof![
        Just("keychain".to_string()),
        Just("op://Servers/prod/password".to_string()),
        Just("bw:my-server-password".to_string()),
        Just("pass:servers/prod".to_string()),
        Just("vault:secret/ssh/prod".to_string()),
        Just(String::new()), // clear askpass
    ]
}

fn forward_strategy() -> impl Strategy<Value = (String, String)> {
    prop_oneof![
        (1024u16..=65535, 1u16..=65535).prop_map(|(local, remote)| {
            (
                "LocalForward".to_string(),
                format!("{local} localhost:{remote}"),
            )
        }),
        (1024u16..=65535, 1u16..=65535).prop_map(|(local, remote)| {
            (
                "RemoteForward".to_string(),
                format!("{local} localhost:{remote}"),
            )
        }),
        (1024u16..=65535,)
            .prop_map(|(port,)| { ("DynamicForward".to_string(), format!("{port}")) }),
    ]
}

fn host_entry(alias: &str, hostname: &str, user: &str) -> HostEntry {
    HostEntry {
        alias: alias.to_string(),
        hostname: hostname.to_string(),
        user: user.to_string(),
        port: 22,
        ..Default::default()
    }
}

// ---------------------------------------------------------------------------
// An action that can be applied to a config
// ---------------------------------------------------------------------------

#[derive(Debug, Clone)]
enum Action {
    AddHost {
        alias: String,
        hostname: String,
        user: String,
    },
    UpdateHost {
        hostname: String,
        user: String,
    },
    DeleteFirst,
    DeleteUndoFirst,
    SwapFirstTwo,
    SetTags {
        tags: Vec<String>,
    },
    SetAskpass {
        source: String,
    },
    SetProvider {
        name: String,
        id: String,
    },
    SetProviderLabeled {
        name: String,
        label: String,
        id: String,
    },
    RewriteLegacyToLabel {
        name: String,
        label: String,
    },
    SetMeta {
        meta: Vec<(String, String)>,
    },
    AddForward {
        key: String,
        value: String,
    },
    RemoveForward {
        key: String,
        value: String,
    },
    SetStale {
        timestamp: u64,
    },
    ClearStale,
    RepairGroupComments,
    RemoveOrphanedHeaders,
}

fn action_strategy() -> impl Strategy<Value = Action> {
    prop_oneof![
        // AddHost
        (alias_strategy(), hostname_strategy(), user_strategy()).prop_map(|(a, h, u)| {
            Action::AddHost {
                alias: a,
                hostname: h,
                user: u,
            }
        }),
        // UpdateHost
        (hostname_strategy(), user_strategy()).prop_map(|(h, u)| {
            Action::UpdateHost {
                hostname: h,
                user: u,
            }
        }),
        // DeleteFirst
        Just(Action::DeleteFirst),
        // DeleteUndoFirst (delete + immediate undo)
        Just(Action::DeleteUndoFirst),
        // SwapFirstTwo
        Just(Action::SwapFirstTwo),
        // SetTags
        tags_strategy().prop_map(|tags| Action::SetTags { tags }),
        // SetAskpass
        askpass_strategy().prop_map(|source| Action::SetAskpass { source }),
        // SetProvider (legacy 2-segment marker)
        (
            prop_oneof![
                Just("aws"),
                Just("digitalocean"),
                Just("hetzner"),
                Just("gcp")
            ],
            "[a-z0-9]{8,16}",
        )
            .prop_map(|(name, id)| Action::SetProvider {
                name: name.to_string(),
                id,
            }),
        // SetProviderLabeled (3-segment marker for multi-config)
        (
            prop_oneof![Just("aws"), Just("digitalocean"), Just("hetzner")],
            prop_oneof![
                Just("work"),
                Just("personal"),
                Just("default"),
                Just("prod-1"),
            ],
            "[a-z0-9]{6,12}",
        )
            .prop_map(|(name, label, id)| Action::SetProviderLabeled {
                name: name.to_string(),
                label: label.to_string(),
                id,
            }),
        // RewriteLegacyToLabel exercises the lazy-migration helper.
        (
            prop_oneof![Just("aws"), Just("digitalocean"), Just("hetzner")],
            prop_oneof![Just("default"), Just("work"), Just("primary")],
        )
            .prop_map(|(name, label)| Action::RewriteLegacyToLabel {
                name: name.to_string(),
                label: label.to_string(),
            }),
        // SetMeta
        meta_strategy().prop_map(|meta| Action::SetMeta { meta }),
        // AddForward
        forward_strategy().prop_map(|(key, value)| Action::AddForward { key, value }),
        // RemoveForward
        forward_strategy().prop_map(|(key, value)| Action::RemoveForward { key, value }),
        // SetStale
        (1_600_000_000u64..=1_900_000_000).prop_map(|ts| Action::SetStale { timestamp: ts }),
        // ClearStale
        Just(Action::ClearStale),
        // Repair
        Just(Action::RepairGroupComments),
        Just(Action::RemoveOrphanedHeaders),
    ]
}

/// Apply an action to a config. Returns an optional undo element for later undo.
fn apply_action(config: &mut SshConfigFile, action: &Action) {
    let entries = config.host_entries();
    let first_alias = entries.first().map(|e| e.alias.clone());

    match action {
        Action::AddHost {
            alias,
            hostname,
            user,
        } => {
            if !config.has_host(alias) {
                config.add_host(&host_entry(alias, hostname, user));
            }
        }
        Action::UpdateHost { hostname, user } => {
            if let Some(alias) = &first_alias {
                config.update_host(
                    alias,
                    &HostEntry {
                        alias: alias.clone(),
                        hostname: hostname.clone(),
                        user: user.clone(),
                        port: 22,
                        ..Default::default()
                    },
                );
            }
        }
        Action::DeleteFirst => {
            if let Some(alias) = &first_alias {
                config.delete_host(alias);
            }
        }
        Action::DeleteUndoFirst => {
            if let Some(alias) = &first_alias {
                if let Some((element, position)) = config.delete_host_undoable(alias) {
                    config.insert_host_at(element, position);
                }
            }
        }
        Action::SwapFirstTwo => {
            if entries.len() >= 2 {
                let a = entries[0].alias.clone();
                let b = entries[1].alias.clone();
                config.swap_hosts(&a, &b);
            }
        }
        Action::SetTags { tags } => {
            if let Some(alias) = &first_alias {
                config.set_host_tags(alias, tags);
            }
        }
        Action::SetAskpass { source } => {
            if let Some(alias) = &first_alias {
                config.set_host_askpass(alias, source);
            }
        }
        Action::SetProvider { name, id } => {
            if let Some(alias) = &first_alias {
                config.set_host_provider_id(alias, &ProviderConfigId::bare(name), id);
            }
        }
        Action::SetProviderLabeled { name, label, id } => {
            if let Some(alias) = &first_alias {
                config.set_host_provider_id(alias, &ProviderConfigId::labeled(name, label), id);
            }
        }
        Action::RewriteLegacyToLabel { name, label } => {
            // Lazy-migration helper. Must round-trip cleanly.
            let _ = config.rewrite_legacy_markers_to_label(name, label);
        }
        Action::SetMeta { meta } => {
            if let Some(alias) = &first_alias {
                config.set_host_meta(alias, meta);
            }
        }
        Action::AddForward { key, value } => {
            if let Some(alias) = &first_alias {
                config.add_forward(alias, key, value);
            }
        }
        Action::RemoveForward { key, value } => {
            if let Some(alias) = &first_alias {
                config.remove_forward(alias, key, value);
            }
        }
        Action::SetStale { timestamp } => {
            if let Some(alias) = &first_alias {
                config.set_host_stale(alias, *timestamp);
            }
        }
        Action::ClearStale => {
            if let Some(alias) = &first_alias {
                config.clear_host_stale(alias);
            }
        }
        Action::RepairGroupComments => {
            config.repair_absorbed_group_comments();
        }
        Action::RemoveOrphanedHeaders => {
            config.remove_all_orphaned_group_headers();
        }
    }
}

// ---------------------------------------------------------------------------
// Property: random action sequences never corrupt the config
// ---------------------------------------------------------------------------

proptest! {
    #![proptest_config(ProptestConfig::with_cases(300))]

    /// Apply 5-30 random actions to a fresh config. After each action,
    /// verify round-trip idempotency and no directive leaks.
    #[test]
    fn random_action_sequence(
        actions in prop::collection::vec(action_strategy(), 5..=30),
    ) {
        let mut config = parse_str("");

        for action in &actions {
            apply_action(&mut config, action);
            assert_idempotent(&config);
        }

        // Final deep check
        assert_no_directive_leak(&config);
    }

    /// Start with a populated config and apply random actions.
    #[test]
    fn random_actions_on_populated_config(
        actions in prop::collection::vec(action_strategy(), 5..=20),
    ) {
        let initial = "\
Host alpha
  HostName 10.0.0.1
  User admin
  IdentityFile ~/.ssh/id_ed25519
  # purple:tags prod,us-east
  # purple:provider aws:i-abc123
  # purple:meta region=us-east-1,status=running
  # purple:askpass keychain
  LocalForward 8080 localhost:80

Host beta
  HostName 10.0.0.2
  User deploy
  DynamicForward 1080
  # purple:tags staging

Host gamma
  HostName 10.0.0.3
  User root
  RemoteForward 9090 localhost:9090
  # purple:provider hetzner:srv-456
  # purple:stale 1700000000
  # purple:askpass op://Servers/gamma/password

Host *
  ServerAliveInterval 60
  ServerAliveCountMax 3
";
        let mut config = parse_str(initial);

        for action in &actions {
            apply_action(&mut config, action);
            assert_idempotent(&config);
        }

        assert_no_directive_leak(&config);
    }
}

// ---------------------------------------------------------------------------
// Property: full lifecycle (add -> annotate -> update -> tunnel -> delete -> undo)
// ---------------------------------------------------------------------------

proptest! {
    #![proptest_config(ProptestConfig::with_cases(200))]

    #[test]
    fn full_host_lifecycle(
        alias in alias_strategy(),
        hostname in hostname_strategy(),
        user in user_strategy(),
        tags in tags_strategy(),
        askpass in askpass_strategy(),
        meta in meta_strategy(),
        forward in forward_strategy(),
        new_hostname in hostname_strategy(),
        new_user in user_strategy(),
    ) {
        let mut config = parse_str("Host existing\n  HostName 1.2.3.4\n  User old\n");

        if config.has_host(&alias) {
            return Ok(());
        }

        // 1. Add host
        config.add_host(&host_entry(&alias, &hostname, &user));
        assert_idempotent(&config);
        prop_assert!(config.has_host(&alias));

        // 2. Set tags
        config.set_host_tags(&alias, &tags);
        assert_idempotent(&config);
        let entry = config.host_entries().into_iter().find(|e| e.alias == alias).unwrap();
        prop_assert_eq!(&entry.tags, &tags);

        // 3. Set provider
        config.set_host_provider_id(&alias, &ProviderConfigId::bare( "aws"),  "i-test123");
        assert_idempotent(&config);
        let entry = config.host_entries().into_iter().find(|e| e.alias == alias).unwrap();
        prop_assert_eq!(entry.provider.as_deref(), Some("aws"));

        // 4. Set meta
        config.set_host_meta(&alias, &meta);
        assert_idempotent(&config);

        // 4b. Set stale, verify, clear, verify
        config.set_host_stale(&alias, 1700000000);
        assert_idempotent(&config);
        let entry = config.host_entries().into_iter().find(|e| e.alias == alias).unwrap();
        prop_assert!(entry.stale.is_some(), "stale should be Some after set");

        config.clear_host_stale(&alias);
        assert_idempotent(&config);
        let entry = config.host_entries().into_iter().find(|e| e.alias == alias).unwrap();
        prop_assert!(entry.stale.is_none(), "stale should be None after clear");

        // 5. Set askpass
        config.set_host_askpass(&alias, &askpass);
        assert_idempotent(&config);

        // 6. Add forward
        config.add_forward(&alias, &forward.0, &forward.1);
        assert_idempotent(&config);

        // 7. Update host (should preserve all annotations)
        config.update_host(
            &alias,
            &HostEntry {
                alias: alias.clone(),
                hostname: new_hostname.clone(),
                user: new_user.clone(),
                port: 22,
                ..Default::default()
            },
        );
        assert_idempotent(&config);

        // Verify annotations survived update
        let entry = config.host_entries().into_iter().find(|e| e.alias == alias).unwrap();
        prop_assert_eq!(&entry.hostname, &new_hostname);
        prop_assert_eq!(&entry.user, &new_user);
        prop_assert_eq!(&entry.tags, &tags);
        prop_assert_eq!(entry.provider.as_deref(), Some("aws"));
        prop_assert!(entry.tunnel_count >= 1);

        // 8. Remove forward
        config.remove_forward(&alias, &forward.0, &forward.1);
        assert_idempotent(&config);

        // 9. Delete undoable
        let count_before = config.host_entries().len();
        let undo = config.delete_host_undoable(&alias);
        prop_assert!(undo.is_some());
        prop_assert_eq!(config.host_entries().len(), count_before - 1);

        // 10. Undo
        let (element, position) = undo.unwrap();
        config.insert_host_at(element, position);
        assert_idempotent(&config);
        prop_assert!(config.has_host(&alias));

        // 11. Final delete
        config.delete_host(&alias);
        assert_idempotent(&config);
        prop_assert!(!config.has_host(&alias));

        // Original host should still be there
        prop_assert!(config.has_host("existing"));

        // Full structural check
        assert_no_directive_leak(&config);
    }
}

// ---------------------------------------------------------------------------
// Property: multiple provider syncs don't corrupt each other
// ---------------------------------------------------------------------------

proptest! {
    #![proptest_config(ProptestConfig::with_cases(100))]

    /// Simulate two providers syncing hosts into the same config.
    #[test]
    fn dual_provider_sync_no_corruption(
        aws_hosts in prop::collection::vec(
            (alias_strategy(), hostname_strategy()),
            1..=8,
        ),
        do_hosts in prop::collection::vec(
            (alias_strategy(), hostname_strategy()),
            1..=8,
        ),
    ) {
        let mut config = parse_str("");

        // Simulate AWS sync: add hosts with provider markers
        for (name, ip) in &aws_hosts {
            let alias = format!("aws-{name}");
            if config.has_host(&alias) {
                continue;
            }
            config.add_host(&host_entry(&alias, ip, "ec2-user"));
            config.set_host_provider_id(&alias, &ProviderConfigId::bare( "aws"),  &format!("i-{name}"));
            config.set_host_tags(&alias, &["aws".to_string(), "cloud".to_string()]);
            config.set_host_meta(
                &alias,
                &[
                    ("region".to_string(), "us-east-1".to_string()),
                    ("status".to_string(), "running".to_string()),
                ],
            );
        }

        assert_idempotent(&config);

        // Simulate DO sync: add hosts with provider markers
        for (name, ip) in &do_hosts {
            let alias = format!("do-{name}");
            if config.has_host(&alias) {
                continue;
            }
            config.add_host(&host_entry(&alias, ip, "root"));
            config.set_host_provider_id(&alias, &ProviderConfigId::bare( "digitalocean"),  &format!("droplet-{name}"));
            config.set_host_tags(&alias, &["do".to_string()]);
        }

        assert_idempotent(&config);

        // Verify both providers coexist
        let entries = config.host_entries();
        let _aws_count = entries.iter().filter(|e| e.provider.as_deref() == Some("aws")).count();
        let _do_count = entries
            .iter()
            .filter(|e| e.provider.as_deref() == Some("digitalocean"))
            .count();

        // Each provider's hosts should be independently correct
        for entry in &entries {
            if entry.provider.as_deref() == Some("aws") {
                prop_assert_eq!(&entry.user, "ec2-user");
                prop_assert!(entry.tags.contains(&"aws".to_string()));
            }
            if entry.provider.as_deref() == Some("digitalocean") {
                prop_assert_eq!(&entry.user, "root");
                prop_assert!(entry.tags.contains(&"do".to_string()));
            }
        }

        // Now update all AWS hosts (simulate IP change on re-sync)
        for entry in entries.iter().filter(|e| e.provider.as_deref() == Some("aws")) {
            config.update_host(
                &entry.alias,
                &HostEntry {
                    alias: entry.alias.clone(),
                    hostname: "10.99.99.99".to_string(),
                    user: "ec2-user".to_string(),
                    port: 22,
                    ..Default::default()
                },
            );
        }

        assert_idempotent(&config);

        // DO hosts should be untouched
        let entries = config.host_entries();
        for entry in entries.iter().filter(|e| e.provider.as_deref() == Some("digitalocean")) {
            prop_assert_ne!(&entry.hostname, "10.99.99.99");
            prop_assert!(entry.tags.contains(&"do".to_string()));
        }

        // Deep check
        assert_no_directive_leak(&config);
    }
}

// ---------------------------------------------------------------------------
// Property: interleaved add/delete cycles don't accumulate blank lines
// ---------------------------------------------------------------------------

proptest! {
    #![proptest_config(ProptestConfig::with_cases(200))]

    #[test]
    fn no_blank_line_accumulation(
        cycles in 5u32..=50,
    ) {
        let mut config = parse_str("Host permanent\n  HostName 1.1.1.1\n  User root\n");

        for i in 0..cycles {
            let alias = format!("temp-{i}");
            config.add_host(&host_entry(&alias, "2.2.2.2", "test"));
            config.set_host_tags(&alias, &["temp".to_string()]);
            config.delete_host(&alias);
        }

        let serialized = config.serialize();

        // No triple blank lines should ever appear
        prop_assert!(
            !serialized.contains("\n\n\n"),
            "Triple blank line found after {} add/delete cycles:\n{}",
            cycles,
            &serialized[..serialized.len().min(500)],
        );

        assert_idempotent(&config);
        prop_assert!(config.has_host("permanent"));
    }
}

// ---------------------------------------------------------------------------
// Property: rapid tag/meta/askpass changes don't corrupt other annotations
// ---------------------------------------------------------------------------

proptest! {
    #![proptest_config(ProptestConfig::with_cases(200))]

    #[test]
    fn rapid_annotation_changes(
        tag_sets in prop::collection::vec(tags_strategy(), 3..=10),
        askpass_values in prop::collection::vec(askpass_strategy(), 3..=10),
        meta_sets in prop::collection::vec(meta_strategy(), 3..=10),
    ) {
        let mut config = parse_str("\
Host target
  HostName 10.0.0.1
  User admin
  IdentityFile ~/.ssh/key
  LocalForward 8080 localhost:80
  # purple:tags initial
  # purple:provider aws:i-initial
  # purple:meta region=us-east-1
  # purple:askpass keychain
");

        let max_rounds = tag_sets.len().min(askpass_values.len()).min(meta_sets.len());

        for i in 0..max_rounds {
            config.set_host_tags("target", &tag_sets[i]);
            config.set_host_askpass("target", &askpass_values[i]);
            config.set_host_meta("target", &meta_sets[i]);
            config.set_host_stale("target", i as u64);
            assert_idempotent(&config);
        }

        // Clear stale after the loop
        config.clear_host_stale("target");
        assert_idempotent(&config);

        // Verify final state
        let entries = config.host_entries();
        let entry = entries.iter().find(|e| e.alias == "target").unwrap();
        prop_assert_eq!(&entry.hostname, "10.0.0.1");
        prop_assert_eq!(&entry.user, "admin");
        prop_assert_eq!(entry.tunnel_count, 1);
        prop_assert_eq!(entry.provider.as_deref(), Some("aws"));

        // Tags should match last set
        let last_tags = &tag_sets[max_rounds - 1];
        prop_assert_eq!(&entry.tags, last_tags);

        assert_no_directive_leak(&config);
    }
}

// ---------------------------------------------------------------------------
// Property: concurrent-style undo stack stress
// ---------------------------------------------------------------------------

proptest! {
    #![proptest_config(ProptestConfig::with_cases(100))]

    #[test]
    fn undo_stack_stress(
        host_count in 5u32..=20,
        delete_indices in prop::collection::vec(0usize..20, 3..=10),
    ) {
        let mut config = parse_str("");
        let mut undo_stack: Vec<(purple_ssh::ssh_config::model::ConfigElement, usize)> = Vec::new();

        // Add hosts
        for i in 0..host_count {
            let alias = format!("host-{i}");
            config.add_host(&host_entry(&alias, &format!("10.0.0.{}", i + 1), "user"));
            config.set_host_tags(&alias, &[format!("group-{}", i % 3)]);
        }

        assert_idempotent(&config);

        // Delete some hosts (undoable)
        for &idx in &delete_indices {
            let entries = config.host_entries();
            if entries.is_empty() {
                break;
            }
            let target_idx = idx % entries.len();
            let alias = entries[target_idx].alias.clone();
            if let Some(undo) = config.delete_host_undoable(&alias) {
                undo_stack.push(undo);
            }
            assert_idempotent(&config);
        }

        // Undo all deletes in reverse order
        while let Some((element, position)) = undo_stack.pop() {
            config.insert_host_at(element, position);
            assert_idempotent(&config);
        }

        // All original hosts should be back
        let entries = config.host_entries();
        for i in 0..host_count {
            let alias = format!("host-{i}");
            prop_assert!(
                entries.iter().any(|e| e.alias == alias),
                "Host '{}' lost after undo cycle",
                alias,
            );
        }

        assert_no_directive_leak(&config);
    }
}

// ---------------------------------------------------------------------------
// Property: mixed CRLF operations
// ---------------------------------------------------------------------------

proptest! {
    #![proptest_config(ProptestConfig::with_cases(100))]

    #[test]
    fn crlf_survives_all_mutations(
        actions in prop::collection::vec(action_strategy(), 5..=15),
    ) {
        let initial = "Host alpha\r\n  HostName 10.0.0.1\r\n  User admin\r\n\r\nHost beta\r\n  HostName 10.0.0.2\r\n  User root\r\n";
        let mut config = parse_str(initial);
        prop_assert!(config.crlf);

        for action in &actions {
            apply_action(&mut config, action);
            // CRLF flag should never change
            prop_assert!(config.crlf, "CRLF flag lost after action: {:?}", action);
            assert_idempotent(&config);
        }

        // All line endings should still be CRLF
        let serialized = config.serialize();
        for line in serialized.split('\n') {
            if !line.is_empty() && line != "\r" {
                // Non-empty lines should end with \r (before the \n we split on)
                // Empty lines are just \r\n which splits into ["", ""]
                if !line.ends_with('\r') && serialized.contains("\r\n") {
                    // This is fine - the last segment after final \n may be empty
                    if !line.trim().is_empty() {
                        prop_assert!(
                            line.ends_with('\r'),
                            "Line without CRLF found: {:?}",
                            line,
                        );
                    }
                }
            }
        }
    }
}