test_assertions.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. from drill.assertions import AssertionResult, run_verify_assertions
  2. class TestAssertionResult:
  3. def test_passing_to_criterion_result(self):
  4. ar = AssertionResult(
  5. command="tool-called Read",
  6. passed=True,
  7. exit_code=0,
  8. stdout="PASS: Read called 3 time(s)",
  9. stderr="",
  10. )
  11. cr = ar.to_criterion_result()
  12. assert cr.verdict == "pass"
  13. assert cr.source == "assertion"
  14. assert "[assertion]" in cr.criterion
  15. assert "tool-called Read" in cr.criterion
  16. def test_failing_to_criterion_result(self):
  17. ar = AssertionResult(
  18. command="tool-not-called Write",
  19. passed=False,
  20. exit_code=1,
  21. stdout="",
  22. stderr="FAIL: Write called 2 time(s)",
  23. )
  24. cr = ar.to_criterion_result()
  25. assert cr.verdict == "fail"
  26. assert cr.source == "assertion"
  27. assert "stderr: FAIL" in cr.evidence
  28. class TestRunVerifyAssertions:
  29. def test_passing_assertion(self, tmp_path):
  30. tc = '{"tool": "Read", "args": {}, "source": "native"}\n'
  31. (tmp_path / "tool_calls.jsonl").write_text(tc)
  32. results = run_verify_assertions(
  33. assertions=["grep -q Read tool_calls.jsonl"],
  34. results_dir=tmp_path,
  35. workdir=tmp_path,
  36. )
  37. assert len(results) == 1
  38. assert results[0].passed is True
  39. assert results[0].exit_code == 0
  40. def test_failing_assertion(self, tmp_path):
  41. tc = '{"tool": "Read", "args": {}, "source": "native"}\n'
  42. (tmp_path / "tool_calls.jsonl").write_text(tc)
  43. results = run_verify_assertions(
  44. assertions=["grep -q NonexistentTool tool_calls.jsonl"],
  45. results_dir=tmp_path,
  46. workdir=tmp_path,
  47. )
  48. assert len(results) == 1
  49. assert results[0].passed is False
  50. def test_runs_all_assertions(self, tmp_path):
  51. (tmp_path / "tool_calls.jsonl").write_text('{"tool": "Read"}\n')
  52. results = run_verify_assertions(
  53. assertions=[
  54. "grep -q Read tool_calls.jsonl",
  55. "grep -q Write tool_calls.jsonl",
  56. "grep -q Read tool_calls.jsonl",
  57. ],
  58. results_dir=tmp_path,
  59. workdir=tmp_path,
  60. )
  61. assert len(results) == 3
  62. assert results[0].passed is True
  63. assert results[1].passed is False
  64. assert results[2].passed is True
  65. def test_timeout_handling(self, tmp_path):
  66. (tmp_path / "tool_calls.jsonl").write_text("{}\n")
  67. results = run_verify_assertions(
  68. assertions=["sleep 30"],
  69. results_dir=tmp_path,
  70. workdir=tmp_path,
  71. timeout_seconds=1,
  72. )
  73. assert len(results) == 1
  74. assert results[0].passed is False
  75. assert results[0].exit_code == 124
  76. assert "Timed out" in results[0].stderr
  77. def test_drill_workdir_env_var(self, tmp_path):
  78. (tmp_path / "tool_calls.jsonl").write_text("{}\n")
  79. workdir = tmp_path / "scenario-workdir"
  80. workdir.mkdir()
  81. results = run_verify_assertions(
  82. assertions=['test "$DRILL_WORKDIR" = "' + str(workdir) + '"'],
  83. results_dir=tmp_path,
  84. workdir=workdir,
  85. )
  86. assert len(results) == 1
  87. assert results[0].passed is True
  88. def test_bin_dir_on_path(self, tmp_path):
  89. (tmp_path / "tool_calls.jsonl").write_text("{}\n")
  90. results = run_verify_assertions(
  91. assertions=["echo $PATH | grep -q bin"],
  92. results_dir=tmp_path,
  93. workdir=tmp_path,
  94. )
  95. assert len(results) == 1
  96. assert results[0].passed is True