mirror of
https://github.com/leonvanzyl/autocoder.git
synced 2026-02-02 07:23:35 +00:00
refactor: improve Vertex AI model conversion and add tests
- Rename compute_mode -> convert_model_for_vertex for clarity - Move `import re` to module top-level (stdlib convention) - Use greedy regex quantifier for more readable pattern matching - Restore PEP 8 double blank line between top-level definitions - Add test_client.py with 10 unit tests covering: - Vertex disabled (env unset, "0", empty) - Standard conversions (Opus, Sonnet, Haiku) - Edge cases (already-converted, non-Claude, no date suffix, empty) Follow-up improvements from PR #129 review. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
105
test_client.py
Normal file
105
test_client.py
Normal file
@@ -0,0 +1,105 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Client Utility Tests
|
||||
====================
|
||||
|
||||
Tests for the client module utility functions.
|
||||
Run with: python test_client.py
|
||||
"""
|
||||
|
||||
import os
|
||||
import unittest
|
||||
|
||||
from client import convert_model_for_vertex
|
||||
|
||||
|
||||
class TestConvertModelForVertex(unittest.TestCase):
|
||||
"""Tests for convert_model_for_vertex function."""
|
||||
|
||||
def setUp(self):
|
||||
"""Save original env state."""
|
||||
self._orig_vertex = os.environ.get("CLAUDE_CODE_USE_VERTEX")
|
||||
|
||||
def tearDown(self):
|
||||
"""Restore original env state."""
|
||||
if self._orig_vertex is None:
|
||||
os.environ.pop("CLAUDE_CODE_USE_VERTEX", None)
|
||||
else:
|
||||
os.environ["CLAUDE_CODE_USE_VERTEX"] = self._orig_vertex
|
||||
|
||||
# --- Vertex AI disabled (default) ---
|
||||
|
||||
def test_returns_model_unchanged_when_vertex_disabled(self):
|
||||
os.environ.pop("CLAUDE_CODE_USE_VERTEX", None)
|
||||
self.assertEqual(
|
||||
convert_model_for_vertex("claude-opus-4-5-20251101"),
|
||||
"claude-opus-4-5-20251101",
|
||||
)
|
||||
|
||||
def test_returns_model_unchanged_when_vertex_set_to_zero(self):
|
||||
os.environ["CLAUDE_CODE_USE_VERTEX"] = "0"
|
||||
self.assertEqual(
|
||||
convert_model_for_vertex("claude-opus-4-5-20251101"),
|
||||
"claude-opus-4-5-20251101",
|
||||
)
|
||||
|
||||
def test_returns_model_unchanged_when_vertex_set_to_empty(self):
|
||||
os.environ["CLAUDE_CODE_USE_VERTEX"] = ""
|
||||
self.assertEqual(
|
||||
convert_model_for_vertex("claude-sonnet-4-5-20250929"),
|
||||
"claude-sonnet-4-5-20250929",
|
||||
)
|
||||
|
||||
# --- Vertex AI enabled: standard conversions ---
|
||||
|
||||
def test_converts_opus_model(self):
|
||||
os.environ["CLAUDE_CODE_USE_VERTEX"] = "1"
|
||||
self.assertEqual(
|
||||
convert_model_for_vertex("claude-opus-4-5-20251101"),
|
||||
"claude-opus-4-5@20251101",
|
||||
)
|
||||
|
||||
def test_converts_sonnet_model(self):
|
||||
os.environ["CLAUDE_CODE_USE_VERTEX"] = "1"
|
||||
self.assertEqual(
|
||||
convert_model_for_vertex("claude-sonnet-4-5-20250929"),
|
||||
"claude-sonnet-4-5@20250929",
|
||||
)
|
||||
|
||||
def test_converts_haiku_model(self):
|
||||
os.environ["CLAUDE_CODE_USE_VERTEX"] = "1"
|
||||
self.assertEqual(
|
||||
convert_model_for_vertex("claude-3-5-haiku-20241022"),
|
||||
"claude-3-5-haiku@20241022",
|
||||
)
|
||||
|
||||
# --- Vertex AI enabled: already converted or non-matching ---
|
||||
|
||||
def test_already_vertex_format_unchanged(self):
|
||||
os.environ["CLAUDE_CODE_USE_VERTEX"] = "1"
|
||||
self.assertEqual(
|
||||
convert_model_for_vertex("claude-opus-4-5@20251101"),
|
||||
"claude-opus-4-5@20251101",
|
||||
)
|
||||
|
||||
def test_non_claude_model_unchanged(self):
|
||||
os.environ["CLAUDE_CODE_USE_VERTEX"] = "1"
|
||||
self.assertEqual(
|
||||
convert_model_for_vertex("gpt-4o"),
|
||||
"gpt-4o",
|
||||
)
|
||||
|
||||
def test_model_without_date_suffix_unchanged(self):
|
||||
os.environ["CLAUDE_CODE_USE_VERTEX"] = "1"
|
||||
self.assertEqual(
|
||||
convert_model_for_vertex("claude-opus-4-5"),
|
||||
"claude-opus-4-5",
|
||||
)
|
||||
|
||||
def test_empty_string_unchanged(self):
|
||||
os.environ["CLAUDE_CODE_USE_VERTEX"] = "1"
|
||||
self.assertEqual(convert_model_for_vertex(""), "")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user