package eu.friedcutlet.yokan.ui.screens.webview import eu.friedcutlet.yokan.ui.ViewModelTestHost import eu.friedcutlet.yokan.data.DefaultYokanRepository import eu.friedcutlet.yokan.data.SettingsStore import eu.friedcutlet.yokan.database.InMemoryYokanDatabase import eu.friedcutlet.yokan.services.http.HttpResponse import eu.friedcutlet.yokan.services.http.cookies.CookieJar import eu.friedcutlet.yokan.services.webview.WebViewRequest import eu.friedcutlet.yokan.services.webview.WebViewService import kotlin.test.AfterTest import kotlin.test.Test import kotlin.test.assertEquals import kotlinx.coroutines.CompletableDeferred import kotlinx.coroutines.CancellationException import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.NonCancellable import kotlinx.coroutines.test.TestScope import kotlinx.coroutines.test.runTest import kotlinx.coroutines.withContext @OptIn(ExperimentalCoroutinesApi::class) class InAppWebViewViewModelTest { private val host = ViewModelTestHost() private val dispatcher = host.dispatcher @AfterTest fun tearDown() = host.close() @Test fun verificationUsesRedirectedPageAndAllowsOnlyOneBrowserSession() = runTest(dispatcher) { val completion = CompletableDeferred() val browser = FakeBrowser { completion.await() } val model = model(browser) model.requireBrowserVerification(redirectUrl) model.solveCloudflare(redirectUrl, "source-agent") model.solveCloudflare(redirectUrl, "source-agent") assertEquals(BrowserVerificationStatus.Waiting, model.state.value.verificationStatus) assertEquals(1, browser.requests.size) assertEquals(redirectUrl, browser.requests.single().url) assertEquals("source-agent", browser.requests.single().headers["User-Agent"]) completion.complete(HttpResponse(200, "Novel UpdatesContent")) assertEquals(BrowserVerificationStatus.Verified, model.state.value.verificationStatus) // Repeated native notifications must not reset the completed state or start another browser. model.requireBrowserVerification(redirectUrl) assertEquals(BrowserVerificationStatus.Verified, model.state.value.verificationStatus) assertEquals(1, browser.requests.size) } @Test fun closedPopupAndUnsolvedPagesRemainRetryable() = runTest(dispatcher) { for (response in listOf(null, HttpResponse(403, "Denied"), HttpResponse(200, "Just a moment..."))) { val browser = FakeBrowser { response } val model = model(browser) model.requireBrowserVerification(redirectUrl) model.solveCloudflare(redirectUrl, null) assertEquals(BrowserVerificationStatus.Failed, model.state.value.verificationStatus) model.solveCloudflare(redirectUrl, null) assertEquals(2, browser.requests.size) } } @Test fun browserFailureDoesNotLeaveVerificationWaiting() = runTest(dispatcher) { val model = model(FakeBrowser { error("Browser unavailable") }) model.requireBrowserVerification(redirectUrl) model.solveCloudflare(redirectUrl, null) assertEquals(BrowserVerificationStatus.Failed, model.state.value.verificationStatus) } @Test fun nativeChallengeNotificationDuringManualVerificationKeepsWaiting() = runTest(dispatcher) { val completion = CompletableDeferred() val model = model(FakeBrowser { completion.await() }) model.solveCloudflare(redirectUrl, null) model.requireBrowserVerification(redirectUrl) assertEquals(BrowserVerificationStatus.Waiting, model.state.value.verificationStatus) completion.complete(HttpResponse(200, "Content")) assertEquals(BrowserVerificationStatus.Verified, model.state.value.verificationStatus) } @Test fun newChallengeIgnoresLateCompletionFromPreviousPage() = runTest(dispatcher) { val oldCompletion = CompletableDeferred() val newCompletion = CompletableDeferred() var calls = 0 val browser = FakeBrowser { if (++calls == 1) withContext(NonCancellable) { oldCompletion.await() } else newCompletion.await() } val model = model(browser) try { model.requireBrowserVerification(redirectUrl) model.solveCloudflare(redirectUrl, null) val nextUrl = "https://translator.test/chapter/1" model.requireBrowserVerification(nextUrl) model.solveCloudflare(nextUrl, null) assertEquals(2, browser.requests.size) oldCompletion.complete(HttpResponse(200, "Previous page")) assertEquals(nextUrl, model.state.value.verificationUrl) assertEquals(BrowserVerificationStatus.Waiting, model.state.value.verificationStatus) newCompletion.complete(HttpResponse(403, "Denied")) assertEquals(BrowserVerificationStatus.Failed, model.state.value.verificationStatus) } finally { oldCompletion.complete(null) newCompletion.complete(null) } } @Test fun cancelledBrowserSessionRemainsRetryable() = runTest(dispatcher) { var calls = 0 val model = model(FakeBrowser { if (++calls == 1) throw CancellationException("Browser closed") HttpResponse(200, "Content") }) model.requireBrowserVerification(redirectUrl) model.solveCloudflare(redirectUrl, null) assertEquals(BrowserVerificationStatus.Failed, model.state.value.verificationStatus) assertEquals(1f, model.state.value.progress) model.solveCloudflare(redirectUrl, null) assertEquals(BrowserVerificationStatus.Verified, model.state.value.verificationStatus) } private fun TestScope.model(browser: WebViewService): InAppWebViewViewModel { val db = InMemoryYokanDatabase() return host.create { InAppWebViewViewModel(browser, CookieJar(db), SettingsStore(DefaultYokanRepository(db), backgroundScope) {}) } } private val redirectUrl = "https://www.novelupdates.com/series-finder/" } private class FakeBrowser(private val solve: suspend () -> HttpResponse?) : WebViewService { val requests = mutableListOf() override suspend fun open(request: WebViewRequest): Boolean = false override suspend fun resolveCloudflare(request: WebViewRequest): HttpResponse? { requests += request return solve() } }