mirror of
https://github.com/Novaloop-AG/element-x-android.git
synced 2026-11-02 12:07:28 +00:00
Add missing tests for RustTemporaryMatrixClient
This commit is contained in:
committed by
Jorge Martin Espinosa
parent
07e90ff704
commit
913b566270
+92
@@ -0,0 +1,92 @@
|
||||
/*
|
||||
* Copyright (c) 2026 Element Creations Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
|
||||
* Please see LICENSE files in the repository root for full details.
|
||||
*/
|
||||
|
||||
package io.element.android.libraries.matrix.impl
|
||||
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import io.element.android.libraries.matrix.api.exception.ClientException
|
||||
import io.element.android.libraries.matrix.api.paths.SessionPaths
|
||||
import io.element.android.libraries.matrix.impl.fixtures.fakes.FakeFfiClient
|
||||
import io.element.android.tests.testutils.lambda.lambdaRecorder
|
||||
import io.element.android.tests.testutils.lambda.value
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.Test
|
||||
import org.matrix.rustcomponents.sdk.Client
|
||||
import org.matrix.rustcomponents.sdk.NoHandle
|
||||
import java.io.File
|
||||
import kotlin.io.path.createTempDirectory
|
||||
import org.matrix.rustcomponents.sdk.ClientException as RustClientException
|
||||
|
||||
class RustTemporaryMatrixClientTest {
|
||||
@Test
|
||||
fun `getUrl method uses client getUrl`() = runTest {
|
||||
val paths = SessionPaths(
|
||||
fileDirectory = File("files"),
|
||||
cacheDirectory = File("cache"),
|
||||
)
|
||||
|
||||
val getUrlRecorder = lambdaRecorder<String, ByteArray> { ByteArray(0) }
|
||||
val client = createRustTemporaryMatrixClient(
|
||||
client = FakeFfiClient(
|
||||
getUrlResult = getUrlRecorder,
|
||||
),
|
||||
paths = paths,
|
||||
)
|
||||
val url = "https://example.com"
|
||||
|
||||
client.getUrl(url)
|
||||
|
||||
getUrlRecorder.assertions().isCalledOnce().with(value(url))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `getUrl method can fail gracefully and map errors to public ones`() = runTest {
|
||||
val paths = SessionPaths(
|
||||
fileDirectory = File("files"),
|
||||
cacheDirectory = File("cache"),
|
||||
)
|
||||
|
||||
val getUrlRecorder = lambdaRecorder<String, ByteArray> { throw RustClientException.Generic("Error", null) }
|
||||
val client = createRustTemporaryMatrixClient(
|
||||
client = FakeFfiClient(
|
||||
getUrlResult = getUrlRecorder,
|
||||
),
|
||||
paths = paths,
|
||||
)
|
||||
val url = "https://example.com"
|
||||
|
||||
val result = client.getUrl(url)
|
||||
|
||||
assertThat(result.isFailure).isTrue()
|
||||
assertThat(result.exceptionOrNull()).isInstanceOf(ClientException.Generic::class.java)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `close deletes the directories if they exist`() = runTest {
|
||||
val paths = SessionPaths(
|
||||
fileDirectory = createTempDirectory().toFile(),
|
||||
cacheDirectory = createTempDirectory().toFile(),
|
||||
)
|
||||
|
||||
val client = createRustTemporaryMatrixClient(paths = paths)
|
||||
|
||||
assertThat(paths.fileDirectory.exists()).isTrue()
|
||||
assertThat(paths.cacheDirectory.exists()).isTrue()
|
||||
|
||||
client.close()
|
||||
|
||||
assertThat(paths.fileDirectory.exists()).isFalse()
|
||||
assertThat(paths.cacheDirectory.exists()).isFalse()
|
||||
}
|
||||
|
||||
private fun createRustTemporaryMatrixClient(
|
||||
client: Client = Client(NoHandle),
|
||||
paths: SessionPaths,
|
||||
): RustTemporaryMatrixClient {
|
||||
return RustTemporaryMatrixClient(client, paths)
|
||||
}
|
||||
}
|
||||
+5
@@ -60,6 +60,7 @@ class FakeFfiClient(
|
||||
private val setAccountDataResult: (String, String) -> Unit = { _, _ -> lambdaError() },
|
||||
private val isUserStatusSupportedResult: () -> Boolean = { false },
|
||||
private val subscribeToOwnProfileResult: (ProfileListener) -> Unit = {},
|
||||
private val getUrlResult: (String) -> ByteArray = { lambdaError() },
|
||||
private val closeResult: () -> Unit = {},
|
||||
) : Client(NoHandle) {
|
||||
override fun userId(): String = userId
|
||||
@@ -134,5 +135,9 @@ class FakeFfiClient(
|
||||
setAccountDataResult(eventType, content)
|
||||
}
|
||||
|
||||
override suspend fun getUrl(url: String): ByteArray = simulateLongTask {
|
||||
getUrlResult(url)
|
||||
}
|
||||
|
||||
override fun close() = closeResult()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user