|
| 1 | +/* |
| 2 | + * Copyright 2026 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.adk.kt.webserver |
| 18 | + |
| 19 | +import com.google.adk.kt.webserver.routes.staticRoutes |
| 20 | +import com.google.common.truth.Truth.assertThat |
| 21 | +import io.ktor.client.request.get |
| 22 | +import io.ktor.client.statement.bodyAsText |
| 23 | +import io.ktor.http.HttpStatusCode |
| 24 | +import io.ktor.server.routing.routing |
| 25 | +import io.ktor.server.testing.testApplication |
| 26 | +import org.junit.Test |
| 27 | +import org.junit.runner.RunWith |
| 28 | +import org.junit.runners.JUnit4 |
| 29 | + |
| 30 | +/** The server resolves the Development UI from `browser/` on the classpath. */ |
| 31 | +@RunWith(JUnit4::class) |
| 32 | +class DevUiAssetsTest { |
| 33 | + |
| 34 | + @Test |
| 35 | + fun devUi_index_isServedFromClasspath() = withoutWebUiDir { |
| 36 | + testApplication { |
| 37 | + application { routing { staticRoutes(this@application) } } |
| 38 | + |
| 39 | + val response = client.get("/dev-ui/index.html") |
| 40 | + |
| 41 | + assertThat(response.status).isEqualTo(HttpStatusCode.OK) |
| 42 | + assertThat(response.bodyAsText()).contains("<html") |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + fun devUi_nestedAsset_isServedFromClasspath() = withoutWebUiDir { |
| 48 | + testApplication { |
| 49 | + application { routing { staticRoutes(this@application) } } |
| 50 | + |
| 51 | + // A nested path proves the whole asset tree is packaged, not just the entry point. |
| 52 | + assertThat(client.get("/dev-ui/assets/audio-processor.js").status) |
| 53 | + .isEqualTo(HttpStatusCode.OK) |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + /** Runs [body] with the `adk.web.ui.dir` system property cleared. */ |
| 58 | + private fun withoutWebUiDir(body: () -> Unit) { |
| 59 | + val previous: String? = System.getProperty(WEB_UI_DIR_PROPERTY) |
| 60 | + System.clearProperty(WEB_UI_DIR_PROPERTY) |
| 61 | + try { |
| 62 | + body() |
| 63 | + } finally { |
| 64 | + if (previous != null) System.setProperty(WEB_UI_DIR_PROPERTY, previous) |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + private companion object { |
| 69 | + const val WEB_UI_DIR_PROPERTY = "adk.web.ui.dir" |
| 70 | + } |
| 71 | +} |
0 commit comments