feat: readoorDict 打包按服务器环境替换词典 APPID 与服务器地址

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shenlei
2026-11-03 10:59:38 +09:00
co-authored by Claude Opus 5.5
parent a9e169b00c
commit 11f4035da7
3 changed files with 168 additions and 1 deletions
+86 -1
View File
@@ -1,7 +1,9 @@
"""服务端工程配置替换测试。"""
import plistlib
from backend.services.project_patcher import apply_project_config
import pytest
from backend.services.project_patcher import ProjectPatchError, apply_project_config
def test_apply_project_config_without_branch_autopacking(tmp_path):
@@ -111,3 +113,86 @@ def test_wxwork_schema_removed_when_not_configured(tmp_path):
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")
return macro
def _dict_config(**extra):
return {"SCHEME": "readoorDict", "VERSION": "2.0.0", "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
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"] == []