diff --git a/src/roborock_local_server/bundled_backend/https_server/routes/api/v1/appplugin.py b/src/roborock_local_server/bundled_backend/https_server/routes/api/v1/appplugin.py index 6f1b74a5..4df6a990 100644 --- a/src/roborock_local_server/bundled_backend/https_server/routes/api/v1/appplugin.py +++ b/src/roborock_local_server/bundled_backend/https_server/routes/api/v1/appplugin.py @@ -4,9 +4,10 @@ from typing import Any from shared.context import ServerContext +from shared.data_helpers import as_int from ...auth.service import ok -from ...plugin.common import APPPLUGIN_LIST, plugin_records_with_snapshot_fallback, proxied_plugin_records +from ...plugin.common import APPPLUGIN_LIST, proxied_plugin_records, snapshot_plugin_records # Request "type" of the iOS app; Android sends 2. IOS_APP_TYPE = 1 @@ -30,15 +31,58 @@ def _request_app_type(body_params: dict[str, list[str]]) -> int | None: return None +def _requested_product_ids(body_params: dict[str, list[str]]) -> set[int] | None: + # The app posts JSON like {"apilevel": 10046, "productids": [263], "type": 2}. + # Form bodies may repeat the field or join the ids with commas. None means + # that the request sent no ids. An empty set means that it sent only ids + # that are not valid product ids. + values: list[Any] = [] + for raw in body_params.get("__json") or []: + try: + parsed = json.loads(raw) + except (TypeError, ValueError): + continue + if isinstance(parsed, dict): + requested = parsed.get("productids") + values.extend(requested if isinstance(requested, list) else [requested]) + for raw in body_params.get("productids") or []: + values.extend(str(raw).strip("[] ").split(",")) + values = [value for value in values if value is not None and str(value).strip()] + if not values: + return None + product_ids = {as_int(value, 0) for value in values} + product_ids.discard(0) + return product_ids + + +def _product_id(record: dict[str, Any]) -> int: + return as_int(record.get("productid"), 0) + + def build( ctx: ServerContext, _query_params: dict[str, list[str]], body_params: dict[str, list[str]], _clean_path: str, ) -> dict[str, Any]: - records = plugin_records_with_snapshot_fallback(ctx, "appplugin", APPPLUGIN_LIST) + snapshot_records = snapshot_plugin_records(ctx, "appplugin") if _request_app_type(body_params) == IOS_APP_TYPE: # Same plugin version, different zip: the iOS app cannot load the Android # one. Imports from before the iOS catalog existed fall back to Android. - records = plugin_records_with_snapshot_fallback(ctx, "appplugin_ios", records) - return ok(proxied_plugin_records(ctx, records)) + snapshot_records = snapshot_plugin_records(ctx, "appplugin_ios") or snapshot_records + product_ids = _requested_product_ids(body_params) + if product_ids is None: + return ok(proxied_plugin_records(ctx, snapshot_records or APPPLUGIN_LIST)) + # When the app opens a device, it installs the first record without + # checking its productid. So a device must only get the records for the + # product ids it asked for. If no record matches, the answer is an empty + # list. + matching = [record for record in snapshot_records if _product_id(record) in product_ids] + # A device added after the snapshot import has no snapshot record, so the + # ids that the snapshot lacks come from the built-in list. The built-in list + # is the same for both platforms. So an iOS request can get an Android zip + # here, which the iOS app cannot load. The fallback to the Android catalog + # above has the same limitation if the snapshot has no iOS catalog. + missing_ids = product_ids - {_product_id(record) for record in snapshot_records} + matching += [record for record in APPPLUGIN_LIST if _product_id(record) in missing_ids] + return ok(proxied_plugin_records(ctx, matching)) diff --git a/tests/test_plugin_routes.py b/tests/test_plugin_routes.py index 686a59ea..1c65e70b 100644 --- a/tests/test_plugin_routes.py +++ b/tests/test_plugin_routes.py @@ -128,6 +128,172 @@ def test_app_plugin_endpoints_return_proxied_urls(tmp_path: Path) -> None: assert str(item["url"]).startswith("https://api-roborock.example.com/plugin/proxy/") +def _app_plugin_product_ids(payload: dict) -> list[int]: + assert payload["code"] == 200 + return [item["productid"] for item in payload["data"]] + + +def test_app_plugin_answers_only_requested_product_ids(tmp_path: Path) -> None: + supervisor = _build_supervisor(tmp_path) + client = TestClient(supervisor.app) + + # The app posts this JSON body and installs the first record it gets back. + response = client.post("/api/v1/appplugin", json={"apilevel": 10046, "productids": [23], "type": 2}) + + assert response.status_code == 200 + assert _app_plugin_product_ids(response.json()) == [23] + [record] = response.json()["data"] + assert record["url"].startswith("https://api-roborock.example.com/plugin/proxy/") + + +def test_app_plugin_reads_product_ids_from_form_body(tmp_path: Path) -> None: + supervisor = _build_supervisor(tmp_path) + client = TestClient(supervisor.app) + + repeated = client.post("/api/v1/appplugin", data={"type": "2", "productids": ["23", "10001"]}) + joined = client.post("/api/v1/appplugin", data={"type": "2", "productids": "23,10001"}) + + assert _app_plugin_product_ids(repeated.json()) == [23, 10001] + assert _app_plugin_product_ids(joined.json()) == [23, 10001] + + +def _write_android_app_plugin_snapshot(supervisor: ReleaseSupervisor, records: list[dict]) -> None: + _write_app_plugin_snapshot(supervisor, {"appplugin": {"data": records}}) + + +_SNAPSHOT_APP_PLUGIN_RECORD = { + "version": 7777, + "url": "https://rrpkg-us.roborock.com/iot/plugin/cloud-only.zip", + "pluginLevel": 3001, + "productid": 4242, + "apilevel": 10043, +} + +# Run each case once with the built-in list and once with a cloud snapshot catalog. +_APP_PLUGIN_CATALOGS = pytest.mark.parametrize( + "snapshot_records", + [None, [_SNAPSHOT_APP_PLUGIN_RECORD]], + ids=["built_in", "snapshot"], +) + + +@_APP_PLUGIN_CATALOGS +def test_app_plugin_answers_full_list_without_product_ids(tmp_path: Path, snapshot_records: list[dict] | None) -> None: + from https_server.routes.plugin import common as plugin_common + + supervisor = _build_supervisor(tmp_path) + if snapshot_records is not None: + _write_android_app_plugin_snapshot(supervisor, snapshot_records) + client = TestClient(supervisor.app) + full_list = [record["productid"] for record in snapshot_records or plugin_common.APPPLUGIN_LIST] + + no_body = client.post("/api/v1/appplugin") + no_ids = client.post("/api/v1/appplugin", json={"apilevel": 10046, "type": 2}) + empty_ids = client.post("/api/v1/appplugin", json={"apilevel": 10046, "productids": [], "type": 2}) + + assert _app_plugin_product_ids(no_body.json()) == full_list + assert _app_plugin_product_ids(no_ids.json()) == full_list + assert _app_plugin_product_ids(empty_ids.json()) == full_list + + +@_APP_PLUGIN_CATALOGS +def test_app_plugin_answers_no_plugin_for_unmatched_product_ids( + tmp_path: Path, snapshot_records: list[dict] | None +) -> None: + supervisor = _build_supervisor(tmp_path) + if snapshot_records is not None: + _write_android_app_plugin_snapshot(supervisor, snapshot_records) + client = TestClient(supervisor.app) + + # The app would install the first record of a full list, which is the plugin of another model. + unknown_id = client.post("/api/v1/appplugin", json={"apilevel": 10046, "productids": [659231], "type": 2}) + # Ids that are no valid product ids match nothing either. + invalid_ids = client.post("/api/v1/appplugin", json={"apilevel": 10046, "productids": ["abc", 0], "type": 2}) + invalid_form_id = client.post("/api/v1/appplugin", data={"productids": "abc"}) + + assert _app_plugin_product_ids(unknown_id.json()) == [] + assert _app_plugin_product_ids(invalid_ids.json()) == [] + assert _app_plugin_product_ids(invalid_form_id.json()) == [] + + +def test_app_plugin_filters_cloud_snapshot_catalog(tmp_path: Path) -> None: + supervisor = _build_supervisor(tmp_path) + snapshot = { + "web_api_cache": { + "plugin_catalogs": { + "appplugin": { + "data": [ + { + "version": 8888, + "url": "https://rrpkg-us.roborock.com/iot/plugin/cloud-first.zip", + "pluginLevel": 3001, + "productid": 110, + "apilevel": 10043, + }, + { + "version": 7777, + "url": "https://rrpkg-us.roborock.com/iot/plugin/cloud-second.zip", + "pluginLevel": 3002, + "productid": 4242, + "apilevel": 10043, + }, + ] + }, + } + } + } + supervisor.paths.cloud_snapshot_path.write_text(json.dumps(snapshot) + "\n", encoding="utf-8") + client = TestClient(supervisor.app) + + response = client.post("/api/v1/appplugin", json={"apilevel": 10046, "productids": [4242], "type": 2}) + + [record] = response.json()["data"] + assert record["version"] == 7777 + assert _source_from_proxied_url(record["url"]) == "https://rrpkg-us.roborock.com/iot/plugin/cloud-second.zip" + + +def test_app_plugin_falls_back_to_built_in_list_for_ids_missing_from_snapshot(tmp_path: Path) -> None: + supervisor = _build_supervisor(tmp_path) + # A device added after the snapshot import has no record in the snapshot. + _write_android_app_plugin_snapshot(supervisor, [_SNAPSHOT_APP_PLUGIN_RECORD]) + client = TestClient(supervisor.app) + + missing = client.post("/api/v1/appplugin", json={"apilevel": 10046, "productids": [23], "type": 2}) + mixed = client.post("/api/v1/appplugin", json={"apilevel": 10046, "productids": [23, 4242], "type": 2}) + + [record] = missing.json()["data"] + assert record["productid"] == 23 + assert record["version"] == 5208 + assert _source_from_proxied_url(record["url"]) == ( + "https://cdn.awsusor0.fds.api.mi-img.com/resources/iot/plugin/7cf4eb4c705c420483741189be389927.zip" + ) + # Snapshot records come first, built-in records after them. + assert _app_plugin_product_ids(mixed.json()) == [4242, 23] + + +def test_app_plugin_prefers_snapshot_record_over_built_in_record(tmp_path: Path) -> None: + supervisor = _build_supervisor(tmp_path) + _write_android_app_plugin_snapshot( + supervisor, + [ + { + "version": 9999, + "url": "https://rrpkg-us.roborock.com/iot/plugin/cloud-23.zip", + "pluginLevel": 3001, + "productid": 23, + "apilevel": 10043, + } + ], + ) + client = TestClient(supervisor.app) + + response = client.post("/api/v1/appplugin", json={"apilevel": 10046, "productids": [23, 10001], "type": 2}) + + records = response.json()["data"] + assert [(item["productid"], item["version"]) for item in records] == [(23, 9999), (10001, 90)] + assert _source_from_proxied_url(records[0]["url"]) == "https://rrpkg-us.roborock.com/iot/plugin/cloud-23.zip" + + def test_plugin_category_zip_uses_expected_source_and_cache(tmp_path: Path, monkeypatch) -> None: from https_server.routes.plugin import common as plugin_common @@ -288,3 +454,78 @@ def test_app_plugin_ios_falls_back_to_android_catalog_from_older_imports(tmp_pat [record] = response.json()["data"] assert _source_from_proxied_url(record["url"]) == ANDROID_ZIP + + +def _app_plugin_record(product_id: int, url: str) -> dict: + return {"version": 6846, "url": url, "pluginLevel": 3002, "productid": product_id, "apilevel": 10030} + + +IOS_ONLY_ZIP = "https://app-files.roborock.com/iot/plugin/ios-only.zip" +ANDROID_ONLY_ZIP = "https://app-files.roborock.com/iot/plugin/android-only.zip" +ANDROID_23_ZIP = "https://app-files.roborock.com/iot/plugin/android-23.zip" + + +def _ios_app_plugin_client(tmp_path: Path) -> TestClient: + # Product 199 is in both catalogs, and 300 is only in the iOS catalog. Products 23 + # and 4242 are only in the Android catalog. The built-in list also has 23. + supervisor = _build_supervisor(tmp_path) + _write_app_plugin_snapshot( + supervisor, + { + "appplugin": { + "data": [ + _app_plugin_record(199, ANDROID_ZIP), + _app_plugin_record(23, ANDROID_23_ZIP), + _app_plugin_record(4242, ANDROID_ONLY_ZIP), + ] + }, + "appplugin_ios": {"data": [_app_plugin_record(199, IOS_ZIP), _app_plugin_record(300, IOS_ONLY_ZIP)]}, + }, + ) + return TestClient(supervisor.app) + + +def _app_plugin_sources(payload: dict) -> list[tuple[int, str]]: + assert payload["code"] == 200 + return [(item["productid"], _source_from_proxied_url(item["url"])) for item in payload["data"]] + + +def test_app_plugin_ios_answers_requested_ids_from_ios_catalog(tmp_path: Path) -> None: + client = _ios_app_plugin_client(tmp_path) + + response = client.post("/api/v1/appplugin", json={"apilevel": 10046, "productids": [199], "type": 1}) + + assert _app_plugin_sources(response.json()) == [(199, IOS_ZIP)] + + +def test_app_plugin_ios_ignores_android_catalog_if_ios_catalog_exists(tmp_path: Path) -> None: + from https_server.routes.plugin import common as plugin_common + + client = _ios_app_plugin_client(tmp_path) + [built_in_23] = [record for record in plugin_common.APPPLUGIN_LIST if record["productid"] == 23] + + # The iOS catalog replaces the whole Android catalog. So an id that only the + # Android catalog has gets the built-in record, or nothing. + android_only = client.post("/api/v1/appplugin", json={"apilevel": 10046, "productids": [4242], "type": 1}) + built_in = client.post("/api/v1/appplugin", json={"apilevel": 10046, "productids": [23], "type": 1}) + + assert _app_plugin_sources(android_only.json()) == [] + assert _app_plugin_sources(built_in.json()) == [(23, built_in_23["url"])] + + +def test_app_plugin_ios_answers_no_plugin_for_unmatched_product_ids(tmp_path: Path) -> None: + client = _ios_app_plugin_client(tmp_path) + + response = client.post("/api/v1/appplugin", json={"apilevel": 10046, "productids": [659231], "type": 1}) + + assert _app_plugin_sources(response.json()) == [] + + +def test_app_plugin_ios_answers_full_ios_catalog_without_product_ids(tmp_path: Path) -> None: + client = _ios_app_plugin_client(tmp_path) + + no_ids = client.post("/api/v1/appplugin", json={"apilevel": 10046, "type": 1}) + empty_ids = client.post("/api/v1/appplugin", json={"apilevel": 10046, "productids": [], "type": 1}) + + assert _app_plugin_sources(no_ids.json()) == [(199, IOS_ZIP), (300, IOS_ONLY_ZIP)] + assert _app_plugin_sources(empty_ids.json()) == [(199, IOS_ZIP), (300, IOS_ONLY_ZIP)]