entangle-mirror 0.1.1

Easy setup for mirroring GitHub repos to Tangled.org in one command
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
//! Input sanitization and validation.
//!
//! All user-facing input passes through this module before being used anywhere.
//! Functions here are pure (no I/O, no side effects) so they are fully unit-testable offline.
//!
//! ## Order of operations (always): sanitize first, then validate.
//!
//! This matters. `"CyrusAE"` becomes `"cyrusae"` before the GitHub username
//! regex runs — the user gets a clean success, not a confusing "uppercase not
//! allowed" error. Every public function in this module sanitizes before it validates.
//!
//! ## Error message style
//!
//! All validation errors follow the pattern:
//!   `"'<input>' is not a valid <field>: <reason>."`
//!
//! The offending input is always included so the user knows exactly what was rejected.
//! The reason is always included so they know what to fix.

// ---------------------------------------------------------------------------
// ValidationError
// ---------------------------------------------------------------------------

/// Errors produced by sanitization or validation.
///
/// The `input` field is the raw string the user provided (before sanitization),
/// included in all error messages so users can spot typos immediately.
#[derive(Debug, PartialEq, Eq)]
pub enum ValidationError {
    /// Input contains a shell metacharacter or space — rejected loudly.
    ///
    /// This is intentionally a hard error (not a silent strip) so the user
    /// knows something unexpected is in their input. The offending character
    /// is named in the message.
    DangerousCharacter {
        /// The character that triggered the rejection.
        ch: char,
    },

    /// Input failed the GitHub username rules.
    InvalidGithubUsername {
        /// The sanitized value that was tested.
        input: String,
        /// Human-readable reason (e.g. "must not start with a hyphen").
        reason: &'static str,
    },

    /// Input failed the ATProto handle rules.
    InvalidTangledUsername {
        /// The sanitized value that was tested.
        input: String,
        /// Human-readable reason.
        reason: &'static str,
    },

    /// Input failed the repository name rules.
    InvalidRepoName {
        /// The sanitized value that was tested.
        input: String,
        /// Human-readable reason.
        reason: &'static str,
    },
}

impl std::fmt::Display for ValidationError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ValidationError::DangerousCharacter { ch } => write!(
                f,
                "Input contains a disallowed character: '{}'. \
                 Shell metacharacters, spaces, and control characters are not allowed.",
                ch.escape_debug()
            ),
            ValidationError::InvalidGithubUsername { input, reason } => {
                write!(f, "'{input}' is not a valid GitHub username: {reason}.")
            }
            ValidationError::InvalidTangledUsername { input, reason } => {
                write!(f, "'{input}' is not a valid Tangled username: {reason}.")
            }
            ValidationError::InvalidRepoName { input, reason } => {
                write!(f, "'{input}' is not a valid repository name: {reason}.")
            }
        }
    }
}

impl std::error::Error for ValidationError {}

// ---------------------------------------------------------------------------
// Sanitization
// ---------------------------------------------------------------------------

/// Characters that are rejected loudly rather than stripped silently.
///
/// Includes spaces, all common shell metacharacters, and ASCII control
/// characters that could be used for git config injection (e.g. newlines).
/// We strip quotes silently (a user wrapping a value in quotes is being
/// overly careful but harmless), but anything in this list in a username
/// or repo name is almost certainly a mistake worth flagging.
///
/// Note: the specific validators (`validate_github_username` etc.) also
/// enforce an allowlist (`[a-z0-9-]`) that catches anything not in this
/// list. This list is the first line of defence and is intentionally
/// comprehensive so `sanitize` alone is a meaningful guard if reused.
const DANGEROUS_CHARS: &[char] = &[
    // Whitespace and separators
    ' ', '\t', '\n', '\r', // Shell expansion / substitution
    '$', '`', // Shell control flow
    ';', '|', '&', // Redirection
    '>', '<', // Globbing and pattern matching
    '*', '?', '[', ']', // Grouping
    '(', ')', '{', '}', // Path / escape characters
    '\\', '~',
];

/// Sanitize a raw user input string.
///
/// Steps applied in order:
/// 1. Strip leading and trailing whitespace.
/// 2. Remove single and double quotes silently.
/// 3. Reject any remaining character from [`DANGEROUS_CHARS`] with a loud error.
/// 4. Lowercase everything.
///
/// Returns the sanitized string on success, or [`ValidationError::DangerousCharacter`]
/// if a disallowed character is found.
///
/// # Why this order?
///
/// Stripping quotes before the metacharacter check means `"hello"` → `hello`
/// without triggering a false positive. Lowercasing last means the dangerous-char
/// check runs on the post-strip value, keeping the logic straightforward.
pub fn sanitize(input: &str) -> Result<String, ValidationError> {
    // Step 1: strip surrounding whitespace.
    let trimmed = input.trim();

    // Step 2: remove single and double quotes silently.
    // A user who typed `entangle set gh-user "cyrusae"` meant well.
    let dequoted: String = trimmed
        .chars()
        .filter(|c| *c != '\'' && *c != '"')
        .collect();

    // Step 3: reject dangerous characters loudly.
    // We check after quote removal so bare quotes don't accidentally mask
    // something suspicious — but in practice quotes are the only thing we strip.
    for ch in dequoted.chars() {
        if DANGEROUS_CHARS.contains(&ch) {
            return Err(ValidationError::DangerousCharacter { ch });
        }
    }

    // Step 4: lowercase.
    Ok(dequoted.to_lowercase())
}

// ---------------------------------------------------------------------------
// GitHub username validation
// ---------------------------------------------------------------------------

/// Validate a GitHub username.
///
/// Sanitizes first, then enforces:
/// - 1–39 characters
/// - Only alphanumeric characters or hyphens (`[a-z0-9-]` after lowercasing)
/// - Must not start with a hyphen
/// - Must not end with a hyphen
/// - Must not contain consecutive hyphens (`--`)
///
/// Returns the sanitized, validated username on success.
pub fn validate_github_username(input: &str) -> Result<String, ValidationError> {
    let s = sanitize(input)?;

    let err = |reason| {
        Err(ValidationError::InvalidGithubUsername {
            input: s.clone(),
            reason,
        })
    };

    if s.is_empty() {
        return err("must not be empty");
    }
    if s.len() > 39 {
        return err("must be 39 characters or fewer");
    }
    if s.starts_with('-') {
        return err("must not start with a hyphen");
    }
    if s.ends_with('-') {
        return err("must not end with a hyphen");
    }
    if s.contains("--") {
        return err("must not contain consecutive hyphens");
    }
    if !s.chars().all(|c| c.is_ascii_alphanumeric() || c == '-') {
        return err("may only contain alphanumeric characters and hyphens");
    }

    Ok(s)
}

// ---------------------------------------------------------------------------
// Tangled (ATProto) username validation
// ---------------------------------------------------------------------------

/// Validate a Tangled username (an ATProto handle).
///
/// Sanitizes first, then validates against the ATProto handle regex:
/// `/^([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]([a-zA-Z]{0,61}[a-zA-Z])?$/`
///
/// In plain terms:
/// - Must contain at least one dot (i.e. have at least two labels)
/// - Each label: starts and ends with alphanumeric, hyphens allowed in the middle
/// - The final label (TLD) must be letters only
/// - No underscores anywhere
///
/// Returns the sanitized, validated username on success.
///
/// # Examples
/// - `atdot.fyi` — valid
/// - `user.tngl.sh` — valid (subdomain handle)
/// - `a.b` — valid (minimal)
/// - `nodot` — invalid (no TLD)
/// - `has_under.score` — invalid (underscore)
pub fn validate_tangled_username(input: &str) -> Result<String, ValidationError> {
    let s = sanitize(input)?;

    let err = |reason| {
        Err(ValidationError::InvalidTangledUsername {
            input: s.clone(),
            reason,
        })
    };

    if s.is_empty() {
        return err("must not be empty");
    }

    // ATProto handle regex (case-insensitive — we've already lowercased, but
    // the regex allows upper too so it works either way).
    //
    // We validate manually rather than pulling in the `regex` crate to keep
    // compile times down and keep this module dependency-free.
    //
    // Rules encoded below:
    //   - Must have at least two dot-separated labels
    //   - Non-final labels: [a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?
    //     (1–63 chars, alphanumeric + interior hyphens, no leading/trailing hyphen)
    //   - Final label (TLD): [a-z]([a-z]{0,61}[a-z])? — letters only, 1–63 chars
    //   - No underscores (already caught by sanitize? No — underscores are not in
    //     DANGEROUS_CHARS because they're legal in some contexts. Catch them here.)

    if s.contains('_') {
        return err("underscores are not allowed in ATProto handles");
    }

    let labels: Vec<&str> = s.split('.').collect();

    if labels.len() < 2 {
        return err("must contain at least one dot (e.g. 'user.bsky.social')");
    }

    // Validate non-final labels.
    let (tld, non_tld_labels) = labels.split_last().unwrap();

    for label in non_tld_labels {
        validate_atproto_non_tld_label(label).map_err(|reason| {
            ValidationError::InvalidTangledUsername {
                input: s.clone(),
                reason,
            }
        })?;
    }

    // Validate the TLD — letters only.
    validate_atproto_tld_label(tld).map_err(|reason| ValidationError::InvalidTangledUsername {
        input: s.clone(),
        reason,
    })?;

    Ok(s)
}

/// Validate a single non-TLD ATProto label (e.g. `atdot` in `atdot.fyi`).
///
/// Rules: 1–63 chars, `[a-z0-9]` with interior hyphens allowed,
/// no leading or trailing hyphen.
fn validate_atproto_non_tld_label(label: &str) -> Result<(), &'static str> {
    if label.is_empty() {
        return Err("each part of the handle must not be empty (check for double dots)");
    }
    if label.len() > 63 {
        return Err("each part of the handle must be 63 characters or fewer");
    }
    if label.starts_with('-') {
        return Err("each part of the handle must not start with a hyphen");
    }
    if label.ends_with('-') {
        return Err("each part of the handle must not end with a hyphen");
    }
    if !label.chars().all(|c| c.is_ascii_alphanumeric() || c == '-') {
        return Err("each part of the handle may only contain alphanumeric characters and hyphens");
    }
    Ok(())
}

/// Validate the TLD label of an ATProto handle (e.g. `fyi` in `atdot.fyi`).
///
/// Rules: 1–63 chars, letters only (`[a-z]`), no hyphens, no digits.
fn validate_atproto_tld_label(tld: &str) -> Result<(), &'static str> {
    if tld.is_empty() {
        return Err("must end with a valid TLD (e.g. '.fyi', '.social', '.sh')");
    }
    if tld.len() > 63 {
        return Err("TLD must be 63 characters or fewer");
    }
    if !tld.chars().all(|c| c.is_ascii_alphabetic()) {
        return Err("TLD must contain only letters (e.g. '.fyi', '.social')");
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// Repository name validation
// ---------------------------------------------------------------------------

/// Validate a repository name.
///
/// Sanitizes first, then enforces the most restrictive superset of GitHub and
/// Tangled naming rules (GitHub allows periods; we don't, to avoid needing
/// special-case handling for names that are legal on one forge but not the other):
///
/// - 1–100 characters
/// - Lowercase alphanumeric characters and hyphens only (no periods, underscores, spaces)
/// - Must not start with a hyphen
/// - Must not end with a hyphen
/// - Must not contain consecutive hyphens (`--`)
///
/// Returns the sanitized, validated name on success.
pub fn validate_repo_name(input: &str) -> Result<String, ValidationError> {
    let s = sanitize(input)?;

    let err = |reason| {
        Err(ValidationError::InvalidRepoName {
            input: s.clone(),
            reason,
        })
    };

    if s.is_empty() {
        return err("must not be empty");
    }
    if s.len() > 100 {
        return err("must be 100 characters or fewer");
    }
    if s.starts_with('-') {
        return err("must not start with a hyphen");
    }
    if s.ends_with('-') {
        return err("must not end with a hyphen");
    }
    if s.contains("--") {
        return err("must not contain consecutive hyphens");
    }
    if s.contains('.') {
        return err("periods are not allowed (use hyphens instead)");
    }
    if !s.chars().all(|c| c.is_ascii_alphanumeric() || c == '-') {
        return err("may only contain alphanumeric characters and hyphens");
    }

    Ok(s)
}

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

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

    // ────────────────────────────────────────────────────────────────────────
    // Sanitization
    // ────────────────────────────────────────────────────────────────────────

    #[test]
    fn sanitize_lowercases() {
        assert_eq!(sanitize("CyrusAE").unwrap(), "cyrusae");
    }

    #[test]
    fn sanitize_strips_surrounding_whitespace() {
        assert_eq!(sanitize("  hello  ").unwrap(), "hello");
    }

    #[test]
    fn sanitize_removes_double_quotes() {
        assert_eq!(sanitize(r#""cyrusae""#).unwrap(), "cyrusae");
    }

    #[test]
    fn sanitize_removes_single_quotes() {
        assert_eq!(sanitize("'cyrusae'").unwrap(), "cyrusae");
    }

    #[test]
    fn sanitize_removes_mixed_quotes() {
        assert_eq!(sanitize(r#"'cy"rus'ae"#).unwrap(), "cyrusae");
    }

    #[test]
    fn sanitize_rejects_space() {
        let err = sanitize("hello world").unwrap_err();
        assert!(matches!(
            err,
            ValidationError::DangerousCharacter { ch: ' ' }
        ));
    }

    #[test]
    fn sanitize_rejects_backtick() {
        let err = sanitize("hello`world").unwrap_err();
        assert!(matches!(
            err,
            ValidationError::DangerousCharacter { ch: '`' }
        ));
    }

    #[test]
    fn sanitize_rejects_dollar_sign() {
        let err = sanitize("$VAR").unwrap_err();
        assert!(matches!(
            err,
            ValidationError::DangerousCharacter { ch: '$' }
        ));
    }

    #[test]
    fn sanitize_rejects_semicolon() {
        let err = sanitize("foo;bar").unwrap_err();
        assert!(matches!(
            err,
            ValidationError::DangerousCharacter { ch: ';' }
        ));
    }

    #[test]
    fn sanitize_rejects_pipe() {
        let err = sanitize("foo|bar").unwrap_err();
        assert!(matches!(
            err,
            ValidationError::DangerousCharacter { ch: '|' }
        ));
    }

    #[test]
    fn sanitize_rejects_ampersand() {
        let err = sanitize("foo&bar").unwrap_err();
        assert!(matches!(
            err,
            ValidationError::DangerousCharacter { ch: '&' }
        ));
    }

    #[test]
    fn sanitize_rejects_gt() {
        let err = sanitize("foo>bar").unwrap_err();
        assert!(matches!(
            err,
            ValidationError::DangerousCharacter { ch: '>' }
        ));
    }

    #[test]
    fn sanitize_rejects_lt() {
        let err = sanitize("foo<bar").unwrap_err();
        assert!(matches!(
            err,
            ValidationError::DangerousCharacter { ch: '<' }
        ));
    }

    #[test]
    fn sanitize_rejects_newline() {
        let err = sanitize("foo\nbar").unwrap_err();
        assert!(matches!(
            err,
            ValidationError::DangerousCharacter { ch: '\n' }
        ));
    }

    #[test]
    fn sanitize_rejects_carriage_return() {
        let err = sanitize("foo\rbar").unwrap_err();
        assert!(matches!(
            err,
            ValidationError::DangerousCharacter { ch: '\r' }
        ));
    }

    #[test]
    fn sanitize_rejects_asterisk() {
        let err = sanitize("foo*bar").unwrap_err();
        assert!(matches!(
            err,
            ValidationError::DangerousCharacter { ch: '*' }
        ));
    }

    #[test]
    fn sanitize_rejects_backslash() {
        let err = sanitize("foo\\bar").unwrap_err();
        assert!(matches!(
            err,
            ValidationError::DangerousCharacter { ch: '\\' }
        ));
    }

    #[test]
    fn sanitize_rejects_tilde() {
        let err = sanitize("~foo").unwrap_err();
        assert!(matches!(
            err,
            ValidationError::DangerousCharacter { ch: '~' }
        ));
    }

    #[test]
    fn dangerous_char_display_uses_escape_debug_for_newline() {
        let err = ValidationError::DangerousCharacter { ch: '\n' };
        let msg = err.to_string();
        // escape_debug renders '\n' as the two-character sequence \n, not a raw newline.
        assert!(
            msg.contains("\\n"),
            "newline must be rendered as \\n in error message, got: {msg}"
        );
        assert!(
            !msg.contains('\n'),
            "raw newline must not appear in error message: {msg}"
        );
    }

    // ────────────────────────────────────────────────────────────────────────
    // GitHub username validation
    // ────────────────────────────────────────────────────────────────────────

    #[test]
    fn github_valid_simple() {
        assert_eq!(validate_github_username("cyrusae").unwrap(), "cyrusae");
    }

    #[test]
    fn github_valid_with_hyphen() {
        assert_eq!(validate_github_username("cyrus-ae").unwrap(), "cyrus-ae");
    }

    #[test]
    fn github_valid_at_exactly_39_chars() {
        // 39 'a's — should pass.
        let name = "a".repeat(39);
        assert_eq!(validate_github_username(&name).unwrap(), name);
    }

    #[test]
    fn github_invalid_at_40_chars() {
        // 40 chars — must fail.
        let name = "a".repeat(40);
        let err = validate_github_username(&name).unwrap_err();
        assert!(matches!(err, ValidationError::InvalidGithubUsername { .. }));
    }

    #[test]
    fn github_uppercase_is_lowercased_before_validation() {
        // "CyrusAE" sanitizes to "cyrusae" — must succeed, not fail on uppercase.
        assert_eq!(validate_github_username("CyrusAE").unwrap(), "cyrusae");
    }

    #[test]
    fn github_quoted_input_is_dequoted() {
        assert_eq!(validate_github_username(r#""cyrusae""#).unwrap(), "cyrusae");
    }

    #[test]
    fn github_leading_hyphen_invalid() {
        let err = validate_github_username("-cyrusae").unwrap_err();
        assert!(matches!(err, ValidationError::InvalidGithubUsername { .. }));
    }

    #[test]
    fn github_trailing_hyphen_invalid() {
        let err = validate_github_username("cyrusae-").unwrap_err();
        assert!(matches!(err, ValidationError::InvalidGithubUsername { .. }));
    }

    #[test]
    fn github_consecutive_hyphens_invalid() {
        let err = validate_github_username("cy--rusae").unwrap_err();
        assert!(matches!(err, ValidationError::InvalidGithubUsername { .. }));
    }

    #[test]
    fn github_underscore_invalid() {
        let err = validate_github_username("cyrus_ae").unwrap_err();
        assert!(matches!(err, ValidationError::InvalidGithubUsername { .. }));
    }

    #[test]
    fn github_empty_invalid() {
        let err = validate_github_username("").unwrap_err();
        assert!(matches!(err, ValidationError::InvalidGithubUsername { .. }));
    }

    #[test]
    fn github_single_char_valid() {
        assert_eq!(validate_github_username("a").unwrap(), "a");
    }

    // ────────────────────────────────────────────────────────────────────────
    // Tangled (ATProto) username validation
    // ────────────────────────────────────────────────────────────────────────

    #[test]
    fn tangled_valid_simple() {
        assert_eq!(validate_tangled_username("atdot.fyi").unwrap(), "atdot.fyi");
    }

    #[test]
    fn tangled_valid_subdomain() {
        assert_eq!(
            validate_tangled_username("user.tngl.sh").unwrap(),
            "user.tngl.sh"
        );
    }

    #[test]
    fn tangled_valid_minimal() {
        // Minimal valid ATProto handle: single-char label + single-char TLD.
        assert_eq!(validate_tangled_username("a.b").unwrap(), "a.b");
    }

    #[test]
    fn tangled_uppercase_lowercased_before_validation() {
        assert_eq!(validate_tangled_username("AtDot.FYI").unwrap(), "atdot.fyi");
    }

    #[test]
    fn tangled_no_dot_invalid() {
        let err = validate_tangled_username("nodot").unwrap_err();
        assert!(matches!(
            err,
            ValidationError::InvalidTangledUsername { .. }
        ));
    }

    #[test]
    fn tangled_underscore_invalid() {
        let err = validate_tangled_username("has_under.score").unwrap_err();
        assert!(matches!(
            err,
            ValidationError::InvalidTangledUsername { .. }
        ));
    }

    #[test]
    fn tangled_tld_with_digit_invalid() {
        // TLD must be letters only — digits not allowed.
        let err = validate_tangled_username("user.fyi2").unwrap_err();
        assert!(matches!(
            err,
            ValidationError::InvalidTangledUsername { .. }
        ));
    }

    #[test]
    fn tangled_label_leading_hyphen_invalid() {
        let err = validate_tangled_username("-user.fyi").unwrap_err();
        assert!(matches!(
            err,
            ValidationError::InvalidTangledUsername { .. }
        ));
    }

    #[test]
    fn tangled_label_trailing_hyphen_invalid() {
        let err = validate_tangled_username("user-.fyi").unwrap_err();
        assert!(matches!(
            err,
            ValidationError::InvalidTangledUsername { .. }
        ));
    }

    #[test]
    fn tangled_empty_label_from_double_dot_invalid() {
        let err = validate_tangled_username("user..fyi").unwrap_err();
        assert!(matches!(
            err,
            ValidationError::InvalidTangledUsername { .. }
        ));
    }

    #[test]
    fn tangled_empty_tld_invalid() {
        // Trailing dot → empty TLD.
        let err = validate_tangled_username("user.fyi.").unwrap_err();
        assert!(matches!(
            err,
            ValidationError::InvalidTangledUsername { .. }
        ));
    }

    #[test]
    fn tangled_empty_invalid() {
        let err = validate_tangled_username("").unwrap_err();
        assert!(matches!(
            err,
            ValidationError::InvalidTangledUsername { .. }
        ));
    }

    #[test]
    fn tangled_hyphen_in_middle_of_label_valid() {
        // Interior hyphens in non-TLD labels are fine.
        assert_eq!(
            validate_tangled_username("my-handle.bsky.social").unwrap(),
            "my-handle.bsky.social"
        );
    }

    #[test]
    fn tangled_numeric_subdomain_valid() {
        // Digits are allowed in non-TLD labels.
        assert_eq!(
            validate_tangled_username("user123.fyi").unwrap(),
            "user123.fyi"
        );
    }

    // ────────────────────────────────────────────────────────────────────────
    // Repository name validation
    // ────────────────────────────────────────────────────────────────────────

    #[test]
    fn repo_valid_simple() {
        assert_eq!(validate_repo_name("entangle").unwrap(), "entangle");
    }

    #[test]
    fn repo_valid_with_hyphen() {
        assert_eq!(validate_repo_name("my-project").unwrap(), "my-project");
    }

    #[test]
    fn repo_valid_single_char() {
        assert_eq!(validate_repo_name("a").unwrap(), "a");
    }

    #[test]
    fn repo_valid_at_exactly_100_chars() {
        let name = "a".repeat(100);
        assert_eq!(validate_repo_name(&name).unwrap(), name);
    }

    #[test]
    fn repo_invalid_at_101_chars() {
        let name = "a".repeat(101);
        let err = validate_repo_name(&name).unwrap_err();
        assert!(matches!(err, ValidationError::InvalidRepoName { .. }));
    }

    #[test]
    fn repo_uppercase_lowercased_before_validation() {
        assert_eq!(validate_repo_name("ENTANGLE").unwrap(), "entangle");
    }

    #[test]
    fn repo_leading_hyphen_invalid() {
        let err = validate_repo_name("-entangle").unwrap_err();
        assert!(matches!(err, ValidationError::InvalidRepoName { .. }));
    }

    #[test]
    fn repo_trailing_hyphen_invalid() {
        let err = validate_repo_name("entangle-").unwrap_err();
        assert!(matches!(err, ValidationError::InvalidRepoName { .. }));
    }

    #[test]
    fn repo_consecutive_hyphens_invalid() {
        let err = validate_repo_name("en--tangle").unwrap_err();
        assert!(matches!(err, ValidationError::InvalidRepoName { .. }));
    }

    #[test]
    fn repo_period_invalid() {
        // Periods are legal on GitHub but not on Tangled — we reject them.
        let err = validate_repo_name("my.project").unwrap_err();
        assert!(matches!(err, ValidationError::InvalidRepoName { .. }));
    }

    #[test]
    fn repo_underscore_invalid() {
        let err = validate_repo_name("my_project").unwrap_err();
        assert!(matches!(err, ValidationError::InvalidRepoName { .. }));
    }

    #[test]
    fn repo_empty_invalid() {
        let err = validate_repo_name("").unwrap_err();
        assert!(matches!(err, ValidationError::InvalidRepoName { .. }));
    }

    // ────────────────────────────────────────────────────────────────────────
    // Cross-cutting: sanitize-before-validate ordering
    // ────────────────────────────────────────────────────────────────────────

    #[test]
    fn github_sanitize_before_validate_mixed_case_passes() {
        // "CyrusAE" must pass (sanitized to "cyrusae" first), not fail with
        // "uppercase not allowed". This pins the sanitize-first contract.
        let result = validate_github_username("CyrusAE");
        assert_eq!(result.unwrap(), "cyrusae");
    }

    #[test]
    fn repo_sanitize_before_validate_mixed_case_passes() {
        let result = validate_repo_name("MyRepo");
        assert_eq!(result.unwrap(), "myrepo");
    }

    #[test]
    fn tangled_sanitize_before_validate_mixed_case_passes() {
        let result = validate_tangled_username("MyHandle.FYI");
        assert_eq!(result.unwrap(), "myhandle.fyi");
    }

    #[test]
    fn dangerous_char_propagates_through_github_validator() {
        // Shell metacharacter → DangerousCharacter, not InvalidGithubUsername.
        let err = validate_github_username("cyrus$ae").unwrap_err();
        assert!(matches!(
            err,
            ValidationError::DangerousCharacter { ch: '$' }
        ));
    }

    #[test]
    fn dangerous_char_propagates_through_repo_validator() {
        let err = validate_repo_name("my|repo").unwrap_err();
        assert!(matches!(
            err,
            ValidationError::DangerousCharacter { ch: '|' }
        ));
    }

    #[test]
    fn dangerous_char_propagates_through_tangled_validator() {
        let err = validate_tangled_username("user;name.fyi").unwrap_err();
        assert!(matches!(
            err,
            ValidationError::DangerousCharacter { ch: ';' }
        ));
    }
}