55 lines
2.6 KiB
Python
55 lines
2.6 KiB
Python
"""Check djev startup selection without importing torch or loading models."""
|
|
import importlib.util
|
|
import json
|
|
from pathlib import Path
|
|
import tempfile
|
|
import unittest
|
|
|
|
SCRIPT = Path(__file__).resolve().parents[2] / "tools/start_d3_lite_local.py"
|
|
spec = importlib.util.spec_from_file_location("djev_launcher", SCRIPT)
|
|
launcher = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(launcher)
|
|
|
|
|
|
class SelectionChecks(unittest.TestCase):
|
|
def test_startup_selection_and_explicit_switch(self):
|
|
with tempfile.TemporaryDirectory() as temporary:
|
|
root = Path(temporary).resolve() / "D3Lite"
|
|
root.mkdir()
|
|
config = root / "djev.json"
|
|
self.assertEqual(launcher.resolve_model_root(root), root)
|
|
|
|
config.write_text(json.dumps({"tier": "nano"}), encoding="utf-8-sig")
|
|
self.assertEqual(launcher.resolve_model_root(root), root.parent / "D3Nano")
|
|
custom = root.parent / "custom nano"
|
|
config.write_text(json.dumps({"tier": "nano", "nano_root": str(custom)}), encoding="utf-8")
|
|
self.assertEqual(launcher.resolve_model_root(root), custom)
|
|
self.assertEqual(launcher.resolve_model_root(root, ignore_selection=True), root)
|
|
config.write_text(json.dumps({"tier": "lite", "nano_root": str(custom)}), encoding="utf-8")
|
|
self.assertEqual(launcher.resolve_model_root(root), root)
|
|
|
|
def test_lite_directory_from_nano_installation(self):
|
|
# 桌面程序写入两个目录后,两种档位均按所选目录启动,不受 Python 安装位置影响。
|
|
with tempfile.TemporaryDirectory() as temporary:
|
|
root = Path(temporary).resolve() / "D3Nano"
|
|
root.mkdir()
|
|
config = root / "djev.json"
|
|
config.write_text(json.dumps({"tier": "lite", "lite_root": "../D3Lite"}), encoding="utf-8")
|
|
self.assertEqual(launcher.resolve_model_root(root), root.parent / "D3Lite")
|
|
self.assertEqual(launcher.resolve_model_root(root, ignore_selection=True), root)
|
|
|
|
def test_invalid_selection_does_not_silently_load_another_model(self):
|
|
with tempfile.TemporaryDirectory() as temporary:
|
|
root = Path(temporary)
|
|
config = root / "djev.json"
|
|
config.write_text('{"tier":"other"}', encoding="utf-8")
|
|
with self.assertRaises(ValueError):
|
|
launcher.resolve_model_root(root)
|
|
config.write_text('{broken', encoding="utf-8")
|
|
with self.assertRaises(json.JSONDecodeError):
|
|
launcher.resolve_model_root(root)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|