Skip to content

Commit

Permalink
fix double witness bug
Browse files Browse the repository at this point in the history
  • Loading branch information
jonesmac committed Nov 22, 2024
1 parent 86651cd commit a7f2378
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@ import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.rule.GrantPermissionRule
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import network.xyo.client.account.Account
import network.xyo.client.lib.TestConstants
import network.xyo.client.lib.XyoSerializable
import network.xyo.client.witness.location.info.LocationActivity
import network.xyo.client.witness.location.info.XyoLocationPayload
import network.xyo.client.witness.location.info.XyoLocationPayloadRaw
import network.xyo.client.witness.location.info.XyoLocationWitness
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.jupiter.api.assertInstanceOf
Expand All @@ -25,24 +31,41 @@ class LocationWitnessTest {
@get:Rule
val activityRule = ActivityScenarioRule(LocationActivity::class.java)

lateinit var appContext: Context

@Before
fun useContext() {
this.appContext = ApplicationProvider.getApplicationContext()
}

@Test
fun testObserve() {
// Get the application context
val context = ApplicationProvider.getApplicationContext<Context>()

CoroutineScope(Dispatchers.Main).launch {
val witness = XyoLocationWitness()
val locationPayload = witness.observe(context)?.first()
val locationPayload = witness.observe(appContext)?.first()

assertInstanceOf<XyoLocationPayload>(locationPayload)
assert(locationPayload.schema == XyoLocationPayload.schema)
assert(locationPayload.currentLocation !== null)
assert(locationPayload.currentLocation?.coords?.latitude !== null)
assert(locationPayload.currentLocation?.coords?.longitude !== null)

val locationRawPayload = witness.observe(context)?.get(1)
val locationRawPayload = witness.observe(appContext)?.get(1)
assertInstanceOf<XyoLocationPayloadRaw>(locationRawPayload)
assert(locationRawPayload.schema == XyoLocationPayloadRaw.schema)
}
}

@OptIn(ExperimentalCoroutinesApi::class)
@Test
fun testInsidePanel() {
runBlocking {
val panel = XyoPanel(appContext, Account.random(), arrayListOf(Pair("${TestConstants.nodeUrlBeta}/Archivist", null)), listOf(XyoLocationWitness()))
val result = panel.reportAsyncQuery()
result.payloads?.forEach{ payload ->
val hash = XyoSerializable.sha256String(payload)
assert(result.bw.payload_hashes.contains(hash))
}
}
}
}
6 changes: 3 additions & 3 deletions sdk/src/main/java/network/xyo/client/witness/XyoPanel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import network.xyo.client.account.model.AccountInstance
import network.xyo.client.archivist.wrapper.ArchivistWrapper
import network.xyo.client.boundwitness.XyoBoundWitnessBuilder
import network.xyo.client.boundwitness.XyoBoundWitnessJson
import network.xyo.client.lib.XyoSerializable
import network.xyo.client.node.client.NodeClient
import network.xyo.client.node.client.PostQueryResult
import network.xyo.client.payload.XyoPayload
Expand Down Expand Up @@ -60,8 +61,7 @@ class XyoPanel(
}
}

private suspend fun generateBoundWitnessJson(): XyoBoundWitnessJson {
val payloads = generatePayloads()
private suspend fun generateBoundWitnessJson(payloads: List<XyoPayload>): XyoBoundWitnessJson {
return XyoBoundWitnessBuilder(context)
.payloads(payloads)
.signer(account)
Expand All @@ -81,8 +81,8 @@ class XyoPanel(
@kotlinx.coroutines.ExperimentalCoroutinesApi
suspend fun reportAsyncQuery(adhocWitnesses: List<XyoWitness<XyoPayload>> = emptyList()): XyoPanelReportQueryResult {
if (nodes == null) resolveNodes()
val bw = generateBoundWitnessJson()
val payloads = generatePayloads(adhocWitnesses)
val bw = generateBoundWitnessJson(payloads)
val results = mutableListOf<PostQueryResult>()

if (nodes.isNullOrEmpty()) {
Expand Down

0 comments on commit a7f2378

Please sign in to comment.