Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -239,19 +239,19 @@ data class TvCatalogFacetSelection(
CatalogQueryGroup(
match = "any",
rules = buildList {
if (hdr) add(CatalogQueryRule(field = "hdr", op = "is", value = "true"))
if (dolbyVision) add(CatalogQueryRule(field = "dolby_vision", op = "is", value = "true"))
if (hdr) add(CatalogQueryRule(field = "hdr", op = "is", value = true))
if (dolbyVision) add(CatalogQueryRule(field = "dolby_vision", op = "is", value = true))
},
),
)
}
watchStatus?.let { status ->
val rule = when (status) {
TvWatchStatusFilter.Unwatched -> CatalogQueryRule(field = "watched", op = "is", value = "false")
TvWatchStatusFilter.Watched -> CatalogQueryRule(field = "watched", op = "is", value = "true")
TvWatchStatusFilter.InProgress -> CatalogQueryRule(field = "in_progress", op = "is", value = "true")
TvWatchStatusFilter.Favorited -> CatalogQueryRule(field = "favorited", op = "is", value = "true")
TvWatchStatusFilter.Watchlist -> CatalogQueryRule(field = "in_watchlist", op = "is", value = "true")
TvWatchStatusFilter.Unwatched -> CatalogQueryRule(field = "watched", op = "is", value = false)
TvWatchStatusFilter.Watched -> CatalogQueryRule(field = "watched", op = "is", value = true)
TvWatchStatusFilter.InProgress -> CatalogQueryRule(field = "in_progress", op = "is", value = true)
TvWatchStatusFilter.Favorited -> CatalogQueryRule(field = "favorited", op = "is", value = true)
TvWatchStatusFilter.Watchlist -> CatalogQueryRule(field = "in_watchlist", op = "is", value = true)
}
add(CatalogQueryGroup(match = "all", rules = listOf(rule)))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.test.setMain
import kotlinx.coroutines.withContext
import kotlinx.coroutines.withTimeout
import kotlinx.serialization.json.JsonPrimitive
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
Expand All @@ -45,6 +46,27 @@ import kotlin.test.assertNull
@Config(sdk = [34], application = android.app.Application::class)
class TvLibraryBrowsePrefsTest {

@Test
fun booleanFacetsLowerToJsonBooleanRules() {
// The catalog API rejects string booleans ("true"); every boolean field must go out as a JSON boolean.
val dynamicRange = TvCatalogFacetSelection(hdr = true, dolbyVision = true).toQueryGroups().single()
assertEquals(
listOf("hdr" to JsonPrimitive(true), "dolby_vision" to JsonPrimitive(true)),
dynamicRange.rules.map { it.field to it.value },
)
val expected = mapOf(
TvWatchStatusFilter.Unwatched to ("watched" to false),
TvWatchStatusFilter.Watched to ("watched" to true),
TvWatchStatusFilter.InProgress to ("in_progress" to true),
TvWatchStatusFilter.Favorited to ("favorited" to true),
TvWatchStatusFilter.Watchlist to ("in_watchlist" to true),
)
for ((status, rule) in expected) {
val actual = TvCatalogFacetSelection(watchStatus = status).toQueryGroups().single().rules.single()
assertEquals(rule.first to JsonPrimitive(rule.second), actual.field to actual.value, "rule for $status")
}
}

@Test
fun savedStateRoundTripsEveryFacetAndDropsPerVisitFields() {
var selection = TvCatalogFacetSelection(matchAll = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,15 +168,15 @@ object CatalogFilterQueryBuilder {
)
CatalogFacet.DynamicRange -> CatalogQueryGroup(
match = "any",
rules = values.map { CatalogQueryRule(field = it, op = "is", value = "true") },
rules = values.map { CatalogQueryRule(field = it, op = "is", value = true) },
)
CatalogFacet.WatchStatus -> {
val (field, value) = when (values.first()) {
"unwatched" -> "watched" to "false"
"watched" -> "watched" to "true"
"inProgress" -> "in_progress" to "true"
"favorited" -> "favorited" to "true"
"watchlist" -> "in_watchlist" to "true"
"unwatched" -> "watched" to false
"watched" -> "watched" to true
"inProgress" -> "in_progress" to true
"favorited" -> "favorited" to true
"watchlist" -> "in_watchlist" to true
else -> continue
}
CatalogQueryGroup(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.siloserver.silo.network.apiv2.CatalogSearchDiagnosticsV2
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonPrimitive

// --- Browse / Catalog ---

Expand Down Expand Up @@ -125,11 +126,19 @@ data class CatalogEffectiveSort(
data class CatalogQueryRule(
val field: String,
val op: String,
val value: String,
/** Sent as-is, so its JSON type is the wire type: boolean fields (watched,
* in_progress, favorited, in_watchlist, hdr, dolby_vision) need a JSON
* boolean; the server rejects the string "true". */
val value: JsonPrimitive,
/** Range ops ("between") carry indexed values instead of [value] —
* encoded as value[0], value[1] on the wire (iOS CatalogQueryBuilder). */
val values: List<String> = emptyList(),
)
) {
constructor(field: String, op: String, value: String, values: List<String> = emptyList()) :
this(field, op, JsonPrimitive(value), values)

constructor(field: String, op: String, value: Boolean) : this(field, op, JsonPrimitive(value))
}

data class CatalogQueryGroup(
val match: String = "all",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ internal fun List<CatalogQueryGroup>.toV2Groups() = map { group ->
val value = when {
rule.values.isNotEmpty() -> JsonArray(rule.values.map(::JsonPrimitive))
// The server rejects flag rules whose value is not a JSON boolean (iOS CatalogQueryBuilder).
rule.field in BOOLEAN_RULE_FIELDS -> JsonPrimitive(rule.value == "true")
else -> JsonPrimitive(rule.value)
rule.field in BOOLEAN_RULE_FIELDS -> JsonPrimitive(rule.value.content == "true")
else -> rule.value
}
CatalogRuleV2(rule.field, rule.op, value)
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.siloserver.silo.catalog.filter

import kotlinx.serialization.json.JsonPrimitive
import org.siloserver.silo.network.api.toV2Groups
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
Expand Down Expand Up @@ -27,7 +29,7 @@ class CatalogFilterQueryBuilderTest {
val groups = CatalogFilterQueryBuilder.buildGroups(state)
assertEquals(1, groups.size)
assertEquals("any", groups[0].match)
assertEquals(listOf("Action", "Drama"), groups[0].rules.map { it.value })
assertEquals(listOf("Action", "Drama"), groups[0].rules.map { it.value.content })
assertTrue(groups[0].rules.all { it.field == "genre" && it.op == "contains" })
}

Expand Down Expand Up @@ -66,29 +68,30 @@ class CatalogFilterQueryBuilderTest {
val groups = CatalogFilterQueryBuilder.buildGroups(state)
assertEquals(1, groups.size)
assertEquals("any", groups[0].match)
val byField = groups[0].rules.associate { it.field to it.value }
assertEquals("true", byField["hdr"])
assertEquals("true", byField["dolby_vision"])
// The server rejects string booleans ("true"), so the wire value must be a JSON boolean.
val byField = groups.toV2Groups()[0].rules.associate { it.field to it.value }
assertEquals(JsonPrimitive(true), byField["hdr"])
assertEquals(JsonPrimitive(true), byField["dolby_vision"])
}

@Test
fun watchStatusIsItsOwnAllGroupWithOneRule() {
val expectations = mapOf(
"unwatched" to ("watched" to "false"),
"watched" to ("watched" to "true"),
"inProgress" to ("in_progress" to "true"),
"favorited" to ("favorited" to "true"),
"watchlist" to ("in_watchlist" to "true"),
"unwatched" to ("watched" to false),
"watched" to ("watched" to true),
"inProgress" to ("in_progress" to true),
"favorited" to ("favorited" to true),
"watchlist" to ("in_watchlist" to true),
)
for ((status, expected) in expectations) {
val state = CatalogFilterState(
selections = mapOf(CatalogFacet.WatchStatus to setOf(status)),
)
val group = CatalogFilterQueryBuilder.buildGroups(state).single()
val group = CatalogFilterQueryBuilder.buildGroups(state).toV2Groups().single()
assertEquals("all", group.match)
val rule = group.rules.single()
assertEquals(expected.first, rule.field, "field for $status")
assertEquals(expected.second, rule.value, "value for $status")
assertEquals(JsonPrimitive(expected.second), rule.value, "JSON boolean value for $status")
assertEquals("is", rule.op)
}
}
Expand Down
Loading