mirror of
https://github.com/Stirling-Tools/Stirling-PDF.git
synced 2026-09-02 21:03:34 +03:00
# Description of Changes Pass a user browser lang ID to engine <img width="1400" height="900" alt="image" src="https://github.com/user-attachments/assets/7e8fc5c2-8881-4a74-b718-7f5cd350d457" /> --- ## Checklist ### General - [ ] I have read the [Contribution Guidelines](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/CONTRIBUTING.md) - [ ] I have read the [Stirling-PDF Developer Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/DeveloperGuide.md) (if applicable) - [ ] I have read the [How to add new languages to Stirling-PDF](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/HowToAddNewLanguage.md) (if applicable) - [ ] I have performed a self-review of my own code - [ ] Every comment I added says something the code does not ([guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/CODE_COMMENTS.md)) - [ ] My changes generate no new warnings ### Documentation - [ ] I have updated relevant docs on [Stirling-PDF's doc repo](https://github.com/Stirling-Tools/Stirling-Tools.github.io/blob/main/docs/) (if functionality has heavily changed) - [ ] I have read the section [Add New Translation Tags](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/HowToAddNewLanguage.md#add-new-translation-tags) (for new translation tags only) ### Translations (if applicable) - [ ] I ran [`scripts/counter_translation.py`](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/docs/counter_translation.md) ### UI Changes (if applicable) - [ ] Screenshots or videos demonstrating the UI changes are attached (e.g., as comments or direct attachments in the PR) ### Testing (if applicable) - [ ] I have run `task check` to verify linters, typechecks, and tests pass - [ ] I have tested my changes locally. Refer to the [Testing Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/DeveloperGuide.md#7-testing) for more details.
67 lines
2.1 KiB
Python
67 lines
2.1 KiB
Python
from __future__ import annotations
|
|
|
|
from collections.abc import Iterator
|
|
from typing import Any, cast
|
|
|
|
import pytest
|
|
|
|
from stirling.agents import OrchestratorAgent
|
|
from stirling.agents.pdf_questions import PdfQuestionAgent
|
|
from stirling.contracts import (
|
|
OrchestratorRequest,
|
|
PdfQuestionAnswerResponse,
|
|
PdfQuestionRequest,
|
|
SupportedCapability,
|
|
)
|
|
from stirling.services import language_directive, set_reply_locale
|
|
from stirling.services.runtime import AppRuntime
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
def reset_locale() -> Iterator[None]:
|
|
set_reply_locale(None)
|
|
yield
|
|
set_reply_locale(None)
|
|
|
|
|
|
def test_directive_falls_back_to_the_message_language() -> None:
|
|
assert "same language as their message" in language_directive()
|
|
|
|
|
|
def test_directive_pins_the_bound_locale() -> None:
|
|
set_reply_locale("fr-FR")
|
|
assert "'fr-FR'" in language_directive()
|
|
|
|
|
|
def test_orchestrator_request_carries_the_locale() -> None:
|
|
assert OrchestratorRequest.model_validate({"userMessage": "hi", "locale": "de-DE"}).locale == "de-DE"
|
|
assert OrchestratorRequest.model_validate({"userMessage": "hi"}).locale is None
|
|
|
|
|
|
def test_question_prompt_carries_the_directive() -> None:
|
|
set_reply_locale("es-ES")
|
|
# _build_prompt ignores self, so call it off the class.
|
|
prompt = PdfQuestionAgent._build_prompt(cast(Any, None), PdfQuestionRequest(question="¿Cuántas páginas?"))
|
|
assert "'es-ES'" in prompt
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_handle_binds_the_locale_for_delegates(runtime: AppRuntime, monkeypatch: pytest.MonkeyPatch) -> None:
|
|
"""The resume path reaches a delegate with the request's locale already bound."""
|
|
agent = OrchestratorAgent(runtime)
|
|
seen: list[str] = []
|
|
|
|
async def capture(request: OrchestratorRequest) -> PdfQuestionAnswerResponse:
|
|
seen.append(language_directive())
|
|
return PdfQuestionAnswerResponse(answer="ok")
|
|
|
|
monkeypatch.setattr(agent, "_run_pdf_question", capture)
|
|
await agent.handle(
|
|
OrchestratorRequest(
|
|
user_message="Combien de pages ?",
|
|
locale="fr-FR",
|
|
resume_with=SupportedCapability.PDF_QUESTION,
|
|
)
|
|
)
|
|
assert "'fr-FR'" in seen[0]
|