Fixes test file loader for iOS

This commit is contained in:
Vitor Pamplona
2026-02-18 12:34:55 -05:00
parent ee7d20e939
commit 086e2046db
2 changed files with 27 additions and 14 deletions
@@ -56,18 +56,20 @@ class HintIndexerTest {
) )
} }
val relays = val relays by
TestResourceLoader() lazy {
.loadString("relayDB.txt") TestResourceLoader()
.split('\n') .loadString("relayDB.txt")
.mapNotNull { .split('\n')
val relay = RelayUrlNormalizer.normalizeOrNull(it) .mapNotNull {
if (relay == null || relay.isLocalHost()) { val relay = RelayUrlNormalizer.normalizeOrNull(it)
null if (relay == null || relay.isLocalHost()) {
} else { null
relay } else {
relay
}
} }
} }
val indexer by lazy { val indexer by lazy {
val result = HintIndexer() val result = HintIndexer()
@@ -32,11 +32,22 @@ import platform.darwin.NSObjectMeta
actual class TestResourceLoader { actual class TestResourceLoader {
@OptIn(ExperimentalForeignApi::class, BetaInteropApi::class) @OptIn(ExperimentalForeignApi::class, BetaInteropApi::class)
actual fun loadString(file: String): String { actual fun loadString(file: String): String {
val bundle = NSBundle.Companion.bundleForClass(BundleMarker) // Split the filename and extension (e.g., "data.json" -> "data", "json")
val basename = file.substringBeforeLast(".")
val extension = file.substringAfterLast(".", "")
// Locate the file in the main application bundle
val path = val path =
bundle.pathForResource(file, ofType = null) NSBundle.mainBundle.pathForResource(basename, ofType = extension)
?: throw IllegalArgumentException("Resource not found: $file") ?: throw IllegalArgumentException("Resource not found: $file")
return NSString.Companion.stringWithContentsOfFile(path, encoding = NSUTF8StringEncoding, error = null)!!
// Read the file content as a UTF-8 string
return NSString
.stringWithContentsOfFile(
path = path,
encoding = NSUTF8StringEncoding,
error = null,
).toString()
} }
private class BundleMarker : NSObject() { private class BundleMarker : NSObject() {