test(setup): cover key validation, env loading and persistence (closes #59) - #81
Merged
shauryagangrade merged 1 commit intoAug 14, 2026
Conversation
…shauryagangrade#59) First-run setup was untested, which is the code every new user meets before anything else works. 21 cases, nothing touching the network. validate_api_key: 200, 401, 403, ConnectionError/Timeout/SSLError, and an unhandled 500 through raise_for_status. One test asserts 401 and 403 do not produce the same string -- the distinction is the point of handling them separately, and a refactor collapsing them would otherwise pass. Another asserts the key actually reaches the Authorization header, since a mocked 200 proves nothing about what was sent. get_api_key: precedence of OPENROUTER over OPENAI, and that a blank OPENROUTER value falls through rather than being returned. load_env / save_api_key: present and absent file, override of a stale variable, directory creation on first run, preservation of whichever variable was already set, and a save-then-load round trip standing in for a restart. An autouse fixture clears both key variables so an inherited one cannot decide a precedence test, and an env_file fixture redirects _ENV_DIR/_ENV_FILE at tmp_path -- without it the suite would overwrite the developer's real ~/.gcode/.env. Test-only; no production change.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #59.
21 cases in
tests/test_setup.py, covering the matrix from the issue. Nothing reaches the network —requests.getis mocked throughout, following the pattern intests/test_models.py.validate_api_key200, 401, 403,
ConnectionError/Timeout/SSLError(parametrised), and an unhandled 500 arriving viaraise_for_status.Two of these are doing more than restating the branch:
test_401_and_403_do_not_share_a_message— distinguishing them is the point of handling them separately, and a refactor that collapsed both into one string would pass every other test here.test_validate_sends_the_key_as_a_bearer_token— a mocked 200 proves nothing about what was sent, so this checks the key reaches theAuthorizationheader (and that the 15s timeout is still passed).get_api_keyEmpty when unset, reads
OPENROUTER_API_KEY, falls back toOPENAI_API_KEY, OpenRouter wins when both are set, and a blankOPENROUTER_API_KEYfalls through rather than being returned — that last one is a real property of theorchain that would be easy to break by switching toos.environ.get(..., default).load_env/save_api_keyFile present and absent (first run must not raise),
override=Truebeating a stale variable, directory creation, preserving whichever variable was already set, and a save-then-load round trip standing in for a restart.Two safety notes
env_filefixture redirects_ENV_DIR/_ENV_FILEattmp_path. Without it,save_api_keywrites to the real~/.gcode/.env— running the suite would overwrite the developer's own key. Worth knowing before adding more tests here.Verification
pytest tests/test_setup.py— 21 passed.ruff checkclean. Test-only; no production change.Full suite on this branch: 73 passed, 2 failed — the 2 are
test_tools.py::test_grep/::test_grep_ignore_case, failing on a Windows checkout ofmainfor an unrelated reason, sent as #78.