from __future__ import annotations from pathlib import Path import pytest from sync_state_machine.config import build_config_from_file from sync_state_machine.config.run_presets import load_overrides_from_file def test_load_overrides_from_yaml_file(tmp_path: Path) -> None: pytest.importorskip("yaml") yaml_file = tmp_path / "profile.yaml" yaml_file.write_text( """ node_types: - company logging: initialize: true suppress_node_logs: true local_datasource: type: jsonl jsonl_dir: ${PROJECT_ROOT}/filtered_datasource/datasource/filtered """.strip(), encoding="utf-8", ) overrides = load_overrides_from_file(yaml_file, project_root=tmp_path) assert overrides["logging"]["suppress_node_logs"] is True assert str(tmp_path) in overrides["local_datasource"]["jsonl_dir"] def test_load_overrides_executes_generic_extensions_bootstraps(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: bootstrap_module = tmp_path / "profile_bootstrap_demo.py" bootstrap_module.write_text( "BOOTSTRAP_CALLED = False\n" "\n" "def register():\n" " global BOOTSTRAP_CALLED\n" " BOOTSTRAP_CALLED = True\n", encoding="utf-8", ) monkeypatch.syspath_prepend(str(tmp_path)) yaml_file = tmp_path / "profile_with_bootstrap.yaml" yaml_file.write_text( """ extensions: bootstraps: - profile_bootstrap_demo:register local_datasource: type: jsonl jsonl_dir: ${PROJECT_ROOT}/filtered_datasource/datasource/filtered remote_datasource: type: jsonl jsonl_dir: ${PROJECT_ROOT}/remote_datasource/datasource persist: db_path: ${PROJECT_ROOT}/runtime/test.db logging: initialize: false """.strip(), encoding="utf-8", ) load_overrides_from_file(yaml_file, project_root=tmp_path) import profile_bootstrap_demo assert profile_bootstrap_demo.BOOTSTRAP_CALLED is True def test_build_config_from_file_uses_registered_bootstraps(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: bootstrap_module = tmp_path / "config_from_file_bootstrap.py" bootstrap_module.write_text( "from pydantic import ConfigDict\n" "from sync_state_machine.config import BaseDataSourceConfig, register_datasource_type\n" "from sync_state_machine.datasource import BaseDataSource\n" "\n" "class FileBackedConfig(BaseDataSourceConfig):\n" " model_config = ConfigDict(extra=\"forbid\", validate_assignment=True)\n" " type: str = \"file_backed\"\n" " namespace: str = \"demo\"\n" "\n" "class FileBackedDatasource(BaseDataSource):\n" " pass\n" "\n" "def build_datasource(config, endpoint_name):\n" " return FileBackedDatasource()\n" "\n" "def register():\n" " register_datasource_type(\n" " \"file_backed\",\n" " config_model=FileBackedConfig,\n" " factory=build_datasource,\n" " replace=True,\n" " )\n", encoding="utf-8", ) monkeypatch.syspath_prepend(str(tmp_path)) profile = tmp_path / "profile.yaml" profile.write_text( """ extensions: bootstraps: - config_from_file_bootstrap:register local_datasource: type: file_backed namespace: local-demo remote_datasource: type: jsonl jsonl_dir: ${PROJECT_ROOT}/remote_datasource/datasource persist: db_path: ${PROJECT_ROOT}/runtime/test.db logging: initialize: false """.strip(), encoding="utf-8", ) config = build_config_from_file(project_root=tmp_path, file_path=profile) assert config.local_datasource.type == "file_backed" assert getattr(config.local_datasource, "namespace") == "local-demo"