Rooms sync: paired rooms keep each other's family up to date

Pairing now leaves both TVs a lasting key. While Orbit is open on both, every two minutes they
exchange profiles (with change times and removals), the PIN's salted hash, Continue watching and
Taste over the home Wi-Fi, sealed with the pair's key, dated and single-use, and merged: newest
change per person, removals that stick, events by id. Channels, schedules and each room's unlock
stay its own. Settings › Rooms shows each paired room's last sync with a switch. 0.108.102.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-07 22:41:02 +00:00
co-authored by Claude Opus 5.5
parent 7436d0755a
commit b3fb2f518f
12 changed files with 261 additions and 16 deletions
@@ -0,0 +1,53 @@
package tv.orbit.classic
import org.json.JSONObject
import org.junit.Assert.*
import org.junit.Test
/** Rooms sync (7 November 2026): newest change per person, removals stick, and only the paired room's fresh messages count. */
class RoomSyncTest {
private val paul = Profile("grown", "Paul", "sil-blue", ProfileKind.Grown, updatedMs = 100)
private val kids = Profile("kids", "Amelia", "ollie", ProfileKind.Kids, DefaultProfiles[1].channels, updatedMs = 100)
@Test fun aNewProfileArrivesAndTheNewestChangeWins() {
val cheryl = Profile("pcher01", "Cheryl", "sil-pink", ProfileKind.Grown, updatedMs = 300)
val renamedHere = paul.copy(name = "Paul D", updatedMs = 500)
val (merged, _) = mergeProfilesSync(listOf(renamedHere, kids), emptyMap(), listOf(paul, kids, cheryl), emptyMap())
assertEquals(listOf("grown", "kids", "pcher01"), merged.map { it.id })
assertEquals("Paul D", merged.first().name)
val (back, _) = mergeProfilesSync(listOf(paul, kids, cheryl), emptyMap(), listOf(renamedHere, kids), emptyMap())
assertEquals("Paul D", back.first { it.id == "grown" }.name)
assertEquals(3, back.size)
}
@Test fun aRemovalSticksUnlessTheProfileChangedAfterIt() {
val cheryl = Profile("pcher01", "Cheryl", "sil-pink", ProfileKind.Grown, updatedMs = 300)
val (gone, tombs) = mergeProfilesSync(listOf(paul, kids, cheryl), emptyMap(), listOf(paul, kids), mapOf("pcher01" to 400L))
assertEquals(listOf("grown", "kids"), gone.map { it.id }); assertEquals(400L, tombs["pcher01"])
// Edited in the other room after the removal: the newer edit keeps them.
val (kept, _) = mergeProfilesSync(listOf(paul, kids), mapOf("pcher01" to 400L), listOf(cheryl.copy(updatedMs = 500)), emptyMap())
assertTrue(kept.any { it.id == "pcher01" })
// A merge never leaves the house without a grown-up.
val (safe, _) = mergeProfilesSync(listOf(paul, kids), emptyMap(), emptyList(), mapOf("grown" to 900L))
assertTrue(safe.any { it.kind == ProfileKind.Grown })
}
@Test fun bothTvsWorkOutTheSameLinkAndOnlyItOpensMessages() {
val seal = ByteArray(32) { it.toByte() }
val a = RoomLink.fromPairing(seal, "Living Room"); val b = RoomLink.fromPairing(seal, "Upstairs")
assertEquals(a.id, b.id); assertArrayEquals(a.key, b.key)
val stranger = RoomLink.fromPairing(ByteArray(32) { 7 }, "Next door")
val now = 1_794_000_000_000L
fun msg(at: Long, nonce: String) = RoomCrypto.seal(a.key, JSONObject().put("v", 1).put("at", at).put("nonce", nonce).toString().toByteArray())
assertNotNull(RoomSync.open(b, msg(now, "n1"), now))
assertNull("the same message twice is refused", RoomSync.open(b, msg(now, "n1"), now))
assertNull("too old", RoomSync.open(b, msg(now - ROOM_SYNC_WINDOW_MS - 1, "n2"), now))
assertNull("another pair's key", RoomSync.open(stranger, msg(now, "n3"), now))
assertNull("tampered", RoomSync.open(b, msg(now, "n4").dropLast(4) + "AAAA", now))
}
@Test fun profilesKeepTheirChangeTimeWhenSaved() {
val back = Profiles.decode(Profiles.encode(listOf(paul.copy(updatedMs = 1234), kids)))
assertEquals(1234L, back.first().updatedMs)
}
}