# -*- 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()