Files
lc-mcp/tests/test_shop_lists.py
T
ywsandClaude Opus 5.5 baa362808c P0-1 补齐镜牢商店必买/必卖/公式合成清单
上游 ebb6629 把图片挪到 assets/images/<主题>/<语言>/ 下,
tasks/mirror/__init__.py 仍按 ./assets/images/share/... 列目录,三个
清单恒为空:11-14 实测进店 51 秒一件没买,弃用饰品识别和公式合成
一起空转。按名字找图的 path_manager 那层没坏。

install_shop_lists() 在 mirror/all 任务里按 AALC 六个图片根合并去重,
原地 extend 三个列表(in_shop 在导入时绑定了列表对象,重新赋值无效);
上游清单已非空时不动,不改 vendor。

验证:执行上游真实 __init__.py 得到 0/0/0,补丁后 5/19/31,且每个
图片名都能在某个图片根下找到;全套 514 项通过。实机待看补丁日志与
进店后的购买动作。

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 05:45:03 +00:00

120 lines
5.0 KiB
Python

# -*- coding: utf-8 -*-
"""镜牢商店"必买 / 必卖 / 公式合成"清单。
2026-11-14 实测: 上游 tasks/mirror/__init__.py 仍按 ./assets/images/share/... 列目录,
图片早已挪到 assets/images/<主题>/<语言>/ 下, 三个清单恒为空, 进店 51 秒一件没买。
"""
import os
import sys
import tempfile
import types
import unittest
from contextlib import contextmanager
from pathlib import Path
from unittest import mock
REPO = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(REPO))
from lc_mcp import aalc_patches # noqa: E402
AALC = REPO / 'vendor' / 'aalc'
IMAGES = AALC / 'assets' / 'images'
@contextmanager
def chdir(path):
old = os.getcwd()
os.chdir(path)
try:
yield
finally:
os.chdir(old)
def as_tasks_mirror(package):
"""让 import tasks.mirror 拿到给定模块(父包也要在 sys.modules 里)。"""
parent = types.ModuleType('tasks')
parent.mirror = package
return mock.patch.dict(sys.modules, {'tasks': parent, 'tasks.mirror': package})
def upstream_mirror_package():
"""在 AALC 目录下执行上游 tasks/mirror/__init__.py, 得到它真实生成的三个列表。"""
module = types.ModuleType('tasks.mirror')
source = (AALC / 'tasks' / 'mirror' / '__init__.py').read_text(encoding='utf-8')
with chdir(AALC):
exec(compile(source, str(AALC / 'tasks' / 'mirror' / '__init__.py'), 'exec'), module.__dict__)
return module
@unittest.skipUnless(IMAGES.is_dir(), 'vendor/aalc 子模块未检出')
class UpstreamShopListTest(unittest.TestCase):
def test_upstream_lists_are_empty_and_patch_fills_them(self):
package = upstream_mirror_package()
self.assertEqual([len(getattr(package, k)) for k in aalc_patches.SHOP_LISTS], [0, 0, 0],
'上游已修好时可以删掉这个补丁')
# in_shop 在导入时绑定的就是这几个列表对象
in_shop = types.SimpleNamespace(**{k: getattr(package, k) for k in aalc_patches.SHOP_LISTS})
lines = []
with as_tasks_mirror(package), chdir(AALC):
filled = aalc_patches.install_shop_lists(log=lines.append)
self.assertEqual(filled, {'must_purchase': 5, 'must_be_abandoned': 19, 'fusion_result': 31})
self.assertEqual([len(getattr(in_shop, k)) for k in aalc_patches.SHOP_LISTS], [5, 19, 31])
self.assertIn('mirror/shop/must_purchase/carmilla.png', in_shop.must_purchase)
self.assertIn('must_purchase=5', '\n'.join(lines))
def test_every_listed_name_resolves_under_some_image_root(self):
for kind in aalc_patches.SHOP_LISTS:
for name in aalc_patches.shop_list_images(kind, str(IMAGES)):
with self.subTest(name=name):
self.assertTrue(any((IMAGES / root / name).is_file() for root in aalc_patches.IMAGE_ROOTS))
def test_local_roots_match_aalc_path_manager(self):
source = (AALC / 'utils' / 'path_manager.py').read_text(encoding='utf-8')
for root in aalc_patches.IMAGE_ROOTS:
self.assertIn('"%s"' % root, source)
class ShopListImagesTest(unittest.TestCase):
def setUp(self):
tmp = tempfile.TemporaryDirectory()
self.addCleanup(tmp.cleanup)
self.images = tmp.name
def put(self, root, kind, name):
folder = os.path.join(self.images, *root.split('/'), 'mirror', 'shop', kind)
os.makedirs(folder, exist_ok=True)
open(os.path.join(folder, name), 'wb').close()
def test_merges_themes_and_languages_without_duplicates(self):
self.put('default/share', 'must_purchase', 'b.png')
self.put('dark/share', 'must_purchase', 'b.png')
self.put('default/zh_cn', 'must_purchase', 'a.png')
self.put('default/share', 'must_purchase', 'notes.txt')
os.makedirs(os.path.join(self.images, 'default', 'share', 'mirror', 'shop', 'must_purchase', 'x.png'))
self.assertEqual(aalc_patches.shop_list_images('must_purchase', self.images),
['mirror/shop/must_purchase/a.png', 'mirror/shop/must_purchase/b.png'])
def test_missing_folders_give_an_empty_list(self):
self.assertEqual(aalc_patches.shop_list_images('fusion_result', self.images), [])
def test_non_empty_upstream_list_is_left_alone(self):
self.put('default/share', 'must_purchase', 'a.png')
package = types.SimpleNamespace(must_purchase=['upstream.png'], must_be_abandoned=[], fusion_result=[])
with as_tasks_mirror(package):
filled = aalc_patches.install_shop_lists(log=lambda m: None, images_dir=self.images)
self.assertEqual(package.must_purchase, ['upstream.png'])
self.assertEqual(filled, {'must_be_abandoned': 0, 'fusion_result': 0})
def test_import_failure_is_reported_not_raised(self):
lines = []
with mock.patch.dict(sys.modules, {'tasks': types.ModuleType('tasks'), 'tasks.mirror': None}):
self.assertEqual(aalc_patches.install_shop_lists(log=lines.append), {})
self.assertIn('商店清单补丁跳过', '\n'.join(lines))
if __name__ == '__main__':
unittest.main()