diff --git a/src/studyflow/platforms/ucampus/course_progress.py b/src/studyflow/platforms/ucampus/course_progress.py index 347eb3a8..112e776a 100644 --- a/src/studyflow/platforms/ucampus/course_progress.py +++ b/src/studyflow/platforms/ucampus/course_progress.py @@ -9,6 +9,7 @@ import base64 import json +import math import re from typing import Any, TYPE_CHECKING from urllib.parse import quote @@ -137,7 +138,9 @@ def parse_tasks(rt: dict[str, Any], unit_id: str) -> list[dict[str, Any]]: state = leaf.get("state") required = strategies.get("required") if isinstance(strategies, dict) else None passed = state.get("pass") if isinstance(state, dict) else None - if passed is True or passed == 1 or isinstance(passed, (int, float)) and passed > 1: + if (passed is True or + (type(passed) is int or type(passed) is float and math.isfinite(passed)) + and passed >= 1): completed = True elif passed in (0, "0") and passed is not None: completed = False diff --git a/tests/test_ucampus_progress.py b/tests/test_ucampus_progress.py index 691d9061..18441166 100644 --- a/tests/test_ucampus_progress.py +++ b/tests/test_ucampus_progress.py @@ -201,3 +201,27 @@ class Response: lambda *a, **kw: Response()) with pytest.raises(UCampusProgressError, match=expected): UCampusProgressReader(credentials_loader=lambda: cfg).list_units("course-v2:real") + + +@pytest.mark.parametrize("invalid", [float("nan"), float("inf"), float("-inf")]) +def test_nonfinite_server_pass_is_unknown_not_completed(invalid): + tasks = parse_tasks({"leafs": { + "video1": {"tab_type": "video", "state": {"pass": invalid}}, + }}, "unit1") + assert len(tasks) == 1 + assert tasks[0]["completed"] is None + + +def test_numeric_server_pass_keeps_previous_valid_behavior(): + for value, expected in ((True, True), (1, True), (2, True), (1.0, True), + (0, False), (False, False), (-1, None)): + tasks = parse_tasks({"leafs": { + "video1": {"tab_type": "video", "state": {"pass": value}}, + }}, "unit1") + assert tasks[0]["completed"] is expected + # Large integer counters should not pass through float(), which may raise + # OverflowError for a JSON integer with more digits than a float can hold. + huge = parse_tasks({"leafs": { + "video1": {"tab_type": "video", "state": {"pass": 10 ** 400}}, + }}, "unit1") + assert huge[0]["completed"] is True