Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion astrbot/core/utils/t2i/network_strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,6 @@ async def render(
text_base64 = base64.b64encode(text.encode("utf-8")).decode("ascii")
return await self.render_custom_template(
tmpl_str,
{"text_base64": text_base64, "version": f"v{VERSION}"},
{"text_base64": text_base64, "text": text, "version": f"v{VERSION}"},
return_url,
)
44 changes: 44 additions & 0 deletions tests/unit/test_t2i_network_strategy.py
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")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The instantiation of NetworkRenderStrategy triggers the TemplateManager constructor, 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 mocking TemplateManager or the NetworkRenderStrategy.__init__ method to prevent these side effects.

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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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
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")
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(
"hello 你好".encode("utf-8")
).decode("ascii")
References
  1. New functionality should be accompanied by corresponding unit tests to ensure correctness across different input types, such as non-ASCII characters.

assert captured["tmpl_data"]["version"] == f"v{VERSION}"
Loading