|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import io |
| 4 | +import json |
| 5 | +import tempfile |
| 6 | +import unittest |
| 7 | +from contextlib import redirect_stdout |
| 8 | +from pathlib import Path |
| 9 | +from unittest.mock import patch |
| 10 | + |
| 11 | +from conftest import FakeAdapter |
| 12 | + |
| 13 | +from teaagent.cli import main |
| 14 | + |
| 15 | + |
| 16 | +class DailyCLIAcceptanceTests(unittest.TestCase): |
| 17 | + def test_daily_cli_read_only_run_preflight_and_audit_summary(self) -> None: |
| 18 | + with tempfile.TemporaryDirectory() as tmp: |
| 19 | + root = Path(tmp) |
| 20 | + (root / 'README.md').write_text('hello teaagent', encoding='utf-8') |
| 21 | + adapter = FakeAdapter( |
| 22 | + [ |
| 23 | + '{"type":"tool","tool_name":"workspace_read_file","arguments":{"path":"README.md"},"call_id":"read-1"}', |
| 24 | + '{"type":"final","content":"repo summarized"}', |
| 25 | + ] |
| 26 | + ) |
| 27 | + |
| 28 | + preflight_out = io.StringIO() |
| 29 | + with redirect_stdout(preflight_out): |
| 30 | + preflight_code = main( |
| 31 | + [ |
| 32 | + 'agent', |
| 33 | + 'preflight', |
| 34 | + 'gpt', |
| 35 | + 'Summarize README.md for onboarding', |
| 36 | + '--root', |
| 37 | + tmp, |
| 38 | + '--permission-mode', |
| 39 | + 'read-only', |
| 40 | + ] |
| 41 | + ) |
| 42 | + preflight_payload = json.loads(preflight_out.getvalue()) |
| 43 | + |
| 44 | + run_out = io.StringIO() |
| 45 | + with ( |
| 46 | + patch('teaagent.cli.create_llm_adapter', return_value=adapter), |
| 47 | + redirect_stdout(run_out), |
| 48 | + ): |
| 49 | + run_code = main( |
| 50 | + [ |
| 51 | + 'agent', |
| 52 | + 'run', |
| 53 | + 'gpt', |
| 54 | + 'Summarize README.md for onboarding', |
| 55 | + '--root', |
| 56 | + tmp, |
| 57 | + '--permission-mode', |
| 58 | + 'read-only', |
| 59 | + ] |
| 60 | + ) |
| 61 | + run_payload = json.loads(run_out.getvalue()) |
| 62 | + |
| 63 | + show_out = io.StringIO() |
| 64 | + with redirect_stdout(show_out): |
| 65 | + show_code = main( |
| 66 | + ['agent', 'show', run_payload['run_id'], '--root', tmp] |
| 67 | + ) |
| 68 | + events = json.loads(show_out.getvalue()) |
| 69 | + |
| 70 | + self.assertEqual(preflight_code, 0) |
| 71 | + self.assertTrue(preflight_payload['ready']) |
| 72 | + self.assertEqual(preflight_payload['permission_mode'], 'read-only') |
| 73 | + self.assertEqual(run_code, 0) |
| 74 | + self.assertEqual(run_payload['status'], 'completed') |
| 75 | + self.assertEqual(run_payload['final_answer'], 'repo summarized') |
| 76 | + self.assertEqual(run_payload['audit_summary']['status'], 'completed') |
| 77 | + self.assertEqual( |
| 78 | + run_payload['audit_summary']['tool_names'], ['workspace_read_file'] |
| 79 | + ) |
| 80 | + self.assertEqual(run_payload['audit_summary']['approval_required'], False) |
| 81 | + self.assertEqual(show_code, 0) |
| 82 | + self.assertIn('run_completed', [event['event_type'] for event in events]) |
| 83 | + |
| 84 | + def test_daily_cli_prompt_approval_resume_is_auditable(self) -> None: |
| 85 | + with tempfile.TemporaryDirectory() as tmp: |
| 86 | + first_adapter = FakeAdapter( |
| 87 | + [ |
| 88 | + '{"type":"tool","tool_name":"workspace_write_file","arguments":{"path":"TODO.md","content":"done"},"call_id":"write-1"}' |
| 89 | + ] |
| 90 | + ) |
| 91 | + first_out = io.StringIO() |
| 92 | + with ( |
| 93 | + patch('teaagent.cli.create_llm_adapter', return_value=first_adapter), |
| 94 | + redirect_stdout(first_out), |
| 95 | + ): |
| 96 | + first_code = main( |
| 97 | + ['agent', 'run', 'gpt', 'Create TODO.md', '--root', tmp] |
| 98 | + ) |
| 99 | + first_payload = json.loads(first_out.getvalue()) |
| 100 | + |
| 101 | + resume_adapter = FakeAdapter( |
| 102 | + [ |
| 103 | + '{"type":"tool","tool_name":"workspace_write_file","arguments":{"path":"TODO.md","content":"done"},"call_id":"write-1"}', |
| 104 | + '{"type":"final","content":"created todo"}', |
| 105 | + ] |
| 106 | + ) |
| 107 | + resume_out = io.StringIO() |
| 108 | + with ( |
| 109 | + patch('teaagent.cli.create_llm_adapter', return_value=resume_adapter), |
| 110 | + redirect_stdout(resume_out), |
| 111 | + ): |
| 112 | + resume_code = main( |
| 113 | + ['agent', 'resume', 'gpt', first_payload['run_id'], '--root', tmp] |
| 114 | + ) |
| 115 | + resume_payload = json.loads(resume_out.getvalue()) |
| 116 | + |
| 117 | + self.assertEqual(first_code, 1) |
| 118 | + self.assertEqual(first_payload['status'], 'pending_approval') |
| 119 | + self.assertTrue(first_payload['audit_summary']['approval_required']) |
| 120 | + self.assertEqual(first_payload['approval']['call_id'], 'write-1') |
| 121 | + self.assertEqual(resume_code, 0) |
| 122 | + self.assertEqual(resume_payload['status'], 'completed') |
| 123 | + self.assertEqual(resume_payload['resumed_from'], first_payload['run_id']) |
| 124 | + self.assertEqual(resume_payload['auto_approved_call_id'], 'write-1') |
| 125 | + self.assertEqual( |
| 126 | + resume_payload['audit_summary']['destructive_tool_calls'], 1 |
| 127 | + ) |
| 128 | + self.assertEqual( |
| 129 | + (Path(tmp) / 'TODO.md').read_text(encoding='utf-8'), 'done' |
| 130 | + ) |
| 131 | + |
| 132 | + |
| 133 | +if __name__ == '__main__': |
| 134 | + unittest.main() |
0 commit comments