import json import os import stat import subprocess from pathlib import Path import pytest ROOT = Path(__file__).parents[1] def test_entrypoint_loads_secrets_writes_tea_login_and_executes_command( tmp_path: Path, ) -> None: password = tmp_path / "opencode-password" password.write_text("server-secret") token = tmp_path / "gitea-token" token.write_text("gitea-secret\n") config_home = tmp_path / "config" environment = { **os.environ, "OPENCODE_SERVER_PASSWORD_FILE": str(password), "AGENTCI_GITEA_TOKEN_FILE": str(token), "AGENTCI_TEA_CONFIG_HOME": str(config_home), "AGENTCI_GITEA_URL": "https://gitea.example", } result = subprocess.run( [ "/bin/sh", str(ROOT / "scripts" / "entrypoint.sh"), "/bin/sh", "-c", 'printf "%s\\n" "$OPENCODE_SERVER_PASSWORD"; printf "arg=%s\\n" "$@"', "child", "first", "second value", ], env=environment, capture_output=True, text=True, timeout=10, check=False, ) assert result.returncode == 0, result.stderr assert result.stdout.splitlines() == [ "server-secret", "arg=first", "arg=second value", ] config_path = config_home / "tea" / "config.yml" assert json.loads(config_path.read_text()) == { "logins": [ { "name": "agentci", "url": "https://gitea.example", "token": "gitea-secret", "default": True, "version_check": False, } ], "preferences": {}, } assert stat.S_IMODE(config_path.stat().st_mode) == 0o600 @pytest.mark.parametrize( ("prompt", "expected"), [ ("Username for 'https://gitea.example':", "agentci-bot\n"), ("Password for 'https://agentci@gitea.example':", "token-secret\n"), ], ) def test_gitea_askpass_selects_credential_for_prompt(prompt: str, expected: str) -> None: result = subprocess.run( ["/bin/sh", str(ROOT / "scripts" / "gitea-askpass.sh"), prompt], env={ "AGENTCI_GIT_USERNAME": "agentci-bot", "AGENTCI_GIT_PASSWORD": "token-secret", }, capture_output=True, text=True, timeout=10, check=False, ) assert result.returncode == 0, result.stderr assert result.stdout == expected