Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 280567a

Browse files
authoredMay 21, 2023
refactor(test): use local test resources (aallam#190)
1 parent 7511dc5 commit 280567a

File tree

15 files changed

+75
-59
lines changed

15 files changed

+75
-59
lines changed
 

‎build.gradle.kts

+16
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import com.diffplug.gradle.spotless.SpotlessExtension
22
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
33
import org.gradle.api.tasks.testing.logging.TestLogEvent.*
44
import org.jetbrains.dokka.gradle.DokkaMultiModuleTask
5+
import org.jetbrains.kotlin.gradle.targets.js.testing.KotlinJsTest
6+
import org.jetbrains.kotlin.gradle.targets.jvm.tasks.KotlinJvmTest
7+
import org.jetbrains.kotlin.gradle.targets.native.tasks.KotlinNativeTest
58

69
@Suppress("DSL_SCOPE_VIOLATION")
710
plugins {
@@ -30,6 +33,19 @@ subprojects {
3033
showStandardStreams = false
3134
}
3235
}
36+
37+
tasks.withType<KotlinJvmTest>().configureEach {
38+
environment("LIB_ROOT", rootDir)
39+
}
40+
41+
tasks.withType<KotlinNativeTest>().configureEach {
42+
environment("SIMCTL_CHILD_LIB_ROOT", rootDir)
43+
environment("LIB_ROOT", rootDir)
44+
}
45+
46+
tasks.withType<KotlinJsTest>().configureEach {
47+
environment("LIB_ROOT", rootDir.toString())
48+
}
3349
}
3450

3551
tasks.withType<DokkaMultiModuleTask>() {

‎openai-client/src/commonTest/kotlin/com/aallam/openai/client/TestAudio.kt

+8-24
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,18 @@ import com.aallam.openai.api.audio.transcriptionRequest
44
import com.aallam.openai.api.audio.translationRequest
55
import com.aallam.openai.api.file.FileSource
66
import com.aallam.openai.api.model.ModelId
7-
import com.aallam.openai.client.internal.asSource
8-
import io.ktor.client.*
9-
import io.ktor.client.call.*
10-
import io.ktor.client.request.*
7+
import com.aallam.openai.client.internal.TestFileSystem
8+
import com.aallam.openai.client.internal.testFilePath
119
import kotlin.test.Test
1210
import kotlin.test.assertEquals
1311
import kotlin.test.assertTrue
1412

1513
class TestAudio : TestOpenAI() {
1614

17-
private val httpClient = HttpClient()
18-
1915
@Test
2016
fun transcription() = test {
21-
val speedTalkingUrl = "https://github.com/aallam/sample-data/raw/main/openai/audio/micro-machines.wav"
22-
val audioBytes: ByteArray = httpClient.get(speedTalkingUrl).body()
2317
val request = transcriptionRequest {
24-
audio = FileSource(name = "micro-machines.wav", source = audioBytes.asSource())
18+
audio = FileSource(path = testFilePath("audio/micro-machines.wav"), fileSystem = TestFileSystem)
2519
model = ModelId("whisper-1")
2620
}
2721

@@ -37,10 +31,8 @@ class TestAudio : TestOpenAI() {
3731

3832
@Test
3933
fun transcriptionText() = test {
40-
val speedTalkingUrl = "https://github.com/aallam/sample-data/raw/main/openai/audio/micro-machines.wav"
41-
val audioBytes: ByteArray = httpClient.get(speedTalkingUrl).body()
4234
val request = transcriptionRequest {
43-
audio = FileSource(name = "micro-machines.wav", source = audioBytes.asSource())
35+
audio = FileSource(path = testFilePath("audio/micro-machines.wav"), fileSystem = TestFileSystem)
4436
model = ModelId("whisper-1")
4537
responseFormat = "text"
4638
}
@@ -50,10 +42,8 @@ class TestAudio : TestOpenAI() {
5042

5143
@Test
5244
fun transcriptionJsonVerbose() = test {
53-
val speedTalkingUrl = "https://github.com/aallam/sample-data/raw/main/openai/audio/micro-machines.wav"
54-
val audioBytes: ByteArray = httpClient.get(speedTalkingUrl).body()
5545
val request = transcriptionRequest {
56-
audio = FileSource(name = "micro-machines.wav", source = audioBytes.asSource())
46+
audio = FileSource(path = testFilePath("audio/micro-machines.wav"), fileSystem = TestFileSystem)
5747
model = ModelId("whisper-1")
5848
responseFormat = "verbose_json"
5949
}
@@ -66,10 +56,8 @@ class TestAudio : TestOpenAI() {
6656

6757
@Test
6858
fun translation() = test {
69-
val multilingualUrl = "https://github.com/aallam/sample-data/raw/main/openai/audio/multilingual.wav"
70-
val audioBytes: ByteArray = httpClient.get(multilingualUrl).body()
7159
val request = translationRequest {
72-
audio = FileSource(name = "multilingual.wav", source = audioBytes.asSource())
60+
audio = FileSource(path = testFilePath("audio/multilingual.wav"), fileSystem = TestFileSystem)
7361
model = ModelId("whisper-1")
7462
}
7563
val translation = openAI.translation(request)
@@ -84,10 +72,8 @@ class TestAudio : TestOpenAI() {
8472

8573
@Test
8674
fun translationText() = test {
87-
val multilingualUrl = "https://github.com/aallam/sample-data/raw/main/openai/audio/multilingual.wav"
88-
val audioBytes: ByteArray = httpClient.get(multilingualUrl).body()
8975
val request = translationRequest {
90-
audio = FileSource(name = "multilingual.wav", source = audioBytes.asSource())
76+
audio = FileSource(path = testFilePath("audio/multilingual.wav"), fileSystem = TestFileSystem)
9177
model = ModelId("whisper-1")
9278
responseFormat = "text"
9379
}
@@ -97,10 +83,8 @@ class TestAudio : TestOpenAI() {
9783

9884
@Test
9985
fun translationJsonVerbose() = test {
100-
val multilingualUrl = "https://github.com/aallam/sample-data/raw/main/openai/audio/multilingual.wav"
101-
val audioBytes: ByteArray = httpClient.get(multilingualUrl).body()
10286
val request = translationRequest {
103-
audio = FileSource(name = "multilingual.wav", source = audioBytes.asSource())
87+
audio = FileSource(path = testFilePath("audio/multilingual.wav"), fileSystem = TestFileSystem)
10488
model = ModelId("whisper-1")
10589
responseFormat = "verbose_json"
10690
}

‎openai-client/src/commonTest/kotlin/com/aallam/openai/client/TestImages.kt

+8-27
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,13 @@ import com.aallam.openai.api.image.ImageSize
55
import com.aallam.openai.api.image.imageCreation
66
import com.aallam.openai.api.image.imageEdit
77
import com.aallam.openai.api.image.imageVariation
8-
import com.aallam.openai.client.internal.asSource
9-
import io.ktor.client.*
10-
import io.ktor.client.call.*
11-
import io.ktor.client.request.*
8+
import com.aallam.openai.client.internal.TestFileSystem
9+
import com.aallam.openai.client.internal.testFilePath
1210
import kotlin.test.Test
1311
import kotlin.test.assertTrue
14-
import kotlin.time.Duration.Companion.minutes
1512

1613
class TestImages : TestOpenAI() {
1714

18-
private val httpClient = HttpClient()
19-
2015
@Test
2116
fun imageCreationURL() = test {
2217
val request = imageCreation {
@@ -42,11 +37,9 @@ class TestImages : TestOpenAI() {
4237

4338
@Test
4439
fun imageEditURL() = test {
45-
val imageBytes: ByteArray = httpClient.get(PoolImage).body()
46-
val maskBytes: ByteArray = httpClient.get(PoolMaskImage).body()
4740
val request = imageEdit {
48-
image = FileSource(name = "pool.png", source = imageBytes.asSource())
49-
mask = FileSource(name = "poolmask.png", source = maskBytes.asSource())
41+
image = FileSource(path = testFilePath("image/pool.png"), fileSystem = TestFileSystem)
42+
mask = FileSource(path = testFilePath("image/poolmask.png"), fileSystem = TestFileSystem)
5043
prompt = "a sunlit indoor lounge area with a pool containing a flamingo"
5144
n = 1
5245
size = ImageSize.is1024x1024
@@ -57,11 +50,9 @@ class TestImages : TestOpenAI() {
5750

5851
@Test
5952
fun imageEditJSON() = test {
60-
val imageBytes: ByteArray = httpClient.get(PoolImage).body()
61-
val maskBytes: ByteArray = httpClient.get(PoolMaskImage).body()
6253
val request = imageEdit {
63-
image = FileSource(name = "pool.png", source = imageBytes.asSource())
64-
mask = FileSource(name = "poolmask.png", source = maskBytes.asSource())
54+
image = FileSource(path = testFilePath("image/pool.png"), fileSystem = TestFileSystem)
55+
mask = FileSource(path = testFilePath("image/poolmask.png"), fileSystem = TestFileSystem)
6556
prompt = "a sunlit indoor lounge area with a pool containing a flamingo"
6657
n = 1
6758
size = ImageSize.is1024x1024
@@ -72,9 +63,8 @@ class TestImages : TestOpenAI() {
7263

7364
@Test
7465
fun imageVariationURL() = test {
75-
val imageBytes: ByteArray = httpClient.get(PetsImage).body()
7666
val request = imageVariation {
77-
image = FileSource("pets.png", imageBytes.asSource())
67+
image = FileSource(path = testFilePath("image/pets.png"), fileSystem = TestFileSystem)
7868
n = 1
7969
size = ImageSize.is1024x1024
8070
}
@@ -84,21 +74,12 @@ class TestImages : TestOpenAI() {
8474

8575
@Test
8676
fun imageVariationJSON() = test {
87-
val imageBytes: ByteArray = httpClient.get(PetsImage).body()
8877
val request = imageVariation {
89-
image = FileSource("pets.png", imageBytes.asSource())
78+
image = FileSource(path = testFilePath("image/pets.png"), fileSystem = TestFileSystem)
9079
n = 1
9180
size = ImageSize.is1024x1024
9281
}
9382
val response = openAI.imageJSON(request)
9483
assertTrue { response.isNotEmpty() }
9584
}
96-
97-
98-
companion object {
99-
private val Path = "https://raw.githubusercontent.com/aallam/sample-data/main/openai/image"
100-
private val PoolImage = "$Path/pool.png"
101-
private val PoolMaskImage = "$Path/poolmask.png"
102-
private val PetsImage = "$Path/pets.png"
103-
}
10485
}

‎openai-client/src/commonTest/kotlin/com/aallam/openai/client/TestOpenAI.kt

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import com.aallam.openai.client.internal.env
88
import com.aallam.openai.client.internal.http.HttpTransport
99
import kotlinx.coroutines.test.TestScope
1010
import kotlinx.coroutines.test.runTest
11+
import okio.Path.Companion.toPath
12+
import okio.Source
1113
import kotlin.time.Duration.Companion.minutes
1214

1315
internal val token: String
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.aallam.openai.client.internal
2+
3+
import okio.FileSystem
4+
import okio.Path
5+
import okio.Path.Companion.toPath
6+
7+
/**
8+
* File system to access test files.
9+
*/
10+
internal expect val TestFileSystem: FileSystem
11+
12+
/**
13+
* Get [Path] of a given [fileName] test file.
14+
*/
15+
fun testFilePath(fileName: String): Path = libRoot / "openai-client/src/commonTest/resources" / fileName
16+
17+
/**
18+
* Get the library lib root.
19+
*/
20+
private val libRoot
21+
get() = env("LIB_ROOT")?.toPath() ?: error("Can't find `LIB_ROOT` environment variable")

‎openai-client/src/commonTest/kotlin/com/aallam/openai/client/internal/Source.kt

-7
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,3 @@ internal fun String.asSource(): Source {
88
buffer.writeUtf8(this)
99
return buffer
1010
}
11-
12-
13-
internal fun ByteArray.asSource(): Source {
14-
val buffer = Buffer()
15-
buffer.write(this)
16-
return buffer
17-
}
Binary file not shown.
Binary file not shown.
Loading
Loading
Loading
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.aallam.openai.client.internal
22

33
internal actual fun env(name: String): String? {
4-
return js("process.env[name]").unsafeCast<String?>()
4+
//return js("process.env[name]").unsafeCast<String?>()
5+
return js("globalThis.process.env[name]") as String?
56
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.aallam.openai.client.internal
2+
3+
import okio.FileSystem
4+
import okio.NodeJsFileSystem
5+
import okio.Path.Companion.toPath
6+
import okio.Source
7+
8+
internal actual val TestFileSystem: FileSystem = NodeJsFileSystem
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.aallam.openai.client.internal
2+
3+
import okio.FileSystem
4+
5+
internal actual val TestFileSystem: FileSystem = FileSystem.SYSTEM
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.aallam.openai.client.internal
2+
3+
import okio.FileSystem
4+
5+
internal actual val TestFileSystem: FileSystem = FileSystem.SYSTEM

0 commit comments

Comments
 (0)
Please sign in to comment.