Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions android/app/src/test/java/com/clhs/score/data/UpdateCheckerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,41 @@ class UpdateCheckerTest {
assertTrue(result.htmlUrl.startsWith("https://github.com/"))
}

@Test
fun networkExceptionReturnsError() = runTest {
val exceptionClient = OkHttpClient.Builder()
.addInterceptor { chain -> throw java.io.IOException("Network failed") }
.build()

val checker = UpdateChecker(
client = exceptionClient,
latestReleaseUrl = server.url("/release/latest").toString(),
)

assertEquals(UpdateResult.Error("Network failed"), checker.check("1.1.9"))
}

@Test
fun nonSuccessfulHttpResponseReturnsError() = runTest {
server.enqueue(MockResponse().setResponseCode(404))

assertEquals(UpdateResult.Error("HTTP 404"), checker().check("1.1.9"))
}

@Test
fun networkExceptionWithoutMessageReturnsDefaultError() = runTest {
val exceptionClient = OkHttpClient.Builder()
.addInterceptor { chain -> throw java.io.IOException() }
.build()

val checker = UpdateChecker(
client = exceptionClient,
latestReleaseUrl = server.url("/release/latest").toString(),
)

assertEquals(UpdateResult.Error("未知錯誤"), checker.check("1.1.9"))
}

private fun checker(): UpdateChecker =
UpdateChecker(
client = OkHttpClient(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package com.clhs.score.data

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This file (UpdateCheckerTest.kt.orig) is a backup file created during conflict resolution or patching. It should be removed from the repository.


import kotlinx.coroutines.test.runTest
import okhttp3.OkHttpClient
import okhttp3.mockwebserver.MockResponse
Comment on lines +1 to +5
import okhttp3.mockwebserver.MockWebServer
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test

class UpdateCheckerTest {
private lateinit var server: MockWebServer

@Before
fun setUp() {
server = MockWebServer()
server.start()
}

@After
fun tearDown() {
server.shutdown()
}

@Test
fun newerReleaseReturnsValidatedDownloadUrl() = runTest {
server.enqueue(
jsonResponse(
"""
{
"tag_name": "v1.2.0",
"html_url": "https://github.com/alvin000009238/clhs_score/releases/tag/v1.2.0",
"body": "Bug fixes",
"assets": [
{
"name": "clhs-score.apk",
"browser_download_url": "https://github.com/alvin000009238/clhs_score/releases/download/v1.2.0/app.apk"
}
]
}
""".trimIndent(),
),
)

val result = checker().check("1.1.9")

result as UpdateResult.NewVersion
assertEquals("1.2.0", result.versionName)
assertEquals("Bug fixes", result.releaseNotes)
assertEquals(
"https://github.com/alvin000009238/clhs_score/releases/download/v1.2.0/app.apk",
result.apkDownloadUrl,
)
}

@Test
fun sameVersionReturnsUpToDate() = runTest {
server.enqueue(
jsonResponse(
"""
{
"tag_name": "v1.2.0",
"html_url": "https://github.com/alvin000009238/clhs_score/releases/tag/v1.2.0",
"body": "",
"assets": []
}
""".trimIndent(),
),
)

assertEquals(UpdateResult.UpToDate, checker().check("1.2.0"))
}

@Test
fun invalidReleaseLinksReturnErrorInsteadOfActionableUpdate() = runTest {
server.enqueue(
jsonResponse(
"""
{
"tag_name": "v1.2.0",
"html_url": "javascript:alert(1)",
"body": "",
"assets": [
{
"name": "clhs-score.apk",
"browser_download_url": "file:///tmp/app.apk"
}
]
}
""".trimIndent(),
),
)

val result = checker().check("1.1.9")

result as UpdateResult.Error
assertEquals("更新連結格式不正確", result.message)
}

@Test
fun invalidApkUrlFallsBackToValidHtmlUrl() = runTest {
server.enqueue(
jsonResponse(
"""
{
"tag_name": "v1.2.0",
"html_url": "https://github.com/alvin000009238/clhs_score/releases/tag/v1.2.0",
"body": "",
"assets": [
{
"name": "clhs-score.apk",
"browser_download_url": "intent://download"
}
]
}
""".trimIndent(),
),
)

val result = checker().check("1.1.9")

result as UpdateResult.NewVersion
assertNull(result.apkDownloadUrl)
assertTrue(result.htmlUrl.startsWith("https://github.com/"))
}

@Test
fun networkExceptionReturnsError() = runTest {
val exceptionClient = OkHttpClient.Builder()
.addInterceptor { chain -> throw java.io.IOException("Network failed") }
.build()

val checker = UpdateChecker(
client = exceptionClient,
latestReleaseUrl = server.url("/release/latest").toString(),
)

assertEquals(UpdateResult.Error("Network failed"), checker.check("1.1.9"))
}

@Test
fun nonSuccessfulHttpResponseReturnsError() = runTest {
server.enqueue(MockResponse().setResponseCode(404))

assertEquals(UpdateResult.Error("HTTP 404"), checker().check("1.1.9"))
}

private fun checker(): UpdateChecker =
UpdateChecker(
client = OkHttpClient(),
latestReleaseUrl = server.url("/release/latest").toString(),
)

private fun jsonResponse(body: String): MockResponse =
MockResponse()
.setResponseCode(200)
.setHeader("Content-Type", "application/json")
.setBody(body)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
--- UpdateCheckerTest.kt

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This file (UpdateCheckerTest.kt.rej) is a patch reject file generated when a patch failed to apply cleanly. It should be removed from the repository.

+++ UpdateCheckerTest.kt
@@ -137,14 +137,14 @@
latestReleaseUrl = server.url("/release/latest").toString(),
)

Comment on lines +1 to +6
- assertEquals(UpdateResult.Error("Network failed"), checker.check("1.1.9"))
+ assertNull(checker.check("1.1.9"))
}

@Test
fun nonSuccessfulHttpResponseReturnsError() = runTest {
server.enqueue(MockResponse().setResponseCode(404))

- assertEquals(UpdateResult.Error("HTTP 404"), checker().check("1.1.9"))
+ assertNull(checker().check("1.1.9"))
}

@Test
@@ -158,7 +158,7 @@
latestReleaseUrl = server.url("/release/latest").toString(),
)

- assertEquals(UpdateResult.Error("未知錯誤"), checker.check("1.1.9"))
+ assertNull(checker.check("1.1.9"))
}

private fun checker(): UpdateChecker =
23 changes: 23 additions & 0 deletions fix_test.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
--- android/app/src/test/java/com/clhs/score/data/UpdateCheckerTest.kt

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This file (fix_test.patch) appears to be a temporary patch file generated during development or conflict resolution. It should not be committed to the repository and should be deleted.

+++ android/app/src/test/java/com/clhs/score/data/UpdateCheckerTest.kt
@@ -128,6 +128,20 @@
assertEquals(UpdateResult.Error("HTTP 404"), checker().check("1.1.9"))
}
Comment on lines +1 to +5

+ @Test
+ fun networkExceptionWithoutMessageReturnsDefaultError() = runTest {
+ val exceptionClient = OkHttpClient.Builder()
+ .addInterceptor { chain -> throw java.io.IOException() }
+ .build()
+
+ val checker = UpdateChecker(
+ client = exceptionClient,
+ latestReleaseUrl = server.url("/release/latest").toString(),
+ )
+
+ assertEquals(UpdateResult.Error("未知錯誤"), checker.check("1.1.9"))
+ }
+
private fun checker(): UpdateChecker =
UpdateChecker(
client = OkHttpClient(),
Loading