package com.cottondb.music import com.cottondb.music.faces.Card import com.cottondb.music.faces.CassetteKnobs import com.cottondb.music.faces.CassetteSong import com.cottondb.music.faces.CassetteTape import com.cottondb.music.faces.CassetteWords import com.cottondb.music.faces.ListCassette import com.cottondb.music.faces.Measure import com.cottondb.music.faces.SongCassette import com.cottondb.music.faces.TrackPlay import com.cottondb.shell.core.Lines import kotlinx.serialization.json.Json import kotlinx.serialization.json.jsonObject import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse import org.junit.Assert.assertNull import org.junit.Assert.assertTrue import org.junit.Test import kotlin.math.sqrt /** The cassette's setting rules (CassetteParts.swift, SongCassetteFace, ListCassetteFace), on invented cards. */ class CassetteSettingTest { private val words = Lines(mapOf("cards.cassette.more_short" to "+{n}"), "Cotton Music") /** Every letter half its size wide. */ private val m = Measure { said, _, size -> said.length * size * 0.5f } private fun card(slots: String, face: String) = Card(Json.parseToJsonElement("""{"id":"c","face":"$face","slots":$slots}""").jsonObject) private val nightDrives = card( """{"title":"Night Drives","count":"3 songs","length":"11:41","songs":[ {"title":"Harbour Lights","duration":221},{"title":"Slow Train","duration":302},{"title":"Blue Hour","duration":178,"unavailable":true}]}""", "list.cassette", ) @Test fun aListsTapeReadsItsSongs() { val said = ListCassette.words(nightDrives) assertEquals("Night Drives", said.title) assertEquals(listOf("Harbour Lights", "Slow Train", "Blue Hour"), said.titles) assertNull(said.byline) assertEquals("11:41", said.length) assertFalse(said.steps) assertEquals(CassetteSong("Blue Hour", 178.0, available = false), said.songs[2]) val blank = ListCassette.words(card("""{"title":"For the new flat","count":"0 songs","songs":[]}""", "list.cassette")) assertEquals("0 songs", blank.byline) assertTrue(blank.songs.isEmpty()) } @Test fun aSongsTapeReadsTheSong() { val song = SongCassette.words(card("""{"title":"Harbour Lights","subtitle":"The Night Ferries","album":"Coastline","length":"4:02"}""", "song.cassette")) assertEquals("The Night Ferries / Coastline", song.byline) assertEquals("4:02", song.length) assertFalse(song.steps) assertEquals(0.0, song.songs.single().seconds, 0.0) val player = card("""{"title":"Harbour Lights","artist":"The Night Ferries","duration":242,"now_playing":"specimen-song"}""", "song.cassette") val held = SongCassette.words(player, TrackPlay(held = true, duration = 300.0)) assertEquals("The Night Ferries", held.byline) assertEquals("4:02", held.length) assertTrue(held.steps) assertEquals(242.0, held.songs.single().seconds, 0.0) // The player's length where the card keeps none. val bare = SongCassette.words(card("""{"title":"Harbour Lights"}""", "song.cassette"), TrackPlay(held = true, duration = 61.0)) assertEquals("1:01", bare.length) assertNull(bare.byline) assertEquals(0, SongCassette.onTheSong(TrackPlay(held = true)).song) assertNull(SongCassette.onTheSong(TrackPlay.still).song) } @Test fun thePacksWindWithThePlace() { val songs = CassetteTape.songs(nightDrives) val play = TrackPlay(held = true, playing = true, elapsed = 134.0, duration = 302.0, song = 1) assertEquals(355.0, CassetteTape.position(songs, play), 1e-9) assertEquals(355.0 / 701.0, CassetteTape.wound(songs, play), 1e-9) assertEquals(0.0, CassetteTape.position(songs, play.copy(song = null)), 0.0) assertEquals(221.0 + 302.0, CassetteTape.position(songs, play.copy(elapsed = 9999.0)), 1e-9) assertEquals(0.0, CassetteTape.wound(emptyList(), play), 0.0) assertEquals(95f, CassetteTape.radius(0.0), 1e-4f) assertEquals(280f, CassetteTape.radius(1.0), 1e-4f) assertEquals(sqrt(95f * 95f + 0.5f * (280f * 280f - 95f * 95f)), CassetteTape.radius(0.5), 1e-3f) val pack = CassetteTape.pack(280f, CassetteKnobs.hubs[0]) assertEquals(576f, pack.w, 1e-4f) assertEquals(338f - 288f, pack.x, 1e-4f) } @Test fun theHubsTurnAndTheCounterSaysWhereItStands() { val said = ListCassette.words(nightDrives) val play = TrackPlay(held = true, playing = true, elapsed = 134.0, duration = 302.0, song = 1) assertEquals(355.0 * 48, CassetteTape.turned(said, play, reduceMotion = false), 1e-9) assertEquals(0.0, CassetteTape.turned(said, play, reduceMotion = true), 0.0) assertEquals("2:14", CassetteTape.counter(said, play)) assertEquals("1:00", CassetteTape.counter(said, play, dragged = 60.0)) assertEquals("11:41", CassetteTape.counter(said, TrackPlay.still)) assertFalse(CassetteTape.scrubs(said, play)) assertTrue(CassetteTape.scrubs(said.copy(steps = true), play)) assertEquals(151.0, CassetteTape.scrubbed(140.5f, 281f, 302.0)!!, 1e-9) assertEquals(302.0, CassetteTape.scrubbed(900f, 281f, 302.0)!!, 0.0) assertNull(CassetteTape.scrubbed(10f, 0f, 302.0)) } @Test fun theHandLineNamesAsManyAsFit() { val titles = listOf("Harbour Lights", "Slow Train", "Blue Hour") assertEquals("Harbour Lights / Slow Train / Blue Hour", CassetteTape.named(titles, 3, words)) assertEquals("Harbour Lights / Slow Train +1", CassetteTape.named(titles, 2, words)) assertEquals("+3", CassetteTape.named(titles, 0, words)) val said = CassetteWords(titles = titles) assertEquals("Harbour Lights / Slow Train / Blue Hour", CassetteTape.line(said, m, Measure.Face.Caslon, words)!!.words) // Forty songs: as many as fit in 960 at 42 (45 letters), then "+N". val many = (1..40).map { "Song $it" } val line = CassetteTape.line(CassetteWords(titles = many), m, Measure.Face.Caslon, words)!! assertTrue(line.words.length * 21f <= 960f) assertTrue(line.words.endsWith("+" + (40 - line.words.split(" / ").size))) // Not one fits: the first cut in its middle. val long = CassetteTape.line(CassetteWords(titles = listOf("A".repeat(80), "B")), m, Measure.Face.Caslon, words)!! assertTrue(long.words.contains("…")) assertTrue(long.words.length * 21f <= 960f) assertTrue(long.words.endsWith("+1")) // A byline is set whole, taken down and cut. assertEquals("The Night Ferries / Coastline", CassetteTape.line(CassetteWords(byline = "The Night Ferries / Coastline"), m, Measure.Face.Caslon, words)!!.words) assertNull(CassetteTape.line(CassetteWords(), m, Measure.Face.Caslon, words)) } @Test fun theShellIsDrawnAtMostThreeHundredAndFortyWide() { assertEquals(340f, CassetteTape.width(400f, 800f), 1e-4f) assertEquals(300f / (1300f / 1200f), CassetteTape.width(300f, 800f), 1e-3f) assertEquals(100f / (900f / 745f) * 1200f / 745f, CassetteTape.width(400f, 100f), 1e-3f) val title = CassetteTape.title("Everything we played on the long drive north to the lake house", m, Measure.Face.Caslon) assertEquals(104f * 0.45f, title.size, 1e-3f) assertTrue(title.words.endsWith("…")) } }