Repository navigation
feat: add vNext engine support #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -641,3 +641,180 @@ async def test_full_localization_workflow(self, mock_post): | |
| assert request_data["locale"]["target"] == "es" | ||
| assert request_data["params"]["fast"] is True | ||
| assert request_data["data"] == {"greeting": "hello", "farewell": "goodbye"} | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| class TestVNextEngine: | ||
| """Test vNext / Engine ID specific behavior""" | ||
|
|
||
| def setup_method(self): | ||
| """Set up test fixtures""" | ||
| self.config = {"api_key": "test_api_key", "engine_id": "my-engine-id"} | ||
| self.engine = LingoDotDevEngine(self.config) | ||
|
|
||
|
AndreyHirsa marked this conversation as resolved.
|
||
| def teardown_method(self): | ||
| """Clean up engine client""" | ||
| if self.engine._client and not self.engine._client.is_closed: | ||
| asyncio.get_event_loop().run_until_complete(self.engine.close()) | ||
|
|
||
| def test_engine_id_empty_string_treated_as_none(self): | ||
| """Test that empty engine_id is treated as None""" | ||
| engine = LingoDotDevEngine({"api_key": "key", "engine_id": ""}) | ||
| assert engine.config.engine_id is None | ||
| assert engine._is_vnext is False | ||
| assert engine.config.api_url == "https://engine.lingo.dev" | ||
|
|
||
| def test_engine_id_whitespace_treated_as_none(self): | ||
| """Test that whitespace-only engine_id is treated as None""" | ||
| engine = LingoDotDevEngine({"api_key": "key", "engine_id": " "}) | ||
| assert engine.config.engine_id is None | ||
| assert engine._is_vnext is False | ||
| assert engine.config.api_url == "https://engine.lingo.dev" | ||
|
|
||
| def test_engine_id_stripped(self): | ||
| """Test that engine_id is stripped of whitespace""" | ||
| engine = LingoDotDevEngine({"api_key": "key", "engine_id": " eng_123 "}) | ||
| assert engine.config.engine_id == "eng_123" | ||
| assert engine._is_vnext is True | ||
|
|
||
| def test_api_url_trailing_slash_stripped(self): | ||
| """Test that trailing slash is stripped from api_url""" | ||
| engine = LingoDotDevEngine( | ||
| { | ||
| "api_key": "key", | ||
| "engine_id": "eng", | ||
| "api_url": "https://custom.api.com/", | ||
| } | ||
| ) | ||
| assert engine.config.api_url == "https://custom.api.com" | ||
|
|
||
| def test_engine_id_default_api_url(self): | ||
| """Test that engine_id switches default api_url to api.lingo.dev""" | ||
| assert self.engine.config.api_url == "https://api.lingo.dev" | ||
| assert self.engine.config.engine_id == "my-engine-id" | ||
|
|
||
| def test_engine_id_with_explicit_api_url(self): | ||
| """Test that explicit api_url is preserved with engine_id""" | ||
| engine = LingoDotDevEngine( | ||
| { | ||
| "api_key": "key", | ||
| "engine_id": "eng", | ||
| "api_url": "https://custom.api.com", | ||
| } | ||
| ) | ||
| assert engine.config.api_url == "https://custom.api.com" | ||
|
|
||
| def test_is_vnext_true(self): | ||
| """Test _is_vnext is True with engine_id""" | ||
| assert self.engine._is_vnext is True | ||
|
|
||
| def test_is_vnext_false_without_engine_id(self): | ||
| """Test _is_vnext is False without engine_id""" | ||
| engine = LingoDotDevEngine( | ||
| {"api_key": "key", "api_url": "https://api.test.com"} | ||
| ) | ||
| assert engine._is_vnext is False | ||
|
|
||
| def test_session_id_generated(self): | ||
| """Test that session_id is generated on init""" | ||
| assert self.engine._session_id | ||
| assert isinstance(self.engine._session_id, str) | ||
|
|
||
| async def test_vnext_ensure_client_uses_x_api_key(self): | ||
| """Test that vNext engine uses X-API-Key header""" | ||
| await self.engine._ensure_client() | ||
| assert self.engine._client is not None | ||
| assert self.engine._client.headers.get("x-api-key") == "test_api_key" | ||
| assert "authorization" not in self.engine._client.headers | ||
| await self.engine.close() | ||
|
|
||
| async def test_classic_ensure_client_uses_bearer(self): | ||
| """Test that classic engine uses Bearer auth header""" | ||
| engine = LingoDotDevEngine( | ||
| {"api_key": "test_key", "api_url": "https://api.test.com"} | ||
| ) | ||
| await engine._ensure_client() | ||
| assert engine._client is not None | ||
| assert engine._client.headers.get("authorization") == "Bearer test_key" | ||
| assert "x-api-key" not in engine._client.headers | ||
| await engine.close() | ||
|
|
||
| @patch("lingodotdev.engine.httpx.AsyncClient.post") | ||
| async def test_vnext_localize_chunk_url_and_body(self, mock_post): | ||
| """Test vNext localize chunk uses correct URL and body format""" | ||
| mock_response = Mock() | ||
| mock_response.is_success = True | ||
| mock_response.json.return_value = {"data": {"key": "translated"}} | ||
| mock_post.return_value = mock_response | ||
|
|
||
| await self.engine._localize_chunk( | ||
| "en", | ||
| "es", | ||
| {"data": {"key": "value"}, "reference": {"es": {"key": "ref"}}}, | ||
| "wf", | ||
| True, | ||
| ) | ||
|
|
||
| call_args = mock_post.call_args | ||
| url = call_args[0][0] | ||
| assert url == "https://api.lingo.dev/process/my-engine-id/localize" | ||
|
|
||
| body = call_args[1]["json"] | ||
| assert body["sourceLocale"] == "en" | ||
| assert body["targetLocale"] == "es" | ||
| assert body["params"] == {"fast": True} | ||
| assert body["data"] == {"key": "value"} | ||
| assert body["sessionId"] == self.engine._session_id | ||
| assert body["reference"] == {"es": {"key": "ref"}} | ||
|
|
||
| @patch("lingodotdev.engine.httpx.AsyncClient.post") | ||
| async def test_vnext_recognize_locale_url(self, mock_post): | ||
| """Test vNext recognize_locale uses correct URL""" | ||
| mock_response = Mock() | ||
| mock_response.is_success = True | ||
| mock_response.json.return_value = {"locale": "es"} | ||
| mock_post.return_value = mock_response | ||
|
|
||
| await self.engine.recognize_locale("Hola mundo") | ||
|
|
||
| url = mock_post.call_args[0][0] | ||
| assert url == "https://api.lingo.dev/process/recognize" | ||
|
|
||
| @patch("lingodotdev.engine.httpx.AsyncClient.get") | ||
| async def test_vnext_whoami(self, mock_get): | ||
| """Test vNext whoami calls GET /users/me""" | ||
| mock_response = Mock() | ||
| mock_response.is_success = True | ||
| mock_response.json.return_value = {"id": "usr_abc", "email": "[email protected]"} | ||
| mock_get.return_value = mock_response | ||
|
|
||
| result = await self.engine.whoami() | ||
|
|
||
| assert result == {"email": "[email protected]", "id": "usr_abc"} | ||
| url = mock_get.call_args[0][0] | ||
| assert url == "https://api.lingo.dev/users/me" | ||
|
|
||
| @patch("lingodotdev.engine.httpx.AsyncClient.post") | ||
| async def test_vnext_full_localization_workflow(self, mock_post): | ||
| """Test full vNext localization workflow via localize_object""" | ||
| mock_response = Mock() | ||
| mock_response.is_success = True | ||
| mock_response.json.return_value = {"data": {"greeting": "hola"}} | ||
| mock_post.return_value = mock_response | ||
|
|
||
| result = await self.engine.localize_object( | ||
| {"greeting": "hello"}, | ||
| {"source_locale": "en", "target_locale": "es", "fast": True}, | ||
| ) | ||
|
|
||
| assert result == {"greeting": "hola"} | ||
|
|
||
| call_args = mock_post.call_args | ||
| url = call_args[0][0] | ||
| assert url == "https://api.lingo.dev/process/my-engine-id/localize" | ||
|
|
||
| body = call_args[1]["json"] | ||
| assert body["sourceLocale"] == "en" | ||
| assert body["targetLocale"] == "es" | ||
| assert "sessionId" in body | ||
| assert "locale" not in body # classic format should NOT be present | ||
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.
Uh oh!
There was an error while loading. Please reload this page.