mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-02 06:39:28 +08:00
decode_escaped_unicode_for_display repairs double-encoded \uXXXX runs (#973) with the unicode_escape codec, which turns a surrogate-pair escape for an emoji into two lone surrogates instead of one emoji. The result cannot be encoded as UTF-8, so an ask_user card or mastery question whose escaped text contains an emoji fails when the event is persisted to SQLite or sent over the WebSocket. Decode each run as a JSON string instead, matching the web decoder's JSON.parse, and leave runs with an unpaired surrogate escaped.
33 lines
1.1 KiB
Python
33 lines
1.1 KiB
Python
"""Tests for learner-facing unicode escape decoding (#973)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from deeptutor.utils.text_display import decode_escaped_unicode_for_display
|
|
|
|
|
|
def test_decodes_dense_non_ascii_runs() -> None:
|
|
escaped = "\\u300c\\u6570\\u5236\\u8f6c\\u6362\\u300d"
|
|
assert decode_escaped_unicode_for_display(escaped) == "「数制转换」"
|
|
|
|
|
|
def test_leaves_short_ascii_runs_alone() -> None:
|
|
text = "A JSON string can encode A as \\u0041."
|
|
assert decode_escaped_unicode_for_display(text) == text
|
|
|
|
|
|
def test_empty_and_plain_text_passthrough() -> None:
|
|
assert decode_escaped_unicode_for_display("") == ""
|
|
assert decode_escaped_unicode_for_display("hello") == "hello"
|
|
|
|
|
|
def test_joins_surrogate_pairs_into_one_character() -> None:
|
|
# ``json.dumps`` escapes characters outside the BMP as surrogate pairs.
|
|
escaped = "\\u597d\\u7684\\ud83d\\udc4d"
|
|
decoded = decode_escaped_unicode_for_display(escaped)
|
|
assert decoded == "好的👍"
|
|
|
|
|
|
def test_leaves_runs_with_unpaired_surrogates_escaped() -> None:
|
|
text = "\\u4f60\\u597d\\ud83d"
|
|
assert decode_escaped_unicode_for_display(text) == text
|