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 R-01 / ISSUES I-08)。 theme_pack_list.yaml 几乎全是 0, 所有卡包并列最高分, 每层拿最左边那个。补丁在 AALC 选卡包时把 「卡包对当局体系的加分」(lc_mcp/data/theme_pack_systems.json, 数据来自 wiki.gg)临时加到权重表上。 端到端用例执行上游真实的 select_theme_pack, 卡面按 AALC 的规则(按表顺序取第一个子串命中的键)认。 """ import ast import json import logging import os import re import sys import tempfile 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 THEME = REPO / 'vendor' / 'aalc' / 'tasks' / 'mirror' / 'select_theme_pack.py' EXAMPLE = REPO / 'vendor' / 'aalc' / 'assets' / 'config' / 'theme_pack_list.example.yaml' BONUS = {'featured': 6, 'unique': (0, 1, 4, 6), 'recommend': 6, 'avoid': 3} BUILD = REPO / 'scripts' / 'build_theme_pack_systems.py' def example_weights(*sections): """读 AALC 自带的 theme_pack_list.example.yaml(只认 `键: 整数` 这种行)。""" found, current = {}, None for line in EXAMPLE.read_text(encoding='utf-8').splitlines(): header = re.match(r'^(\w+):\s*$', line) if header: current = header.group(1) continue item = re.match(r'^\s+(\S.*?):\s*(-?\d+)', line) if item and current in sections: found[item.group(1)] = int(item.group(2)) return found def pack(zh, en='x', featured=(), recommend=(), avoid=(), normal='1F', hard='1F', **unique): return {'zh': zh, 'en': en, 'featured': list(featured), 'unique': unique, 'recommend': set(recommend), 'avoid': set(avoid), 'normal': normal, 'hard': hard} class ThemePackTableTest(unittest.TestCase): def setUp(self): self.bonus, self.packs = ap.load_theme_pack_systems() self.by_name = {p['zh']: p for p in self.packs} def test_table_covers_the_current_theme_packs(self): self.assertEqual(len(self.packs), len({p['id'] for p in self.packs})) self.assertGreaterEqual(len(self.packs), 120) self.assertEqual(self.bonus, BONUS) def test_keyword_packs_and_story_packs(self): # wiki.gg: Featured Gifts 用体系分类的就是体系包; 专属饰品按体系计数 self.assertEqual(self.by_name['电闪雷鸣']['featured'], ['charge']) self.assertEqual(self.by_name['动力渐盈']['featured'], ['charge']) self.assertEqual(self.by_name['尸山血海']['featured'], ['bleed']) self.assertEqual(self.by_name['LCB定期体检']['featured'], ['burn', 'rupture']) self.assertEqual(self.by_name['当斩之物']['featured'], ['slash']) self.assertEqual(self.by_name['WARP快车谋杀案']['unique']['charge'], 5) self.assertEqual(self.by_name['时间杀人时间']['unique']['tremor'], 5) self.assertEqual(self.by_name['被遗忘者们']['unique'], {}) def test_community_entries_are_attached(self): self.assertEqual(self.by_name['憎恶与绝望']['recommend'], {'burn', 'tremor', 'sinking', 'charge'}) self.assertEqual(self.by_name['被遗忘者们']['avoid'], {'*'}) self.assertEqual(self.by_name['琥珀色的黄昏']['avoid'], {'bleed'}) self.assertEqual(self.by_name['琥珀色的黄昏']['recommend'], {'sinking'}) def test_community_tiers(self): b = self.bonus self.assertEqual(ap.theme_pack_bonus(self.by_name['憎恶与绝望'], 'charge', b), 6) # 专属 2 件 4, 推荐 6 self.assertEqual(ap.theme_pack_bonus(self.by_name['地狱鸡'], 'poise', b), 6) self.assertEqual(ap.theme_pack_bonus(self.by_name['琥珀色的黄昏'], 'bleed', b), 0) # 专属 3 件, 但攻略说不划算 self.assertEqual(ap.theme_pack_bonus(self.by_name['被遗忘者们'], 'charge', b), -3) self.assertEqual(ap.theme_pack_bonus(self.by_name['当碎之物'], 'blunt', b), 3) # 体系包 6, 拖时间 -3 def test_route_bonus_ignores_system_and_avoid(self): b = self.bonus self.assertEqual(ap.theme_pack_bonus(dict(self.by_name['被遗忘者们'], route=5), None, b), 5) self.assertEqual(ap.theme_pack_bonus(dict(self.by_name['憎恶与绝望'], route=2), 'charge', b), 6) # 取大的 self.assertEqual(ap.theme_pack_bonus(self.by_name['憎恶与绝望'], None, b), 0) def test_bonus_tiers(self): self.assertEqual(ap.theme_pack_bonus(self.by_name['电闪雷鸣'], 'charge', self.bonus), 6) self.assertEqual(ap.theme_pack_bonus(self.by_name['WARP快车谋杀案'], 'charge', self.bonus), 6) self.assertEqual(ap.theme_pack_bonus(self.by_name['开店抢购'], 'charge', self.bonus), 4) self.assertEqual(ap.theme_pack_bonus(self.by_name['WARP快车谋杀案'], 'bleed', self.bonus), 1) self.assertEqual(ap.theme_pack_bonus(self.by_name['电闪雷鸣'], 'bleed', self.bonus), 0) def test_bad_tables_are_rejected(self): for data in ({'bonus': {'featured': 6, 'unique': []}, 'packs': [pack('a')]}, {'bonus': BONUS | {'unique': [0, 1]}, 'packs': []}, {'bonus': {'featured': 6, 'unique': [0, 1]}, 'packs': [pack('a', featured=['fire'])]}, {'bonus': {'featured': 6, 'unique': [0, 1]}, 'packs': [pack('a', burn=0)]}, {'bonus': {'featured': 6, 'unique': [0, 1]}, 'packs': [pack('', 'x')]}, {'bonus': {'featured': 6, 'unique': [0, 1]}, 'packs': [pack('a')], 'community': {'recommend': [{'pack': '不存在', 'systems': ['burn']}]}}, {'bonus': {'featured': 6, 'unique': [0, 1]}, 'packs': [pack('a')], 'community': {'recommend': [{'pack': 'a', 'systems': ['*']}]}}, {'bonus': {'featured': 6, 'unique': [0, 1]}, 'packs': [pack('a')], 'community': {'avoid': [{'pack': 'a', 'systems': []}]}}): with self.subTest(data=data), tempfile.TemporaryDirectory() as d: path = os.path.join(d, 't.json') with open(path, 'w', encoding='utf-8') as fh: json.dump(data, fh, ensure_ascii=False, default=list) with self.assertRaises(ValueError): ap.load_theme_pack_systems(path) class BoostTest(unittest.TestCase): def setUp(self): self.bonus, self.packs = ap.load_theme_pack_systems() def boost(self, weights, system, language='zh_cn', packs=None): return ap.boost_theme_pack_weights(dict(weights), packs or self.packs, system, self.bonus, language) @unittest.skipUnless(EXAMPLE.is_file(), 'vendor/aalc 子模块未检出') def test_charge_team_in_normal_mode(self): weights = example_weights('theme_pack_list_cn') result, info = self.boost(weights, 'charge') self.assertEqual((result['谋杀'], result['开店']), (1, 1)) # WARP 专属 5 件充能, 开店 2 件 self.assertEqual((result['绝望'], result['绞丝']), (1, 1)) # 社区推荐 self.assertEqual(result['赌徒'], 0) self.assertEqual((result['遗忘'], result['自动']), (-3, -5)) # 速刷攻略说拖时间 self.assertEqual(result['地狱鸡'], 1) self.assertEqual(info['谋杀'], {'packs': ['WARP快车谋杀案', 'WARP快车谋杀案 复刻'], 'system': 'charge', 'from': -5, 'to': 1}) self.assertEqual(set(result), set(weights) | {k for k in info if k not in weights}) # 加过分的键排在最前面 self.assertEqual(list(result)[:len(info)], list(info)) @unittest.skipUnless(EXAMPLE.is_file(), 'vendor/aalc 子模块未检出') def test_keyword_packs_in_hard_mode(self): weights = example_weights('theme_pack_list_cn', 'theme_pack_list_hard_cn') result, _ = self.boost(weights, 'charge') self.assertEqual((result['电闪雷鸣'], result['动力']), (6, 6)) result, _ = self.boost(weights, 'bleed') self.assertEqual(result['渗出的'], 6) @unittest.skipUnless(EXAMPLE.is_file(), 'vendor/aalc 子模块未检出') def test_shared_key_takes_the_smaller_bonus_and_the_reprint_gets_its_full_name(self): # 「谋杀」同时命中原版(流血 1 件)和复刻(流血 3 件) result, info = self.boost(example_weights('theme_pack_list_cn'), 'bleed') self.assertEqual(result['谋杀'], -4) self.assertEqual(result['WARP快车谋杀案 复刻'], 1) order = list(result) self.assertLess(order.index('WARP快车谋杀案 复刻'), order.index('谋杀')) @unittest.skipUnless(EXAMPLE.is_file(), 'vendor/aalc 子模块未检出') def test_key_shared_with_an_unrelated_pack_is_left_alone(self): # 「区的奇」= 20区的奇迹 + 复刻; 只有复刻有震颤专属饰品 result, info = self.boost(example_weights('theme_pack_list_cn'), 'tremor') self.assertEqual(result['区的奇'], 0) self.assertEqual(result['20区的奇迹 复刻'], 1) self.assertNotIn('区的奇', info) def test_pack_unknown_to_aalc_starts_from_the_unknown_weight(self): packs = [pack('黎明后贯穿而来之楔', pierce=3)] result, info = self.boost({'遗忘': 0, '未知': -5}, 'pierce', packs=packs) self.assertEqual(result['黎明后贯穿而来之楔'], 1) self.assertEqual(result['未知'], -5) def test_english_keys(self): packs = [pack('WARP快车谋杀案', 'Murder on the WARP Express', charge=5), pack('电闪雷鸣', 'Thunder and Lightning', featured=['charge']), pack('被遗忘者们', 'The Forgotten')] result, _ = self.boost({'forgot': 0, 'warp': -5, 'thunder': 0, 'unknown': -5}, 'charge', 'en', packs) self.assertEqual(result, {'thunder': 6, 'warp': 1, 'forgot': 0, 'unknown': -5}) def test_unrelated_keys_keep_their_weight(self): weights = {'信仰': 0, '赌徒': -1} result, info = self.boost(weights, 'charge') self.assertEqual((result['信仰'], result['赌徒']), (0, -1)) self.assertNotIn('信仰', info) # 表里没有的充能卡包补全名键, 从「未知」(表里也没有就 -5)起算 self.assertEqual(result['电闪雷鸣'], 1) @unittest.skipUnless(EXAMPLE.is_file(), 'vendor/aalc 子模块未检出') def test_packs_missing_from_the_difficulty_do_not_block_a_key(self): # 「海·边」也是活动专用包 去·海·边·激·蟹 的一部分; 普通/困难都不会出现它, 不算冲突 weights = example_weights('theme_pack_list_cn') self.assertEqual(self.boost(weights, 'pierce')[0]['海·边'], 0) result, _ = ap.boost_theme_pack_weights(dict(weights), self.packs, 'pierce', self.bonus, 'zh_cn', False) self.assertEqual((result['海·边'], result['区的奇'], result['赌徒']), (6, 6, 6)) # 困难模式 20区的奇迹 复刻 也会出现, 它和突刺无关, 「区的奇」就不动 weights = example_weights('theme_pack_list_cn', 'theme_pack_list_hard_cn') result, _ = ap.boost_theme_pack_weights(dict(weights), self.packs, 'pierce', self.bonus, 'zh_cn', True) self.assertEqual(result['区的奇'], 0) def test_penalties_follow_the_same_shared_key_rule(self): packs = [pack('被遗忘者们', avoid=['*']), pack('自动工厂', avoid=['*']), pack('遗忘的工厂')] result, info = self.boost({'遗忘': 0, '工厂': -2}, 'charge', packs=packs) self.assertEqual(result['遗忘'], 0) # 遗忘的工厂 不减分, 有加有不加就不动短键 self.assertEqual(result['被遗忘者们'], -3) # 改用全名 self.assertEqual(result['工厂'], -2) self.assertEqual(result['自动工厂'], -5) def test_system_without_any_pack_changes_nothing(self): packs = [pack('被遗忘者们'), pack('电闪雷鸣', featured=['charge'])] weights = {'遗忘': 0, '电闪雷鸣': -5} self.assertEqual(self.boost(weights, 'burn', packs=packs), (weights, {})) @unittest.skipUnless(BUILD.is_file(), 'scripts 目录缺失') class GeneratorFormatTest(unittest.TestCase): def test_generator_writes_the_shipped_file_byte_for_byte(self): import importlib.util spec = importlib.util.spec_from_file_location('build_theme_pack_systems', BUILD) module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) text = Path(ap.THEME_PACK_SYSTEMS_FILE).read_text(encoding='utf-8') self.assertEqual(module.dump_table(json.loads(text)), text) class Match: def __init__(self, value, text): self.value, self.text, self.position = value, text, [0, 0] @unittest.skipUnless(THEME.is_file(), 'vendor/aalc 子模块未检出') class UpstreamSelectThemePackTest(unittest.TestCase): """执行上游真实的 select_theme_pack(普通模式, 中文)。""" def setUp(self): self.lines = [] tree = ast.parse(THEME.read_text(encoding='utf-8')) self.node = next(n for n in tree.body if isinstance(n, ast.FunctionDef) and n.name == 'select_theme_pack') self.node.decorator_list = [] def play(self, cards, system, patched=True, audit=False, env=None, hard=False, route=None): positions = [(300 + 300 * i, 300) for i in range(len(cards))] weights = example_weights('theme_pack_list_cn', *(('theme_pack_list_hard_cn',) if hard else ())) lookups = [] def find_language_text(zh, en, crop): # AALC: 按表的顺序, 第一个(去空格、不分大小写)是卡面文字子串的键 x = (crop[0] + crop[2]) / 2 text = cards[min(range(len(cards)), key=lambda i: abs(positions[i][0] - x))].replace(' ', '').lower() lookups.append(zh) for key, value in zh.items(): if key.replace(' ', '').lower() in text: return Match(value, key) return None 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) theme_list = SimpleNamespace(preferred_thresholds=0, get_effective_theme_pack_list=lambda *a: dict(weights)) ns = dict(auto=auto, TextMatchResult=Match, sleep=lambda *a: None, back_init_menu=mock.Mock(), log=logging.getLogger('AALC'), theme_list=theme_list, cfg=SimpleNamespace(set_win_size=720, select_event_pack=False, skip_event_pack=False), path_manager=SimpleNamespace(current_language='zh_cn')) exec(compile(ast.Module(body=[self.node], type_ignores=[]), str(THEME), 'exec'), ns) theme_mod = types.ModuleType('tasks.mirror.select_theme_pack') theme_mod.theme_list = theme_list mirror_mod = types.ModuleType('tasks.mirror.mirror') mirror_mod.select_theme_pack = ns['select_theme_pack'] mirror_mod.Mirror = type('Mirror', (), {'__init__': lambda self, system: setattr(self, 'system', system)}) package = types.ModuleType('tasks.mirror') package.mirror, package.select_theme_pack = mirror_mod, theme_mod logger = logging.getLogger('AALC') with mock.patch.dict(sys.modules, {'tasks': types.ModuleType('tasks'), 'tasks.mirror': package, 'tasks.mirror.mirror': mirror_mod, 'tasks.mirror.select_theme_pack': theme_mod}), \ mock.patch.object(logger, 'level', logging.DEBUG), mock.patch.object(logger, 'propagate', False), \ mock.patch.object(ap.decisions, 'record') as record: if patched: self.assertIsNotNone(ap.install_theme_pack_systems(log=self.lines.append, env=env or {}, route=route, route_name='兔子洞路线')) if audit: ap.install_theme_pack_audit(log=self.lines.append) mirror_mod.Mirror(system) mirror_mod.select_theme_pack(hard, 4, 1, False) self.assertEqual(theme_list.get_effective_theme_pack_list(), weights, '权重表的原方法必须还原') drag = auto.mouse_drag_down.call_args[0] picked = cards[positions.index(drag)] return picked, record, lookups @property def log(self): return '\n'.join(self.lines) FLOOR4 = ['伏行深渊', '落泪者们', 'WARP快车谋杀案', '堕落的忧郁'] def test_upstream_takes_the_leftmost_of_equal_weights(self): self.assertEqual(self.play(self.FLOOR4, 'charge', patched=False)[0], '落泪者们') def test_charge_team_takes_the_charge_pack(self): picked, _, _ = self.play(self.FLOOR4, 'charge') self.assertEqual(picked, 'WARP快车谋杀案') self.assertIn('patch: 镜牢卡包按体系加分(theme_pack_systems.json', self.log) self.assertIn('卡包按体系 charge 加分: ', self.log) self.assertIn('谋杀 -5→1', self.log) FLOOR1 = ['被遗忘者们', '身无分文的赌徒', '钉与锤'] def test_slow_pack_is_avoided_and_pierce_takes_its_route(self): self.assertEqual(self.play(self.FLOOR1, 'charge', patched=False)[0], '被遗忘者们') self.assertEqual(self.play(self.FLOOR1, 'charge')[0], '身无分文的赌徒') # 被遗忘者们 -3 self.assertEqual(self.play(['钉与锤', '身无分文的赌徒'], 'pierce')[0], '身无分文的赌徒') def test_bleed_team_does_not_take_the_original_warp(self): # 原版 WARP 只有 1 件流血专属饰品, 加 1 分仍然低于 0 分的卡包 self.assertEqual(self.play(self.FLOOR4, 'bleed')[0], '落泪者们') def test_audit_records_the_system_bonus(self): picked, record, _ = self.play(self.FLOOR4, 'charge', audit=True) args, kwargs = record.call_args self.assertEqual(args, ('theme_pack', '谋杀', 'preferred')) # 普通模式: 复刻只在困难出现, 不算进「谋杀」这个键 self.assertEqual(kwargs['system_bonus'], {'packs': ['WARP快车谋杀案'], 'system': 'charge', 'from': -5, 'to': 1}) def test_audit_keeps_a_key_with_a_space(self): # 困难模式流血队: 复刻 WARP 专属流血 3 件, 用全名键「WARP快车谋杀案 复刻」; 审计不能截在空格前 picked, record, _ = self.play(['钉与锤', 'WARP快车谋杀案 复刻'], 'bleed', audit=True, hard=True) self.assertEqual(picked, 'WARP快车谋杀案 复刻') args, kwargs = record.call_args self.assertEqual(args, ('theme_pack', 'WARP快车谋杀案 复刻', 'preferred')) self.assertEqual(kwargs['system_bonus']['to'], 1) def test_audit_without_bonus_has_no_system_field(self): _, record, _ = self.play(self.FLOOR4, 'bleed', audit=True) self.assertNotIn('system_bonus', record.call_args[1]) def test_unknown_system_keeps_upstream_weights(self): picked, _, lookups = self.play(self.FLOOR4, 'unknown_system') self.assertEqual(picked, '落泪者们') self.assertEqual(lookups[0], example_weights('theme_pack_list_cn')) def test_rabbit_hole_route_without_a_system(self): # 兔子洞编队没有体系; 困难模式 AALC 默认「绝望」「号线」都是 -5, 不加分拿 0 分的落泪者们 route = ap.load_rabbit_hole()['theme_packs'] floor4 = ['2号线', '憎恶与绝望', '落泪者们'] self.assertEqual(self.play(floor4, None, hard=True)[0], '落泪者们') self.assertEqual(self.play(floor4, None, hard=True, route=route)[0], '憎恶与绝望') # 2号线留给 5 层 self.assertIn('卡包按体系 (无)+兔子洞路线 加分: ', self.log) self.assertIn('兔子洞路线 身无分文的赌徒+10 地狱鸡+10 去·海·边+10 憎恶与绝望+12 2号线+10', self.log) self.assertEqual(self.play(['电闪雷鸣', '2号线'], None, hard=True, route=route)[0], '2号线') self.assertEqual(self.play(['地狱鸡', '去·海·边'], None, hard=True, route=route)[0], '地狱鸡') self.assertEqual(self.play(['憎恶与绝望', '去·海·边'], None, hard=True, route=route)[0], '去·海·边') def test_route_beats_the_system_bonus_and_unknown_names_are_errors(self): picked, _, _ = self.play(self.FLOOR4, 'charge', route={'落泪者们': 20, '不存在的包': 5}) self.assertEqual(picked, '落泪者们') self.assertIn('[ERROR] [lc_mcp] 兔子洞路线卡包不在卡包表里, 不加分: 不存在的包', self.log) def test_env_zero_and_bad_table(self): self.assertIsNone(ap.install_theme_pack_systems(log=self.lines.append, env={ap.THEME_PACK_SYSTEMS_ENV: '0'})) with tempfile.TemporaryDirectory() as d: path = os.path.join(d, 'bad.json') with open(path, 'w', encoding='utf-8') as fh: fh.write('{}') self.assertIsNone(ap.install_theme_pack_systems(log=self.lines.append, env={ap.THEME_PACK_SYSTEMS_ENV: path})) self.assertIn('[ERROR] [lc_mcp] 卡包体系表读不了', self.log) def test_upstream_without_the_weight_lookup_is_an_error(self): with mock.patch('inspect.getsource', return_value='def select_theme_pack(): pass'): theme_mod = types.ModuleType('tasks.mirror.select_theme_pack') theme_mod.theme_list = SimpleNamespace() mirror_mod = types.ModuleType('tasks.mirror.mirror') mirror_mod.select_theme_pack = lambda *a: None package = types.ModuleType('tasks.mirror') package.mirror, package.select_theme_pack = mirror_mod, theme_mod with mock.patch.dict(sys.modules, {'tasks': types.ModuleType('tasks'), 'tasks.mirror': package, 'tasks.mirror.mirror': mirror_mod, 'tasks.mirror.select_theme_pack': theme_mod}): self.assertIsNone(ap.install_theme_pack_systems(log=self.lines.append, env={})) self.assertIn('[ERROR] [lc_mcp] 上游 select_theme_pack 不再从 theme_list.get_effective_theme_pack_list 取权重', self.log) def test_current_upstream_source_has_the_weight_lookup(self): self.assertIn('theme_list.get_effective_theme_pack_list(', THEME.read_text(encoding='utf-8')) 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