1use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
9#[serde(rename_all = "camelCase")]
10pub struct UpdateStatusSnapshot {
11 pub current_version: String,
13 #[serde(default, skip_serializing_if = "Option::is_none")]
20 pub latest_version: Option<String>,
21 #[serde(default)]
23 pub update_available: bool,
24 #[serde(default, skip_serializing_if = "Option::is_none")]
26 pub release_url: Option<String>,
27 #[serde(default, skip_serializing_if = "Vec::is_empty")]
33 pub release_highlights: Vec<String>,
34 #[serde(default, skip_serializing_if = "Vec::is_empty")]
38 pub current_release_highlights: Vec<String>,
39 #[serde(default, skip_serializing_if = "Option::is_none")]
42 pub current_release_url: Option<String>,
43 #[serde(default)]
47 pub current_release_seen: bool,
48 #[serde(default, skip_serializing_if = "Option::is_none")]
50 pub channel: Option<String>,
51 #[serde(default)]
53 pub check_due: bool,
54 #[serde(default)]
58 pub install: UpdateInstallState,
59}
60
61#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
68#[serde(rename_all = "camelCase", tag = "phase", rename_all_fields = "camelCase")]
69pub enum UpdateInstallState {
70 #[default]
72 Idle,
73 Downloading {
75 downloaded: u64,
77 total: Option<u64>,
80 },
81 Verifying,
83 Extracting,
85 Installing,
87 Completed {
89 version: String,
91 #[serde(default)]
96 needs_gatekeeper_approval: bool,
97 },
98 Failed {
100 message: String,
102 },
103}
104
105impl UpdateInstallState {
106 pub fn is_in_flight(&self) -> bool {
109 matches!(self, Self::Downloading { .. } | Self::Verifying | Self::Extracting | Self::Installing)
110 }
111}
112
113#[cfg(test)]
114mod tests {
115 use super::*;
116
117 #[test]
118 fn update_status_snapshot_round_trips_and_older_payloads_decode() {
119 let snapshot = UpdateStatusSnapshot {
120 current_version: "0.180.5".into(),
121 latest_version: Some("0.181.0".into()),
122 update_available: true,
123 release_url: Some("https://github.com/vinhnx/vtcode/releases/tag/v0.181.0".into()),
124 release_highlights: vec!["Faster startup".into(), "New model picker".into()],
125 current_release_highlights: vec!["Older highlight".into()],
126 current_release_url: Some("https://github.com/vinhnx/vtcode/releases/tag/v0.180.5".into()),
127 current_release_seen: true,
128 channel: Some("stable".into()),
129 check_due: false,
130 install: UpdateInstallState::Downloading { downloaded: 1024, total: Some(2048) },
131 };
132 let decoded: UpdateStatusSnapshot = serde_json::from_value(serde_json::to_value(&snapshot).unwrap()).unwrap();
133 assert_eq!(decoded, snapshot);
134
135 let quiet = UpdateStatusSnapshot {
139 current_version: "0.181.0".into(),
140 latest_version: None,
141 update_available: false,
142 release_url: None,
143 release_highlights: Vec::new(),
144 current_release_highlights: vec!["Freshly installed".into()],
145 current_release_url: Some("https://github.com/vinhnx/vtcode/releases/tag/v0.181.0".into()),
146 current_release_seen: false,
147 channel: None,
148 check_due: true,
149 install: UpdateInstallState::Idle,
150 };
151 let decoded_quiet: UpdateStatusSnapshot =
152 serde_json::from_value(serde_json::to_value(&quiet).unwrap()).unwrap();
153 assert_eq!(decoded_quiet, quiet);
154
155 let legacy: UpdateStatusSnapshot =
156 serde_json::from_value(serde_json::json!({ "currentVersion": "0.179.0" })).unwrap();
157 assert_eq!(legacy.current_version, "0.179.0");
158 assert!(legacy.latest_version.is_none());
159 assert!(!legacy.update_available);
160 assert!(legacy.release_url.is_none());
161 assert!(legacy.release_highlights.is_empty());
162 assert!(legacy.current_release_highlights.is_empty());
163 assert!(legacy.current_release_url.is_none());
164 assert!(!legacy.current_release_seen);
165 assert!(legacy.channel.is_none());
166 assert!(!legacy.check_due);
167 assert_eq!(legacy.install, UpdateInstallState::Idle);
168 }
169
170 #[test]
173 fn install_state_round_trips_with_a_named_phase() {
174 let states = [
175 UpdateInstallState::Idle,
176 UpdateInstallState::Downloading { downloaded: 1846, total: Some(18462109) },
177 UpdateInstallState::Downloading { downloaded: 4096, total: None },
180 UpdateInstallState::Verifying,
181 UpdateInstallState::Extracting,
182 UpdateInstallState::Installing,
183 UpdateInstallState::Completed {
184 version: "0.181.8".into(),
185 needs_gatekeeper_approval: false,
186 },
187 UpdateInstallState::Failed { message: "network unreachable".into() },
188 ];
189
190 for state in states {
191 let encoded = serde_json::to_value(&state).unwrap();
192 let decoded: UpdateInstallState = serde_json::from_value(encoded.clone()).unwrap();
193 assert_eq!(decoded, state);
194 assert!(encoded.get("phase").is_some_and(|phase| phase.is_string()), "{encoded}");
195 }
196
197 let gated = UpdateInstallState::Completed {
199 version: "0.181.8".into(),
200 needs_gatekeeper_approval: true,
201 };
202 let decoded: UpdateInstallState = serde_json::from_value(serde_json::to_value(&gated).unwrap()).unwrap();
203 assert_eq!(decoded, gated);
204
205 let legacy: UpdateInstallState =
208 serde_json::from_value(serde_json::json!({ "phase": "completed", "version": "0.181.8" })).unwrap();
209 assert_eq!(
210 legacy,
211 UpdateInstallState::Completed {
212 version: "0.181.8".into(),
213 needs_gatekeeper_approval: false
214 }
215 );
216
217 assert_eq!(
218 serde_json::to_value(UpdateInstallState::Downloading { downloaded: 7, total: None }).unwrap(),
219 serde_json::json!({ "phase": "downloading", "downloaded": 7, "total": null })
220 );
221 }
222
223 #[test]
226 fn a_payload_without_install_decodes_as_idle() {
227 let decoded: UpdateStatusSnapshot =
228 serde_json::from_value(serde_json::json!({ "currentVersion": "0.181.7" })).unwrap();
229
230 assert_eq!(decoded.install, UpdateInstallState::Idle);
231 }
232
233 #[test]
234 fn only_active_phases_report_an_install_in_flight() {
235 assert!(!UpdateInstallState::Idle.is_in_flight());
236 assert!(
237 !UpdateInstallState::Completed {
238 version: "0.181.8".into(),
239 needs_gatekeeper_approval: true
240 }
241 .is_in_flight()
242 );
243 assert!(!UpdateInstallState::Failed { message: "boom".into() }.is_in_flight());
244 assert!(UpdateInstallState::Downloading { downloaded: 1, total: Some(2) }.is_in_flight());
245 assert!(UpdateInstallState::Verifying.is_in_flight());
246 assert!(UpdateInstallState::Extracting.is_in_flight());
247 assert!(UpdateInstallState::Installing.is_in_flight());
248 }
249}