package de.fojadrachi.playtube import org.json.JSONObject import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse import org.junit.Assert.assertTrue import org.junit.Test class SleepAndHistoryTest { private fun song(title: String, durationS: Long = 200) = NowPlaying(title, "Band", "https://i.ytimg.com/x.jpg", playing = true, positionMs = 0, durationMs = durationS * 1000) @Test fun `Schlaf-Timer wartet, blendet aus und stoppt`() { val timer = SleepTimer.minutes(1, 0) assertEquals(60L, timer.remainingSeconds(0)) assertEquals(SleepTimer.Step.Wait, timer.tick(10_000, null)) val fade = timer.tick(50_000, null) as SleepTimer.Step.Fade assertEquals(10.0 / 15.0, fade.factor, 0.01) assertEquals(SleepTimer.Step.Stop, timer.tick(60_000, null)) } @Test fun `Schlaf-Timer am Songende stoppt beim naechsten Song`() { val timer = SleepTimer.endOfTrack(null) assertTrue(timer.endOfTrack) assertEquals(-1L, timer.remainingSeconds(0)) assertEquals(SleepTimer.Step.Wait, timer.tick(0, "aaa")) assertEquals(SleepTimer.Step.Wait, timer.tick(0, null)) assertEquals(SleepTimer.Step.Wait, timer.tick(0, "aaa")) assertEquals(SleepTimer.Step.Stop, timer.tick(0, "bbb")) assertEquals("1:05", SleepTimer.format(65)) assertEquals("1:02:03", SleepTimer.format(3723)) } @Test fun `Verlauf zaehlt nach 30 Sekunden und haelt die Hoerdauer fest`() { val history = PlayHistory() val info = song("Lied") assertFalse(history.observe("vid1", info, true, 0, 1000)) var changed = false for (i in 1..14) changed = changed or history.observe("vid1", info, true, i * 2000L, 1000) assertFalse(changed) assertTrue(history.observe("vid1", info, true, 30_000, 1000)) assertEquals(1, history.entries.size) history.observe("vid1", info, true, 40_000, 1000) assertTrue(history.observe(null, info, false, 41_000, 1000)) assertEquals(40, history.entries[0].listened) assertFalse("schon gespeichert", history.observe(null, info, false, 42_000, 1000)) assertEquals(1000L, history.entries[0].playedAt) } @Test fun `Uebersprungene Songs zaehlen nicht, kurze nach der Haelfte`() { val history = PlayHistory() history.observe("skip", song("Weg"), true, 0, 0) history.observe("skip", song("Weg"), true, 5_000, 0) history.observe("short", song("Kurz", 40), true, 6_000, 0) history.observe("short", song("Kurz", 40), true, 16_000, 0) history.observe("short", song("Kurz", 40), true, 26_000, 0) assertEquals(listOf("Kurz"), history.entries.map { it.title }) } @Test fun `Speichern, Laden und Suchen`() { val history = PlayHistory(listOf( HistoryEntry("a1", "Alpha", "X", null, 1, 40), HistoryEntry("b2", "Beta", "alphaband", "https://i.ytimg.com/b.jpg", 2, 50), )) val loaded = PlayHistory.fromJson(history.toJson()) assertEquals(history.entries, loaded.entries) assertEquals(listOf("Beta", "Alpha"), loaded.search("").map { it.title }) assertEquals(listOf("Beta", "Alpha"), loaded.search("ALPHA").map { it.title }) assertEquals(listOf("Alpha"), loaded.search("x").map { it.title }) assertTrue(PlayHistory.fromJson("kaputt").entries.isEmpty()) } @Test fun `PC-Verlauf wird gelesen und geprueft`() { val reply = JSONObject("""{"ok":true,"history":[ {"videoId":"dQw4w9WgXcQ","title":"Lied","artist":"Band","thumbnail":"https://i.ytimg.com/a.jpg","playedAt":100,"listened":60}, {"videoId":"bad id!","title":"X"}, {"videoId":"ok_id-1","title":"Y","thumbnail":"javascript:x"}]}""") val list = PlayHistory.parsePcHistory(reply) assertEquals(listOf("dQw4w9WgXcQ", "ok_id-1"), list.map { it.videoId }) assertEquals(100_000L, list[0].playedAt) assertEquals(null, list[1].artwork) } }