~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""Unit tests for .github/scripts/combine-ai-reviews.sh.
The AI Code Reviewer workflow runs N models over a PR diff (network) and writes
each reviewer's raw stdout to resp_<i>.json and exit code to code_<i>. The
combine-ai-reviews.sh helper turns those files into the single sticky-comment
body, the deduped label set, the pass/fail decision, and the success count.
These tests exercise that pure-assembly logic directly with canned fixtures —
no network, no GitHub API — so the non-trivial bash/jq/perl parts (comma-split
already happens in the workflow; here it's header/footer stripping, anonymized
"Reviewer N" section assembly, label union, and decision aggregation) are
covered in CI.
"""
import json
import os
import shutil
import subprocess
from pathlib import Path
import pytest
SCRIPT = (
Path(__file__).resolve().parents[2]
/ ".github"
/ "scripts"
/ "combine-ai-reviews.sh"
)
_MISSING_TOOLS = [t for t in ("bash", "jq", "perl") if shutil.which(t) is None]
_IN_CI = bool(os.environ.get("CI") or os.environ.get("GITHUB_ACTIONS"))
# Skip ONLY when the tools are missing locally. In CI they must be present
# (jq+perl are installed into the test image), so we deliberately do NOT skip
# there: if they're ever missing in CI these tests run and fail loudly instead
# of silently skipping and leaving combine-ai-reviews.sh with zero enforced
# coverage. Failing per-test (vs a module-level raise) keeps the failure
# localized to this lane — see test_required_tools_present_in_ci for the
# explicit, clearly-messaged guard.
pytestmark = pytest.mark.skipif(
bool(_MISSING_TOOLS) and not _IN_CI,
reason=f"combine-ai-reviews.sh requires {_MISSING_TOOLS} (missing locally)",
)
def test_required_tools_present_in_ci():
"""The test image must ship the tools the helper needs. If this fails in CI,
install the missing tool in the Dockerfile `ldr-test` stage — otherwise the
rest of this suite would be a silent no-op."""
if _IN_CI:
assert not _MISSING_TOOLS, (
f"CI test image is missing {_MISSING_TOOLS}; add it to the Dockerfile "
"ldr-test stage so combine-ai-reviews.sh keeps real CI coverage."
)
FOOTER_SINGULAR = (
"*Review by [Friendly AI Reviewer]"
"(https://github.com/LearningCircuit/Friendly-AI-Reviewer) - made with ❤️*"
)
def make_response(body, decision="pass", labels=None, *, trailing=""):
"""Build a realistic ai-reviewer.sh JSON response (review + verdict + labels)."""
review = f"## AI Code Review\n\n{body}\n\n---\n{FOOTER_SINGULAR}{trailing}"
return json.dumps(
{
"review": review,
"fail_pass_workflow": decision,
"labels_added": labels or [],
}
)
def run_combine(work_dir, reviewers, *, head_sha="abc1234def", debug=False):
"""Write fixtures for each reviewer and run the helper; return its outputs.
`reviewers` is a list of dicts: {"code": int, "resp": str}. Model names are
deliberately distinctive so tests can assert they never leak into the
(anonymized) comment body.
"""
models = []
for i, rv in enumerate(reviewers):
(work_dir / f"code_{i}").write_text(str(rv["code"]))
(work_dir / f"resp_{i}.json").write_text(rv["resp"])
(work_dir / f"err_{i}.log").write_text("")
models.append(f"secret-model-{i}-DO-NOT-LEAK")
env = {"HEAD_SHA": head_sha, "DEBUG_MODE": "true" if debug else "false"}
result = subprocess.run(
["bash", str(SCRIPT), str(work_dir), *models],
capture_output=True,
text=True,
env={**env, "PATH": __import__("os").environ["PATH"]},
)
assert result.returncode == 0, (
f"script failed ({result.returncode}): {result.stderr}"
)
return {
"models": models,
"comment": (work_dir / "comment_body.md").read_text(),
"labels": (work_dir / "labels.txt").read_text(),
"trigger_labels": (work_dir / "trigger_labels.txt").read_text(),
"rejected_labels": json.loads(
(work_dir / "rejected_labels.json").read_text()
),
"decision": (work_dir / "decision.txt").read_text(),
"success_count": (work_dir / "success_count.txt").read_text(),
"stderr": result.stderr,
}
def test_two_reviewers_pass_and_fail(tmp_path):
out = run_combine(
tmp_path,
[
{
"code": 0,
"resp": make_response(
"All good here.", "pass", ["enhancement"]
),
},
{
"code": 0,
"resp": make_response("Race condition.", "fail", ["bug"]),
},
],
)
c = out["comment"]
# Both anonymized sections present, in order.
assert "### 👤 Reviewer 1" in c
assert "### 👤 Reviewer 2" in c
assert c.index("Reviewer 1") < c.index("Reviewer 2")
# Bodies carried through.
assert "All good here." in c
assert "Race condition." in c
# Aggregation: any fail -> fail; labels unioned + sorted; both succeeded.
assert out["decision"] == "fail"
# `bug` is issue-only in this repository; PR reviews canonicalize it to
# the PR-specific `bugfix` label.
assert out["labels"].split() == ["bugfix", "enhancement"]
assert out["success_count"] == "2"
def test_header_and_per_review_footer_are_stripped(tmp_path):
out = run_combine(
tmp_path,
[{"code": 0, "resp": make_response("Body one.", "pass")}],
)
c = out["comment"]
# The per-review "## AI Code Review" H2 is removed (only the combined H2 with
# the reviewer count remains), and the singular per-review footer is gone.
assert "## 🤖 AI Code Review (1 reviewer)" in c
assert "## AI Code Review\n" not in c
assert "Review by [Friendly AI Reviewer]" not in c
# Exactly one combined (plural) footer is appended.
assert c.count("Reviews by [Friendly AI Reviewer]") == 1
def test_footer_strip_tolerates_trailing_whitespace(tmp_path):
# LLMs sometimes append trailing newlines/spaces after the footer.
out = run_combine(
tmp_path,
[
{
"code": 0,
"resp": make_response("Body.", "pass", trailing="\n\n \n"),
}
],
)
assert "Review by [Friendly AI Reviewer]" not in out["comment"]
# ...but the actual review body must survive (guard against an over-greedy
# strip that nukes everything).
assert "Body." in out["comment"]
assert out["success_count"] == "1"
def test_failed_reviewer_degrades_without_sinking_others(tmp_path):
out = run_combine(
tmp_path,
[
{
"code": 0,
"resp": make_response("Good review.", "pass", ["enhancement"]),
},
{"code": 1, "resp": ""}, # hard error: non-zero exit, empty stdout
{"code": 0, "resp": "this is not json"}, # garbage stdout
],
)
c = out["comment"]
assert "Good review." in c
assert c.count("could not complete its review") == 2
# Two of three reviewers failed; the survivor still counts.
assert out["success_count"] == "1"
# A reviewer that errors out never flips the decision to fail.
assert out["decision"] == "pass"
# Failed reviewers contribute no labels.
assert out["labels"].split() == ["enhancement"]
def test_all_reviewers_failed(tmp_path):
out = run_combine(
tmp_path,
[{"code": 1, "resp": ""}, {"code": 1, "resp": ""}],
)
assert out["success_count"] == "0"
assert out["decision"] == "pass"
assert out["comment"].count("could not complete its review") == 2
def test_labels_are_unioned_and_deduped(tmp_path):
out = run_combine(
tmp_path,
[
{
"code": 0,
"resp": make_response("a", "pass", ["bug", "security"]),
},
{
"code": 0,
"resp": make_response("b", "pass", ["bug", "enhancement"]),
},
],
)
assert out["labels"].split() == ["bugfix", "enhancement", "security"]
def test_label_policy_separates_ci_recommendations_from_applied_labels(
tmp_path,
):
out = run_combine(
tmp_path,
[
{
"code": 0,
"resp": make_response(
"UI changes.",
"pass",
["test:e2e", "test:ui-full-shards", "security"],
),
}
],
)
assert out["labels"].split() == ["security"]
# The legacy alias is canonicalized before presenting the recommendation.
assert out["trigger_labels"].split() == [
"test:puppeteer",
"test:ui-full-shards",
]
assert "### 🧪 Suggested CI" in out["comment"]
assert "- `test:puppeteer`" in out["comment"]
assert "A maintainer must apply or re-add each label" in out["comment"]
def test_label_policy_rejects_unknown_human_only_and_control_char_labels(
tmp_path,
):
control_label = "security\nfeature"
out = run_combine(
tmp_path,
[
{
"code": 0,
"resp": make_response(
"Hostile suggestions.",
"pass",
[
"security",
"code-ready",
"code-ready-preliminary",
"invented-by-model",
control_label,
],
),
}
],
)
assert out["labels"].split() == ["security"]
assert out["trigger_labels"] == ""
assert out["rejected_labels"] == [
"code-ready",
"code-ready-preliminary",
"invented-by-model",
control_label,
]
# A newline inside one untrusted suggestion must not split into two allowed
# labels (`security` + `feature`).
assert "feature" not in out["labels"].split()
def test_entry_count_bound_pins_the_label_flood(tmp_path):
"""A flood past MAX_LABELS_PER_REVIEWER alone is dropped; the run still
produces output.
201 short entries trips only the entry-count bound: the whole array is a
few hundred bytes, nowhere near MAX_LABELS_BYTES. Remove
MAX_LABELS_PER_REVIEWER (keep MAX_LABELS_BYTES) and this fixture is no
longer oversized by either bound, so it fails on ``rejected_labels`` —
the flood's 201 junk names get filtered and unioned in normally instead
of being dropped wholesale, and the "oversized" stderr warning
disappears. See test_byte_size_bound_pins_the_label_flood for the
complementary fixture that pins MAX_LABELS_BYTES alone.
"""
flood = [f"flood-{i:03d}" for i in range(201)]
assert len(flood) > 200, "fixture must exceed MAX_LABELS_PER_REVIEWER alone"
assert len(json.dumps(flood)) < 16384, (
"fixture must stay under MAX_LABELS_BYTES so only the entry-count "
"bound is pinned"
)
out = run_combine(
tmp_path,
[
{"code": 0, "resp": make_response("Flooding.", "pass", flood)},
{
"code": 0,
"resp": make_response("Real review.", "pass", ["security"]),
},
],
)
# The legitimate reviewer is unaffected: the flood is one reviewer's
# malformed output, exactly like a non-object response.
assert out["labels"].split() == ["security"]
assert out["comment"].startswith("<!-- ai-code-review:sticky -->")
assert "Real review." in out["comment"]
assert out["success_count"] == "2"
assert out["decision"] == "pass"
# Dropped wholesale, so the flood is not echoed back through the rejected
# list either (which is written into the run summary).
assert out["rejected_labels"] == []
assert "oversized labels_added" in out["stderr"]
def test_byte_size_bound_pins_the_label_flood(tmp_path):
"""A flood past MAX_LABELS_BYTES alone is dropped; the run still produces
output.
30 entries of 1 KB each trips only the byte-size bound: 30 entries is far
under MAX_LABELS_PER_REVIEWER (200), but ~30 KB of text is well past
MAX_LABELS_BYTES (16 KiB). This is the bound that matters most for the
run-summary path (N7): with only the entry-count bound in force, a
handful of very large entries could still push tens of megabytes into
$GITHUB_STEP_SUMMARY. Remove MAX_LABELS_BYTES (keep
MAX_LABELS_PER_REVIEWER) and this fixture is no longer oversized by
either bound, so it fails the same way the entry-count test does when its
own bound is removed.
"""
flood = [f"flood-{i:03d}-" + ("x" * 1000) for i in range(30)]
assert len(flood) < 200, "fixture must stay under MAX_LABELS_PER_REVIEWER"
assert len(json.dumps(flood)) > 16384, (
"fixture must exceed MAX_LABELS_BYTES so only the byte-size bound is "
"pinned"
)
out = run_combine(
tmp_path,
[
{"code": 0, "resp": make_response("Flooding.", "pass", flood)},
{
"code": 0,
"resp": make_response("Real review.", "pass", ["security"]),
},
],
)
assert out["labels"].split() == ["security"]
assert out["comment"].startswith("<!-- ai-code-review:sticky -->")
assert "Real review." in out["comment"]
assert out["success_count"] == "2"
assert out["decision"] == "pass"
assert out["rejected_labels"] == []
assert "oversized labels_added" in out["stderr"]
def test_many_reviewers_under_the_bound_do_not_overflow_argv(tmp_path):
"""The accumulator must never be an execve argument, bound or no bound.
The per-reviewer bound alone is not enough: ten reviewers each staying
*under* it still add up to ~145 KB, past MAX_ARG_STRLEN. Put the
accumulation back on ``jq --argjson "$ACC"`` and this test fails with
returncode 126 and "Argument list too long"; the file-based --slurpfile
accumulator is what makes the total size irrelevant.
"""
def suggestions(reviewer):
return ["b" * 66 + f"{reviewer}{i:03d}" for i in range(199)]
per_reviewer = len(json.dumps(suggestions(0), separators=(",", ":")))
assert per_reviewer < 16384, "each reviewer must stay under the size bound"
assert per_reviewer * 10 > 131072, "the total must exceed MAX_ARG_STRLEN"
reviewers = [
{"code": 0, "resp": make_response(f"r{n}", "pass", suggestions(n))}
for n in range(9)
]
reviewers.append(
{
"code": 0,
"resp": make_response(
"Real review.", "pass", suggestions(9)[:198] + ["security"]
),
}
)
out = run_combine(tmp_path, reviewers)
assert out["labels"].split() == ["security"]
assert "Real review." in out["comment"]
assert out["success_count"] == "10"
assert len(out["rejected_labels"]) == 199 * 10 - 1
def test_label_suggestions_at_the_size_bound_are_still_policy_filtered(
tmp_path,
):
# The bound must reject only floods. A 200-entry list — far beyond any real
# review, still well under the argv cap — is filtered normally rather than
# discarded, so tightening the bound to something a genuine review could
# hit fails here.
suggestions = [f"invented-{i:03d}" for i in range(199)] + ["security"]
out = run_combine(
tmp_path,
[{"code": 0, "resp": make_response("Many.", "pass", suggestions)}],
)
assert out["labels"].split() == ["security"]
assert len(out["rejected_labels"]) == 199
assert "oversized labels_added" not in out["stderr"]
def test_multi_document_label_policy_is_rejected(tmp_path):
"""Validation must read the same document the filter applies.
``jq -e FILE`` takes its exit status from the *last* document in the file,
while the filter reads ``$policy[0]`` — the first. A two-document file whose
first document allows human-only labels therefore validated clean and was
then applied. Revert the validator to ``jq -e FILE`` and this test fails:
the helper exits 0 and writes ``code-ready`` into labels.txt.
"""
permissive = {
"apply": ["security", "code-ready"],
"recommend": [],
"aliases": {},
}
strict = {"apply": ["security"], "recommend": [], "aliases": {}}
policy_path = tmp_path / "policy.json"
policy_path.write_text(f"{json.dumps(permissive)}\n{json.dumps(strict)}\n")
(tmp_path / "code_0").write_text("0")
(tmp_path / "resp_0.json").write_text(
make_response("x", "pass", ["security", "code-ready"])
)
(tmp_path / "err_0.log").write_text("")
result = subprocess.run(
["bash", str(SCRIPT), str(tmp_path), "model"],
capture_output=True,
text=True,
env={
"AI_LABEL_POLICY_FILE": str(policy_path),
"PATH": os.environ["PATH"],
},
)
assert result.returncode == 2
assert "invalid AI label policy" in result.stderr
assert not (tmp_path / "labels.txt").exists()
def test_label_policy_normalizes_legacy_pr_labels(tmp_path):
out = run_combine(
tmp_path,
[
{
"code": 0,
"resp": make_response(
"Legacy labels.",
"pass",
["bug", "testing", "code-quality"],
),
}
],
)
assert out["labels"].split() == ["bugfix", "code-quality", "tests"]
assert out["rejected_labels"] == []
def test_full_research_recommendation_wins_over_static(tmp_path):
out = run_combine(
tmp_path,
[
{
"code": 0,
"resp": make_response(
"Research requested.",
"pass",
["ldr_research_static", "ldr_research"],
),
}
],
)
assert out["trigger_labels"].split() == ["ldr_research"]
assert "ldr_research_static" not in out["comment"]
@pytest.mark.parametrize(
"policy",
[
None,
{},
{
"apply": ["security"],
"recommend": ["security"],
"aliases": {},
},
],
ids=["missing", "malformed", "overlapping-sets"],
)
def test_missing_or_invalid_label_policy_fails_closed(tmp_path, policy):
policy_path = tmp_path / "policy.json"
if policy is not None:
policy_path.write_text(json.dumps(policy))
result = subprocess.run(
["bash", str(SCRIPT), str(tmp_path), "model"],
capture_output=True,
text=True,
env={
"AI_LABEL_POLICY_FILE": str(policy_path),
"PATH": os.environ["PATH"],
},
)
assert result.returncode == 2
assert "invalid AI label policy" in result.stderr
def test_model_names_never_leak_into_comment(tmp_path):
out = run_combine(
tmp_path,
[
{"code": 0, "resp": make_response("x", "pass")},
{"code": 0, "resp": make_response("y", "fail")},
],
)
for model in out["models"]:
assert model not in out["comment"]
def test_single_reviewer_uses_singular_wording_and_sticky_marker(tmp_path):
out = run_combine(
tmp_path,
[{"code": 0, "resp": make_response("Solo.", "pass")}],
)
c = out["comment"]
assert "(1 reviewer)" in c
assert "(1 reviewers)" not in c
# Sticky marker must lead the body (the workflow searches for it verbatim).
assert c.startswith("<!-- ai-code-review:sticky -->")
# Commit sha line rendered with real backticks (not escaped backslashes).
assert "_Last reviewed at commit `abc1234`_" in c
def test_blank_line_before_horizontal_rule(tmp_path):
# A "text\n---" with no blank line is a setext H2 in Markdown; ensure the
# divider before the footer is preceded by a blank line.
out = run_combine(
tmp_path,
[{"code": 0, "resp": make_response("Body.", "pass")}],
)
assert "\n\n---\n" in out["comment"]
# --- Regression tests for resilience (one bad reviewer must not sink others) ---
def test_valid_json_non_object_does_not_sink_other_reviewers(tmp_path):
# A model can return valid JSON that is a bare string/array (e.g. a refusal),
# not the expected object. That must degrade to a per-reviewer failure note,
# not crash the whole combine and lose the good review. (Regression: a bare
# `jq .` gate accepted non-objects, then `.review` errored under set -e.)
out = run_combine(
tmp_path,
[
{
"code": 0,
"resp": '"I refuse to review this"',
}, # valid JSON string
{
"code": 0,
"resp": '["also", "not", "an", "object"]',
}, # JSON array
{"code": 0, "resp": make_response("Real review.", "pass", ["bug"])},
],
)
c = out["comment"]
assert "Real review." in c
assert c.count("could not complete its review") == 2
assert out["success_count"] == "1"
assert out["labels"].split() == ["bugfix"]
def test_multi_document_review_response_is_rejected(tmp_path):
"""A concatenated multi-document response must not bypass the object gate.
`jq -e 'type == "object"'` takes its exit status from the *last* JSON
document in the stream, so a two-document response `{...}{...}` — each
document individually a well-formed, in-bounds object — passed the old
gate and then had every document's `labels_added` walked and appended to
the accumulator, silently doubling (or smuggling) label suggestions past
the per-reviewer bound. Revert the `jq -es 'length == 1 and ...'` guard to
the bare `jq -e 'type == "object"'` and this fails: `security` reaches
labels.txt from a reviewer that should have been rejected outright.
"""
doc1 = json.loads(make_response("First doc.", "pass", ["security"]))
doc2 = json.loads(make_response("Second doc.", "fail", ["code-ready"]))
multi_doc_resp = json.dumps(doc1) + json.dumps(doc2)
out = run_combine(
tmp_path,
[
{"code": 0, "resp": multi_doc_resp},
{
"code": 0,
"resp": make_response("Real review.", "pass", ["bug"]),
},
],
)
c = out["comment"]
assert "could not complete its review" in c
assert "Real review." in c
assert out["success_count"] == "1"
# No label from either document of the rejected multi-document response
# reaches the applied set or even the rejected-suggestions list.
assert out["labels"].split() == ["bugfix"]
assert "security" not in out["rejected_labels"]
assert "code-ready" not in out["rejected_labels"]
# The rejected reviewer's (fail-verdict) document must not flip the
# aggregate decision either — it never counted as a usable review.
assert out["decision"] == "pass"
def test_weird_verdict_string_does_not_crash(tmp_path):
# A verdict value containing a quote previously aborted the script via
# `| xargs` (unmatched quote). It must now be handled gracefully.
resp = json.dumps(
{
"review": f"## AI Code Review\n\nBody.\n\n---\n{FOOTER_SINGULAR}",
"fail_pass_workflow": "it's complicated",
"labels_added": [],
}
)
out = run_combine(tmp_path, [{"code": 0, "resp": resp}])
assert out["success_count"] == "1"
# A non-"fail" verdict (even a weird one) does not request changes.
assert out["decision"] == "pass"
def test_empty_exit_code_file_counts_as_failed(tmp_path):
# An empty/unreadable code_<i> must be treated as a failed reviewer, not
# silently counted as a success (regression: `[ "" -ne 0 ]` errored and fell
# through to the success path).
out = run_combine(
tmp_path,
[{"code": "", "resp": make_response("Should be ignored.", "pass")}],
)
assert out["success_count"] == "0"
assert "could not complete its review" in out["comment"]
assert "Should be ignored." not in out["comment"]
def test_non_numeric_exit_code_counts_as_failed(tmp_path):
out = run_combine(
tmp_path,
[{"code": "boom", "resp": make_response("Ignored.", "pass")}],
)
assert out["success_count"] == "0"
assert "integer expression" not in out["stderr"]
def test_mixed_pass_fail_error_aggregation(tmp_path):
out = run_combine(
tmp_path,
[
{"code": 0, "resp": make_response("ok", "pass", ["enhancement"])},
{"code": 0, "resp": make_response("blocker", "fail", ["bug"])},
{"code": 1, "resp": ""}, # hard error
],
)
# Only usable reviews count; the one real "fail" verdict drives the decision;
# labels union only from successful reviewers; exactly one failure note.
assert out["success_count"] == "2"
assert out["decision"] == "fail"
assert out["labels"].split() == ["bugfix", "enhancement"]
assert out["comment"].count("could not complete its review") == 1
def test_valid_json_missing_review_field(tmp_path):
resp = json.dumps({"fail_pass_workflow": "pass", "labels_added": []})
out = run_combine(tmp_path, [{"code": 0, "resp": resp}])
# Object with no .review still counts as a (degenerate) success and renders
# the jq fallback text rather than crashing.
assert out["success_count"] == "1"
assert "No review provided" in out["comment"]
def test_debug_mode_does_not_change_comment_and_keeps_model_in_stderr(tmp_path):
reviewers = [{"code": 0, "resp": make_response("Body.", "pass")}]
# Separate dirs so the two runs' fixtures/outputs don't collide.
plain_dir = tmp_path / "plain"
dbg_dir = tmp_path / "debug"
plain_dir.mkdir()
dbg_dir.mkdir()
plain = run_combine(plain_dir, reviewers)
dbg = run_combine(dbg_dir, reviewers, debug=True)
# The posted comment must be byte-identical regardless of DEBUG_MODE — debug
# content (raw response, model name) goes only to stderr, never the comment.
assert dbg["comment"] == plain["comment"]
# DEBUG must not perturb any of the machine-read outputs either.
assert dbg["decision"] == plain["decision"]
assert dbg["labels"] == plain["labels"]
assert dbg["success_count"] == plain["success_count"]
assert "RAW AI RESPONSE" in dbg["stderr"]
assert "RAW AI RESPONSE" not in plain["stderr"]
# Model name leaks only into stderr logs (anonymization), never the comment.
assert dbg["models"][0] in dbg["stderr"]
assert dbg["models"][0] not in dbg["comment"]
def test_four_reviewers_keep_order(tmp_path):
out = run_combine(
tmp_path,
[
{"code": 0, "resp": make_response(f"body-{n}", "pass")}
for n in range(4)
],
)
c = out["comment"]
positions = [c.index(f"Reviewer {n}") for n in range(1, 5)]
assert positions == sorted(positions)
for n in range(4):
assert f"body-{n}" in c
def test_usage_errors_exit_nonzero(tmp_path):
# Usage errors fail before review assembly or label-policy output.
env = {**os.environ}
no_dir = subprocess.run(
["bash", str(SCRIPT), str(tmp_path / "does-not-exist"), "m"],
capture_output=True,
text=True,
env=env,
)
assert no_dir.returncode == 2
no_models = subprocess.run(
["bash", str(SCRIPT), str(tmp_path)],
capture_output=True,
text=True,
env=env,
)
assert no_models.returncode == 2
def test_exit_code_with_trailing_newline(tmp_path):
# The workflow writes exit codes via `echo $? > code_i`, i.e. WITH a trailing
# newline ("0\n"), whereas the other tests use the bare "0" form. Confirm the
# real workflow shape is handled (command substitution strips the newline).
(tmp_path / "code_0").write_text("0\n")
(tmp_path / "resp_0.json").write_text(make_response("ok", "pass"))
(tmp_path / "err_0.log").write_text("")
result = subprocess.run(
["bash", str(SCRIPT), str(tmp_path), "m"],
capture_output=True,
text=True,
env={
"HEAD_SHA": "abc1234def",
"DEBUG_MODE": "false",
"PATH": os.environ["PATH"],
},
)
assert result.returncode == 0, result.stderr
assert (tmp_path / "success_count.txt").read_text() == "1"