我问了一句, 得到的答复是"我用过(升阶之类)"。所以我前面那一轮追查的方向一开始
就该先问人: 这类"数字对不上"的现象, 默认解释可能就是人手动操作, 而不是故障。
改动:
* module_trajectory 的措辞从"倒退"改成"比上一轮少 —— 若是人工升阶用掉的属正常;
否则要查", 并补一条反向检查: 模组数**自己变多**才是真反常(自动化之外的来源)
* audit_runs 的输出从"发现 N 处问题"改成"有 N 处需要确认" —— 有些只是需要确认
* docs/WATCH.md 的失败模式表改成先问人再查代码
保留的: close_game 的优雅关闭。那是独立的健壮性改进(强制结束本来就有丢未保存
状态的风险), 和这次这件事无关, 不因为结论变了就回退。
全套 352/352 通过。
214 lines
9.8 KiB
Python
214 lines
9.8 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""audit_runs 的核对逻辑。
|
|
|
|
这个工具的职责是"把日志和报告对起来", 所以测试重点不是它能通过, 而是
|
|
**它能不能抓到问题** —— 一个什么都不报的体检工具等于没有。
|
|
|
|
⛔ 第一版就踩过这个坑: 它拿"报告.started + elapsed"去找日志结论行, 能对上就说明
|
|
elapsed 一致, 于是"耗时不符"那个分支永远不可能触发 —— 是死代码。现在改成按日志里
|
|
的"=== 开始: 体力换模块 ===" 配对, 时间关系和耗时才能各自独立核对。
|
|
"""
|
|
import json
|
|
import os
|
|
import shutil
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
from datetime import datetime
|
|
from pathlib import Path
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'scripts'))
|
|
import audit_runs as ar # noqa: E402
|
|
|
|
START = '=== 开始: 体力换模块 ==='
|
|
|
|
|
|
def start_line(t):
|
|
return '[%s] %s' % (t, START)
|
|
|
|
|
|
def conclusion(t, result, secs, ok='OK'):
|
|
return '[%s] === %s (%s), 耗时 %ds ===' % (t, result, ok, secs)
|
|
|
|
|
|
def report(start, secs, result, ok=True):
|
|
return json.dumps({'started': start, 'elapsed_s': secs, 'ok': ok,
|
|
'result': result, 'steps': []}, ensure_ascii=False)
|
|
|
|
|
|
class AuditTest(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
self.d = tempfile.mkdtemp()
|
|
|
|
def write_log(self, lines):
|
|
with open(os.path.join(self.d, 'convert.log'), 'w', encoding='utf-8') as f:
|
|
f.write('\n'.join(lines) + '\n')
|
|
|
|
def write_report(self, name, content):
|
|
with open(os.path.join(self.d, name), 'w', encoding='utf-8') as f:
|
|
f.write(content)
|
|
|
|
def problems(self):
|
|
return ar.audit(self.d)['problems']
|
|
|
|
# ---------------------------------------------------------- 正常情况
|
|
|
|
def test_matching_run_is_clean(self):
|
|
self.write_log([start_line('2026-01-01 10:00:00'),
|
|
conclusion('2026-01-01 10:00:30', '合成完成: 模组 1 -> 2 (+1)', 30)])
|
|
self.write_report('convert_20260101_100000.json',
|
|
report('2026-01-01 10:00:00', 30, '合成完成: 模组 1 -> 2 (+1)'))
|
|
self.assertEqual(self.problems(), [])
|
|
|
|
def test_empty_dir_is_clean(self):
|
|
self.assertEqual(self.problems(), [])
|
|
|
|
# ---------------------------------------------------------- 抓问题
|
|
|
|
def test_report_without_run_is_flagged(self):
|
|
self.write_report('convert_20260101_110000.json',
|
|
report('2026-01-01 11:00:00', 40, '体力不足(3<20), 本轮无需合成'))
|
|
self.assertTrue(any('找不到对应的开始行' in p for p in self.problems()))
|
|
|
|
def test_run_without_report_is_flagged(self):
|
|
self.write_log([start_line('2026-01-01 12:00:00'),
|
|
conclusion('2026-01-01 12:00:10', '跑了但没留报告', 10)])
|
|
self.assertTrue(any('没有对应报告' in p for p in self.problems()))
|
|
|
|
def test_elapsed_mismatch_is_flagged(self):
|
|
"""日志说耗时 30s, 报告说 99s —— 必须能发现。
|
|
|
|
老版本这里永远查不出来(死代码), 所以这条用例是专门守它的。
|
|
"""
|
|
self.write_log([start_line('2026-01-01 10:00:00'),
|
|
conclusion('2026-01-01 10:00:30', '合成完成', 30)])
|
|
self.write_report('convert_20260101_100000.json',
|
|
report('2026-01-01 10:00:00', 99, '合成完成'))
|
|
got = self.problems()
|
|
self.assertTrue(any('耗时不符' in p for p in got), got)
|
|
|
|
def test_end_time_mismatch_is_flagged(self):
|
|
# 开始和耗时都对得上, 但结论时刻对不上 -> 时间关系有问题
|
|
self.write_log([start_line('2026-01-01 10:00:00'),
|
|
conclusion('2026-01-01 10:05:00', '合成完成', 30)])
|
|
self.write_report('convert_20260101_100000.json',
|
|
report('2026-01-01 10:00:00', 30, '合成完成'))
|
|
self.assertTrue(any('时间对不上' in p for p in self.problems()))
|
|
|
|
def test_result_text_mismatch_is_flagged(self):
|
|
self.write_log([start_line('2026-01-01 10:00:00'),
|
|
conclusion('2026-01-01 10:00:30', '合成完成: 模组 1 -> 2 (+1)', 30)])
|
|
self.write_report('convert_20260101_100000.json',
|
|
report('2026-01-01 10:00:00', 30, '合成完成: 模组 5 -> 9 (+4)'))
|
|
self.assertTrue(any('结果文本不符' in p for p in self.problems()))
|
|
|
|
def test_same_second_cluster_is_flagged(self):
|
|
"""同一秒多行结论 = 测试批量产生的典型特征。"""
|
|
self.write_log([conclusion('2026-01-01 12:00:00', '第一条', 0),
|
|
conclusion('2026-01-01 12:00:00', '第二条', 0)])
|
|
self.assertTrue(any('同一秒' in p for p in self.problems()))
|
|
|
|
def test_impossible_content_is_flagged(self):
|
|
self.write_log([start_line('2026-01-01 12:00:00'),
|
|
conclusion('2026-01-01 12:00:05', '看门狗执行失败: powershell not found', 5)])
|
|
self.assertTrue(any('可疑内容' in p for p in self.problems()))
|
|
|
|
# ---------------------------------------------------------- 解析
|
|
|
|
def test_parse_pairs_start_with_conclusion(self):
|
|
self.write_log([
|
|
'[2026-01-01 09:00:00] === 开始: 体力换模块 ===',
|
|
'[2026-01-01 09:00:05] 游戏已在运行',
|
|
conclusion('2026-01-01 09:01:00', '合成完成', 60),
|
|
])
|
|
runs = ar.parse_runs(os.path.join(self.d, 'convert.log'))
|
|
self.assertEqual(len(runs), 1)
|
|
self.assertEqual(runs[0]['secs'], 60)
|
|
self.assertEqual(runs[0]['start'].strftime('%H:%M:%S'), '09:00:00')
|
|
|
|
def test_parse_handles_missing_file(self):
|
|
self.assertEqual(ar.parse_runs(os.path.join(self.d, 'nope.log')), [])
|
|
|
|
def test_conclusion_without_start_line_still_listed(self):
|
|
# 没有开始行的结论不该被丢掉(要能报"没有对应报告")
|
|
self.write_log([conclusion('2026-01-01 12:00:00', '独苗', 10)])
|
|
runs = ar.parse_runs(os.path.join(self.d, 'convert.log'))
|
|
self.assertEqual(len(runs), 1)
|
|
self.assertIsNone(runs[0]['start'])
|
|
|
|
def test_skip_broken_report_json(self):
|
|
with open(os.path.join(self.d, 'convert_20260101_100000.json'), 'w', encoding='utf-8') as f:
|
|
f.write('{ 半截')
|
|
self.assertEqual(ar.load_reports(self.d), [])
|
|
|
|
|
|
class ModuleTrajectoryTest(unittest.TestCase):
|
|
"""跨轮检查模组数。
|
|
|
|
⛔ 2026-10-17 实测: 22:05 合成后是 35, 06:04 那轮读到的合成前只有 33。
|
|
追查了一轮, 最后**是用户自己升阶用掉的** —— 不是 bug。
|
|
|
|
教训: 这类"数字对不上"的现象, 默认解释可能是人手动操作。所以措辞是"需要确认",
|
|
不是"出问题了"。真正反常的是反过来: 模组数**自己变多**(自动化之外的来源)。
|
|
"""
|
|
|
|
def _rep(self, started, info, converted=True):
|
|
return {'start': datetime.strptime(started, '%Y-%m-%d %H:%M:%S'),
|
|
'steps': [{'step': '合成', 'ok': True, 'info': info}],
|
|
'converted': converted}
|
|
|
|
def test_normal_progress_is_clean(self):
|
|
reps = [self._rep('2026-01-01 10:00:00', '模组 29 -> 30 (+1)'),
|
|
self._rep('2026-01-01 14:00:00', '模组 30 -> 31 (+1)')]
|
|
self.assertEqual(ar.module_trajectory(reps), [])
|
|
|
|
def test_decrease_is_reported_but_not_as_a_failure_claim(self):
|
|
reps = [self._rep('2026-10-16 22:03:42', '模组 33 -> 35 (+2)'),
|
|
self._rep('2026-10-17 06:03:42', '模组 33 -> 35 (+2)')]
|
|
got = ar.module_trajectory(reps)
|
|
self.assertEqual(len(got), 1)
|
|
self.assertIn('比上一轮少', got[0])
|
|
self.assertIn('人工升阶', got[0]) # 默认解释要摆出来, 别一口咬定是故障
|
|
self.assertIn('35', got[0])
|
|
self.assertIn('33', got[0])
|
|
|
|
def test_unexplained_increase_is_flagged(self):
|
|
"""模组自己变多才是真反常 —— 自动化之外的来源。"""
|
|
reps = [self._rep('2026-01-01 10:00:00', '模组 29 -> 30 (+1)'),
|
|
self._rep('2026-01-01 14:00:00', '模组 33 -> 35 (+2)')]
|
|
got = ar.module_trajectory(reps)
|
|
self.assertEqual(len(got), 1)
|
|
self.assertIn('无端变多', got[0])
|
|
|
|
def test_skipped_runs_do_not_count(self):
|
|
"""中间那些"体力不足没合成"的轮次不该被当成链条上的一环。"""
|
|
reps = [self._rep('2026-01-01 10:00:00', '模组 29 -> 30 (+1)'),
|
|
self._rep('2026-01-01 14:00:00', '', converted=False),
|
|
self._rep('2026-01-01 18:00:00', '模组 30 -> 31 (+1)')]
|
|
self.assertEqual(ar.module_trajectory(reps), [])
|
|
|
|
def test_missing_numbers_are_skipped(self):
|
|
reps = [self._rep('2026-01-01 10:00:00', '合成完成'),
|
|
self._rep('2026-01-01 14:00:00', '模组 30 -> 31 (+1)')]
|
|
self.assertEqual(ar.module_trajectory(reps), [])
|
|
|
|
def test_audit_includes_trajectory_problems(self):
|
|
"""端到端: 报告目录里的减少要被 audit() 报出来。"""
|
|
d = tempfile.mkdtemp()
|
|
self.addCleanup(shutil.rmtree, d, True)
|
|
for name, started, secs, info in (
|
|
('convert_a.json', '2026-01-01 10:00:00', 30, '模组 29 -> 31 (+2)'),
|
|
('convert_b.json', '2026-01-01 14:00:00', 30, '模组 29 -> 31 (+2)')):
|
|
with open(os.path.join(d, name), 'w', encoding='utf-8') as f:
|
|
f.write(json.dumps({'started': started, 'elapsed_s': secs, 'ok': True,
|
|
'result': '合成完成', 'steps': [
|
|
{'step': '合成', 'ok': True, 'info': info}],
|
|
'converted': True}, ensure_ascii=False))
|
|
problems = ar.audit(d)['problems']
|
|
self.assertTrue(any('比上一轮少' in p for p in problems), problems)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|