package com.cottondb.messages.chat import com.cottondb.messages.InApp import com.cottondb.shell.core.CottonCore import kotlinx.serialization.json.jsonObject import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse import org.junit.Assert.assertNull import org.junit.Assert.assertTrue import org.junit.Test import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner import org.robolectric.annotation.Config /** Links in a message's words, as the core answers them (`links`), which ones a Custom Tab shows, and which go to Sites. */ @RunWith(RobolectricTestRunner::class) @Config(sdk = [35], application = android.app.Application::class) class LinksOfTest { @Test fun links_are_read_where_they_stand() { val o = CottonCore.json.parseToJsonElement("""{"links":[{"at":[15,39],"url":"https://example.com/plan"},{"at":[3,3],"url":"https://x"},{"url":"https://y"}]}""").jsonObject assertEquals(listOf((15 until 39) to "https://example.com/plan"), linksOf(o)) assertEquals(emptyList>(), linksOf(CottonCore.json.parseToJsonElement("{}").jsonObject)) } @Test fun only_a_web_page_opens_in_a_custom_tab() { assertTrue(InApp.web(android.net.Uri.parse("https://example.com/plan"))) assertTrue(InApp.web(android.net.Uri.parse("HTTP://example.com"))) assertFalse(InApp.web(android.net.Uri.parse("mailto:asha@example.com"))) assertFalse(InApp.web(android.net.Uri.parse("upi://pay?pa=x"))) } /** * COMMS WITHOUT MAIL: a web link goes to Cotton Sites of this build's kind when it is on the phone (`chat.link`'s * `sites.android`), else to the Custom Tab; a refused link (`not_web`) or no answer is the Custom Tab. */ @Test fun a_web_link_goes_to_sites_when_it_is_on_the_phone_else_the_custom_tab() { val link = CottonCore.json.parseToJsonElement( """{"url":"https://example.com/plan","sites":{"android":{"package":"com.cottondb.sites","dev_package":"com.cottondb.sites.dev","action":"android.intent.action.VIEW","extra":{"from":"comms"}}}}""", ).jsonObject val onPhone = setOf("com.cottondb.sites.dev") assertEquals("com.cottondb.sites.dev", InApp.sitesFor(link, dev = true) { it in onPhone }) assertNull("the live Sites is not on the phone: the Custom Tab", InApp.sitesFor(link, dev = false) { it in onPhone }) assertEquals("com.cottondb.sites", InApp.sitesFor(link, dev = false) { true }) assertNull("refused or unanswered: the Custom Tab", InApp.sitesFor(null, dev = false) { true }) assertNull("an answer naming no Sites: the Custom Tab", InApp.sitesFor(CottonCore.json.parseToJsonElement("""{"url":"https://example.com"}""").jsonObject, dev = false) { true }) } }