-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
fix: restore legacy t2i template variable #7819
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,44 @@ | ||||||||||||||||||||||||||||||
| import base64 | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| import pytest | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| from astrbot.core.config import VERSION | ||||||||||||||||||||||||||||||
| from astrbot.core.utils.t2i.network_strategy import NetworkRenderStrategy | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| @pytest.mark.asyncio | ||||||||||||||||||||||||||||||
| async def test_network_render_strategy_passes_text_and_text_base64(monkeypatch): | ||||||||||||||||||||||||||||||
| captured: dict = {} | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| async def fake_get_template(self, name: str = "base") -> str: | ||||||||||||||||||||||||||||||
| return "<html>{{ text }}</html>" | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| async def fake_render_custom_template( | ||||||||||||||||||||||||||||||
| self, | ||||||||||||||||||||||||||||||
| tmpl_str: str, | ||||||||||||||||||||||||||||||
| tmpl_data: dict, | ||||||||||||||||||||||||||||||
| return_url: bool = True, | ||||||||||||||||||||||||||||||
| options: dict | None = None, | ||||||||||||||||||||||||||||||
| ) -> str: | ||||||||||||||||||||||||||||||
| captured["tmpl_str"] = tmpl_str | ||||||||||||||||||||||||||||||
| captured["tmpl_data"] = tmpl_data | ||||||||||||||||||||||||||||||
| captured["return_url"] = return_url | ||||||||||||||||||||||||||||||
| captured["options"] = options | ||||||||||||||||||||||||||||||
| return "ok" | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| monkeypatch.setattr(NetworkRenderStrategy, "get_template", fake_get_template) | ||||||||||||||||||||||||||||||
| monkeypatch.setattr( | ||||||||||||||||||||||||||||||
| NetworkRenderStrategy, | ||||||||||||||||||||||||||||||
| "render_custom_template", | ||||||||||||||||||||||||||||||
| fake_render_custom_template, | ||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| strategy = NetworkRenderStrategy(base_url="https://example.com") | ||||||||||||||||||||||||||||||
| await strategy.render("hello", return_url=True, template_name="base") | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| assert captured["tmpl_str"] == "<html>{{ text }}</html>" | ||||||||||||||||||||||||||||||
| assert captured["tmpl_data"]["text"] == "hello" | ||||||||||||||||||||||||||||||
| assert captured["tmpl_data"]["text_base64"] == base64.b64encode( | ||||||||||||||||||||||||||||||
| b"hello" | ||||||||||||||||||||||||||||||
| ).decode("ascii") | ||||||||||||||||||||||||||||||
|
Comment on lines
+37
to
+43
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The test currently uses a simple ASCII string ("hello"). Since the implementation explicitly handles UTF-8 encoding before base64 conversion, it is recommended to include non-ASCII characters (e.g., Chinese characters or emojis) in the test case to verify that the encoding logic works correctly for all supported inputs.
Suggested change
References
|
||||||||||||||||||||||||||||||
| assert captured["tmpl_data"]["version"] == f"v{VERSION}" | ||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The instantiation of
NetworkRenderStrategytriggers theTemplateManagerconstructor, which performs filesystem operations (creating directories and copying files) in the data directory. Unit tests should ideally be isolated from the filesystem to ensure they are fast, reproducible, and don't leave side effects in the environment. Consider mockingTemplateManageror theNetworkRenderStrategy.__init__method to prevent these side effects.