from __future__ import annotations import tempfile import unittest from pathlib import Path from unittest.mock import patch from backend.dto.common import ApiError, ErrorCode, error_response, success_response from core.audit_service import AuditService from core.config_service import ConfigService from core.config_writer import config_file_lock from core.identity import RequestContext from core.runtime_paths import PLUGIN_NAME, REPO_ROOT, get_default_config_path, resolve_data_path from backend.services.settings_service import SettingsService, _dump_yaml from database.db_helper import ( Database, DatabaseSettings, PostgresSettings, SqliteSettings, _Psycopg2ConnectionAdapter, _load_yaml_subset, load_config_file_payload, load_database_settings, set_runtime_config_overrides, ) from database.models import table_spec from database.seed import DEFAULT_MATERIAL_CATEGORIES, seed_default_material_categories class CommonDtoTests(unittest.TestCase): def test_success_response_matches_contract(self): response = success_response({"ready": True}, request_id="req_test") self.assertEqual( response, { "ok": True, "data": {"ready": True}, "warnings": [], "request_id": "req_test", }, ) def test_error_response_matches_contract(self): response = error_response( error_code=ErrorCode.VALIDATION_FAILED, message="缺少字段", details={"field": "name"}, request_id="req_test", ) self.assertEqual(response["ok"], False) self.assertEqual(response["error_code"], "VALIDATION_FAILED") self.assertEqual(response["message"], "缺少字段") self.assertEqual(response["details"], {"field": "name"}) self.assertEqual(response["request_id"], "req_test") class ConfigWriterTests(unittest.TestCase): def test_config_lock_preserves_frozen_api_error(self): with tempfile.TemporaryDirectory() as tmp: path = Path(tmp) / "config.yaml" with self.assertRaises(ApiError) as raised: with config_file_lock(path): raise ApiError(ErrorCode.VALIDATION_FAILED, "invalid config", {"field": "database"}) self.assertEqual(raised.exception.error_code, ErrorCode.VALIDATION_FAILED) class DatabaseSettingsTests(unittest.TestCase): def tearDown(self): set_runtime_config_overrides({}) def test_sqlite_is_default_backend(self): settings = DatabaseSettings() self.assertEqual(settings.selected_backend, "sqlite") def test_yaml_subset_parses_nested_lists(self): payload = _load_yaml_subset( """ review_policy: version: custom-v2 require_selected_image_for_statuses: - selected workflow_terminal_statuses: - selected - skipped """ ) self.assertEqual(payload["review_policy"]["require_selected_image_for_statuses"], ["selected"]) self.assertEqual(payload["review_policy"]["workflow_terminal_statuses"], ["selected", "skipped"]) def test_yaml_subset_preserves_hashes_inside_quotes(self): payload = _load_yaml_subset( 'quoted: "token#fragment"\n' "single: 'other#fragment'\n" "url: https://example.test/page#section\n" "plain: visible # comment\n" ) self.assertEqual(payload["quoted"], "token#fragment") self.assertEqual(payload["single"], "other#fragment") self.assertEqual(payload["url"], "https://example.test/page#section") self.assertEqual(payload["plain"], "visible") def test_yaml_dump_quotes_ambiguous_string_values(self): dumped = _dump_yaml( { "boolish": "true", "numberish": "123", "fragment": "value#part", "windows_path": r"C:\ImageStudio\data", } ) self.assertEqual( _load_yaml_subset(dumped), { "boolish": "true", "numberish": "123", "fragment": "value#part", "windows_path": r"C:\ImageStudio\data", }, ) def test_runtime_overrides_apply_to_default_config_only(self): set_runtime_config_overrides( { "database": { "backend": "postgresql", "postgresql": { "enabled": True, "host": "127.0.0.1", "database": "imagestudio_test", "user": "imagestudio", }, } } ) settings = load_database_settings() self.assertEqual(settings.selected_backend, "postgresql") self.assertEqual(settings.postgresql.host, "127.0.0.1") with tempfile.TemporaryDirectory() as tmp: config_path = Path(tmp) / "config.yaml" config_path.write_text( """ database: backend: sqlite sqlite: path: local.db """, encoding="utf-8", ) isolated_settings = load_database_settings(config_path) self.assertEqual(isolated_settings.selected_backend, "sqlite") def test_astrbot_runtime_uses_plugin_data_root_and_writable_config(self): with tempfile.TemporaryDirectory() as tmp: astrbot_plugin_data = Path(tmp) / "plugin_data" runtime_root = astrbot_plugin_data / PLUGIN_NAME packaged_before = (REPO_ROOT / "config.yaml").read_text(encoding="utf-8") with patch( "core.runtime_paths._astrbot_plugin_data_path_getter", return_value=lambda: astrbot_plugin_data, ): self.assertEqual(resolve_data_path("data/images"), runtime_root / "images") self.assertEqual(resolve_data_path("custom/cache"), runtime_root / "custom" / "cache") self.assertEqual(get_default_config_path(), runtime_root / "config.yaml") config_service = ConfigService() SettingsService(config_service).save( {"config": {"plugin": {"display_name": "AstrBot Runtime"}}}, RequestContext.from_astrbot(username="admin", user_id="admin"), ) self.assertEqual(config_service.config_path, runtime_root / "config.yaml") self.assertTrue(config_service.config_path.exists()) self.assertIn("AstrBot Runtime", config_service.config_path.read_text(encoding="utf-8")) self.assertEqual(list(runtime_root.glob("*.tmp")), []) self.assertEqual((REPO_ROOT / "config.yaml").read_text(encoding="utf-8"), packaged_before) def test_absolute_data_and_config_paths_are_unchanged(self): with tempfile.TemporaryDirectory() as tmp: absolute = Path(tmp) / "explicit" with patch( "core.runtime_paths._astrbot_plugin_data_path_getter", return_value=lambda: Path(tmp) / "plugin_data", ): self.assertEqual(resolve_data_path(absolute), absolute) self.assertEqual(ConfigService(absolute / "config.yaml").config_path, absolute / "config.yaml") def test_settings_save_initializes_a_new_sqlite_database_before_switch(self): with tempfile.TemporaryDirectory() as tmp: config_path = Path(tmp) / "config.yaml" first_db = Path(tmp) / "first.db" next_db = Path(tmp) / "next.db" config_path.write_text( f'database:\n backend: sqlite\n sqlite:\n path: "{first_db.as_posix()}"\n', encoding="utf-8", ) Database(DatabaseSettings(sqlite=SqliteSettings(path=str(first_db)))).initialize() service = SettingsService(ConfigService(config_path)) service.save( { "config": { "database": { "backend": "sqlite", "sqlite": {"path": str(next_db)}, } } }, RequestContext.from_astrbot(username="admin", user_id="admin"), ) self.assertTrue(next_db.exists()) conn = Database(DatabaseSettings(sqlite=SqliteSettings(path=str(next_db)))).connect() try: table = conn.execute( "SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'character_profiles'" ).fetchone() finally: conn.close() self.assertIsNotNone(table) def test_settings_save_rejects_astrbot_runtime_managed_fields(self): with tempfile.TemporaryDirectory() as tmp: config_path = Path(tmp) / "config.yaml" config_path.write_text( "database:\n backend: sqlite\n sqlite:\n path: local.db\n", encoding="utf-8", ) set_runtime_config_overrides({"database": {"sqlite": {"path": "runtime.db"}}}) service = SettingsService(ConfigService(config_path)) with patch("backend.services.settings_service.is_default_config_path", return_value=True): with self.assertRaises(ApiError) as raised: service.save( {"config": {"database": {"sqlite": {"path": "page.db"}}}}, RequestContext.from_astrbot(username="admin", user_id="admin"), ) self.assertEqual(raised.exception.error_code, ErrorCode.VALIDATION_FAILED) self.assertEqual( raised.exception.details["runtime_managed_fields"], ["database.sqlite.path"], ) def test_settings_save_accepts_masked_runtime_field_without_persisting_override(self): with tempfile.TemporaryDirectory() as tmp: config_path = Path(tmp) / "config.yaml" config_path.write_text( "plugin:\n name: astrbot_plugin_imagestudio\n display_name: ImageStudio\n", encoding="utf-8", ) set_runtime_config_overrides( {"generation": {"provider": {"api_key_env_var": "IMAGESTUDIO_RUNTIME_KEY"}}} ) service = SettingsService(ConfigService(config_path)) with patch("database.db_helper._should_apply_runtime_overrides", return_value=True), patch( "backend.services.settings_service.is_default_config_path", return_value=True, ): current = service.get_current()["config"] current["plugin"]["display_name"] = "Renamed Studio" saved = service.save( {"config": current}, RequestContext.from_astrbot(username="admin", user_id="admin"), ) self.assertTrue(saved["saved"]) self.assertEqual(saved["config"]["plugin"]["display_name"], "Renamed Studio") written = config_path.read_text(encoding="utf-8") self.assertNotIn("IMAGESTUDIO_RUNTIME_KEY", written) self.assertNotIn("***MASKED***", written) def test_settings_rollback_does_not_snapshot_or_persist_runtime_override(self): with tempfile.TemporaryDirectory() as tmp: root = Path(tmp) config_path = root / "config.yaml" config_path.write_text( "plugin:\n name: astrbot_plugin_imagestudio\n display_name: Snapshot Target\n", encoding="utf-8", ) database = Database(DatabaseSettings(sqlite=SqliteSettings(path=str(root / "settings.db")))) database.initialize() service = SettingsService( ConfigService(config_path), audit_service=AuditService(database), ) ctx = RequestContext.from_astrbot(username="admin", user_id="admin") snapshot_id = service._record_snapshot(load_config_file_payload(config_path), ctx, "manual") config_path.write_text( "plugin:\n name: astrbot_plugin_imagestudio\n display_name: Current Name\n", encoding="utf-8", ) set_runtime_config_overrides( {"generation": {"provider": {"api_key_env_var": "IMAGESTUDIO_RUNTIME_SECRET_NAME"}}} ) with patch("database.db_helper._should_apply_runtime_overrides", return_value=True), patch( "backend.services.settings_service.is_default_config_path", return_value=True, ): result = service.rollback({"snapshot_id": snapshot_id}, ctx) self.assertTrue(result["rolled_back"]) written = config_path.read_text(encoding="utf-8") self.assertIn("Snapshot Target", written) self.assertNotIn("IMAGESTUDIO_RUNTIME_SECRET_NAME", written) conn = database.connect() try: snapshots = conn.execute( "SELECT config_json FROM config_snapshots WHERE reason = ?", ("before_rollback",), ).fetchall() finally: conn.close() self.assertTrue(snapshots) self.assertNotIn("IMAGESTUDIO_RUNTIME_SECRET_NAME", str(snapshots)) def test_settings_diff_treats_masked_sensitive_values_as_unchanged(self): with tempfile.TemporaryDirectory() as tmp: config_path = Path(tmp) / "config.yaml" config_path.write_text( "database:\n backend: postgresql\n postgresql:\n enabled: true\n dsn: postgresql://user:secret@example/db\n", encoding="utf-8", ) service = SettingsService(ConfigService(config_path)) current = service.get_current()["config"] result = service.diff({"config": current}) self.assertFalse(result["has_changes"]) self.assertEqual(result["changes"], []) class DatabaseMetadataTests(unittest.TestCase): def test_sqlite_connections_enforce_foreign_keys(self): with tempfile.TemporaryDirectory() as tmp: database = Database(DatabaseSettings(sqlite=SqliteSettings(path=str(Path(tmp) / "db.sqlite")))) conn = database.connect() try: enabled = conn.execute("PRAGMA foreign_keys").fetchone()[0] finally: conn.close() self.assertEqual(enabled, 1) def test_table_spec_exposes_known_tables(self): workflow_nodes = table_spec("workflow_nodes") self.assertIsNotNone(workflow_nodes) self.assertEqual(workflow_nodes.primary_key, "node_id") self.assertIn("status", workflow_nodes.column_names()) self.assertEqual(table_spec("config_snapshots").sensitive_columns(), ["config_json"]) def test_seed_default_material_categories_is_idempotent(self): with tempfile.TemporaryDirectory() as tmp: db_path = Path(tmp) / "imagestudio.db" database = Database(DatabaseSettings(sqlite=SqliteSettings(path=str(db_path)))) database.initialize() first = seed_default_material_categories(database) second = seed_default_material_categories(database) self.assertEqual(first["total"], len(DEFAULT_MATERIAL_CATEGORIES)) self.assertEqual(second["total"], 0) conn = database.connect() try: count = conn.execute("SELECT COUNT(*) AS total FROM material_categories").fetchone()["total"] accessory = conn.execute( """ SELECT parent_id, category_type FROM material_categories WHERE category_id = 'cat_imagestudio_accessory_头部' """ ).fetchone() finally: conn.close() self.assertEqual(count, len(DEFAULT_MATERIAL_CATEGORIES)) self.assertEqual(accessory["parent_id"], "cat_imagestudio_配饰") self.assertEqual(accessory["category_type"], "accessory_group") def test_postgres_is_selected_when_configured(self): settings = DatabaseSettings( backend="auto", postgresql=PostgresSettings( enabled=True, host="localhost", database="imagestudio", user="imagestudio", ), ) self.assertEqual(settings.selected_backend, "postgresql") def test_forced_sqlite_ignores_configured_postgres(self): settings = DatabaseSettings( backend="sqlite", postgresql=PostgresSettings(enabled=True, dsn="postgresql://example"), ) self.assertEqual(settings.selected_backend, "sqlite") def test_postgres_migration_runner_executes_split_statements(self): class FakeConnection: def __init__(self): self.statements = [] def execute(self, statement): self.statements.append(statement) database = Database( DatabaseSettings( backend="postgresql", postgresql=PostgresSettings(enabled=True, dsn="postgresql://example"), ) ) conn = FakeConnection() database._execute_script( conn, """ CREATE TABLE IF NOT EXISTS demo (id TEXT PRIMARY KEY); CREATE INDEX IF NOT EXISTS idx_demo_id ON demo(id); """, ) self.assertEqual(len(conn.statements), 2) self.assertTrue(conn.statements[0].startswith("CREATE TABLE")) self.assertTrue(conn.statements[1].startswith("CREATE INDEX")) def test_psycopg2_adapter_exposes_connection_execute_shape(self): class FakeCursor: def __init__(self): self.calls = [] def execute(self, query, params): self.calls.append((query, params)) def fetchone(self): return {"ok": True} class FakeConnection: def __init__(self): self.cursor_instance = FakeCursor() self.committed = False self.closed = False def cursor(self): return self.cursor_instance def commit(self): self.committed = True def close(self): self.closed = True raw = FakeConnection() adapter = _Psycopg2ConnectionAdapter(raw) cursor = adapter.execute("SELECT * FROM demo WHERE id = %s", ("demo_1",)) adapter.commit() adapter.close() self.assertEqual(cursor.fetchone(), {"ok": True}) self.assertEqual(raw.cursor_instance.calls, [("SELECT * FROM demo WHERE id = %s", ("demo_1",))]) self.assertTrue(raw.committed) self.assertTrue(raw.closed) class AuditServiceTests(unittest.TestCase): def test_record_writes_audit_log_to_sqlite(self): with tempfile.TemporaryDirectory() as tmp: db_path = Path(tmp) / "imagestudio.db" settings = DatabaseSettings(sqlite=SqliteSettings(path=str(db_path))) database = Database(settings) database.initialize() ctx = RequestContext.system() record = AuditService(database).record( ctx=ctx, action="unit_test", target_type="test_target", target_id="target_001", after_snapshot={"ok": True}, ) conn = database.connect() try: row = conn.execute( "SELECT action, target_type, target_id FROM audit_logs WHERE audit_id = ?", (record.audit_id,), ).fetchone() finally: conn.close() self.assertIsNotNone(row) self.assertEqual(row["action"], "unit_test") self.assertEqual(row["target_type"], "test_target") self.assertEqual(row["target_id"], "target_001") if __name__ == "__main__": unittest.main()