diff --git a/tests/test_candidate.py b/tests/test_candidate.py index 28dfdc6e..1b228c55 100644 --- a/tests/test_candidate.py +++ b/tests/test_candidate.py @@ -11,15 +11,18 @@ import base64 import subprocess from copy import deepcopy +from functools import partial from importlib import import_module from itertools import cycle from os import environ +from shutil import copytree import pytest from loopzero import candidate from loopzero.candidate import CandidateError, CandidateRaceError, attest_candidate from loopzero.types import LoopZeroError +from tests.conftest import commit_file, git as isolated_git @pytest.fixture(autouse=True) @@ -205,38 +208,36 @@ def block(number: int, _source: dict) -> None: run_attestation(github_payloads=values, source_policy=block) +@pytest.fixture(scope="session") +def candidate_seed(tmp_path_factory): + remote = tmp_path_factory.mktemp("candidate-seed") + git = partial(isolated_git, remote) + git("init", "-b", "main") + commit_file(remote, "base", "base\n", "base") + for branch, content in (("parent", "H1\n"), ("source", "reviewed\n")): + git("checkout", "-b", branch, "main") + commit_file(remote, branch, content, branch) + git("checkout", "-b", "candidate", "main") + git("merge", "--no-ff", "parent", "-m", "parent integration") + git("merge", "--no-ff", "source", "-m", "source integration") + git("checkout", "main") + git("merge", "--squash", "parent") + git("commit", "-m", "squashed parent") + return remote, *git("rev-parse", "source^", "parent", "source", "candidate", "main").split() + + @pytest.fixture -def tree_candidate(tmp_path, monkeypatch): - remote = tmp_path / "remote" - remote.mkdir() +def tree_candidate(candidate_seed, tmp_path, monkeypatch): + seed, base, parent, source, candidate_sha, main = candidate_seed + remote = copytree(seed, tmp_path / "remote") def git(*args): - return subprocess.run(["git", *args], cwd=remote, check=True, capture_output=True, - text=True).stdout.strip() - - git("init", "-b", "main") - git("config", "user.name", "Test") - git("config", "user.email", "test@example.org") + return isolated_git(remote, *args).strip() def commit(name, content): - (remote / name).write_text(content) - git("add", ".") - git("commit", "-m", name) + commit_file(remote, name, content, name) return git("rev-parse", "HEAD") - base = commit("base", "base\n") - git("checkout", "-b", "parent") - parent = commit("parent", "H1\n") - git("checkout", "-b", "source", base) - source = commit("source", "reviewed\n") - git("checkout", "-b", "candidate", base) - git("merge", "--no-ff", parent, "-m", "parent integration") - git("merge", "--no-ff", source, "-m", "source integration") - candidate_sha = git("rev-parse", "HEAD") - git("checkout", "main") - git("merge", "--squash", parent) - git("commit", "-m", "squashed parent") - main = git("rev-parse", "HEAD") popen = subprocess.Popen def isolated_fetch(argv, **kwargs): @@ -281,10 +282,9 @@ def attest( reads = cycle([main_head, main_after or main_head]) def get(path): - if path.endswith("/git/ref/heads/main"): - sha = next(reads) - return {"object": {"sha": sha}} if sha else {} - return deepcopy(values[path]) + if not path.endswith("/git/ref/heads/main"): + return deepcopy(values[path]) + return {"object": {"sha": sha}} if (sha := next(reads)) else {} return run(candidate_event, github_get=get, mergify_status=lambda *_: deepcopy(queue), source_policy=policy) diff --git a/tests/test_codex_transport.py b/tests/test_codex_transport.py index 498d8e09..b954c896 100644 --- a/tests/test_codex_transport.py +++ b/tests/test_codex_transport.py @@ -18,6 +18,7 @@ fake_bwrap = test_runners.fake_bwrap gh = test_cli.gh APPROVE = test_runners.APPROVE +BYTE_LIMIT_REVIEW = test_runners._native_codex_result("a" * 40) @pytest.fixture @@ -595,14 +596,13 @@ def test_entire_leading_block_budget_includes_adjudication(native, monkeypatch): ["\n", "\r\n", "\r", "\v", "\f", "\x1c", "\x1d", "\x1e", "\x85", "\u2028", "\u2029"], ) @pytest.mark.parametrize("overflow", [0, 1, 2]) -def test_raw_leading_utf8_span_at_limit_and_overflow(native, separator, overflow): - result = native[0]() +def test_raw_leading_utf8_span_at_limit_and_overflow(separator, overflow): + result = BYTE_LIMIT_REVIEW marker = cli.review_marker(result.head, result.kind) header = runners.encode_codex_evidence(runners.codex_evidence(result)) adjudication = "" span = separator.join((marker, header, adjudication)) - padding = 8192 + overflow - len(span.encode("utf-8")) - padded = marker[:-3] + " " * padding + marker[-3:] + padded = marker[:-3] + " " * (8192 + overflow - len(span.encode("utf-8"))) + marker[-3:] span = separator.join((padded, header, adjudication)) assert len(span.encode("utf-8")) == 8192 + overflow rendered = span + separator + "**approve**" diff --git a/tests/test_stage_runner.py b/tests/test_stage_runner.py index 17491193..6fe18983 100644 --- a/tests/test_stage_runner.py +++ b/tests/test_stage_runner.py @@ -12,16 +12,15 @@ from .test_sandbox import bwrap_log, make_config # noqa: F401 -@pytest.mark.parametrize('base_ref', ['stale', 'missing', 'recorded']) -@pytest.mark.parametrize('body,verdict,tails', [ - ('print(json.dumps(r))', True, ['ok', 'ok']), - ('r["exit_code"]=7;print(json.dumps(r))', False, ['ok']), - ('print("x" * 3000 + "worker socket unavailable", file=sys.stderr);sys.exit(42)', False, None), - ('r["exit_code"]=7;r["tail"]="Ω"*10000+" newest";print(json.dumps(r))', False, +@pytest.mark.parametrize('base_ref,body,verdict,tails', [ + *[(base_ref, 'print(json.dumps(r))', True, ['ok', 'ok']) for base_ref in ('stale', 'missing', 'recorded')], + ('stale', 'r["exit_code"]=7;print(json.dumps(r))', False, ['ok']), + ('stale', 'print("x" * 3000 + "worker socket unavailable", file=sys.stderr);sys.exit(42)', False, None), + ('stale', 'r["exit_code"]=7;r["tail"]="Ω"*10000+" newest";print(json.dumps(r))', False, ['Ω' * 8188 + ' newest']), - ('subprocess.run(["git","-C",repo,"-c","user.name=test","-c","user.email=t@e","commit","--allow-empty","-m","drift"],stdout=sys.stderr,check=True);print(json.dumps(r))', False, None), - ('r.update(not_started=True,duration_s=0,tail="");print(json.dumps(r))', True, ['one', 'two']), - *[(body, False, None) for body in [ + ('stale', 'subprocess.run(["git","-C",repo,"-c","user.name=test","-c","user.email=t@e","commit","--allow-empty","-m","drift"],stdout=sys.stderr,check=True);print(json.dumps(r))', False, None), + ('stale', 'r.update(not_started=True,duration_s=0,tail="");print(json.dumps(r))', True, ['one', 'two']), + *[('stale', body, False, None) for body in [ '', '''print('{"x":' * 100000 + "0" + "}" * 100000)''', 'print("{")', 'print(json.dumps(r));print(json.dumps(r))', 'del r["exit_code"];print(json.dumps(r))', 'del r["worktree"];print(json.dumps(r))', 'r["duration_s"]=float("nan");print(json.dumps(r))', @@ -66,17 +65,15 @@ def test_runner(git_repo, tmp_path, bwrap_log, body, verdict, tails, base_ref): if tails is not None: assert [r.tail for r in report.results] == tails if not verdict: + assert calls == ['echo one'] assert sum(r.tail == 'ok' for r in report.results) <= 1 if '* 100000' in body: assert 'Stage runner failed' in report.results[0].tail assert any(word in report.results[0].tail.lower() for word in ('recursion', 'stack overflow')) - if 'drift' in body: - assert report.dirty and calls == ['echo one'] - assert 'Worktree changed after stage' in report.results[0].tail - elif not verdict: - assert calls == ['echo one'] current_head = _save_report(git_repo, report) if 'drift' in body: + assert report.dirty + assert 'Worktree changed after stage' in report.results[0].tail assert current_head == git(git_repo, 'rev-parse', 'HEAD').strip() and current_head != report.head assert _load_report(git_repo) != report return