File size: 11,546 Bytes
ea84b46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
"""CLI wiring tests for --prompt-wording (argparse choices/env default, and
plumbing into SGLangBackend + create_app). uvicorn.run is monkeypatched so
main() never actually binds a socket; SGLangBackend/create_app/NativeTokenizer
are monkeypatched to capture their constructor arguments instead of talking to
a real engine or loading real tokenizer files (transformers is an optional,
not-installed-here dependency for this default venv)."""

import sys

import pytest

import jev_adapter.__main__ as main_module
import jev_adapter.native_tokenizer as native_tokenizer_module
import jev_adapter.server as server_module
import jev_adapter.sglang as sglang_module


class Capture:
    def __init__(self):
        self.backend_kwargs = None
        self.app_kwargs = None

    def fake_backend(self, *args, **kwargs):
        self.backend_kwargs = kwargs
        return object()

    def fake_create_app(self, backend, **kwargs):
        self.app_kwargs = kwargs
        return object()


@pytest.fixture
def capture(monkeypatch):
    capture = Capture()
    monkeypatch.setattr(sglang_module, "SGLangBackend", capture.fake_backend)
    monkeypatch.setattr(server_module, "create_app", capture.fake_create_app)
    monkeypatch.setattr("uvicorn.run", lambda app, **kwargs: None)
    return capture


def run_main(monkeypatch, argv, env=None):
    monkeypatch.setattr(sys, "argv", ["jev-adapter", *argv])
    for key in (
        "JEV_PROMPT_WORDING",
        "JEV_DEFAULT_TEMPERATURE",
        "JEV_NATIVE_SYSTEM_PROMPT",
    ):
        monkeypatch.delenv(key, raising=False)
    for key, value in (env or {}).items():
        monkeypatch.setenv(key, value)
    main_module.main()


def test_prompt_wording_defaults_to_served(monkeypatch, capture):
    run_main(monkeypatch, ["--model", "decision-model"])
    assert capture.backend_kwargs["prompt_wording"] == "served"
    assert capture.backend_kwargs["native_system_prompt"] is None
    assert capture.app_kwargs["prompt_wording"] == "served"


def test_prompt_wording_flag_selects_native(monkeypatch, capture):
    run_main(monkeypatch, ["--model", "decision-model", "--prompt-wording", "native"])
    assert capture.backend_kwargs["prompt_wording"] == "native"
    assert capture.app_kwargs["prompt_wording"] == "native"


def test_prompt_wording_env_var_sets_the_default(monkeypatch, capture):
    run_main(
        monkeypatch,
        ["--model", "decision-model"],
        env={"JEV_PROMPT_WORDING": "native"},
    )
    assert capture.backend_kwargs["prompt_wording"] == "native"
    assert capture.app_kwargs["prompt_wording"] == "native"


def test_explicit_flag_overrides_env_var(monkeypatch, capture):
    run_main(
        monkeypatch,
        ["--model", "decision-model", "--prompt-wording", "served"],
        env={"JEV_PROMPT_WORDING": "native"},
    )
    assert capture.backend_kwargs["prompt_wording"] == "served"


def test_invalid_prompt_wording_choice_rejected(monkeypatch, capture):
    monkeypatch.setattr(sys, "argv", ["jev-adapter", "--model", "m", "--prompt-wording", "bogus"])
    with pytest.raises(SystemExit):
        main_module.main()


def test_native_wording_without_tokenizer_model_serves_text_only_and_warns(
    monkeypatch, capture, caplog
):
    with caplog.at_level("WARNING"):
        run_main(monkeypatch, ["--model", "decision-model", "--prompt-wording", "native"])
    assert capture.backend_kwargs["native_system_prompt"] is None
    assert any("native" in record.message for record in caplog.records)


def test_native_wording_with_tokenizer_model_extracts_the_system_prompt(
    monkeypatch, capture
):
    calls = []

    class FakeNativeTokenizer:
        @classmethod
        def from_pretrained(cls, model, revision):
            calls.append(("from_pretrained", model, revision))
            return object()

        @classmethod
        def native_default_system_prompt(cls, model, revision):
            calls.append(("native_default_system_prompt", model, revision))
            return "Default system text."

    monkeypatch.setattr(native_tokenizer_module, "NativeTokenizer", FakeNativeTokenizer)
    run_main(
        monkeypatch,
        [
            "--model",
            "decision-model",
            "--prompt-wording",
            "native",
            "--tokenizer-model",
            "mistralai/Ministral-3-8B-Instruct-2512-BF16",
            "--tokenizer-revision",
            "f" * 40,
        ],
    )
    assert calls == [
        ("from_pretrained", "mistralai/Ministral-3-8B-Instruct-2512-BF16", "f" * 40),
        (
            "native_default_system_prompt",
            "mistralai/Ministral-3-8B-Instruct-2512-BF16",
            "f" * 40,
        ),
    ]
    assert capture.backend_kwargs["native_system_prompt"] == "Default system text."


def test_served_wording_with_tokenizer_model_never_extracts_a_system_prompt(
    monkeypatch, capture
):
    calls = []

    class FakeNativeTokenizer:
        @classmethod
        def from_pretrained(cls, model, revision):
            calls.append("from_pretrained")
            return object()

        @classmethod
        def native_default_system_prompt(cls, model, revision):
            calls.append("native_default_system_prompt")
            return "should not be reached"

    monkeypatch.setattr(native_tokenizer_module, "NativeTokenizer", FakeNativeTokenizer)
    run_main(
        monkeypatch,
        [
            "--model",
            "decision-model",
            "--tokenizer-model",
            "org/model",
            "--tokenizer-revision",
            "a" * 40,
        ],
    )
    assert calls == ["from_pretrained"]
    assert capture.backend_kwargs["prompt_wording"] == "served"
    assert capture.backend_kwargs["native_system_prompt"] is None


def test_native_system_prompt_defaults_to_auto(monkeypatch, capture):
    calls = []

    class FakeNativeTokenizer:
        @classmethod
        def from_pretrained(cls, model, revision):
            return object()

        @classmethod
        def native_default_system_prompt(cls, model, revision):
            calls.append((model, revision))
            return "Default system text."

    monkeypatch.setattr(native_tokenizer_module, "NativeTokenizer", FakeNativeTokenizer)
    run_main(
        monkeypatch,
        [
            "--model",
            "decision-model",
            "--prompt-wording",
            "native",
            "--tokenizer-model",
            "org/model",
            "--tokenizer-revision",
            "a" * 40,
        ],
    )
    assert calls == [("org/model", "a" * 40)]
    assert capture.backend_kwargs["native_system_prompt"] == "Default system text."


def test_native_system_prompt_none_skips_extraction_and_warning(
    monkeypatch, capture, caplog
):
    calls = []

    class FakeNativeTokenizer:
        @classmethod
        def from_pretrained(cls, model, revision):
            return object()

        @classmethod
        def native_default_system_prompt(cls, model, revision):
            calls.append((model, revision))
            return "should not be reached"

    monkeypatch.setattr(native_tokenizer_module, "NativeTokenizer", FakeNativeTokenizer)
    with caplog.at_level("WARNING"):
        run_main(
            monkeypatch,
            [
                "--model",
                "decision-model",
                "--prompt-wording",
                "native",
                "--tokenizer-model",
                "org/model",
                "--tokenizer-revision",
                "a" * 40,
                "--native-system-prompt",
                "none",
            ],
        )
    assert calls == []
    assert capture.backend_kwargs["native_system_prompt"] is None
    assert not any("native" in record.message for record in caplog.records)


def test_native_system_prompt_none_without_tokenizer_never_warns(
    monkeypatch, capture, caplog
):
    with caplog.at_level("WARNING"):
        run_main(
            monkeypatch,
            [
                "--model",
                "decision-model",
                "--prompt-wording",
                "native",
                "--native-system-prompt",
                "none",
            ],
        )
    assert capture.backend_kwargs["native_system_prompt"] is None
    assert caplog.records == []


def test_native_system_prompt_env_var_sets_the_default(monkeypatch, capture, caplog):
    with caplog.at_level("WARNING"):
        run_main(
            monkeypatch,
            ["--model", "decision-model", "--prompt-wording", "native"],
            env={"JEV_NATIVE_SYSTEM_PROMPT": "none"},
        )
    assert capture.backend_kwargs["native_system_prompt"] is None
    assert caplog.records == []


def test_native_system_prompt_explicit_flag_overrides_env_var(monkeypatch, capture):
    calls = []

    class FakeNativeTokenizer:
        @classmethod
        def from_pretrained(cls, model, revision):
            return object()

        @classmethod
        def native_default_system_prompt(cls, model, revision):
            calls.append((model, revision))
            return "Default system text."

    monkeypatch.setattr(native_tokenizer_module, "NativeTokenizer", FakeNativeTokenizer)
    run_main(
        monkeypatch,
        [
            "--model",
            "decision-model",
            "--prompt-wording",
            "native",
            "--tokenizer-model",
            "org/model",
            "--tokenizer-revision",
            "a" * 40,
            "--native-system-prompt",
            "auto",
        ],
        env={"JEV_NATIVE_SYSTEM_PROMPT": "none"},
    )
    assert calls == [("org/model", "a" * 40)]
    assert capture.backend_kwargs["native_system_prompt"] == "Default system text."


def test_native_system_prompt_none_has_no_effect_on_served_wording(
    monkeypatch, capture
):
    calls = []

    class FakeNativeTokenizer:
        @classmethod
        def from_pretrained(cls, model, revision):
            return object()

        @classmethod
        def native_default_system_prompt(cls, model, revision):
            calls.append((model, revision))
            return "should not be reached"

    monkeypatch.setattr(native_tokenizer_module, "NativeTokenizer", FakeNativeTokenizer)
    run_main(
        monkeypatch,
        [
            "--model",
            "decision-model",
            "--tokenizer-model",
            "org/model",
            "--tokenizer-revision",
            "a" * 40,
            "--native-system-prompt",
            "auto",
        ],
    )
    assert calls == []
    assert capture.backend_kwargs["prompt_wording"] == "served"
    assert capture.backend_kwargs["native_system_prompt"] is None


def test_invalid_native_system_prompt_choice_rejected(monkeypatch, capture):
    monkeypatch.setattr(
        sys,
        "argv",
        ["jev-adapter", "--model", "m", "--native-system-prompt", "bogus"],
    )
    with pytest.raises(SystemExit):
        main_module.main()


def test_native_system_prompt_startup_log_line(monkeypatch, capture, caplog):
    with caplog.at_level("INFO"):
        run_main(
            monkeypatch,
            [
                "--model",
                "decision-model",
                "--prompt-wording",
                "native",
                "--native-system-prompt",
                "none",
            ],
        )
    info_records = [r for r in caplog.records if r.levelname == "INFO"]
    assert len(info_records) == 1
    message = info_records[0].getMessage()
    assert "prompt_wording=native" in message
    assert "native_system_prompt=none" in message