|
| 1 | +package com.algolia.client |
| 2 | + |
| 3 | +import com.algolia.client.api.SearchClient |
| 4 | +import com.algolia.client.configuration.ClientOptions |
| 5 | +import com.algolia.client.extensions.browseSynonyms |
| 6 | +import com.algolia.client.model.search.SearchSynonymsParams |
| 7 | +import com.algolia.client.model.search.SynonymHit |
| 8 | +import io.ktor.client.engine.mock.MockEngine |
| 9 | +import io.ktor.client.engine.mock.respond |
| 10 | +import io.ktor.http.ContentType |
| 11 | +import io.ktor.http.HttpHeaders |
| 12 | +import io.ktor.http.HttpStatusCode |
| 13 | +import io.ktor.http.content.OutgoingContent |
| 14 | +import io.ktor.http.headersOf |
| 15 | +import kotlin.test.Test |
| 16 | +import kotlin.test.assertEquals |
| 17 | +import kotlin.test.assertTrue |
| 18 | +import kotlinx.coroutines.runBlocking |
| 19 | +import kotlinx.serialization.json.Json |
| 20 | +import kotlinx.serialization.json.int |
| 21 | +import kotlinx.serialization.json.jsonObject |
| 22 | +import kotlinx.serialization.json.jsonPrimitive |
| 23 | + |
| 24 | +/** |
| 25 | + * Regression guard for the `browseSynonyms` pagination contract (Jira CR-11727). |
| 26 | + * |
| 27 | + * The Java `browseSynonyms` helper once failed to write the incremented `page` back into the |
| 28 | + * request params, so it re-fetched page 0 forever (infinite loop / OOM). Kotlin's implementation is |
| 29 | + * correct; this test locks that behavior in by mocking the HTTP layer and asserting that |
| 30 | + * consecutive, distinct pages are requested until a partial page ends the iteration. |
| 31 | + * |
| 32 | + * Placed under `jvmTest` (not `commonTest`) on purpose: the client module's test classpath has no |
| 33 | + * coroutine runner in `commonTest` (`kotlinx-coroutines-test` is not a declared dependency, and |
| 34 | + * `runBlocking` is not part of the JS-less multiplatform common source set), so a self-contained |
| 35 | + * coroutine-driven test must live on the JVM target where `runBlocking` is available. `ktor-client-mock` |
| 36 | + * is a `commonTest` dependency and is inherited here. |
| 37 | + */ |
| 38 | +class TestBrowseSynonyms { |
| 39 | + |
| 40 | + private val hitsPerPage = 1000 |
| 41 | + |
| 42 | + /** Builds a canned `SearchSynonymsResponse` body with [count] hits prefixed by [objectIdPrefix]. */ |
| 43 | + private fun synonymsPageBody(objectIdPrefix: String, count: Int): String { |
| 44 | + val hits = |
| 45 | + (0 until count).joinToString(",") { i -> |
| 46 | + """{"objectID":"$objectIdPrefix-hit$i","type":"synonym"}""" |
| 47 | + } |
| 48 | + return """{"hits":[$hits],"nbHits":1003}""" |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + fun browseSynonymsPaginatesUntilPartialPage() = runBlocking { |
| 53 | + val requestedPages = mutableListOf<Int>() |
| 54 | + val requestedPaths = mutableListOf<String>() |
| 55 | + |
| 56 | + val engine = |
| 57 | + MockEngine { request -> |
| 58 | + requestedPaths += request.url.encodedPath |
| 59 | + |
| 60 | + val bodyBytes = |
| 61 | + (request.body as? OutgoingContent.ByteArrayContent)?.bytes() |
| 62 | + ?: error("Unexpected request body type: ${request.body::class}") |
| 63 | + val page = |
| 64 | + Json.parseToJsonElement(bodyBytes.decodeToString()) |
| 65 | + .jsonObject["page"]!! |
| 66 | + .jsonPrimitive |
| 67 | + .int |
| 68 | + requestedPages += page |
| 69 | + |
| 70 | + // page 0 -> a full page (1000 hits) so iteration continues. |
| 71 | + // page >= 1 -> a partial page (3 hits) so iteration stops. |
| 72 | + val content = |
| 73 | + if (page == 0) synonymsPageBody("page0", hitsPerPage) |
| 74 | + else synonymsPageBody("page$page", 3) |
| 75 | + |
| 76 | + respond( |
| 77 | + content = content, |
| 78 | + status = HttpStatusCode.OK, |
| 79 | + headers = headersOf(HttpHeaders.ContentType, ContentType.Application.Json.toString()), |
| 80 | + ) |
| 81 | + } |
| 82 | + |
| 83 | + val client = |
| 84 | + SearchClient(appId = "appId", apiKey = "apiKey", options = ClientOptions(engine = engine)) |
| 85 | + |
| 86 | + val collected = mutableListOf<SynonymHit>() |
| 87 | + client.use { |
| 88 | + it.browseSynonyms( |
| 89 | + indexName = "my-index", |
| 90 | + searchSynonymsParams = SearchSynonymsParams(), |
| 91 | + aggregator = { response -> collected += response.hits }, |
| 92 | + ) |
| 93 | + } |
| 94 | + |
| 95 | + // The helper must request page 0 then page 1 (proving the incremented page is sent), and stop. |
| 96 | + assertEquals(listOf(0, 1), requestedPages, "browseSynonyms should request consecutive pages 0, 1") |
| 97 | + |
| 98 | + // Every request must target the synonyms search endpoint (page read from that POST body). |
| 99 | + assertTrue( |
| 100 | + requestedPaths.all { it.endsWith("/synonyms/search") }, |
| 101 | + "all requests should hit /synonyms/search, got $requestedPaths", |
| 102 | + ) |
| 103 | + |
| 104 | + // All hits across both pages are aggregated, with no page re-fetched (no duplicates). |
| 105 | + assertEquals(1003, collected.size, "aggregated hit count should be 1000 + 3") |
| 106 | + assertEquals( |
| 107 | + 1003, |
| 108 | + collected.map { it.objectID }.toSet().size, |
| 109 | + "aggregated objectIDs should all be distinct (no page re-fetched)", |
| 110 | + ) |
| 111 | + } |
| 112 | +} |
0 commit comments