feat: add manual HLTB matching

Allow setting an HLTB ID for missed games via POST /api/hltb/match and a small modal on the HLTB page.
This commit is contained in:
strsv committed 2026-11-07 05:03:56 +03:00
1 parent 92c4c62ca9
commit 6644a94d8c
5 files changed
+169 -2

No files matched your search

+42
View File
@@ -12,6 +12,7 @@ import urllib.error
import urllib.parse
import urllib.request
from http.server import ThreadingHTTPServer
from types import SimpleNamespace
from unittest import mock
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
@@ -834,6 +835,47 @@ class ApiTests(unittest.TestCase):
data = self.post("/api/hltb", {"limit": 5}, expect=400)
self.assertIn("howlongtobeatpy", data["error"])
def test_hltb_manual_match(self):
entry = SimpleNamespace(main_story=10.0, main_extra=12.0,
completionist=15.0, game_id=999)
class FakeSearch:
def search_from_id(self, hltb_id):
return entry if hltb_id == 999 else None
try:
with mock.patch.object(server.importlib.util, "find_spec", return_value=object()), \
mock.patch.object(server.fetch_hltb, "HowLongToBeat",
return_value=FakeSearch()):
data = self.post("/api/hltb/match", {"appid": 2, "hltb_id": 999})
self.assertEqual(data["estimated_hours"], 10.0)
self.assertEqual(self.post("/api/hltb/match", {
"appid": 2, "hltb_id": 0}, expect=400)["error"], "invalid HLTB id")
self.post("/api/hltb/match", {"appid": 2, "hltb_id": 123}, expect=404)
con = db.connect(self.db_path)
try:
row = con.execute(
"SELECT hltb_main, hltb_match, hltb_id FROM games WHERE appid = 2"
).fetchone()
finally:
con.close()
self.assertEqual(row["hltb_main"], 10.0)
self.assertEqual(row["hltb_match"], 1)
self.assertEqual(row["hltb_id"], 999)
finally:
con = db.connect(self.db_path)
try:
con.execute(
"""UPDATE games SET hltb_main = NULL, hltb_extra = NULL,
hltb_complete = NULL, hltb_id = NULL,
hltb_checked_at = '2026-01-02', hltb_match = 0,
hltb_updated_at = NULL, estimated_hours = 10
WHERE appid = 2""")
con.commit()
finally:
con.close()
if __name__ == "__main__":
unittest.main()