158 行
6.5 KiB
Python
158 行
6.5 KiB
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import json
|
||
|
|
from pathlib import Path
|
||
|
|
|
||
|
|
import main
|
||
|
|
from backend.services.settings_service import SettingsService
|
||
|
|
from core import runtime_paths
|
||
|
|
from core.config_service import ConfigService
|
||
|
|
from core.identity import RequestContext
|
||
|
|
from database import db_helper
|
||
|
|
|
||
|
|
|
||
|
|
class _AstrBotConfig(dict[str, object]):
|
||
|
|
def __init__(self, values: dict[str, object]):
|
||
|
|
super().__init__(values)
|
||
|
|
self.saved_snapshots: list[dict[str, object]] = []
|
||
|
|
|
||
|
|
def save_config(self) -> None:
|
||
|
|
self.saved_snapshots.append(dict(self))
|
||
|
|
|
||
|
|
|
||
|
|
def _cross_review_config() -> _AstrBotConfig:
|
||
|
|
return _AstrBotConfig(
|
||
|
|
{
|
||
|
|
"model_visual_review_routes": ["vision-primary", "vision-secondary"],
|
||
|
|
"model_cross_review_primary": "vision-primary",
|
||
|
|
"model_cross_review_secondary": "vision-secondary",
|
||
|
|
"model_cross_review_routes": ["vision-primary", "vision-secondary"],
|
||
|
|
"model_generation_cross_review_primary": "image-primary",
|
||
|
|
"model_generation_cross_review_secondary": "image-secondary",
|
||
|
|
"model_generation_cross_review_routes": ["image-primary", "image-secondary"],
|
||
|
|
}
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
def test_cross_review_assignments_are_declared_in_astrbot_schema() -> None:
|
||
|
|
schema = json.loads(Path("_conf_schema.json").read_text(encoding="utf-8"))
|
||
|
|
|
||
|
|
expected = {
|
||
|
|
"model_visual_review_routes": "list",
|
||
|
|
"model_cross_review_primary": "string",
|
||
|
|
"model_cross_review_secondary": "string",
|
||
|
|
"model_cross_review_routes": "list",
|
||
|
|
"model_generation_cross_review_primary": "string",
|
||
|
|
"model_generation_cross_review_secondary": "string",
|
||
|
|
"model_generation_cross_review_routes": "list",
|
||
|
|
}
|
||
|
|
assert {key: schema[key]["type"] for key in expected} == expected
|
||
|
|
|
||
|
|
|
||
|
|
def test_cross_review_assignments_map_from_flat_astrbot_config() -> None:
|
||
|
|
patch = main._runtime_config_patch_from_mapping(_cross_review_config())
|
||
|
|
|
||
|
|
assert patch["model_assignments"] == {
|
||
|
|
"visual_review_routes": ["vision-primary", "vision-secondary"],
|
||
|
|
"cross_review_primary": "vision-primary",
|
||
|
|
"cross_review_secondary": "vision-secondary",
|
||
|
|
"cross_review_routes": ["vision-primary", "vision-secondary"],
|
||
|
|
"generation_cross_review_primary": "image-primary",
|
||
|
|
"generation_cross_review_secondary": "image-secondary",
|
||
|
|
"generation_cross_review_routes": ["image-primary", "image-secondary"],
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
def test_empty_generation_cross_review_assignments_remain_runtime_managed() -> None:
|
||
|
|
patch = main._runtime_config_patch_from_mapping(
|
||
|
|
{
|
||
|
|
"model_generation_cross_review_primary": "",
|
||
|
|
"model_generation_cross_review_secondary": "",
|
||
|
|
"model_generation_cross_review_routes": [],
|
||
|
|
}
|
||
|
|
)
|
||
|
|
|
||
|
|
assert patch["model_assignments"] == {
|
||
|
|
"generation_cross_review_primary": "",
|
||
|
|
"generation_cross_review_secondary": "",
|
||
|
|
"generation_cross_review_routes": [],
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
def test_cross_review_ui_save_round_trips_through_astrbot_config(
|
||
|
|
tmp_path: Path,
|
||
|
|
monkeypatch,
|
||
|
|
) -> None:
|
||
|
|
# Simulate AstrBot's writable plugin-data config without touching the
|
||
|
|
# repository configuration used by the rest of the suite.
|
||
|
|
source_root = Path(__file__).resolve().parents[1]
|
||
|
|
(tmp_path / "config.yaml").write_text(
|
||
|
|
(source_root / "config.yaml").read_text(encoding="utf-8"),
|
||
|
|
encoding="utf-8",
|
||
|
|
)
|
||
|
|
monkeypatch.setattr(runtime_paths, "REPO_ROOT", tmp_path)
|
||
|
|
astrbot_config = _cross_review_config()
|
||
|
|
|
||
|
|
try:
|
||
|
|
applied = main.apply_astrbot_config_overrides(astrbot_config)
|
||
|
|
assert "model_assignments.generation_cross_review_routes" in applied["keys"]
|
||
|
|
|
||
|
|
service = SettingsService(
|
||
|
|
config_service=ConfigService(),
|
||
|
|
schema_path=source_root / "_conf_schema.json",
|
||
|
|
)
|
||
|
|
service.save(
|
||
|
|
{
|
||
|
|
"config": {
|
||
|
|
"model_assignments": {
|
||
|
|
"visual_review_routes": ["vision-new"],
|
||
|
|
"cross_review_primary": "vision-new",
|
||
|
|
"cross_review_secondary": "vision-backup",
|
||
|
|
"cross_review_routes": ["vision-new", "vision-backup"],
|
||
|
|
"generation_cross_review_primary": "image-new",
|
||
|
|
"generation_cross_review_secondary": "image-backup",
|
||
|
|
"generation_cross_review_routes": ["image-new", "image-backup"],
|
||
|
|
}
|
||
|
|
}
|
||
|
|
},
|
||
|
|
RequestContext.system("runtime-config-round-trip"),
|
||
|
|
)
|
||
|
|
|
||
|
|
assert len(astrbot_config.saved_snapshots) == 1
|
||
|
|
assert astrbot_config["model_visual_review_routes"] == ["vision-new"]
|
||
|
|
assert astrbot_config["model_cross_review_routes"] == ["vision-new", "vision-backup"]
|
||
|
|
assert astrbot_config["model_generation_cross_review_primary"] == "image-new"
|
||
|
|
assert astrbot_config["model_generation_cross_review_secondary"] == "image-backup"
|
||
|
|
assert astrbot_config["model_generation_cross_review_routes"] == ["image-new", "image-backup"]
|
||
|
|
|
||
|
|
service.save(
|
||
|
|
{
|
||
|
|
"config": {
|
||
|
|
"model_assignments": {
|
||
|
|
"generation_cross_review_primary": "",
|
||
|
|
"generation_cross_review_secondary": "",
|
||
|
|
"generation_cross_review_routes": [],
|
||
|
|
}
|
||
|
|
}
|
||
|
|
},
|
||
|
|
RequestContext.system("runtime-config-clear-round-trip"),
|
||
|
|
)
|
||
|
|
assert len(astrbot_config.saved_snapshots) == 2
|
||
|
|
assert astrbot_config["model_generation_cross_review_primary"] == ""
|
||
|
|
assert astrbot_config["model_generation_cross_review_secondary"] == ""
|
||
|
|
assert astrbot_config["model_generation_cross_review_routes"] == []
|
||
|
|
|
||
|
|
# A fresh runtime application must read the values saved through
|
||
|
|
# AstrBot rather than restoring the packaged defaults.
|
||
|
|
db_helper.set_runtime_config_overrides({})
|
||
|
|
main.apply_astrbot_config_overrides(astrbot_config)
|
||
|
|
assignments = service.get_current()["config"]["model_assignments"]
|
||
|
|
assert assignments["visual_review_routes"] == ["vision-new"]
|
||
|
|
assert assignments["cross_review_routes"] == ["vision-new", "vision-backup"]
|
||
|
|
assert assignments["generation_cross_review_primary"] == ""
|
||
|
|
assert assignments["generation_cross_review_secondary"] == ""
|
||
|
|
assert assignments["generation_cross_review_routes"] == []
|
||
|
|
finally:
|
||
|
|
db_helper.set_runtime_config_persister(None)
|
||
|
|
db_helper.set_runtime_config_overrides({})
|