|
| 1 | +import json |
| 2 | + |
| 3 | +from pydantic import SecretStr |
| 4 | + |
| 5 | +from openhands.sdk.llm.llm import LLM |
| 6 | +from openhands.sdk.llm.llm_registry import LLMRegistry |
| 7 | +from openhands.sdk.llm.profile_manager import ProfileManager |
| 8 | + |
| 9 | + |
| 10 | +def test_list_profiles_returns_sorted_names(tmp_path): |
| 11 | + manager = ProfileManager(base_dir=tmp_path) |
| 12 | + (tmp_path / "b.json").write_text("{}", encoding="utf-8") |
| 13 | + (tmp_path / "a.json").write_text("{}", encoding="utf-8") |
| 14 | + |
| 15 | + assert manager.list_profiles() == ["a", "b"] |
| 16 | + |
| 17 | + |
| 18 | +def test_save_profile_excludes_secret_fields(tmp_path): |
| 19 | + manager = ProfileManager(base_dir=tmp_path) |
| 20 | + llm = LLM( |
| 21 | + model="gpt-4o-mini", |
| 22 | + service_id="service", |
| 23 | + api_key=SecretStr("secret"), |
| 24 | + aws_access_key_id=SecretStr("id"), |
| 25 | + aws_secret_access_key=SecretStr("value"), |
| 26 | + ) |
| 27 | + |
| 28 | + path = manager.save_profile("sample", llm) |
| 29 | + data = json.loads(path.read_text(encoding="utf-8")) |
| 30 | + |
| 31 | + assert data["profile_id"] == "sample" |
| 32 | + assert data["service_id"] == "service" |
| 33 | + assert "api_key" not in data |
| 34 | + assert "aws_access_key_id" not in data |
| 35 | + assert "aws_secret_access_key" not in data |
| 36 | + |
| 37 | + |
| 38 | +def test_load_profile_assigns_profile_id_when_missing(tmp_path): |
| 39 | + manager = ProfileManager(base_dir=tmp_path) |
| 40 | + profile_path = tmp_path / "foo.json" |
| 41 | + profile_path.write_text( |
| 42 | + json.dumps({"model": "gpt-4o-mini", "service_id": "svc"}), |
| 43 | + encoding="utf-8", |
| 44 | + ) |
| 45 | + |
| 46 | + llm = manager.load_profile("foo") |
| 47 | + |
| 48 | + assert llm.profile_id == "foo" |
| 49 | + assert llm.service_id == "svc" |
| 50 | + |
| 51 | + |
| 52 | +def test_register_all_skips_invalid_and_duplicate_profiles(tmp_path): |
| 53 | + manager = ProfileManager(base_dir=tmp_path) |
| 54 | + registry = LLMRegistry() |
| 55 | + |
| 56 | + llm = LLM(model="gpt-4o-mini", service_id="shared") |
| 57 | + manager.save_profile("alpha", llm) |
| 58 | + |
| 59 | + duplicate_data = llm.model_dump(exclude_none=True) |
| 60 | + duplicate_data["profile_id"] = "beta" |
| 61 | + (tmp_path / "beta.json").write_text( |
| 62 | + json.dumps(duplicate_data), |
| 63 | + encoding="utf-8", |
| 64 | + ) |
| 65 | + |
| 66 | + (tmp_path / "gamma.json").write_text("{", encoding="utf-8") |
| 67 | + |
| 68 | + manager.register_all(registry) |
| 69 | + |
| 70 | + assert registry.list_services() == ["shared"] |
0 commit comments