| 1 | use serde_json::{Value, json}; |
| 2 | use session::{Method, Request}; |
| 3 | |
| 4 | use super::Pins; |
| 5 | use super::chats::{FOLDER_QUERY, NO_OP_CODES, api_error, ensure_success, with_answer_context}; |
| 6 | use super::state::{error_code, hierarchy_version}; |
| 7 | use super::transport::CsaTransport; |
| 8 | use crate::error::{Error, Result}; |
| 9 | use crate::messages::encode; |
| 10 | |
| 11 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 12 | pub enum FolderKind { |
| 13 | Favorites, |
| 14 | UserCreated, |
| 15 | Recent, |
| 16 | Meeting, |
| 17 | Muted, |
| 18 | } |
| 19 | |
| 20 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 21 | pub struct Folder { |
| 22 | pub id: String, |
| 23 | pub name: String, |
| 24 | pub kind: FolderKind, |
| 25 | pub expanded: bool, |
| 26 | pub conversation_ids: Vec<String>, |
| 27 | } |
| 28 | |
| 29 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 30 | pub struct Folders { |
| 31 | pub hierarchy_version: i64, |
| 32 | pub folders: Vec<Folder>, |
| 33 | pub order: Vec<String>, |
| 34 | } |
| 35 | |
| 36 | impl Folders { |
| 37 | fn find(&self, folder_id: &str) -> Result<&Folder> { |
| 38 | self.folders |
| 39 | .iter() |
| 40 | .find(|folder| folder.id == folder_id) |
| 41 | .ok_or_else(|| Error::UnknownFolder(folder_id.to_owned())) |
| 42 | } |
| 43 | |
| 44 | fn user_created(&self, folder_id: &str) -> Result<&Folder> { |
| 45 | let folder = self.find(folder_id)?; |
| 46 | if folder.kind != FolderKind::UserCreated { |
| 47 | return Err(Error::SystemFolder(folder_id.to_owned())); |
| 48 | } |
| 49 | Ok(folder) |
| 50 | } |
| 51 | |
| 52 | fn containing(&self, conversation_id: &str) -> Option<&Folder> { |
| 53 | self.folders.iter().find(|folder| { |
| 54 | matches!(folder.kind, FolderKind::Favorites | FolderKind::UserCreated) |
| 55 | && folder |
| 56 | .conversation_ids |
| 57 | .iter() |
| 58 | .any(|id| id == conversation_id) |
| 59 | }) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | pub fn parse_folders(body: &Value) -> Result<Folders> { |
| 64 | let hierarchy_version = hierarchy_version(body)?; |
| 65 | let raw = body |
| 66 | .get("conversationFolders") |
| 67 | .and_then(Value::as_array) |
| 68 | .ok_or_else(|| Error::UnexpectedAnswer("conversationFolders missing".into()))?; |
| 69 | let mut folders: Vec<Folder> = raw.iter().filter_map(parse_folder).collect(); |
| 70 | let parsed_ids: Vec<String> = folders.iter().map(|folder| folder.id.clone()).collect(); |
| 71 | let mut order: Vec<String> = body |
| 72 | .get("conversationFolderOrder") |
| 73 | .and_then(Value::as_array) |
| 74 | .map(|ids| { |
| 75 | ids.iter() |
| 76 | .filter_map(Value::as_str) |
| 77 | .map(str::to_owned) |
| 78 | .collect() |
| 79 | }) |
| 80 | .unwrap_or_default(); |
| 81 | if order.is_empty() { |
| 82 | order = parsed_ids; |
| 83 | } else { |
| 84 | let rank = |folder: &Folder| { |
| 85 | order |
| 86 | .iter() |
| 87 | .position(|id| *id == folder.id) |
| 88 | .unwrap_or(usize::MAX) |
| 89 | }; |
| 90 | folders.sort_by_key(rank); |
| 91 | } |
| 92 | Ok(Folders { |
| 93 | hierarchy_version, |
| 94 | folders, |
| 95 | order, |
| 96 | }) |
| 97 | } |
| 98 | |
| 99 | fn parse_folder(folder: &Value) -> Option<Folder> { |
| 100 | if folder.get("isDeleted").and_then(Value::as_bool) == Some(true) { |
| 101 | return None; |
| 102 | } |
| 103 | let kind = match folder.get("folderType").and_then(Value::as_str)? { |
| 104 | "Favorites" => FolderKind::Favorites, |
| 105 | "UserCreated" => FolderKind::UserCreated, |
| 106 | "RecentChats" => FolderKind::Recent, |
| 107 | "MeetingChats" => FolderKind::Meeting, |
| 108 | "MutedChats" => FolderKind::Muted, |
| 109 | _ => return None, |
| 110 | }; |
| 111 | Some(Folder { |
| 112 | id: folder.get("id")?.as_str()?.to_owned(), |
| 113 | name: folder |
| 114 | .get("name") |
| 115 | .and_then(Value::as_str) |
| 116 | .unwrap_or_default() |
| 117 | .to_owned(), |
| 118 | kind, |
| 119 | expanded: folder |
| 120 | .get("isExpanded") |
| 121 | .and_then(Value::as_bool) |
| 122 | .unwrap_or(true), |
| 123 | conversation_ids: folder |
| 124 | .get("conversationFolderItems") |
| 125 | .and_then(Value::as_array) |
| 126 | .map(|items| { |
| 127 | items |
| 128 | .iter() |
| 129 | .filter_map(|item| item.get("conversationId").and_then(Value::as_str)) |
| 130 | .map(str::to_owned) |
| 131 | .collect() |
| 132 | }) |
| 133 | .unwrap_or_default(), |
| 134 | }) |
| 135 | } |
| 136 | |
| 137 | fn expanded_body(hierarchy_version: i64, expanded: bool) -> Value { |
| 138 | json!({ |
| 139 | "folderHierarchyVersion": hierarchy_version, |
| 140 | "supportsAdditionalSystemGeneratedFolders": true, |
| 141 | "supportsSliceItems": true, |
| 142 | "isExpanded": expanded, |
| 143 | }) |
| 144 | } |
| 145 | |
| 146 | fn add_item(folder_id: &str, conversation_id: &str, index: Option<usize>) -> Value { |
| 147 | let mut action = json!({"action": "AddItem", "folderId": folder_id, "itemId": conversation_id}); |
| 148 | if let Some(index) = index { |
| 149 | action["itemIndex"] = json!(index); |
| 150 | } |
| 151 | action |
| 152 | } |
| 153 | |
| 154 | fn reorder_items(folder_id: &str, conversation_ids: Vec<String>) -> Value { |
| 155 | json!({"action": "ReorderItems", "folderId": folder_id, "newItemsOrder": conversation_ids}) |
| 156 | } |
| 157 | |
| 158 | fn placed_order(conversation_ids: &[String], conversation_id: &str, index: usize) -> Vec<String> { |
| 159 | let mut order: Vec<String> = conversation_ids |
| 160 | .iter() |
| 161 | .filter(|id| *id != conversation_id) |
| 162 | .cloned() |
| 163 | .collect(); |
| 164 | order.insert(index.min(order.len()), conversation_id.to_owned()); |
| 165 | order |
| 166 | } |
| 167 | |
| 168 | fn place_actions( |
| 169 | state: &Folders, |
| 170 | conversation_id: &str, |
| 171 | target_folder_id: &str, |
| 172 | index: Option<usize>, |
| 173 | ) -> Result<Vec<Value>> { |
| 174 | let target = state.find(target_folder_id)?; |
| 175 | if target |
| 176 | .conversation_ids |
| 177 | .iter() |
| 178 | .any(|id| id == conversation_id) |
| 179 | { |
| 180 | let Some(index) = index else { |
| 181 | return Ok(Vec::new()); |
| 182 | }; |
| 183 | let order = placed_order(&target.conversation_ids, conversation_id, index); |
| 184 | if order == target.conversation_ids { |
| 185 | return Ok(Vec::new()); |
| 186 | } |
| 187 | return Ok(vec![reorder_items(target_folder_id, order)]); |
| 188 | } |
| 189 | let mut actions = vec![add_item(target_folder_id, conversation_id, index)]; |
| 190 | if let Some(current) = state.containing(conversation_id) { |
| 191 | actions.push(remove_item(¤t.id, conversation_id)); |
| 192 | } |
| 193 | Ok(actions) |
| 194 | } |
| 195 | |
| 196 | fn remove_item(folder_id: &str, conversation_id: &str) -> Value { |
| 197 | json!({"action": "RemoveItem", "folderId": folder_id, "itemId": conversation_id}) |
| 198 | } |
| 199 | |
| 200 | struct FolderWrite { |
| 201 | method: Method, |
| 202 | folder_id: Option<String>, |
| 203 | fields: Value, |
| 204 | answer_is_state: bool, |
| 205 | } |
| 206 | |
| 207 | impl FolderWrite { |
| 208 | fn actions(actions: Vec<Value>) -> Self { |
| 209 | FolderWrite { |
| 210 | method: Method::Post, |
| 211 | folder_id: None, |
| 212 | fields: json!({"actions": actions}), |
| 213 | answer_is_state: true, |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | fn reread(method: Method, folder_id: Option<&str>, fields: Value) -> Self { |
| 218 | FolderWrite { |
| 219 | method, |
| 220 | folder_id: folder_id.map(str::to_owned), |
| 221 | fields, |
| 222 | answer_is_state: false, |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | fn body(&self, hierarchy_version: i64) -> Value { |
| 227 | let mut body = json!({ |
| 228 | "folderHierarchyVersion": hierarchy_version, |
| 229 | "supportsAdditionalSystemGeneratedFolders": true, |
| 230 | "supportsSliceItems": true, |
| 231 | }); |
| 232 | if let (Some(target), Some(fields)) = (body.as_object_mut(), self.fields.as_object()) { |
| 233 | target.extend(fields.clone()); |
| 234 | } |
| 235 | body |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | fn reordered(order: &[String], folder_id: &str, target_folder_id: &str) -> Result<Vec<String>> { |
| 240 | let position = |id: &str| { |
| 241 | order |
| 242 | .iter() |
| 243 | .position(|candidate| candidate == id) |
| 244 | .ok_or_else(|| Error::UnknownFolder(id.to_owned())) |
| 245 | }; |
| 246 | let (source, target) = (position(folder_id)?, position(target_folder_id)?); |
| 247 | let mut reordered = order.to_vec(); |
| 248 | let moved = reordered.remove(source); |
| 249 | reordered.insert(target, moved); |
| 250 | Ok(reordered) |
| 251 | } |
| 252 | |
| 253 | fn is_full_state(body: &Value) -> bool { |
| 254 | body.get("isPartialResponse").and_then(Value::as_bool) != Some(true) |
| 255 | } |
| 256 | |
| 257 | impl<T: CsaTransport> Pins<T> { |
| 258 | pub async fn folders(&self) -> Result<Folders> { |
| 259 | let url = format!("{}/conversationFolders?{FOLDER_QUERY}", self.base_url); |
| 260 | let answer = self.transport.send(Request::get(url)).await?; |
| 261 | ensure_success(&answer)?; |
| 262 | with_answer_context(parse_folders(&answer.body), &answer) |
| 263 | } |
| 264 | |
| 265 | pub async fn move_to_folder( |
| 266 | &self, |
| 267 | conversation_id: &str, |
| 268 | target_folder_id: &str, |
| 269 | index: Option<usize>, |
| 270 | ) -> Result<Folders> { |
| 271 | self.write_folders(|state| place_actions(state, conversation_id, target_folder_id, index)) |
| 272 | .await |
| 273 | } |
| 274 | |
| 275 | pub async fn remove_from_folder( |
| 276 | &self, |
| 277 | conversation_id: &str, |
| 278 | folder_id: &str, |
| 279 | ) -> Result<Folders> { |
| 280 | self.write_folders(|state| { |
| 281 | let folder = state |
| 282 | .folders |
| 283 | .iter() |
| 284 | .find(|folder| folder.id == folder_id) |
| 285 | .ok_or_else(|| Error::UnknownFolder(folder_id.to_owned()))?; |
| 286 | if !folder |
| 287 | .conversation_ids |
| 288 | .iter() |
| 289 | .any(|id| id == conversation_id) |
| 290 | { |
| 291 | return Ok(Vec::new()); |
| 292 | } |
| 293 | Ok(vec![remove_item(folder_id, conversation_id)]) |
| 294 | }) |
| 295 | .await |
| 296 | } |
| 297 | |
| 298 | pub async fn create_folder(&self, name: &str) -> Result<Folders> { |
| 299 | self.write(|_| { |
| 300 | Ok(Some(FolderWrite::reread( |
| 301 | Method::Post, |
| 302 | None, |
| 303 | json!({"folderName": name, "actions": [{"action": "AddFolder"}]}), |
| 304 | ))) |
| 305 | }) |
| 306 | .await |
| 307 | } |
| 308 | |
| 309 | pub async fn rename_folder(&self, folder_id: &str, name: &str) -> Result<Folders> { |
| 310 | self.write(|state| { |
| 311 | if state.user_created(folder_id)?.name == name { |
| 312 | return Ok(None); |
| 313 | } |
| 314 | Ok(Some(FolderWrite::reread( |
| 315 | Method::Put, |
| 316 | Some(folder_id), |
| 317 | json!({"folderName": name}), |
| 318 | ))) |
| 319 | }) |
| 320 | .await |
| 321 | } |
| 322 | |
| 323 | pub async fn delete_folder(&self, folder_id: &str) -> Result<Folders> { |
| 324 | self.write(|state| { |
| 325 | let Some(folder) = state.folders.iter().find(|folder| folder.id == folder_id) else { |
| 326 | return Ok(None); |
| 327 | }; |
| 328 | if folder.kind != FolderKind::UserCreated { |
| 329 | return Err(Error::SystemFolder(folder_id.to_owned())); |
| 330 | } |
| 331 | Ok(Some(FolderWrite::reread( |
| 332 | Method::Delete, |
| 333 | Some(folder_id), |
| 334 | json!({}), |
| 335 | ))) |
| 336 | }) |
| 337 | .await |
| 338 | } |
| 339 | |
| 340 | pub async fn move_folder(&self, folder_id: &str, target_folder_id: &str) -> Result<Folders> { |
| 341 | self.write(|state| { |
| 342 | let order = reordered(&state.order, folder_id, target_folder_id)?; |
| 343 | if order == state.order { |
| 344 | return Ok(None); |
| 345 | } |
| 346 | Ok(Some(FolderWrite::actions(vec![ |
| 347 | json!({"action": "ReorderFolders", "newFoldersOrder": order}), |
| 348 | ]))) |
| 349 | }) |
| 350 | .await |
| 351 | } |
| 352 | |
| 353 | pub async fn set_folder_expanded(&self, folder_id: &str, expanded: bool) -> Result<()> { |
| 354 | let mut hierarchy_version = self.folders().await?.hierarchy_version; |
| 355 | for attempt in 0..2 { |
| 356 | let url = format!( |
| 357 | "{}/conversationFolders/{}?{FOLDER_QUERY}", |
| 358 | self.base_url, |
| 359 | encode(folder_id) |
| 360 | ); |
| 361 | let body = expanded_body(hierarchy_version, expanded); |
| 362 | let answer = self |
| 363 | .transport |
| 364 | .send(Request::with_body(Method::Put, url, body)) |
| 365 | .await?; |
| 366 | match answer.status { |
| 367 | 200..=299 => return Ok(()), |
| 368 | 412 if attempt == 0 => { |
| 369 | hierarchy_version = match parse_folders(&answer.body) { |
| 370 | Ok(current) => current.hierarchy_version, |
| 371 | Err(_) => self.folders().await?.hierarchy_version, |
| 372 | }; |
| 373 | } |
| 374 | 412 => return Err(Error::VersionConflict), |
| 375 | _ => return Err(api_error(&answer)), |
| 376 | } |
| 377 | } |
| 378 | Err(Error::VersionConflict) |
| 379 | } |
| 380 | |
| 381 | async fn write_folders( |
| 382 | &self, |
| 383 | plan: impl Fn(&Folders) -> Result<Vec<Value>>, |
| 384 | ) -> Result<Folders> { |
| 385 | self.write(|state| { |
| 386 | let actions = plan(state)?; |
| 387 | Ok((!actions.is_empty()).then(|| FolderWrite::actions(actions))) |
| 388 | }) |
| 389 | .await |
| 390 | } |
| 391 | |
| 392 | async fn write( |
| 393 | &self, |
| 394 | plan: impl Fn(&Folders) -> Result<Option<FolderWrite>>, |
| 395 | ) -> Result<Folders> { |
| 396 | let mut state = self.folders().await?; |
| 397 | for attempt in 0..2 { |
| 398 | let Some(write) = plan(&state)? else { |
| 399 | return Ok(state); |
| 400 | }; |
| 401 | let path = match &write.folder_id { |
| 402 | Some(folder_id) => format!("conversationFolders/{}", encode(folder_id)), |
| 403 | None => "conversationFolders".to_owned(), |
| 404 | }; |
| 405 | let url = format!("{}/{path}?{FOLDER_QUERY}", self.base_url); |
| 406 | let request = |
| 407 | Request::with_body(write.method, url, write.body(state.hierarchy_version)); |
| 408 | let answer = self.transport.send(request).await?; |
| 409 | match answer.status { |
| 410 | 200..=299 => { |
| 411 | return match parse_folders(&answer.body) { |
| 412 | Ok(current) if write.answer_is_state && is_full_state(&answer.body) => { |
| 413 | Ok(current) |
| 414 | } |
| 415 | _ => self.folders().await, |
| 416 | }; |
| 417 | } |
| 418 | 412 if attempt == 0 => { |
| 419 | state = match parse_folders(&answer.body) { |
| 420 | Ok(current) => current, |
| 421 | Err(_) => self.folders().await?, |
| 422 | }; |
| 423 | } |
| 424 | 412 => return Err(Error::VersionConflict), |
| 425 | 400 if error_code(&answer.body).is_some_and(|code| NO_OP_CODES.contains(&code)) => { |
| 426 | return self.folders().await; |
| 427 | } |
| 428 | _ => return Err(api_error(&answer)), |
| 429 | } |
| 430 | } |
| 431 | Err(Error::VersionConflict) |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | #[cfg(test)] |
| 436 | mod tests { |
| 437 | use super::*; |
| 438 | |
| 439 | #[test] |
| 440 | fn keeps_folders_in_server_order() { |
| 441 | let body = json!({ |
| 442 | "folderHierarchyVersion": 5, |
| 443 | "conversationFolderOrder": ["b", "a", "r"], |
| 444 | "conversationFolders": [ |
| 445 | {"id": "a", "name": "A", "folderType": "UserCreated", "conversationFolderItems": [{"conversationId": "c1"}, {"conversationId": "c2"}]}, |
| 446 | {"id": "r", "folderType": "RecentChats", "conversationFolderItems": []}, |
| 447 | {"id": "b", "name": "Pins", "folderType": "Favorites", "conversationFolderItems": []}, |
| 448 | {"id": "x", "folderType": "UserCreated", "isDeleted": true} |
| 449 | ] |
| 450 | }); |
| 451 | let parsed = parse_folders(&body).unwrap(); |
| 452 | let ids: Vec<&str> = parsed |
| 453 | .folders |
| 454 | .iter() |
| 455 | .map(|folder| folder.id.as_str()) |
| 456 | .collect(); |
| 457 | assert_eq!(ids, ["b", "a", "r"]); |
| 458 | assert_eq!(parsed.folders[1].conversation_ids, ["c1", "c2"]); |
| 459 | assert_eq!(parsed.folders[0].kind, FolderKind::Favorites); |
| 460 | } |
| 461 | |
| 462 | #[test] |
| 463 | fn falls_back_to_parsed_ids_without_folder_order() { |
| 464 | let body = json!({ |
| 465 | "folderHierarchyVersion": 5, |
| 466 | "conversationFolders": [ |
| 467 | {"id": "a", "name": "A", "folderType": "UserCreated", "conversationFolderItems": []}, |
| 468 | {"id": "r", "folderType": "RecentChats", "conversationFolderItems": []} |
| 469 | ] |
| 470 | }); |
| 471 | let parsed = parse_folders(&body).unwrap(); |
| 472 | assert_eq!(parsed.order, ["a", "r"]); |
| 473 | } |
| 474 | |
| 475 | #[test] |
| 476 | fn keeps_system_folders_with_id_order_and_expanded_state() { |
| 477 | let body = json!({ |
| 478 | "folderHierarchyVersion": 5, |
| 479 | "conversationFolderOrder": ["m", "f", "r", "u"], |
| 480 | "conversationFolders": [ |
| 481 | {"id": "r", "folderType": "RecentChats", "isExpanded": true, "conversationFolderItems": []}, |
| 482 | {"id": "u", "folderType": "MutedChats", "isExpanded": false, "conversationFolderItems": []}, |
| 483 | {"id": "m", "folderType": "MeetingChats", "conversationFolderItems": []}, |
| 484 | {"id": "f", "name": "Pins", "folderType": "Favorites", "conversationFolderItems": []}, |
| 485 | {"id": "q", "folderType": "QuickViews", "conversationFolderItems": []} |
| 486 | ] |
| 487 | }); |
| 488 | let parsed = parse_folders(&body).unwrap(); |
| 489 | let summary: Vec<(&str, FolderKind, bool)> = parsed |
| 490 | .folders |
| 491 | .iter() |
| 492 | .map(|folder| (folder.id.as_str(), folder.kind, folder.expanded)) |
| 493 | .collect(); |
| 494 | assert_eq!( |
| 495 | summary, |
| 496 | [ |
| 497 | ("m", FolderKind::Meeting, true), |
| 498 | ("f", FolderKind::Favorites, true), |
| 499 | ("r", FolderKind::Recent, true), |
| 500 | ("u", FolderKind::Muted, false), |
| 501 | ] |
| 502 | ); |
| 503 | } |
| 504 | |
| 505 | #[test] |
| 506 | fn reorder_swaps_with_an_adjacent_target_in_both_directions() { |
| 507 | let order: Vec<String> = ["q", "a", "f", "r", "m"].map(String::from).to_vec(); |
| 508 | assert_eq!( |
| 509 | reordered(&order, "f", "a").unwrap(), |
| 510 | ["q", "f", "a", "r", "m"] |
| 511 | ); |
| 512 | assert_eq!( |
| 513 | reordered(&order, "f", "r").unwrap(), |
| 514 | ["q", "a", "r", "f", "m"] |
| 515 | ); |
| 516 | assert!(matches!( |
| 517 | reordered(&order, "f", "x"), |
| 518 | Err(Error::UnknownFolder(_)) |
| 519 | )); |
| 520 | } |
| 521 | |
| 522 | fn state(folders: &[(&str, FolderKind, &[&str])]) -> Folders { |
| 523 | Folders { |
| 524 | hierarchy_version: 1, |
| 525 | folders: folders |
| 526 | .iter() |
| 527 | .map(|(id, kind, ids)| Folder { |
| 528 | id: (*id).to_owned(), |
| 529 | name: (*id).to_owned(), |
| 530 | kind: *kind, |
| 531 | expanded: true, |
| 532 | conversation_ids: ids.iter().map(|id| (*id).to_owned()).collect(), |
| 533 | }) |
| 534 | .collect(), |
| 535 | order: Vec::new(), |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | #[test] |
| 540 | fn placing_inside_the_same_folder_sends_the_full_new_order() { |
| 541 | let folders = state(&[("fav", FolderKind::Favorites, &["a", "b", "c", "d"])]); |
| 542 | assert_eq!( |
| 543 | place_actions(&folders, "a", "fav", Some(2)).unwrap(), |
| 544 | [ |
| 545 | json!({"action": "ReorderItems", "folderId": "fav", "newItemsOrder": ["b", "c", "a", "d"]}) |
| 546 | ] |
| 547 | ); |
| 548 | assert_eq!( |
| 549 | place_actions(&folders, "d", "fav", Some(1)).unwrap(), |
| 550 | [ |
| 551 | json!({"action": "ReorderItems", "folderId": "fav", "newItemsOrder": ["a", "d", "b", "c"]}) |
| 552 | ] |
| 553 | ); |
| 554 | assert!( |
| 555 | place_actions(&folders, "b", "fav", Some(1)) |
| 556 | .unwrap() |
| 557 | .is_empty() |
| 558 | ); |
| 559 | assert!( |
| 560 | place_actions(&folders, "b", "fav", None) |
| 561 | .unwrap() |
| 562 | .is_empty() |
| 563 | ); |
| 564 | } |
| 565 | |
| 566 | #[test] |
| 567 | fn moving_out_of_favorites_adds_at_the_index_then_removes_like_teams() { |
| 568 | let folders = state(&[ |
| 569 | ("fav", FolderKind::Favorites, &["a"]), |
| 570 | ("work", FolderKind::UserCreated, &["b", "c"]), |
| 571 | ]); |
| 572 | assert_eq!( |
| 573 | place_actions(&folders, "a", "work", Some(1)).unwrap(), |
| 574 | [ |
| 575 | json!({"action": "AddItem", "folderId": "work", "itemId": "a", "itemIndex": 1}), |
| 576 | json!({"action": "RemoveItem", "folderId": "fav", "itemId": "a"}), |
| 577 | ] |
| 578 | ); |
| 579 | assert_eq!( |
| 580 | place_actions(&folders, "b", "fav", None).unwrap(), |
| 581 | [ |
| 582 | json!({"action": "AddItem", "folderId": "fav", "itemId": "b"}), |
| 583 | json!({"action": "RemoveItem", "folderId": "work", "itemId": "b"}), |
| 584 | ] |
| 585 | ); |
| 586 | } |
| 587 | |
| 588 | #[test] |
| 589 | fn write_body_puts_the_lock_before_the_fields() { |
| 590 | let write = FolderWrite::reread(Method::Put, Some("a"), json!({"folderName": "B"})); |
| 591 | assert_eq!( |
| 592 | write.body(3), |
| 593 | json!({ |
| 594 | "folderHierarchyVersion": 3, |
| 595 | "supportsAdditionalSystemGeneratedFolders": true, |
| 596 | "supportsSliceItems": true, |
| 597 | "folderName": "B", |
| 598 | }) |
| 599 | ); |
| 600 | } |
| 601 | |
| 602 | #[test] |
| 603 | fn expanded_body_carries_only_the_flag_and_the_lock() { |
| 604 | assert_eq!( |
| 605 | expanded_body(7, false), |
| 606 | json!({ |
| 607 | "folderHierarchyVersion": 7, |
| 608 | "supportsAdditionalSystemGeneratedFolders": true, |
| 609 | "supportsSliceItems": true, |
| 610 | "isExpanded": false, |
| 611 | }) |
| 612 | ); |
| 613 | } |
| 614 | } |