From e93ccdc5a75123e021f3d9d9035416f3eabf9fdf Mon Sep 17 00:00:00 2001
From: Koen <koen@pop-os.localdomain>
Date: Mon, 24 Jul 2023 16:03:59 +0200
Subject: [PATCH] Added additional tests for dummy data.

---
 .idea/androidTestResultsUserPreferences.xml   |  30 +++
 .idea/misc.xml                                |   1 -
 .../core/GenerateTestProfileTests.kt          | 188 ++++++++++++++++++
 .../polycentric/core/ProcessHandleTests.kt    |  32 ++-
 .../futo/polycentric/core/TestConstants.kt    |   7 +
 5 files changed, 239 insertions(+), 19 deletions(-)
 create mode 100644 app/src/androidTest/java/com/futo/polycentric/core/GenerateTestProfileTests.kt
 create mode 100644 app/src/androidTest/java/com/futo/polycentric/core/TestConstants.kt

diff --git a/.idea/androidTestResultsUserPreferences.xml b/.idea/androidTestResultsUserPreferences.xml
index ef9ca58..b624309 100644
--- a/.idea/androidTestResultsUserPreferences.xml
+++ b/.idea/androidTestResultsUserPreferences.xml
@@ -306,7 +306,9 @@
             <AndroidTestResultsTableState>
               <option name="preferredColumnWidths">
                 <map>
+                  <entry key="29211JEGR13699" value="120" />
                   <entry key="Duration" value="90" />
+                  <entry key="Google&#10; Pixel 6a" value="120" />
                   <entry key="R5CNC07TZCY" value="120" />
                   <entry key="Tests" value="360" />
                   <entry key="samsung&#10; SM-G998B" value="120" />
@@ -368,6 +370,20 @@
             </AndroidTestResultsTableState>
           </value>
         </entry>
+        <entry key="1489134328">
+          <value>
+            <AndroidTestResultsTableState>
+              <option name="preferredColumnWidths">
+                <map>
+                  <entry key="29211JEGR13699" value="120" />
+                  <entry key="Duration" value="90" />
+                  <entry key="Google&#10; Pixel 6a" value="120" />
+                  <entry key="Tests" value="360" />
+                </map>
+              </option>
+            </AndroidTestResultsTableState>
+          </value>
+        </entry>
         <entry key="1814633309">
           <value>
             <AndroidTestResultsTableState>
@@ -427,6 +443,20 @@
             </AndroidTestResultsTableState>
           </value>
         </entry>
+        <entry key="2076253436">
+          <value>
+            <AndroidTestResultsTableState>
+              <option name="preferredColumnWidths">
+                <map>
+                  <entry key="29211JEGR13699" value="120" />
+                  <entry key="Duration" value="90" />
+                  <entry key="Google&#10; Pixel 6a" value="120" />
+                  <entry key="Tests" value="360" />
+                </map>
+              </option>
+            </AndroidTestResultsTableState>
+          </value>
+        </entry>
       </map>
     </option>
   </component>
diff --git a/.idea/misc.xml b/.idea/misc.xml
index 0ad17cb..8978d23 100644
--- a/.idea/misc.xml
+++ b/.idea/misc.xml
@@ -1,4 +1,3 @@
-<?xml version="1.0" encoding="UTF-8"?>
 <project version="4">
   <component name="ExternalStorageConfigurationManager" enabled="true" />
   <component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="jbr-17" project-jdk-type="JavaSDK">
diff --git a/app/src/androidTest/java/com/futo/polycentric/core/GenerateTestProfileTests.kt b/app/src/androidTest/java/com/futo/polycentric/core/GenerateTestProfileTests.kt
new file mode 100644
index 0000000..ad2909d
--- /dev/null
+++ b/app/src/androidTest/java/com/futo/polycentric/core/GenerateTestProfileTests.kt
@@ -0,0 +1,188 @@
+package com.futo.polycentric.core
+
+import android.util.Log
+import org.junit.Assert.assertEquals
+import org.junit.Test
+import userpackage.Protocol
+
+class GenerateTestProfileTests {
+    @Test
+    fun scenarioKoenFuto() {
+        Store.initializeMemoryStore()
+
+        val s1p1 = ProcessHandle.create()
+        s1p1.addServer(TestConstants.SERVER)
+        s1p1.setUsername("scenarioKoenFuto subject")
+
+        val claims = listOf(
+            s1p1.claim(Claims.youtube("@koen-futo")),
+            s1p1.claim(Claims.youtube("UCR7KMD7jkSefYYWgSwNPEBA")),
+            s1p1.claim(Claims.rumble("c/c-3366838")),
+            s1p1.claim(Claims.rumble("user/koenfuto")),
+        )
+
+        Synchronization.fullyBackFillServers(s1p1, s1p1.system)
+
+        val s2p1 = ProcessHandle.create()
+        s2p1.addServer(TestConstants.SERVER)
+        s2p1.setUsername("scenarioKoenFuto authority")
+
+        val vouches = arrayListOf<Pointer>()
+        for (claim in claims) {
+            vouches.add(s2p1.vouch(claim))
+        }
+
+        Synchronization.fullyBackFillServers(s2p1, s2p1.system)
+
+        //Verify the bundle
+        for (i in 0 until vouches.size) {
+            val claimPointer = claims[i];
+            val vouchPointer = vouches[i];
+            val queryReferences = ApiMethods.getQueryReferences(TestConstants.SERVER, claimPointer.toReference(), null,
+                Protocol.QueryReferencesRequestEvents.newBuilder()
+                    .setFromType(ContentType.VOUCH.value)
+                    .build())
+
+            assertEquals(1, queryReferences.itemsCount)
+            val item = queryReferences.getItems(0)
+            val vouchEvent = SignedEvent.fromProto(item.event)
+            assertEquals(vouchPointer, vouchEvent.toPointer())
+            assertEquals(1, vouchEvent.event.references.size)
+            assertEquals(claimPointer.toProto().toByteString(), vouchEvent.event.references[0].reference)
+        }
+
+        Log.i("EXPORT_BUNDLE s1p1", createExportBundle(s1p1))
+        Log.i("EXPORT_BUNDLE s2p1", createExportBundle(s2p1))
+        Log.i("verifier pub key", s2p1.system.key.toBase64())
+    }
+
+    @Test
+    fun scenarioEveryClaimType() {
+        Store.initializeMemoryStore()
+
+        val s1p1 = ProcessHandle.create()
+        s1p1.addServer(TestConstants.SERVER)
+        s1p1.setUsername("scenarioEveryClaimType subject")
+
+        val claims = listOf(
+            s1p1.claim(Claims.hackerNews("eron_wolf")),
+            s1p1.claim(Claims.youtube("@FUTOTECH")),
+            s1p1.claim(Claims.odysee("@FUTO")),
+            s1p1.claim(Claims.rumble("rossmanngroup")),
+            s1p1.claim(Claims.discord("thekinocorner")),
+            s1p1.claim(Claims.instagram("@the_kino_corner")),
+            s1p1.claim(Claims.twitch("thekinocorner")),
+            s1p1.claim(Claims.url("https://futo.org/grants"))
+        )
+
+        Synchronization.fullyBackFillServers(s1p1, s1p1.system)
+
+        val s2p1 = ProcessHandle.create()
+        s2p1.addServer(TestConstants.SERVER)
+        s2p1.setUsername("scenarioEveryClaimType authority")
+
+        val vouches = arrayListOf<Pointer>()
+        for (claim in claims) {
+            vouches.add(s2p1.vouch(claim))
+        }
+
+        Synchronization.fullyBackFillServers(s2p1, s2p1.system)
+
+        //Verify the bundle
+        for (i in 0 until vouches.size) {
+            val claimPointer = claims[i];
+            val vouchPointer = vouches[i];
+            val queryReferences = ApiMethods.getQueryReferences(TestConstants.SERVER, claimPointer.toReference(), null,
+                Protocol.QueryReferencesRequestEvents.newBuilder()
+                    .setFromType(ContentType.VOUCH.value)
+                    .build())
+
+            assertEquals(1, queryReferences.itemsCount)
+            val item = queryReferences.getItems(0)
+            val vouchEvent = SignedEvent.fromProto(item.event)
+            assertEquals(vouchPointer, vouchEvent.toPointer())
+            assertEquals(1, vouchEvent.event.references.size)
+            assertEquals(claimPointer.toProto().toByteString(), vouchEvent.event.references[0].reference)
+        }
+
+        Log.i("EXPORT_BUNDLE s1p1", createExportBundle(s1p1))
+        Log.i("EXPORT_BUNDLE s2p1", createExportBundle(s2p1))
+    }
+
+    private fun createExportBundle(processHandle: ProcessHandle): String {
+        val relevantContentTypes = listOf(ContentType.SERVER.value, ContentType.AVATAR.value, ContentType.USERNAME.value);
+        val crdtSetItems = arrayListOf<Pair<SignedEvent, StorageTypeCRDTSetItem>>()
+        val crdtItems = arrayListOf<Pair<SignedEvent, StorageTypeCRDTItem>>()
+
+        Store.instance.enumerateSignedEvents(processHandle.system) { signedEvent ->
+            if (!relevantContentTypes.contains(signedEvent.event.contentType)) {
+                return@enumerateSignedEvents;
+            }
+
+            val event = signedEvent.event;
+            event.lwwElementSet?.let { lwwElementSet ->
+                val foundIndex = crdtSetItems.indexOfFirst { pair ->
+                    pair.second.contentType == event.contentType && pair.second.value.contentEquals(lwwElementSet.value)
+                }
+
+                var found = false
+                if (foundIndex != -1) {
+                    val foundPair = crdtSetItems[foundIndex]
+                    if (foundPair.second.unixMilliseconds < lwwElementSet.unixMilliseconds) {
+                        foundPair.second.operation = lwwElementSet.operation
+                        foundPair.second.unixMilliseconds = lwwElementSet.unixMilliseconds
+                        found = true
+                    }
+                }
+
+                if (!found) {
+                    crdtSetItems.add(Pair(signedEvent, StorageTypeCRDTSetItem(event.contentType, lwwElementSet.value, lwwElementSet.unixMilliseconds, lwwElementSet.operation)))
+                }
+            }
+
+            event.lwwElement?.let { lwwElement ->
+                val foundIndex = crdtItems.indexOfFirst { pair ->
+                    pair.second.contentType == event.contentType
+                }
+
+                var found = false
+                if (foundIndex != -1) {
+                    val foundPair = crdtItems[foundIndex]
+                    if (foundPair.second.unixMilliseconds < lwwElement.unixMilliseconds) {
+                        foundPair.second.value = lwwElement.value
+                        foundPair.second.unixMilliseconds = lwwElement.unixMilliseconds
+                        found = true
+                    }
+                }
+
+                if (!found) {
+                    crdtItems.add(Pair(signedEvent, StorageTypeCRDTItem(event.contentType, lwwElement.value, lwwElement.unixMilliseconds)))
+                }
+            }
+        };
+
+        val relevantEvents = arrayListOf<SignedEvent>();
+        for (pair in crdtSetItems) {
+            relevantEvents.add(pair.first);
+        }
+
+        for (pair in crdtItems) {
+            relevantEvents.add(pair.first);
+        }
+
+        val exportBundle = Protocol.ExportBundle.newBuilder()
+            .setKeyPair(processHandle.processSecret.system.toProto())
+            .setEvents(
+                Protocol.Events.newBuilder()
+                .addAllEvents(relevantEvents.map { it.toProto() })
+                .build())
+            .build();
+
+        val urlInfo = Protocol.URLInfo.newBuilder()
+            .setUrlType(3)
+            .setBody(exportBundle.toByteString())
+            .build();
+
+        return "polycentric://" + urlInfo.toByteArray().toBase64Url()
+    }
+}
\ No newline at end of file
diff --git a/app/src/androidTest/java/com/futo/polycentric/core/ProcessHandleTests.kt b/app/src/androidTest/java/com/futo/polycentric/core/ProcessHandleTests.kt
index 6654d18..fa3d036 100644
--- a/app/src/androidTest/java/com/futo/polycentric/core/ProcessHandleTests.kt
+++ b/app/src/androidTest/java/com/futo/polycentric/core/ProcessHandleTests.kt
@@ -103,7 +103,7 @@ class ProcessHandleTests {
         Store.initializeMemoryStore()
 
         val s1p1 = ProcessHandle.create()
-        s1p1.addServer(SERVER)
+        s1p1.addServer(TestConstants.SERVER)
         s1p1.setDescription("hello")
 
         val claim = Claims.hackerNews("pg")
@@ -114,13 +114,13 @@ class ProcessHandleTests {
 
         val s2p1 = ProcessHandle.create()
 
-        Synchronization.fullyBackFillClient(s2p1, s1p1.system, SERVER)
+        Synchronization.fullyBackFillClient(s2p1, s1p1.system, TestConstants.SERVER)
 
         val s1State = SystemState.fromStorageTypeSystemState(Store.instance.getSystemState(s1p1.system))
 
         assertEquals("hello", s1State.description)
 
-        val resolved = ApiMethods.getResolveClaim(SERVER, s1p1.system.toProto(), claim)
+        val resolved = ApiMethods.getResolveClaim(TestConstants.SERVER, s1p1.system.toProto(), claim)
         assertEquals(2, resolved.eventsList.size)
     }
 
@@ -129,7 +129,7 @@ class ProcessHandleTests {
         Store.initializeMemoryStore()
 
         val s1p1 = ProcessHandle.create()
-        s1p1.addServer(SERVER)
+        s1p1.addServer(TestConstants.SERVER)
         Log.i("ProcessHandleTests", s1p1.processSecret.toProto().toByteArray().toBase64());
 
         val claim = Claims.hackerNews("pg")
@@ -138,7 +138,7 @@ class ProcessHandleTests {
 
         Synchronization.fullyBackFillServers(s1p1, s1p1.system)
 
-        val resolved = ApiMethods.getResolveClaim(SERVER, s1p1.system.toProto(), claim)
+        val resolved = ApiMethods.getResolveClaim(TestConstants.SERVER, s1p1.system.toProto(), claim)
         assertEquals(2, resolved.eventsList.size)
 
         val resolvedEvents = resolved.eventsList.map { i -> SignedEvent.fromProto(i) }
@@ -180,7 +180,7 @@ class ProcessHandleTests {
         val subject = Models.referenceFromBuffer(("https://fake.com/" + Math.random().toString()).toByteArray())
         val createHandle = { username: String ->
             val handle = ProcessHandle.create()
-            handle.addServer(SERVER)
+            handle.addServer(TestConstants.SERVER)
             handle.setUsername(username)
             Synchronization.fullyBackFillServers(handle, handle.system)
             handle
@@ -231,7 +231,7 @@ class ProcessHandleTests {
         babbage.fullyBackfillServers()
         turing.fullyBackfillServers()
 
-        val queryReferences = ApiMethods.getQueryReferences(SERVER, subject, null,
+        val queryReferences = ApiMethods.getQueryReferences(TestConstants.SERVER, subject, null,
             Protocol.QueryReferencesRequestEvents.newBuilder()
                 .setFromType(ContentType.POST.value)
                 .addAllCountLwwElementReferences(arrayListOf(
@@ -305,13 +305,13 @@ class ProcessHandleTests {
         Store.initializeMemoryStore()
 
         val s1p1 = ProcessHandle.create()
-        s1p1.addServer(SERVER)
+        s1p1.addServer(TestConstants.SERVER)
         s1p1.setUsername("test1")
         Synchronization.fullyBackFillServers(s1p1, s1p1.system)
 
         run {
             val events = ApiMethods.getQueryLatest(
-                SERVER,
+                TestConstants.SERVER,
                 s1p1.system.toProto(),
                 listOf(ContentType.USERNAME.value)
             )
@@ -331,7 +331,7 @@ class ProcessHandleTests {
 
         run {
             val events = ApiMethods.getQueryLatest(
-                SERVER,
+                TestConstants.SERVER,
                 s1p1.system.toProto(),
                 listOf(ContentType.USERNAME.value)
             )
@@ -352,7 +352,7 @@ class ProcessHandleTests {
         Store.initializeMemoryStore()
 
         val s1p1 = ProcessHandle.create()
-        s1p1.addServer(SERVER)
+        s1p1.addServer(TestConstants.SERVER)
         s1p1.setDescription("howdy")
 
         val claim = Claims.hackerNews("fake_user")
@@ -362,7 +362,7 @@ class ProcessHandleTests {
         Synchronization.fullyBackFillServers(s1p1, s1p1.system)
 
         val resolved = ApiMethods.getResolveClaim(
-            SERVER,
+            TestConstants.SERVER,
             s1p1.system.toProto(),
             claim
         )
@@ -370,7 +370,7 @@ class ProcessHandleTests {
         assertEquals(1, validClaims.size)
 
         val events = ApiMethods.getQueryIndex(
-            SERVER,
+            TestConstants.SERVER,
             validClaims[0].system.toProto(),
             ContentType.DESCRIPTION.value
         )
@@ -384,7 +384,7 @@ class ProcessHandleTests {
         val systemState = SystemState.fromStorageTypeSystemState(storageSystemState)
         assertEquals("howdy", systemState.description)
 
-        val queryReferences = ApiMethods.getQueryReferences(SERVER, claimPointer.toReference(), null,
+        val queryReferences = ApiMethods.getQueryReferences(TestConstants.SERVER, claimPointer.toReference(), null,
             Protocol.QueryReferencesRequestEvents.newBuilder()
                 .setFromType(ContentType.VOUCH.value)
                 .build());
@@ -412,8 +412,4 @@ class ProcessHandleTests {
             }
         }
     }
-    
-    companion object {
-        private const val SERVER = "https://srv1-stg.polycentric.io"
-    }
 }
\ No newline at end of file
diff --git a/app/src/androidTest/java/com/futo/polycentric/core/TestConstants.kt b/app/src/androidTest/java/com/futo/polycentric/core/TestConstants.kt
new file mode 100644
index 0000000..9500f00
--- /dev/null
+++ b/app/src/androidTest/java/com/futo/polycentric/core/TestConstants.kt
@@ -0,0 +1,7 @@
+package com.futo.polycentric.core
+
+class TestConstants {
+    companion object {
+        const val SERVER = "https://srv1-stg.polycentric.io"
+    }
+}
\ No newline at end of file
-- 
GitLab