nightshift/tests/test_what_happened.py
K. Hodges 2f2146f47d setup scripts improvement and what happened
- Added nightshift what-happened to summarize the latest run/task failure from artifacts.
  - Added integ-run --setup with setup options, so sandbox creation can immediately run integ-setup.
  - integ-setup output now explicitly shows the venv activation command.
  - Command stages now prefer a detected project/adjacent .venv, so python -m pytest -q should use the integ venv without relying on shell activation.
  - Retry command outputs now get attempt-specific artifact names, like test-output-1.txt, instead of overwriting.
  - Docs updated in README.md, the pastebin template README, and examples/tutorial/03-pastebin.
  - Added pytest config so root python -m pytest -q ignores generated integ/template target tests.
  - Version bumped to 0.2.4-alpha-bratwurst-relish.
2026-05-20 04:20:15 -07:00

57 lines
2.3 KiB
Python

from pathlib import Path
import tempfile
import unittest
from nightshift.what_happened import build_what_happened
class WhatHappenedTests(unittest.TestCase):
def test_build_what_happened_summarizes_latest_failed_task(self) -> None:
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
task_dir = root / ".nightshift" / "runs" / "20260520T000000.000000Z" / "tasks" / "TASK-001"
diagnostics = task_dir / "diagnostics"
diagnostics.mkdir(parents=True)
run_dir = task_dir.parents[1]
(run_dir / "run-summary.md").write_text(
"# Run Summary\n\n- Task: TASK-001\n- Status: failed\n- Retry count: 1\n- Reason: test failed\n",
encoding="utf-8",
)
(task_dir / "stage-results.md").write_text(
"\n".join(
[
"# Stage Results",
"",
"## test",
"",
"Status: fail",
"Reason: Command exited with code 2: python -m pytest -q",
"Output: test-output-1.txt",
]
),
encoding="utf-8",
)
(task_dir / "test-output-1.txt").write_text(
"Command: `python -m pytest -q`\nExit code: 2\nModuleNotFoundError: No module named 'pastebin_app'\n",
encoding="utf-8",
)
(diagnostics / "test-failure-retry-1.md").write_text(
"Failure category: missing dependency\nProbable root cause: Runtime cannot import required package.\n",
encoding="utf-8",
)
(task_dir / "repair-1.patch").write_text(
"diff --git a/src/app.py b/src/app.py\n--- a/src/app.py\n+++ b/src/app.py\n",
encoding="utf-8",
)
report = build_what_happened(root, ".nightshift")
self.assertIn("Status: failed", report.content)
self.assertIn("ModuleNotFoundError", report.content)
self.assertIn("missing dependency", report.content)
self.assertIn("repair-1.patch", report.content)
if __name__ == "__main__":
unittest.main()