band-aider-pub/tests/band_aider/test_test_environment.py
2026-10-17 11:57:37 -07:00

92 lines
2.6 KiB
Python

# SPDX-FileCopyrightText: 2026 Art Sea LLC
#
# SPDX-License-Identifier: AGPL-3.0-only
from __future__ import annotations
from band_aider.judgement.test_environment import (
TEST_ENV_VARIABLES_TO_CLEAR,
sanitized_test_environment,
)
from tests.support.memory_repo_exec import MemoryRepoExec
class _HostEnvironmentMemoryRepoExec(MemoryRepoExec):
"""Exercise environment policy without reading a host repository or process state."""
@property
def preserves_host_environment(self) -> bool:
"""Request explicit-input filtering while retaining memory-only repository IO."""
return True
def test_sanitized_test_environment_removes_provider_variables_and_preserves_unrelated_values() -> None:
"""Verify the test environment clears only Band-Aider provider configuration."""
source = {
"IONA_MODEL": "default",
"BAND_AIDER_MODEL": "other",
"OPENAI_MODEL": "third",
"PATH": "/bin",
"CUSTOM_TEST_VALUE": "preserved",
}
assert sanitized_test_environment(source, repo_exec=_HostEnvironmentMemoryRepoExec()) == {
"PATH": "/bin",
"CUSTOM_TEST_VALUE": "preserved",
}
def test_sanitized_test_environment_removes_api_base_and_key_aliases() -> None:
"""Verify API base and API key aliases are removed from subprocess test envs."""
source = {
name: f"value-for-{name}"
for name in TEST_ENV_VARIABLES_TO_CLEAR
}
source["PATH"] = "/bin"
source["CUSTOM_TEST_VALUE"] = "preserved"
env = sanitized_test_environment(source, repo_exec=_HostEnvironmentMemoryRepoExec())
assert env == {
"PATH": "/bin",
"CUSTOM_TEST_VALUE": "preserved",
}
def test_sanitized_test_environment_does_not_mutate_input_mapping() -> None:
"""Verify sanitizing a supplied mapping is side-effect free."""
source = {
"IONA_MODEL": "default",
"PATH": "/bin",
"CUSTOM_TEST_VALUE": "preserved",
}
before = dict(source)
sanitized_test_environment(source, repo_exec=_HostEnvironmentMemoryRepoExec())
assert source == before
def test_sanitized_test_environment_does_not_mutate_explicit_environment() -> None:
"""Filtering an explicitly supplied environment leaves its owner unchanged."""
source = {
"IONA_MODEL": "default",
"BAND_AIDER_MODEL": "other",
"CUSTOM_TEST_VALUE": "preserved",
}
before = dict(source)
env = sanitized_test_environment(
source,
repo_exec=_HostEnvironmentMemoryRepoExec(),
)
assert source == before
assert "IONA_MODEL" not in env
assert "BAND_AIDER_MODEL" not in env
assert env["CUSTOM_TEST_VALUE"] == "preserved"