fix: 修复 GUI.html 完全不可用,并为网页客户端开启 CORS

GUI.html 里有一句硬语法错误:

    progressFill?.style.width = '100%';

可选链不能做赋值目标,整个 <script> 块解析失败——页面加载看着正常,但所有按钮
都没反应,浏览器只在控制台报一条错,极容易被误判成「服务连不上」。
改成 if (progressFill) ...,并用 node --check 加了回归测试。

网页端调接口是跨域请求,而服务端没开 CORS:/api/v1/i2t 发 JSON 会先发预检
OPTIONS,直接失败;上传虽然能发出去,但响应读不到。加了 CORSMiddleware,
来源由 cors_allow_origins 配置(默认 ["*"],[] 可关)。命令行脚本不受影响,
所以这个坑只在网页端暴露。

顺带修掉 GUI 里几处:
  · 安静模式下没有进度条,onProgress 会拿 undefined 赋值
  · 字段渲染用 innerHTML 拼值——OCR 结果来自用户上传的图片,内容不可信,改 textContent
  · 置信度/超时输入框清空时 parseFloat 得到 NaN,会让服务端返回 422,加回退值
  · 处理完重置 fileInput.value,否则再选同一个文件不触发 change
  · 页面经 http(s) 打开时,服务地址默认填 location.origin(同源,免配)

测试:新增 CORS 预检/关闭两条用例,以及 GUI.html 的 JS 语法检查(用 node,
没装 node 则跳过)。101 passed, 1 skipped。

Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
zimoyin
2026-10-14 12:31:44 +08:00
parent ec6b9c7ae3
commit 3772998137
9 changed files with 624 additions and 75 deletions
+33
View File
@@ -1,6 +1,8 @@
"""打包辅助脚本的回归测试(config 改写与 paddle 变体探测)。"""
import os
import re
import shutil
import subprocess
import sys
from pathlib import Path
@@ -14,6 +16,7 @@ INSTALL_PADDLE = PROJECT_ROOT / "scripts" / "install_paddle.py"
SWITCH_ENV = PROJECT_ROOT / "scripts" / "switch_env.py"
API_CLIENT = PROJECT_ROOT / "scripts" / "api_client.py"
API_CLIENT_BAT = PROJECT_ROOT / "scripts" / "api-client.bat"
GUI_HTML = PROJECT_ROOT / "scripts" / "GUI.html"
SAMPLE = """\
# 中文注释必须原样保留
@@ -106,6 +109,34 @@ def test_install_paddle_rejects_unknown_cuda_channel() -> None:
assert result.returncode == 2
def test_gui_html_javascript_parses(tmp_path: Path) -> None:
"""GUI.html 里的 script 必须能通过 JS 语法检查。
实测踩过:一句 `progressFill?.style.width = '100%'` 是语法错误(可选链不能做
赋值目标),整个 script 块解析失败——页面加载正常,但所有按钮都没反应,
浏览器只在控制台报一条错,很容易被误判成「服务连不上」。
"""
node = shutil.which("node")
if node is None:
pytest.skip("本机没有 node,跳过 JS 语法检查")
html = GUI_HTML.read_text(encoding="utf-8")
match = re.search(r"<script>(.*?)</script>", html, re.S)
assert match, "GUI.html 里没找到 <script> 块"
script_path = tmp_path / "gui.js"
script_path.write_text(match.group(1), encoding="utf-8")
result = subprocess.run(
[node, "--check", str(script_path)],
capture_output=True,
text=True,
encoding="utf-8",
timeout=60,
check=False,
)
assert result.returncode == 0, result.stderr
def test_windows_batch_files_use_crlf() -> None:
""".bat 必须是 CRLF 换行。
@@ -121,6 +152,8 @@ def test_windows_batch_files_use_crlf() -> None:
@pytest.mark.skipif(os.name != "nt", reason=".bat 只能在 Windows 上执行")
def test_api_client_bat_parses_and_runs() -> None:
"""bat 能被 cmd 正常解析并透传参数。带超时,坏掉时报失败而不是把测试卡死。"""
if not API_CLIENT_BAT.is_file():
pytest.skip("api-client.bat 不存在(已改用 GUI.html 时属正常)")
result = subprocess.run(
["cmd", "/c", str(API_CLIENT_BAT), "--help"],
capture_output=True,