//! Music, the kit's package of songs kept as files (Cotton Music, 4 Nov 2026), //! in a synthetic database: it imports as data, its views read, its parts //! read through their links, and a song's card says what it plays. //! Every artist, album and song here is invented; no recording is real. mod support; use base64::{engine::general_purpose::STANDARD, Engine}; use serde_json::{json, Value}; use support::{ mcp::{call_tool, mint_token}, Harness, }; fn create(h: &Harness, token: &str, table: &str, data: Value) -> String { let made = call_tool(h, token, "create_record", json!({"table": table, "data": data})); made["record"]["id"].as_str().unwrap_or_else(|| panic!("{table}: {made}")).to_string() } /// The columns only the file and the player write (a song's recording and /// length, an album's last play): read only to every door a person or an /// assistant writes through, so written here as the phone's own op, the way /// the intake and the player write them. fn as_the_file_says(h: &Harness, table: &str, id: &str, columns: Value) { use std::sync::atomic::{AtomicU32, Ordering}; static NEXT: AtomicU32 = AtomicU32::new(1); let n = NEXT.fetch_add(1, Ordering::Relaxed); h.seed(&[json!({ "opId": format!("op-the-file-says-{n}"), "deviceId": "device:phone", "kind": "upsert", "table": format!("cotton_{table}"), "rowId": id, "patch": columns, "hlc": format!("1900000000000:{n:04}:device:phone"), "schemaVersion": cotton_store::schema::ddl::SCHEMA_VERSION, })]); } fn song(h: &Harness, token: &str, data: Value, duration: i64, file: Option<&str>) -> String { let id = create(h, token, "song", data); as_the_file_says(h, "song", &id, json!({"duration": duration, "file": file})); id } fn read(h: &Harness, token: &str, view: &str, table: &str) -> Vec { let answer = call_tool(h, token, "list_records", json!({"table": table, "view": format!("music/{view}")})); answer["records"].as_array().unwrap_or_else(|| panic!("{view}: {answer}")).iter().map(|record| record["data"].clone()).collect() } fn said<'a>(rows: &'a [Value], key: &str) -> Vec<&'a str> { rows.iter().map(|row| row[key].as_str().unwrap_or_default()).collect() } /// RULE: Music installs from the kit as `import_tool` installs any package — /// its four tables are this database's own data — and each view reads its /// table: Songs the songs with their album and cover read through the album /// link, Albums by album artist with how many songs each holds and their /// whole length, Artists by sort name with their albums, Favourites the /// songs marked so, Recently added the newest first, and Listening the /// albums played lately. #[test] fn music_imports_into_a_synthetic_database_and_its_views_read() { let mut h = Harness::bare(); let token = mint_token(&mut h, &["read", "write"], "Synthetic listener"); let archive = cotton_core::kit::archive("music").expect("the kit holds Music").expect("Music packs"); let imported = call_tool(&h, &token, "import_tool", json!({"bytes": STANDARD.encode(archive)})); assert_eq!(imported["tool_id"], "music", "{imported}"); let again = call_tool(&h, &token, "add_tool", json!({"tool_id": "music"})); assert_eq!(again["added"], false, "Music is here once: {again}"); let quartet = create(&h, &token, "artist", json!({"name": "The Synthetic Quartet", "sort_name": "Synthetic Quartet"})); let mara = create(&h, &token, "artist", json!({"name": "Mara Invente", "sort_name": "Invente, Mara"})); let shores = create(&h, &token, "music_album", json!({ "title": "Made-Up Shores", "artist": quartet, "artist_name": "The Synthetic Quartet", "year": "2024", "cover": "photo:synthetic-shores-sleeve", })); as_the_file_says(&h, "music_album", &shores, json!({"last_played_at": "2026-10-20T09:00:00.000Z"})); let hills = create(&h, &token, "music_album", json!({"title": "Pretend Hills", "artist": mara, "artist_name": "Mara Invente", "year": "2021"})); song(&h, &token, json!({ "title": "Invented Tide", "artist_name": "The Synthetic Quartet", "artist": quartet, "album": shores, "track_number": 2, "favourite": true, "added_on": "2026-10-02", }), 222, Some("objects/invented-tide.m4a")); song(&h, &token, json!({ "title": "Placeholder Dunes", "artist_name": "The Synthetic Quartet", "artist": quartet, "album": shores, "track_number": 1, "added_on": "2026-10-01", }), 185, None); song(&h, &token, json!({ "title": "A Song Nobody Wrote", "artist_name": "Mara Invente", "artist": mara, "album": hills, "track_number": 1, "added_on": "2026-10-03", }), 200, None); let songs = read(&h, &token, "songs", "song"); assert_eq!(said(&songs, "title"), ["A Song Nobody Wrote", "Invented Tide", "Placeholder Dunes"], "Songs in title order"); assert_eq!(said(&songs, "album_title"), ["Pretend Hills", "Made-Up Shores", "Made-Up Shores"], "the album is read through the link"); assert_eq!(said(&songs, "cover")[1..], ["photo:synthetic-shores-sleeve"; 2], "a song's cover is its album's sleeve"); let albums = read(&h, &token, "albums", "music_album"); assert_eq!(said(&albums, "title"), ["Pretend Hills", "Made-Up Shores"], "by album artist"); assert_eq!(albums[1]["songs"].as_f64(), Some(2.0), "an album counts its songs: {}", albums[1]); assert_eq!(albums[1]["length"].as_f64(), Some(407.0), "and sums their lengths: {}", albums[1]); let artists = read(&h, &token, "artists", "artist"); assert_eq!(said(&artists, "name"), ["Mara Invente", "The Synthetic Quartet"], "by sort name"); assert_eq!(artists[1]["albums"].as_f64(), Some(1.0), "{}", artists[1]); assert_eq!(artists[1]["songs"].as_f64(), Some(2.0), "{}", artists[1]); assert_eq!(said(&read(&h, &token, "favourites", "song"), "title"), ["Invented Tide"]); assert_eq!(said(&read(&h, &token, "recently_added", "song"), "title"), ["A Song Nobody Wrote", "Invented Tide", "Placeholder Dunes"]); assert_eq!(said(&read(&h, &token, "listening", "music_album"), "title"), ["Made-Up Shores"], "only the albums played"); } /// RULE: a song's card (the 7-inch single), resolved from the song as a read /// answers it, says its title, its artist as tagged, its place on its album /// ("Track 2"), its length and the file it plays; an album's sleeve says its /// artist and year apart and its whole length (its songs' lengths summed, as /// a clock); an artist's divider says its name. The faces draw the places /// and sum nothing. #[test] fn a_song_an_album_and_an_artist_read_as_their_cards() { let mut h = Harness::bare(); let token = mint_token(&mut h, &["read", "write"], "Synthetic listener"); call_tool(&h, &token, "add_tool", json!({"tool_id": "music"})); let quartet = create(&h, &token, "artist", json!({"name": "The Synthetic Quartet", "sort_name": "Synthetic Quartet"})); let shores = create(&h, &token, "music_album", json!({"title": "Made-Up Shores", "artist": quartet, "artist_name": "The Synthetic Quartet", "year": "2024"})); song(&h, &token, json!({ "title": "Invented Tide", "artist_name": "The Synthetic Quartet", "artist": quartet, "album": shores, "track_number": 2, }), 222, Some("objects/invented-tide.m4a")); song(&h, &token, json!({"title": "Placeholder Dunes", "artist_name": "The Synthetic Quartet", "artist": quartet, "album": shores, "track_number": 1}), 185, None); let package = cotton_core::kit::package("music").unwrap().unwrap().package; let cards = cotton_core::rules::shell_lines::LineSet::shipped("cards").unwrap(); let words = cotton_core::records::page::Words::shipped(); let now = chrono::DateTime::parse_from_rfc3339("2026-10-23T10:00:00+05:30").unwrap().with_timezone(&chrono::Utc); let card = |table: &str, view: &str| { let definition = package.tables.iter().find(|definition| definition.id == table).unwrap(); let row = read(&h, &token, view, table).remove(0); cotton_core::records::card::resolve(definition.card.as_ref().unwrap(), definition, "r", row.as_object().unwrap(), "Asia/Kolkata", now, words, &cards, Default::default()) .expect("the card resolves") }; let song = card("song", "songs"); assert_eq!(song.face, "song.single"); assert_eq!(song.fields["title"], "Invented Tide", "{:?}", song.fields); assert_eq!(song.fields["subtitle"], "The Synthetic Quartet", "{:?}", song.fields); assert_eq!(song.fields["play"], "objects/invented-tide.m4a", "{:?}", song.fields); assert_eq!(song.fields["length"], "3:42", "{:?}", song.fields); assert_eq!(song.fields["number"], "Track 2", "{:?}", song.fields); let album = card("music_album", "albums"); assert_eq!(album.face, "album.sleeve"); assert_eq!((album.fields["subtitle"].as_str(), album.fields["year"].as_str()), (Some("The Synthetic Quartet"), Some("2024"))); assert_eq!(album.fields["length"], "6:47", "222 s and 185 s: {:?}", album.fields); let artist = card("artist", "artists"); assert_eq!(artist.face, "artist.divider"); assert_eq!(artist.fields["title"], "The Synthetic Quartet", "{:?}", artist.fields); }