//! Every remote form, and what each one reads as. //! //! The scp form is the one every naive implementation gets wrong, which is why //! it is exercised hardest β€” four shapes rather than one. Three malformed //! remotes are pinned beside it, each with the answer this reader gives where //! a regex-shaped parser gives a worse one, so that behaviour stays a decision //! rather than an accident. use super::*; /// Cotton's own relay β€” what every household that never chose reads with. fn ours() -> RelayAddress { RelayAddress::cotton() } /// A household of that name on Cotton's relay. fn at(name: &str) -> HouseholdAddress { HouseholdAddress::new(RelayAddress::cotton(), Some(name)) } /// A household of that name on a relay of its own. fn on_its_own_relay(name: &str) -> HouseholdAddress { HouseholdAddress::new( RelayAddress::new("relay.example.org", "tunnel.example.org").expect("two hostnames"), Some(name), ) } fn parsed(raw: &str) -> RepoRef { parse_repo_ref(&ours(), raw).unwrap_or_else(|| panic!("{raw} did not parse")) } #[test] fn reads_the_stored_bare_form() { let found = parsed("github.com/decemberfm/cottondb"); assert_eq!(found.host, "github.com"); assert_eq!(found.owner, "decemberfm"); assert_eq!(found.repo, "cottondb"); assert_eq!(found.kind, CodeHostKind::Github); } #[test] fn anything_that_is_not_github_is_a_forgejo_instance() { // No allowlist on purpose: an instance the owner stands up tomorrow must // work without a code change. let found = parsed("git.example.org/decemberfm/cottondb"); assert_eq!(found.kind, CodeHostKind::Forgejo); assert_eq!(found.api_base_url(), "https://git.example.org/api/v1"); assert_eq!( parsed("github.com/a/b").api_base_url(), "https://api.github.com" ); // A host nobody has ever seen is a Forgejo instance, which is the whole // point of there being no allowlist. assert_eq!(parsed("git.example.org/a/b").kind, CodeHostKind::Forgejo); assert_eq!( parsed("github.com/decemberfm/cottondb").kind, CodeHostKind::Github ); } #[test] fn reads_an_https_url_with_and_without_the_git_suffix() { for raw in [ "https://git.example.org/decemberfm/cottondb", "https://git.example.org/decemberfm/cottondb.git", "https://git.example.org/decemberfm/cottondb.git/", "https://git.example.org/decemberfm/cottondb/", ] { let found = parsed(raw); assert_eq!( found.key(), "git.example.org/decemberfm/cottondb", "{raw}" ); } } #[test] fn reads_an_ssh_url_with_a_user_and_a_port() { let found = parsed("ssh://git@git.example.org:2222/decemberfm/cottondb.git"); assert_eq!(found.host, "git.example.org"); assert_eq!(found.owner, "decemberfm"); assert_eq!(found.repo, "cottondb"); } #[test] fn reads_the_scp_form() { let found = parsed("git@github.com:decemberfm/cottondb.git"); assert_eq!(found.key(), "github.com/decemberfm/cottondb"); assert_eq!( parsed("github.com:decemberfm/cottondb").key(), "github.com/decemberfm/cottondb" ); assert_eq!( parsed("git@github.com:/decemberfm/cottondb").key(), "github.com/decemberfm/cottondb" ); } #[test] fn a_port_after_a_colon_is_not_a_repository_name() { // `host:2222/owner/repo` is an ssh URL missing its scheme; reading it as // the scp form would name a repository "2222". let found = parse_repo_ref(&ours(), "git.example.org:2222/decemberfm/cottondb"); assert!(found.is_none(), "{found:?}"); } #[test] fn the_host_is_lower_cased_and_the_path_is_not() { // Both roads to `from_parts`: the bare stored form and a full URL. A // person types the first and pastes the second, and a host that lowercased // on only one of them would be the same repository under two keys. for raw in [ "GitHub.com/DecemberFM/CottonDB", "https://GitHub.com/DecemberFM/CottonDB", ] { let found = parsed(raw); assert_eq!(found.host, "github.com", "{raw}"); assert_eq!(found.owner, "DecemberFM", "{raw}"); assert_eq!(found.repo, "CottonDB", "{raw}"); assert_eq!(found.kind, CodeHostKind::Github, "{raw}"); } } /// A remote is free text an assistant wrote, and free text is not ASCII. /// /// `github.com/o/πŸ˜€ab` has an eight-byte path whose fourth byte from the end /// falls INSIDE the emoji. The `.git` check used to slice there, and a slice /// off a character boundary panics β€” across the FFI that is a process abort, /// which takes `/code` and all nine `/projects/[slug]/code/*` screens with it. /// The module's own promise is that it is silent about what it drops. #[test] fn a_remote_that_is_not_ascii_is_read_or_dropped_but_never_a_panic() { let found = parsed("github.com/o/πŸ˜€ab"); assert_eq!(found.host, "github.com"); assert_eq!(found.owner, "o"); assert_eq!(found.repo, "πŸ˜€ab"); // And the same shape everywhere else a path is read: the scp form, an // https URL, and a repository whose whole name is one character. assert_eq!(parsed("git@github.com:o/πŸ˜€ab").key(), "github.com/o/πŸ˜€ab"); assert_eq!( parsed("https://git.example.org/ΓΆ/nΓ©.git").key(), "git.example.org/ΓΆ/nΓ©" ); assert!(parse_repo_ref(&ours(), "πŸ˜€").is_none()); assert_eq!( project_repo_refs(["github.com/o/πŸ˜€ab"], &OwnCotton::default()).len(), 1 ); } /// Three malformed remotes a regex-shaped parser answers badly, kept as /// assertions so the better answer is a decision rather than an accident. #[test] fn a_malformed_remote_reads_better_here_than_a_regex_reads_it() { // An unparseable port must not push the SCHEME into the host slot. let found = parsed("https://host.example.org:abc/o/r"); assert_eq!(found.host, "host.example.org"); assert_eq!(found.label(), "o/r"); // A doubled trailing slash still ends a clone URL, so `.git` comes off. assert_eq!( parsed("git.example.org/owner/repo.git//").repo, "repo", "a doubled trailing slash still ends a clone URL" ); // The optional user group takes the LAST `@`, or `b@git.example.org` // reads as the host. assert_eq!(parsed("a@b@git.example.org:o/r").host, "git.example.org"); } #[test] fn refuses_what_names_no_repository() { for raw in [ "", " ", "cottondb", "owner/repo", "github.com", "github.com/only", ] { assert!(parse_repo_ref(&ours(), raw).is_none(), "{raw:?} should not parse"); } } #[test] fn the_web_url_is_the_hosts_own_origin_never_the_api_base() { let found = parsed("github.com/decemberfm/cottondb"); assert_eq!(found.web_url(), "https://github.com/decemberfm/cottondb"); assert_ne!(found.web_url(), found.api_base_url()); } #[test] fn a_projects_list_drops_what_it_cannot_read_and_folds_duplicates() { let refs = project_repo_refs( [ "github.com/decemberfm/cottondb", "not a remote", "https://github.com/decemberfm/cottondb.git", "git.example.org/decemberfm/cottondb", ], &OwnCotton::default(), ); assert_eq!(refs.len(), 2, "{refs:?}"); assert_eq!(refs[0].key(), "github.com/decemberfm/cottondb"); assert_eq!(refs[1].key(), "git.example.org/decemberfm/cottondb"); } #[test] fn the_label_is_owner_slash_repo_and_the_key_carries_the_host() { let found = parsed("git.example.org/decemberfm/cottondb"); assert_eq!(found.label(), "decemberfm/cottondb"); assert_eq!(found.key(), "git.example.org/decemberfm/cottondb"); } // ── the URL a Cotton-served repository is cloned from ───────────────────── #[test] fn the_clone_path_is_the_one_the_route_mounts() { assert_eq!(clone_path_for("aruna", "ledger"), "/git/aruna/ledger.git"); } #[test] fn the_clone_url_hangs_the_path_off_the_relay_name() { assert_eq!( clone_url_for(&at("studio"), "aruna", "ledger").as_deref(), Some("https://studio.relay.cottondb.com/git/aruna/ledger.git") ); // Surrounding whitespace in the column is a name, not a different host. assert_eq!( clone_url_for(&at(" studio "), "o", "r").as_deref(), Some("https://studio.relay.cottondb.com/git/o/r.git") ); } #[test] fn a_workspace_with_no_relay_name_has_no_clone_url() { // The three shapes an absent or unusable name arrives in. Each answers // `None` rather than composing a host nobody answers. for name in ["", " ", "A", "not a name!", "studio.relay.cottondb.com"] { assert_eq!( clone_url_for(&at(name), "o", "r"), None, "`{name}` composed a URL" ); } } // ── a household's own Cotton address ────────────────────────────────────── // // 28 Sep 2026: the Cotton project named `asha.relay.cottondb.com/git/…` // beside the git host's address, and the worker minted a second, Forgejo-kind // row for the relay host that asked for a token nothing could ever supply. /// The household the fixtures are: relay name `studio`, holding /// `aruna/ledger` with Cotton its origin at the relay host where it /// now lives. fn household() -> OwnCotton { OwnCotton::new( at("studio"), [("studio.relay.cottondb.com", "aruna", "ledger")], ) } #[test] fn a_cotton_address_is_never_a_git_host() { for raw in [ "studio.relay.cottondb.com/git/aruna/ledger", "https://studio.relay.cottondb.com/git/aruna/ledger.git", "https://Studio.Relay.CottonDB.com/git/aruna/ledger/", // The phantom row's own key, pasted back into a project. "studio.relay.cottondb.com/aruna/ledger", ] { assert_eq!(parse_repo_ref(&ours(), raw), None, "{raw} read as a git host"); assert_eq!( read_repo_address(&ours(), raw), Some(RepoAddress::Cotton(CottonAddress { relay: ours(), relay_name: "studio".to_string(), owner: "aruna".to_string(), repo: "ledger".to_string(), })), "{raw}" ); } // Under the relay domain but no relay name β€” nobody's Cotton, and no git // host either. for raw in [ "a.b.relay.cottondb.com/git/o/r", "relay.cottondb.com/git/o/r", "x.relay.cottondb.com/git/o/r", ] { assert_eq!(read_repo_address(&ours(), raw), None, "{raw}"); } assert!(is_cotton_host(&ours(), "studio.relay.cottondb.com")); assert!(is_cotton_host(&ours(), "ASHA.relay.cottondb.com")); assert!(!is_cotton_host(&ours(), "git.example.org")); assert!(!is_cotton_host(&ours(), "relay.cottondb.com.evil.com")); } /// RULE: A Cotton address resolves to the rekeyed relay row, so a project cannot recreate the pre-move git-host row after the repository moved. #[test] fn the_households_own_address_resolves_to_the_rekeyed_held_repository() { // The move writer changes project remotes in the same transaction; this // reader keeps the resulting Cotton spelling on that one row. let refs = project_repo_refs( [ "studio.relay.cottondb.com/git/aruna/ledger", "studio.relay.cottondb.com/aruna/ledger", ], &household(), ); // ONE repository, the rekeyed row's key β€” both Cotton spellings are one // place. assert_eq!(refs.len(), 1, "{refs:?}"); assert_eq!(refs[0].key(), "studio.relay.cottondb.com/aruna/ledger"); assert_eq!(refs[0].kind, CodeHostKind::Cotton); assert_eq!( refs[0].web_url(), "https://studio.relay.cottondb.com/git/aruna/ledger" ); assert_eq!( refs[0].clone_url(), "https://studio.relay.cottondb.com/git/aruna/ledger.git" ); // The Cotton address alone still names the held row. let alone = project_repo_refs( ["https://studio.relay.cottondb.com/git/Aruna/Ledger.git"], &household(), ); assert_eq!(alone.len(), 1); assert_eq!(alone[0].key(), "studio.relay.cottondb.com/aruna/ledger"); // And what it answers lives at the Cotton address. let home = household().home_of(&alone[0]).expect("a Cotton home"); assert_eq!(home.host(), "studio.relay.cottondb.com"); // A repository the household does not hold on Cotton has no Cotton home. let elsewhere = parse_repo_ref(&ours(), "git.example.org/aruna/quillby").unwrap(); assert_eq!(household().home_of(&elsewhere), None); } #[test] fn a_cotton_address_that_names_nothing_held_here_is_dropped_never_minted() { for (raw, own) in [ // Another household's Cotton. ( "someone.relay.cottondb.com/git/aruna/ledger", household(), ), // This household, a repository it does not hold. ( "studio.relay.cottondb.com/git/aruna/other", household(), ), // A household with no relay name has no address to match. ( "studio.relay.cottondb.com/git/aruna/ledger", OwnCotton::new( HouseholdAddress::none(), [("git.example.org", "aruna", "ledger")], ), ), ( "studio.relay.cottondb.com/git/aruna/ledger", OwnCotton::new( at(" "), [("git.example.org", "aruna", "ledger")], ), ), // Holding nothing at all. ( "studio.relay.cottondb.com/git/aruna/ledger", OwnCotton::new(at("studio"), []), ), ] { assert!( project_repo_refs([raw], &own).is_empty(), "{raw} with {own:?}" ); } } #[test] fn a_held_repository_whose_origin_is_cotton_lives_at_its_cotton_address() { let home = cotton_home( &at("studio"), "aruna", "ledger", true, Some("cotton"), ) .expect("a held, Cotton-origin repository has a Cotton home"); assert_eq!(home.host(), "studio.relay.cottondb.com"); assert_eq!( home.web_url(), "https://studio.relay.cottondb.com/git/aruna/ledger" ); // The home is the address a checkout clones, less `.git`: one spelling. assert_eq!( format!("{}.git", home.web_url()), clone_url_for(&at("studio"), "aruna", "ledger").unwrap() ); // And the address reads back as the same place. assert_eq!( read_repo_address(&ours(), &home.web_url()), Some(RepoAddress::Cotton(home.clone())) ); } #[test] fn only_a_held_cotton_origin_repository_has_a_cotton_home() { for (relay, held, origin) in [ (Some("studio"), false, Some("cotton")), (Some("studio"), true, Some("forgejo")), (Some("studio"), true, None), (None, true, Some("cotton")), (Some(""), true, Some("cotton")), (Some("not a name"), true, Some("cotton")), ] { assert_eq!( cotton_home( &HouseholdAddress::new(ours(), relay), "o", "r", held, origin ), None, "{relay:?} held={held} origin={origin:?}" ); } } /// RULE: A checkout is never addressed at the other app's household β€” Cotton Dev only at a `-dev` name, CottonDB never at one. #[test] fn a_clone_url_is_never_the_other_app_s_household() { use crate::flavor::Flavor; assert_eq!(clone_url_as(&at("studio"), "o", "r", Some(Flavor::Dev)), None); assert_eq!(clone_url_as(&at("studio-dev"), "o", "r", Some(Flavor::Real)), None); assert!(clone_url_as(&at("studio-dev"), "o", "r", Some(Flavor::Dev)) .is_some_and(|url| url.starts_with("https://studio-dev.relay.cottondb.com/"))); assert_eq!( clone_url_as(&at("studio"), "o", "r", Some(Flavor::Real)), clone_url_as(&at("studio"), "o", "r", None) ); } /// RULE: a household's repositories live under ITS relay β€” a household that /// runs its own (`packages/relay/README.md`) clones from, prints and reads /// back its own hosts, and no reader assumes Cotton's. The relay is part of /// the address: the same name under another relay is another household. #[test] fn a_household_on_its_own_relay_has_its_repositories_under_that_relay() { let harbour = on_its_own_relay("harbour"); let theirs = harbour.relay().clone(); let own = OwnCotton::new( harbour.clone(), [("harbour.relay.example.org", "aruna", "ledger")], ); // What a checkout clones, and what the screens print as the home. let clone = clone_url_for(&harbour, "aruna", "ledger").expect("a named household"); assert_eq!(clone, "https://harbour.relay.example.org/git/aruna/ledger.git"); let home = cotton_home(&harbour, "aruna", "ledger", true, Some("cotton")).expect("a home"); assert_eq!(home.host(), "harbour.relay.example.org"); assert_eq!(format!("{}.git", home.web_url()), clone); assert!(!clone.contains("cottondb.com"), "{clone}"); // Its own address is Cotton, never a git host, and resolves to the row. assert_eq!(parse_repo_ref(&theirs, &clone), None); assert_eq!( read_repo_address(&theirs, &clone), Some(RepoAddress::Cotton(home.clone())) ); let refs = project_repo_refs([clone.as_str()], &own); assert_eq!(refs.len(), 1, "{refs:?}"); assert_eq!(refs[0].key(), "harbour.relay.example.org/aruna/ledger"); assert_eq!(own.home_of(&refs[0]), Some(home)); assert!(is_cotton_host(&theirs, "harbour.relay.example.org")); // The SAME NAME under Cotton's relay is somebody else's Cotton: still no // git host for anybody (the 28 Sep 2026 row), and never this household's // repository. let namesake = "harbour.relay.cottondb.com/git/aruna/ledger"; assert_eq!(parse_repo_ref(&theirs, namesake), None); assert!(is_cotton_host(&theirs, "harbour.relay.cottondb.com")); assert!(project_repo_refs([namesake], &own).is_empty()); // And the other way round: a household on Cotton's relay holding the same // repository does not answer for the name under theirs. let ours_same_name = OwnCotton::new( at("harbour"), [("harbour.relay.cottondb.com", "aruna", "ledger")], ); assert_eq!(project_repo_refs([namesake], &ours_same_name).len(), 1); let from_theirs = project_repo_refs([clone.as_str()], &ours_same_name); assert!( from_theirs.iter().all(|found| found.host == "harbour.relay.example.org"), "a relay this household cannot know reads as the host it spells: {from_theirs:?}" ); // A stored relay that cannot be used has no address at all β€” never the // name under Cotton's relay. let unusable = HouseholdAddress::from_stored(Some("harbour"), Some("relay.example.org"), None); assert_eq!(unusable, HouseholdAddress::none()); assert_eq!(clone_url_for(&unusable, "aruna", "ledger"), None); assert_eq!(cotton_home(&unusable, "aruna", "ledger", true, Some("cotton")), None); // The flavor rule is on the NAME, whatever the relay. use crate::flavor::Flavor; assert_eq!(clone_url_as(&harbour, "o", "r", Some(Flavor::Dev)), None); assert!(clone_url_as(&on_its_own_relay("harbour-dev"), "o", "r", Some(Flavor::Dev)) .is_some_and(|url| url.starts_with("https://harbour-dev.relay.example.org/"))); } /// RULE: A repository's natural key ignores remote spelling and path case, so alternate clone URLs cannot create independent archives of the same host repository. #[test] fn repository_keys_fold_case_and_remote_spellings() { let remotes = [ "https://GIT.example.test/Team/Repo.git/", "git@git.example.test:team/repo.git", "ssh://git@GIT.example.test:2222/TEAM/REPO.GIT", ]; for remote in remotes { assert_eq!(parsed(remote).key(), "git.example.test/team/repo"); } assert_eq!(project_repo_refs(remotes, &OwnCotton::default()).len(), 1); }