guts-compat 0.1.0

Git and GitHub compatibility layer for Guts code collaboration platform.
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
//! User account types and management.

use serde::{Deserialize, Serialize};
use std::time::{SystemTime, UNIX_EPOCH};

/// Unique identifier for a user.
pub type UserId = u64;

/// A user account in the system.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct User {
    /// Unique user ID.
    pub id: UserId,
    /// Unique username (lowercase, alphanumeric with hyphens).
    pub username: String,
    /// Display name (can contain any characters).
    pub display_name: Option<String>,
    /// Email address (optional).
    pub email: Option<String>,
    /// Short biography.
    pub bio: Option<String>,
    /// Location string.
    pub location: Option<String>,
    /// Personal website URL.
    pub website: Option<String>,
    /// Avatar URL.
    pub avatar_url: Option<String>,
    /// Ed25519 public key (hex-encoded identity).
    pub public_key: String,
    /// Whether the user's email is public.
    pub email_public: bool,
    /// Unix timestamp when created.
    pub created_at: u64,
    /// Unix timestamp when last updated.
    pub updated_at: u64,
}

impl User {
    /// Create a new user.
    pub fn new(id: UserId, username: String, public_key: String) -> Self {
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_secs();

        Self {
            id,
            username,
            display_name: None,
            email: None,
            bio: None,
            location: None,
            website: None,
            avatar_url: None,
            public_key,
            email_public: false,
            created_at: now,
            updated_at: now,
        }
    }

    /// Validate a username format.
    ///
    /// Usernames must:
    /// - Be 1-39 characters long
    /// - Start with an alphanumeric character
    /// - Contain only lowercase alphanumeric characters and hyphens
    /// - Not contain consecutive hyphens
    /// - Not end with a hyphen
    pub fn validate_username(username: &str) -> Result<(), String> {
        if username.is_empty() {
            return Err("username cannot be empty".to_string());
        }

        if username.len() > 39 {
            return Err("username must be 39 characters or less".to_string());
        }

        let chars: Vec<char> = username.chars().collect();

        // Must start with alphanumeric
        if !chars[0].is_ascii_alphanumeric() {
            return Err("username must start with a letter or number".to_string());
        }

        // Must end with alphanumeric
        if !chars.last().unwrap().is_ascii_alphanumeric() {
            return Err("username must end with a letter or number".to_string());
        }

        // Check each character
        for (i, c) in chars.iter().enumerate() {
            if !c.is_ascii_lowercase() && !c.is_ascii_digit() && *c != '-' {
                if c.is_ascii_uppercase() {
                    return Err("username must be lowercase".to_string());
                }
                return Err(format!("invalid character in username: {}", c));
            }

            // No consecutive hyphens
            if *c == '-' && i > 0 && chars[i - 1] == '-' {
                return Err("username cannot contain consecutive hyphens".to_string());
            }
        }

        // Reserved usernames
        let reserved = [
            "admin",
            "api",
            "git",
            "guts",
            "help",
            "login",
            "logout",
            "new",
            "organizations",
            "repos",
            "settings",
            "signup",
            "user",
            "users",
        ];
        if reserved.contains(&username) {
            return Err(format!("username '{}' is reserved", username));
        }

        Ok(())
    }

    /// Update the updated_at timestamp.
    pub fn touch(&mut self) {
        self.updated_at = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_secs();
    }

    /// Convert to a public profile (for API responses).
    pub fn to_profile(&self, public_repos: u64, followers: u64, following: u64) -> UserProfile {
        UserProfile {
            login: self.username.clone(),
            id: self.id,
            avatar_url: self.avatar_url.clone(),
            name: self.display_name.clone(),
            email: if self.email_public {
                self.email.clone()
            } else {
                None
            },
            bio: self.bio.clone(),
            location: self.location.clone(),
            blog: self.website.clone(),
            public_repos,
            followers,
            following,
            created_at: format_timestamp(self.created_at),
            updated_at: format_timestamp(self.updated_at),
        }
    }
}

/// User profile for public API responses (GitHub-compatible).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UserProfile {
    /// Username (GitHub calls this "login").
    pub login: String,
    /// User ID.
    pub id: u64,
    /// Avatar URL.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub avatar_url: Option<String>,
    /// Display name.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    /// Public email (if enabled).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub email: Option<String>,
    /// Biography.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub bio: Option<String>,
    /// Location.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub location: Option<String>,
    /// Website/blog URL.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub blog: Option<String>,
    /// Number of public repositories.
    pub public_repos: u64,
    /// Number of followers.
    pub followers: u64,
    /// Number of users following.
    pub following: u64,
    /// ISO 8601 creation timestamp.
    pub created_at: String,
    /// ISO 8601 last update timestamp.
    pub updated_at: String,
}

/// Request to create a new user.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreateUserRequest {
    /// Desired username.
    pub username: String,
    /// Ed25519 public key (hex-encoded).
    pub public_key: String,
    /// Optional email address.
    #[serde(default)]
    pub email: Option<String>,
    /// Optional display name.
    #[serde(default)]
    pub name: Option<String>,
}

/// Request to update a user profile.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct UpdateUserRequest {
    /// New display name.
    #[serde(default)]
    pub name: Option<String>,
    /// New email address.
    #[serde(default)]
    pub email: Option<String>,
    /// New biography.
    #[serde(default)]
    pub bio: Option<String>,
    /// New location.
    #[serde(default)]
    pub location: Option<String>,
    /// New website/blog URL.
    #[serde(default)]
    pub blog: Option<String>,
    /// Whether email is public.
    #[serde(default)]
    pub email_public: Option<bool>,
}

/// Format a Unix timestamp as ISO 8601.
fn format_timestamp(timestamp: u64) -> String {
    use std::fmt::Write;
    // Simple ISO 8601 format: 2024-01-15T12:00:00Z
    // For a proper implementation, use chrono or time crate
    let secs_per_day = 86400;
    let secs_per_hour = 3600;
    let secs_per_min = 60;

    // Days since epoch
    let mut days = timestamp / secs_per_day;
    let remaining = timestamp % secs_per_day;
    let hours = remaining / secs_per_hour;
    let remaining = remaining % secs_per_hour;
    let minutes = remaining / secs_per_min;
    let seconds = remaining % secs_per_min;

    // Calculate year/month/day (simplified, doesn't handle leap seconds)
    let mut year = 1970;
    loop {
        let days_in_year = if is_leap_year(year) { 366 } else { 365 };
        if days < days_in_year {
            break;
        }
        days -= days_in_year;
        year += 1;
    }

    let days_in_month = if is_leap_year(year) {
        [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    } else {
        [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    };

    let mut month = 0;
    for (i, &dim) in days_in_month.iter().enumerate() {
        if days < dim as u64 {
            month = i + 1;
            break;
        }
        days -= dim as u64;
    }
    let day = days + 1;

    let mut s = String::with_capacity(20);
    write!(
        s,
        "{:04}-{:02}-{:02}T{:02}:{:02}:{:02}Z",
        year, month, day, hours, minutes, seconds
    )
    .unwrap();
    s
}

fn is_leap_year(year: u64) -> bool {
    (year.is_multiple_of(4) && !year.is_multiple_of(100)) || year.is_multiple_of(400)
}

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

    #[test]
    fn test_validate_username_valid() {
        assert!(User::validate_username("alice").is_ok());
        assert!(User::validate_username("bob123").is_ok());
        assert!(User::validate_username("my-project").is_ok());
        assert!(User::validate_username("a").is_ok());
        assert!(User::validate_username("a1").is_ok());
    }

    #[test]
    fn test_validate_username_invalid() {
        assert!(User::validate_username("").is_err());
        assert!(User::validate_username("-alice").is_err());
        assert!(User::validate_username("alice-").is_err());
        assert!(User::validate_username("alice--bob").is_err());
        assert!(User::validate_username("Alice").is_err());
        assert!(User::validate_username("alice_bob").is_err());
        assert!(User::validate_username("admin").is_err());

        // Too long
        let long_name = "a".repeat(40);
        assert!(User::validate_username(&long_name).is_err());
    }

    #[test]
    fn test_validate_username_reserved() {
        // Test all reserved usernames
        let reserved = [
            "admin",
            "api",
            "git",
            "guts",
            "help",
            "login",
            "logout",
            "new",
            "organizations",
            "repos",
            "settings",
            "signup",
            "user",
            "users",
        ];
        for name in reserved {
            let result = User::validate_username(name);
            assert!(result.is_err(), "Expected '{}' to be reserved", name);
            assert!(
                result.unwrap_err().contains("reserved"),
                "Error should mention 'reserved'"
            );
        }
    }

    #[test]
    fn test_validate_username_max_length() {
        // 39 chars should be valid
        let max_valid = "a".repeat(39);
        assert!(User::validate_username(&max_valid).is_ok());

        // 40 chars should be invalid
        let too_long = "a".repeat(40);
        assert!(User::validate_username(&too_long).is_err());
    }

    #[test]
    fn test_validate_username_special_chars() {
        // Invalid special characters
        assert!(User::validate_username("user@name").is_err());
        assert!(User::validate_username("user.name").is_err());
        assert!(User::validate_username("user#name").is_err());
        assert!(User::validate_username("user$name").is_err());
        assert!(User::validate_username("user%name").is_err());
        assert!(User::validate_username("user name").is_err());
        assert!(User::validate_username("user\tname").is_err());
        assert!(User::validate_username("user\nname").is_err());
    }

    #[test]
    fn test_validate_username_unicode() {
        // Unicode should be rejected
        assert!(User::validate_username("αβγ").is_err());
        assert!(User::validate_username("日本語").is_err());
        assert!(User::validate_username("émoji").is_err());
        assert!(User::validate_username("user🔥").is_err());
    }

    #[test]
    fn test_validate_username_edge_cases() {
        // Single character at boundaries
        assert!(User::validate_username("0").is_ok()); // digit
        assert!(User::validate_username("z").is_ok()); // letter

        // Hyphen placement
        assert!(User::validate_username("a-b").is_ok());
        assert!(User::validate_username("a-b-c").is_ok());
        assert!(User::validate_username("-a").is_err()); // starts with hyphen
        assert!(User::validate_username("a-").is_err()); // ends with hyphen
        assert!(User::validate_username("a--b").is_err()); // consecutive hyphens
        assert!(User::validate_username("---").is_err()); // all hyphens
    }

    #[test]
    fn test_create_user() {
        let user = User::new(1, "alice".to_string(), "abc123".to_string());
        assert_eq!(user.id, 1);
        assert_eq!(user.username, "alice");
        assert_eq!(user.public_key, "abc123");
        assert!(user.display_name.is_none());
    }

    #[test]
    fn test_user_profile() {
        let mut user = User::new(1, "alice".to_string(), "abc123".to_string());
        user.display_name = Some("Alice Smith".to_string());
        user.email = Some("alice@example.com".to_string());
        user.email_public = true;

        let profile = user.to_profile(5, 10, 3);
        assert_eq!(profile.login, "alice");
        assert_eq!(profile.name, Some("Alice Smith".to_string()));
        assert_eq!(profile.email, Some("alice@example.com".to_string()));
        assert_eq!(profile.public_repos, 5);
        assert_eq!(profile.followers, 10);
        assert_eq!(profile.following, 3);
    }

    #[test]
    fn test_user_profile_private_email() {
        let mut user = User::new(1, "alice".to_string(), "abc123".to_string());
        user.email = Some("alice@example.com".to_string());
        user.email_public = false;

        let profile = user.to_profile(0, 0, 0);
        assert!(profile.email.is_none());
    }

    #[test]
    fn test_format_timestamp() {
        // 2024-01-01 00:00:00 UTC = 1704067200
        let ts = format_timestamp(1704067200);
        assert_eq!(ts, "2024-01-01T00:00:00Z");
    }

    #[test]
    fn test_format_timestamp_epoch() {
        let ts = format_timestamp(0);
        assert_eq!(ts, "1970-01-01T00:00:00Z");
    }

    #[test]
    fn test_format_timestamp_leap_year() {
        // Feb 29, 2024 12:00:00 UTC (2024 is a leap year)
        let ts = format_timestamp(1709208000);
        assert_eq!(ts, "2024-02-29T12:00:00Z");
    }

    #[test]
    fn test_is_leap_year() {
        assert!(is_leap_year(2000)); // divisible by 400
        assert!(!is_leap_year(1900)); // divisible by 100 but not 400
        assert!(is_leap_year(2024)); // divisible by 4 but not 100
        assert!(!is_leap_year(2023)); // not divisible by 4
    }
}

#[cfg(test)]
mod proptests {
    use super::*;
    use proptest::prelude::*;

    /// Strategy for generating valid usernames
    fn valid_username_strategy() -> impl Strategy<Value = String> {
        // Generate usernames with pattern: alphanumeric, optionally with single hyphens
        prop::collection::vec(
            prop_oneof![
                4 => prop::char::ranges(vec!['a'..='z', '0'..='9'].into_iter().collect()),
                1 => Just('-'),
            ],
            1..=39,
        )
        .prop_filter_map("filter valid usernames", |chars| {
            let s: String = chars.into_iter().collect();
            // Must start and end with alphanumeric, no consecutive hyphens
            if s.is_empty() {
                return None;
            }
            let chars: Vec<char> = s.chars().collect();
            if !chars[0].is_ascii_alphanumeric() {
                return None;
            }
            if !chars.last().unwrap().is_ascii_alphanumeric() {
                return None;
            }
            // Check for consecutive hyphens
            for i in 1..chars.len() {
                if chars[i] == '-' && chars[i - 1] == '-' {
                    return None;
                }
            }
            // Skip reserved names
            let reserved = [
                "admin",
                "api",
                "git",
                "guts",
                "help",
                "login",
                "logout",
                "new",
                "organizations",
                "repos",
                "settings",
                "signup",
                "user",
                "users",
            ];
            if reserved.contains(&s.as_str()) {
                return None;
            }
            Some(s)
        })
    }

    proptest! {
        /// Property: Valid usernames should always be accepted
        #[test]
        fn prop_valid_usernames_accepted(username in valid_username_strategy()) {
            prop_assert!(
                User::validate_username(&username).is_ok(),
                "Username '{}' should be valid", username
            );
        }

        /// Property: Empty strings are always rejected
        #[test]
        fn prop_empty_string_rejected(_seed in 0u32..1000) {
            prop_assert!(User::validate_username("").is_err());
        }

        /// Property: Strings > 39 chars are always rejected
        #[test]
        fn prop_long_usernames_rejected(len in 40usize..200) {
            let long_name: String = (0..len).map(|_| 'a').collect();
            prop_assert!(
                User::validate_username(&long_name).is_err(),
                "Username of length {} should be rejected", len
            );
        }

        /// Property: Uppercase letters are always rejected
        #[test]
        fn prop_uppercase_rejected(prefix in "[a-z]{0,5}", upper in "[A-Z]", suffix in "[a-z]{0,5}") {
            let username = format!("{}{}{}", prefix, upper, suffix);
            if !username.is_empty() && username.len() <= 39 {
                prop_assert!(User::validate_username(&username).is_err());
            }
        }

        /// Property: Starting with hyphen is rejected
        #[test]
        fn prop_hyphen_start_rejected(rest in "[a-z0-9-]{0,10}") {
            let username = format!("-{}", rest);
            prop_assert!(User::validate_username(&username).is_err());
        }

        /// Property: Ending with hyphen is rejected
        #[test]
        fn prop_hyphen_end_rejected(prefix in "[a-z0-9]{1,10}") {
            let username = format!("{}-", prefix);
            prop_assert!(User::validate_username(&username).is_err());
        }

        /// Property: Consecutive hyphens are rejected
        #[test]
        fn prop_consecutive_hyphens_rejected(prefix in "[a-z0-9]{1,5}", suffix in "[a-z0-9]{1,5}") {
            let username = format!("{}--{}", prefix, suffix);
            prop_assert!(User::validate_username(&username).is_err());
        }

        /// Property: Underscores are rejected
        #[test]
        fn prop_underscore_rejected(prefix in "[a-z]{1,5}", suffix in "[a-z]{1,5}") {
            let username = format!("{}_{}", prefix, suffix);
            prop_assert!(User::validate_username(&username).is_err());
        }

        /// Property: Spaces are rejected
        #[test]
        fn prop_space_rejected(prefix in "[a-z]{1,5}", suffix in "[a-z]{1,5}") {
            let username = format!("{} {}", prefix, suffix);
            prop_assert!(User::validate_username(&username).is_err());
        }

        /// Property: Arbitrary Unicode is rejected
        #[test]
        fn prop_unicode_rejected(s in "\\PC{1,10}") {
            // If the string contains any non-ASCII characters, it should be rejected
            if !s.is_ascii() {
                prop_assert!(User::validate_username(&s).is_err());
            }
        }

        /// Property: Validation is consistent (idempotent)
        #[test]
        fn prop_validation_consistent(s in ".*") {
            let result1 = User::validate_username(&s);
            let result2 = User::validate_username(&s);
            prop_assert_eq!(result1.is_ok(), result2.is_ok());
        }
    }
}