lplalbert commented on code in PR #418:
URL: https://github.com/apache/hugegraph-ai/pull/418#discussion_r4528078064


##########
hugegraph-llm/src/tests/config/test_config.py:
##########
@@ -87,3 +86,103 @@ def test_env_path_prefers_explicit_override(self):
         custom_env = Path("/tmp/hugegraph-llm-test.env")
         with patch.dict(environ, {base_config.ENV_PATH_ENV_VAR: 
str(custom_env)}):
             self.assertEqual(Path(base_config.resolve_env_path()), custom_env)
+
+
+_ENV_SCRUB_PREFIXES = (
+    "OPENAI_",
+    "OLLAMA_",
+    "CHAT_",
+    "EXTRACT_",
+    "TEXT2GQL_",
+    "EMBEDDING_",
+    "RERANKER_",
+    "COHERE_",
+    "CO_API_",
+    "HUGEGRAPH_LLM_",
+)
+
+
+def _scrub_config_env(monkeypatch):
+    for name in list(environ):
+        if name.upper().startswith(_ENV_SCRUB_PREFIXES):
+            monkeypatch.delenv(name)
+
+
+def _config_import_env(home: Path) -> dict:
+    env = dict(environ)
+    for name in list(env):
+        if name.upper().startswith(_ENV_SCRUB_PREFIXES):
+            env.pop(name)
+    env["HUGEGRAPH_LLM_ENV_PATH"] = str(home / ".env")
+    env["HUGEGRAPH_LLM_SETTINGS_PATH"] = str(home / "settings.yaml")
+    env["HUGEGRAPH_LLM_PROMPT_CONFIG_PATH"] = str(home / "config_prompt.yaml")
+    return env
+
+
+def _run_config_import(tmp_path: Path, home: Path) -> 
subprocess.CompletedProcess:
+    return subprocess.run(
+        [sys.executable, "-c", "import hugegraph_llm.config"],
+        env=_config_import_env(home),
+        cwd=tmp_path,
+        capture_output=True,
+        text=True,
+        timeout=120,
+    )
+
+
+def test_env_export_round_trip_preserves_typed_fields(tmp_path, monkeypatch):
+    from hugegraph_llm.config.llm_config import LLMConfig
+    from hugegraph_llm.config.models import base_config
+
+    _scrub_config_env(monkeypatch)
+    monkeypatch.setattr(base_config, "env_path", str(tmp_path / ".env"))
+
+    settings = LLMConfig()
+    settings.ollama_chat_port = 12345
+    settings.openai_chat_tokens = 4096
+    settings.extract_use_chat = False
+    settings.hybrid_llm_weights = 0.0
+    settings.generate_env()
+
+    # Assert the exported lines themselves: reloaded falsy values alone do not 
prove
+    # they were written, because LLMConfig derives extract_use_chat from the 
other
+    # extract fields when the variable is absent.
+    exported_lines = (tmp_path / 
".env").read_text(encoding="utf-8").splitlines()
+    assert "OLLAMA_CHAT_PORT='12345'" in exported_lines
+    assert "OPENAI_CHAT_TOKENS='4096'" in exported_lines
+    assert "EXTRACT_USE_CHAT='False'" in exported_lines
+    assert "HYBRID_LLM_WEIGHTS='0.0'" in exported_lines
+
+    reloaded = LLMConfig()
+    assert reloaded.ollama_chat_port == 12345
+    assert isinstance(reloaded.ollama_chat_port, int)
+    assert reloaded.openai_chat_tokens == 4096
+    assert isinstance(reloaded.openai_chat_tokens, int)
+    assert reloaded.extract_use_chat is False
+    assert reloaded.hybrid_llm_weights == 0.0
+    assert isinstance(reloaded.hybrid_llm_weights, float)
+
+
+def test_config_import_creates_no_config_files(tmp_path):
+    home = tmp_path / "config-home"
+    home.mkdir()
+
+    result = _run_config_import(tmp_path, home)
+
+    assert result.returncode == 0, result.stderr
+    assert list(home.iterdir()) == []

Review Comment:
   Fair point - the old FIXME also tracked the import-time write, and dropping 
it lost that.
   
   Done in 4b1fa0c: a narrowed FIXME now sits above the import-purity tests and 
records the remaining gap precisely - importing hugegraph_llm.config still 
creates logs/llm-server.log in the working directory through 
hugegraph_llm.utils.log, while these tests guard config-home only. Making the 
log setup lazy looked like a separate behavior change, so this PR keeps 
tracking instead of fixing.
   
   Validation: config suite 11 passed (with HYBRID_LLM_WEIGHTS=0.5 ambient); 
ruff format/check clean.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to