File size: 20,518 Bytes
8ede856 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 | """Tests for config module."""
import json
import os
import pytest
from astrbot.core.config.astrbot_config import AstrBotConfig, RateLimitStrategy
from astrbot.core.config.default import DEFAULT_VALUE_MAP
from astrbot.core.config.i18n_utils import ConfigMetadataI18n
@pytest.fixture
def temp_config_path(tmp_path):
"""Create a temporary config path."""
return str(tmp_path / "test_config.json")
@pytest.fixture
def minimal_default_config():
"""Create a minimal default config for testing."""
return {
"config_version": 2,
"platform_settings": {
"unique_session": False,
"rate_limit": {
"time": 60,
"count": 30,
"strategy": "stall",
},
},
"provider_settings": {
"enable": True,
"default_provider_id": "",
},
}
class TestRateLimitStrategy:
"""Tests for RateLimitStrategy enum."""
def test_stall_value(self):
"""Test stall enum value."""
assert RateLimitStrategy.STALL.value == "stall"
def test_discard_value(self):
"""Test discard enum value."""
assert RateLimitStrategy.DISCARD.value == "discard"
class TestAstrBotConfigLoad:
"""Tests for AstrBotConfig loading and initialization."""
def test_init_creates_file_if_not_exists(
self, temp_config_path, minimal_default_config
):
"""Test that config file is created when it doesn't exist."""
assert not os.path.exists(temp_config_path)
config = AstrBotConfig(
config_path=temp_config_path, default_config=minimal_default_config
)
assert os.path.exists(temp_config_path)
assert config.config_version == 2
assert config.platform_settings["unique_session"] is False
def test_init_loads_existing_file(self, temp_config_path, minimal_default_config):
"""Test that existing config file is loaded."""
existing_config = {
"config_version": 2,
"platform_settings": {"unique_session": True},
"provider_settings": {"enable": False},
}
with open(temp_config_path, "w", encoding="utf-8-sig") as f:
json.dump(existing_config, f)
config = AstrBotConfig(
config_path=temp_config_path, default_config=minimal_default_config
)
assert config.platform_settings["unique_session"] is True
assert config.provider_settings["enable"] is False
def test_first_deploy_flag(self, temp_config_path, minimal_default_config):
"""Test first_deploy flag is set for new config."""
config = AstrBotConfig(
config_path=temp_config_path, default_config=minimal_default_config
)
assert hasattr(config, "first_deploy")
assert config.first_deploy is True
def test_init_with_schema(self, temp_config_path):
"""Test initialization with schema."""
schema = {
"test_field": {
"type": "string",
"default": "test_value",
},
"nested": {
"type": "object",
"items": {
"enabled": {"type": "bool"},
"count": {"type": "int"},
},
},
}
config = AstrBotConfig(config_path=temp_config_path, schema=schema)
assert config.test_field == "test_value"
assert config.nested["enabled"] is False
assert config.nested["count"] == 0
def test_dot_notation_access(self, temp_config_path, minimal_default_config):
"""Test accessing config values using dot notation."""
config = AstrBotConfig(
config_path=temp_config_path, default_config=minimal_default_config
)
assert config.platform_settings is not None
assert config.non_existent_field is None
def test_setattr_updates_config(self, temp_config_path, minimal_default_config):
"""Test that setting attributes updates config."""
config = AstrBotConfig(
config_path=temp_config_path, default_config=minimal_default_config
)
config.new_field = "new_value"
assert config.new_field == "new_value"
def test_delattr_removes_field(self, temp_config_path, minimal_default_config):
"""Test that deleting attributes removes them."""
config = AstrBotConfig(
config_path=temp_config_path, default_config=minimal_default_config
)
config.temp_field = "temp"
del config.temp_field
# Accessing a deleted field returns None due to __getattr__
assert config.temp_field is None
# But the field is removed from the dict
assert "temp_field" not in config
def test_delattr_saves_config(self, temp_config_path, minimal_default_config):
"""Test that deleting attributes saves config to file."""
config = AstrBotConfig(
config_path=temp_config_path, default_config=minimal_default_config
)
config.temp_field = "temp"
del config.temp_field
with open(temp_config_path, encoding="utf-8-sig") as f:
loaded_config = json.load(f)
assert "temp_field" not in loaded_config
def test_check_exist(self, temp_config_path, minimal_default_config):
"""Test check_exist method."""
config = AstrBotConfig(
config_path=temp_config_path, default_config=minimal_default_config
)
assert config.check_exist() is True
# Create a path that definitely doesn't exist
import pathlib
temp_dir = pathlib.Path(temp_config_path).parent
non_existent_path = str(temp_dir / "non_existent_config.json")
# Check that the file doesn't exist before creating config
assert not os.path.exists(non_existent_path)
# Create config which will auto-create the file
config2 = AstrBotConfig(
config_path=non_existent_path, default_config=minimal_default_config
)
# Now it exists
assert config2.check_exist() is True
assert os.path.exists(non_existent_path)
class TestConfigValidation:
"""Tests for config validation and integrity checking."""
def test_insert_missing_config_items(
self, temp_config_path, minimal_default_config
):
"""Test that missing config items are inserted with default values."""
existing_config = {"config_version": 2}
with open(temp_config_path, "w", encoding="utf-8-sig") as f:
json.dump(existing_config, f)
config = AstrBotConfig(
config_path=temp_config_path, default_config=minimal_default_config
)
assert "platform_settings" in config
assert "provider_settings" in config
def test_replace_none_with_default(self, temp_config_path, minimal_default_config):
"""Test that None values are replaced with defaults."""
existing_config = {
"config_version": 2,
"platform_settings": None,
"provider_settings": None,
}
with open(temp_config_path, "w", encoding="utf-8-sig") as f:
json.dump(existing_config, f)
AstrBotConfig(
config_path=temp_config_path, default_config=minimal_default_config
)
# Reload to verify the values were replaced
config2 = AstrBotConfig(
config_path=temp_config_path, default_config=minimal_default_config
)
assert config2.platform_settings is not None
assert config2.provider_settings is not None
def test_reorder_config_keys(self, temp_config_path, minimal_default_config):
"""Test that config keys are reordered to match default."""
existing_config = {
"provider_settings": {"enable": True},
"config_version": 2,
"platform_settings": {"unique_session": False},
}
with open(temp_config_path, "w", encoding="utf-8-sig") as f:
json.dump(existing_config, f)
AstrBotConfig(
config_path=temp_config_path, default_config=minimal_default_config
)
with open(temp_config_path, encoding="utf-8-sig") as f:
loaded_config = json.load(f)
keys = list(loaded_config.keys())
assert keys[0] == "config_version"
assert keys[1] == "platform_settings"
assert keys[2] == "provider_settings"
def test_remove_unknown_config_keys(self, temp_config_path, minimal_default_config):
"""Test that unknown config keys are removed."""
existing_config = {
"config_version": 2,
"platform_settings": {},
"unknown_key": "should_be_removed",
}
with open(temp_config_path, "w", encoding="utf-8-sig") as f:
json.dump(existing_config, f)
config = AstrBotConfig(
config_path=temp_config_path, default_config=minimal_default_config
)
assert "unknown_key" not in config
def test_nested_config_validation(self, temp_config_path):
"""Test validation of nested config structures."""
default_config = {
"nested": {
"level1": {
"level2": {
"value": 42,
},
},
},
}
existing_config = {
"nested": {
"level1": {}, # Missing level2
},
}
with open(temp_config_path, "w", encoding="utf-8-sig") as f:
json.dump(existing_config, f)
config = AstrBotConfig(
config_path=temp_config_path, default_config=default_config
)
assert "level2" in config.nested["level1"]
assert config.nested["level1"]["level2"]["value"] == 42
class TestConfigHotReload:
"""Tests for config hot reload functionality."""
def test_save_config(self, temp_config_path, minimal_default_config):
"""Test saving config to file."""
config = AstrBotConfig(
config_path=temp_config_path, default_config=minimal_default_config
)
config.new_field = "new_value"
config.save_config()
with open(temp_config_path, encoding="utf-8-sig") as f:
loaded_config = json.load(f)
assert loaded_config["new_field"] == "new_value"
def test_save_config_with_replace(self, temp_config_path, minimal_default_config):
"""Test saving config with replacement."""
config = AstrBotConfig(
config_path=temp_config_path, default_config=minimal_default_config
)
replacement_config = {
"replaced": True,
"extra_field": "value",
}
config.save_config(replace_config=replacement_config)
with open(temp_config_path, encoding="utf-8-sig") as f:
loaded_config = json.load(f)
# The replacement config is merged with existing config
assert loaded_config["replaced"] is True
assert loaded_config["extra_field"] == "value"
# Original fields are preserved because update merges
assert "platform_settings" in loaded_config
def test_modification_persists_after_reload(
self, temp_config_path, minimal_default_config
):
"""Test that modifications persist after reloading."""
config1 = AstrBotConfig(
config_path=temp_config_path, default_config=minimal_default_config
)
config1.platform_settings["unique_session"] = True
config1.save_config()
config2 = AstrBotConfig(
config_path=temp_config_path, default_config=minimal_default_config
)
assert config2.platform_settings["unique_session"] is True
class TestConfigSchemaToDefault:
"""Tests for schema to default config conversion."""
def test_convert_schema_with_defaults(self, temp_config_path):
"""Test converting schema with explicit defaults."""
schema = {
"string_field": {"type": "string", "default": "custom"},
"int_field": {"type": "int", "default": 100},
"bool_field": {"type": "bool", "default": True},
}
config = AstrBotConfig(config_path=temp_config_path, schema=schema)
assert config.string_field == "custom"
assert config.int_field == 100
assert config.bool_field is True
def test_convert_schema_without_defaults(self, temp_config_path):
"""Test converting schema using default value map."""
schema = {
"string_field": {"type": "string"},
"int_field": {"type": "int"},
"bool_field": {"type": "bool"},
}
config = AstrBotConfig(config_path=temp_config_path, schema=schema)
assert config.string_field == DEFAULT_VALUE_MAP["string"]
assert config.int_field == DEFAULT_VALUE_MAP["int"]
assert config.bool_field == DEFAULT_VALUE_MAP["bool"]
def test_unsupported_schema_type_raises_error(self, temp_config_path):
"""Test that unsupported schema types raise error."""
schema = {
"field": {"type": "unsupported_type"},
}
with pytest.raises(TypeError, match="不受支持的配置类型"):
AstrBotConfig(config_path=temp_config_path, schema=schema)
def test_template_list_type(self, temp_config_path):
"""Test template_list schema type."""
schema = {
"templates": {"type": "template_list", "default": []},
}
config = AstrBotConfig(config_path=temp_config_path, schema=schema)
assert config.templates == []
def test_nested_object_schema(self, temp_config_path):
"""Test nested object schema conversion."""
schema = {
"nested": {
"type": "object",
"items": {
"field1": {"type": "string"},
"field2": {"type": "int"},
},
},
}
config = AstrBotConfig(config_path=temp_config_path, schema=schema)
assert config.nested["field1"] == ""
assert config.nested["field2"] == 0
class TestConfigMetadataI18n:
"""Tests for i18n utils."""
def test_get_i18n_key(self):
"""Test generating i18n key."""
key = ConfigMetadataI18n._get_i18n_key(
group="ai_group",
section="general",
field="enable",
attr="description",
)
assert key == "ai_group.general.enable.description"
def test_get_i18n_key_without_field(self):
"""Test generating i18n key without field."""
key = ConfigMetadataI18n._get_i18n_key(
group="ai_group",
section="general",
field="",
attr="description",
)
assert key == "ai_group.general.description"
def test_convert_to_i18n_keys_simple(self):
"""Test converting simple metadata to i18n keys."""
metadata = {
"ai_group": {
"name": "AI Settings",
"metadata": {
"general": {
"description": "General settings",
"items": {
"enable": {
"description": "Enable feature",
"type": "bool",
"default": True,
},
},
},
},
},
}
result = ConfigMetadataI18n.convert_to_i18n_keys(metadata)
assert result["ai_group"]["name"] == "ai_group.name"
assert (
result["ai_group"]["metadata"]["general"]["description"]
== "ai_group.general.description"
)
assert (
result["ai_group"]["metadata"]["general"]["items"]["enable"]["description"]
== "ai_group.general.enable.description"
)
def test_convert_to_i18n_keys_with_hint(self):
"""Test converting metadata with hint."""
metadata = {
"group": {
"metadata": {
"section": {
"hint": "This is a hint",
"items": {
"field": {
"hint": "Field hint",
"type": "string",
},
},
},
},
},
}
result = ConfigMetadataI18n.convert_to_i18n_keys(metadata)
assert result["group"]["metadata"]["section"]["hint"] == "group.section.hint"
assert (
result["group"]["metadata"]["section"]["items"]["field"]["hint"]
== "group.section.field.hint"
)
def test_convert_to_i18n_keys_with_labels(self):
"""Test converting metadata with labels."""
metadata = {
"group": {
"metadata": {
"section": {
"items": {
"field": {
"labels": ["Label1", "Label2"],
"type": "string",
},
},
},
},
},
}
result = ConfigMetadataI18n.convert_to_i18n_keys(metadata)
assert (
result["group"]["metadata"]["section"]["items"]["field"]["labels"]
== "group.section.field.labels"
)
def test_convert_to_i18n_keys_nested_items(self):
"""Test converting metadata with nested items."""
metadata = {
"group": {
"metadata": {
"section": {
"items": {
"nested": {
"description": "Nested field",
"type": "object",
"items": {
"inner": {
"description": "Inner field",
"type": "string",
},
},
},
},
},
},
},
}
result = ConfigMetadataI18n.convert_to_i18n_keys(metadata)
assert (
result["group"]["metadata"]["section"]["items"]["nested"]["description"]
== "group.section.nested.description"
)
assert (
result["group"]["metadata"]["section"]["items"]["nested"]["items"]["inner"][
"description"
]
== "group.section.nested.inner.description"
)
def test_convert_to_i18n_keys_preserves_non_i18n_fields(self):
"""Test that non-i18n fields are preserved."""
metadata = {
"group": {
"metadata": {
"section": {
"items": {
"field": {
"description": "Field description",
"type": "string",
"other_field": "preserve this",
},
},
},
},
},
}
result = ConfigMetadataI18n.convert_to_i18n_keys(metadata)
assert (
result["group"]["metadata"]["section"]["items"]["field"]["other_field"]
== "preserve this"
)
def test_convert_to_i18n_keys_with_name(self):
"""Test converting metadata with name field."""
metadata = {
"group": {
"metadata": {
"section": {
"items": {
"field": {
"name": "Field Name",
"type": "string",
},
},
},
},
},
}
result = ConfigMetadataI18n.convert_to_i18n_keys(metadata)
assert (
result["group"]["metadata"]["section"]["items"]["field"]["name"]
== "group.section.field.name"
)
|