Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / share / src / main / rust / src / lib_tests.rs
lib_tests.rs92 lines · 3.5 KiBRaw
| 1 | //! Tests for the pure helpers in `lib.rs`: the file-list JSON codec |
| 2 | //! (PROTOCOL_CONTRACT.md §6) and the global session registry. |
| 3 | |
| 4 | use super::{file_list_json, json_escape, parse_file_list_json, sessions, with_session}; |
| 5 | use crate::payload::FileMeta; |
| 6 | use crate::session::{Role, Session}; |
| 7 | |
| 8 | fn meta(name: &str, size: u64, mime: &str) -> FileMeta { |
| 9 | FileMeta { |
| 10 | name: name.to_string(), |
| 11 | size_bytes: size, |
| 12 | mime_type: mime.to_string(), |
| 13 | payload_id: 0, |
| 14 | } |
| 15 | } |
| 16 | |
| 17 | #[test] |
| 18 | fn json_escape_quotes_backslashes_only() { |
| 19 | assert_eq!(json_escape("plain.txt"), "plain.txt"); |
| 20 | assert_eq!(json_escape("a\"b"), "a\\\"b"); |
| 21 | assert_eq!(json_escape("a\\b"), "a\\\\b"); |
| 22 | assert_eq!(json_escape("\\\""), "\\\\\\\""); |
| 23 | } |
| 24 | |
| 25 | #[test] |
| 26 | fn file_list_json_writes_the_contract_shape() { |
| 27 | assert_eq!(file_list_json(&[]), "[]"); |
| 28 | assert_eq!( |
| 29 | file_list_json(&[meta("a.txt", 12, "text/plain")]), |
| 30 | "[{\"name\":\"a.txt\",\"sizeBytes\":12,\"mimeType\":\"text/plain\"}]" |
| 31 | ); |
| 32 | assert_eq!( |
| 33 | file_list_json(&[meta("a.txt", 1, "text/plain"), meta("b.bin", 2, "")]), |
| 34 | "[{\"name\":\"a.txt\",\"sizeBytes\":1,\"mimeType\":\"text/plain\"},{\"name\":\"b.bin\",\"sizeBytes\":2,\"mimeType\":\"\"}]" |
| 35 | ); |
| 36 | } |
| 37 | |
| 38 | #[test] |
| 39 | fn parse_file_list_json_reads_the_contract_shape() { |
| 40 | assert_eq!(parse_file_list_json("[]"), Some(Vec::new())); |
| 41 | assert_eq!(parse_file_list_json(" [ ] "), Some(Vec::new())); |
| 42 | let parsed = |
| 43 | parse_file_list_json("[{\"name\":\"a.txt\",\"sizeBytes\":12,\"mimeType\":\"text/plain\"}]") |
| 44 | .expect("parses"); |
| 45 | assert_eq!(parsed, vec![meta("a.txt", 12, "text/plain")]); |
| 46 | // A missing mimeType defaults to empty; order of keys is irrelevant |
| 47 | // to the field extraction. |
| 48 | let parsed = parse_file_list_json("[{\"sizeBytes\":7,\"name\":\"b.bin\"}]").expect("parses"); |
| 49 | assert_eq!(parsed, vec![meta("b.bin", 7, "")]); |
| 50 | } |
| 51 | |
| 52 | #[test] |
| 53 | fn parse_file_list_json_rejects_malformed_input() { |
| 54 | assert_eq!(parse_file_list_json("not json"), None); |
| 55 | assert_eq!(parse_file_list_json("[{\"sizeBytes\":1}]"), None, "no name"); |
| 56 | assert_eq!(parse_file_list_json("[{\"name\":\"a\"}]"), None, "no size"); |
| 57 | assert_eq!(parse_file_list_json(""), None); |
| 58 | } |
| 59 | |
| 60 | #[test] |
| 61 | fn the_file_list_codec_round_trips_awkward_names() { |
| 62 | // Names with quotes and backslashes are legal file names; the |
| 63 | // encoder escapes them, so the parser must unescape them or the |
| 64 | // receiver announces a different name than the sender sent. |
| 65 | let files = vec![ |
| 66 | meta("my \"quoted\" file.txt", 3, "text/plain"), |
| 67 | meta("back\\slash.bin", 4, "application/octet-stream"), |
| 68 | meta("plain.dat", 5, ""), |
| 69 | meta("a},b.txt", 6, "text/plain"), |
| 70 | ]; |
| 71 | let json = file_list_json(&files); |
| 72 | assert_eq!(parse_file_list_json(&json), Some(files)); |
| 73 | } |
| 74 | |
| 75 | #[test] |
| 76 | fn with_session_applies_to_a_live_handle_and_falls_back_for_a_dead_one() { |
| 77 | const HANDLE: i64 = 0x5eed_0001; |
| 78 | let expected = Session::new(Role::Responder, "Registry".to_string(), Vec::new(), String::new()) |
| 79 | .query_state(); |
| 80 | sessions().lock().unwrap().insert( |
| 81 | HANDLE, |
| 82 | Session::new(Role::Responder, "Registry".to_string(), Vec::new(), String::new()), |
| 83 | ); |
| 84 | assert_eq!(with_session(HANDLE, -9, |s| s.query_state()), expected); |
| 85 | assert_eq!(with_session(HANDLE + 1, -9, |s| s.query_state()), -9); |
| 86 | // The closure really receives the stored session, mutable. |
| 87 | with_session(HANDLE, (), |s| { |
| 88 | let _ = s.outbound_drain(); |
| 89 | }); |
| 90 | sessions().lock().unwrap().remove(&HANDLE); |
| 91 | assert_eq!(with_session(HANDLE, -9, |s| s.query_state()), -9); |
| 92 | } |