package com.cottondb.callingimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertNullimport org.junit.Testimport java.io.Fileimport kotlinx.serialization.json.jsonArrayimport kotlinx.serialization.json.jsonObjectimport kotlinx.serialization.json.jsonPrimitiveclassCallPushTest {/** The Worker's two data messages (apps/business/src/calling/fcm.ts): every value a string. */ @Testfun a_ring_and_a_stop_are_read() { assertEquals(CallPush.Ring("r1", true), CallPush.of(mapOf("kind" to "ring", "room" to "r1", "video" to "1"))) assertEquals(CallPush.Ring("r1", false), CallPush.of(mapOf("kind" to "ring", "room" to "r1", "video" to "0"))) assertEquals(CallPush.Stop("r1", "answered_elsewhere"), CallPush.of(mapOf("kind" to "stop", "room" to "r1", "stop" to "answered_elsewhere"))) }/** The ring's sealed caller (`box`, a c1 box) rides along to the core; anything else in its place is dropped. */ @Testfun a_ring_carries_its_sealed_caller() { assertEquals(CallPush.Ring("r1", true, "c1:abc"), CallPush.of(mapOf("kind" to "ring", "room" to "r1", "video" to "1", "box" to "c1:abc"))) assertEquals(CallPush.Ring("r1", false, null), CallPush.of(mapOf("kind" to "ring", "room" to "r1", "video" to "0", "box" to "plain words"))) }/** * CALLS FIX 2: a missed call's stop to a phone that speaks the new way carries who called (sealed) and when it rang; * a missed call kept for this phone and told as its socket opened (`kind: missed`) is read as the same stop, which * writes the row and rings nothing. Times out of order are none. */ @Testfun a_missed_stop_carries_its_caller_and_times() {valdata = mapOf("kind" to "stop", "room" to "r1", "stop" to "missed", "box" to "c1:abc", "at" to "1790000000000", "ended" to "1790000045000") assertEquals(CallPush.Stop("r1", "missed", "c1:abc", 1_790_000_000_000, 1_790_000_045_000), CallPush.of(data)) assertEquals(CallPush.Stop("r1", "missed", "c1:abc", 1_790_000_000_000, 1_790_000_045_000), CallPush.of(data + ("kind" to "missed"))) assertEquals(CallPush.Stop("r1", "missed", "c1:abc"), CallPush.of(data + ("ended" to "5"))) }/** Another app's message (Messages' `{kind: chat}`) and a ring with no room are no call's. */ @Testfun anything_else_is_no_calls() { assertNull(CallPush.of(mapOf("kind" to "chat"))) assertNull(CallPush.of(mapOf("kind" to "ring", "room" to ""))) assertNull(CallPush.of(emptyMap())) }/** * CALLS FIX 3: what the Worker sends, read as the Worker's own test checks it sends it: the shared fixture * apps/business/test/fixtures/call-push-data.json (calling-units.test.ts holds the Worker to it). Behaviour on both * sides; neither reads the other's source. */ @Testfun the_workers_push_data_reads_as_its_call() {val repo = generateSequence(File("").absoluteFile) { it.parentFile }.first { File(it, "apps/registry.json").isFile }val cases = kotlinx.serialization.json.Json.parseToJsonElement(File(repo, "apps/business/test/fixtures/call-push-data.json").readText()) .jsonObject["cases"]!!.jsonArray.associate { c -> c.jsonObject["name"]!!.jsonPrimitive.content to c.jsonObject["data"]!!.jsonObject.mapValues { it.value.jsonPrimitive.content } }val room = "0123456789abcdef0123456789abcdef" assertEquals(CallPush.Ring(room, true, "c1:sealed"), CallPush.of(cases.getValue("ring"))) assertEquals(CallPush.Ring(room, false, null), CallPush.of(cases.getValue("ring, voice, no box"))) assertEquals(CallPush.Ring(room, false, "c1:sealed", 1_790_000_000_000), CallPush.of(cases.getValue("ring to a phone that speaks it, with its start"))) assertEquals(CallPush.Stop(room, "answered_elsewhere"), CallPush.of(cases.getValue("stop, as main"))) assertEquals(CallPush.Stop(room, "missed", "c1:sealed", 1_790_000_000_000, 1_790_000_045_000), CallPush.of(cases.getValue("missed stop to a phone that speaks it"))) }}