Files
douyi_guaji_main/tests/test_realtime_viewer.py
T
CodexandClaude Opus 4.7 fe97e82104 fix(gui): 实时查看器/状态 Tab 的耗时解析支持 ms 单位
问题:实际 actions.log 写的是 `耗时=10.00ms`(毫秒),但 _parse_minimal
和 parse_action_duration 的正则只匹配 `s`,导致:
- 实时查看弹窗永远显示"暂无动作"
- 实时状态 Tab 同样空白

同时存在的副 bug:兜底正则在整行匹配,前缀里的 `192.168.5.2:5555` /
日志时间戳 `2026-05-21` 都可能被误识别成耗时。

修复:
- 解析窗口限定到 line.find("耗时") 之后的子串 (tail)
- 优先匹配 ms(顺序很重要:\s*s 会先吃掉 "10ms" 的 m)
- s 单位加 (?![a-zA-Z]) 负向后顾,避免匹配 "ms" / "second" 之类
- 同步修复 gui_app.py:3685 的 parse_action_duration(实时状态 Tab)

测试:
- _parse_minimal 加 4 个新测试覆盖 ms / 整数 ms / 优先级 / 前缀干扰
- 全量 1426 passed (+5)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-23 09:08:58 +08:00

126 lines
4.5 KiB
Python

"""gui/components/realtime_viewer 单元测试。
只测纯函数 _tail_read / _parse_minimal。
open_realtime_viewer 涉及 Tk Toplevel + 后台计时器,单测难度高,跳过。
"""
from __future__ import annotations
import pytest
from gui.components.realtime_viewer import _parse_minimal, _tail_read
# ============================================================
# _tail_read
# ============================================================
class TestTailRead:
def test_nonexistent_file_returns_empty(self, tmp_path):
assert _tail_read(str(tmp_path / "nope.log")) == []
def test_small_file_returns_all_lines(self, tmp_path):
p = tmp_path / "test.log"
p.write_text("line1\nline2\nline3\n", encoding="utf-8")
result = _tail_read(str(p))
assert result == ["line1", "line2", "line3"]
def test_large_file_truncated_to_max_bytes(self, tmp_path):
p = tmp_path / "test.log"
# 写 100 行
text = "\n".join(f"line{i:04d}" for i in range(1000))
p.write_text(text, encoding="utf-8")
# max_bytes=200 → 只读末尾 200 字节
result = _tail_read(str(p), max_bytes=200)
# 末尾几行应该都在
assert any("line0999" in line for line in result)
# 开头不应在
assert not any("line0000" in line for line in result)
def test_invalid_encoding_does_not_crash(self, tmp_path):
p = tmp_path / "binary.log"
p.write_bytes(b"\xff\xfe valid text \n line2\n\xff\xfe")
# decode(errors="ignore") 应该跳过无效字节
result = _tail_read(str(p))
assert isinstance(result, list)
def test_empty_file(self, tmp_path):
p = tmp_path / "empty.log"
p.write_text("", encoding="utf-8")
assert _tail_read(str(p)) == []
# ============================================================
# _parse_minimal
# ============================================================
class TestParseMinimal:
def test_no_耗时_returns_none(self):
assert _parse_minimal("普通日志行 没有 timing 信息") is None
def test_耗时_with_colon(self):
result = _parse_minimal("点击参与按钮 耗时:1.5s")
assert result == ("点击参与按钮", "1.5s")
def test_耗时_with_equals(self):
result = _parse_minimal("OCR识别 耗时=2.3s")
assert result == ("OCR识别", "2.3s")
def test_耗时_no_separator(self):
# "耗时 1.5s" → 应回落到 "([0-9]+s)" pattern
result = _parse_minimal("操作完成 耗时 1.5s")
# action 取 idx="耗时" 之前的文本 = "操作完成 "
assert result is not None
assert result[0] == "操作完成"
assert result[1] == "1.5s"
def test_耗时_present_but_no_seconds_returns_none(self):
# "耗时未知" 没有数字 → None
assert _parse_minimal("某操作 耗时未知") is None
def test_action_truncated_to_80_chars(self):
long_action = "a" * 100
result = _parse_minimal(f"{long_action} 耗时:1.5s")
assert result is not None
action, dur = result
assert len(action) <= 80
assert action.endswith("...")
assert dur == "1.5s"
def test_empty_action_uses_line_start(self):
result = _parse_minimal("耗时:0.5s")
assert result is not None
action, dur = result
# action 为空时回退到 line.strip()[:80]
assert action == "耗时:0.5s"
def test_integer_duration(self):
result = _parse_minimal("步骤A 耗时:5s")
assert result == ("步骤A", "5s")
def test_ms_unit(self):
"""实际 actions.log 写的是 `耗时=10.00ms`,必须支持 ms 单位。"""
result = _parse_minimal(
"2026-05-21 14:59:00,513 - [ACTION] test step 耗时=10.00ms"
)
assert result is not None
action, dur = result
assert dur == "10.00ms"
assert "test step" in action
def test_ms_integer(self):
result = _parse_minimal("OCR识别 耗时=100ms")
assert result == ("OCR识别", "100ms")
def test_ms_takes_priority_over_s(self):
"""`10ms` 不能被错误识别为 `10` + `s` 的前半部分。"""
result = _parse_minimal("click 耗时=10ms")
assert result == ("click", "10ms")
def test_number_before_耗时_not_picked_up(self):
"""前缀里的 `5555` / `192.168...` 不能被当成耗时。"""
result = _parse_minimal(
"192.168.5.2:5555 [ACTION] click 耗时=10ms"
)
assert result is not None
assert result[1] == "10ms"