test_smoke_model.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. from __future__ import annotations
  2. import runpy
  3. from pathlib import Path
  4. import pytest
  5. ROOT = Path(__file__).resolve().parents[3]
  6. SMOKE = runpy.run_path(ROOT / "scripts" / "smoke-python-runtime.py")
  7. @pytest.mark.parametrize(
  8. ("prompt_name", "expected"),
  9. [
  10. ("SNAPSHOT_DIRECT_CHILD_PROMPT", "DIRECT_CHILD_OK"),
  11. ("SNAPSHOT_WORKFLOW_CHILD_PROMPT", "WORKFLOW_CHILD_OK"),
  12. ],
  13. )
  14. def test_child_prompt_precedes_runtime_context(prompt_name: str, expected: str) -> None:
  15. chunks = SMOKE["completion_chunks"]({
  16. "messages": [
  17. {"role": "user", "content": SMOKE[prompt_name]},
  18. {"role": "user", "content": "Current runtime context"},
  19. ],
  20. })
  21. assert any(
  22. choice.get("delta", {}).get("content") == expected
  23. for chunk in chunks
  24. for choice in chunk.get("choices", [])
  25. )
  26. def test_mcp_smoke_requests_the_discovered_tool() -> None:
  27. chunks = SMOKE["completion_chunks"]({
  28. "messages": [{"role": "user", "content": SMOKE["MCP_PROMPT"]}],
  29. "tools": [{"type": "function", "function": {"name": "mcp__fixture__add"}}],
  30. })
  31. calls = [
  32. call
  33. for chunk in chunks
  34. for choice in chunk.get("choices", [])
  35. for call in choice.get("delta", {}).get("tool_calls", [])
  36. ]
  37. assert calls[0]["function"] == {
  38. "name": "mcp__fixture__add",
  39. "arguments": '{"a": 19, "b": 23}',
  40. }
  41. def test_mcp_smoke_accepts_the_external_server_result() -> None:
  42. chunks = SMOKE["completion_chunks"]({
  43. "messages": [
  44. {"role": "user", "content": SMOKE["MCP_PROMPT"]},
  45. {
  46. "role": "assistant",
  47. "tool_calls": [{
  48. "id": "mcp-add",
  49. "type": "function",
  50. "function": {"name": "mcp__fixture__add", "arguments": '{}'},
  51. }],
  52. },
  53. {"role": "tool", "tool_call_id": "mcp-add", "content": "42"},
  54. ],
  55. })
  56. assert any(
  57. choice.get("delta", {}).get("content") == SMOKE["MCP_TEXT"]
  58. for chunk in chunks
  59. for choice in chunk.get("choices", [])
  60. )
  61. def test_snapshot_comparison_normalizes_only_session_generation_provenance() -> None:
  62. normalize = SMOKE["normalize_session_format_comparison"]
  63. expected = {
  64. "header": {"type": "session", "version": 0, "otherVersion": 7},
  65. "accepted": {
  66. "type": "session-log-deepseek/delivery-accepted",
  67. "data": {"sessionId": "s", "throughSeq": 4},
  68. },
  69. "source": {
  70. "kind": "session-reference",
  71. "references": [{"sessionId": "other", "capturedThroughSeq": 8}],
  72. },
  73. }
  74. actual = {
  75. "header": {"type": "session", "version": 1, "otherVersion": 7},
  76. "accepted": {
  77. "type": "session-log-deepseek/delivery-accepted",
  78. "data": {"sessionId": "s", "sessionFormatVersion": 1, "throughSeq": 4},
  79. },
  80. "source": {
  81. "kind": "session-reference",
  82. "references": [{
  83. "sessionId": "other",
  84. "capturedFormatVersion": 1,
  85. "capturedThroughSeq": 8,
  86. }],
  87. },
  88. }
  89. assert normalize(expected) == normalize(actual)
  90. assert normalize(expected)["header"]["otherVersion"] == 7
  91. def test_snapshot_generation_names_select_highest_role_without_double_counting(
  92. tmp_path: Path,
  93. ) -> None:
  94. render = SMOKE["snapshot_session_filename"]
  95. select = SMOKE["selected_snapshot_session_files"]
  96. assert render(0, 0) == "session.jsonl"
  97. assert render(0, 2) == "session.v2.jsonl"
  98. assert render(3, 0) == "session.3.jsonl"
  99. assert render(3, 2) == "session.3.v2.jsonl"
  100. (tmp_path / "session.jsonl").write_text(
  101. '{"type":"session","version":0}\n', encoding="utf-8",
  102. )
  103. (tmp_path / "session.v1.jsonl").write_text(
  104. '{"type":"session","version":1}\n', encoding="utf-8",
  105. )
  106. (tmp_path / "session.1.jsonl").write_text(
  107. '{"type":"session","version":0}\n', encoding="utf-8",
  108. )
  109. assert {index: path.name for index, path in select(tmp_path).items()} == {
  110. 0: "session.v1.jsonl",
  111. 1: "session.1.jsonl",
  112. }
  113. def test_snapshot_generation_filename_must_match_header(tmp_path: Path) -> None:
  114. (tmp_path / "session.v1.jsonl").write_text(
  115. '{"type":"session","version":0}\n', encoding="utf-8",
  116. )
  117. with pytest.raises(AssertionError, match="filename declares Session format v1"):
  118. SMOKE["selected_snapshot_session_files"](tmp_path)