import json import tempfile import unittest from pathlib import Path from unittest.mock import patch from fastapi.testclient import TestClient from music_server.app import create_app class AppUpdateRouteTests(unittest.TestCase): def test_app_version_json_returns_server_apk_download_url(self): with tempfile.TemporaryDirectory() as tmp: release_dir = Path(tmp) version_json = release_dir / "version.json" apk_path = release_dir / "MusicFree_latest_release_universal.apk" version_json.write_text( json.dumps( { "version": "9.9.9", "changeLog": ["server app update"], "download": ["https://official.example/download"], } ), encoding="utf-8", ) apk_path.write_bytes(b"fake-apk") with patch.dict( "os.environ", { "MUSICFREE_VERSION_JSON": str(version_json), "MUSICFREE_APK_PATH": str(apk_path), "MUSIC_SERVER_CACHE_RECONCILE_INTERVAL_SECONDS": "0", }, clear=False, ): client = TestClient(create_app()) response = client.get("/app/version.json") self.assertEqual(200, response.status_code) self.assertIn("application/json", response.headers.get("content-type", "")) payload = response.json() self.assertEqual("9.9.9", payload["version"]) self.assertEqual(["server app update"], payload["changeLog"]) self.assertEqual( ["http://testserver/app/MusicFree_latest_release_universal.apk"], payload["download"], ) def test_app_apk_route_serves_configured_apk_file(self): with tempfile.TemporaryDirectory() as tmp: release_dir = Path(tmp) version_json = release_dir / "version.json" apk_path = release_dir / "MusicFree_latest_release_universal.apk" version_json.write_text( json.dumps({"version": "9.9.9", "changeLog": [], "download": []}), encoding="utf-8", ) apk_path.write_bytes(b"fake-apk") with patch.dict( "os.environ", { "MUSICFREE_VERSION_JSON": str(version_json), "MUSICFREE_APK_PATH": str(apk_path), "MUSIC_SERVER_CACHE_RECONCILE_INTERVAL_SECONDS": "0", }, clear=False, ): client = TestClient(create_app()) response = client.get("/app/MusicFree_latest_release_universal.apk") self.assertEqual(200, response.status_code) self.assertEqual(b"fake-apk", response.content) self.assertIn( "application/vnd.android.package-archive", response.headers.get("content-type", ""), ) if __name__ == "__main__": unittest.main()