interplane.Get Omega
Read in your language

hop

Hop: a small native macOS launcher. Clipboard history, find, on-device dictation and your coding agent behind one key.

Brought by drakestapleton

branch main Branches Tags History

hop / tests / test_review_after_checks.py

172 lines · 9324 bytes Raw Blame History
1import json
2import os
3import pathlib
4import subprocess
5import sys
6import tempfile
7import unittest
8
9SCRIPT = pathlib.Path(__file__).resolve().parents[1] / 'tools/review-after-checks.py'
10
11
12class CaptureTests(unittest.TestCase):
13 def setUp(self):
14 self.temp = tempfile.TemporaryDirectory(prefix='review-runner-test-')
15 self.addCleanup(self.temp.cleanup)
16 self.root = pathlib.Path(self.temp.name)
17 self.repo = self.root / 'repo'
18 self.repo.mkdir()
19 self.env = dict(os.environ, GIT_CONFIG_NOSYSTEM='1', GIT_CONFIG_GLOBAL=os.devnull,
20 PYTHONDONTWRITEBYTECODE='1')
21 def git(*args):
22 return subprocess.check_output(['git', '-c', 'core.hooksPath=/dev/null', *args],
23 cwd=self.repo, env=self.env, text=True).strip()
24 self.git = git
25 git('init', '-q')
26 git('config', 'user.name', 'Fixture')
27 git('config', 'user.email', 'fixture@example.invalid')
28 (self.repo / 'source').write_text('base')
29 (self.repo / 'REVIEW-LESSONS.md').write_text('Landed constraints, no author conclusions.')
30 git('add', 'source', 'REVIEW-LESSONS.md'); git('commit', '-qm', 'base')
31 self.base = git('rev-parse', 'HEAD')
32 (self.repo / 'source').write_text('head')
33 (self.repo / 'REVIEW-LESSONS.md').write_text('UNLANDED author conclusion')
34 git('commit', '-qam', 'head')
35 self.head = git('rev-parse', 'HEAD')
36 self.task = self.root / 'task'
37 self.task.write_text('Inspect the requested source. No author conclusions.')
38 binpath = self.root / 'bin'; binpath.mkdir()
39 fake = self.root / 'fake.py'
40 fake.write_text('''import json, os, pathlib, re, sys
41a=sys.argv[1:]
42assert a[-1].startswith('@')
43a[-1]=pathlib.Path(a[-1][1:]).read_text()
44assert 'Landed constraints' not in a[-1] and 'UNLANDED' not in a[-1]
45assert '--max-time' not in a
46assert '--no-prewalk' in a
47c=json.loads(pathlib.Path(a[a.index('--config')+1]).read_text())
48assert c['advisor']['enabled'] is False
49assert c['retry']['modelFallback'] is False and c['retry']['usageAwareFallback'] is False
50assert c['contextPromotion']['enabled'] is False
51assert a[a.index('--model')+1]=='anthropic/claude-opus-5-5'
52assert a[a.index('--thinking')+1]=='medium'
53assert a[a.index('--mode')+1]=='json' and '--no-session' not in a
54sessions=pathlib.Path(a[a.index('--session-dir')+1]); sessions.mkdir()
55(sessions/'fixture-session').write_text('retained')
56out=sessions.parent
57pathlib.Path(os.environ['FAKE_STARTED']).write_text('called')
58print(json.dumps({'original':'all original findings '*5000}),flush=True)
59print(json.dumps({'followup':'short final reply'}),flush=True)
60mode=os.environ.get('FAKE_MODE','ok')
61if mode!='missing':
62 head=re.search(r'First line exactly, with no punctuation:\\nHEAD ([a-f0-9]{40})\\n',a[-1])[1]
63 (out/'verdict.txt').write_text('HEAD '+('f'*40 if mode=='wrong' else head)+('\\n' if mode=='empty' else '\\nREJECT: complete fixture report; no real model ran.\\n'))
64if mode=='mutate': pathlib.Path('changed-by-review').write_text('fixture')
65if mode=='fail': sys.exit(9)
66''')
67 omp = binpath / 'omp'
68 omp.write_text('#!/bin/sh\nexec "$FAKE_PYTHON" "$FAKE_SOURCE" "$@"\n')
69 omp.chmod(0o700)
70 self.marker = self.root / 'started'
71 self.env.update(PATH=str(binpath) + os.pathsep + self.env['PATH'], FAKE_PYTHON=sys.executable,
72 FAKE_SOURCE=str(fake), FAKE_STARTED=str(self.marker))
73 self.output = self.root / 'evidence'
74
75 def run_capture(self, check=None, mode='ok', output=None):
76 return subprocess.run([sys.executable, str(SCRIPT), self.base, str(self.task),
77 str(output or self.output), '--', *(check or [sys.executable, '-c', 'pass'])],
78 cwd=self.repo, env=dict(self.env, FAKE_MODE=mode),
79 capture_output=True, text=True, timeout=20)
80
81 def test_retains_full_stream_session_and_reject(self):
82 result = self.run_capture()
83 self.assertEqual(result.returncode, 0, result.stderr)
84 self.assertIn('Capture success is not MERGE', result.stdout)
85 self.assertEqual((self.output/'transcript.jsonl').read_text(),
86 json.dumps({'original':'all original findings '*5000})+'\n'+
87 json.dumps({'followup':'short final reply'})+'\n')
88 self.assertIn('REJECT', (self.output/'verdict.txt').read_text())
89 self.assertTrue((self.output/'sessions/fixture-session').exists())
90 self.assertEqual(json.loads((self.output/'result.json').read_text())['head'], self.head)
91
92 def test_failed_checks_never_start_review(self):
93 result = self.run_capture([sys.executable, '-c', 'raise SystemExit(7)'])
94 self.assertNotEqual(result.returncode, 0)
95 self.assertIn('checks failed (7)', result.stderr)
96 self.assertFalse(self.marker.exists())
97 self.assertFalse((self.output/'transcript.jsonl').exists())
98 record = json.loads((self.output/'result.json').read_text())
99 self.assertEqual(record['check_exit_code'], 7)
100 self.assertEqual(record['post_head'], self.head)
101 self.assertEqual(record['post_git_porcelain'], '')
102 self.assertIn('checks failed', record['failure'])
103
104 def test_source_changed_by_checks_never_starts_review(self):
105 result = self.run_capture([sys.executable, '-c', "from pathlib import Path; Path('dirty').write_text('x')"])
106 self.assertNotEqual(result.returncode, 0)
107 self.assertIn('checks changed source', result.stderr)
108 self.assertFalse(self.marker.exists())
109
110 def test_missing_wrong_failed_or_mutating_review_stops(self):
111 for mode, reason in [('missing','No such file'), ('empty','empty or mismatched verdict'), ('wrong','mismatched verdict'),
112 ('fail','review command failed (9)'), ('mutate','source differs')]:
113 with self.subTest(mode=mode):
114 result = self.run_capture(mode=mode, output=self.root/mode)
115 self.assertNotEqual(result.returncode, 0)
116 self.assertIn(reason, result.stderr)
117 self.assertTrue((self.root/mode/'transcript.jsonl').exists())
118 record = json.loads((self.root/mode/'result.json').read_text())
119 self.assertIn('failure', record)
120 self.assertEqual(record['post_head'], self.head)
121 self.assertEqual(bool(record['post_git_porcelain']), mode == 'mutate')
122
123 def test_unavailable_check_records_failure(self):
124 result = self.run_capture(['/nonexistent-hop-review-check'])
125 self.assertNotEqual(result.returncode, 0)
126 record = json.loads((self.output/'result.json').read_text())
127 self.assertEqual(record['stage'], 'checks')
128 self.assertIn('FileNotFoundError', record['failure'])
129 self.assertIn('post_head', record)
130 self.assertFalse(self.marker.exists())
131
132 def test_verifier_requires_its_own_passing_regressions(self):
133 (self.repo/'tools').mkdir()
134 (self.repo/'tools/check-app-review.py').write_text('# fixture verifier')
135 self.git('add', 'tools'); self.git('commit', '-qm', 'verifier')
136 result = self.run_capture(output=self.root/'missing-regression')
137 self.assertNotEqual(result.returncode, 0)
138 self.assertIn('requires its regression tests', result.stderr)
139 self.assertFalse(self.marker.exists())
140 (self.repo/'tests').mkdir()
141 (self.repo/'tests/test_app_review.py').write_text('raise RuntimeError("broken verifier regression")')
142 self.git('add', 'tests'); self.git('commit', '-qm', 'regression')
143 result = self.run_capture(output=self.root/'failing-regression')
144 self.assertNotEqual(result.returncode, 0)
145 self.assertIn('regression tests failed', result.stderr)
146 self.assertFalse(self.marker.exists())
147
148 def test_passing_verifier_regressions_run_before_check_and_review(self):
149 (self.repo/'tools').mkdir(); (self.repo/'tests').mkdir()
150 (self.repo/'tools/check-app-review.py').write_text('# verifier fixture')
151 (self.repo/'tests/test_app_review.py').write_text('import unittest\nclass Check(unittest.TestCase):\n def test_pass(self): self.assertTrue(True)\n')
152 self.git('add', 'tools', 'tests'); self.git('commit', '-qm', 'passing verifier fixture')
153 result = self.run_capture()
154 self.assertEqual(result.returncode, 0, result.stderr)
155 record = json.loads((self.output/'result.json').read_text())
156 self.assertEqual(record['verifier_test_exit_code'], 0)
157 self.assertEqual(record['check_exit_code'], 0)
158 self.assertTrue(self.marker.exists())
159
160 def test_existing_output_and_source_output_are_not_overwritten(self):
161 self.output.mkdir(); (self.output/'keep').write_text('keep')
162 result = self.run_capture()
163 self.assertNotEqual(result.returncode, 0)
164 self.assertEqual((self.output/'keep').read_text(), 'keep')
165 result = self.run_capture(output=self.repo/'evidence')
166 self.assertNotEqual(result.returncode, 0)
167 self.assertIn('outside source', result.stderr)
168 self.assertFalse(self.marker.exists())
169
170
171if __name__ == '__main__':
172 unittest.main()