Yokan / rebuild
Course 1 8624561

AnilistWatchOrderServiceTest.kt

composeApp/src/commonTest/kotlin/eu/friedcutlet/yokan/services/metadata/AnilistWatchOrderServiceTest.kt

test / fixture313 lines
1package eu.friedcutlet.yokan.services.metadata23import eu.friedcutlet.yokan.services.http.HttpResponse4import eu.friedcutlet.yokan.services.http.RecordingHttpService5import kotlin.test.Test6import kotlin.test.assertEquals7import kotlin.test.assertTrue8import kotlinx.coroutines.test.runTest910class AnilistWatchOrderServiceTest {11    @Test12    fun searchQueriesAniListGraphQLAndParsesResults() = runTest {13        val http = RecordingHttpService { req ->14            assertTrue("graphql.anilist.co" in req.url)15            assertTrue("POST" == req.method)16            assertTrue("Mushoku Tensei" in req.body.orEmpty())17            HttpResponse(18                statusCode = 200,19                body = """20                    {21                      "data": {22                        "Page": {23                          "media": [24                            {25                              "id": 108465,26                              "idMal": 39535,27                              "title": {28                                "romaji": "Mushoku Tensei: Isekai Ittara Honki Dasu",29                                "english": "Mushoku Tensei: Jobless Reincarnation",30                                "native": "無職転生"31                              },32                              "coverImage": { "large": "https://s4.anilist.co/cover1.jpg", "color": "#f1a852" },33                              "format": "TV",34                              "episodes": 11,35                              "seasonYear": 2021,36                              "startDate": { "year": 2021, "month": 1 }37                            }38                          ]39                        }40                      }41                    }42                """.trimIndent(),43            )44        }4546        val service = WatchOrderService(http)47        val hits = service.search("Mushoku Tensei")4849        assertEquals(1, hits.size)50        assertEquals("108465", hits[0].id)51        assertEquals("Mushoku Tensei: Jobless Reincarnation", hits[0].name)52        assertEquals("https://s4.anilist.co/cover1.jpg", hits[0].imageUrl)53        assertEquals("TV", hits[0].type)54        assertEquals(2021, hits[0].year)55    }5657    @Test58    fun watchOrderWalksRelationGraphFiltersNonAnimeOrdersChronologicallyAndMarksRoles() = runTest {59        // Media nodes:60        // S1: 108465 (2021/01) -> SEQUEL: 127720, SIDE_STORY: 146065, ADAPTATION (manga): 8547061        // S1 Part 2: 127720 (2021/10) -> SEQUEL: 14606662        // OVA: 146065 (2022/03) -> no further chain63        // S2: 146066 (2023/07) -> SEQUEL: 16687364        // S2 Part 2: 166873 (2024/04) -> SEQUEL: 18063065        // S3: 180630 (2026/04) -> no further chain6667        val http = RecordingHttpService { req ->68            val body = req.body.orEmpty()69            val id = Regex("\"id\":\\s*(\\d+)").find(body)?.groupValues?.get(1)?.toIntOrNull()70            when (id) {71                108465 -> HttpResponse(72                    200,73                    """74                    {75                      "data": {76                        "Media": {77                          "id": 108465,78                          "title": { "romaji": "Mushoku Tensei S1", "english": "Jobless Reincarnation S1" },79                          "coverImage": { "large": "https://s4.anilist.co/s1.jpg" },80                          "format": "TV", "type": "ANIME", "episodes": 11,81                          "startDate": { "year": 2021, "month": 1 },82                          "relations": {83                            "edges": [84                              { "relationType": "SEQUEL", "node": { "id": 127720, "type": "ANIME", "startDate": { "year": 2021, "month": 10 } } },85                              { "relationType": "SIDE_STORY", "node": { "id": 146065, "type": "ANIME", "title": { "romaji": "Eris OVA" }, "coverImage": { "large": "https://s4.anilist.co/ova.jpg" }, "startDate": { "year": 2022, "month": 3 } } },86                              { "relationType": "ADAPTATION", "node": { "id": 85470, "type": "MANGA", "startDate": { "year": 2014, "month": 5 } } }87                            ]88                          }89                        }90                      }91                    }92                    """.trimIndent(),93                )94                127720 -> HttpResponse(95                    200,96                    """97                    {98                      "data": {99                        "Media": {100                          "id": 127720,101                          "title": { "romaji": "Mushoku Tensei S1 Part 2", "english": "Jobless Reincarnation S1 Part 2" },102                          "coverImage": { "large": "https://s4.anilist.co/s1p2.jpg" },103                          "format": "TV", "type": "ANIME", "episodes": 12,104                          "startDate": { "year": 2021, "month": 10 },105                          "relations": {106                            "edges": [107                              { "relationType": "PREQUEL", "node": { "id": 108465, "type": "ANIME", "startDate": { "year": 2021, "month": 1 } } },108                              { "relationType": "SEQUEL", "node": { "id": 146066, "type": "ANIME", "startDate": { "year": 2023, "month": 7 } } }109                            ]110                          }111                        }112                      }113                    }114                    """.trimIndent(),115                )116                146065 -> HttpResponse(117                    200,118                    """119                    {120                      "data": {121                        "Media": {122                          "id": 146065,123                          "title": { "romaji": "Eris the Goblin Slayer OVA" },124                          "coverImage": { "large": "https://s4.anilist.co/ova.jpg" },125                          "format": "OVA", "type": "ANIME", "episodes": 1,126                          "startDate": { "year": 2022, "month": 3 },127                          "relations": { "edges": [] }128                        }129                      }130                    }131                    """.trimIndent(),132                )133                146066 -> HttpResponse(134                    200,135                    """136                    {137                      "data": {138                        "Media": {139                          "id": 146066,140                          "title": { "romaji": "Mushoku Tensei S2", "english": "Jobless Reincarnation S2" },141                          "coverImage": { "large": "https://s4.anilist.co/s2.jpg" },142                          "format": "TV", "type": "ANIME", "episodes": 12,143                          "startDate": { "year": 2023, "month": 7 },144                          "relations": {145                            "edges": [146                              { "relationType": "PREQUEL", "node": { "id": 127720, "type": "ANIME", "startDate": { "year": 2021, "month": 10 } } },147                              { "relationType": "SEQUEL", "node": { "id": 166873, "type": "ANIME", "startDate": { "year": 2024, "month": 4 } } }148                            ]149                          }150                        }151                      }152                    }153                    """.trimIndent(),154                )155                166873 -> HttpResponse(156                    200,157                    """158                    {159                      "data": {160                        "Media": {161                          "id": 166873,162                          "title": { "romaji": "Mushoku Tensei S2 Part 2", "english": "Jobless Reincarnation S2 Part 2" },163                          "coverImage": { "large": "https://s4.anilist.co/s2p2.jpg" },164                          "format": "TV", "type": "ANIME", "episodes": 12,165                          "startDate": { "year": 2024, "month": 4 },166                          "relations": {167                            "edges": [168                              { "relationType": "PREQUEL", "node": { "id": 146066, "type": "ANIME", "startDate": { "year": 2023, "month": 7 } } },169                              { "relationType": "SEQUEL", "node": { "id": 180630, "type": "ANIME", "startDate": { "year": 2026, "month": 4 } } }170                            ]171                          }172                        }173                      }174                    }175                    """.trimIndent(),176                )177                180630 -> HttpResponse(178                    200,179                    """180                    {181                      "data": {182                        "Media": {183                          "id": 180630,184                          "title": { "romaji": "Mushoku Tensei S3", "english": "Jobless Reincarnation S3" },185                          "coverImage": { "large": "https://s4.anilist.co/s3.jpg" },186                          "format": "TV", "type": "ANIME", "episodes": 12,187                          "startDate": { "year": 2026, "month": 4 },188                          "relations": { "edges": [] }189                        }190                      }191                    }192                    """.trimIndent(),193                )194                else -> HttpResponse(404, "{}")195            }196        }197198        val service = WatchOrderService(http)199200        // Query anchored at S2 (146066)201        val entries = service.watchOrder("146066")202203        // S1 (2021/01), S1 Part 2 (2021/10), OVA (2022/03), S2 (2023/07), S2 Part 2 (2024/04), S3 (2026/04)204        assertEquals(6, entries.size)205206        // Verify chronological ordering207        assertEquals("108465", entries[0].id)208        assertEquals("127720", entries[1].id)209        assertEquals("146065", entries[2].id)210        assertEquals("146066", entries[3].id)211        assertEquals("166873", entries[4].id)212        assertEquals("180630", entries[5].id)213214        // Verify roles relative to anchor (146066 / S2)215        assertEquals(WatchOrderRole.Previous, entries[0].role)216        assertEquals(WatchOrderRole.Previous, entries[1].role)217        assertEquals(WatchOrderRole.Previous, entries[2].role)218        assertEquals(WatchOrderRole.Current, entries[3].role) // anchor219        assertEquals(WatchOrderRole.Next, entries[4].role)220        assertEquals(WatchOrderRole.Next, entries[5].role)221222        // Verify non-anime (Manga 85470) was excluded223        assertTrue(entries.none { it.id == "85470" })224    }225226    @Test227    fun watchOrderByNameAutoBuildsWatchOrderDirectly() = runTest {228        val http = RecordingHttpService { req ->229            val body = req.body.orEmpty()230            if ("search" in body) {231                HttpResponse(232                    200,233                    """234                    {235                      "data": {236                        "Page": {237                          "media": [238                            {239                              "id": 108465,240                              "title": { "romaji": "Mushoku Tensei" },241                              "startDate": { "year": 2021, "month": 1 }242                            }243                          ]244                        }245                      }246                    }247                    """.trimIndent(),248                )249            } else {250                HttpResponse(251                    200,252                    """253                    {254                      "data": {255                        "Media": {256                          "id": 108465,257                          "title": { "romaji": "Mushoku Tensei S1" },258                          "format": "TV", "type": "ANIME", "episodes": 11,259                          "startDate": { "year": 2021, "month": 1 },260                          "relations": { "edges": [] }261                        }262                      }263                    }264                    """.trimIndent(),265                )266            }267        }268269        val service = WatchOrderService(http)270        val entries = service.watchOrderByName("Mushoku Tensei")271272        assertEquals(1, entries.size)273        assertEquals("108465", entries[0].id)274        assertEquals(WatchOrderRole.Current, entries[0].role)275    }276277    @Test278    fun watchOrderAvoidsInfiniteLoopsOnCyclicRelationsAndRespectsQueryCap() = runTest {279        // Cyclic: A -> B -> A280        var callCount = 0281        val http = RecordingHttpService { req ->282            callCount++283            val id = Regex("\"id\":\\s*(\\d+)").find(req.body.orEmpty())?.groupValues?.get(1)?.toIntOrNull() ?: 1284            val nextId = if (id == 1) 2 else 1285            HttpResponse(286                200,287                """288                {289                  "data": {290                    "Media": {291                      "id": $id,292                      "title": { "romaji": "Anime $id" },293                      "format": "TV", "type": "ANIME",294                      "startDate": { "year": ${2020 + id}, "month": 1 },295                      "relations": {296                        "edges": [297                          { "relationType": "SEQUEL", "node": { "id": $nextId, "type": "ANIME" } }298                        ]299                      }300                    }301                  }302                }303                """.trimIndent(),304            )305        }306307        val service = WatchOrderService(http)308        val entries = service.watchOrder("1")309310        assertEquals(2, entries.size)311        assertEquals(2, callCount) // visited set stops the loop after node 1 and 2312    }313}314

Search chapters, reference and source filenames.