File size: 21,315 Bytes
e65cda1 | 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 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 | from __future__ import annotations
import json
import runpy
import subprocess
import urllib.request
from pathlib import Path
from types import SimpleNamespace
import pytest
from deepseek_harness import RunResult
ROOT = Path(__file__).resolve().parents[3]
SMOKE = runpy.run_path(ROOT / "scripts" / "smoke-python-runtime.py")
def live_result(**overrides: object) -> RunResult:
values = {
"session_id": "installed-wheel-live-api",
"final_response": SMOKE["LIVE_API_SENTINEL"],
"finish_reason": "completed",
"events": [{"type": "tool/call", "data": {"name": "unrelated_tool"}}],
"notifications": [],
}
values.update(overrides)
return RunResult(**values)
@pytest.fixture
def live_smoke(monkeypatch: pytest.MonkeyPatch) -> SimpleNamespace:
import deepseek_harness
state = SimpleNamespace(
prompts=[], session_ids=[], challenges=[], checked_logs=[], closed=False,
create_bytes=SMOKE["LIVE_API_SENTINEL"].encode("utf-8"),
create_result=live_result(), verify_result=live_result(), receipt_mode="copy",
)
globals_ = SMOKE["smoke_sdk_live"].__globals__
token_hex = globals_["secrets"].token_hex
def fresh_challenge(size: int) -> str:
assert len(state.prompts) == 1
value = token_hex(size)
state.challenges.append(value)
return value
class ScriptedHarness:
def __init__(self, **kwargs: object) -> None:
state.root = Path(kwargs["cwd"])
assert "toolChoice" not in kwargs
def __enter__(self) -> ScriptedHarness:
return self
def __exit__(self, *args: object) -> None:
state.closed = True
def run(self, prompt: str, *, session_id: str) -> RunResult:
state.prompts.append(prompt)
state.session_ids.append(session_id)
if len(state.prompts) == 1:
state.marker = Path(prompt.splitlines()[-1])
assert state.marker.parent == state.root
if state.create_bytes is not None:
state.marker.write_bytes(state.create_bytes)
return state.create_result
assert len(state.prompts) == 2
assert len(state.challenges) == 1
challenge = state.challenges[0]
assert all(challenge not in sent for sent in state.prompts)
assert str(state.marker) not in prompt
assert "previous turn" in prompt and "changed externally" in prompt
assert state.marker.read_bytes() == challenge.encode("ascii")
receipt = Path(prompt.splitlines()[-1])
assert receipt != state.marker and not receipt.exists()
if state.receipt_mode == "copy":
receipt.write_bytes(state.marker.read_bytes())
elif state.receipt_mode == "stale":
receipt.write_bytes(state.create_bytes)
elif state.receipt_mode == "wrong":
receipt.write_bytes(b"wrong")
elif state.receipt_mode == "newline":
receipt.write_bytes(state.marker.read_bytes() + b"\n")
elif state.receipt_mode == "changed-source":
receipt.write_bytes(state.marker.read_bytes())
state.marker.write_bytes(b"changed")
elif state.receipt_mode != "missing":
raise AssertionError(state.receipt_mode)
return state.verify_result
monkeypatch.setenv("DEEPSEEK_API_KEY", "unit-test-key")
monkeypatch.setenv("DEEPSEEK_BASE_URL", "https://api.invalid")
monkeypatch.setattr(deepseek_harness, "DeepSeekHarness", ScriptedHarness)
monkeypatch.setattr(globals_["secrets"], "token_hex", fresh_challenge)
monkeypatch.setitem(globals_, "assert_zstd_session_log", state.checked_logs.append)
return state
def test_live_smoke_requires_fresh_external_content(live_smoke: SimpleNamespace) -> None:
SMOKE["smoke_sdk_live"]()
assert len(live_smoke.prompts) == 2
assert live_smoke.session_ids == ["installed-wheel-live-api"] * 2
assert len(live_smoke.checked_logs) == 1
assert live_smoke.closed and not live_smoke.root.exists()
@pytest.mark.parametrize("label", ["create", "verify"])
@pytest.mark.parametrize(("overrides", "message"), [
({"finish_reason": "error"}, "turn ended with 'error'"),
({"finish_reason": "error", "events": [{
"type": "turn/end", "data": {"turn": 1, "reason": {
"kind": "error", "error": {"code": "AUTH", "status": 401},
}},
}]}, "turn ended with.*AUTH.*401"),
({"events": []}, "turn made no model-requested tool call"),
({"final_response": "PYTHON_SDK_LIVE_OK extra"}, "turn returned"),
])
def test_live_smoke_rejects_invalid_turn_before_continuing(
live_smoke: SimpleNamespace, label: str, overrides: dict[str, object], message: str,
) -> None:
setattr(live_smoke, f"{label}_result", live_result(**overrides))
with pytest.raises(AssertionError, match=f"{label} {message}"):
SMOKE["smoke_sdk_live"]()
assert len(live_smoke.prompts) == (1 if label == "create" else 2)
assert not live_smoke.checked_logs
assert live_smoke.closed
if label == "create":
assert not live_smoke.challenges
@pytest.mark.parametrize("content", [None, b"wrong", b"PYTHON_SDK_LIVE_OK\n"])
def test_live_smoke_rejects_bad_create_before_host_overwrite(
live_smoke: SimpleNamespace, content: bytes | None,
) -> None:
live_smoke.create_bytes = content
with pytest.raises(AssertionError, match="create turn (did not create|wrote unexpected bytes)"):
SMOKE["smoke_sdk_live"]()
assert len(live_smoke.prompts) == 1
assert not live_smoke.challenges and not live_smoke.checked_logs
assert live_smoke.closed
@pytest.mark.parametrize(("mode", "message"), [
("missing", "did not create receipt"),
("stale", "wrote unexpected bytes to receipt"),
("wrong", "wrote unexpected bytes to receipt"),
("newline", "wrote unexpected bytes to receipt"),
("changed-source", "changed source file"),
])
def test_live_smoke_rejects_unrelated_tool_without_exact_receipt(
live_smoke: SimpleNamespace, mode: str, message: str,
) -> None:
live_smoke.receipt_mode = mode
with pytest.raises(AssertionError, match=f"verify turn {message}"):
SMOKE["smoke_sdk_live"]()
assert len(live_smoke.prompts) == 2
assert not live_smoke.checked_logs
assert live_smoke.closed
@pytest.mark.parametrize(
("prompt_name", "expected"),
[
("SNAPSHOT_DIRECT_CHILD_PROMPT", "DIRECT_CHILD_OK"),
("SNAPSHOT_WORKFLOW_CHILD_PROMPT", "WORKFLOW_CHILD_OK"),
],
)
def test_child_prompt_precedes_runtime_context(prompt_name: str, expected: str) -> None:
chunks = SMOKE["completion_chunks"]({
"messages": [
{"role": "user", "content": [
{"type": "text", "text": SMOKE[prompt_name]},
{"type": "text", "text": "Current runtime context"},
]},
],
})
assert any(
chunk.get("delta", {}).get("text") == expected
for chunk in chunks
)
def test_mcp_smoke_requests_the_discovered_tool() -> None:
chunks = SMOKE["completion_chunks"]({
"messages": [{"role": "user", "content": [{"type": "text", "text": SMOKE["MCP_PROMPT"]}]}],
"tools": [{"name": "mcp__fixture__add", "input_schema": {"type": "object"}}],
})
calls = [
chunk["content_block"]
for chunk in chunks
if chunk.get("type") == "content_block_start"
]
assert calls == [{"type": "tool_use", "id": "mcp-add", "name": "mcp__fixture__add", "input": {}}]
arguments = next(chunk["delta"]["partial_json"] for chunk in chunks if chunk.get("type") == "content_block_delta")
assert json.loads(arguments) == {"a": 19, "b": 23}
def test_mcp_smoke_accepts_the_external_server_result() -> None:
chunks = SMOKE["completion_chunks"]({
"messages": [
{"role": "user", "content": [{"type": "text", "text": SMOKE["MCP_PROMPT"]}]},
{
"role": "assistant",
"content": [{
"type": "tool_use", "id": "mcp-add", "name": "mcp__fixture__add", "input": {},
}],
},
{"role": "user", "content": [
{"type": "tool_result", "tool_use_id": "mcp-add", "content": [{"type": "text", "text": "42"}]},
]},
],
})
assert any(
chunk.get("delta", {}).get("text") == SMOKE["MCP_TEXT"]
for chunk in chunks
)
def test_mock_model_serves_native_messages_events() -> None:
with SMOKE["MockModel"]() as model:
body = {"model": "smoke-model", "stream": True, "messages": [{
"role": "user", "content": [{"type": "text", "text": "hello"}],
}]}
request = urllib.request.Request(
model.url + "/v1/messages", data=json.dumps(body).encode(),
headers={"content-type": "application/json"},
)
with urllib.request.urlopen(request) as response:
stream = response.read().decode()
frames = [frame.splitlines() for frame in stream.strip().split("\n\n")]
events = [json.loads(frame[1].removeprefix("data: ")) for frame in frames]
assert all(frame[0] == f"event: {event['type']}" for frame, event in zip(frames, events))
assert events[0]["type"] == "message_start"
assert events[-1] == {"type": "message_stop"}
assert next(event["delta"]["text"] for event in events if event["type"] == "content_block_delta") == SMOKE["EXPECTED_TEXT"]
def test_advanced_snapshot_normalizes_catalog_child_creation_time() -> None:
value = {
"type": "subagent/catalog",
"data": {
"childCreatedAt": 1788246207176,
},
}
assert SMOKE["normalize_snapshot_value"](value, []) == {
"type": "subagent/catalog",
"data": {
"childCreatedAt": 0,
},
}
assert SMOKE["normalize_snapshot_value"](
{"type": "fixture/event", "data": {"childCreatedAt": 1788246207176}},
[],
) == {"type": "fixture/event", "data": {"childCreatedAt": 1788246207176}}
def test_snapshot_comparison_preserves_opaque_generation_qualifiers() -> None:
normalize = SMOKE["normalize_session_format_comparison"]
expected = {
"header": {"type": "session", "version": 0, "otherVersion": 7},
"accepted": {
"type": "session-log-deepseek/delivery-accepted",
"data": {"sessionId": "s", "throughSeq": 4},
},
"source": {
"kind": "session-reference",
"references": [{"sessionId": "other", "capturedThroughSeq": 8}],
},
}
actual = {
"header": {"type": "session", "version": 1, "otherVersion": 7},
"accepted": {
"type": "session-log-deepseek/delivery-accepted",
"data": {"sessionId": "s", "sessionFormatVersion": 1, "throughSeq": 4},
},
"source": {
"kind": "session-reference",
"references": [{
"sessionId": "other",
"capturedFormatVersion": 1,
"capturedThroughSeq": 8,
}],
},
}
assert normalize(expected) != normalize(actual)
assert normalize(expected)["header"] == normalize(actual)["header"]
assert normalize(expected)["header"]["otherVersion"] == 7
assert normalize(actual)["accepted"] == actual["accepted"]
assert normalize(actual)["source"] == actual["source"]
def test_snapshot_value_scrubs_system_nodes_without_erasing_header_fields() -> None:
normalize = SMOKE["normalize_snapshot_value"]
system = {
"type": "system/message",
"data": {"message": {"role": "system", "content": [{"type": "text", "text": "prompt"}]}},
}
header = {"type": "request/header", "data": {"header": {"system": "unexpected"}}}
assert normalize(system, [])["data"]["message"]["content"] == [{"type": "text", "text": "{{system}}"}]
assert normalize(header, []) == header
empty = {"type": "system/message", "data": {"message": {"role": "system", "content": []}}}
assert normalize(empty, []) == empty
def test_snapshot_value_normalizes_embedded_assistant_stream_timing() -> None:
normalize = SMOKE["normalize_snapshot_value"]
event = {
"type": "assistant/message",
"seq": 4,
"time": 100,
"data": {
"stream": [
{"type": "chunk", "time": 101, "chunk": {"type": "finish"}},
{"type": "text-chunks", "time0": 102, "dt": [1, 2], "texts": ["a", "b", "c"]},
],
},
}
normalized = normalize(event, [])
assert normalized["time"] == 0
assert normalized["data"]["stream"] == [
{"type": "chunk", "time": 0, "chunk": {"type": "finish"}},
{"type": "text-chunks", "time0": 0, "dt": [0, 0], "texts": ["a", "b", "c"]},
]
def test_snapshot_comparison_expands_embedded_assistant_streams() -> None:
normalize = SMOKE["normalize_session_format_comparison"]
expected = [
{
"type": "assistant/chunk",
"seq": 4,
"time": 0,
"data": {"turn": 1, "step": 1, "chunk": {
"type": "text-delta", "index": 0, "text": "done",
}},
},
{
"type": "assistant/message",
"seq": 5,
"time": 0,
"data": {"turn": 1, "step": 1, "message": {"role": "assistant"}},
"sourceEventSeqs": [4],
"surfaceOp": "append",
},
]
actual = [{
"type": "assistant/message",
"seq": 4,
"time": 0,
"data": {
"turn": 1,
"step": 1,
"message": {"role": "assistant"},
"stream": [{
"type": "text-chunks", "time0": 0, "index": 0, "dt": [], "texts": ["done"],
}],
},
"surfaceOp": "append",
}]
assert normalize(actual, 2) == normalize(expected, 1)
tool_result = {
"type": "tool/result",
"data": {"turn": 1, "step": 1},
"sourceEventSeqs": [4],
}
assert normalize(tool_result, 1)["sourceEventSeqs"] == [4]
assert normalize(tool_result, 2)["sourceEventSeqs"] == [4]
def test_snapshot_stream_expands_reasoning_and_tool_call_records() -> None:
expand = SMOKE["expand_snapshot_stream_member"]
assert expand({
"type": "reasoning-chunks", "time0": 0, "index": 1,
"dt": [], "texts": ["think"],
}) == [{"type": "reasoning-delta", "index": 1, "text": "think"}]
assert expand({
"type": "tool-call-chunks", "time0": 0, "index": 2,
"id": "call-1", "name": "read", "dt": [1], "args": ["{", "}"],
}) == [
{"type": "tool-call-delta", "index": 2, "id": "call-1", "name": "read", "argumentsDelta": "{"},
{"type": "tool-call-delta", "index": 2, "id": "call-1", "name": "read", "argumentsDelta": "}"},
]
def test_snapshot_file_builder_order_is_checked_outside_update_mode(tmp_path: Path) -> None:
compare = SMOKE["compare_snapshot_files"]
with pytest.raises(AssertionError, match="snapshot builder produced"):
compare({}, False, tmp_path, ("result.json",))
def test_snapshot_comparison_expands_sdk_wrapped_attempts() -> None:
normalize = SMOKE["normalize_session_format_comparison"]
actual = [{
"method": "session.event",
"payload": {
"sessionId": "s",
"event": {
"type": "assistant/attempt",
"seq": 7,
"time": 0,
"data": {
"turn": 1,
"step": 1,
"stream": [{"type": "chunk", "time": 0, "chunk": {"type": "finish"}}],
},
},
},
}]
assert normalize(actual) == [{
"method": "session.event",
"payload": {
"sessionId": "s",
"event": {
"type": "assistant/chunk",
"data": {"turn": 1, "step": 1, "chunk": {"type": "finish"}},
},
},
}]
def test_snapshot_generation_names_select_highest_role_without_double_counting(
tmp_path: Path,
) -> None:
render = SMOKE["snapshot_session_filename"]
select = SMOKE["selected_snapshot_session_files"]
assert render(0, 0) == "session.jsonl"
assert render(0, 2) == "session.v2.jsonl"
assert render(3, 0) == "session.3.jsonl"
assert render(3, 2) == "session.3.v2.jsonl"
(tmp_path / "session.jsonl").write_text(
'{"type":"session","version":0}\n', encoding="utf-8",
)
(tmp_path / "session.v1.jsonl").write_text(
'{"type":"session","version":1}\n', encoding="utf-8",
)
(tmp_path / "session.1.jsonl").write_text(
'{"type":"session","version":0}\n', encoding="utf-8",
)
assert {index: path.name for index, path in select(tmp_path).items()} == {
0: "session.v1.jsonl",
1: "session.1.jsonl",
}
def test_snapshot_comparison_accepts_v3_output_against_v2_without_rewriting(tmp_path: Path) -> None:
predecessor = '{"type":"session","version":2}\n'
successor = '{"type":"session","version":3}\n'
old_path = tmp_path / "session.v2.jsonl"
old_path.write_text(predecessor, encoding="utf-8")
files = {"session.v3.jsonl": successor}
SMOKE["compare_snapshot_files"](files, False, tmp_path, ("session.v2.jsonl",))
assert old_path.read_text(encoding="utf-8") == predecessor
assert not (tmp_path / "session.v3.jsonl").exists()
SMOKE["compare_snapshot_files"](files, True, tmp_path, ("session.v2.jsonl",))
assert old_path.read_text(encoding="utf-8") == predecessor
assert (tmp_path / "session.v3.jsonl").read_text(encoding="utf-8") == successor
assert SMOKE["selected_snapshot_session_files"](tmp_path) == {0: tmp_path / "session.v3.jsonl"}
@pytest.mark.parametrize("filenames", [
("session.1.v2.jsonl", "session.v2.jsonl"),
("session.v2.jsonl",),
("session.v2.jsonl", "session.2.v2.jsonl"),
])
def test_snapshot_builder_checks_role_order_and_count_across_generations(
tmp_path: Path, filenames: tuple[str, ...],
) -> None:
files = {"session.v3.jsonl": "", "session.1.v3.jsonl": ""}
with pytest.raises(AssertionError, match="snapshot builder produced"):
SMOKE["compare_snapshot_files"](files, False, tmp_path, filenames)
def test_snapshot_generation_comparison_rejects_changed_payload(tmp_path: Path) -> None:
(tmp_path / "session.v2.jsonl").write_text(
'{"type":"session","version":2,"id":"expected"}\n', encoding="utf-8",
)
with pytest.raises(AssertionError, match="executable snapshot mismatch"):
SMOKE["compare_snapshot_files"](
{"session.v3.jsonl": '{"type":"session","version":3,"id":"changed"}\n'},
False, tmp_path, ("session.v2.jsonl",),
)
@pytest.mark.parametrize("version", [2, 4])
@pytest.mark.parametrize("update", [False, True])
def test_snapshot_comparison_rejects_noncurrent_writer(
tmp_path: Path, version: int, update: bool,
) -> None:
golden = '{"type":"session","version":2}\n'
(tmp_path / "session.v2.jsonl").write_text(golden, encoding="utf-8")
content = json.dumps({"type": "session", "version": version}) + "\n"
with pytest.raises(AssertionError, match="expected current Session format v3"):
SMOKE["compare_snapshot_files"](
{f"session.v{version}.jsonl": content}, update, tmp_path, ("session.v2.jsonl",),
)
assert (tmp_path / "session.v2.jsonl").read_text(encoding="utf-8") == golden
assert not (tmp_path / "session.v4.jsonl").exists()
@pytest.mark.parametrize("version", [2, 3, 4])
def test_persisted_session_requires_current_writer(version: int) -> None:
content = json.dumps({"type": "session", "version": version}) + "\n"
path = Path(f"session.v{version}.jsonl")
if version == 3:
assert SMOKE["assert_persisted_session_version"](path, content) == version
else:
with pytest.raises(AssertionError, match="expected current Session format v3"):
SMOKE["assert_persisted_session_version"](path, content)
def test_snapshot_generation_filename_must_match_header(tmp_path: Path) -> None:
(tmp_path / "session.v1.jsonl").write_text(
'{"type":"session","version":0}\n', encoding="utf-8",
)
with pytest.raises(AssertionError, match="filename declares Session format v1"):
SMOKE["selected_snapshot_session_files"](tmp_path)
@pytest.mark.parametrize("returncode", [1, -1073741819, 3221225477])
def test_profile_plugin_failure_reports_native_exit_status(monkeypatch: pytest.MonkeyPatch, returncode: int) -> None:
def failed_install(*args: object, **kwargs: object) -> subprocess.CompletedProcess[str]:
return subprocess.CompletedProcess(args=[], returncode=returncode, stdout="", stderr="")
monkeypatch.setattr(subprocess, "run", failed_install)
with pytest.raises(AssertionError) as error:
SMOKE["smoke_sdk_profile_plugin"]("http://127.0.0.1:1")
message = str(error.value)
assert f"returncode={returncode}" in message
assert f"0x{returncode & 0xffffffff:08x}" in message
assert "stdout='' stderr=''" in message
|