This website requires JavaScript.
0
Issues
Pull Requests
Milestones
Explore
0
Create…
New Repository
New Migration
New Organization
public
Signed in as
public
Profile
Starred
Subscriptions
Settings
Help
Sign Out
yws
/
lc-mcp
Watch
1
Star
0
Fork
0
Code
Issues
Pull Requests
Actions
Packages
Projects
Releases
Wiki
Activity
Files
lc-mcp
/
tests
/
or
Cancel
Edit File
Preview
Preview Changes
Spaces
Tabs
2
4
8
Wrap
No wrap
# -*- coding: utf-8 -*- """决策审计日志(BACKLOG P2-8): 写入、统计、TaskRunner 归档, 以及商店/卡包两处只观察的审计。""" import ast import json import logging import os import sys import tempfile import time import types import unittest from pathlib import Path from types import SimpleNamespace from unittest import mock REPO = Path(__file__).resolve().parents[1] sys.path.insert(0, str(REPO)) from lc_mcp import aalc_patches as ap # noqa: E402 from lc_mcp.decisions import DECISIONS_ENV, SUFFIX, DecisionLog, summarize # noqa: E402 from lc_mcp.tasks import TaskRunner # noqa: E402 THEME = REPO / 'vendor' / 'aalc' / 'tasks' / 'mirror' / 'select_theme_pack.py' def read(path): with open(path, encoding='utf-8') as f: return [json.loads(line) for line in f] class DecisionLogTest(unittest.TestCase): def setUp(self): tmp = tempfile.TemporaryDirectory() self.addCleanup(tmp.cleanup) self.path = os.path.join(tmp.name, 'run' + SUFFIX) def test_without_env_nothing_is_written(self): log = DecisionLog() self.assertIsNone(log.open(env={})) log.record('gift', 1, 'keyword') self.assertFalse(os.path.exists(self.path)) def test_records_and_counts(self): log = DecisionLog() self.assertEqual(log.open(env={DECISIONS_ENV: self.path}), self.path) log.record('gift', 2, 'keyword', system='bleed', hits=[[], ['流血']]) log.record('gift', 1, 'no_keyword', system='bleed', hits=[[], []]) log.record('gift', 1, 'keyword', system='bleed', hits=[['出血'], []]) log.record('shop', 'carmilla', 'purchase_clicked') entries = read(self.path) self.assertEqual(entries[0]['hits'], [[], ['流血']]) self.assertEqual(set(entries[0]), {'time', 'stage', 'choice', 'reason', 'system', 'hits'}) self.assertEqual(summarize(self.path), {'gift': {'keyword': 2, 'no_keyword': 1}, 'shop': {'purchase_clicked': 1}}) def test_bad_lines_are_skipped_and_missing_file_is_none(self): with open(self.path, 'w', encoding='utf-8') as f: f.write('{"stage": "road", "reason": "lowest"}\nnot json\n{"stage": "road"}\n[]\n') self.assertEqual(summarize(self.path), {'road': {'lowest': 1}}) self.assertIsNone(summarize(self.path + '.missing')) def test_write_failure_does_not_raise(self): DecisionLog(os.path.dirname(self.path)).record('gift', 1, 'keyword') # 路径是目录 class TaskRunnerDecisionsTest(unittest.TestCase): CHILD = ('import os\n' 'from lc_mcp.decisions import decisions\n' 'decisions.open()\n' 'decisions.record("theme_pack", "信仰", "preferred")\n' 'decisions.record("gift", 2, "keyword")\n' 'decisions.record("gift", 1, "no_keyword")\n' 'print("[lc_mcp] done")\n') def wait(self, runner): deadline = time.time() + 15 while time.time() < deadline and runner.running(): time.sleep(0.05) def test_child_writes_next_to_its_log_and_status_counts_it(self): with tempfile.TemporaryDirectory() as d: runner = TaskRunner(d, argv_factory=lambda t, p: [sys.executable, '-c', self.CHILD]) started = runner.start('mirror', {'count': 1}) self.assertTrue(started['ok'], started) self.wait(runner) status = runner.status() self.assertEqual(status['state'], 'succeeded', status) self.assertEqual(status['decisions']['path'], started['log'][:-len('.log')] + SUFFIX) self.assertEqual(status['decisions']['counts'], {'gift': {'keyword': 1, 'no_keyword': 1}, 'theme_pack': {'preferred': 1}}) def test_no_decisions_field_when_nothing_was_recorded(self): with tempfile.TemporaryDirectory() as d: runner = TaskRunner(d, argv_factory=lambda t, p: [sys.executable, '-c', 'print(1)']) runner.start('daily', {}) self.wait(runner) self.assertNotIn('decisions', runner.status()) def test_prune_removes_decisions_with_their_log(self): with tempfile.TemporaryDirectory() as d: runner = TaskRunner(d, argv_factory=lambda t, p: [sys.executable, '-c', 'print(1)'], keep_logs=1) old = os.path.join(d, 'task_20260101_000000_mirror') for path in (old + '.log', old + SUFFIX): with open(path, 'w') as f: f.write('{}\n') os.utime(old + '.log', (1, 1)) runner.start('daily', {}) self.wait(runner) self.assertFalse(os.path.exists(old + '.log')) self.assertFalse(os.path.exists(old + SUFFIX)) class RecordingTest(unittest.TestCase): """把模块级 decisions 指到临时文件。""" def setUp(self): tmp = tempfile.TemporaryDirectory() self.addCleanup(tmp.cleanup) self.path = os.path.join(tmp.name, 'run' + SUFFIX) patcher = mock.patch.object(ap.decisions, 'path', self.path) patcher.start() self.addCleanup(patcher.stop) self.lines = [] def entries(self): return read(self.path) if os.path.exists(self.path) else [] class ShopAuditTest(RecordingTest): def setUp(self): super().setUp() self.auto = SimpleNamespace(click_element=mock.Mock(return_value=True), mouse_action_with_pos=mock.Mock()) self.shop_cls = type('Shop', (), {'system': 'bleed'}) in_shop = types.ModuleType('tasks.mirror.in_shop') in_shop.Shop = self.shop_cls modules = {'tasks': types.ModuleType('tasks'), 'tasks.mirror': types.ModuleType('tasks.mirror'), 'tasks.mirror.in_shop': in_shop, 'module': types.ModuleType('module'), 'module.automation': SimpleNamespace(auto=self.auto)} patcher = mock.patch.dict(sys.modules, modules) patcher.start() self.addCleanup(patcher.stop) def test_purchases_are_recorded_with_the_item_clicked_before(self): auto = self.auto def buy_gifts(shop): # 上游的三条购买路径: 必买项、四级素材、本体系饰品(按坐标) auto.click_element('mirror/shop/must_purchase/carmilla.png', threshold=0.85) auto.click_element(ap.SHOP_PURCHASE) auto.click_element(ap.SHOP_CONFIRM) auto.click_element('mirror/shop/level_IV_to_buy.png', threshold=0.82) auto.click_element(ap.SHOP_PURCHASE) auto.mouse_action_with_pos((300, 400), offset=True) auto.click_element(ap.SHOP_PURCHASE, take_screenshot=True) return 'done' self.shop_cls.buy_gifts = buy_gifts click, position = auto.click_element, auto.mouse_action_with_pos ap.install_shop_audit(log=self.lines.append) self.assertEqual(self.shop_cls().buy_gifts(), 'done') self.assertEqual([(e['choice'], e['reason'], e['system']) for e in self.entries()], [('carmilla', 'purchase_clicked', 'bleed'), ('level_IV_to_buy', 'purchase_clicked', 'bleed'), ('by_position', 'purchase_clicked', 'bleed')]) self.assertIs(auto.click_element, click) self.assertIs(auto.mouse_action_with_pos, position) position.assert_called_once_with((300, 400), offset=True) def test_failed_purchase_click_is_not_recorded(self): auto, real = self.auto, self.auto.click_element def buy_gifts(shop): auto.click_element('mirror/shop/must_purchase/carmilla.png') real.return_value = False # 钱不够: 购买按钮匹配不上 auto.click_element(ap.SHOP_PURCHASE) self.shop_cls.buy_gifts = buy_gifts ap.install_shop_audit(log=self.lines.append) self.shop_cls().buy_gifts() self.assertEqual(self.entries(), []) class ThemePackDecisionTest(unittest.TestCase): def test_messages_to_decision(self): weights = "当前主题包权重列表:[('信仰', 0), ('unknown', -5)]" self.assertEqual(ap.theme_pack_decision([weights, '此次选择卡包关键词:信仰']), ('信仰', 'preferred', "[('信仰', 0), ('unknown', -5)]")) self.assertEqual(ap.theme_pack_decision(['无匹配最低阈值的主题包,选择最高权重主题包\n此次选择卡包关键词:赌徒'])[:2], ('赌徒', 'highest_weight')) self.assertEqual(ap.theme_pack_decision(['此次主题包选择了最左边的(活动)卡包'])[:2], ('最左边的活动卡包', 'event_pack')) self.assertIsNone(ap.theme_pack_decision(['选择卡包: (1, 2)'])) @unittest.skipUnless(THEME.is_file(), 'vendor/aalc 子模块未检出') class UpstreamThemePackAuditTest(RecordingTest): """执行上游真实的 select_theme_pack, 审计从它自己的日志里取结果。""" class Match: def __init__(self, value, text): self.value, self.text, self.position = value, text, [0, 0] def run_upstream(self, packs, thresholds=0): tree = ast.parse(THEME.read_text(encoding='utf-8')) node = next(n for n in tree.body if isinstance(n, ast.FunctionDef) and n.name == 'select_theme_pack') node.decorator_list = [] positions = [(300 + 300 * i, 300) for i in range(len(packs))] weights = {name: weight for name, weight in packs if name} def find_language_text(zh, en, crop): x = (crop[0] + crop[2]) / 2 name = packs[min(range(len(packs)), key=lambda i: abs(positions[i][0] - x))][0] return self.Match(weights[name], name) if name else False auto = SimpleNamespace( model=None, take_screenshot=lambda: object(), mouse_to_blank=lambda *a, **k: None, find_element=lambda target, **kw: list(positions) if 'theme_pack_features' in target else False, find_language_text=find_language_text, mouse_drag_down=mock.Mock(), click_element=lambda *a, **k: False) ns = dict(auto=auto, TextMatchResult=self.Match, sleep=lambda *a: None, back_init_menu=mock.Mock(), log=logging.getLogger('AALC'), cfg=SimpleNamespace(set_win_size=720, select_event_pack=False, skip_event_pack=False), path_manager=SimpleNamespace(current_language='zh_cn'), theme_list=SimpleNamespace(preferred_thresholds=thresholds, get_effective_theme_pack_list=lambda *a: dict(weights))) exec(compile(ast.Module(body=[node], type_ignores=[]), str(THEME), 'exec'), ns) mirror_mod = types.ModuleType('tasks.mirror.mirror') mirror_mod.select_theme_pack = ns['select_theme_pack'] package = types.ModuleType('tasks.mirror') package.mirror = mirror_mod logger = logging.getLogger('AALC') with mock.patch.dict(sys.modules, {'tasks': types.ModuleType('tasks'), 'tasks.mirror': package, 'tasks.mirror.mirror': mirror_mod}), \ mock.patch.object(logger, 'level', logging.DEBUG), \ mock.patch.object(logger, 'propagate', False): ap.install_theme_pack_audit(log=self.lines.append) handlers = list(logger.handlers) mirror_mod.select_theme_pack(False, 2, 1, False) self.assertEqual(logger.handlers, handlers, '临时 handler 必须移除') return auto.mouse_drag_down def test_preferred_pack_is_recorded(self): drag = self.run_upstream([('赌徒', -1), ('信仰', 2), (None, None)]) drag.assert_called_once_with(600, 300) entry, = self.entries() self.assertEqual((entry['stage'], entry['choice'], entry['reason'], entry['floor']), ('theme_pack', '信仰', 'preferred', 2)) self.assertIn("('信仰', 2)", entry['weights']) if __name__ == '__main__': unittest.main()
Commit Changes
Add a Signed-off-by trailer by the committer at the end of the commit log message.
Commit directly to the
codex/feedback-unattended-setup-20261004
branch.
Create a
new branch
for this commit and start a pull request.
Commit Changes
Cancel