1+ #!/usr/bin/env python3
2+ import pytest
3+
4+ from utils import ServerPreset , ServerProcess
5+
6+ server : ServerProcess
7+
8+
9+ @pytest .fixture (autouse = True )
10+ def create_server ():
11+ global server
12+ server = ServerPreset .tinyllama2 ()
13+ server .jinja = True
14+ server .reasoning_budget = 1
15+ server .chat_template_file = "../../../models/templates/deepseek-ai-DeepSeek-R1-Distill-Qwen-32B.jinja"
16+ server .reasoning_force_close_message = "!!!ABORT REASONING"
17+ server .start ()
18+ yield
19+ server .stop ()
20+
21+
22+ def test_reasoning_budget_forces_close ():
23+ """Ensure reasoning budget triggers forced close injection with end tag."""
24+ res = server .make_request ("POST" , "/v1/chat/completions" , data = {
25+ "model" : server .model_alias or "test" ,
26+ "messages" : [
27+ {"role" : "user" , "content" : "Tell me a short story." },
28+ ],
29+ "max_tokens" : 32 ,
30+ })
31+
32+ assert res .status_code == 200
33+ body = res .body
34+ assert "choices" in body and body ["choices" ], "no choices returned"
35+
36+ message = body ["choices" ][0 ]["message" ]
37+ reasoning_content = message .get ("reasoning_content" , "" )
38+
39+ assert server .reasoning_force_close_message in reasoning_content , "reasoning force close message not found in reasoning content"
40+
41+ def test_reasoning_custom_budget ():
42+ """Ensure reasoning budget triggers forced close injection with end tag."""
43+ res = server .make_request ("POST" , "/v1/chat/completions" , data = {
44+ "model" : server .model_alias or "test" ,
45+ "messages" : [
46+ {"role" : "user" , "content" : "Tell me a short story." },
47+ ],
48+ "max_tokens" : 32 ,
49+ "thinking_budget_tokens" : 5
50+ })
51+
52+ assert res .status_code == 200
53+ body = res .body
54+ assert "choices" in body and body ["choices" ], "no choices returned"
55+
56+ message = body ["choices" ][0 ]["message" ]
57+ reasoning_content = message .get ("reasoning_content" , "" )
58+
59+ reasoning_before_abort = reasoning_content .split (server .reasoning_force_close_message )[0 ]
60+ assert len (reasoning_before_abort .split ()) > 1 , "reasoning content too short before force close"
61+
62+ assert server .reasoning_force_close_message in reasoning_content , "reasoning force close message not found in reasoning content"
0 commit comments