Skip to content

Commit cee3f09

Browse files
CopilotByron
andcommitted
Add test to detect JSON defaults vs FeatureFlags struct mismatch
Co-authored-by: Byron <[email protected]>
1 parent ec09647 commit cee3f09

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

crates/but-settings/assets/defaults.jsonc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,13 @@
1818
"oauthClientId": "cd51880daa675d9e6452"
1919
},
2020
"featureFlags": {
21-
// Deprecated, was used for the new version of the UI.
22-
"v3": true,
2321
// Enables the v3 safe checkout.
2422
"cv3": false,
2523
/// Enable the usage of V3 workspace APIs.
2624
"ws3": true,
2725
/// Enable undo/redo support.
2826
"undo": false,
29-
/// Enable the usage of GitButler Acitions.
27+
/// Enable the usage of GitButler Actions.
3028
"actions": false,
3129
/// Enable the usage of the butbot chat.
3230
"butbot": false,

crates/but-settings/src/persistence.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ impl AppSettings {
5959
}
6060

6161
mod tests {
62+
use crate::app_settings::FeatureFlags;
63+
6264
#[test]
6365
fn ensure_default_settings_covers_all_fields() {
6466
let settings: serde_json::Value =
@@ -79,4 +81,53 @@ mod tests {
7981
}
8082
assert!(app_settings.is_ok())
8183
}
84+
85+
#[test]
86+
fn ensure_feature_flags_json_defaults_match_struct_defaults() {
87+
// Get the feature flags from the JSON defaults file
88+
let json_defaults: serde_json::Value =
89+
serde_json_lenient::from_str(crate::persistence::DEFAULTS).unwrap();
90+
let json_feature_flags = json_defaults
91+
.get("featureFlags")
92+
.expect("featureFlags should exist in defaults.jsonc");
93+
94+
// Create what the struct would look like with proper serde defaults
95+
// Based on the struct definition, most fields are false except ws3 which has #[serde(default = "default_true")]
96+
let expected_default_feature_flags = FeatureFlags {
97+
ws3: true, // This has #[serde(default = "default_true")] so it should be true
98+
cv3: false,
99+
undo: false,
100+
actions: false,
101+
butbot: false,
102+
rules: false,
103+
single_branch: false,
104+
};
105+
106+
// Serialize the expected default struct to JSON for comparison
107+
let expected_default_as_json: serde_json::Value = serde_json::to_value(&expected_default_feature_flags)
108+
.expect("Expected default FeatureFlags should be serializable");
109+
110+
// Compare the JSON values directly - this will catch extra fields like 'v3' that don't exist in the struct
111+
if *json_feature_flags != expected_default_as_json {
112+
println!("\n===========================================================================================");
113+
println!("FeatureFlags JSON defaults don't match the default struct serialization!");
114+
println!();
115+
println!("JSON from defaults.jsonc:");
116+
println!("{}", serde_json::to_string_pretty(json_feature_flags).unwrap());
117+
println!();
118+
println!("Expected default struct serialized:");
119+
println!("{}", serde_json::to_string_pretty(&expected_default_as_json).unwrap());
120+
println!();
121+
println!("This means the JSON defaults contain fields or values that don't match");
122+
println!("the FeatureFlags struct definition. Please update either:");
123+
println!("1. The defaults in 'crates/but-settings/assets/defaults.jsonc' (remove extra fields, fix values)");
124+
println!("2. The FeatureFlags struct in 'crates/but-settings/src/app_settings.rs' (add missing fields)");
125+
println!("===========================================================================================\n");
126+
}
127+
128+
assert_eq!(
129+
*json_feature_flags, expected_default_as_json,
130+
"FeatureFlags JSON defaults should match the expected default struct when serialized"
131+
);
132+
}
82133
}

0 commit comments

Comments
 (0)