Files
gitea-agent/harness/tests/_support/delegation.py
T

66 lines
4.1 KiB
Python

"""Synthetic real-Git delegation evidence builder; no test cases."""
import copy
import hashlib
import json
import subprocess
from harness.tests._support.data import canonical_digest
class RealEvidence:
def __init__(self, root):
self.root = root
self.bundle = root / 'bundle'
(self.bundle / 'src').mkdir(parents=True)
(self.bundle / 'tests').mkdir()
(self.bundle / 'evidence').mkdir()
(self.bundle / 'src/a.py').write_text('VALUE = 1\n')
(self.bundle / 'tests/test_a.py').write_text('def test_value():\n assert 1 == 1\n')
(self.bundle / 'evidence/red.txt').write_text('red\n')
(self.bundle / 'evidence/green.txt').write_text('green\n')
subprocess.run(['/usr/bin/git', 'init', '-q', str(self.root / 'repo')], check=True)
repo = self.root / 'repo'
(repo / 'src').mkdir(); (repo / 'tests').mkdir()
(repo / 'src/a.py').write_text('VALUE = 1\n')
(repo / 'tests/test_a.py').write_text('def test_value():\n assert 1 == 1\n')
subprocess.run(['/usr/bin/git', '-C', str(repo), 'config', 'user.email', 'test@example.invalid'], check=True)
subprocess.run(['/usr/bin/git', '-C', str(repo), 'config', 'user.name', 'Integration'], check=True)
subprocess.run(['/usr/bin/git', '-C', str(repo), 'add', '.'], check=True)
subprocess.run(['/usr/bin/git', '-C', str(repo), 'commit', '-q', '-m', 'fixture'], check=True)
self.revision = subprocess.check_output(['/usr/bin/git', '-C', str(repo), 'rev-parse', 'HEAD'], text=True).strip()
self.git_dir = repo / '.git'
self.files = {p: hashlib.sha256((self.bundle / p).read_bytes()).hexdigest() for p in ('src/a.py', 'tests/test_a.py')}
self.bundle_sha = self.digest(self.files)
self.context = {'schema': 'gitea-agent.delegation-evidence-context/v1',
'source': {'kind': 'git_tree', 'revision': self.revision},
'stage': 'spec-review', 'bundle_sha256': self.bundle_sha,
'files': self.files, 'allowed_actions': ['static', 'execute'],
'obligations': [{'id': 'static', 'kind': 'static'}, {'id': 'rg', 'kind': 'red_green'}],
'executions': [self.execution('red', 1, 'assertion', 'evidence/red.txt'),
self.execution('green', 0, 'none', 'evidence/green.txt')]}
line_hash = hashlib.sha256(b'VALUE = 1\n').hexdigest()
self.result = {'schema': 'gitea-agent.delegation-evidence-result/v1',
'source': copy.deepcopy(self.context['source']), 'stage': 'spec-review',
'bundle_sha256': self.bundle_sha,
'claims': [{'id': 'static', 'status': 'supported', 'anchors': [{'kind': 'lines', 'path': 'src/a.py', 'start_line': 1, 'end_line': 1, 'text_sha256': line_hash}], 'execution_ids': []},
{'id': 'rg', 'status': 'supported', 'anchors': [{'kind': 'test', 'path': 'tests/test_a.py', 'symbol': 'test_value'}], 'execution_ids': ['red', 'green']}]}
self.context_path = root / 'context.json'; self.result_path = root / 'result.json'
self.write()
digest = staticmethod(canonical_digest)
def execution(self, eid, code, kind, path):
return {'id': eid, 'test_id': 'tests/test_a.py::test_value', 'bundle_sha256': self.bundle_sha,
'test_file_sha256': self.files['tests/test_a.py'], 'argv': ['python', '-m', 'pytest'],
'exit_code': code, 'failure_kind': kind, 'evidence_path': path,
'evidence_sha256': hashlib.sha256((self.bundle / path).read_bytes()).hexdigest(), 'origin': 'direct'}
def write(self):
self.context_path.write_text(json.dumps(self.context))
self.result_path.write_text(json.dumps(self.result))
def declaration(self):
return {'mode': 'delegated', 'bundle_root': str(self.bundle), 'context_path': str(self.context_path),
'result_path': str(self.result_path), 'git_dir': str(self.git_dir)}
def set_stage(self, stage):
self.context['stage'] = stage; self.result['stage'] = stage; self.write()