"""服务端工程配置替换测试。""" import plistlib import pytest from backend.services.project_patcher import ProjectPatchError, apply_project_config def test_apply_project_config_without_branch_autopacking(tmp_path): build_dir = tmp_path / "build" project = build_dir / "readoor.xcodeproj" swift = build_dir / "readoor" / "3.0" / "AppConfig" resources = build_dir / "readoor" / "3.0" / "Resources" assets = build_dir / "readoor" / "BookShelf" / "Resources" / "Images.xcassets" logo = resources / "3.0.xcassets" / "AppLogo.imageset" for path in [project, swift, resources, assets, logo]: path.mkdir(parents=True, exist_ok=True) (project / "project.pbxproj").write_text( "MARKETING_VERSION = 1.0;\nCURRENT_PROJECT_VERSION = 1;\n" "PRODUCT_BUNDLE_IDENTIFIER = old.id;\nDEVELOPMENT_TEAM = OLD;\n" '"DEVELOPMENT_TEAM[sdk=iphoneos*]" = OLD;\n' "PROVISIONING_PROFILE_SPECIFIER = old;\n" '"PROVISIONING_PROFILE_SPECIFIER[sdk=iphoneos*]" = old;\n' 'CODE_SIGN_IDENTITY = "iPhone Developer";\n' '"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";\n', encoding="utf-8", ) (swift / "RDAppConfiguration.swift").write_text( 'let RD_APP_GUID: String = "old"\nlet RD_API_DOMAIN: String = "old"\n' 'let RD_WECHAT_PAY_ID: String = "old"\nlet RD_SOURCE_VERSION: String = "old"\n', encoding="utf-8", ) (build_dir / "readoor" / "3.0" / "readoor31.entitlements").write_text( "applinks:old\n", encoding="utf-8" ) with (build_dir / "readoor" / "3.0" / "readoor31.plist").open("wb") as f: plistlib.dump({"CFBundleURLTypes": [ {"CFBundleURLName": "wxwork", "CFBundleURLSchemes": ["1"]}, ]}, f) theme = tmp_path / "theme" / "AppIcon.appiconset" theme.mkdir(parents=True) (theme / "icon-1024.png").write_bytes(b"icon") config = { "VERSION": "2.0.0", "BUILD_VERSION": "2.0.0.1", "APPID": "guid", "API": "https://api.example.com", "weixinpay": "wx-pay", "APPID_NAME": "Test", "wxworkSchema": "wx1234567890abcdef", "BUNDLE_ID": "com.example.test", "TEAM_ID": "TEAM", "PROVISIONING_NAME": "profile", "ASSOCIATED_DOMAINS": "applinks:example.com", "THEME": str(theme.parent), "BUILD_TYPE": "Ad_Hoc", "CERTIFICATE": "Apple Distribution", } messages = apply_project_config(build_dir, config) assert "工程、签名、版本和分发配置已更新" in messages assert 'let RD_APP_GUID: String = "guid"' in (swift / "RDAppConfiguration.swift").read_text() project_content = (project / "project.pbxproj").read_text() assert "PRODUCT_BUNDLE_IDENTIFIER = com.example.test;" in project_content assert 'CODE_SIGN_IDENTITY = "iPhone Distribution";' in project_content assert '"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Distribution";' in project_content assert "iPhone Developer" not in project_content assert (build_dir / "exportOptions.plist").exists() assert (logo / "icon-1024.png").read_bytes() == b"icon" with (build_dir / "readoor" / "3.0" / "readoor31.plist").open("rb") as f: url_types = plistlib.load(f)["CFBundleURLTypes"] wxwork = next(i for i in url_types if i["CFBundleURLName"] == "wxwork") assert wxwork["CFBundleURLSchemes"] == ["wx1234567890abcdef"] def _make_minimal_project(build_dir): """构造 apply_project_config 所需的最小工程骨架,返回关键文件路径。""" project = build_dir / "readoor.xcodeproj" swift = build_dir / "readoor" / "3.0" / "AppConfig" entitlements_dir = build_dir / "readoor" / "3.0" for path in [project, swift, entitlements_dir]: path.mkdir(parents=True, exist_ok=True) (project / "project.pbxproj").write_text( "MARKETING_VERSION = 1.0;\nCURRENT_PROJECT_VERSION = 1;\n" 'CODE_SIGN_IDENTITY = "iPhone Developer";\n' '"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";\n', encoding="utf-8", ) (swift / "RDAppConfiguration.swift").write_text( 'let RD_APP_GUID: String = "old"\nlet RD_API_DOMAIN: String = "old"\n' 'let RD_WECHAT_PAY_ID: String = "old"\nlet RD_SOURCE_VERSION: String = "old"\n', encoding="utf-8", ) (entitlements_dir / "readoor31.entitlements").write_text( "applinks:old\n", encoding="utf-8" ) return entitlements_dir / "readoor31.plist" def test_wxwork_schema_removed_when_not_configured(tmp_path): build_dir = tmp_path / "build" plist_path = _make_minimal_project(build_dir) with plist_path.open("wb") as f: plistlib.dump({"CFBundleURLTypes": [ {"CFBundleURLName": "wxwork", "CFBundleURLSchemes": ["1"]}, ]}, f) config = { "VERSION": "2.0.0", "BUILD_VERSION": "2.0.0.1", "APPID": "guid", "API": "https://api.example.com", "APPID_NAME": "Test", "ASSOCIATED_DOMAINS": "applinks:example.com", "BUILD_TYPE": "Ad_Hoc", "CERTIFICATE": "Apple Distribution", } apply_project_config(build_dir, config) with plist_path.open("rb") as f: url_types = plistlib.load(f)["CFBundleURLTypes"] assert all(i.get("CFBundleURLName") != "wxwork" for i in url_types) # ---- 英汉大词典(readoorDict)---- _DICT_MACRO = """#define SOURCE_VERSION @"1.0.0" #if TARGETTYPE == 1 || TARGETTYPE == 3 #define APPID @"other" #define SERVERURL @"http://other/" #elif TARGETTYPE == 2 //#define SERVERURL @"http://commented/" #define APPID @"old" //旧环境 #define SERVERURL @"http://old/" #define SERVERHTTPSURL @"https://old/" #define UCSERVERURL @"https://uc-old/" #define RECORDSERVERURL @"http://record-old/" #define LOGSERVERURL @"http://log-old/" #endif """ _DICT_SERVER_VARS = { "DICT_SERVERURL": "http://api-stage3/", "DICT_SERVERHTTPSURL": "https://api-stage3/", "DICT_UCSERVERURL": "https://uc-dev/", "DICT_RECORDSERVERURL": "http://record-dev/", "DICT_LOGSERVERURL": "http://log-dev/", } def _dict_build(tmp_path): macro = tmp_path / "readoor" / "BookShelf" / "Classes" / "Macro" / "STSConstantsMacro.h" macro.parent.mkdir(parents=True) macro.write_text(_DICT_MACRO, encoding="utf-8") project = tmp_path / "readoor.xcodeproj" project.mkdir() (project / "project.pbxproj").write_text( "\t\t\t\tMARKETING_VERSION = 2.192.3;\n\t\t\t\tCURRENT_PROJECT_VERSION = 2.192.3;\n", encoding="utf-8" ) return macro def _dict_config(**extra): return {"SCHEME": "readoorDict", "VERSION": "2.0.0", "BUILD_VERSION": "2.0.0.3", "SERVER": "词典测试", "APPID": "1593413622", "NO_CERT": True, "BUILD_TYPE": "Ad_Hoc", **extra} def test_dict_env_macros_replaced_only_in_targettype_2(tmp_path): macro = _dict_build(tmp_path) apply_project_config(tmp_path, _dict_config(SERVER_VARS=_DICT_SERVER_VARS), {"patcher": "readoor"}) content = macro.read_text() assert '#define SOURCE_VERSION @"2.0.0"' in content pbxproj = (tmp_path / "readoor.xcodeproj" / "project.pbxproj").read_text() assert "MARKETING_VERSION = 2.0.0;" in pbxproj assert "CURRENT_PROJECT_VERSION = 2.0.0.3;" in pbxproj assert "2.192.3" not in pbxproj assert '#define APPID @"1593413622"' in content assert '#define SERVERURL @"http://api-stage3/"' in content assert '#define LOGSERVERURL @"http://log-dev/"' in content # 其它 TARGETTYPE 与注释行不变 assert '#define APPID @"other"' in content assert '#define SERVERURL @"http://other/"' in content assert '//#define SERVERURL @"http://commented/"' in content assert "http://old/" not in content def test_dict_env_not_configured_keeps_source(tmp_path): macro = _dict_build(tmp_path) messages = apply_project_config(tmp_path, _dict_config(SERVER_VARS={}), {"patcher": "readoor"}) content = macro.read_text() assert '#define SERVERURL @"http://old/"' in content assert '#define APPID @"old"' in content assert any("沿用源码" in m for m in messages) def test_dict_env_partially_configured_fails(tmp_path): _dict_build(tmp_path) with pytest.raises(ProjectPatchError, match="DICT_LOGSERVERURL"): apply_project_config( tmp_path, _dict_config(SERVER_VARS={**_DICT_SERVER_VARS, "DICT_LOGSERVERURL": ""}), {"patcher": "readoor"}, ) def test_readoor_definition_has_dict_server_fields(): from backend.projects import LEGACY_PROJECT_ID, get_definition keys = [f["key"] for f in get_definition(LEGACY_PROJECT_ID, {"server_fields": []})["server_fields"]] assert keys == ["DICT_SERVERURL", "DICT_SERVERHTTPSURL", "DICT_UCSERVERURL", "DICT_RECORDSERVERURL", "DICT_LOGSERVERURL"] assert get_definition("other", {"patcher": "generic"})["server_fields"] == []