forge
Explore
Log in

Don't you dare go Hollow

Pure forge-core tests merge into tables and drop no real checks

Browse files at this commit

c389fb6bc0927a5bd13f3b97ed9bd0e01d1d2525Ahmet Kaan GümüşParent: 1093644
  • test
    • pure forge-core tests merge into tables and drop no real checks
Show full message
✅ test: pure forge-core tests merge into tables and drop no real checks

Test counts before/after per file in scope:
- config.rs 25 -> 15 (listen fallback and parse merged, four bad-config rejections merged, four optional-section default checks merged, four r2 field checks merged)
- issue_refs.rs 19 -> 3 (issue reference and closing reference cases as tables, keyword loop kept)
- names.rs 8 -> 7 (valid names and usernames folded into one test, rejections into one test)
- suggestions.rs 9 -> 3 (split_body and replace_line cases as tables)
- server_settings.rs 7 -> 7, time.rs 4 -> 4, tz.rs 4 -> 4 (unchanged)
Total 76 -> 43. Every assertion kept, each with a case label in its message.
crates/forge-core/src/config.rsmodified+168 -166
@@ -806,36 +806,75 @@ mod tests {
806 }806 }
807 807
808 #[test]808 #[test]
809- fn missing_user_content_listen_uses_default() {809+ fn user_content_listen_defaults_and_parses() {
810 let dir = tempfile::tempdir().unwrap();810 let dir = tempfile::tempdir().unwrap();
811 let path = dir.path().join("forge.toml");811 let path = dir.path().join("forge.toml");
812- let text: String = DEV_DEFAULTS812+ let without_line: String = DEV_DEFAULTS
813 .lines()813 .lines()
814 .filter(|line| !line.starts_with("user_content_listen"))814 .filter(|line| !line.starts_with("user_content_listen"))
815 .collect::<Vec<_>>()815 .collect::<Vec<_>>()
816 .join("\n");816 .join("\n");
817- std::fs::write(&path, text).unwrap();817+ let replaced = DEV_DEFAULTS.replace("127.0.0.1:7661\"\n# SSH", "0.0.0.0:9000\"\n# SSH");
818- let cfg = Config::load(&path).unwrap();818+ let cases: Vec<(&str, String, &str)> = vec![
819- assert_eq!(cfg.user_content_listen, "127.0.0.1:7661".parse().unwrap());819+ ("missing line uses default", without_line, "127.0.0.1:7661"),
820- }820+ ("explicit value is parsed", replaced, "0.0.0.0:9000"),
821-821+ ];
822- #[test]822+ for (label, text, want) in cases {
823- fn user_content_listen_is_parsed() {823+ std::fs::write(&path, text).unwrap();
824- let dir = tempfile::tempdir().unwrap();824+ let cfg = Config::load(&path).unwrap();
825- let path = dir.path().join("forge.toml");825+ assert_eq!(
826- let text = DEV_DEFAULTS.replace("127.0.0.1:7661\"\n# SSH", "0.0.0.0:9000\"\n# SSH");826+ cfg.user_content_listen,
827- std::fs::write(&path, text).unwrap();827+ want.parse().unwrap(),
828- let cfg = Config::load(&path).unwrap();828+ "case {label}"
829- assert_eq!(cfg.user_content_listen, "0.0.0.0:9000".parse().unwrap());829+ );
830+ }
830 }831 }
831 832
832 #[test]833 #[test]
833- fn unknown_keys_are_rejected() {834+ fn bad_configs_are_refused() {
834 let dir = tempfile::tempdir().unwrap();835 let dir = tempfile::tempdir().unwrap();
835 let path = dir.path().join("forge.toml");836 let path = dir.path().join("forge.toml");
836- let text = DEV_DEFAULTS.replace("registration", "bogus = 1\nregistration");837+ let cases: Vec<(&str, String)> = vec![
837- std::fs::write(&path, text).unwrap();838+ (
838- assert!(matches!(Config::load(&path), Err(Error::Config(_))));839+ "unknown top-level key",
840+ DEV_DEFAULTS.replace("registration", "bogus = 1\nregistration"),
841+ ),
842+ (
843+ "invalid trusted proxy",
844+ DEV_DEFAULTS.replace("trusted_proxies = []", "trusted_proxies = [\"not-an-ip\"]"),
845+ ),
846+ (
847+ "self_update unknown key",
848+ format!("{DEV_DEFAULTS}\n[self_update]\nbogus = 1\n"),
849+ ),
850+ (
851+ "storage unknown key",
852+ format!("{DEV_DEFAULTS}\n[storage]\nbogus = 1\n"),
853+ ),
854+ (
855+ "storage.s3 secret_access_key",
856+ format!("{DEV_DEFAULTS}\n[storage.s3]\nsecret_access_key = \"x\"\n"),
857+ ),
858+ (
859+ "r2 endpoint",
860+ format!("{DEV_DEFAULTS}\n[storage.r2]\nendpoint = \"https://x\"\n"),
861+ ),
862+ (
863+ "r2 region",
864+ format!("{DEV_DEFAULTS}\n[storage.r2]\nregion = \"auto\"\n"),
865+ ),
866+ (
867+ "r2 unknown key",
868+ format!("{DEV_DEFAULTS}\n[storage.r2]\nbogus = 1\n"),
869+ ),
870+ ];
871+ for (label, text) in cases {
872+ std::fs::write(&path, text).unwrap();
873+ assert!(
874+ matches!(Config::load(&path), Err(Error::Config(_))),
875+ "case {label}"
876+ );
877+ }
839 }878 }
840 879
841 #[test]880 #[test]
@@ -873,39 +912,45 @@ mod tests {
873 }912 }
874 913
875 #[test]914 #[test]
876- fn invalid_trusted_proxy_is_rejected() {915+ fn optional_sections_default_to_disabled() {
877- let dir = tempfile::tempdir().unwrap();
878- let path = dir.path().join("forge.toml");
879- let text =
880- DEV_DEFAULTS.replace("trusted_proxies = []", "trusted_proxies = [\"not-an-ip\"]");
881- std::fs::write(&path, text).unwrap();
882- assert!(matches!(Config::load(&path), Err(Error::Config(_))));
883- }
884-
885- #[test]
886- fn backup_section_defaults_to_disabled() {
887 let dir = tempfile::tempdir().unwrap();916 let dir = tempfile::tempdir().unwrap();
888 let path = dir.path().join("forge.toml");917 let path = dir.path().join("forge.toml");
889 std::fs::write(&path, DEV_DEFAULTS).unwrap();918 std::fs::write(&path, DEV_DEFAULTS).unwrap();
890 let cfg = Config::load(&path).unwrap();919 let cfg = Config::load(&path).unwrap();
891- assert!(!cfg.backup.enabled());920+ // backup
892- assert_eq!(cfg.backup.interval_hours, 24);921+ assert!(!cfg.backup.enabled(), "backup enabled");
893- assert_eq!(cfg.backup.keep, 7);922+ assert_eq!(cfg.backup.interval_hours, 24, "backup interval");
894- assert_eq!(cfg.backup.fsck_interval_days, 7);923+ assert_eq!(cfg.backup.keep, 7, "backup keep");
895- assert!(cfg.backup.healthcheck_url.is_empty());924+ assert_eq!(cfg.backup.fsck_interval_days, 7, "backup fsck");
896- }925+ assert!(cfg.backup.healthcheck_url.is_empty(), "backup healthcheck");
897-926+ // replication
898- #[test]927+ assert!(!cfg.replication.enabled(), "replication enabled");
899- fn replication_section_defaults_to_disabled() {928+ assert_eq!(cfg.replication.ssh_port, 22, "replication ssh port");
900- let dir = tempfile::tempdir().unwrap();929+ assert_eq!(
901- let path = dir.path().join("forge.toml");930+ cfg.replication.db_snapshot_minutes, 15,
902- std::fs::write(&path, DEV_DEFAULTS).unwrap();931+ "replication snapshot"
903- let cfg = Config::load(&path).unwrap();932+ );
904- assert!(!cfg.replication.enabled());933+ assert_eq!(
905- assert_eq!(cfg.replication.ssh_port, 22);934+ cfg.replication.reconcile_minutes, 60,
906- assert_eq!(cfg.replication.db_snapshot_minutes, 15);935+ "replication reconcile"
907- assert_eq!(cfg.replication.reconcile_minutes, 60);936+ );
908- assert!(cfg.replication.healthcheck_url.is_empty());937+ assert!(
938+ cfg.replication.healthcheck_url.is_empty(),
939+ "replication healthcheck"
940+ );
941+ // self_update
942+ assert!(!cfg.self_update.enabled, "self_update enabled");
943+ assert_eq!(cfg.self_update.branch, "main", "self_update branch");
944+ assert_eq!(cfg.self_update.job, "build", "self_update job");
945+ assert_eq!(
946+ cfg.self_update.artifact, "dist_forge",
947+ "self_update artifact"
948+ );
949+ // storage
950+ assert_eq!(cfg.storage.backend, StorageBackend::Fs, "storage backend");
951+ assert!(cfg.storage.s3.bucket.is_empty(), "s3 bucket");
952+ assert!(!cfg.storage.s3.allow_http, "s3 allow_http");
953+ assert!(!cfg.storage.s3.virtual_hosted_style, "s3 virtual hosted");
909 }954 }
910 955
911 #[test]956 #[test]
@@ -933,18 +978,6 @@ mod tests {
933 assert!(matches!(Config::load(&path), Err(Error::Config(_))));978 assert!(matches!(Config::load(&path), Err(Error::Config(_))));
934 }979 }
935 980
936- #[test]
937- fn self_update_section_defaults_to_disabled() {
938- let dir = tempfile::tempdir().unwrap();
939- let path = dir.path().join("forge.toml");
940- std::fs::write(&path, DEV_DEFAULTS).unwrap();
941- let cfg = Config::load(&path).unwrap();
942- assert!(!cfg.self_update.enabled);
943- assert_eq!(cfg.self_update.branch, "main");
944- assert_eq!(cfg.self_update.job, "build");
945- assert_eq!(cfg.self_update.artifact, "dist_forge");
946- }
947-
948 #[test]981 #[test]
949 fn self_update_enabled_requires_owner_name_repo() {982 fn self_update_enabled_requires_owner_name_repo() {
950 let dir = tempfile::tempdir().unwrap();983 let dir = tempfile::tempdir().unwrap();
@@ -963,14 +996,6 @@ mod tests {
963 assert_eq!(cfg.self_update.repo, "Tahinli/forge");996 assert_eq!(cfg.self_update.repo, "Tahinli/forge");
964 }997 }
965 998
966- #[test]
967- fn self_update_rejects_unknown_keys() {
968- let dir = tempfile::tempdir().unwrap();
969- let path = dir.path().join("forge.toml");
970- std::fs::write(&path, format!("{DEV_DEFAULTS}\n[self_update]\nbogus = 1\n")).unwrap();
971- assert!(Config::load(&path).is_err());
972- }
973-
974 #[test]999 #[test]
975 fn backup_section_is_parsed_and_validated() {1000 fn backup_section_is_parsed_and_validated() {
976 let dir = tempfile::tempdir().unwrap();1001 let dir = tempfile::tempdir().unwrap();
@@ -991,18 +1016,6 @@ mod tests {
991 assert!(matches!(Config::load(&path), Err(Error::Config(_))));1016 assert!(matches!(Config::load(&path), Err(Error::Config(_))));
992 }1017 }
993 1018
994- #[test]
995- fn storage_section_defaults_to_fs() {
996- let dir = tempfile::tempdir().unwrap();
997- let path = dir.path().join("forge.toml");
998- std::fs::write(&path, DEV_DEFAULTS).unwrap();
999- let cfg = Config::load(&path).unwrap();
1000- assert_eq!(cfg.storage.backend, StorageBackend::Fs);
1001- assert!(cfg.storage.s3.bucket.is_empty());
1002- assert!(!cfg.storage.s3.allow_http);
1003- assert!(!cfg.storage.s3.virtual_hosted_style);
1004- }
1005-
1006 #[test]1019 #[test]
1007 fn s3_backend_requires_bucket_region_and_credentials() {1020 fn s3_backend_requires_bucket_region_and_credentials() {
1008 let dir = tempfile::tempdir().unwrap();1021 let dir = tempfile::tempdir().unwrap();
@@ -1076,28 +1089,6 @@ mod tests {
1076 assert!(Config::load(&path).is_ok());1089 assert!(Config::load(&path).is_ok());
1077 }1090 }
1078 1091
1079- #[test]
1080- fn storage_rejects_unknown_keys() {
1081- let dir = tempfile::tempdir().unwrap();
1082- let path = dir.path().join("forge.toml");
1083- std::fs::write(&path, format!("{DEV_DEFAULTS}\n[storage]\nbogus = 1\n")).unwrap();
1084- assert!(matches!(Config::load(&path), Err(Error::Config(_))));
1085- std::fs::write(
1086- &path,
1087- format!("{DEV_DEFAULTS}\n[storage.s3]\nsecret_access_key = \"x\"\n"),
1088- )
1089- .unwrap();
1090- assert!(matches!(Config::load(&path), Err(Error::Config(_))));
1091- // R2 users never set an endpoint or region, so those keys are refused outright.
1092- for key in ["endpoint = \"https://x\"", "region = \"auto\"", "bogus = 1"] {
1093- std::fs::write(&path, format!("{DEV_DEFAULTS}\n[storage.r2]\n{key}\n")).unwrap();
1094- assert!(
1095- matches!(Config::load(&path), Err(Error::Config(_))),
1096- "{key}"
1097- );
1098- }
1099- }
1100-
1101 /// A complete `[storage.r2]` table with a real secret file, minus any lines the caller drops.1092 /// A complete `[storage.r2]` table with a real secret file, minus any lines the caller drops.
1102 fn r2_config_text(dir: &Path, drop: &[&str], extra: &str) -> String {1093 fn r2_config_text(dir: &Path, drop: &[&str], extra: &str) -> String {
1103 let secret = dir.join("r2-secret");1094 let secret = dir.join("r2-secret");
@@ -1151,74 +1142,85 @@ mod tests {
1151 }1142 }
1152 1143
1153 #[test]1144 #[test]
1154- fn r2_backend_requires_its_fields() {1145+ fn r2_fields_are_validated() {
1155- let dir = tempfile::tempdir().unwrap();
1156- let path = dir.path().join("forge.toml");
1157- std::fs::write(&path, r2_config_text(dir.path(), &[], "")).unwrap();
1158- assert!(Config::load(&path).is_ok());
1159- for missing in [
1160- "account_id",
1161- "bucket",
1162- "access_key_id",
1163- "secret_access_key_file",
1164- ] {
1165- std::fs::write(&path, r2_config_text(dir.path(), &[missing], "")).unwrap();
1166- assert!(
1167- matches!(Config::load(&path), Err(Error::Config(_))),
1168- "{missing} should be required"
1169- );
1170- }
1171- }
1172-
1173- #[test]
1174- fn r2_account_id_must_be_hex() {
1175 let dir = tempfile::tempdir().unwrap();1146 let dir = tempfile::tempdir().unwrap();
1176 let path = dir.path().join("forge.toml");1147 let path = dir.path().join("forge.toml");
1177- for bad in ["evil.example.com/", "acct id", "xyz"] {1148+ let absent = dir.path().join("absent").display().to_string();
1178- let text = r2_config_text(1149+ // (label, lines to drop from the complete table, extra lines, expected to load)
1179- dir.path(),1150+ let cases: Vec<(&str, &[&str], String, bool)> = vec![
1151+ ("complete", &[], String::new(), true),
1152+ ("account_id missing", &["account_id"], String::new(), false),
1153+ ("bucket missing", &["bucket"], String::new(), false),
1154+ (
1155+ "access_key_id missing",
1156+ &["access_key_id"],
1157+ String::new(),
1158+ false,
1159+ ),
1160+ (
1161+ "secret_access_key_file missing",
1162+ &["secret_access_key_file"],
1163+ String::new(),
1164+ false,
1165+ ),
1166+ (
1167+ "account_id with a host",
1180 &["account_id"],1168 &["account_id"],
1181- &format!("account_id = \"{bad}\"\n"),1169+ "account_id = \"evil.example.com/\"\n".into(),
1182- );1170+ false,
1183- std::fs::write(&path, text).unwrap();1171+ ),
1184- assert!(1172+ (
1185- matches!(Config::load(&path), Err(Error::Config(_))),1173+ "account_id with a space",
1186- "`{bad}`"1174+ &["account_id"],
1187- );1175+ "account_id = \"acct id\"\n".into(),
1188- }1176+ false,
1189- }1177+ ),
1190-1178+ (
1191- #[test]1179+ "account_id not hex",
1192- fn r2_jurisdiction_must_be_known() {1180+ &["account_id"],
1193- let dir = tempfile::tempdir().unwrap();1181+ "account_id = \"xyz\"\n".into(),
1194- let path = dir.path().join("forge.toml");1182+ false,
1195- for (value, ok) in [1183+ ),
1196- ("", true),1184+ (
1197- ("eu", true),1185+ "secret file absent",
1198- ("fedramp", true),1186+ &["secret_access_key_file"],
1199- ("us", false),1187+ format!("secret_access_key_file = \"{absent}\"\n"),
1200- ("EU", false),1188+ false,
1201- ] {1189+ ),
1202- let text = r2_config_text(1190+ (
1203- dir.path(),1191+ "jurisdiction empty",
1204 &["jurisdiction"],1192 &["jurisdiction"],
1205- &format!("jurisdiction = \"{value}\"\n"),1193+ "jurisdiction = \"\"\n".into(),
1206- );1194+ true,
1207- std::fs::write(&path, text).unwrap();1195+ ),
1208- assert_eq!(Config::load(&path).is_ok(), ok, "jurisdiction `{value}`");1196+ (
1197+ "jurisdiction eu",
1198+ &["jurisdiction"],
1199+ "jurisdiction = \"eu\"\n".into(),
1200+ true,
1201+ ),
1202+ (
1203+ "jurisdiction fedramp",
1204+ &["jurisdiction"],
1205+ "jurisdiction = \"fedramp\"\n".into(),
1206+ true,
1207+ ),
1208+ (
1209+ "jurisdiction us",
1210+ &["jurisdiction"],
1211+ "jurisdiction = \"us\"\n".into(),
1212+ false,
1213+ ),
1214+ (
1215+ "jurisdiction EU",
1216+ &["jurisdiction"],
1217+ "jurisdiction = \"EU\"\n".into(),
1218+ false,
1219+ ),
1220+ ];
1221+ for (label, drop, extra, ok) in cases {
1222+ std::fs::write(&path, r2_config_text(dir.path(), drop, &extra)).unwrap();
1223+ assert_eq!(Config::load(&path).is_ok(), ok, "case {label}");
1209 }1224 }
1210 }1225 }
1211-
1212- #[test]
1213- fn r2_secret_file_must_exist() {
1214- let dir = tempfile::tempdir().unwrap();
1215- let path = dir.path().join("forge.toml");
1216- let text = r2_config_text(dir.path(), &["secret_access_key_file"], "")
1217- + &format!(
1218- "secret_access_key_file = \"{}\"\n",
1219- dir.path().join("absent").display()
1220- );
1221- std::fs::write(&path, text).unwrap();
1222- assert!(matches!(Config::load(&path), Err(Error::Config(_))));
1223- }
1224 }1226 }
crates/forge-core/src/issue_refs.rsmodified+33 -87
@@ -115,80 +115,42 @@ mod tests {
115 use super::*;115 use super::*;
116 116
117 #[test]117 #[test]
118- fn plain_reference() {118+ fn issue_references_table() {
119- assert_eq!(find_issue_refs("fixes #12"), vec![12]);119+ let cases: &[(&str, Vec<i64>)] = &[
120- }120+ ("fixes #12", vec![12]),
121-121+ ("Fixes: #3, closes #4", vec![3, 4]),
122- #[test]122+ ("prefix#5", vec![]),
123- fn multiple_references_in_order() {123+ ("&#39;", vec![]),
124- assert_eq!(find_issue_refs("Fixes: #3, closes #4"), vec![3, 4]);124+ ("a/#7", vec![]),
125- }125+ ("#0012", vec![12]),
126-126+ ("#1234567890", vec![]),
127- #[test]127+ ("#123456789", vec![123_456_789]),
128- fn prefix_before_hash_is_not_a_reference() {128+ ("#0", vec![]),
129- assert!(find_issue_refs("prefix#5").is_empty());129+ ("(#8)", vec![8]),
130- }130+ ("#9.", vec![9]),
131-131+ ("#12abc", vec![]),
132- #[test]132+ ("#2 and #1 and #2", vec![2, 1]),
133- fn html_entity_is_not_a_reference() {133+ ("unfixes #2", vec![2]),
134- assert!(find_issue_refs("&#39;").is_empty());134+ ];
135- }135+ for (text, want) in cases {
136-136+ assert_eq!(find_issue_refs(text), *want, "case {text:?}");
137- #[test]137+ }
138- fn slash_before_hash_is_not_a_reference() {
139- assert!(find_issue_refs("a/#7").is_empty());
140- }
141-
142- #[test]
143- fn leading_zeros_are_parsed() {
144- assert_eq!(find_issue_refs("#0012"), vec![12]);
145- }
146-
147- #[test]
148- fn ten_digits_is_not_a_reference() {
149- assert!(find_issue_refs("#1234567890").is_empty());
150- }
151-
152- #[test]
153- fn nine_digits_is_a_reference() {
154- assert_eq!(find_issue_refs("#123456789"), vec![123_456_789]);
155- }
156-
157- #[test]
158- fn zero_is_not_a_reference() {
159- assert!(find_issue_refs("#0").is_empty());
160- }
161-
162- #[test]
163- fn parentheses_are_allowed_around_reference() {
164- assert_eq!(find_issue_refs("(#8)"), vec![8]);
165- }
166-
167- #[test]
168- fn trailing_punctuation_is_allowed() {
169- assert_eq!(find_issue_refs("#9."), vec![9]);
170- }
171-
172- #[test]
173- fn alphanumeric_suffix_is_not_a_reference() {
174- assert!(find_issue_refs("#12abc").is_empty());
175- }
176-
177- #[test]
178- fn duplicates_are_removed() {
179- assert_eq!(find_issue_refs("#2 and #1 and #2"), vec![2, 1]);
180- assert_eq!(find_closing_refs("fixes #2, closes #2"), vec![2]);
181- }
182-
183- #[test]
184- fn unfixes_is_not_closing() {
185- assert!(find_closing_refs("unfixes #2").is_empty());
186- assert_eq!(find_issue_refs("unfixes #2"), vec![2]);
187 }138 }
188 139
189 #[test]140 #[test]
190- fn fixed_is_closing() {141+ fn closing_references_table() {
191- assert_eq!(find_closing_refs("fixed #2"), vec![2]);142+ let cases: &[(&str, Vec<i64>)] = &[
143+ ("fixes #2, closes #2", vec![2]),
144+ ("unfixes #2", vec![]),
145+ ("fixed #2", vec![2]),
146+ ("fixes#2", vec![]),
147+ ("fixes:#2", vec![]),
148+ ("see #2", vec![]),
149+ ("prefixes #2", vec![]),
150+ ];
151+ for (text, want) in cases {
152+ assert_eq!(find_closing_refs(text), *want, "case {text:?}");
153+ }
192 }154 }
193 155
194 #[test]156 #[test]
@@ -200,20 +162,4 @@ mod tests {
200 assert_eq!(find_closing_refs(&text), vec![5], "{kw}");162 assert_eq!(find_closing_refs(&text), vec![5], "{kw}");
201 }163 }
202 }164 }
203-
204- #[test]
205- fn keyword_needs_whitespace_before_hash() {
206- assert!(find_closing_refs("fixes#2").is_empty());
207- assert!(find_closing_refs("fixes:#2").is_empty());
208- }
209-
210- #[test]
211- fn plain_reference_is_not_closing() {
212- assert!(find_closing_refs("see #2").is_empty());
213- }
214-
215- #[test]
216- fn keyword_must_be_a_whole_word() {
217- assert!(find_closing_refs("prefixes #2").is_empty());
218- }
219 }165 }
crates/forge-core/src/names.rsmodified+25 -24
@@ -152,16 +152,33 @@ mod tests {
152 }152 }
153 153
154 #[test]154 #[test]
155- fn accepts_valid_names_and_returns_folded_form() {155+ fn accepted_names_return_folded_form() {
156- assert_eq!(validate_name("Tahinli").unwrap(), "tahinli");156+ let good: [(&str, &str); 5] = [
157- assert_eq!(validate_name("my-repo_2.x").unwrap(), "my-repo_2.x");157+ ("Tahinli", "tahinli"),
158- assert_eq!(validate_name("a").unwrap(), "a");158+ ("my-repo_2.x", "my-repo_2.x"),
159- assert_eq!(validate_name(&"a".repeat(64)).unwrap().len(), 64);159+ ("a", "a"),
160- assert_eq!(validate_name("x.gitx").unwrap(), "x.gitx");160+ ("x.gitx", "x.gitx"),
161+ ("Tahinli.Repo", "tahinli.repo"),
162+ ];
163+ for (name, want) in good {
164+ assert_eq!(validate_name(name).unwrap(), want, "case {name:?}");
165+ }
166+ let long = "a".repeat(64);
167+ assert_eq!(validate_name(&long).unwrap(), long, "case 64 chars");
168+ assert_eq!(
169+ validate_username("Tahinli").unwrap(),
170+ ("Tahinli".to_string(), "tahinli".to_string()),
171+ "case username display case"
172+ );
173+ assert_eq!(
174+ validate_username("TAHINLI").unwrap(),
175+ ("TAHINLI".to_string(), "tahinli".to_string()),
176+ "case username fullwidth"
177+ );
161 }178 }
162 179
163 #[test]180 #[test]
164- fn rejects_invalid_names() {181+ fn rejected_names_and_usernames_are_invalid_name_errors() {
165 let bad = [182 let bad = [
166 "".to_string(),183 "".to_string(),
167 "a".repeat(65),184 "a".repeat(65),
@@ -186,22 +203,6 @@ mod tests {
186 "expected rejection for {name:?}"203 "expected rejection for {name:?}"
187 );204 );
188 }205 }
189- }
190-
191- #[test]
192- fn username_keeps_display_case_and_folds_key() {
193- assert_eq!(
194- validate_username("Tahinli").unwrap(),
195- ("Tahinli".to_string(), "tahinli".to_string())
196- );
197- assert_eq!(
198- validate_username("TAHINLI").unwrap(),
199- ("TAHINLI".to_string(), "tahinli".to_string())
200- );
201- }
202-
203- #[test]
204- fn username_rejects_reserved_and_invalid() {
205 for bad in [206 for bad in [
206 "ADMIN",207 "ADMIN",
207 "Settings",208 "Settings",
@@ -214,7 +215,7 @@ mod tests {
214 ] {215 ] {
215 assert!(216 assert!(
216 matches!(validate_username(bad), Err(Error::InvalidName(_))),217 matches!(validate_username(bad), Err(Error::InvalidName(_))),
217- "expected rejection for {bad:?}"218+ "expected rejection for username {bad:?}"
218 );219 );
219 }220 }
220 }221 }
crates/forge-core/src/suggestions.rsmodified+52 -79
@@ -114,43 +114,36 @@ mod tests {
114 }114 }
115 115
116 #[test]116 #[test]
117- fn body_without_a_suggestion_is_one_text_part() {117+ fn split_body_cases() {
118- assert_eq!(118+ let cases: Vec<(&str, Vec<Part>)> = vec![
119- split_body("looks fine\n\n```rust\nlet x = 1;\n```"),119+ (
120- vec![Part::Text("looks fine\n\n```rust\nlet x = 1;\n```".into())]120+ "looks fine\n\n```rust\nlet x = 1;\n```",
121- );121+ vec![Part::Text("looks fine\n\n```rust\nlet x = 1;\n```".into())],
122- }122+ ),
123-123+ (
124- #[test]124+ "Try this:\n```suggestion\nlet y = 2;\n```\nThanks",
125- fn suggestion_block_is_split_out_with_its_surrounding_text() {125+ vec![
126- let body = "Try this:\n```suggestion\nlet y = 2;\n```\nThanks";126+ Part::Text("Try this:".into()),
127- assert_eq!(127+ Part::Suggestion(lines(&["let y = 2;"])),
128- split_body(body),128+ Part::Text("Thanks".into()),
129- vec![129+ ],
130- Part::Text("Try this:".into()),130+ ),
131- Part::Suggestion(lines(&["let y = 2;"])),131+ (
132- Part::Text("Thanks".into()),132+ "```suggestion\n```\n\n````suggestion\na\n```\nb\n````",
133- ]133+ vec![
134- );134+ Part::Suggestion(vec![]),
135- }135+ Part::Text("".into()),
136-136+ Part::Suggestion(lines(&["a", "```", "b"])),
137- #[test]137+ ],
138- fn empty_suggestion_and_several_blocks_are_parsed_in_order() {138+ ),
139- let body = "```suggestion\n```\n\n````suggestion\na\n```\nb\n````";139+ (
140- assert_eq!(140+ "```suggestion\nlet y = 2;",
141- split_body(body),141+ vec![Part::Text("```suggestion\nlet y = 2;".into())],
142- vec![142+ ),
143- Part::Suggestion(vec![]),143+ ];
144- Part::Text("".into()),144+ for (body, want) in cases {
145- Part::Suggestion(lines(&["a", "```", "b"])),145+ assert_eq!(split_body(body), want, "case {body:?}");
146- ]146+ }
147- );
148- }
149-
150- #[test]
151- fn unclosed_suggestion_fence_stays_text() {
152- let body = "```suggestion\nlet y = 2;";
153- assert_eq!(split_body(body), vec![Part::Text(body.into())]);
154 }147 }
155 148
156 #[test]149 #[test]
@@ -162,48 +155,28 @@ mod tests {
162 }155 }
163 156
164 #[test]157 #[test]
165- fn replace_line_swaps_one_line_and_keeps_the_rest() {158+ fn replace_line_cases() {
166- let text = "a\nb\nc\n";159+ // (text, 1-based line, expected line text, replacement, want)
167- assert_eq!(160+ type Case<'a> = (
168- replace_line(text, 2, "b", &lines(&["B"])),161+ &'a str,
169- Ok("a\nB\nc\n".into())162+ u32,
170- );163+ &'a str,
171- }164+ &'a [&'a str],
172-165+ Result<&'a str, ApplyError>,
173- #[test]
174- fn replace_line_can_expand_delete_and_keep_a_missing_newline() {
175- assert_eq!(
176- replace_line("a\nb", 2, "b", &lines(&["b1", "b2"])),
177- Ok("a\nb1\nb2".into())
178- );
179- assert_eq!(replace_line("a\nb\nc\n", 2, "b", &[]), Ok("a\nc\n".into()));
180- }
181-
182- #[test]
183- fn replace_line_refuses_a_changed_or_missing_line() {
184- assert_eq!(
185- replace_line("a\nb\n", 2, "old", &lines(&["x"])),
186- Err(ApplyError::Changed)
187- );
188- assert_eq!(
189- replace_line("a\nb\n", 3, "b", &lines(&["x"])),
190- Err(ApplyError::OutOfRange)
191- );
192- assert_eq!(
193- replace_line("a\n", 0, "a", &lines(&["x"])),
194- Err(ApplyError::OutOfRange)
195- );
196- assert_eq!(
197- replace_line("", 1, "", &lines(&["x"])),
198- Err(ApplyError::OutOfRange)
199- );
200- }
201-
202- #[test]
203- fn replace_line_ignores_carriage_returns_on_the_expected_line() {
204- assert_eq!(
205- replace_line("a\r\nb\r\n", 1, "a", &lines(&["A"])),
206- Ok("A\nb\r\n".into())
207 );166 );
167+ let cases: Vec<Case<'_>> = vec![
168+ ("a\nb\nc\n", 2, "b", &["B"], Ok("a\nB\nc\n")),
169+ ("a\nb", 2, "b", &["b1", "b2"], Ok("a\nb1\nb2")),
170+ ("a\nb\nc\n", 2, "b", &[], Ok("a\nc\n")),
171+ ("a\nb\n", 2, "old", &["x"], Err(ApplyError::Changed)),
172+ ("a\nb\n", 3, "b", &["x"], Err(ApplyError::OutOfRange)),
173+ ("a\n", 0, "a", &["x"], Err(ApplyError::OutOfRange)),
174+ ("", 1, "", &["x"], Err(ApplyError::OutOfRange)),
175+ ("a\r\nb\r\n", 1, "a", &["A"], Ok("A\nb\r\n")),
176+ ];
177+ for (text, line, expect, new, want) in cases {
178+ let got = replace_line(text, line, expect, &lines(new));
179+ assert_eq!(got, want.map(str::to_string), "case {text:?} line {line}");
180+ }
208 }181 }
209 }182 }