Skip to content

Commit f37a148

Browse files
committed
[ECO-5014] feat: basic sandbox setup
added basic sandbox setup for simple happy-path testing
1 parent 4861812 commit f37a148

File tree

4 files changed

+102
-1
lines changed

4 files changed

+102
-1
lines changed

chat-android/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ dependencies {
4949
testImplementation(libs.junit)
5050
testImplementation(libs.mockk)
5151
testImplementation(libs.coroutine.test)
52+
testImplementation(libs.bundles.ktor.client)
5253
androidTestImplementation(libs.androidx.test.core)
5354
androidTestImplementation(libs.androidx.test.runner)
5455
androidTestImplementation(libs.androidx.junit)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.ably.chat
2+
3+
import com.google.gson.JsonElement
4+
import com.google.gson.JsonParser
5+
import io.ktor.client.HttpClient
6+
import io.ktor.client.engine.cio.CIO
7+
import io.ktor.client.request.delete
8+
import io.ktor.client.request.get
9+
import io.ktor.client.request.post
10+
import io.ktor.client.request.setBody
11+
import io.ktor.client.statement.HttpResponse
12+
import io.ktor.client.statement.bodyAsText
13+
import io.ktor.http.ContentType
14+
import io.ktor.http.contentType
15+
16+
val client = HttpClient(CIO)
17+
18+
class Sandbox {
19+
private lateinit var appId: String
20+
21+
lateinit var apiKey: String
22+
private set
23+
24+
suspend fun setUp() {
25+
val response: HttpResponse = client.post("https://sandbox-rest.ably.io/apps") {
26+
contentType(ContentType.Application.Json)
27+
setBody(loadAppCreationRequestBody().toString())
28+
}
29+
val body = JsonParser.parseString(response.bodyAsText())
30+
// From JS chat repo at 7985ab7 — "The key we need to use is the one at index 5, which gives enough permissions to interact with Chat and Channels"
31+
apiKey = body.asJsonObject["keys"].asJsonArray[5].asJsonObject["keyStr"].asString
32+
appId = body.asJsonObject["appId"].asString
33+
}
34+
35+
suspend fun tearDown() {
36+
client.delete("https://sandbox-rest.ably.io/apps/${appId}")
37+
}
38+
}
39+
40+
suspend fun loadAppCreationRequestBody(): JsonElement =
41+
JsonParser.parseString(
42+
client.get("https://raw.githubusercontent.com/ably/ably-common/refs/heads/main/test-resources/test-app-setup.json") {
43+
contentType(ContentType.Application.Json)
44+
}.bodyAsText(),
45+
).asJsonObject.get("post_apps")
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.ably.chat
2+
3+
import io.ably.lib.realtime.AblyRealtime
4+
import io.ably.lib.realtime.ChannelState
5+
import kotlinx.coroutines.test.runTest
6+
import org.junit.After
7+
import org.junit.AfterClass
8+
import org.junit.Assert.assertEquals
9+
import org.junit.Before
10+
import org.junit.Test
11+
import io.ably.lib.types.ClientOptions as PubSubClientOptions
12+
13+
class SandboxTest {
14+
15+
private val sandbox: Sandbox = Sandbox()
16+
17+
@Before
18+
fun setUp() = runTest {
19+
sandbox.setUp()
20+
}
21+
22+
@After
23+
fun tearDown() = runTest {
24+
sandbox.tearDown()
25+
}
26+
27+
@Test
28+
fun basicIntegrationTest() = runTest {
29+
val chatClient = createSandboxChatClient(sandbox.apiKey)
30+
val room = chatClient.rooms.get("basketball")
31+
room.attach()
32+
assertEquals(ChannelState.attached, room.messages.channel.state)
33+
}
34+
}
35+
36+
private fun createSandboxChatClient(apiKey: String): DefaultChatClient {
37+
val realtime = createSandboxRealtime(apiKey)
38+
return DefaultChatClient(realtime, ClientOptions())
39+
}
40+
41+
private fun createSandboxRealtime(chatApiKey: String, chatClientId: String = "sandbox-client"): AblyRealtime =
42+
AblyRealtime(
43+
PubSubClientOptions().apply {
44+
key = chatApiKey
45+
environment = "sandbox"
46+
clientId = chatClientId
47+
},
48+
)

gradle/libs.versions.toml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ gson = "2.11.0"
2020
mockk = "1.13.12"
2121
coroutine = "1.8.1"
2222
build-config = "5.4.0"
23+
ktor = "3.0.1"
2324

2425
[libraries]
2526
junit = { group = "junit", name = "junit", version.ref = "junit" }
@@ -50,9 +51,15 @@ mockk = { group = "io.mockk", name = "mockk", version.ref = "mockk" }
5051
coroutine-core = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-core", version.ref = "coroutine" }
5152
coroutine-test = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-test", version.ref = "coroutine" }
5253

54+
ktor-client-core = { module = "io.ktor:ktor-client-core", version.ref = "ktor" }
55+
ktor-client-cio = { module = "io.ktor:ktor-client-cio", version.ref = "ktor" }
56+
57+
[bundles]
58+
ktor-client = ["ktor-client-core", "ktor-client-cio"]
59+
5360
[plugins]
5461
detekt = { id = "io.gitlab.arturbosch.detekt", version.ref = "detekt"}
55-
android-kotlin = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
62+
android-kotlin = { id = "org.jetbrains.kotlin.android", version = "2.0.21" }
5663
android-library = { id = "com.android.library", version.ref = "agp" }
5764
android-application = { id = "com.android.application", version.ref = "agp" }
5865
compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }

0 commit comments

Comments
 (0)