File size: 4,192 Bytes
c453128 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | """Unit tests for hermes_worker._process_think_content.
The function parses streaming <think>β¦</think> tokens and routes them to
separate token vs. thinking callbacks. It must handle split boundaries that
arrive across multiple chunks.
We import the function directly β the module redirects sys.stdout at import
time but that's harmless for pytest (captured by pytest's own capture).
"""
import sys
# Preserve stdout before import so pytest's own capture still works.
_real_stdout = sys.stdout
from agent_gui.hermes_worker import _process_think_content # noqa: E402
sys.stdout = _real_stdout
def _run(chunks: list[str]):
"""Helper: feed chunks through _process_think_content and collect results."""
tokens: list[str] = []
thoughts: list[str] = []
in_thinking = False
partial = ""
for chunk in chunks:
in_thinking, partial = _process_think_content(
chunk,
in_thinking=in_thinking,
partial=partial,
emit_token=tokens.append,
thinking_cb=thoughts.append,
)
return tokens, thoughts, in_thinking, partial
# ββ Basic routing ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def test_plain_text_emits_tokens():
tokens, thoughts, _, _ = _run(["hello world"])
assert "".join(tokens) == "hello world"
assert thoughts == []
def test_think_block_routes_to_thinking():
tokens, thoughts, _, _ = _run(["<think>reasoning here</think>"])
assert thoughts == ["reasoning here"]
assert tokens == []
def test_text_before_and_after_think():
tokens, thoughts, _, _ = _run(["before<think>middle</think>after"])
assert "".join(tokens) == "beforeafter"
assert "".join(thoughts) == "middle"
def test_think_only_no_close_stays_in_thinking():
tokens, thoughts, in_thinking, partial = _run(["<think>still thinking"])
assert in_thinking is True
assert "".join(thoughts) == "still thinking"
# ββ Split boundary handling ββββββββββββββββββββββββββββββββββββββββββββββββββββ
def test_think_open_tag_split_across_chunks():
"""<think> split as '<thi' + 'nk>text</think>' must not emit '<thi' as a token."""
tokens, thoughts, _, _ = _run(["<thi", "nk>text</think>"])
assert "thi" not in "".join(tokens)
assert "".join(thoughts) == "text"
def test_think_open_tag_split_single_char():
tokens, thoughts, _, _ = _run(["prefix<", "think>inside</think>"])
assert "".join(tokens) == "prefix"
assert "".join(thoughts) == "inside"
def test_close_tag_inside_thinking_completes():
tokens, thoughts, in_thinking, _ = _run(["<think>part1", "</think>after"])
assert in_thinking is False
assert "".join(thoughts) == "part1"
assert "".join(tokens) == "after"
def test_multiple_think_blocks():
tokens, thoughts, _, _ = _run(["<think>a</think>x<think>b</think>y"])
assert "".join(tokens) == "xy"
assert "".join(thoughts) == "ab"
def test_empty_think_block():
tokens, thoughts, _, _ = _run(["<think></think>"])
assert thoughts == []
assert tokens == []
def test_no_tags_multi_chunk():
tokens, thoughts, _, _ = _run(["hel", "lo ", "world"])
assert "".join(tokens) == "hello world"
assert thoughts == []
def test_partial_open_tag_at_end_held():
"""A trailing '<thi' with no follow-up shouldn't be emitted as a token yet."""
tokens, thoughts, in_thinking, partial = _run(["prefix<thi"])
# The partial '<thi' should be held in `partial`, not emitted as a token
assert "<thi" not in "".join(tokens)
assert partial == "<thi"
assert in_thinking is False
def test_partial_held_then_resolved_as_normal_text():
"""'<abc' looks like a partial tag start but '<abc>' is not <think>, so emit it."""
tokens, thoughts, _, _ = _run(["prefix<abc", ">suffix"])
full = "".join(tokens)
# '<abc>' is not a <think> tag β should be emitted as plain text
assert "prefix" in full
assert "suffix" in full
|