use cotton_core::definitions::TableDefinition; use cotton_core::kit::AppKit; use super::*; fn tables() -> Vec { [ include_str!("../../../cotton-core/kits/personal/tables/book.json"), include_str!("../../../cotton-core/kits/personal/tables/song.json"), include_str!("../../../cotton-core/kits/personal/tables/film.json"), ] .iter() .map(|raw| TableDefinition::from_json(raw).expect("a kit table")) .collect() } fn door(kit: &str, mime: Option<&str>, name: Option<&str>) -> Option<(String, String)> { door_for(AppKit::named(kit).as_ref(), &tables(), mime, name).map(|(table, door)| (table, door["id"].as_str().unwrap().to_string())) } /// RULE (15 Nov 2026): a file shared into Books, Music or /// Cinemas lands through the door its Plus would take it by: Books' Add a file /// or Bring Goodreads in, Music's and Cinemas' Add files. What no door of the /// app takes goes on to the app's own branch, and Cotton is answered nothing. #[test] fn a_shared_file_takes_the_door_its_plus_would() { let at = |table: &str, door: &str| Some((table.to_string(), door.to_string())); assert_eq!(door("books", Some("application/epub+zip"), Some("Harbour.epub")), at("book", "file")); assert_eq!(door("books", Some("application/pdf"), None), at("book", "file")); assert_eq!(door("books", Some("text/comma-separated-values"), Some("goodreads_library_export.csv")), at("book", "goodreads")); assert_eq!(door("books", Some("audio/mpeg"), Some("a.mp3")), None, "a song is not Books'"); // Music is retired (TABLE DROP 1, 27 Nov 2026): its song table is no kit's now, so nothing is shared into it. assert_eq!(door("music", Some("audio/flac"), Some("01 Low Tide.flac")), None, "a retired app takes no file"); assert_eq!(door("cinemas", Some("application/octet-stream"), Some("Harbour (2019).mkv")), at("film", "files")); assert_eq!(door("cinemas", Some("video/mp4"), None), at("film", "files")); assert_eq!(door("cinemas", Some("image/jpeg"), Some("poster.jpg")), None); assert_eq!(door_for(None, &tables(), Some("application/pdf"), None), None, "Cotton takes shares by its own drain"); } /// The door comes as `shell.plus` draws it: the shell hands it to the same /// intake a picked file goes through. #[test] fn the_door_is_drawn_as_plus_draws_it() { let (_, drawn) = door_for(AppKit::named("books").as_ref(), &tables(), Some("application/pdf"), None).expect("a door"); assert_eq!(drawn["accepts"]["files"]["mimes"], json!(["application/epub+zip", "application/pdf"])); assert_eq!(drawn["label"], "Add a file"); } /// The media types an Android app's manifest takes shares and Open with by, /// each once (every `android:mimeType` it declares: only its share filters /// name one). fn manifest_mimes(app: &str) -> std::collections::BTreeSet { let path = cotton_store::manifest_dir().join(format!("../../apps/android/app/src/{app}/AndroidManifest.xml")); let manifest = std::fs::read_to_string(&path).unwrap_or_else(|_| panic!("{}", path.display())); manifest.split("android:mimeType=\"").skip(1).map(|rest| rest[..rest.find('"').expect("a closed attribute")].to_string()).collect() } /// RULE (15 Nov 2026): Books, Music and Cinemas on /// Android take shares and Open with for exactly the media types of their /// kit's file doors (a folder door aside), so the share sheet offers the app /// what `share.door` lands, and nothing it would turn away. #[test] fn an_android_app_takes_shares_of_its_doors_types() { // Music is retired (TABLE DROP 1): it has no door, and its Android manifest is the Android lane's to retire. for app in ["books", "cinemas"] { let kit = AppKit::named(app).expect("ships"); let mut doors = std::collections::BTreeSet::new(); for table in tables().iter().filter(|table| kit.tables.contains(&table.id)) { for door in table.create.iter().flat_map(|create| create.doors.iter()) { for kind in door.files.iter().flat_map(|files| files.types.iter()).filter(|kind| *kind != FOLDER) { doors.extend(cotton_core::definitions::file_type_mimes(kind).expect("a known type").iter().map(|mime| mime.to_string())); } } } assert!(!doors.is_empty(), "{app} takes files"); assert_eq!(manifest_mimes(app), doors, "apps/android/app/src/{app}/AndroidManifest.xml's share types"); } }