# Copyright (c) 2006-Present, Redis Ltd.
# All rights reserved.
#
# Licensed under your choice of the Redis Source Available License 2.0
# (RSALv2); or (b) the Server Side Public License v1 (SSPLv1); or (c) the
# GNU Affero General Public License v3 (AGPLv3).

"""Action/agent handoff, publication recovery, and real git conflict coverage."""

import json
import os
from pathlib import Path
import subprocess
import sys
import tempfile
import unittest
from unittest.mock import patch, Mock

sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
import common
import resolve_create
import unified


class UnifiedTests(unittest.TestCase):
    def setUp(self):
        temp = tempfile.TemporaryDirectory()
        self.addCleanup(temp.cleanup)
        self.root = Path(temp.name)
        self.addCleanup(patch.stopall)
        patch.dict(os.environ, {
            "RUNNER_TEMP": temp.name, "GITHUB_REPOSITORY": "o/r", "GITHUB_RUN_ID": "123",
            "GITHUB_RUN_ATTEMPT": "1", "GITHUB_WORKSPACE": temp.name,
            "BACKPORT_WORK": str(self.root / "work"), "BACKPORT_BOT_LOGIN": "app[bot]",
            "BACKPORT_MANIFEST_FILE": str(self.root / "manifest.json"),
            "GH_TOKEN": "test", "CREATED_PULL_NUMBERS": "", "SUCCESS_BY_TARGET": "",
        }).start()
        self.ctx = {"pr": 1, "sha": "abcdef1234", "targets": ["8.8", "8.6"], "agent_allowed": True}
        self.outputs = {}
        patch.object(common, "set_output", side_effect=lambda k, v: self.outputs.update({k: v})).start()
        self.state = {"rows": {t: {"target": t, "status": "error", "detail": "unfinished"}
                               for t in self.ctx["targets"]},
                      "action_targets": self.ctx["targets"], "agent_targets": [],
                      "bot": "app[bot]", "comment_ids": [10]}
        unified.write("results", self.state)

    def test_creation_uses_only_canonical_command_and_labels(self):
        labels = {"labels": [{"name": n} for n in ["backport 8.8", "backport-8.2-agent", "backport 8.6"]]}
        for command in ("/backport",):
            self.assertEqual(resolve_create.resolve_targets("issue_comment", "created", command, labels),
                             ["8.8", "8.6"])
            self.assertEqual(resolve_create.resolve_targets("issue_comment", "created", command + " 8.2", labels),
                             ["8.2"])
        for command in ("/backport-agent", "/backport-agent 8.6", "/backport-agent-fix", "/backport-agent-context x",
                        "/backport-fix", "/backport-context x", "/backporting"):
            self.assertEqual(resolve_create.resolve_targets("issue_comment", "created", command, labels), [])

    def test_legacy_labels_do_not_select_targets(self):
        labels = {"labels": [{"name": "backport-8.6-agent"}]}
        for event, action, label, comment in (
            ("pull_request_target", "closed", "", ""),
            ("pull_request_target", "labeled", "backport-8.6-agent", ""),
            ("issue_comment", "created", "", "/backport"),
        ):
            with self.subTest(event=event, action=action):
                self.assertEqual(resolve_create.resolve_targets(event, action, comment, labels), [])

    def test_invalid_explicit_target_is_reported_without_label_fallback(self):
        diagnostics = []
        self.assertEqual(resolve_create.resolve_targets("issue_comment", "created", "/backport bad", {
            "labels": [{"name": "backport 8.6"}]}, diagnostics), [])
        self.assertEqual(diagnostics, ["Invalid target(s): bad"])

    def test_resolver_preserves_fork_clean_path_and_metadata(self):
        os.environ.update(EVENT_NAME="issue_comment", EVENT_ACTION="created", GITHUB_ACTOR="alice",
                          COMMENT_BODY="/backport 8.6", PR_NUMBER_FROM_ISSUE="1")
        pr = {"state": "MERGED", "isCrossRepository": True, "mergeCommit": {"oid": "a" * 40},
              "author": {"login": "author"}, "labels": [{"name": n} for n in (
                  "bug", "backport 8.8", "backport 8.6")]}
        with patch.object(common, "has_write_permission", return_value=True), patch.object(
                common, "fetch_pr", return_value=pr):
            self.assertEqual(resolve_create.main(), 0)
        ctx = unified.read(self.outputs["context_file"])
        self.assertFalse(ctx["agent_allowed"])
        self.assertEqual(ctx["targets"], ["8.6"])
        self.assertEqual(ctx["labels"], ["bug"])
        self.assertEqual(ctx["author"], "author")

    def test_resolver_ignores_label_events_before_fetching_pr(self):
        os.environ.update(EVENT_NAME="pull_request_target", EVENT_ACTION="labeled")
        with patch.object(common, "fetch_pr") as fetch, self.assertRaises(SystemExit):
            resolve_create.main()
        fetch.assert_not_called()
        self.assertEqual(self.outputs["skip"], "true")

    def test_creation_skips_unmerged_prs(self):
        for event, action, comment in (("pull_request_target", "closed", ""),
                                       ("issue_comment", "created", "/backport >= 8.2")):
            for state in ("OPEN", "CLOSED"):
                with self.subTest(event=event, state=state), patch.dict(os.environ, {
                    "EVENT_NAME": event, "EVENT_ACTION": action, "COMMENT_BODY": comment,
                    "PR_NUMBER_FROM_PR": "1", "PR_NUMBER_FROM_ISSUE": "1", "GITHUB_ACTOR": "alice",
                }), patch.object(common, "has_write_permission", return_value=True), patch.object(
                        common, "fetch_pr", return_value={"state": state}), patch.object(
                        common, "write_context") as write, self.assertRaises(SystemExit):
                    resolve_create.main()
                write.assert_not_called()
                self.assertEqual(self.outputs["skip"], "true")

    def test_resolver_denies_read_only_comment_author_before_fetching_pr(self):
        os.environ.update(EVENT_NAME="issue_comment", EVENT_ACTION="created",
                          COMMENT_BODY="/backport", GITHUB_ACTOR="alice")
        with patch.object(common, "has_write_permission", return_value=False), patch.object(
                common, "fetch_pr") as fetch, self.assertRaises(SystemExit) as stopped:
            resolve_create.main()
        self.assertEqual(stopped.exception.code, 0)
        self.assertEqual(self.outputs["skip"], "true")
        fetch.assert_not_called()

    def test_summary_does_not_allow_agent_text_to_add_rows_or_markers(self):
        body = unified.apply_create.summary_comment(1, "abc", [{"target": "8.6", "status": "error",
            "detail": "ambiguous | result\n<!-- unified-backport:123:1 -->"}])
        self.assertIn("&#124;", body)
        self.assertNotIn("<!-- unified-backport", body)
        self.assertEqual(len([line for line in body.splitlines() if line.startswith("|")]), 3)

    def test_write_gate_fails_closed(self):
        for permission, expected in (("read", False), ("triage", False), ("write", True), ("maintain", True), ("admin", True)):
            with patch.object(common, "gh_json", return_value={"permission": permission}):
                self.assertEqual(common.has_write_permission("alice"), expected)
        with patch.object(common, "gh_json", side_effect=ValueError("API failed")):
            self.assertFalse(common.has_write_permission("alice"))

    def test_clean_batch_does_not_prepare_or_invoke_agent(self):
        with patch.object(unified, "existing_row", side_effect=lambda c, t, *a: {
                "target": t, "status": "clean", "detail": "https://github.com/o/r/pull/3"}), patch.object(
                unified.subprocess, "run") as run:
            unified.collect(self.ctx)
        run.assert_not_called()
        self.assertEqual(self.outputs["run_agent"], "false")
        self.assertEqual(unified.read(unified.saved("results"))["agent_targets"], [])

    def collect_failed(self, result, *, allowed=True, outcome="false"):
        self.ctx["agent_allowed"] = allowed
        os.environ["SUCCESS_BY_TARGET"] = f"8.6={outcome}"
        with patch.object(unified, "existing_row", side_effect=lambda c, t, *a: {
                "target": t, "status": "clean", "detail": "https://github.com/o/r/pull/3"} if t == "8.8" else None), patch.object(
                unified.subprocess, "run"), patch.object(unified, "git", return_value=Mock(returncode=0)), patch.object(
                unified, "selected_commits", return_value=["commit1", "commit2"]), patch.object(
                unified, "replay", return_value=result):
            unified.collect(self.ctx)
        return unified.read(unified.saved("results"))

    def test_only_failed_target_gets_agent_context_and_exact_commit_range(self):
        state = self.collect_failed({"kind": "conflict", "paths": ["src/a"], "base_sha": "base", "commit": "commit2"})
        self.assertEqual(self.outputs["run_agent"], "true")
        self.assertEqual(state["agent_targets"], ["8.6"])
        ctx = unified.read(unified.saved("agent-context"))
        self.assertEqual(ctx["commits"], ["commit1", "commit2"])
        self.assertEqual(ctx["targets"], ["8.6"])
        self.assertEqual(state["rows"]["8.8"]["status"], "clean")
        self.assertNotIn("GIT_CONFIG_VALUE_0", os.environ)

    def test_missing_action_output_is_not_success(self):
        state = self.collect_failed({"kind": "conflict", "base_sha": "base"}, outcome="")
        self.assertEqual(state["agent_targets"], ["8.6"])

    def test_fork_conflict_is_manual_without_agent(self):
        state = self.collect_failed({"kind": "conflict"}, allowed=False)
        self.assertEqual(self.outputs["run_agent"], "false")
        self.assertIn("fork-sourced", state["rows"]["8.6"]["detail"])

    def test_publish_failure_and_already_applied_do_not_use_agent(self):
        for kind, status in (("clean", "error"), ("already applied", "already applied")):
            state = self.collect_failed({"kind": kind})
            self.assertEqual(self.outputs["run_agent"], "false")
            self.assertEqual(state["rows"]["8.6"]["status"], status)

    def prepare_agent(self):
        self.state["agent_targets"] = ["8.6"]
        unified.write("results", self.state)
        unified.write("agent-context", {**self.ctx, "targets": ["8.6"]})

    def test_missing_agent_manifest_preserves_clean_rows_and_reports_failure(self):
        self.prepare_agent()
        self.state["rows"]["8.8"]["status"] = "clean"
        unified.write("results", self.state)
        unified.apply(self.ctx)
        rows = unified.read(unified.saved("results"))["rows"]
        self.assertEqual(rows["8.8"]["status"], "clean")
        self.assertEqual(rows["8.6"]["status"], "error")
        self.assertIn("unavailable", rows["8.6"]["detail"])

    def test_invalid_duplicate_and_successful_target_manifests_never_publish(self):
        self.prepare_agent()
        for manifest in ([], {"targets": None}, {"targets": [{"target": "8.8"}]},
                         {"targets": [{"target": "8.6"}, {"target": "8.6"}]}):
            Path(os.environ["BACKPORT_MANIFEST_FILE"]).write_text(json.dumps(manifest))
            with patch.object(common, "PrivilegedGit") as privileged:
                unified.apply(self.ctx)
            privileged.assert_not_called()

    def test_omitted_target_remains_error(self):
        self.prepare_agent()
        Path(os.environ["BACKPORT_MANIFEST_FILE"]).write_text('{"targets": []}')
        with patch.object(common, "PrivilegedGit"):
            unified.apply(self.ctx)
        self.assertIn("omitted", unified.read(unified.saved("results"))["rows"]["8.6"]["detail"])

    def comment(self, ident=20, bot="app[bot]", run="123"):
        return {"id": ident, "user": {"login": bot}, "body":
                f"[Backport-action](https://github.com/korthout/backport-action) in [workflow run {run}](https://github.com/o/r/actions/runs/{run})."}

    def test_progress_then_final_report_updates_same_comment(self):
        comments = [self.comment(10), self.comment(20), self.comment(30, "alice")]
        with patch.object(unified, "api_pages", return_value=comments), patch.object(common, "gh") as gh, patch.object(
                unified, "existing_row", return_value=None):
            unified.progress(self.ctx)
            self.assertIn("repos/o/r/issues/comments/20", gh.call_args.args)
            progress_body = gh.call_args.args[-1].removeprefix("body=")
            self.assertIn("in progress", progress_body)
            self.assertNotIn("failed", progress_body)
            comments[1]["body"] = progress_body
            self.assertEqual(unified.report(self.ctx), 0)
            self.assertEqual(self.outputs["has_failures"], "true")
            self.assertIn("repos/o/r/issues/comments/20", gh.call_args.args)
            self.assertEqual(gh.call_count, 2)

    def test_reporting_api_failure_does_not_claim_success(self):
        with patch.object(unified, "api_pages", return_value=[]), patch.object(
                unified, "existing_row", return_value=None), patch.object(
                common, "gh", side_effect=subprocess.CalledProcessError(1, "gh")):
            with self.assertRaises(subprocess.CalledProcessError):
                unified.report(self.ctx)
        self.assertNotIn("has_failures", self.outputs)
        self.assertTrue(unified.saved("summary").with_suffix(".md").exists())

    def test_finalizer_updates_exact_action_comment_after_partial_failure(self):
        comments = [self.comment(10), self.comment(20), self.comment(30, "alice"), self.comment(40, run="456")]
        with patch.object(unified, "api_pages", return_value=comments), patch.object(common, "gh") as gh, patch.object(
                unified, "existing_row", return_value=None):
            self.assertEqual(unified.report(self.ctx), 0)
            self.assertEqual(self.outputs["has_failures"], "true")
        gh.assert_called_once()
        self.assertIn("repos/o/r/issues/comments/20", gh.call_args.args)
        self.assertIn("PATCH", gh.call_args.args)
        body = gh.call_args.args[-1]
        self.assertIn("8.8", body)
        self.assertIn("8.6", body)
        self.assertTrue(unified.saved("summary").with_suffix(".md").exists())

    def test_finalizer_creates_comment_when_action_never_started(self):
        unified.saved("results").unlink()
        with patch.object(unified, "api_pages", return_value=[]), patch.object(common, "gh") as gh, patch.object(
                unified, "existing_row", return_value=None):
            self.assertEqual(unified.report(self.ctx), 0)
            self.assertEqual(self.outputs["has_failures"], "true")
        self.assertIn("POST", gh.call_args.args)

    def test_finalizer_recovers_prs_created_before_collector_crash(self):
        with patch.object(unified, "api_pages", return_value=[self.comment()]), patch.object(common, "gh"), patch.object(
                unified, "existing_row", side_effect=lambda c, t, *a: {"target": t, "status": "clean", "detail": "https://github.com/o/r/pull/3"}):
            self.assertEqual(unified.report(self.ctx), 0)
            self.assertEqual(self.outputs["has_failures"], "false")

    def test_closed_backport_is_not_reopened(self):
        pr = {"state": "closed", "merged_at": None, "number": 3, "html_url": "https://github.com/o/r/pull/3"}
        with patch.object(unified, "backports", return_value=[pr]):
            self.assertTrue(unified.existing_row(self.ctx, "8.6")["status"].startswith("closed"))

    def test_finalizer_preserves_auto_merge_failure_and_retry_repairs_existing_pr(self):
        self.ctx["targets"] = ["8.6"]
        url = "https://github.com/o/r/pull/3"
        existing = {"target": "8.6", "status": "already open", "detail": url}
        merge = ("pr", "merge", url, "--auto", "--merge")

        def reject_merge(*args, **kwargs):
            if args == merge:
                raise subprocess.CalledProcessError(1, "gh", stderr="permission denied")
            return ""

        with patch.object(unified, "api_pages", return_value=[]), patch.object(
                unified, "existing_row", side_effect=lambda *a: dict(existing)), patch.object(
                common, "gh", side_effect=reject_merge) as gh:
            unified.report(self.ctx)
            self.assertEqual(self.outputs["has_failures"], "true")
            self.assertIn("auto-merge failed", unified.saved("summary").with_suffix(".md").read_text())
            self.assertIn(url, unified.saved("summary").with_suffix(".md").read_text())
            result = unified.read(unified.saved("results"))["rows"]["8.6"]
            self.assertEqual(result["status"], "error")
            self.assertIn("auto-merge failed", result["detail"])
            gh.assert_any_call(*merge)

        # A new request deduplicates to this existing PR before reporting.
        self.state["rows"]["8.6"] = dict(existing)
        unified.write("results", self.state)
        with patch.object(unified, "api_pages", return_value=[]), patch.object(common, "gh") as gh:
            unified.report(self.ctx)
            self.assertEqual(self.outputs["has_failures"], "false")
            gh.assert_any_call(*merge)
            self.assertFalse(any(c.args[:2] == ("pr", "create") for c in gh.call_args_list))

    def test_finalizer_persists_reconciled_results_before_comment_failure(self):
        url = "https://github.com/o/r/pull/3"
        for status in ("clean", "already open", "conflicts(1)"):
            with self.subTest(status=status):
                self.state["rows"]["8.6"] = {"target": "8.6", "status": status, "detail": url}
                unified.write("results", self.state)
                recovered = {"target": "8.8", "status": "already merged", "detail": url}
                with patch.object(unified, "existing_row", return_value=recovered), patch.object(
                        unified, "api_pages", return_value=[]), patch.object(
                        common, "gh", side_effect=subprocess.CalledProcessError(1, "gh")):
                    with self.assertRaises(subprocess.CalledProcessError):
                        unified.report(self.ctx)
                result = unified.read(unified.saved("results"))
                self.assertEqual(result["rows"]["8.8"], recovered)
                self.assertEqual(result["rows"]["8.6"]["status"], "error")
                self.assertIn(url, result["rows"]["8.6"]["detail"])
                self.assertIn("auto-merge failed", result["rows"]["8.6"]["detail"])
                self.assertEqual(result["comment_ids"], self.state["comment_ids"])

    def test_finalizer_does_not_enable_auto_merge_on_closed_or_merged_prs(self):
        for status in ("already merged", "closed — manual intervention"):
            with self.subTest(status=status):
                for row in self.state["rows"].values():
                    row.update(status=status, detail="https://github.com/o/r/pull/3")
                unified.write("results", self.state)
                with patch.object(unified, "api_pages", return_value=[]), patch.object(common, "gh") as gh:
                    unified.report(self.ctx)
                    self.assertFalse(any(c.args[:2] == ("pr", "merge") for c in gh.call_args_list))

    def test_backport_lookup_checks_both_names_and_rejects_forks(self):
        def pr(repo):
            return {"head": {"repo": {"full_name": repo}, "ref": "backport-1-to-8.6"}, "base": {"ref": "8.6"}}
        with patch.object(unified, "api_pages", side_effect=[[], [pr("evil/r"), pr("o/r")]]) as api:
            self.assertEqual(unified.backports(self.ctx, "8.6"), [pr("o/r")])
        self.assertIn("backport-agent/pr-1-to-8.6", api.call_args_list[0].args[0])
        self.assertIn("backport-1-to-8.6", api.call_args_list[1].args[0])

    def test_finalizer_retry_reuses_its_own_marker(self):
        marker = {"id": 20, "user": {"login": "app[bot]"}, "body": "summary <!-- unified-backport:123:1 -->"}
        with patch.object(unified, "api_pages", return_value=[marker]), patch.object(common, "gh") as gh, patch.object(
                unified, "existing_row", return_value=None):
            unified.report(self.ctx)
        self.assertIn("PATCH", gh.call_args.args)
        self.assertIn("repos/o/r/issues/comments/20", gh.call_args.args)

    def test_partial_manifest_applies_success_and_reports_omitted_target(self):
        self.state["agent_targets"] = ["8.8", "8.6"]
        unified.write("results", self.state)
        unified.write("agent-context", self.ctx)
        Path(os.environ["BACKPORT_MANIFEST_FILE"]).write_text(json.dumps({"targets": [{"target": "8.8", "status": "conflicts"}]}))
        with patch.object(common, "PrivilegedGit"), patch.object(unified, "existing_row", return_value=None), patch.object(
                unified, "validate_branch"), patch.object(unified.apply_create, "apply_target", return_value={
                    "target": "8.8", "status": "conflicts(1)", "detail": "https://github.com/o/r/pull/3"}) as apply:
            unified.apply(self.ctx)
        apply.assert_called_once()
        rows = unified.read(unified.saved("results"))["rows"]
        self.assertEqual(rows["8.8"]["status"], "conflicts(1)")
        self.assertIn("omitted", rows["8.6"]["detail"])

    def test_unknown_status_and_missing_conflict_log_do_not_publish(self):
        for status in ("clean", "unknown", "conflicts"):
            with self.assertRaisesRegex(ValueError, "conflict log"):
                unified.validate_branch(self.ctx, Mock(), {"target": "8.6", "status": status})

    def test_agent_decline_is_a_failure(self):
        self.prepare_agent()
        Path(os.environ["BACKPORT_MANIFEST_FILE"]).write_text(json.dumps({"targets": [{
            "target": "8.6", "status": "skipped", "reason": "ambiguous API semantics"}]}))
        with patch.object(common, "PrivilegedGit"), patch.object(unified, "existing_row", return_value=None):
            unified.apply(self.ctx)
        row = unified.read(unified.saved("results"))["rows"]["8.6"]
        self.assertEqual(row["status"], "error")
        self.assertEqual(row["detail"], "ambiguous API semantics")

    def test_api_errors_cannot_be_treated_as_no_existing_backport(self):
        with patch.object(common, "gh", side_effect=subprocess.CalledProcessError(1, "gh")):
            with self.assertRaises(subprocess.CalledProcessError):
                unified.backports(self.ctx, "8.6")

    def test_merge_selection_matches_action_for_squash_rebase_and_merge(self):
        # Each fixture describes the same inputs queried by the pinned action.
        cases = [
            (["parent"], ["source"], {}, ["abcdef1234"]),
            (["parent"], ["first", "last"], {"parent": False, "abcdef1234": True}, ["abcdef1234"]),
            (["parent"], ["first", "last"], {"parent": True, "abcdef1234": True}, ["rebased1", "rebased2"]),
            (["p1", "p2"], ["first", "last"], {}, ["first", "last"]),
            (["parent"], ["first", "last"], {"parent": False, "abcdef1234": False}, ["first", "last"]),
        ]
        for parents, commits, associations, expected in cases:
            def api(endpoint):
                if "/pulls/1/commits?" in endpoint:
                    return [{"sha": c} for c in commits]
                sha = endpoint.split("/commits/")[1].split("/")[0]
                return [{"number": 1}] if associations[sha] else []
            def git(work, *args, **kwargs):
                if args[0] == "rev-list":
                    return Mock(stdout="rebased1\nrebased2\n")
                return Mock(stdout=" ".join(parents) if args[-1] == self.ctx["sha"] else "parent")
            with self.subTest(expected=expected), patch.object(unified, "api_pages", side_effect=api), patch.object(
                    unified, "git", side_effect=git):
                self.assertEqual(unified.selected_commits(self.ctx, "/work"), expected)

    def test_merge_selection_skips_source_merge_commits(self):
        def git(work, *args, **kwargs):
            return Mock(stdout="p1 p2" if args[-1] in {self.ctx["sha"], "merge"} else "parent")
        with patch.object(unified, "api_pages", return_value=[{"sha": "first"}, {"sha": "merge"}]), patch.object(
                unified, "git", side_effect=git):
            self.assertEqual(unified.selected_commits(self.ctx, "/work"), ["first"])


class GitReplayTests(unittest.TestCase):
    def setUp(self):
        temp = tempfile.TemporaryDirectory()
        self.addCleanup(temp.cleanup)
        self.work = temp.name
        self.file = Path(self.work, "file")
        self.git("init", "-b", "master")
        self.git("config", "user.name", "Test")
        self.git("config", "user.email", "test@example.com")
        self.base = self.commit("base")
        self.git("update-ref", "refs/remotes/origin/8.8", self.base)
        self.source = self.commit("source")
        self.git("checkout", "--detach", self.base)
        self.target = self.commit("target")
        self.git("update-ref", "refs/remotes/origin/8.6", self.target)

    def git(self, *args):
        return subprocess.run(["git", "-C", self.work, *args], check=True, text=True, capture_output=True).stdout.strip()

    def commit(self, content):
        self.file.write_text(content + "\n")
        self.git("add", "file")
        self.git("commit", "-m", content)
        return self.git("rev-parse", "HEAD")

    def test_conflict_produces_evidence_and_cleans_sequencer(self):
        result = unified.replay(self.work, "8.6", [self.source])
        self.assertEqual(result["kind"], "conflict")
        self.assertEqual(result["paths"], ["file"])
        self.assertEqual(result["base_sha"], self.target)
        self.assertEqual(self.git("status", "--porcelain"), "")
        self.assertFalse(Path(self.work, ".git", "CHERRY_PICK_HEAD").exists())

    def test_clean_and_empty_picks_are_distinguished(self):
        self.assertEqual(unified.replay(self.work, "8.8", [self.source])["kind"], "clean")
        self.git("update-ref", "refs/remotes/origin/8.8", self.source)
        self.assertEqual(unified.replay(self.work, "8.8", [self.source])["kind"], "already applied")

    def test_empty_first_commit_does_not_drop_later_changes(self):
        self.git("checkout", "--detach", self.source)
        later = self.commit("later")
        self.git("update-ref", "refs/remotes/origin/8.8", self.source)
        self.assertEqual(unified.replay(self.work, "8.8", [self.source, later])["kind"], "clean")

    def test_partial_backport_handoff_publishes_remainder_without_agent(self):
        self.git("checkout", "--detach", self.source)
        later = self.commit("later")
        self.git("branch", "8.6", self.source)
        with tempfile.TemporaryDirectory() as run_temp:
            work = str(Path(run_temp, "clone"))
            ctx = {"pr": 1, "sha": later, "targets": ["8.6"], "agent_allowed": True}
            outputs = {}
            actual_git = unified.git
            def git(work, *args, **kwargs):
                if args[0] == "fetch":
                    return actual_git(work, "update-ref", "refs/remotes/origin/8.6", self.source)
                return actual_git(work, *args, **kwargs)
            env = {"RUNNER_TEMP": run_temp, "BACKPORT_WORK": work, "GITHUB_WORKSPACE": self.work,
                   "GITHUB_REPOSITORY": "o/r", "GH_TOKEN": "test", "SUCCESS_BY_TARGET": "8.6=false",
                   "GIT_COMMITTER_NAME": "Test", "GIT_COMMITTER_EMAIL": "test@example.com",
                   "CREATED_PULL_NUMBERS": ""}
            with patch.dict(os.environ, env), patch.object(unified, "existing_row", return_value=None), patch.object(
                    unified, "selected_commits", return_value=[self.source, later]), patch.object(
                    unified, "git", side_effect=git), patch.object(common, "set_output", side_effect=lambda k,v: outputs.update({k:v})):
                unified.write("results", {"rows": {"8.6": {"target": "8.6", "status": "error", "detail": ""}},
                                          "action_targets": ["8.6"], "agent_targets": []})
                unified.collect(ctx)
                self.assertEqual(outputs["run_agent"], "false")
                self.assertEqual(outputs["apply_needed"], "true")
                entry = unified.read(unified.saved("results"))["clean_recoveries"]["8.6"]
                self.assertEqual(entry["skipped_commits"], [self.source])
                self.assertEqual(actual_git(work, "show", entry["branch"] + ":file").stdout, "later\n")
                def publish_git(*args, **kwargs):
                    if args[0] == "ls-remote":
                        return Mock(stdout=self.source + " refs/heads/8.6", returncode=0)
                    return actual_git(work, *args, **kwargs)
                with patch.object(common, "PrivilegedGit", return_value=publish_git), patch.object(
                        unified.apply_create, "apply_target", return_value={"target": "8.6", "status": "clean", "detail": "https://github.com/o/r/pull/3"}) as publish:
                    unified.apply(ctx)
                    publish.assert_called_once()
                    self.assertEqual(unified.read(unified.saved("results"))["rows"]["8.6"]["status"], "clean")
                    # A model cannot replace the prepared ref before privileged publication.
                    actual_git(work, "update-ref", "refs/heads/" + entry["branch"], self.source)
                    publish.reset_mock()
                    unified.apply(ctx)
                    publish.assert_not_called()
                    self.assertIn("modified after triage", unified.read(unified.saved("results"))["rows"]["8.6"]["detail"])

    def test_publish_validation_rejects_wrong_base_and_extra_commits(self):
        ctx = {"pr": 1, "commits": [self.source], "failures": {"8.6": {"base_sha": self.target}}}
        entry = {"target": "8.6", "branch": "backport-agent/pr-1-to-8.6", "status": "conflicts", "conflict_log": [{}]}
        self.git("checkout", "-b", entry["branch"], self.target)
        self.commit("resolved")
        def git(*args, **kwargs):
            if args[0] == "ls-remote":
                return Mock(stdout=f"{self.target} refs/heads/8.6")
            return unified.git(self.work, *args, **kwargs)
        unified.validate_branch(ctx, git, entry)
        self.commit("unrelated extra commit")
        with self.assertRaisesRegex(ValueError, "commit count"):
            unified.validate_branch(ctx, git, entry)
        ctx["failures"]["8.6"]["base_sha"] = self.source
        with self.assertRaisesRegex(ValueError, "authorized base"):
            unified.validate_branch(ctx, git, entry)


if __name__ == "__main__":
    unittest.main()
