Taste, Stage 1: profile-aware signals, recorded only
Each profile gets its own bounded, recoverable signal log (chose, finished, saved, stayed, brief with no weight, removed) in taste/<profile>.events.jsonl, with a state file for Personalise (on for Adult, off for Kids until an adult turns it on) and reversible suppressions. Events carry random ids and a schema version, ready for Rooms merging. Inspect and reset through the phone for adults. Nothing on screen, in schedules or in playback changes. 0.108.97. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
package tv.orbit.classic
|
||||
|
||||
import org.junit.Assert.*
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.junit.rules.TemporaryFolder
|
||||
|
||||
/** Taste, Stage 1: signals recorded accurately, per profile, bounded, recoverable, inspectable and removable. */
|
||||
class TasteTest {
|
||||
@get:Rule val tmp = TemporaryFolder()
|
||||
private val minute = 60_000L
|
||||
private val now = 1_794_000_000_000L
|
||||
private val adult = Profile("grown", "Adult", "sil-blue", ProfileKind.Grown)
|
||||
private val dad = Profile("dad", "Dad", "rocket", ProfileKind.Grown)
|
||||
private val kids = Profile("kids", "Kids", "ollie", ProfileKind.Kids, KidsStartChannels)
|
||||
private var n = 0
|
||||
private fun event(p: Profile, kind: TasteKind, item: String = "1001", at: Long = now) =
|
||||
TasteEvent("e${n++}", p.id, kind, item, null, "Title $item", 106, at, null, "test")
|
||||
private fun show(item: String, start: Long, minutes: Long = 45) =
|
||||
SampleBroadcast(ScheduledMedia(SampleItem("Show / S1 E1 / Pilot", "Episode", minutes, "", item, minutes * minute), "src-$item", minutes * minute), start)
|
||||
|
||||
@Test fun eachProfileHasItsOwnSignals() {
|
||||
val store = TasteStore(tmp.newFolder("taste"))
|
||||
assertTrue(store.record(adult, event(adult, TasteKind.Chose, "1001")))
|
||||
assertTrue(store.record(dad, event(dad, TasteKind.Chose, "2002")))
|
||||
assertEquals(listOf("1001"), store.events("grown").map { it.itemId })
|
||||
assertEquals(listOf("2002"), store.events("dad").map { it.itemId })
|
||||
// An event filed under another profile is never written to this one.
|
||||
assertFalse(store.record(adult, event(dad, TasteKind.Chose, "3003")))
|
||||
assertNotEquals(store.eventsFile("grown"), store.eventsFile("dad"))
|
||||
}
|
||||
|
||||
@Test fun kidsRecordsNothingUntilAnAdultTurnsItOn() {
|
||||
val store = TasteStore(tmp.newFolder("taste"))
|
||||
assertFalse(TasteState.defaultFor(kids).personalise); assertTrue(TasteState.defaultFor(adult).personalise)
|
||||
assertFalse(store.record(kids, event(kids, TasteKind.Chose)))
|
||||
assertTrue(store.events("kids").isEmpty())
|
||||
store.saveState(kids, TasteState(true))
|
||||
assertTrue(store.record(kids, event(kids, TasteKind.Chose)))
|
||||
// Kids' file never holds Adult's history, and Adult's never holds Kids'.
|
||||
store.record(adult, event(adult, TasteKind.Finished, "4004"))
|
||||
assertTrue(store.events("kids").none { it.itemId == "4004" })
|
||||
assertTrue(store.events("grown").none { it.profile == "kids" })
|
||||
}
|
||||
|
||||
@Test fun repeatsCountOnce() {
|
||||
val store = TasteStore(tmp.newFolder("taste"))
|
||||
assertTrue(store.record(adult, event(adult, TasteKind.Chose, "1001", now)))
|
||||
assertFalse(store.record(adult, event(adult, TasteKind.Chose, "1001", now + 3 * minute)))
|
||||
assertTrue(store.record(adult, event(adult, TasteKind.Finished, "1001", now + 3 * minute)))
|
||||
assertTrue(store.record(adult, event(adult, TasteKind.Chose, "1001", now + 30 * minute)))
|
||||
assertEquals(3, store.events("grown").size)
|
||||
// The same event id arriving twice (as a later Rooms join would bring it) counts once.
|
||||
val e = event(adult, TasteKind.Saved, "5005")
|
||||
assertEquals(1, compactTaste(listOf(e, e), now).size)
|
||||
}
|
||||
|
||||
@Test fun theLogStaysWithinItsBounds() {
|
||||
val many = (0 until TASTE_MAX_EVENTS + 300).map { event(adult, TasteKind.Stayed, "${1000 + it}", now - it * minute) }
|
||||
val old = event(adult, TasteKind.Chose, "9009", now - TASTE_MAX_AGE_MS - minute)
|
||||
val kept = compactTaste(many + old, now)
|
||||
assertEquals(TASTE_MAX_EVENTS, kept.size)
|
||||
assertTrue(kept.none { it.itemId == "9009" })
|
||||
assertEquals(kept.sortedBy { it.atMs }, kept)
|
||||
val store = TasteStore(tmp.newFolder("taste"))
|
||||
many.take(50).forEach { store.record(adult, it) }
|
||||
store.compact("grown", now)
|
||||
assertTrue(java.io.File(store.directory, "grown.events.jsonl.bak").exists())
|
||||
assertEquals(50, store.events("grown").size)
|
||||
}
|
||||
|
||||
@Test fun damagedFilesAreRecovered() {
|
||||
val store = TasteStore(tmp.newFolder("taste"))
|
||||
store.record(adult, event(adult, TasteKind.Chose, "1001"))
|
||||
store.eventsFile("grown").appendText("{not json\n" + """{"v":99,"id":"x","p":"grown","k":"chose","i":"1002","w":1}""" + "\n")
|
||||
store.record(adult, event(adult, TasteKind.Finished, "1003", now + minute))
|
||||
assertEquals(listOf("1001", "1003"), store.events("grown").map { it.itemId })
|
||||
store.stateFile("grown").writeText("{broken")
|
||||
assertTrue(store.state(adult).personalise)
|
||||
assertFalse(store.state(kids).personalise)
|
||||
}
|
||||
|
||||
@Test fun inspectAndReset() {
|
||||
val store = TasteStore(tmp.newFolder("taste"))
|
||||
store.record(adult, event(adult, TasteKind.Chose, "1001"))
|
||||
store.record(adult, event(adult, TasteKind.Brief, "1002", now + minute))
|
||||
val s = store.summary(adult) { it == "1001" }
|
||||
assertEquals(2, s.getInt("events")); assertEquals(1, s.getJSONObject("byKind").getInt("brief"))
|
||||
// A title gone from the library is counted as stale, never a crash.
|
||||
assertEquals(1, s.getInt("stale"))
|
||||
assertEquals("Title 1002", s.getJSONArray("latest").getJSONObject(0).getString("title"))
|
||||
store.saveState(adult, TasteState(false, mapOf("1001" to "hide")))
|
||||
store.reset(adult, now)
|
||||
assertTrue(store.events("grown").isEmpty())
|
||||
assertTrue(store.state(adult).suppressions.isEmpty())
|
||||
assertFalse("reset keeps the Personalise choice", store.state(adult).personalise)
|
||||
}
|
||||
|
||||
@Test fun watchingBecomesSignals() {
|
||||
val w = TasteWatch()
|
||||
val a = show("1001", now)
|
||||
assertTrue(w.observe(106, a, null, 0, false, now).isEmpty())
|
||||
assertEquals(listOf(TasteKind.Stayed), w.observe(106, a, null, 21 * minute, false, now + 21 * minute).map { it.first })
|
||||
assertEquals(listOf(TasteKind.Finished), w.observe(106, a, null, 41 * minute, false, now + 41 * minute).map { it.first })
|
||||
assertTrue(w.observe(106, a, null, 42 * minute, false, now + 42 * minute).isEmpty())
|
||||
// A short visit is noted as brief, with no weight, when you move on.
|
||||
val b = show("1002", now + 45 * minute)
|
||||
w.observe(106, b, null, 0, false, now + 45 * minute)
|
||||
val moved = w.observe(107, show("1003", now + 40 * minute), null, 8 * minute, false, now + 47 * minute)
|
||||
assertEquals(listOf(TasteKind.Brief to "1002"), moved.map { it.first to it.second })
|
||||
assertEquals(0.0, TasteKind.Brief.weight, 0.0)
|
||||
// Picked deliberately: no "stayed" on top of the choice.
|
||||
val w2 = TasteWatch(); val c = show("1004", now)
|
||||
w2.observe(106, c, null, 0, true, now)
|
||||
assertTrue(w2.observe(106, c, null, 25 * minute, false, now + 25 * minute).isEmpty())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user