#!/usr/bin/env python3 """Tests for cve-fix/scripts/unattended.py. Covers request validation (type safety, operation-specific rules, Jira completeness, contradictions), result construction (lifecycle_complete, jira_results, consistency), scope deconfliction (live-PR awareness, self-exclusion, malformed state), state loading (file vs evidence distinction, invalid JSON), stage resumption (prerequisite reporting), human decision validation (identity binding, boolean accepted, effect authorization, rejection semantics, timestamp, justification), context derivation, and CLI integration. """ from __future__ import annotations import argparse import importlib.util import io import json import os import sys import tempfile import textwrap import typing import unittest from pathlib import Path from typing import Any from unittest.mock import MagicMock _SCRIPT = Path(__file__).resolve().parent / "unattended.py" _spec = importlib.util.spec_from_file_location("unattended", _SCRIPT) assert _spec and _spec.loader unattended = importlib.util.module_from_spec(_spec) sys.modules["unattended"] = unattended _spec.loader.exec_module(unattended) # --------------------------------------------------------------------------- # Request Validation — basic # --------------------------------------------------------------------------- class TestValidateRequestBasic(unittest.TestCase): """validate_request: well-formed and malformed requests.""" def test_valid_remediate_with_cve(self) -> None: req = {"operation": "remediate", "cve_ids": ["CVE-2024-1234"]} self.assertEqual(unattended.validate_request(req), []) def test_valid_remediate_with_jira(self) -> None: req = {"operation": "remediate", "jira_tickets": ["EDM-1234"]} self.assertEqual(unattended.validate_request(req), []) def test_valid_remediate_with_both(self) -> None: req = { "operation": "remediate", "cve_ids": ["CVE-2024-1234"], "jira_tickets": ["EDM-1234"], } self.assertEqual(unattended.validate_request(req), []) def test_remediate_with_pr_url_existing(self) -> None: """remediate accepts pr_url for existing-PR continuation.""" req = { "operation": "remediate", "pr_url": "https://github.com/org/repo/pull/42", "cve_ids": ["CVE-2024-1234"], } self.assertEqual(unattended.validate_request(req), []) def test_remediate_with_only_pr_url(self) -> None: """remediate accepts pr_url alone for existing-PR continuation.""" req = { "operation": "remediate", "pr_url": "https://github.com/org/repo/pull/42", } self.assertEqual(unattended.validate_request(req), []) def test_remediate_with_only_work_ref(self) -> None: req = { "operation": "remediate", "work_ref": "cve-fix/EDM-1234", } self.assertEqual(unattended.validate_request(req), []) def test_remediate_missing_all_identifiers(self) -> None: req = {"operation": "remediate"} errors = unattended.validate_request(req) self.assertTrue(len(errors) >= 1) self.assertTrue(any("remediate" in e for e in errors)) def test_missing_operation(self) -> None: req = {"cve_ids": ["CVE-2024-1234"]} errors = unattended.validate_request(req) self.assertTrue(any("operation" in e for e in errors)) def test_invalid_operation(self) -> None: req = {"operation": "advance"} # removed operation errors = unattended.validate_request(req) self.assertTrue(any("operation" in e for e in errors)) def test_invalid_cve_format(self) -> None: req = {"operation": "remediate", "cve_ids": ["not-a-cve"]} errors = unattended.validate_request(req) self.assertTrue(any("CVE ID format" in e for e in errors)) def test_invalid_jira_key_format(self) -> None: req = {"operation": "remediate", "jira_tickets": ["bad-key"]} errors = unattended.validate_request(req) self.assertTrue(any("Jira ticket key" in e for e in errors)) # --- Trailing newline regression (fullmatch) --- def test_cve_trailing_lf_rejected(self) -> None: """CVE ID with trailing newline must be rejected (fullmatch).""" req = {"operation": "remediate", "cve_ids": ["CVE-2024-1234\n"]} errors = unattended.validate_request(req) self.assertTrue(any("CVE ID format" in e for e in errors)) def test_cve_trailing_crlf_rejected(self) -> None: """CVE ID with trailing CRLF must be rejected.""" req = {"operation": "remediate", "cve_ids": ["CVE-2024-1234\r\n"]} errors = unattended.validate_request(req) self.assertTrue(any("CVE ID format" in e for e in errors)) def test_jira_ticket_trailing_lf_rejected(self) -> None: """Jira ticket with trailing newline must be rejected (fullmatch).""" req = {"operation": "remediate", "jira_tickets": ["EDM-1234\n"]} errors = unattended.validate_request(req) self.assertTrue(any("Jira ticket key" in e for e in errors)) def test_jira_ticket_trailing_crlf_rejected(self) -> None: """Jira ticket with trailing CRLF must be rejected.""" req = {"operation": "remediate", "jira_tickets": ["EDM-1234\r\n"]} errors = unattended.validate_request(req) self.assertTrue(any("Jira ticket key" in e for e in errors)) def test_jira_action_ticket_trailing_lf_rejected(self) -> None: """jira_action.tickets with trailing LF must be rejected.""" req = { "operation": "jira_action", "jira_action": { "action": "comment", "tickets": ["EDM-1234\n"], "comment": "test", }, } errors = unattended.validate_request(req) self.assertTrue(any("invalid key" in e for e in errors)) def test_jira_action_ticket_trailing_crlf_rejected(self) -> None: """jira_action.tickets with trailing CRLF must be rejected.""" req = { "operation": "jira_action", "jira_action": { "action": "comment", "tickets": ["EDM-1234\r\n"], "comment": "test", }, } errors = unattended.validate_request(req) self.assertTrue(any("invalid key" in e for e in errors)) def test_canonical_ids_still_accepted(self) -> None: """Clean CVE / Jira IDs are still accepted after fullmatch.""" req_cve = {"operation": "remediate", "cve_ids": ["CVE-2024-1234"]} self.assertEqual(unattended.validate_request(req_cve), []) req_jira = {"operation": "remediate", "jira_tickets": ["EDM-1234"]} self.assertEqual(unattended.validate_request(req_jira), []) req_action = { "operation": "jira_action", "jira_action": { "action": "comment", "tickets": ["EDM-1234"], "comment": "test", }, } self.assertEqual(unattended.validate_request(req_action), []) def test_duplicate_nested_tickets_still_rejected_after_fullmatch( self, ) -> None: """Duplicate jira_action.tickets still rejected after fullmatch.""" req = { "operation": "jira_action", "jira_action": { "action": "comment", "tickets": ["EDM-1", "EDM-1"], "comment": "test", }, } errors = unattended.validate_request(req) self.assertTrue(any("duplicate" in e for e in errors)) class TestDeriveContextFullmatch(unittest.TestCase): """derive_context must not recognize padded IDs as artifact identifiers.""" def test_jira_ticket_trailing_lf_falls_through(self) -> None: """Trailing LF on jira_tickets[0] should not be used as context.""" ctx = unattended.derive_context({"jira_tickets": ["EDM-1234\n"]}) self.assertNotIn("\n", ctx) self.assertNotEqual(ctx, "EDM-1234\n") def test_cve_trailing_lf_falls_through(self) -> None: """Trailing LF on cve_ids[0] should not be used as context.""" ctx = unattended.derive_context({"cve_ids": ["CVE-2024-1234\n"]}) self.assertNotIn("\n", ctx) self.assertNotEqual(ctx, "CVE-2024-1234\n") def test_clean_jira_still_preferred(self) -> None: """Clean Jira ticket is still preferred over CVE for context.""" ctx = unattended.derive_context({ "jira_tickets": ["EDM-1234"], "cve_ids": ["CVE-2024-5678"], }) self.assertEqual(ctx, "EDM-1234") def test_clean_cve_fallback_works(self) -> None: """Clean CVE ID still used as fallback context.""" ctx = unattended.derive_context({"cve_ids": ["CVE-2024-5678"]}) self.assertEqual(ctx, "CVE-2024-5678") def test_jira_ticket_trailing_crlf_falls_through(self) -> None: """Trailing CRLF on jira_tickets[0] should not be used as context.""" ctx = unattended.derive_context( {"jira_tickets": ["EDM-1234\r\n"]}, ) self.assertNotIn("\r", ctx) self.assertNotIn("\n", ctx) self.assertNotEqual(ctx, "EDM-1234\r\n") def test_cve_trailing_crlf_falls_through(self) -> None: """Trailing CRLF on cve_ids[0] should not be used as context.""" ctx = unattended.derive_context( {"cve_ids": ["CVE-2024-1234\r\n"]}, ) self.assertNotIn("\r", ctx) self.assertNotIn("\n", ctx) self.assertNotEqual(ctx, "CVE-2024-1234\r\n") class TestTrailingNewlineCLI(unittest.TestCase): """CLI validate rejects trailing newlines in identifiers.""" def test_cli_cve_trailing_lf_rejected(self) -> None: """CLI validate rejects CVE ID with trailing LF.""" import io from unittest.mock import patch as _patch with tempfile.TemporaryDirectory() as td: req_path = os.path.join(td, "req.json") with open(req_path, "w") as f: json.dump({ "operation": "remediate", "cve_ids": ["CVE-2024-1234\n"], }, f) args = argparse.Namespace(request_file=req_path) stdout_cap = io.StringIO() stderr_cap = io.StringIO() with _patch("sys.stdout", stdout_cap), \ _patch("sys.stderr", stderr_cap): rc = unattended._cmd_validate(args) self.assertEqual(rc, 1) out_json = json.loads(stdout_cap.getvalue()) self.assertFalse(out_json["valid"]) self.assertTrue( any("CVE ID format" in e for e in out_json["errors"]), ) def test_cli_jira_action_ticket_trailing_lf_rejected(self) -> None: """CLI validate rejects jira_action.tickets with trailing LF.""" import io from unittest.mock import patch as _patch with tempfile.TemporaryDirectory() as td: req_path = os.path.join(td, "req.json") with open(req_path, "w") as f: json.dump({ "operation": "jira_action", "jira_action": { "action": "comment", "tickets": ["EDM-1234\n"], "comment": "test", }, }, f) args = argparse.Namespace(request_file=req_path) stdout_cap = io.StringIO() stderr_cap = io.StringIO() with _patch("sys.stdout", stdout_cap), \ _patch("sys.stderr", stderr_cap): rc = unattended._cmd_validate(args) self.assertEqual(rc, 1) out_json = json.loads(stdout_cap.getvalue()) self.assertFalse(out_json["valid"]) self.assertTrue( any("invalid key" in e for e in out_json["errors"]), ) # --------------------------------------------------------------------------- # Request Validation — type safety (finding 6) # --------------------------------------------------------------------------- class TestValidateRequestTypes(unittest.TestCase): """validate_request: type enforcement for all fields.""" def test_non_dict_request(self) -> None: errors = unattended.validate_request("not a dict") self.assertEqual(errors, ["Request must be a JSON object"]) def test_non_dict_request_list(self) -> None: errors = unattended.validate_request([1, 2]) self.assertEqual(errors, ["Request must be a JSON object"]) def test_operation_not_string(self) -> None: req = {"operation": 123} errors = unattended.validate_request(req) self.assertTrue(any("string" in e for e in errors)) def test_cve_ids_not_array(self) -> None: req = {"operation": "remediate", "cve_ids": "CVE-2024-1"} errors = unattended.validate_request(req) self.assertTrue(any("array" in e for e in errors)) def test_cve_ids_element_not_string(self) -> None: req = {"operation": "remediate", "cve_ids": [123]} errors = unattended.validate_request(req) self.assertTrue(any("string" in e for e in errors)) def test_boolean_as_max_retries(self) -> None: """bool is int subclass in Python; must reject True/False.""" req = { "operation": "remediate", "cve_ids": ["CVE-2024-1"], "max_retries": True, } errors = unattended.validate_request(req) self.assertTrue(any("max_retries" in e for e in errors)) def test_negative_max_retries(self) -> None: req = { "operation": "remediate", "cve_ids": ["CVE-2024-1"], "max_retries": -1, } errors = unattended.validate_request(req) self.assertTrue(any("max_retries" in e for e in errors)) def test_overrides_not_dict(self) -> None: req = { "operation": "remediate", "cve_ids": ["CVE-2024-1"], "overrides": "invalid", } errors = unattended.validate_request(req) self.assertTrue(any("overrides" in e for e in errors)) def test_time_budget_valid(self) -> None: req = { "operation": "remediate", "cve_ids": ["CVE-2024-1"], "time_budget_seconds": 3600, } self.assertEqual(unattended.validate_request(req), []) def test_time_budget_invalid(self) -> None: req = { "operation": "remediate", "cve_ids": ["CVE-2024-1"], "time_budget_seconds": -100, } errors = unattended.validate_request(req) self.assertTrue(any("time_budget" in e for e in errors)) def test_time_budget_boolean(self) -> None: req = { "operation": "remediate", "cve_ids": ["CVE-2024-1"], "time_budget_seconds": True, } errors = unattended.validate_request(req) self.assertTrue(any("time_budget" in e for e in errors)) def test_stage_not_string(self) -> None: req = { "operation": "remediate", "cve_ids": ["CVE-2024-1"], "stage": 42, } errors = unattended.validate_request(req) self.assertTrue(any("stage" in e for e in errors)) # --------------------------------------------------------------------------- # Request Validation — Jira completeness (finding 6) # --------------------------------------------------------------------------- class TestValidateRequestJira(unittest.TestCase): """Jira action field completeness and cross-operation rejection.""" def test_transition_missing_target_status(self) -> None: req = { "operation": "jira_action", "jira_action": { "action": "transition", "tickets": ["EDM-1234"], }, } errors = unattended.validate_request(req) self.assertTrue(any("target_status" in e for e in errors)) def test_transition_blank_target_status(self) -> None: req = { "operation": "jira_action", "jira_action": { "action": "transition", "tickets": ["EDM-1234"], "target_status": " ", }, } errors = unattended.validate_request(req) self.assertTrue(any("target_status" in e for e in errors)) def test_comment_missing_comment(self) -> None: req = { "operation": "jira_action", "jira_action": { "action": "comment", "tickets": ["EDM-1234"], }, } errors = unattended.validate_request(req) self.assertTrue(any("comment" in e for e in errors)) def test_comment_blank_comment(self) -> None: req = { "operation": "jira_action", "jira_action": { "action": "comment", "tickets": ["EDM-1234"], "comment": "", }, } errors = unattended.validate_request(req) self.assertTrue(any("comment" in e for e in errors)) def test_jira_action_on_wrong_operation(self) -> None: """jira_action field on remediate is rejected.""" req = { "operation": "remediate", "cve_ids": ["CVE-2024-1"], "jira_action": {"action": "transition", "tickets": ["EDM-1"]}, } errors = unattended.validate_request(req) self.assertTrue(any("only valid with" in e for e in errors)) def test_jira_action_valid_transition(self) -> None: req = { "operation": "jira_action", "jira_action": { "action": "transition", "tickets": ["EDM-1234"], "target_status": "MODIFIED", }, } self.assertEqual(unattended.validate_request(req), []) def test_jira_action_valid_comment(self) -> None: req = { "operation": "jira_action", "jira_action": { "action": "comment", "tickets": ["EDM-1234"], "comment": "CVE fix merged.", }, } self.assertEqual(unattended.validate_request(req), []) def test_jira_action_invalid_ticket_keys(self) -> None: req = { "operation": "jira_action", "jira_action": { "action": "transition", "tickets": ["not-valid"], "target_status": "MODIFIED", }, } errors = unattended.validate_request(req) self.assertTrue(any("invalid key" in e for e in errors)) def test_jira_action_not_dict(self) -> None: req = {"operation": "jira_action", "jira_action": "invalid"} errors = unattended.validate_request(req) self.assertTrue(any("object" in e for e in errors)) # --------------------------------------------------------------------------- # Request Validation — backport # --------------------------------------------------------------------------- class TestValidateRequestBackport(unittest.TestCase): def test_backport_missing_target_branches(self) -> None: req = {"operation": "backport", "work_ref": "cve-fix/EDM-1234"} errors = unattended.validate_request(req) self.assertTrue(any("target_branches" in e for e in errors)) def test_backport_valid(self) -> None: req = { "operation": "backport", "pr_url": "https://github.com/org/repo/pull/42", "target_branches": ["release-2.16"], } self.assertEqual(unattended.validate_request(req), []) def test_backport_missing_work_ref(self) -> None: req = { "operation": "backport", "target_branches": ["release-2.16"], } errors = unattended.validate_request(req) self.assertTrue(any("work_ref" in e for e in errors)) def test_backport_blank_target_branch_rejected(self) -> None: """Blank string in target_branches is rejected early.""" req = { "operation": "backport", "pr_url": "https://github.com/org/repo/pull/42", "target_branches": ["release-2.16", " "], } errors = unattended.validate_request(req) self.assertTrue( any("nonempty" in e for e in errors), f"Expected nonempty-string error, got: {errors}", ) def test_backport_empty_string_target_rejected(self) -> None: """Empty string in target_branches is rejected early.""" req = { "operation": "backport", "pr_url": "https://github.com/org/repo/pull/42", "target_branches": ["release-2.16", ""], } errors = unattended.validate_request(req) self.assertTrue( any("nonempty" in e for e in errors), f"Expected nonempty-string error, got: {errors}", ) def test_backport_none_target_entry_rejected(self) -> None: """None entry in target_branches is rejected early.""" req = { "operation": "backport", "pr_url": "https://github.com/org/repo/pull/42", "target_branches": ["release-2.16", None], } errors = unattended.validate_request(req) self.assertTrue( any("nonempty" in e for e in errors), f"Expected nonempty-string error, got: {errors}", ) def test_backport_nonstring_target_entry_rejected(self) -> None: """Non-string entry in target_branches is rejected early.""" req = { "operation": "backport", "pr_url": "https://github.com/org/repo/pull/42", "target_branches": ["release-2.16", 42], } errors = unattended.validate_request(req) self.assertTrue( any("nonempty" in e for e in errors), f"Expected nonempty-string error, got: {errors}", ) def test_backport_duplicate_target_branches_rejected(self) -> None: """Duplicate entries in target_branches rejected early.""" req = { "operation": "backport", "pr_url": "https://github.com/org/repo/pull/42", "target_branches": ["release-2.16", "release-2.16"], } errors = unattended.validate_request(req) self.assertTrue( any("duplicate" in e for e in errors), f"Expected duplicate error, got: {errors}", ) def test_backport_valid_unique_passes(self) -> None: """Valid unique target_branches passes validation.""" req = { "operation": "backport", "pr_url": "https://github.com/org/repo/pull/42", "target_branches": ["release-2.16", "release-2.17"], } self.assertEqual(unattended.validate_request(req), []) def test_backport_leading_whitespace_rejected(self) -> None: """Leading whitespace in target_branches is rejected, not trimmed.""" req = { "operation": "backport", "pr_url": "https://github.com/org/repo/pull/42", "target_branches": [" release-2.16"], } errors = unattended.validate_request(req) self.assertTrue( any("leading" in e and "trailing" in e for e in errors), f"Expected whitespace error, got: {errors}", ) def test_backport_trailing_whitespace_rejected(self) -> None: """Trailing whitespace in target_branches is rejected, not trimmed.""" req = { "operation": "backport", "pr_url": "https://github.com/org/repo/pull/42", "target_branches": ["release-2.16 "], } errors = unattended.validate_request(req) self.assertTrue( any("leading" in e and "trailing" in e for e in errors), f"Expected whitespace error, got: {errors}", ) def test_backport_both_whitespace_rejected(self) -> None: """Both leading and trailing whitespace rejected.""" req = { "operation": "backport", "pr_url": "https://github.com/org/repo/pull/42", "target_branches": [" release-2.16 "], } errors = unattended.validate_request(req) self.assertTrue( any("whitespace" in e for e in errors), f"Expected whitespace error, got: {errors}", ) def test_backport_tab_whitespace_rejected(self) -> None: """Tab characters in target_branches are rejected.""" req = { "operation": "backport", "pr_url": "https://github.com/org/repo/pull/42", "target_branches": ["\trelease-2.16"], } errors = unattended.validate_request(req) self.assertTrue( any("whitespace" in e for e in errors), f"Expected whitespace error, got: {errors}", ) def test_backport_newline_whitespace_rejected(self) -> None: """Newline characters in target_branches are rejected.""" req = { "operation": "backport", "pr_url": "https://github.com/org/repo/pull/42", "target_branches": ["release-2.16\n"], } errors = unattended.validate_request(req) self.assertTrue( any("whitespace" in e for e in errors), f"Expected whitespace error, got: {errors}", ) def test_backport_canonical_branch_no_whitespace_passes(self) -> None: """Canonical branch names without whitespace pass.""" req = { "operation": "backport", "pr_url": "https://github.com/org/repo/pull/42", "target_branches": ["release-2.16", "release-2.17"], } self.assertEqual(unattended.validate_request(req), []) # --------------------------------------------------------------------------- # Request Validation — target_branches whitespace (CLI) # --------------------------------------------------------------------------- class TestBackportWhitespaceCLI(unittest.TestCase): """CLI validate rejects whitespace-padded target_branches.""" def test_cli_leading_whitespace_rejected(self) -> None: """CLI rejects leading whitespace in target_branches.""" import io from unittest.mock import patch as _patch with tempfile.TemporaryDirectory() as td: req_path = os.path.join(td, "req.json") with open(req_path, "w") as f: json.dump({ "operation": "backport", "pr_url": "https://github.com/o/r/pull/1", "target_branches": [" release-2.16"], }, f) args = argparse.Namespace(request_file=req_path) stdout_cap = io.StringIO() stderr_cap = io.StringIO() with _patch("sys.stdout", stdout_cap), _patch("sys.stderr", stderr_cap): rc = unattended._cmd_validate(args) self.assertEqual(rc, 1) out_json = json.loads(stdout_cap.getvalue()) self.assertFalse(out_json["valid"]) self.assertTrue( any("whitespace" in e for e in out_json["errors"]), f"Expected whitespace error, got: {out_json['errors']}", ) def test_cli_trailing_whitespace_rejected(self) -> None: """CLI rejects trailing whitespace in target_branches.""" import io from unittest.mock import patch as _patch with tempfile.TemporaryDirectory() as td: req_path = os.path.join(td, "req.json") with open(req_path, "w") as f: json.dump({ "operation": "backport", "pr_url": "https://github.com/o/r/pull/1", "target_branches": ["release-2.16\t"], }, f) args = argparse.Namespace(request_file=req_path) stdout_cap = io.StringIO() stderr_cap = io.StringIO() with _patch("sys.stdout", stdout_cap), _patch("sys.stderr", stderr_cap): rc = unattended._cmd_validate(args) self.assertEqual(rc, 1) out_json = json.loads(stdout_cap.getvalue()) self.assertFalse(out_json["valid"]) self.assertTrue( any("whitespace" in e for e in out_json["errors"]), f"Expected whitespace error, got: {out_json['errors']}", ) def test_cli_padded_expected_list_rejected(self) -> None: """make-result: padded entries in expected target_branches rejected.""" import io from unittest.mock import patch as _patch with tempfile.TemporaryDirectory() as td: args = argparse.Namespace( status="completed", operation="backport", artifact_dir=td, data=json.dumps({ "scope": {"cve_ids": ["CVE-2024-1"]}, "backport_results": [ {"target_branch": "rel-1", "status": "completed", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}}, ], "target_branches": [" rel-1 "], }), ) captured = io.StringIO() with _patch("sys.stderr", captured): rc = unattended._cmd_make_result(args) self.assertEqual(rc, 1, "Padded target_branches must be rejected") self.assertIn("whitespace", captured.getvalue()) self.assertFalse( Path(td, "unattended-result.json").exists(), "Rejected make-result must not write artifact", ) def test_cli_padded_child_vs_canonical_expected(self) -> None: """make-result: padded child target_branch cannot match canonical expected.""" import io from unittest.mock import patch as _patch with tempfile.TemporaryDirectory() as td: args = argparse.Namespace( status="completed", operation="backport", artifact_dir=td, data=json.dumps({ "scope": {"cve_ids": ["CVE-2024-1"]}, "backport_results": [ {"target_branch": " rel-1 ", "status": "completed", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}}, ], "target_branches": ["rel-1"], }), ) captured = io.StringIO() with _patch("sys.stderr", captured): rc = unattended._cmd_make_result(args) self.assertEqual(rc, 1, "Padded child target_branch must not match") stderr_out = captured.getvalue() self.assertTrue( "target branch" in stderr_out or "whitespace" in stderr_out, f"Expected target branch or whitespace error, got: {stderr_out}", ) self.assertFalse( Path(td, "unattended-result.json").exists(), "Rejected make-result must not write artifact", ) # --------------------------------------------------------------------------- # make_result whitespace target_branches guard (API) # --------------------------------------------------------------------------- class TestMakeResultTargetBranchesWhitespace(unittest.TestCase): """make_result rejects whitespace-padded target_branches at success.""" _VALID_CHILD = { "target_branch": "rel-1", "status": "completed", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}, } def test_leading_whitespace_rejected(self) -> None: with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[self._VALID_CHILD], target_branches=[" rel-1"], ) self.assertIn("whitespace", str(ctx.exception)) def test_trailing_whitespace_rejected(self) -> None: with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[self._VALID_CHILD], target_branches=["rel-1 "], ) self.assertIn("whitespace", str(ctx.exception)) def test_both_whitespace_rejected(self) -> None: with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[self._VALID_CHILD], target_branches=[" rel-1 "], ) self.assertIn("whitespace", str(ctx.exception)) def test_tab_whitespace_rejected(self) -> None: with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[self._VALID_CHILD], target_branches=["\trel-1"], ) self.assertIn("whitespace", str(ctx.exception)) def test_newline_whitespace_rejected(self) -> None: with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[self._VALID_CHILD], target_branches=["rel-1\n"], ) self.assertIn("whitespace", str(ctx.exception)) def test_canonical_branch_passes(self) -> None: result = unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[self._VALID_CHILD], target_branches=["rel-1"], ) self.assertEqual(result["status"], "completed") self.assertEqual(result["target_branches"], ["rel-1"]) # --------------------------------------------------------------------------- # Context and Branch Derivation # --------------------------------------------------------------------------- class TestDeriveContext(unittest.TestCase): def test_jira_ticket_preferred(self) -> None: req = {"jira_tickets": ["EDM-1234"], "cve_ids": ["CVE-2024-5678"]} self.assertEqual(unattended.derive_context(req), "EDM-1234") def test_cve_fallback(self) -> None: req = {"cve_ids": ["CVE-2024-5678"]} self.assertEqual(unattended.derive_context(req), "CVE-2024-5678") def test_work_ref_fallback_deterministic(self) -> None: """work_ref without valid Jira/CVE uses hash fallback.""" req = {"work_ref": "cve-fix/EDM-1234"} ctx = unattended.derive_context(req) self.assertTrue(ctx.startswith("unattended-")) self.assertEqual(len(ctx), len("unattended-") + 32) # 128-bit hex # Deterministic: same request gives same context self.assertEqual(unattended.derive_context(req), ctx) def test_pr_url_fallback_deterministic(self) -> None: """pr_url without valid Jira/CVE uses hash fallback.""" req = {"pr_url": "https://github.com/org/repo/pull/42"} ctx = unattended.derive_context(req) self.assertTrue(ctx.startswith("unattended-")) self.assertEqual(unattended.derive_context(req), ctx) def test_auto_generated(self) -> None: ctx = unattended.derive_context({}) self.assertTrue(ctx.startswith("unattended-")) self.assertEqual(len(ctx), len("unattended-") + 32) def test_empty_request_deterministic(self) -> None: """Empty request produces deterministic context.""" ctx1 = unattended.derive_context({}) ctx2 = unattended.derive_context({}) self.assertEqual(ctx1, ctx2) def test_unsafe_chars_not_in_hash(self) -> None: """Hash fallback produces safe directory names.""" req = {"work_ref": "../../../etc/passwd"} ctx = unattended.derive_context(req) self.assertNotIn("..", ctx) self.assertNotIn("/", ctx) self.assertTrue(ctx.startswith("unattended-")) class TestDeriveBranch(unittest.TestCase): def test_explicit_branch(self) -> None: req = {"branch": "my-branch"} self.assertEqual(unattended.derive_branch(req, "EDM-1234"), "my-branch") def test_auto_branch(self) -> None: self.assertEqual( unattended.derive_branch({}, "EDM-1234"), "cve-fix/EDM-1234" ) class TestDeriveBaseBranch(unittest.TestCase): """derive_base_branch: preserves attended defaults (finding 3).""" def test_explicit_base(self) -> None: req = {"base_branch": "release-2.16"} self.assertEqual(unattended.derive_base_branch(req), "release-2.16") def test_no_base_returns_none(self) -> None: """Omitted base_branch returns None -- attended defaults apply.""" self.assertIsNone(unattended.derive_base_branch({})) def test_null_base_returns_none(self) -> None: req = {"base_branch": None} self.assertIsNone(unattended.derive_base_branch(req)) # --------------------------------------------------------------------------- # Result Construction (findings 1, 11) # --------------------------------------------------------------------------- class TestMakeResult(unittest.TestCase): def test_valid_result(self) -> None: result = unattended.make_result( status="completed", operation="remediate", scope={"cve_ids": ["CVE-2024-1234"]}, stages_completed=["start", "scan", "patch"], ) self.assertEqual(result["status"], "completed") self.assertFalse(result["lifecycle_complete"]) self.assertIn("timestamp", result) def test_invalid_status_raises(self) -> None: with self.assertRaises(ValueError): unattended.make_result( status="invalid_status", operation="remediate", scope={} ) def test_lifecycle_complete_flag(self) -> None: """lifecycle_complete distinguishes operation from CVE lifecycle.""" result = unattended.make_result( status="completed", operation="remediate", scope={}, lifecycle_complete=True, ) self.assertTrue(result["lifecycle_complete"]) def test_jira_results_carried(self) -> None: result = unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[{ "ticket": "EDM-1234", "action": "transition", "target_status": "MODIFIED", "result": "success", }], expected_tickets=["EDM-1234"], expected_jira_action={ "action": "transition", "target_status": "MODIFIED", }, ) self.assertEqual(len(result["jira_results"]), 1) def test_all_fields_present(self) -> None: result = unattended.make_result( status="partial", operation="remediate", scope={"cve_ids": ["CVE-2024-1"]}, pr={"url": "https://example.com/pull/1"}, stages_completed=["scan"], stages_remaining=["patch"], evidence={"scan_verdict": "present"}, backport_results=[{"target_branch": "rel-1", "status": "completed", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}}], blockers=[{"type": "test", "reason": "tests fail"}], human_decisions=[{"decision_id": "d1"}], next_permitted_operations=[ {"operation": "remediate", "prerequisite": "none"}, ], artifacts_dir=".artifacts/cve-fix/test/", ) self.assertIsNotNone(result["pr"]) self.assertEqual(len(result["backport_results"]), 1) self.assertIn("lifecycle_complete", result) # --------------------------------------------------------------------------- # Human Decision Validation (finding 5) # --------------------------------------------------------------------------- class TestHumanDecisionRequest(unittest.TestCase): def test_fields_populated(self) -> None: req = unattended.make_human_decision_request( decision_id="vex-1", question="Is this VEX accurate?", proposed_justification="component_not_present", evidence="Package not imported", scan_id="scan-abc", scope={"cve_ids": ["CVE-2024-1"]}, ) self.assertEqual(req["decision_id"], "vex-1") self.assertEqual(req["scan_id"], "scan-abc") class TestValidateHumanDecisionResponse(unittest.TestCase): def _valid_original_request(self) -> dict: return { "decision_id": "vex-1", "question": "Is this VEX accurate?", "permitted_effects": ["close_vex", "reject", "defer"], "scan_id": "scan-abc", "scope": {"cve_ids": ["CVE-2024-1"]}, } def _valid_authority_context(self) -> dict: return {"allowed_reviewer_ids": ["jsmith"]} def _valid_response(self) -> dict: return { "decision_id": "vex-1", "accepted": True, "reviewer": "jsmith", "justification": "component_not_present", "evidence": "Verified: package not in binary.", "scan_id": "scan-abc", "scope": {"cve_ids": ["CVE-2024-1"]}, "timestamp": "2024-11-04T14:00:00Z", "selected_effect": "close_vex", } def test_valid_response(self) -> None: errors = unattended.validate_human_decision_response( self._valid_response(), "scan-abc", {"cve_ids": ["CVE-2024-1"]}, original_request=self._valid_original_request(), authority_context=self._valid_authority_context(), ) self.assertEqual(errors, []) def test_missing_reviewer(self) -> None: resp = self._valid_response() resp["reviewer"] = "" errors = unattended.validate_human_decision_response( resp, "scan-abc", {"cve_ids": ["CVE-2024-1"]}, original_request=self._valid_original_request(), authority_context=self._valid_authority_context(), ) self.assertTrue(any("reviewer" in e for e in errors)) def test_blank_evidence(self) -> None: resp = self._valid_response() resp["evidence"] = " " errors = unattended.validate_human_decision_response( resp, "scan-abc", {"cve_ids": ["CVE-2024-1"]}, original_request=self._valid_original_request(), authority_context=self._valid_authority_context(), ) self.assertTrue(any("evidence" in e for e in errors)) def test_scan_id_mismatch(self) -> None: errors = unattended.validate_human_decision_response( self._valid_response(), "scan-different", {"cve_ids": ["CVE-2024-1"]}, original_request=self._valid_original_request(), authority_context=self._valid_authority_context(), ) self.assertTrue(any("scan_id" in e for e in errors)) def test_scope_mismatch(self) -> None: errors = unattended.validate_human_decision_response( self._valid_response(), "scan-abc", {"cve_ids": ["CVE-2024-9999"]}, original_request=self._valid_original_request(), authority_context=self._valid_authority_context(), ) self.assertTrue(any("scope" in e for e in errors)) def test_accepted_must_be_boolean(self) -> None: """String 'true' is not boolean.""" resp = self._valid_response() resp["accepted"] = "true" errors = unattended.validate_human_decision_response( resp, "scan-abc", {"cve_ids": ["CVE-2024-1"]}, original_request=self._valid_original_request(), authority_context=self._valid_authority_context(), ) self.assertTrue(any("boolean" in e for e in errors)) def test_accepted_integer_rejected(self) -> None: resp = self._valid_response() resp["accepted"] = 1 errors = unattended.validate_human_decision_response( resp, "scan-abc", {"cve_ids": ["CVE-2024-1"]}, original_request=self._valid_original_request(), authority_context=self._valid_authority_context(), ) self.assertTrue(any("boolean" in e for e in errors)) def test_missing_timestamp(self) -> None: resp = self._valid_response() del resp["timestamp"] errors = unattended.validate_human_decision_response( resp, "scan-abc", {"cve_ids": ["CVE-2024-1"]}, original_request=self._valid_original_request(), authority_context=self._valid_authority_context(), ) self.assertTrue(any("timestamp" in e for e in errors)) def test_accepted_requires_justification(self) -> None: resp = self._valid_response() del resp["justification"] errors = unattended.validate_human_decision_response( resp, "scan-abc", {"cve_ids": ["CVE-2024-1"]}, original_request=self._valid_original_request(), authority_context=self._valid_authority_context(), ) self.assertTrue(any("justification" in e for e in errors)) def test_rejected_cannot_authorize_action(self) -> None: """Rejected decision with action effect must be invalid.""" resp = self._valid_response() resp["accepted"] = False resp["selected_effect"] = "close_vex" errors = unattended.validate_human_decision_response( resp, "scan-abc", {"cve_ids": ["CVE-2024-1"]}, original_request=self._valid_original_request(), authority_context=self._valid_authority_context(), ) self.assertTrue(any("authorize" in e for e in errors)) def test_rejected_with_defer_is_valid(self) -> None: resp = self._valid_response() resp["accepted"] = False resp["selected_effect"] = "defer" del resp["justification"] # not required for rejection errors = unattended.validate_human_decision_response( resp, "scan-abc", {"cve_ids": ["CVE-2024-1"]}, original_request=self._valid_original_request(), authority_context=self._valid_authority_context(), ) # Should not have authorization error self.assertFalse(any("authorize" in e for e in errors)) def test_decision_id_binding(self) -> None: """decision_id must match original request when provided.""" resp = self._valid_response() original = {"decision_id": "vex-original", "permitted_effects": []} errors = unattended.validate_human_decision_response( resp, "scan-abc", {"cve_ids": ["CVE-2024-1"]}, original_request=original, authority_context=self._valid_authority_context(), ) self.assertTrue(any("decision_id" in e and "mismatch" in e for e in errors)) def test_effect_outside_permitted(self) -> None: """selected_effect not in permitted_effects must fail.""" resp = self._valid_response() resp["selected_effect"] = "close_vex" original = { "decision_id": "vex-1", "permitted_effects": ["reject", "defer"], } errors = unattended.validate_human_decision_response( resp, "scan-abc", {"cve_ids": ["CVE-2024-1"]}, original_request=original, authority_context=self._valid_authority_context(), ) self.assertTrue(any("permitted effects" in e for e in errors)) def test_non_dict_response(self) -> None: errors = unattended.validate_human_decision_response( "not a dict", "scan-abc", {"cve_ids": ["CVE-2024-1"]} ) self.assertEqual(errors, ["Decision response must be a JSON object"]) def test_stale_scan_id_rejected(self) -> None: resp = self._valid_response() resp["scan_id"] = "scan-run-1" errors = unattended.validate_human_decision_response( resp, "scan-run-2", {"cve_ids": ["CVE-2024-1"]}, original_request=self._valid_original_request(), authority_context=self._valid_authority_context(), ) self.assertTrue(any("scan_id" in e for e in errors)) # --- New regression tests for defect B --- def test_whitespace_justification_rejected(self) -> None: """Whitespace-only justification must fail (defect B).""" resp = self._valid_response() resp["justification"] = " " errors = unattended.validate_human_decision_response( resp, "scan-abc", {"cve_ids": ["CVE-2024-1"]}, original_request=self._valid_original_request(), authority_context=self._valid_authority_context(), ) self.assertTrue(any("justification" in e and "nonblank" in e for e in errors)) def test_empty_scan_id_expected_rejected(self) -> None: """Empty expected_scan_id must fail (defect B).""" errors = unattended.validate_human_decision_response( self._valid_response(), "", {"cve_ids": ["CVE-2024-1"]}, original_request=self._valid_original_request(), authority_context=self._valid_authority_context(), ) self.assertTrue(any("expected_scan_id" in e for e in errors)) def test_empty_expected_scope_rejected(self) -> None: """Empty expected_scope must fail (defect B).""" errors = unattended.validate_human_decision_response( self._valid_response(), "scan-abc", {}, original_request=self._valid_original_request(), authority_context=self._valid_authority_context(), ) self.assertTrue(any("expected_scope" in e for e in errors)) def test_garbage_timestamp_rejected(self) -> None: """Non-ISO timestamp must fail (defect B).""" resp = self._valid_response() resp["timestamp"] = "garbage-not-a-date" errors = unattended.validate_human_decision_response( resp, "scan-abc", {"cve_ids": ["CVE-2024-1"]}, original_request=self._valid_original_request(), authority_context=self._valid_authority_context(), ) self.assertTrue(any("ISO 8601" in e for e in errors)) def test_accepted_requires_selected_effect(self) -> None: """Accepted decision must have selected_effect (defect B).""" resp = self._valid_response() del resp["selected_effect"] errors = unattended.validate_human_decision_response( resp, "scan-abc", {"cve_ids": ["CVE-2024-1"]}, original_request=self._valid_original_request(), authority_context=self._valid_authority_context(), ) self.assertTrue(any("selected_effect" in e for e in errors)) def test_accepted_requires_original_request(self) -> None: """Accepted without original_request must fail (defect B/C).""" errors = unattended.validate_human_decision_response( self._valid_response(), "scan-abc", {"cve_ids": ["CVE-2024-1"]}, ) self.assertTrue(any("original_request" in e for e in errors)) def test_accepted_requires_authority_context(self) -> None: """Accepted without authority_context must fail (defect B).""" errors = unattended.validate_human_decision_response( self._valid_response(), "scan-abc", {"cve_ids": ["CVE-2024-1"]}, original_request=self._valid_original_request(), ) self.assertTrue(any("authority_context" in e for e in errors)) def test_reviewer_not_in_allowed_ids(self) -> None: """Reviewer not in allowed_reviewer_ids must fail (defect B).""" errors = unattended.validate_human_decision_response( self._valid_response(), "scan-abc", {"cve_ids": ["CVE-2024-1"]}, original_request=self._valid_original_request(), authority_context={"allowed_reviewer_ids": ["other-person"]}, ) self.assertTrue(any("not in" in e and "allowed_reviewer_ids" in e for e in errors)) def test_empty_allowed_reviewer_ids(self) -> None: """Empty allowed_reviewer_ids must fail (defect B).""" errors = unattended.validate_human_decision_response( self._valid_response(), "scan-abc", {"cve_ids": ["CVE-2024-1"]}, original_request=self._valid_original_request(), authority_context={"allowed_reviewer_ids": []}, ) self.assertTrue(any("nonempty list" in e for e in errors)) def test_rejected_returns_not_authorized(self) -> None: """Rejected decision is valid but never authorizes action.""" resp = self._valid_response() resp["accepted"] = False resp["selected_effect"] = "reject" errors = unattended.validate_human_decision_response( resp, "scan-abc", {"cve_ids": ["CVE-2024-1"]}, original_request=self._valid_original_request(), authority_context=self._valid_authority_context(), ) # No authorization errors for reject effect self.assertFalse(any("authorize" in e for e in errors)) # --------------------------------------------------------------------------- # Scope Deconfliction (finding 8) # --------------------------------------------------------------------------- class TestCheckScopeConflict(unittest.TestCase): def test_no_artifacts(self) -> None: with tempfile.TemporaryDirectory() as td: self.assertIsNone(unattended.check_scope_conflict( "org/repo", "main", ["CVE-2024-1"], ["pkg"], td )) def test_no_conflict_different_repo(self) -> None: with tempfile.TemporaryDirectory() as td: ctx_dir = Path(td) / "EDM-100" ctx_dir.mkdir() (ctx_dir / "unattended-result.json").write_text(json.dumps({ "status": "partial", "scope": { "repo": "other/repo", "base_branch": "main", "cve_ids": ["CVE-2024-1"], "packages": ["pkg"], }, })) self.assertIsNone(unattended.check_scope_conflict( "org/repo", "main", ["CVE-2024-1"], ["pkg"], td )) def test_conflict_same_scope(self) -> None: with tempfile.TemporaryDirectory() as td: ctx_dir = Path(td) / "EDM-100" ctx_dir.mkdir() (ctx_dir / "unattended-result.json").write_text(json.dumps({ "status": "partial", "scope": { "repo": "org/repo", "base_branch": "main", "cve_ids": ["CVE-2024-1"], "packages": ["pkg"], }, })) self.assertIsNotNone(unattended.check_scope_conflict( "org/repo", "main", ["CVE-2024-1"], ["pkg"], td )) def test_completed_with_open_pr_is_conflict(self) -> None: """Completed work with a live draft PR still conflicts (finding 8).""" with tempfile.TemporaryDirectory() as td: ctx_dir = Path(td) / "EDM-100" ctx_dir.mkdir() (ctx_dir / "unattended-result.json").write_text(json.dumps({ "status": "completed", "scope": { "repo": "org/repo", "base_branch": "main", "cve_ids": ["CVE-2024-1"], "packages": ["pkg"], }, "pr": { "url": "https://github.com/org/repo/pull/42", "state": "draft", }, })) result = unattended.check_scope_conflict( "org/repo", "main", ["CVE-2024-1"], ["pkg"], td ) self.assertIsNotNone(result) def test_completed_with_closed_pr_no_conflict(self) -> None: """Completed work with a closed PR does not conflict.""" with tempfile.TemporaryDirectory() as td: ctx_dir = Path(td) / "EDM-100" ctx_dir.mkdir() (ctx_dir / "unattended-result.json").write_text(json.dumps({ "status": "completed", "scope": { "repo": "org/repo", "base_branch": "main", "cve_ids": ["CVE-2024-1"], "packages": ["pkg"], }, "pr": { "url": "https://github.com/org/repo/pull/42", "state": "closed", }, })) self.assertIsNone(unattended.check_scope_conflict( "org/repo", "main", ["CVE-2024-1"], ["pkg"], td )) def test_self_exclusion(self) -> None: """exclude_context skips own context for updates.""" with tempfile.TemporaryDirectory() as td: ctx_dir = Path(td) / "EDM-100" ctx_dir.mkdir() (ctx_dir / "unattended-result.json").write_text(json.dumps({ "status": "partial", "scope": { "repo": "org/repo", "base_branch": "main", "cve_ids": ["CVE-2024-1"], "packages": ["pkg"], }, })) self.assertIsNone(unattended.check_scope_conflict( "org/repo", "main", ["CVE-2024-1"], ["pkg"], td, exclude_context="EDM-100", )) def test_different_base_branch_no_conflict(self) -> None: with tempfile.TemporaryDirectory() as td: ctx_dir = Path(td) / "EDM-300" ctx_dir.mkdir() (ctx_dir / "unattended-result.json").write_text(json.dumps({ "status": "partial", "scope": { "repo": "org/repo", "base_branch": "release-2.16", "cve_ids": ["CVE-2024-1"], "packages": ["pkg"], }, })) self.assertIsNone(unattended.check_scope_conflict( "org/repo", "main", ["CVE-2024-1"], ["pkg"], td )) def test_failed_with_pr_still_conflicts(self) -> None: """Failed work that left a PR behind still conflicts.""" with tempfile.TemporaryDirectory() as td: ctx_dir = Path(td) / "EDM-400" ctx_dir.mkdir() (ctx_dir / "unattended-result.json").write_text(json.dumps({ "status": "failed", "scope": { "repo": "org/repo", "base_branch": "main", "cve_ids": ["CVE-2024-1"], "packages": ["pkg"], }, "pr": {"url": "https://github.com/org/repo/pull/99", "state": "draft"}, })) self.assertIsNotNone(unattended.check_scope_conflict( "org/repo", "main", ["CVE-2024-1"], ["pkg"], td )) def test_error_without_pr_no_conflict(self) -> None: with tempfile.TemporaryDirectory() as td: ctx_dir = Path(td) / "EDM-500" ctx_dir.mkdir() (ctx_dir / "unattended-result.json").write_text(json.dumps({ "status": "error", "scope": { "repo": "org/repo", "base_branch": "main", "cve_ids": ["CVE-2024-1"], "packages": ["pkg"], }, })) self.assertIsNone(unattended.check_scope_conflict( "org/repo", "main", ["CVE-2024-1"], ["pkg"], td )) def test_malformed_json_skipped(self) -> None: """Malformed state cannot block new work.""" with tempfile.TemporaryDirectory() as td: ctx_dir = Path(td) / "EDM-600" ctx_dir.mkdir() (ctx_dir / "unattended-result.json").write_text("not json{") self.assertIsNone(unattended.check_scope_conflict( "org/repo", "main", ["CVE-2024-1"], ["pkg"], td )) # --------------------------------------------------------------------------- # State Loading (finding 9) # --------------------------------------------------------------------------- class TestLoadState(unittest.TestCase): def test_empty_dir(self) -> None: with tempfile.TemporaryDirectory() as td: state = unattended.load_state(td) self.assertFalse(state["has_context"]) self.assertFalse(state["has_scan_file"]) def test_with_context_and_pr(self) -> None: with tempfile.TemporaryDirectory() as td: (Path(td) / "context.md").write_text("# ctx\n## Main PR\n- URL: https://x/1\n") state = unattended.load_state(td) self.assertTrue(state["has_context"]) self.assertEqual(state.get("pr_url"), "https://x/1") def test_invalid_scan_json_not_valid(self) -> None: """Invalid scan JSON: has_scan_file True but scan_valid False.""" with tempfile.TemporaryDirectory() as td: (Path(td) / "scan-result.json").write_text("not json") state = unattended.load_state(td) self.assertTrue(state["has_scan_file"]) self.assertFalse(state["scan_valid"]) def test_scan_without_verdict_not_valid(self) -> None: """Scan JSON without verdict field: present but not valid.""" with tempfile.TemporaryDirectory() as td: (Path(td) / "scan-result.json").write_text('{"status": "ok"}') state = unattended.load_state(td) self.assertTrue(state["has_scan_file"]) self.assertFalse(state["scan_valid"]) def test_valid_scan(self) -> None: with tempfile.TemporaryDirectory() as td: (Path(td) / "scan-result.json").write_text( '{"verdict": "present", "scan_id": "abc"}' ) state = unattended.load_state(td) self.assertTrue(state["scan_valid"]) def test_malformed_result(self) -> None: with tempfile.TemporaryDirectory() as td: (Path(td) / "unattended-result.json").write_text("bad json") state = unattended.load_state(td) self.assertTrue(state.get("result_malformed")) self.assertNotIn("result", state) # --------------------------------------------------------------------------- # Stage Resumption (finding 9) # --------------------------------------------------------------------------- class TestDetermineResumeStage(unittest.TestCase): def test_no_state_starts_at_start(self) -> None: state = {"has_context": False, "has_scan_file": False, "has_patch_file": False, "has_validation_file": False} res = unattended.determine_resume_stage(state, None) self.assertEqual(res["stage"], "start") def test_with_context_resumes_at_scan(self) -> None: state = {"has_context": True, "has_scan_file": False, "has_patch_file": False, "has_validation_file": False} res = unattended.determine_resume_stage(state, None) self.assertEqual(res["stage"], "scan") def test_with_validation_resumes_at_pr(self) -> None: state = {"has_context": True, "has_scan_file": True, "has_patch_file": True, "has_validation_file": True} res = unattended.determine_resume_stage(state, None) self.assertEqual(res["stage"], "pr") def test_requested_stage_honored(self) -> None: state = {"has_context": True, "has_scan_file": True, "has_patch_file": True, "has_validation_file": True} res = unattended.determine_resume_stage(state, "validate") self.assertEqual(res["stage"], "validate") def test_requested_stage_missing_prereqs_reports(self) -> None: """Missing prereqs are reported, not silently ignored.""" state = {"has_context": True, "has_scan_file": False, "has_patch_file": False, "has_validation_file": False} res = unattended.determine_resume_stage(state, "validate") self.assertIn("has_patch_file", res["missing_prereqs"]) self.assertIn("note", res) # --------------------------------------------------------------------------- # CLI integration # --------------------------------------------------------------------------- class TestCLIValidate(unittest.TestCase): def test_valid_request_file(self) -> None: with tempfile.NamedTemporaryFile( mode="w", suffix=".json", delete=False ) as f: json.dump({"operation": "remediate", "cve_ids": ["CVE-2024-1234"]}, f) f.flush() try: args = argparse.Namespace(request_file=f.name) self.assertEqual(unattended._cmd_validate(args), 0) finally: os.unlink(f.name) def test_invalid_request_file(self) -> None: with tempfile.NamedTemporaryFile( mode="w", suffix=".json", delete=False ) as f: json.dump({"operation": "advance"}, f) f.flush() try: args = argparse.Namespace(request_file=f.name) self.assertEqual(unattended._cmd_validate(args), 1) finally: os.unlink(f.name) def test_backport_blank_target_cli_rejected(self) -> None: """CLI validate rejects blank target_branches with specific error.""" import io from unittest.mock import patch as _patch with tempfile.TemporaryDirectory() as td: req_path = os.path.join(td, "req.json") with open(req_path, "w") as f: json.dump({ "operation": "backport", "pr_url": "https://github.com/o/r/pull/1", "target_branches": ["rel-1", " "], }, f) args = argparse.Namespace(request_file=req_path) stdout_cap = io.StringIO() stderr_cap = io.StringIO() with _patch("sys.stdout", stdout_cap), _patch("sys.stderr", stderr_cap): rc = unattended._cmd_validate(args) self.assertEqual(rc, 1) out_json = json.loads(stdout_cap.getvalue()) self.assertFalse(out_json["valid"]) self.assertTrue( any("nonempty" in e for e in out_json["errors"]), f"Expected nonempty error in JSON, got: {out_json['errors']}", ) self.assertIn("nonempty", stderr_cap.getvalue()) def test_backport_duplicate_target_cli_rejected(self) -> None: """CLI validate rejects duplicate target_branches with specific error.""" import io from unittest.mock import patch as _patch with tempfile.TemporaryDirectory() as td: req_path = os.path.join(td, "req.json") with open(req_path, "w") as f: json.dump({ "operation": "backport", "pr_url": "https://github.com/o/r/pull/1", "target_branches": ["rel-1", "rel-1"], }, f) args = argparse.Namespace(request_file=req_path) stdout_cap = io.StringIO() stderr_cap = io.StringIO() with _patch("sys.stdout", stdout_cap), _patch("sys.stderr", stderr_cap): rc = unattended._cmd_validate(args) self.assertEqual(rc, 1) out_json = json.loads(stdout_cap.getvalue()) self.assertFalse(out_json["valid"]) self.assertTrue( any("duplicate" in e for e in out_json["errors"]), f"Expected duplicate error in JSON, got: {out_json['errors']}", ) self.assertIn("duplicate", stderr_cap.getvalue()) def test_backport_valid_unique_cli_accepted(self) -> None: """CLI validate accepts valid unique target_branches.""" import io from unittest.mock import patch as _patch with tempfile.TemporaryDirectory() as td: req_path = os.path.join(td, "req.json") with open(req_path, "w") as f: json.dump({ "operation": "backport", "pr_url": "https://github.com/o/r/pull/1", "target_branches": ["rel-1", "rel-2"], }, f) args = argparse.Namespace(request_file=req_path) stdout_cap = io.StringIO() with _patch("sys.stdout", stdout_cap): rc = unattended._cmd_validate(args) self.assertEqual(rc, 0) out_json = json.loads(stdout_cap.getvalue()) self.assertTrue(out_json["valid"]) self.assertEqual(out_json["operation"], "backport") class TestCLIMakeResult(unittest.TestCase): def test_writes_result(self) -> None: with tempfile.TemporaryDirectory() as td: args = argparse.Namespace( status="completed", operation="remediate", artifact_dir=td, data=json.dumps({ "scope": {"cve_ids": ["CVE-2024-1"]}, "stages_completed": ["start", "scan"], "jira_results": [{"ticket": "EDM-1", "result": "success"}], }), ) self.assertEqual(unattended._cmd_make_result(args), 0) written = json.loads( (Path(td) / "unattended-result.json").read_text() ) self.assertEqual(written["status"], "completed") self.assertEqual(len(written["jira_results"]), 1) self.assertIn("lifecycle_complete", written) class TestCLIValidateDecision(unittest.TestCase): def _write_json_file(self, data: dict) -> str: """Write data to a temp JSON file and return its path.""" f = tempfile.NamedTemporaryFile( mode="w", suffix=".json", delete=False ) json.dump(data, f) f.flush() f.close() return f.name def test_valid_decision(self) -> None: resp_path = self._write_json_file({ "decision_id": "d1", "accepted": True, "reviewer": "jsmith", "justification": "component_not_present", "evidence": "Verified", "scan_id": "scan-1", "scope": {"cve_ids": ["CVE-2024-1"]}, "timestamp": "2024-11-04T14:00:00Z", "selected_effect": "close_vex", }) orig_path = self._write_json_file({ "decision_id": "d1", "permitted_effects": ["close_vex", "reject", "defer"], "scan_id": "scan-1", "scope": {"cve_ids": ["CVE-2024-1"]}, }) try: args = argparse.Namespace( response_file=resp_path, scan_id="scan-1", scope=json.dumps({"cve_ids": ["CVE-2024-1"]}), original_request=orig_path, authority_context=json.dumps( {"allowed_reviewer_ids": ["jsmith"]} ), ) self.assertEqual(unattended._cmd_validate_decision(args), 0) finally: os.unlink(resp_path) os.unlink(orig_path) def test_invalid_decision_scan_id(self) -> None: resp_path = self._write_json_file({ "decision_id": "d1", "accepted": True, "reviewer": "jsmith", "justification": "component_not_present", "evidence": "Verified", "scan_id": "wrong-scan", "scope": {"cve_ids": ["CVE-2024-1"]}, "timestamp": "2024-11-04T14:00:00Z", "selected_effect": "close_vex", }) orig_path = self._write_json_file({ "decision_id": "d1", "permitted_effects": ["close_vex"], "scan_id": "scan-1", "scope": {"cve_ids": ["CVE-2024-1"]}, }) try: args = argparse.Namespace( response_file=resp_path, scan_id="scan-1", scope=json.dumps({"cve_ids": ["CVE-2024-1"]}), original_request=orig_path, authority_context=json.dumps( {"allowed_reviewer_ids": ["jsmith"]} ), ) self.assertEqual(unattended._cmd_validate_decision(args), 1) finally: os.unlink(resp_path) os.unlink(orig_path) def test_missing_original_request_exits_2(self) -> None: """CLI exits 2 when --original-request missing (defect C).""" resp_path = self._write_json_file({"accepted": True}) try: args = argparse.Namespace( response_file=resp_path, scan_id="scan-1", scope=json.dumps({"cve_ids": ["CVE-2024-1"]}), original_request=None, authority_context=None, ) self.assertEqual(unattended._cmd_validate_decision(args), 2) finally: os.unlink(resp_path) def test_missing_scope_exits_2(self) -> None: """CLI exits 2 when --scope missing.""" resp_path = self._write_json_file({"accepted": True}) try: args = argparse.Namespace( response_file=resp_path, scan_id="scan-1", scope=None, original_request=resp_path, authority_context=None, ) self.assertEqual(unattended._cmd_validate_decision(args), 2) finally: os.unlink(resp_path) def test_empty_scan_id_exits_2(self) -> None: """CLI exits 2 when --scan-id is empty.""" resp_path = self._write_json_file({"accepted": True}) try: args = argparse.Namespace( response_file=resp_path, scan_id="", scope=json.dumps({"cve_ids": ["CVE-2024-1"]}), original_request=resp_path, authority_context=None, ) self.assertEqual(unattended._cmd_validate_decision(args), 2) finally: os.unlink(resp_path) # --------------------------------------------------------------------------- # Defect A regression: validation robustness # --------------------------------------------------------------------------- class TestValidateRequestDefectA(unittest.TestCase): """Regression tests for defect A: malformed/untrusted input.""" def test_jira_action_action_list_type_error(self) -> None: """action=[] must not raise TypeError (defect A).""" req = { "operation": "jira_action", "jira_action": {"action": [], "tickets": ["EDM-1"]}, } errors = unattended.validate_request(req) self.assertTrue(any("string" in e for e in errors)) def test_jira_action_action_integer(self) -> None: req = { "operation": "jira_action", "jira_action": {"action": 42, "tickets": ["EDM-1"]}, } errors = unattended.validate_request(req) self.assertTrue(any("string" in e for e in errors)) def test_whitespace_pr_url_rejected(self) -> None: req = {"operation": "remediate", "pr_url": " "} errors = unattended.validate_request(req) self.assertTrue(any("nonempty" in e for e in errors)) def test_whitespace_work_ref_rejected(self) -> None: req = {"operation": "remediate", "work_ref": " "} errors = unattended.validate_request(req) self.assertTrue(any("nonempty" in e for e in errors)) def test_unknown_field_rejected(self) -> None: req = { "operation": "remediate", "cve_ids": ["CVE-2024-1"], "bogus_field": 42, } errors = unattended.validate_request(req) self.assertTrue(any("Unknown" in e for e in errors)) def test_operation_stage_mismatch_remediate_backport(self) -> None: req = { "operation": "remediate", "cve_ids": ["CVE-2024-1"], "stage": "backport", } errors = unattended.validate_request(req) self.assertTrue(any("not valid for operation" in e for e in errors)) def test_operation_stage_mismatch_jira_action_patch(self) -> None: req = { "operation": "jira_action", "jira_action": { "action": "comment", "tickets": ["EDM-1"], "comment": "test", }, "stage": "patch", } errors = unattended.validate_request(req) self.assertTrue(any("not valid for operation" in e for e in errors)) def test_human_decisions_string_rejected(self) -> None: req = { "operation": "remediate", "cve_ids": ["CVE-2024-1"], "human_decisions": "not an array", } errors = unattended.validate_request(req) self.assertTrue(any("human_decisions" in e for e in errors)) def test_human_decisions_element_not_dict(self) -> None: req = { "operation": "remediate", "cve_ids": ["CVE-2024-1"], "human_decisions": ["not a dict"], } errors = unattended.validate_request(req) self.assertTrue(any("human_decisions[0]" in e for e in errors)) def test_reviewer_policy_string_rejected(self) -> None: req = { "operation": "remediate", "cve_ids": ["CVE-2024-1"], "reviewer_policy": "not an object", } errors = unattended.validate_request(req) self.assertTrue(any("reviewer_policy" in e for e in errors)) def test_null_request(self) -> None: errors = unattended.validate_request(None) self.assertEqual(errors, ["Request must be a JSON object"]) def test_boolean_request(self) -> None: errors = unattended.validate_request(True) self.assertEqual(errors, ["Request must be a JSON object"]) def test_empty_list_cve_ids(self) -> None: """Empty arrays are structurally valid but remediate needs identifier.""" req = {"operation": "remediate", "cve_ids": []} errors = unattended.validate_request(req) self.assertTrue(any("remediate" in e for e in errors)) # --------------------------------------------------------------------------- # Defect D regression: result consistency # --------------------------------------------------------------------------- class TestMakeResultDefectD(unittest.TestCase): """Regression tests for defect D: contradictory results.""" def test_completed_with_fail_evidence(self) -> None: with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="remediate", scope={"cve_ids": ["CVE-2024-1"]}, evidence={"validation_verdict": "FAIL"}, ) self.assertIn("FAIL", str(ctx.exception)) def test_completed_with_blockers(self) -> None: with self.assertRaises(ValueError): unattended.make_result( status="completed", operation="remediate", scope={"cve_ids": ["CVE-2024-1"]}, blockers=[{"type": "test", "reason": "fail"}], ) def test_completed_with_error(self) -> None: with self.assertRaises(ValueError): unattended.make_result( status="completed", operation="remediate", scope={"cve_ids": ["CVE-2024-1"]}, error="something failed", ) def test_lifecycle_complete_non_completed(self) -> None: with self.assertRaises(ValueError): unattended.make_result( status="partial", operation="remediate", scope={"cve_ids": ["CVE-2024-1"]}, lifecycle_complete=True, ) def test_unknown_operation(self) -> None: with self.assertRaises(ValueError): unattended.make_result( status="completed", operation="advance", scope={}, ) def test_npo_bare_string_rejected(self) -> None: """next_permitted_operations entries must be dicts.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="remediate", scope={"cve_ids": ["CVE-2024-1"]}, next_permitted_operations=["backport"], ) self.assertIn("dict", str(ctx.exception)) def test_npo_valid_dict(self) -> None: """next_permitted_operations with proper dicts passes.""" result = unattended.make_result( status="completed", operation="remediate", scope={"cve_ids": ["CVE-2024-1"]}, next_permitted_operations=[ {"operation": "backport", "prerequisite": "main PR merged"}, ], ) self.assertEqual(len(result["next_permitted_operations"]), 1) def test_backport_lifecycle_incomplete_branch(self) -> None: """lifecycle_complete with a failed backport branch is rejected.""" with self.assertRaises(ValueError): unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": "completed", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}}, {"target_branch": "rel-2", "status": "failed"}, ], lifecycle_complete=True, target_branches=["rel-1", "rel-2"], ) def test_jira_lifecycle_failed_result(self) -> None: """lifecycle_complete with failed jira result is rejected.""" with self.assertRaises(ValueError): unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[{"ticket": "EDM-1", "result": "failed"}], expected_tickets=["EDM-1"], lifecycle_complete=True, ) def test_awaiting_merge_lifecycle_complete(self) -> None: with self.assertRaises(ValueError): unattended.make_result( status="awaiting_merge", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, lifecycle_complete=True, target_branches=["rel-1"], ) def test_scope_not_dict_rejected(self) -> None: with self.assertRaises(ValueError): unattended.make_result( status="completed", operation="remediate", scope="not a dict", ) def test_valid_failed_result(self) -> None: """Failed result is valid when consistent.""" result = unattended.make_result( status="failed", operation="remediate", scope={"cve_ids": ["CVE-2024-1"]}, error="patch strategies exhausted", ) self.assertEqual(result["status"], "failed") def test_valid_needs_human_review(self) -> None: """needs_human_review without PR is valid.""" result = unattended.make_result( status="needs_human_review", operation="remediate", scope={"cve_ids": ["CVE-2024-1"]}, human_decisions=[{"decision_id": "d1"}], ) self.assertIsNone(result["pr"]) # --------------------------------------------------------------------------- # Defect E regression: state/resume safety # --------------------------------------------------------------------------- class TestDetermineResumeStageDefectE(unittest.TestCase): """Regression tests for defect E: file-based resume safety.""" def test_malformed_scan_does_not_suggest_patch(self) -> None: """Malformed scan-result.json must not suggest patch stage.""" with tempfile.TemporaryDirectory() as td: Path(td, "context.md").write_text("# ctx") Path(td, "scan-result.json").write_text("not-json") state = unattended.load_state(td) resume = unattended.determine_resume_stage(state, None) self.assertNotEqual(resume["stage"], "patch") self.assertEqual(resume["stage"], "scan") def test_request_patch_with_invalid_scan_needs_input(self) -> None: """Requesting patch when scan is invalid returns needs_input.""" with tempfile.TemporaryDirectory() as td: Path(td, "context.md").write_text("# ctx") Path(td, "scan-result.json").write_text("not-json") state = unattended.load_state(td) resume = unattended.determine_resume_stage(state, "patch") self.assertEqual(resume["status"], "needs_input") self.assertIn("scan_valid", resume["missing_prereqs"]) def test_resume_is_never_authoritative(self) -> None: """Resume result always has authoritative=False.""" state = { "has_context": True, "scan_valid": True, "has_scan_file": True, "has_patch_file": True, "has_validation_file": True, } resume = unattended.determine_resume_stage(state, None) self.assertFalse(resume["authoritative"]) def test_valid_state_auto_detects_pr(self) -> None: """Valid full artifacts suggest pr stage.""" with tempfile.TemporaryDirectory() as td: Path(td, "context.md").write_text("# ctx") Path(td, "scan-result.json").write_text( '{"verdict": "present", "scan_id": "s1"}' ) Path(td, "patch-log.md").write_text("# patch") Path(td, "validation-results.md").write_text("PASS") state = unattended.load_state(td) resume = unattended.determine_resume_stage(state, None) self.assertEqual(resume["stage"], "pr") self.assertEqual(resume["status"], "ok") # --------------------------------------------------------------------------- # Defect F regression: scope conflict robustness # --------------------------------------------------------------------------- class TestCheckScopeConflictDefectF(unittest.TestCase): """Regression tests for defect F: malformed JSON / list.""" def test_json_list_no_crash(self) -> None: """JSON [] result file must not crash (defect F).""" with tempfile.TemporaryDirectory() as td: ctx_dir = Path(td) / "EDM-100" ctx_dir.mkdir() (ctx_dir / "unattended-result.json").write_text("[]") result = unattended.check_scope_conflict( "org/repo", "main", ["CVE-2024-1"], ["pkg"], td ) self.assertIsNone(result) def test_json_string_no_crash(self) -> None: """JSON string result file must not crash.""" with tempfile.TemporaryDirectory() as td: ctx_dir = Path(td) / "EDM-200" ctx_dir.mkdir() (ctx_dir / "unattended-result.json").write_text('"hello"') result = unattended.check_scope_conflict( "org/repo", "main", ["CVE-2024-1"], ["pkg"], td ) self.assertIsNone(result) def test_json_number_no_crash(self) -> None: """JSON number result file must not crash.""" with tempfile.TemporaryDirectory() as td: ctx_dir = Path(td) / "EDM-300" ctx_dir.mkdir() (ctx_dir / "unattended-result.json").write_text("42") result = unattended.check_scope_conflict( "org/repo", "main", ["CVE-2024-1"], ["pkg"], td ) self.assertIsNone(result) def test_json_null_no_crash(self) -> None: """JSON null result file must not crash.""" with tempfile.TemporaryDirectory() as td: ctx_dir = Path(td) / "EDM-400" ctx_dir.mkdir() (ctx_dir / "unattended-result.json").write_text("null") result = unattended.check_scope_conflict( "org/repo", "main", ["CVE-2024-1"], ["pkg"], td ) self.assertIsNone(result) # --------------------------------------------------------------------------- # Defect H regression: finite time budget # --------------------------------------------------------------------------- class TestDefaultTimeBudget(unittest.TestCase): """Regression tests for defect H: finite default time budget.""" def test_default_budget_is_finite(self) -> None: self.assertIsInstance(unattended.DEFAULT_TIME_BUDGET_SECONDS, int) self.assertGreater(unattended.DEFAULT_TIME_BUDGET_SECONDS, 0) def test_default_budget_is_two_hours(self) -> None: self.assertEqual(unattended.DEFAULT_TIME_BUDGET_SECONDS, 7200) # --------------------------------------------------------------------------- # Approval bypass regression (task item 2) # --------------------------------------------------------------------------- class TestApprovalBypassRegression(unittest.TestCase): """Exact bypass scenarios from task item 2.""" def _resp(self, **overrides: Any) -> dict: base = { "decision_id": "v1", "accepted": True, "reviewer": "jsmith", "justification": "ok", "evidence": "verified", "scan_id": "s1", "scope": {"cve_ids": ["CVE-2024-1"]}, "timestamp": "2024-01-01T00:00:00Z", "selected_effect": "close_vex", } base.update(overrides) return base def _orig(self, **overrides: Any) -> dict: base = { "decision_id": "v1", "permitted_effects": ["close_vex", "reject"], "scan_id": "s1", "scope": {"cve_ids": ["CVE-2024-1"]}, } base.update(overrides) return base def _auth(self) -> dict: return {"allowed_reviewer_ids": ["jsmith"]} def _scope(self) -> dict: return {"cve_ids": ["CVE-2024-1"]} def test_authority_context_list_rejected(self) -> None: errs = unattended.validate_human_decision_response( self._resp(), "s1", self._scope(), self._orig(), [] ) self.assertTrue(any("dict" in e for e in errs)) def test_authority_context_bool_rejected(self) -> None: errs = unattended.validate_human_decision_response( self._resp(), "s1", self._scope(), self._orig(), True ) self.assertTrue(any("dict" in e for e in errs)) def test_authority_context_str_rejected(self) -> None: errs = unattended.validate_human_decision_response( self._resp(), "s1", self._scope(), self._orig(), "x" ) self.assertTrue(any("dict" in e for e in errs)) def test_orig_decision_id_int_rejected(self) -> None: errs = unattended.validate_human_decision_response( self._resp(), "s1", self._scope(), self._orig(decision_id=123), self._auth(), ) self.assertTrue(any("decision_id" in e for e in errs)) def test_empty_permitted_effects_rejected(self) -> None: errs = unattended.validate_human_decision_response( self._resp(), "s1", self._scope(), self._orig(permitted_effects=[]), self._auth(), ) self.assertTrue(any("permitted" in e for e in errs)) def test_missing_permitted_effects_rejected(self) -> None: orig = {"decision_id": "v1"} # no permitted_effects errs = unattended.validate_human_decision_response( self._resp(), "s1", self._scope(), orig, self._auth(), ) self.assertTrue(any("permitted" in e for e in errs)) def test_invalid_date_rejected(self) -> None: errs = unattended.validate_human_decision_response( self._resp(timestamp="2026-99-99T99:99:99Z"), "s1", self._scope(), self._orig(), self._auth(), ) self.assertTrue(any("timestamp" in e for e in errs)) def test_valid_receipt_rejected_not_authorized(self) -> None: """Rejected decision: valid receipt, exit 0, but NOT authorized.""" resp = self._resp(accepted=False, selected_effect="reject") del resp["justification"] # not required for rejection del resp["selected_effect"] resp["selected_effect"] = "reject" errs = unattended.validate_human_decision_response( resp, "s1", self._scope(), self._orig(), self._auth(), ) # No errors for a properly rejected decision self.assertEqual(errs, []) def test_orig_scan_id_mismatch_rejected(self) -> None: """original_request.scan_id must match expected.""" errs = unattended.validate_human_decision_response( self._resp(), "s1", self._scope(), self._orig(scan_id="old-scan"), self._auth(), ) self.assertTrue(any("original_request.scan_id" in e for e in errs)) def test_orig_scope_mismatch_rejected(self) -> None: """original_request.scope must match expected.""" errs = unattended.validate_human_decision_response( self._resp(), "s1", self._scope(), self._orig(scope={"cve_ids": ["CVE-2024-9999"]}), self._auth(), ) self.assertTrue(any("original_request.scope" in e for e in errs)) def test_all_green_accepted(self) -> None: """Fully valid accepted decision passes.""" errs = unattended.validate_human_decision_response( self._resp(), "s1", self._scope(), self._orig(), self._auth(), ) self.assertEqual(errs, []) def test_authority_blank_ids_rejected(self) -> None: """allowed_reviewer_ids with blank strings rejected.""" errs = unattended.validate_human_decision_response( self._resp(), "s1", self._scope(), self._orig(), {"allowed_reviewer_ids": [""]}, ) self.assertTrue(any("nonempty" in e for e in errs)) # --------------------------------------------------------------------------- # Result contradiction regression (task item 3) # --------------------------------------------------------------------------- class TestResultContradictionRegression(unittest.TestCase): """Exact contradiction scenarios from task item 3.""" def test_unknown_verdict_rejected(self) -> None: with self.assertRaises(ValueError): unattended.make_result( status="completed", operation="remediate", scope={"x": 1}, evidence={"validation_verdict": "UNKNOWN"}, ) def test_pending_verdict_rejected(self) -> None: with self.assertRaises(ValueError): unattended.make_result( status="completed", operation="remediate", scope={"x": 1}, evidence={"validation_verdict": "pending"}, ) def test_nonstring_verdict_rejected(self) -> None: with self.assertRaises(ValueError): unattended.make_result( status="completed", operation="remediate", scope={"x": 1}, evidence={"validation_verdict": 42}, ) def test_npo_missing_prerequisite(self) -> None: with self.assertRaises(ValueError): unattended.make_result( status="completed", operation="remediate", scope={"x": 1}, next_permitted_operations=[ {"operation": "backport"}, # no prerequisite ], ) def test_npo_blank_operation(self) -> None: with self.assertRaises(ValueError): unattended.make_result( status="completed", operation="remediate", scope={"x": 1}, next_permitted_operations=[ {"operation": "", "prerequisite": "x"}, ], ) def test_npo_blank_prerequisite(self) -> None: with self.assertRaises(ValueError): unattended.make_result( status="completed", operation="remediate", scope={"x": 1}, next_permitted_operations=[ {"operation": "backport", "prerequisite": ""}, ], ) def test_nonlist_stages_completed(self) -> None: with self.assertRaises(ValueError): unattended.make_result( status="completed", operation="remediate", scope={"x": 1}, stages_completed="scan", ) def test_nondict_evidence_rejected(self) -> None: with self.assertRaises(ValueError): unattended.make_result( status="completed", operation="remediate", scope={"x": 1}, evidence="string", ) def test_valid_read_only_no_pr(self) -> None: """Read-only revalidation: completed without PR is valid.""" result = unattended.make_result( status="completed", operation="remediate", scope={"cve_ids": ["CVE-2024-1"]}, evidence={"validation_verdict": "PASS"}, ) self.assertIsNone(result["pr"]) self.assertEqual(result["status"], "completed") # --------------------------------------------------------------------------- # Scope conflict with malformed nested (task item 4) # --------------------------------------------------------------------------- class TestScopeConflictMalformedNested(unittest.TestCase): """Malformed nested scope shapes must not crash.""" def test_cve_ids_with_dict_elements(self) -> None: with tempfile.TemporaryDirectory() as td: ctx_dir = Path(td) / "EDM-100" ctx_dir.mkdir() (ctx_dir / "unattended-result.json").write_text(json.dumps({ "status": "partial", "scope": { "repo": "org/repo", "base_branch": "main", "cve_ids": [{}], "packages": ["pkg"], }, })) # Must not crash — dict elements are skipped result = unattended.check_scope_conflict( "org/repo", "main", ["CVE-2024-1"], ["pkg"], td, ) # pkg overlap still detected (as warning) — assert # unconditionally so the test fails if the result is None self.assertIsNotNone(result) self.assertEqual( result.get("_overlap_type"), "same_package_warning" ) def test_scope_is_list(self) -> None: with tempfile.TemporaryDirectory() as td: ctx_dir = Path(td) / "EDM-200" ctx_dir.mkdir() (ctx_dir / "unattended-result.json").write_text(json.dumps({ "status": "partial", "scope": ["not", "a", "dict"], })) result = unattended.check_scope_conflict( "org/repo", "main", ["CVE-2024-1"], ["pkg"], td, ) self.assertIsNone(result) # --------------------------------------------------------------------------- # Prior result / portable handoff (task item 1) # --------------------------------------------------------------------------- class TestPriorResultValidation(unittest.TestCase): """prior_result input validation and handoff.""" def test_valid_prior_result_accepted(self) -> None: req = { "operation": "remediate", "cve_ids": ["CVE-2024-1"], "prior_result": { "result": {"status": "needs_human_review"}, "decision_requests": [ {"decision_id": "vex-1", "permitted_effects": ["close_vex"]}, ], "scan_snapshot": { "scan_id": "scan-abc", "scope": {"cve_ids": ["CVE-2024-1"]}, "candidate_sha": "abc123", }, }, } errors = unattended.validate_request(req) self.assertEqual(errors, []) def test_prior_result_not_dict_rejected(self) -> None: req = { "operation": "remediate", "cve_ids": ["CVE-2024-1"], "prior_result": "not a dict", } errors = unattended.validate_request(req) self.assertTrue(any("prior_result" in e for e in errors)) def test_prior_result_missing_result_field(self) -> None: req = { "operation": "remediate", "cve_ids": ["CVE-2024-1"], "prior_result": {"decision_requests": []}, } errors = unattended.validate_request(req) self.assertTrue(any("prior_result.result" in e for e in errors)) def test_prior_result_stale_scope_rejected(self) -> None: """scan_snapshot.scope must match current request scope.""" req = { "operation": "remediate", "cve_ids": ["CVE-2024-1"], "prior_result": { "result": {}, "scan_snapshot": { "scan_id": "s1", "scope": {"cve_ids": ["CVE-2024-9999"]}, "candidate_sha": "abc", }, }, } errors = unattended.validate_request(req) self.assertTrue(any("stale" in e for e in errors)) def test_match_decision_response_to_request(self) -> None: """Finds matching OUT request by decision_id.""" requests = [ {"decision_id": "d1", "question": "q1"}, {"decision_id": "d2", "question": "q2"}, ] resp = {"decision_id": "d2"} match = unattended.match_decision_response_to_request(resp, requests) self.assertIsNotNone(match) self.assertEqual(match["question"], "q2") def test_match_no_match(self) -> None: resp = {"decision_id": "d3"} match = unattended.match_decision_response_to_request( resp, [{"decision_id": "d1"}], ) self.assertIsNone(match) class TestTwoInvocationRoundtrip(unittest.TestCase): """Full two-invocation consumer test: OUT -> discard -> IN -> validate. Simulates: 1. First invocation produces decision request OUT and scan snapshot 2. First artifact dir is discarded (fresh workspace) 3. Second invocation supplies prior_result + human response 4. validate-decision succeeds with rehydrated scan identity """ def test_full_roundtrip(self) -> None: # --- Invocation 1: produce decision request OUT --- decision_out = unattended.make_human_decision_request( decision_id="vex-review-1", question="Is this VEX accurate?", proposed_justification="component_not_present", evidence="Package not imported in binary.", scope={"cve_ids": ["CVE-2024-1234"], "repo": "org/repo", "base_branch": "main"}, scan_id="scan-20241104-abc", requested_effect="close_vex", permitted_effects=["close_vex", "reject", "defer"], ) # Invocation 1 result inv1_result = unattended.make_result( status="needs_human_review", operation="remediate", scope={"cve_ids": ["CVE-2024-1234"], "repo": "org/repo", "base_branch": "main"}, human_decisions=[decision_out], evidence={"scan_id": "scan-20241104-abc", "scan_verdict": "absent"}, ) # --- Discard first artifact dir (fresh workspace) --- # --- Invocation 2: build request with prior_result --- prior_result = { "result": inv1_result, "decision_requests": [decision_out], "scan_snapshot": { "scan_id": "scan-20241104-abc", "scope": {"cve_ids": ["CVE-2024-1234"], "repo": "org/repo", "base_branch": "main"}, "candidate_sha": "abc123", }, } # Human response (corrected justification) human_response = { "decision_id": "vex-review-1", "accepted": True, "reviewer": "jsmith", "justification": "component_not_present - verified in CI", "evidence": "Package not in shipped binary per CI scan.", "scan_id": "scan-20241104-abc", "scope": {"cve_ids": ["CVE-2024-1234"], "repo": "org/repo", "base_branch": "main"}, "timestamp": "2024-11-04T15:30:00Z", "selected_effect": "close_vex", } # Build second request inv2_request = { "operation": "remediate", "cve_ids": ["CVE-2024-1234"], "prior_result": prior_result, "human_decisions": [human_response], "reviewer_policy": { "allowed_reviewer_ids": ["jsmith"], }, } # Validate the full request req_errors = unattended.validate_request(inv2_request) self.assertEqual(req_errors, [], f"Request errors: {req_errors}") # Find matching OUT request matched_orig = unattended.match_decision_response_to_request( human_response, prior_result["decision_requests"], ) self.assertIsNotNone(matched_orig) self.assertEqual(matched_orig["decision_id"], "vex-review-1") # Rehydrate scan identity from prior_result scan_id = prior_result["scan_snapshot"]["scan_id"] current_scope = prior_result["scan_snapshot"]["scope"] # Validate the decision with all binding args authority = {"allowed_reviewer_ids": ["jsmith"]} dec_errors = unattended.validate_human_decision_response( response=human_response, expected_scan_id=scan_id, expected_scope=current_scope, original_request=matched_orig, authority_context=authority, ) self.assertEqual(dec_errors, [], f"Decision errors: {dec_errors}") def test_stale_scan_roundtrip_rejected(self) -> None: """If scan snapshot doesn't match current, rejection fires.""" decision_out = unattended.make_human_decision_request( decision_id="vex-2", question="VEX?", scan_id="scan-old", scope={"cve_ids": ["CVE-2024-1"]}, permitted_effects=["close_vex"], ) human_response = { "decision_id": "vex-2", "accepted": True, "reviewer": "jsmith", "justification": "ok", "evidence": "verified", "scan_id": "scan-old", "scope": {"cve_ids": ["CVE-2024-1"]}, "timestamp": "2024-11-04T15:30:00Z", "selected_effect": "close_vex", } # Current scan has changed errors = unattended.validate_human_decision_response( response=human_response, expected_scan_id="scan-new", expected_scope={"cve_ids": ["CVE-2024-1"]}, original_request=decision_out, authority_context={"allowed_reviewer_ids": ["jsmith"]}, ) self.assertTrue( any("scan_id" in e and "mismatch" in e for e in errors) ) # --------------------------------------------------------------------------- # Reviewer policy narrowing (task item 1) # --------------------------------------------------------------------------- class TestReviewerPolicyValidation(unittest.TestCase): """Only allowed_reviewer_ids is enforced (v1).""" def test_valid_policy_accepted(self) -> None: req = { "operation": "remediate", "cve_ids": ["CVE-2024-1"], "reviewer_policy": { "allowed_reviewer_ids": ["jsmith", "alice"], }, } self.assertEqual(unattended.validate_request(req), []) def test_empty_allowed_ids_rejected(self) -> None: req = { "operation": "remediate", "cve_ids": ["CVE-2024-1"], "reviewer_policy": { "allowed_reviewer_ids": [], }, } errors = unattended.validate_request(req) self.assertTrue(any("nonempty" in e for e in errors)) def test_blank_id_entry_rejected(self) -> None: req = { "operation": "remediate", "cve_ids": ["CVE-2024-1"], "reviewer_policy": { "allowed_reviewer_ids": ["jsmith", ""], }, } errors = unattended.validate_request(req) self.assertTrue(any("nonempty" in e for e in errors)) # --------------------------------------------------------------------------- # Item 2: Mandatory original request shapes # --------------------------------------------------------------------------- class TestOriginalRequestMandatoryShapes(unittest.TestCase): """original_request must have mandatory scan_id/scope for accepted.""" def _resp(self, **kw: Any) -> dict: r = { "decision_id": "v1", "accepted": True, "reviewer": "jsmith", "justification": "ok", "evidence": "verified", "scan_id": "s1", "scope": {"cve_ids": ["CVE-2024-1"]}, "timestamp": "2024-01-01T00:00:00Z", "selected_effect": "close_vex", } r.update(kw) return r def _auth(self) -> dict: return {"allowed_reviewer_ids": ["jsmith"]} def test_missing_orig_scan_id(self) -> None: """original_request without scan_id must fail.""" orig = {"decision_id": "v1", "permitted_effects": ["close_vex"], "scope": {"cve_ids": ["CVE-2024-1"]}} errs = unattended.validate_human_decision_response( self._resp(), "s1", {"cve_ids": ["CVE-2024-1"]}, orig, self._auth()) self.assertTrue(any("original_request.scan_id" in e for e in errs)) def test_blank_orig_scan_id(self) -> None: orig = {"decision_id": "v1", "permitted_effects": ["close_vex"], "scan_id": "", "scope": {"cve_ids": ["CVE-2024-1"]}} errs = unattended.validate_human_decision_response( self._resp(), "s1", {"cve_ids": ["CVE-2024-1"]}, orig, self._auth()) self.assertTrue(any("original_request.scan_id" in e for e in errs)) def test_missing_orig_scope(self) -> None: orig = {"decision_id": "v1", "permitted_effects": ["close_vex"], "scan_id": "s1"} errs = unattended.validate_human_decision_response( self._resp(), "s1", {"cve_ids": ["CVE-2024-1"]}, orig, self._auth()) self.assertTrue(any("original_request.scope" in e for e in errs)) def test_empty_orig_scope(self) -> None: orig = {"decision_id": "v1", "permitted_effects": ["close_vex"], "scan_id": "s1", "scope": {}} errs = unattended.validate_human_decision_response( self._resp(), "s1", {"cve_ids": ["CVE-2024-1"]}, orig, self._auth()) self.assertTrue(any("original_request.scope" in e for e in errs)) def test_blank_permitted_effect_entry(self) -> None: orig = {"decision_id": "v1", "permitted_effects": ["close_vex", ""], "scan_id": "s1", "scope": {"cve_ids": ["CVE-2024-1"]}} errs = unattended.validate_human_decision_response( self._resp(), "s1", {"cve_ids": ["CVE-2024-1"]}, orig, self._auth()) self.assertTrue(any("nonempty" in e for e in errs)) class TestPriorResultMandatoryFields(unittest.TestCase): """validate_prior_result requires all documented fields.""" def test_missing_decision_requests(self) -> None: pr = {"result": {}, "scan_snapshot": { "scan_id": "s1", "scope": {"cve_ids": ["CVE-2024-1"]}, "candidate_sha": "abc"}} errs = unattended.validate_prior_result(pr, {}) self.assertTrue(any("decision_requests" in e for e in errs)) def test_missing_scan_snapshot(self) -> None: pr = {"result": {}, "decision_requests": []} errs = unattended.validate_prior_result(pr, {}) self.assertTrue(any("scan_snapshot" in e for e in errs)) def test_missing_candidate_sha(self) -> None: pr = {"result": {}, "decision_requests": [], "scan_snapshot": {"scan_id": "s1", "scope": {"cve_ids": ["CVE-2024-1"]}}} errs = unattended.validate_prior_result(pr, {}) self.assertTrue(any("candidate_sha" in e for e in errs)) def test_missing_scope_cve_ids_key(self) -> None: pr = {"result": {}, "decision_requests": [], "scan_snapshot": {"scan_id": "s1", "scope": {"repo": "x"}, "candidate_sha": "abc"}} errs = unattended.validate_prior_result(pr, {}) self.assertTrue(any("cve_ids" in e for e in errs)) def test_duplicate_decision_ids(self) -> None: pr = {"result": {}, "scan_snapshot": { "scan_id": "s1", "scope": {"cve_ids": ["CVE-2024-1"]}, "candidate_sha": "abc"}, "decision_requests": [ {"decision_id": "d1"}, {"decision_id": "d1"}]} errs = unattended.validate_prior_result(pr, {}) self.assertTrue(any("duplicate" in e for e in errs)) def test_missing_current_scope_key_in_snapshot(self) -> None: """Snapshot missing a key from current scope must fail.""" pr = {"result": {}, "decision_requests": [], "scan_snapshot": {"scan_id": "s1", "scope": {"cve_ids": ["CVE-2024-1"]}, "candidate_sha": "abc"}} # Current scope includes packages but snapshot doesn't errs = unattended.validate_prior_result( pr, {"cve_ids": ["CVE-2024-1"], "packages": ["pkg-a"]}) self.assertTrue(any("missing" in e and "packages" in e for e in errs)) # --------------------------------------------------------------------------- # Item 3: Result false states # --------------------------------------------------------------------------- class TestResultFalseStates(unittest.TestCase): """Completed with failed child even when lifecycle_complete=False.""" def test_completed_failed_backport_child_lifecycle_false(self) -> None: """completed + failed child + lifecycle_complete=False rejects.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": "completed", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}}, {"target_branch": "rel-2", "status": "failed"}, ], lifecycle_complete=False, ) self.assertIn("failed", str(ctx.exception)) def test_completed_blocked_child_lifecycle_false(self) -> None: """'blocked' child rejected with valid surrounding fixture.""" with self.assertRaisesRegex( ValueError, r"status.*'blocked'.*must be one of" ): unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": "blocked"}, ], target_branches=["rel-1"], ) def test_completed_jira_error_child(self) -> None: with self.assertRaises(ValueError): unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[{"ticket": "EDM-1", "result": "error"}], expected_tickets=["EDM-1"], ) def test_valid_all_completed_lifecycle_false(self) -> None: """All children completed, lifecycle_complete=False is valid.""" result = unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": "completed", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}}, {"target_branch": "rel-2", "status": "merged", "pr": {"url": "https://github.com/o/r/pull/2"}, "evidence": {"test_result": "PASS"}}, ], target_branches=["rel-1", "rel-2"], lifecycle_complete=False, ) self.assertEqual(result["status"], "completed") self.assertFalse(result["lifecycle_complete"]) def test_valid_read_only_no_pr(self) -> None: """Completed remediation without PR (read-only/no-code) is valid.""" result = unattended.make_result( status="completed", operation="remediate", scope={"cve_ids": ["CVE-2024-1"]}, evidence={"validation_verdict": "PASS"}, ) self.assertIsNone(result["pr"]) # --------------------------------------------------------------------------- # Review 5775951665 — lifecycle_complete requires merged children # --------------------------------------------------------------------------- class TestLifecycleCompleteRequiresMerged(unittest.TestCase): """lifecycle_complete=True requires all backport children to be merged. Operation completion (status='completed') means the draft PR was created successfully. Lifecycle completion (lifecycle_complete=True) means all required PRs were actually merged. Only 'merged' children satisfy this. """ def test_all_completed_lifecycle_true_rejected(self) -> None: """All 'completed' children with lifecycle_complete=True is invalid.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": "completed", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}}, {"target_branch": "rel-2", "status": "completed", "pr": {"url": "https://github.com/o/r/pull/2"}, "evidence": {"test_result": "PASS"}}, ], lifecycle_complete=True, target_branches=["rel-1", "rel-2"], ) self.assertIn("lifecycle_complete=True", str(ctx.exception)) self.assertIn("completed", str(ctx.exception)) def test_mixed_completed_merged_lifecycle_true_rejected(self) -> None: """Mixed completed+merged children with lifecycle_complete=True fails.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": "completed", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}}, {"target_branch": "rel-2", "status": "merged", "pr": {"url": "https://github.com/o/r/pull/2"}, "evidence": {"test_result": "PASS"}}, ], lifecycle_complete=True, target_branches=["rel-1", "rel-2"], ) self.assertIn("lifecycle_complete=True", str(ctx.exception)) def test_all_merged_lifecycle_true_accepted(self) -> None: """All 'merged' children with lifecycle_complete=True is valid.""" result = unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": "merged", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}}, {"target_branch": "rel-2", "status": "merged", "pr": {"url": "https://github.com/o/r/pull/2"}, "evidence": {"test_result": "PASS"}}, ], lifecycle_complete=True, target_branches=["rel-1", "rel-2"], ) self.assertTrue(result["lifecycle_complete"]) self.assertEqual(result["status"], "completed") # Verify target_branches is carried through to output self.assertEqual(result["target_branches"], ["rel-1", "rel-2"]) def test_all_completed_lifecycle_false_still_valid(self) -> None: """All 'completed' children with lifecycle_complete=False remains valid.""" result = unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": "completed", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}}, {"target_branch": "rel-2", "status": "completed", "pr": {"url": "https://github.com/o/r/pull/2"}, "evidence": {"test_result": "PASS"}}, ], target_branches=["rel-1", "rel-2"], lifecycle_complete=False, ) self.assertFalse(result["lifecycle_complete"]) self.assertEqual(result["status"], "completed") # --------------------------------------------------------------------------- # Review 5776505195 — backport lifecycle requires target-set proof # --------------------------------------------------------------------------- class TestBackportTargetSetCoverage(unittest.TestCase): """lifecycle_complete=True for backport requires target_branches. The requested target_branches must be supplied so make_result can verify one merged child per requested branch (no missing, extra, or duplicate branches). Without target_branches, lifecycle completion cannot be asserted because coverage is unverifiable. """ def test_missing_target_branches_rejected(self) -> None: """lifecycle_complete=True without target_branches raises.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": "merged", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}}, ], lifecycle_complete=True, ) self.assertIn("target_branches", str(ctx.exception)) def test_empty_target_branches_rejected(self) -> None: """lifecycle_complete=True with empty target_branches raises.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": "merged", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}}, ], lifecycle_complete=True, target_branches=[], ) self.assertIn("target_branches", str(ctx.exception)) def test_subset_results_rejected(self) -> None: """One merged child but two requested targets is rejected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": "merged", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}}, ], lifecycle_complete=True, target_branches=["rel-1", "rel-2"], ) self.assertIn("target branch", str(ctx.exception)) self.assertIn("rel-2", str(ctx.exception)) def test_empty_results_rejected(self) -> None: """No children but requested targets is rejected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[], lifecycle_complete=True, target_branches=["rel-1", "rel-2"], ) self.assertIn("target branch", str(ctx.exception)) def test_extra_results_rejected(self) -> None: """Extra child not in target_branches is rejected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": "merged", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}}, {"target_branch": "rel-2", "status": "merged", "pr": {"url": "https://github.com/o/r/pull/2"}, "evidence": {"test_result": "PASS"}}, {"target_branch": "rel-3", "status": "merged", "pr": {"url": "https://github.com/o/r/pull/3"}, "evidence": {"test_result": "PASS"}}, ], lifecycle_complete=True, target_branches=["rel-1", "rel-2"], ) self.assertIn("target branch", str(ctx.exception)) def test_duplicate_targets_rejected(self) -> None: """Duplicate entries in target_branches is rejected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": "merged", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}}, {"target_branch": "rel-1", "status": "merged", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}}, ], lifecycle_complete=True, target_branches=["rel-1", "rel-1"], ) self.assertIn("duplicate", str(ctx.exception).lower()) def test_malformed_target_empty_string_rejected(self) -> None: """Empty string in target_branches is rejected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": "merged", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}}, ], lifecycle_complete=True, target_branches=["rel-1", ""], ) self.assertIn("nonempty string", str(ctx.exception)) def test_malformed_target_whitespace_rejected(self) -> None: """Whitespace-only string in target_branches is rejected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": "merged", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}}, ], lifecycle_complete=True, target_branches=["rel-1", " "], ) self.assertIn("nonempty string", str(ctx.exception)) def test_malformed_target_none_rejected(self) -> None: """None entry in target_branches is rejected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": "merged", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}}, ], lifecycle_complete=True, target_branches=["rel-1", None], ) self.assertIn("nonempty string", str(ctx.exception)) def test_malformed_target_nonstring_rejected(self) -> None: """Non-string entry in target_branches is rejected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": "merged", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}}, ], lifecycle_complete=True, target_branches=["rel-1", 42], ) self.assertIn("nonempty string", str(ctx.exception)) def test_valid_full_coverage_passes(self) -> None: """All requested branches covered with merged status passes.""" result = unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": "merged", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}}, {"target_branch": "rel-2", "status": "merged", "pr": {"url": "https://github.com/o/r/pull/2"}, "evidence": {"test_result": "PASS"}}, ], lifecycle_complete=True, target_branches=["rel-1", "rel-2"], ) self.assertTrue(result["lifecycle_complete"]) self.assertEqual(result["target_branches"], ["rel-1", "rel-2"]) def test_reordered_coverage_passes(self) -> None: """Result order doesn't need to match target_branches order.""" result = unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-2", "status": "merged", "pr": {"url": "https://github.com/o/r/pull/2"}, "evidence": {"test_result": "PASS"}}, {"target_branch": "rel-1", "status": "merged", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}}, ], lifecycle_complete=True, target_branches=["rel-1", "rel-2"], ) self.assertTrue(result["lifecycle_complete"]) def test_completed_no_target_branches_rejected(self) -> None: """status=completed backport without target_branches is rejected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": "completed", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}}, ], lifecycle_complete=False, ) self.assertIn("target_branches", str(ctx.exception)) def test_partial_status_no_target_branches_valid(self) -> None: """Non-completed status doesn't require target_branches.""" result = unattended.make_result( status="partial", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": "completed", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}}, ], ) self.assertEqual(result["status"], "partial") def test_target_branches_serialized_in_result(self) -> None: """target_branches is carried through to serialized result.""" result = unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": "merged", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}}, ], lifecycle_complete=True, target_branches=["rel-1"], ) self.assertEqual(result["target_branches"], ["rel-1"]) def test_target_branches_none_completed_rejected(self) -> None: """target_branches=None with status=completed backport is rejected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": "completed", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}}, ], ) self.assertIn("target_branches", str(ctx.exception)) class TestBackportTargetSetCLI(unittest.TestCase): """CLI make-result roundtrip for target_branches.""" def test_cli_passes_target_branches(self) -> None: """make-result CLI carries target_branches from data to result.""" with tempfile.TemporaryDirectory() as td: args = argparse.Namespace( status="completed", operation="backport", artifact_dir=td, data=json.dumps({ "scope": {"cve_ids": ["CVE-2024-1"]}, "backport_results": [ {"target_branch": "rel-1", "status": "merged", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}}, {"target_branch": "rel-2", "status": "merged", "pr": {"url": "https://github.com/o/r/pull/2"}, "evidence": {"test_result": "PASS"}}, ], "target_branches": ["rel-1", "rel-2"], "lifecycle_complete": True, }), ) self.assertEqual(unattended._cmd_make_result(args), 0) written = json.loads( (Path(td) / "unattended-result.json").read_text() ) self.assertTrue(written["lifecycle_complete"]) self.assertEqual( written["target_branches"], ["rel-1", "rel-2"] ) def test_cli_rejects_missing_targets_lifecycle(self) -> None: """make-result CLI rejects lifecycle_complete without targets.""" import io from unittest.mock import patch as _patch with tempfile.TemporaryDirectory() as td: args = argparse.Namespace( status="completed", operation="backport", artifact_dir=td, data=json.dumps({ "scope": {"cve_ids": ["CVE-2024-1"]}, "backport_results": [ {"target_branch": "rel-1", "status": "merged", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}}, ], "lifecycle_complete": True, }), ) captured = io.StringIO() with _patch("sys.stderr", captured): self.assertEqual(unattended._cmd_make_result(args), 1) self.assertIn("target_branches", captured.getvalue()) self.assertFalse( Path(td, "unattended-result.json").exists(), "Rejected make-result must not write artifact", ) def test_cli_rejects_subset_coverage(self) -> None: """make-result CLI rejects subset coverage with lifecycle_complete.""" import io from unittest.mock import patch as _patch with tempfile.TemporaryDirectory() as td: args = argparse.Namespace( status="completed", operation="backport", artifact_dir=td, data=json.dumps({ "scope": {"cve_ids": ["CVE-2024-1"]}, "backport_results": [ {"target_branch": "rel-1", "status": "merged", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}}, ], "target_branches": ["rel-1", "rel-2"], "lifecycle_complete": True, }), ) captured = io.StringIO() with _patch("sys.stderr", captured): self.assertEqual(unattended._cmd_make_result(args), 1) self.assertIn("target branch", captured.getvalue()) self.assertFalse( Path(td, "unattended-result.json").exists(), "Rejected make-result must not write artifact", ) # --------------------------------------------------------------------------- # Review 5777476150 — completed backport coverage (lifecycle_complete=False) # --------------------------------------------------------------------------- class TestCompletedBackportCoverage(unittest.TestCase): """Completed backport requires target_branches coverage regardless of lifecycle_complete. The contract says status=completed for a backport means all target branches have completed or merged children. The coverage guard must fire for any completed backport, not only lifecycle_complete=True. lifecycle_complete=True additionally requires every child to be merged. """ # --- Rejection: missing / empty target_branches --- def test_completed_empty_results_no_targets_rejected(self) -> None: """Completed backport with no results and no targets is rejected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[], lifecycle_complete=False, ) self.assertIn("target_branches", str(ctx.exception)) def test_completed_empty_targets_rejected(self) -> None: """Completed backport with empty target_branches is rejected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": "completed", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}}, ], target_branches=[], lifecycle_complete=False, ) self.assertIn("target_branches", str(ctx.exception)) # --- Rejection: subset / extra / duplicate --- def test_completed_subset_children_rejected(self) -> None: """Subset of target branches covered is rejected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": "completed", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}}, ], target_branches=["rel-1", "rel-2"], lifecycle_complete=False, ) self.assertIn("target branch", str(ctx.exception)) self.assertIn("rel-2", str(ctx.exception)) def test_completed_extra_children_rejected(self) -> None: """Extra child not in target_branches is rejected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": "completed", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}}, {"target_branch": "rel-2", "status": "completed", "pr": {"url": "https://github.com/o/r/pull/2"}, "evidence": {"test_result": "PASS"}}, {"target_branch": "rel-3", "status": "completed", "pr": {"url": "https://github.com/o/r/pull/3"}, "evidence": {"test_result": "PASS"}}, ], target_branches=["rel-1", "rel-2"], lifecycle_complete=False, ) self.assertIn("target branch", str(ctx.exception)) def test_completed_duplicate_targets_rejected(self) -> None: """Duplicate target_branches entries are rejected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": "completed", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}}, {"target_branch": "rel-1", "status": "completed", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}}, ], target_branches=["rel-1", "rel-1"], lifecycle_complete=False, ) self.assertIn("duplicate", str(ctx.exception).lower()) # --- Rejection: malformed children and target labels --- def test_completed_non_dict_child_rejected(self) -> None: """Non-dict child in completed backport (lifecycle=false) rejected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": "completed", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}}, "bogus-scalar", ], target_branches=["rel-1", "rel-2"], lifecycle_complete=False, ) self.assertIn("dict", str(ctx.exception)) def test_completed_blank_target_entry_rejected(self) -> None: """Blank string in target_branches is rejected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": "completed", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}}, ], target_branches=["rel-1", " "], lifecycle_complete=False, ) self.assertIn("nonempty string", str(ctx.exception)) def test_completed_none_target_entry_rejected(self) -> None: """None entry in target_branches is rejected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": "completed", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}}, ], target_branches=["rel-1", None], lifecycle_complete=False, ) self.assertIn("nonempty string", str(ctx.exception)) def test_completed_child_blank_target_branch_rejected(self) -> None: """Child with blank target_branch is rejected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "", "status": "completed"}, ], target_branches=["rel-1"], lifecycle_complete=False, ) self.assertIn("nonempty string", str(ctx.exception)) # --- Success: exact coverage with completed children --- def test_completed_exact_coverage_completed_children(self) -> None: """Exact target coverage with all completed children passes.""" result = unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": "completed", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}}, {"target_branch": "rel-2", "status": "completed", "pr": {"url": "https://github.com/o/r/pull/2"}, "evidence": {"test_result": "PASS"}}, ], target_branches=["rel-1", "rel-2"], lifecycle_complete=False, ) self.assertEqual(result["status"], "completed") self.assertFalse(result["lifecycle_complete"]) self.assertEqual(result["target_branches"], ["rel-1", "rel-2"]) def test_completed_exact_coverage_merged_children(self) -> None: """Exact target coverage with all merged children passes.""" result = unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": "merged", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}}, {"target_branch": "rel-2", "status": "merged", "pr": {"url": "https://github.com/o/r/pull/2"}, "evidence": {"test_result": "PASS"}}, ], target_branches=["rel-1", "rel-2"], lifecycle_complete=False, ) self.assertEqual(result["status"], "completed") self.assertFalse(result["lifecycle_complete"]) def test_completed_reordered_coverage_passes(self) -> None: """Result order doesn't need to match target_branches order.""" result = unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-2", "status": "completed", "pr": {"url": "https://github.com/o/r/pull/2"}, "evidence": {"test_result": "PASS"}}, {"target_branch": "rel-1", "status": "completed", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}}, ], target_branches=["rel-1", "rel-2"], lifecycle_complete=False, ) self.assertEqual(result["status"], "completed") self.assertFalse(result["lifecycle_complete"]) def test_completed_mixed_completed_merged_valid(self) -> None: """Mixed completed+merged children valid for operation completion.""" result = unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": "completed", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}}, {"target_branch": "rel-2", "status": "merged", "pr": {"url": "https://github.com/o/r/pull/2"}, "evidence": {"test_result": "PASS"}}, ], target_branches=["rel-1", "rel-2"], lifecycle_complete=False, ) self.assertEqual(result["status"], "completed") self.assertFalse(result["lifecycle_complete"]) def test_lifecycle_true_rejects_mixed_completed_merged(self) -> None: """lifecycle=true rejects mixed completed+merged (all must merge).""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": "completed", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}}, {"target_branch": "rel-2", "status": "merged", "pr": {"url": "https://github.com/o/r/pull/2"}, "evidence": {"test_result": "PASS"}}, ], target_branches=["rel-1", "rel-2"], lifecycle_complete=True, ) self.assertIn("lifecycle_complete=True", str(ctx.exception)) # --- Non-completed status is exempt from coverage --- def test_partial_status_no_targets_valid(self) -> None: """status=partial doesn't require target_branches.""" result = unattended.make_result( status="partial", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": "completed", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}}, ], ) self.assertEqual(result["status"], "partial") def test_blocked_status_no_targets_valid(self) -> None: """status=blocked doesn't require target_branches.""" result = unattended.make_result( status="blocked", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, blockers=[{"description": "conflict"}], ) self.assertEqual(result["status"], "blocked") # --- CLI coverage tests for lifecycle_complete=False --- def test_cli_completed_no_targets_rejected(self) -> None: """CLI rejects completed backport without target_branches.""" import io from unittest.mock import patch as _patch with tempfile.TemporaryDirectory() as td: args = argparse.Namespace( status="completed", operation="backport", artifact_dir=td, data=json.dumps({ "scope": {"cve_ids": ["CVE-2024-1"]}, "backport_results": [ {"target_branch": "rel-1", "status": "completed", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}}, ], "lifecycle_complete": False, }), ) captured = io.StringIO() with _patch("sys.stderr", captured): self.assertEqual(unattended._cmd_make_result(args), 1) self.assertIn("target_branches", captured.getvalue()) self.assertFalse( Path(td, "unattended-result.json").exists(), "Rejected make-result must not write artifact", ) def test_cli_completed_subset_rejected(self) -> None: """CLI rejects completed backport with subset coverage.""" import io from unittest.mock import patch as _patch with tempfile.TemporaryDirectory() as td: args = argparse.Namespace( status="completed", operation="backport", artifact_dir=td, data=json.dumps({ "scope": {"cve_ids": ["CVE-2024-1"]}, "backport_results": [ {"target_branch": "rel-1", "status": "completed", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}}, ], "target_branches": ["rel-1", "rel-2"], "lifecycle_complete": False, }), ) captured = io.StringIO() with _patch("sys.stderr", captured): self.assertEqual(unattended._cmd_make_result(args), 1) self.assertIn("target branch", captured.getvalue()) self.assertFalse( Path(td, "unattended-result.json").exists(), "Rejected make-result must not write artifact", ) def test_cli_completed_exact_accepted(self) -> None: """CLI accepts completed backport with exact coverage.""" with tempfile.TemporaryDirectory() as td: args = argparse.Namespace( status="completed", operation="backport", artifact_dir=td, data=json.dumps({ "scope": {"cve_ids": ["CVE-2024-1"]}, "backport_results": [ {"target_branch": "rel-1", "status": "completed", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}}, {"target_branch": "rel-2", "status": "completed", "pr": {"url": "https://github.com/o/r/pull/2"}, "evidence": {"test_result": "PASS"}}, ], "target_branches": ["rel-1", "rel-2"], "lifecycle_complete": False, }), ) self.assertEqual(unattended._cmd_make_result(args), 0) result_path = Path(td, "unattended-result.json") self.assertTrue( result_path.exists(), "Accepted make-result must write artifact", ) written = json.loads(result_path.read_text()) self.assertEqual(written["status"], "completed") self.assertFalse(written["lifecycle_complete"]) self.assertEqual( written["target_branches"], ["rel-1", "rel-2"], ) # --------------------------------------------------------------------------- # Review 5777712816 — completed/merged children require pr.url + evidence # --------------------------------------------------------------------------- class TestBackportChildPrEvidence(unittest.TestCase): """Completed/merged backport children must have pr.url and evidence. The contract requires independent evidence for each completed or merged target branch child. Structural validation ensures pr is a dict with a nonempty string url and evidence is a nonempty dict. """ _VALID_PR = {"url": "https://github.com/o/r/pull/1"} _VALID_EV = {"test_result": "PASS"} # --- Rejection: missing/malformed pr --- def test_completed_child_no_pr_rejected(self) -> None: """Completed child without pr is rejected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": "completed", "evidence": self._VALID_EV}, ], target_branches=["rel-1"], ) self.assertIn("pr.url", str(ctx.exception)) def test_completed_child_pr_not_dict_rejected(self) -> None: """Completed child with pr as string is rejected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": "completed", "pr": "https://github.com/o/r/pull/1", "evidence": self._VALID_EV}, ], target_branches=["rel-1"], ) self.assertIn("pr.url", str(ctx.exception)) def test_completed_child_empty_pr_url_rejected(self) -> None: """Completed child with empty pr.url is rejected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": "completed", "pr": {"url": ""}, "evidence": self._VALID_EV}, ], target_branches=["rel-1"], ) self.assertIn("pr.url", str(ctx.exception)) def test_completed_child_whitespace_pr_url_rejected(self) -> None: """Completed child with whitespace-only pr.url is rejected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": "completed", "pr": {"url": " "}, "evidence": self._VALID_EV}, ], target_branches=["rel-1"], ) self.assertIn("pr.url", str(ctx.exception)) def test_completed_child_pr_url_not_string_rejected(self) -> None: """Completed child with non-string pr.url is rejected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": "completed", "pr": {"url": 42}, "evidence": self._VALID_EV}, ], target_branches=["rel-1"], ) self.assertIn("pr.url", str(ctx.exception)) # --- Rejection: missing/malformed evidence --- def test_completed_child_no_evidence_rejected(self) -> None: """Completed child without evidence is rejected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": "completed", "pr": self._VALID_PR}, ], target_branches=["rel-1"], ) self.assertIn("evidence", str(ctx.exception)) def test_completed_child_empty_evidence_rejected(self) -> None: """Completed child with empty evidence dict is rejected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": "completed", "pr": self._VALID_PR, "evidence": {}}, ], target_branches=["rel-1"], ) self.assertIn("evidence", str(ctx.exception)) def test_completed_child_evidence_not_dict_rejected(self) -> None: """Completed child with evidence as string is rejected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": "completed", "pr": self._VALID_PR, "evidence": "PASS"}, ], target_branches=["rel-1"], ) self.assertIn("evidence", str(ctx.exception)) # --- Rejection: merged child same checks --- def test_merged_child_no_pr_rejected(self) -> None: """Merged child without pr is rejected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": "merged", "evidence": self._VALID_EV}, ], target_branches=["rel-1"], lifecycle_complete=True, ) self.assertIn("pr.url", str(ctx.exception)) def test_merged_child_no_evidence_rejected(self) -> None: """Merged child without evidence is rejected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": "merged", "pr": self._VALID_PR}, ], target_branches=["rel-1"], lifecycle_complete=True, ) self.assertIn("evidence", str(ctx.exception)) # --- Rejection: later bad child detected --- def test_second_child_bad_pr_rejected(self) -> None: """First child valid, second child missing pr is rejected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": "completed", "pr": self._VALID_PR, "evidence": self._VALID_EV}, {"target_branch": "rel-2", "status": "completed", "evidence": self._VALID_EV}, ], target_branches=["rel-1", "rel-2"], ) self.assertIn("[1]", str(ctx.exception)) self.assertIn("pr.url", str(ctx.exception)) # --- Success: valid children --- def test_completed_child_with_pr_evidence_accepted(self) -> None: """Completed child with valid pr and evidence passes.""" result = unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": "completed", "pr": self._VALID_PR, "evidence": self._VALID_EV}, ], target_branches=["rel-1"], ) self.assertEqual(result["status"], "completed") def test_mixed_completed_merged_with_evidence(self) -> None: """Mixed completed/merged children with pr/evidence passes.""" result = unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": "completed", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}}, {"target_branch": "rel-2", "status": "merged", "pr": {"url": "https://github.com/o/r/pull/2"}, "evidence": {"scan_verdict": "fixed"}}, ], target_branches=["rel-1", "rel-2"], ) self.assertEqual(result["status"], "completed") self.assertFalse(result["lifecycle_complete"]) def test_all_merged_lifecycle_true_with_evidence(self) -> None: """All merged lifecycle=true with pr/evidence passes.""" result = unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": "merged", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}}, {"target_branch": "rel-2", "status": "merged", "pr": {"url": "https://github.com/o/r/pull/2"}, "evidence": {"test_result": "PASS"}}, ], target_branches=["rel-1", "rel-2"], lifecycle_complete=True, ) self.assertTrue(result["lifecycle_complete"]) # --- CLI tests --- def test_cli_no_pr_rejected(self) -> None: """CLI rejects completed child without pr.""" import io from unittest.mock import patch as _patch with tempfile.TemporaryDirectory() as td: args = argparse.Namespace( status="completed", operation="backport", artifact_dir=td, data=json.dumps({ "scope": {"cve_ids": ["CVE-2024-1"]}, "backport_results": [ {"target_branch": "rel-1", "status": "completed", "evidence": {"test_result": "PASS"}}, ], "target_branches": ["rel-1"], }), ) captured = io.StringIO() with _patch("sys.stderr", captured): self.assertEqual(unattended._cmd_make_result(args), 1) self.assertIn("pr.url", captured.getvalue()) self.assertFalse( Path(td, "unattended-result.json").exists(), "Rejected make-result must not write artifact", ) def test_cli_no_evidence_rejected(self) -> None: """CLI rejects completed child without evidence.""" import io from unittest.mock import patch as _patch with tempfile.TemporaryDirectory() as td: args = argparse.Namespace( status="completed", operation="backport", artifact_dir=td, data=json.dumps({ "scope": {"cve_ids": ["CVE-2024-1"]}, "backport_results": [ {"target_branch": "rel-1", "status": "completed", "pr": {"url": "https://github.com/o/r/pull/1"}}, ], "target_branches": ["rel-1"], }), ) captured = io.StringIO() with _patch("sys.stderr", captured): self.assertEqual(unattended._cmd_make_result(args), 1) self.assertIn("evidence", captured.getvalue()) self.assertFalse( Path(td, "unattended-result.json").exists(), "Rejected make-result must not write artifact", ) def test_cli_valid_completed_child_accepted(self) -> None: """CLI accepts completed child with valid pr and evidence.""" with tempfile.TemporaryDirectory() as td: args = argparse.Namespace( status="completed", operation="backport", artifact_dir=td, data=json.dumps({ "scope": {"cve_ids": ["CVE-2024-1"]}, "backport_results": [ {"target_branch": "rel-1", "status": "completed", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}}, ], "target_branches": ["rel-1"], }), ) self.assertEqual(unattended._cmd_make_result(args), 0) result_path = Path(td, "unattended-result.json") self.assertTrue(result_path.exists()) written = json.loads(result_path.read_text()) self.assertEqual(written["status"], "completed") self.assertEqual( written["backport_results"][0]["pr"]["url"], "https://github.com/o/r/pull/1", ) class TestMalformedBackportChildren(unittest.TestCase): """Malformed backport_results entries must be cleanly rejected. Non-dict children, non-string status values, and non-string target_branch values must raise ValueError (not silently pass or raise uncaught TypeError). """ def test_non_dict_child_rejected(self) -> None: """Scalar child in backport_results raises ValueError.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": "merged", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}}, "bogus-scalar", ], lifecycle_complete=True, target_branches=["rel-1"], ) self.assertIn("dict", str(ctx.exception)) self.assertIn("[1]", str(ctx.exception)) def test_non_dict_child_lifecycle_false_rejected(self) -> None: """Non-dict child rejected even with lifecycle_complete=False.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": "completed", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}}, 42, ], ) self.assertIn("dict", str(ctx.exception)) def test_child_status_list_rejected(self) -> None: """Child status=[] raises ValueError, not TypeError.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": []}, ], ) self.assertIn("string", str(ctx.exception)) def test_child_status_bool_rejected(self) -> None: """Child status=True raises ValueError.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": True}, ], ) self.assertIn("string", str(ctx.exception)) def test_child_target_branch_none_rejected(self) -> None: """Child target_branch=None raises ValueError, not TypeError.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": "merged", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}}, {"target_branch": None, "status": "merged"}, ], lifecycle_complete=True, target_branches=["rel-1", "rel-2"], ) self.assertIn("nonempty string", str(ctx.exception)) def test_child_target_branch_bool_rejected(self) -> None: """Child target_branch=True raises ValueError.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": True, "status": "merged"}, ], lifecycle_complete=True, target_branches=["rel-1"], ) self.assertIn("nonempty string", str(ctx.exception)) def test_child_target_branch_blank_rejected(self) -> None: """Child target_branch='' raises ValueError.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "", "status": "merged"}, ], lifecycle_complete=True, target_branches=["rel-1"], ) self.assertIn("nonempty string", str(ctx.exception)) def test_duplicate_child_branches_unique_targets_rejected(self) -> None: """Two children claim same branch; unique expected targets.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": "merged", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}}, {"target_branch": "rel-1", "status": "merged", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}}, ], lifecycle_complete=True, target_branches=["rel-1", "rel-2"], ) self.assertIn("target branch", str(ctx.exception)) class TestMalformedBackportChildrenCLI(unittest.TestCase): """CLI malformed children must return exit 1, not traceback.""" def test_cli_non_dict_child_exit1(self) -> None: """Non-dict child via CLI returns clean exit 1 with cause.""" import io from unittest.mock import patch as _patch with tempfile.TemporaryDirectory() as td: args = argparse.Namespace( status="completed", operation="backport", artifact_dir=td, data=json.dumps({ "scope": {"cve_ids": ["CVE-2024-1"]}, "backport_results": [ {"target_branch": "rel-1", "status": "merged", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}}, "bogus", ], "target_branches": ["rel-1"], "lifecycle_complete": True, }), ) captured = io.StringIO() with _patch("sys.stderr", captured): self.assertEqual(unattended._cmd_make_result(args), 1) self.assertIn("dict", captured.getvalue()) self.assertFalse( Path(td, "unattended-result.json").exists(), "Rejected make-result must not write artifact", ) def test_cli_child_status_list_exit1(self) -> None: """List-status child via CLI returns clean exit 1 with cause.""" import io from unittest.mock import patch as _patch with tempfile.TemporaryDirectory() as td: args = argparse.Namespace( status="completed", operation="backport", artifact_dir=td, data=json.dumps({ "scope": {"cve_ids": ["CVE-2024-1"]}, "backport_results": [ {"target_branch": "rel-1", "status": []}, ], "target_branches": ["rel-1"], }), ) captured = io.StringIO() with _patch("sys.stderr", captured): self.assertEqual(unattended._cmd_make_result(args), 1) self.assertIn("must be a string", captured.getvalue()) self.assertFalse( Path(td, "unattended-result.json").exists(), "Rejected make-result must not write artifact", ) def test_cli_child_target_none_exit1(self) -> None: """None target_branch child via CLI returns clean exit 1 with cause.""" import io from unittest.mock import patch as _patch with tempfile.TemporaryDirectory() as td: args = argparse.Namespace( status="completed", operation="backport", artifact_dir=td, data=json.dumps({ "scope": {"cve_ids": ["CVE-2024-1"]}, "backport_results": [ {"target_branch": "rel-1", "status": "merged", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}}, {"target_branch": None, "status": "merged"}, ], "target_branches": ["rel-1", "rel-2"], "lifecycle_complete": True, }), ) captured = io.StringIO() with _patch("sys.stderr", captured): self.assertEqual(unattended._cmd_make_result(args), 1) self.assertIn("nonempty string", captured.getvalue()) self.assertFalse( Path(td, "unattended-result.json").exists(), "Rejected make-result must not write artifact", ) def test_cli_duplicate_children_exit1(self) -> None: """Duplicate child branches via CLI returns clean exit 1 with cause.""" import io from unittest.mock import patch as _patch with tempfile.TemporaryDirectory() as td: args = argparse.Namespace( status="completed", operation="backport", artifact_dir=td, data=json.dumps({ "scope": {"cve_ids": ["CVE-2024-1"]}, "backport_results": [ {"target_branch": "rel-1", "status": "merged", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}}, {"target_branch": "rel-1", "status": "merged", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}}, ], "target_branches": ["rel-1", "rel-2"], "lifecycle_complete": True, }), ) captured = io.StringIO() with _patch("sys.stderr", captured): self.assertEqual(unattended._cmd_make_result(args), 1) self.assertIn("target branch", captured.getvalue()) self.assertFalse( Path(td, "unattended-result.json").exists(), "Rejected make-result must not write artifact", ) # --------------------------------------------------------------------------- # B1-4472834644: prior_result base_branch binding # --------------------------------------------------------------------------- class TestPriorResultBaseBranchBinding(unittest.TestCase): """Explicit request base_branch must bind to prior_result snapshot.""" def test_cross_branch_prior_result_rejected(self) -> None: """Request base_branch=release-2.16 vs snapshot main must fail.""" req = { "operation": "remediate", "cve_ids": ["CVE-2024-1"], "base_branch": "release-2.16", "prior_result": { "result": {"status": "needs_human_review"}, "decision_requests": [], "scan_snapshot": { "scan_id": "s1", "scope": { "cve_ids": ["CVE-2024-1"], "base_branch": "main", }, "candidate_sha": "abc123", }, }, } errors = unattended.validate_request(req) self.assertTrue( any("base_branch" in e and "stale" in e for e in errors), f"Expected base_branch mismatch error, got: {errors}", ) def test_matching_base_branch_prior_result_accepted(self) -> None: """Request base_branch=main matching snapshot passes.""" req = { "operation": "remediate", "cve_ids": ["CVE-2024-1"], "base_branch": "main", "prior_result": { "result": {"status": "needs_human_review"}, "decision_requests": [], "scan_snapshot": { "scan_id": "s1", "scope": { "cve_ids": ["CVE-2024-1"], "base_branch": "main", }, "candidate_sha": "abc123", }, }, } errors = unattended.validate_request(req) self.assertEqual(errors, []) def test_omitted_base_branch_not_checked(self) -> None: """No request base_branch means no snapshot comparison for it.""" req = { "operation": "remediate", "cve_ids": ["CVE-2024-1"], "prior_result": { "result": {}, "decision_requests": [], "scan_snapshot": { "scan_id": "s1", "scope": { "cve_ids": ["CVE-2024-1"], "base_branch": "main", }, "candidate_sha": "abc123", }, }, } errors = unattended.validate_request(req) self.assertEqual(errors, []) def test_repo_path_not_used_for_scope(self) -> None: """repo_path is a filesystem path; must not be equated to repo. A filesystem repo_path like '/tmp/checkout/org/repo' is not the canonical repository identity ('org/repo'). Validation must not derive a scope 'repo' key from it. The request is fully valid: repo_path is an opaque string hint for the local checkout, not a canonical identifier. """ req = { "operation": "remediate", "cve_ids": ["CVE-2024-1"], "repo_path": "/tmp/checkout/org/repo", "prior_result": { "result": {}, "decision_requests": [], "scan_snapshot": { "scan_id": "s1", "scope": { "cve_ids": ["CVE-2024-1"], "repo": "org/repo", }, "candidate_sha": "abc123", }, }, } errors = unattended.validate_request(req) # The request is valid — repo_path is not used for scope # comparison, so no stale/mismatch errors should appear self.assertEqual( errors, [], "repo_path is a filesystem hint, not canonical repo identity; " "request should be fully valid", ) # --------------------------------------------------------------------------- # Review 5776505195 — close_vex no-code completion consumer roundtrip # --------------------------------------------------------------------------- class TestCloseVexNoCodeCompletion(unittest.TestCase): """Accepted close_vex decision enables valid no-code completion. An accepted, authorized close_vex decision on a CVE-absent scan should produce a valid completed result with no PR and no Jira write. Absent, stale, or unauthorized decisions must produce needs_human_review, not completion. """ def _make_scope(self) -> dict: return { "cve_ids": ["CVE-2024-1234"], "packages": ["golang.org/x/net"], "repo": "org/repo", "base_branch": "main", } def _make_decision_request(self, scan_id: str, scope: dict) -> dict: return unattended.make_human_decision_request( decision_id="vex-review-1", question="CVE-2024-1234 absent — authorize close_vex?", evidence="Package not imported in any shipped binary.", scope=scope, scan_id=scan_id, requested_effect=( "Record CVE-2024-1234 as reviewed VEX finding. " "Jira closure requires separate jira_action request." ), permitted_effects=["close_vex", "reject", "defer"], ) def _make_accepted_response(self, scan_id: str, scope: dict) -> dict: return { "decision_id": "vex-review-1", "accepted": True, "reviewer": "jsmith", "justification": "component_not_present", "evidence": "Package not imported in any shipped binary.", "scan_id": scan_id, "scope": scope, "timestamp": "2026-11-04T12:00:00Z", "selected_effect": "close_vex", } def test_accepted_close_vex_valid_completion(self) -> None: """Accepted close_vex -> completed no-code/no-PR result is valid.""" scope = self._make_scope() scan_id = "scan-20261104-abc" # Validate the decision response original_request = self._make_decision_request(scan_id, scope) response = self._make_accepted_response(scan_id, scope) authority = {"allowed_reviewer_ids": ["jsmith"]} errors = unattended.validate_human_decision_response( response=response, expected_scan_id=scan_id, expected_scope=scope, original_request=original_request, authority_context=authority, ) self.assertEqual(errors, [], f"Decision validation failed: {errors}") # Produce a completed result with no PR (valid no-code completion) result = unattended.make_result( status="completed", operation="remediate", scope=scope, evidence={ "scan_verdict": "absent", "scan_id": scan_id, "validation_verdict": "PASS", "vex_justification": "component_not_present", "vex_reviewer": "jsmith", }, human_decisions=[response], ) self.assertEqual(result["status"], "completed") self.assertIsNone(result["pr"]) self.assertFalse(result["lifecycle_complete"]) self.assertEqual(len(result["jira_results"]), 0) def test_absent_decision_needs_human_review(self) -> None: """No decision supplied -> needs_human_review result is valid.""" scope = self._make_scope() result = unattended.make_result( status="needs_human_review", operation="remediate", scope=scope, evidence={ "scan_verdict": "absent", "scan_id": "scan-20261104-abc", }, human_decisions=[], ) self.assertEqual(result["status"], "needs_human_review") self.assertIsNone(result["pr"]) def test_stale_scan_decision_rejected(self) -> None: """Decision with wrong scan_id is rejected by validation.""" scope = self._make_scope() current_scan = "scan-20261104-def" stale_scan = "scan-20261104-abc" original_request = self._make_decision_request(stale_scan, scope) response = self._make_accepted_response(stale_scan, scope) authority = {"allowed_reviewer_ids": ["jsmith"]} errors = unattended.validate_human_decision_response( response=response, expected_scan_id=current_scan, expected_scope=scope, original_request=original_request, authority_context=authority, ) self.assertTrue(len(errors) > 0) self.assertTrue( any("scan" in e.lower() for e in errors), f"Expected scan mismatch error, got: {errors}", ) def test_unauthorized_reviewer_rejected(self) -> None: """Decision from unauthorized reviewer is rejected.""" scope = self._make_scope() scan_id = "scan-20261104-abc" original_request = self._make_decision_request(scan_id, scope) response = self._make_accepted_response(scan_id, scope) # jsmith is the reviewer, but allowed list has different user authority = {"allowed_reviewer_ids": ["admin-only"]} errors = unattended.validate_human_decision_response( response=response, expected_scan_id=scan_id, expected_scope=scope, original_request=original_request, authority_context=authority, ) self.assertTrue(len(errors) > 0) self.assertTrue( any("allowed_reviewer" in e for e in errors), f"Expected reviewer rejection, got: {errors}", ) def test_cli_validate_decision_roundtrip(self) -> None: """Full CLI validate-decision roundtrip for accepted close_vex.""" scope = self._make_scope() scan_id = "scan-20261104-abc" original_request = self._make_decision_request(scan_id, scope) response = self._make_accepted_response(scan_id, scope) authority = {"allowed_reviewer_ids": ["jsmith"]} with tempfile.TemporaryDirectory() as td: resp_path = os.path.join(td, "response.json") with open(resp_path, "w") as resp_f: json.dump(response, resp_f) orig_path = os.path.join(td, "original_request.json") with open(orig_path, "w") as orig_f: json.dump(original_request, orig_f) args = argparse.Namespace( response_file=resp_path, scan_id=scan_id, scope=json.dumps(scope), original_request=orig_path, authority_context=json.dumps(authority), ) # Capture stdout to verify action_authorized (exit 0 alone # is insufficient — a well-formed rejected decision also # exits 0 with action_authorized=false) import io from unittest.mock import patch as _patch captured = io.StringIO() with _patch("sys.stdout", captured): exit_code = unattended._cmd_validate_decision(args) self.assertEqual(exit_code, 0, "CLI should exit 0 for valid decision") output = json.loads(captured.getvalue()) self.assertTrue( output.get("action_authorized"), "Accepted close_vex must produce action_authorized=true", ) self.assertTrue(output.get("valid")) self.assertEqual(output.get("selected_effect"), "close_vex") # --------------------------------------------------------------------------- # B1-4472834621: completion allowlists (verdict + child statuses) # --------------------------------------------------------------------------- class TestCompletionAllowlistRegression(unittest.TestCase): """Allowlist enforcement for completed status (not denylist).""" def test_whitespace_verdict_normalized(self) -> None: """' PASS ' should be accepted after stripping.""" result = unattended.make_result( status="completed", operation="remediate", scope={"cve_ids": ["CVE-2024-1"]}, evidence={"validation_verdict": " PASS "}, ) self.assertEqual(result["status"], "completed") def test_lowercase_pass_accepted(self) -> None: """'pass' should be accepted after case normalization.""" result = unattended.make_result( status="completed", operation="remediate", scope={"cve_ids": ["CVE-2024-1"]}, evidence={"validation_verdict": "pass"}, ) self.assertEqual(result["status"], "completed") def test_fixed_verdict_rejected(self) -> None: """FIXED is a scanner verdict, not an aggregate validation verdict.""" with self.assertRaises(ValueError): unattended.make_result( status="completed", operation="remediate", scope={"cve_ids": ["CVE-2024-1"]}, evidence={"validation_verdict": "Fixed"}, ) def test_not_affected_verdict_rejected(self) -> None: """NOT_AFFECTED is not a valid aggregate validation verdict.""" with self.assertRaises(ValueError): unattended.make_result( status="completed", operation="remediate", scope={"cve_ids": ["CVE-2024-1"]}, evidence={"validation_verdict": "NOT_AFFECTED"}, ) def test_blank_verdict_rejected(self) -> None: """Blank/whitespace verdict with completed must fail.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="remediate", scope={"cve_ids": ["CVE-2024-1"]}, evidence={"validation_verdict": " "}, ) self.assertIn("blank", str(ctx.exception)) def test_partial_verdict_rejected(self) -> None: """PARTIAL is not in the success allowlist.""" with self.assertRaises(ValueError): unattended.make_result( status="completed", operation="remediate", scope={"cve_ids": ["CVE-2024-1"]}, evidence={"validation_verdict": "PARTIAL"}, ) def test_unknown_verdict_still_rejected(self) -> None: """UNKNOWN is not in the success allowlist.""" with self.assertRaises(ValueError): unattended.make_result( status="completed", operation="remediate", scope={"cve_ids": ["CVE-2024-1"]}, evidence={"validation_verdict": "UNKNOWN"}, ) def test_skipped_child_rejected(self) -> None: """'skipped' backport child rejected even lifecycle_complete=False.""" with self.assertRaisesRegex( ValueError, r"status.*'skipped'.*must be one of" ): unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": "skipped"}, ], target_branches=["rel-1"], ) def test_pending_child_rejected(self) -> None: """'pending' backport child rejected even lifecycle_complete=False.""" with self.assertRaisesRegex( ValueError, r"status.*'pending'.*must be one of" ): unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": "pending"}, ], target_branches=["rel-1"], ) def test_conflicted_child_rejected(self) -> None: """'conflicted' backport child rejected.""" with self.assertRaisesRegex( ValueError, r"status.*'conflicted'.*must be one of" ): unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": "conflicted"}, ], target_branches=["rel-1"], ) def test_empty_child_status_rejected(self) -> None: """Empty string child status rejected.""" with self.assertRaisesRegex( ValueError, r"status.*''.*must be one of" ): unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": ""}, ], target_branches=["rel-1"], ) def test_awaiting_merge_child_rejected(self) -> None: """'awaiting_merge' is a top-level status, not a valid child outcome.""" with self.assertRaisesRegex( ValueError, r"status.*'awaiting_merge'.*must be one of" ): unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": "awaiting_merge"}, ], target_branches=["rel-1"], ) def test_awaiting_review_child_rejected(self) -> None: """'awaiting_review' is a top-level status, not a valid child outcome.""" with self.assertRaisesRegex( ValueError, r"status.*'awaiting_review'.*must be one of" ): unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, backport_results=[ {"target_branch": "rel-1", "status": "awaiting_review"}, ], target_branches=["rel-1"], ) def test_absent_verdict_still_valid(self) -> None: """No validation_verdict in evidence is fine (e.g. jira_action).""" result = unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[{"ticket": "EDM-1", "result": "success"}], expected_tickets=["EDM-1"], expected_jira_action={"action": "transition", "target_status": "MODIFIED"}, ) self.assertEqual(result["status"], "completed") # NOTE: The 7 assertRaisesRegex tests above verify each bad status # against the specific "must be one of" error pattern from the # _VALID_COMPLETED_CHILD_STATUSES allowlist, with valid surrounding # fixtures (matching target_branches, valid scope). No separate # mutation test needed — the regex pattern matches only the # allowlist guard's error message. # --------------------------------------------------------------------------- # Validation verdict bypass guard (finding B: completed remediate with # validate in stages_completed requires evidence.validation_verdict) # --------------------------------------------------------------------------- class TestValidateVerdictBypassGuard(unittest.TestCase): """completed+remediate+validate stage requires actual verdict proof.""" def _base_kwargs(self, **overrides: Any) -> dict[str, Any]: """Base kwargs for make_result with completed remediate.""" defaults: dict[str, Any] = { "status": "completed", "operation": "remediate", "scope": {"cve_ids": ["CVE-2024-1234"]}, "stages_completed": ["start", "scan", "patch", "validate", "pr"], "evidence": {"validation_verdict": "PASS"}, } defaults.update(overrides) return defaults # --- Rejected: validate in stages but no valid verdict --- def test_none_evidence_rejected(self) -> None: """evidence=None with validate stage listed -> reject.""" with self.assertRaisesRegex(ValueError, "validation_verdict"): unattended.make_result(**self._base_kwargs(evidence=None)) def test_empty_evidence_rejected(self) -> None: """evidence={} (missing verdict) with validate stage -> reject.""" with self.assertRaisesRegex(ValueError, "validation_verdict"): unattended.make_result(**self._base_kwargs(evidence={})) def test_null_verdict_rejected(self) -> None: """evidence.validation_verdict=None with validate stage -> reject.""" with self.assertRaisesRegex(ValueError, "validation_verdict"): unattended.make_result( **self._base_kwargs(evidence={"validation_verdict": None}) ) def test_blank_verdict_rejected(self) -> None: """Blank verdict with validate stage -> reject.""" with self.assertRaisesRegex(ValueError, "blank"): unattended.make_result( **self._base_kwargs(evidence={"validation_verdict": " "}) ) def test_fail_verdict_rejected(self) -> None: with self.assertRaisesRegex(ValueError, "FAIL"): unattended.make_result( **self._base_kwargs(evidence={"validation_verdict": "FAIL"}) ) def test_unknown_verdict_rejected(self) -> None: with self.assertRaisesRegex(ValueError, "UNKNOWN"): unattended.make_result( **self._base_kwargs( evidence={"validation_verdict": "UNKNOWN"} ) ) def test_pending_verdict_rejected(self) -> None: with self.assertRaisesRegex(ValueError, "PENDING"): unattended.make_result( **self._base_kwargs( evidence={"validation_verdict": "PENDING"} ) ) def test_scan_only_verdict_rejected(self) -> None: """scan_verdict='fixed' is not validation_verdict.""" with self.assertRaisesRegex(ValueError, "validation_verdict"): unattended.make_result( **self._base_kwargs( evidence={"scan_verdict": "fixed"} ) ) def test_nonstring_verdict_rejected(self) -> None: with self.assertRaisesRegex(ValueError, "non-string"): unattended.make_result( **self._base_kwargs(evidence={"validation_verdict": True}) ) # --- Accepted: valid PASS verdict --- def test_pass_verdict_accepted(self) -> None: result = unattended.make_result(**self._base_kwargs()) self.assertEqual(result["status"], "completed") def test_lowercase_pass_accepted(self) -> None: result = unattended.make_result( **self._base_kwargs(evidence={"validation_verdict": "pass"}) ) self.assertEqual(result["status"], "completed") # --- Preserved: no-code/close_vex without validate stage --- def test_close_vex_no_validate_stage_accepted(self) -> None: """close_vex completion without validate in stages is valid.""" result = unattended.make_result( status="completed", operation="remediate", scope={"cve_ids": ["CVE-2024-1"]}, stages_completed=["start", "scan"], evidence={ "scan_verdict": "absent", "vex_justification": "component_not_present", }, ) self.assertEqual(result["status"], "completed") def test_merged_pr_no_validate_stage_accepted(self) -> None: """Merged existing PR completion without validate listed is valid.""" result = unattended.make_result( status="completed", operation="remediate", scope={"cve_ids": ["CVE-2024-1"]}, stages_completed=["start"], pr={"url": "https://github.com/org/repo/pull/42"}, ) self.assertEqual(result["status"], "completed") # --- Preserved: other operations --- def test_backport_no_verdict_accepted(self) -> None: """Backport operation doesn't require validation_verdict.""" result = unattended.make_result( status="completed", operation="backport", scope={"cve_ids": ["CVE-2024-1"]}, stages_completed=["backport"], backport_results=[{ "target_branch": "rel-1", "status": "completed", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test_result": "PASS"}, }], target_branches=["rel-1"], ) self.assertEqual(result["status"], "completed") def test_jira_action_no_verdict_accepted(self) -> None: """jira_action doesn't require validation_verdict.""" result = unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[{"ticket": "EDM-1", "result": "success"}], expected_tickets=["EDM-1"], expected_jira_action={"action": "transition", "target_status": "MODIFIED"}, ) self.assertEqual(result["status"], "completed") # --- Preserved: non-completed status --- def test_partial_with_validate_stage_no_verdict_accepted(self) -> None: """partial status doesn't trigger the verdict guard.""" result = unattended.make_result( status="partial", operation="remediate", scope={"cve_ids": ["CVE-2024-1"]}, stages_completed=["start", "scan", "patch", "validate"], evidence={"scan_verdict": "present"}, ) self.assertEqual(result["status"], "partial") def test_failed_with_validate_stage_no_verdict_accepted(self) -> None: """failed status doesn't trigger the verdict guard.""" result = unattended.make_result( status="failed", operation="remediate", scope={"cve_ids": ["CVE-2024-1"]}, stages_completed=["start", "scan", "patch", "validate"], evidence={"validation_verdict": "FAIL"}, error="Validation failed after retries", ) self.assertEqual(result["status"], "failed") # --- CLI test --- def test_cli_missing_verdict_exit1(self) -> None: """CLI rejects completed remediate+validate with missing verdict.""" import io from unittest.mock import patch as _patch data = { "scope": {"cve_ids": ["CVE-2024-1"]}, "stages_completed": ["start", "scan", "patch", "validate", "pr"], "evidence": {"scan_verdict": "fixed"}, } with tempfile.TemporaryDirectory() as td: args = argparse.Namespace( status="completed", operation="remediate", data=json.dumps(data), artifact_dir=td, ) captured_err = io.StringIO() with _patch("sys.stderr", captured_err): exit_code = unattended._cmd_make_result(args) self.assertEqual(exit_code, 1) self.assertIn("validation_verdict", captured_err.getvalue()) def test_cli_valid_verdict_accepted(self) -> None: """CLI accepts completed remediate+validate with PASS verdict.""" import io from unittest.mock import patch as _patch data = { "scope": {"cve_ids": ["CVE-2024-1"]}, "stages_completed": ["start", "scan", "patch", "validate", "pr"], "evidence": {"validation_verdict": "PASS", "scan_verdict": "fixed"}, } with tempfile.TemporaryDirectory() as td: args = argparse.Namespace( status="completed", operation="remediate", data=json.dumps(data), artifact_dir=td, ) captured_out = io.StringIO() with _patch("sys.stdout", captured_out): exit_code = unattended._cmd_make_result(args) self.assertEqual(exit_code, 0) output = json.loads(captured_out.getvalue()) self.assertEqual(output["status"], "completed") # --------------------------------------------------------------------------- # B1-4472834692: early-warning / later-exact CVE conflict precedence # --------------------------------------------------------------------------- class TestScopeConflictPrecedence(unittest.TestCase): """Exact CVE conflict takes precedence over earlier package warning.""" def test_early_warning_later_exact_returns_exact(self) -> None: """Package-only warning in first dir must not mask later CVE match.""" with tempfile.TemporaryDirectory() as td: # First dir: package-only overlap (alphabetically first) dir_a = Path(td) / "AAA-100" dir_a.mkdir() (dir_a / "unattended-result.json").write_text(json.dumps({ "status": "partial", "scope": { "repo": "org/repo", "base_branch": "main", "cve_ids": ["CVE-2024-9999"], "packages": ["shared-pkg"], }, })) # Second dir: exact CVE overlap (alphabetically second) dir_b = Path(td) / "BBB-200" dir_b.mkdir() (dir_b / "unattended-result.json").write_text(json.dumps({ "status": "partial", "scope": { "repo": "org/repo", "base_branch": "main", "cve_ids": ["CVE-2024-1234"], "packages": ["other-pkg"], }, })) result = unattended.check_scope_conflict( "org/repo", "main", ["CVE-2024-1234"], ["shared-pkg"], td, ) self.assertIsNotNone(result) # Must be the exact CVE conflict, not the package warning self.assertNotIn("_overlap_type", result) self.assertEqual( result["scope"]["cve_ids"], ["CVE-2024-1234"], ) def test_only_warning_still_returned(self) -> None: """When only a package warning exists (no CVE match), return it.""" with tempfile.TemporaryDirectory() as td: dir_a = Path(td) / "AAA-100" dir_a.mkdir() (dir_a / "unattended-result.json").write_text(json.dumps({ "status": "partial", "scope": { "repo": "org/repo", "base_branch": "main", "cve_ids": ["CVE-2024-9999"], "packages": ["shared-pkg"], }, })) result = unattended.check_scope_conflict( "org/repo", "main", ["CVE-2024-5555"], ["shared-pkg"], td, ) self.assertIsNotNone(result) self.assertEqual( result.get("_overlap_type"), "same_package_warning", ) def test_no_overlap_returns_none(self) -> None: """No CVE or package overlap still returns None.""" with tempfile.TemporaryDirectory() as td: dir_a = Path(td) / "AAA-100" dir_a.mkdir() (dir_a / "unattended-result.json").write_text(json.dumps({ "status": "partial", "scope": { "repo": "org/repo", "base_branch": "main", "cve_ids": ["CVE-2024-9999"], "packages": ["other-pkg"], }, })) self.assertIsNone(unattended.check_scope_conflict( "org/repo", "main", ["CVE-2024-5555"], ["different-pkg"], td, )) # --------------------------------------------------------------------------- # B2-4472834711: CLI check-scope CSV normalization # --------------------------------------------------------------------------- class TestCheckScopeCLICSVNormalization(unittest.TestCase): """check-scope CLI strips whitespace and drops blank CSV segments.""" def _setup_conflict(self, td: str, cve: str, pkg: str) -> None: ctx_dir = Path(td) / "EDM-100" ctx_dir.mkdir(exist_ok=True) (ctx_dir / "unattended-result.json").write_text(json.dumps({ "status": "partial", "scope": { "repo": "org/repo", "base_branch": "main", "cve_ids": [cve], "packages": [pkg], }, })) def test_whitespace_around_cve_ids(self) -> None: """' CVE-2024-1 , CVE-2024-2 ' should detect conflict on CVE-2024-1.""" with tempfile.TemporaryDirectory() as td: self._setup_conflict(td, "CVE-2024-1", "pkg-a") args = argparse.Namespace( repo="org/repo", base_branch="main", cve_ids=" CVE-2024-1 , CVE-2024-2 ", packages="", artifact_base=td, exclude_context=None, ) self.assertEqual(unattended._cmd_check_scope(args), 1) def test_trailing_comma_cve_ids(self) -> None: """'CVE-2024-1,' should detect conflict (no blank token).""" with tempfile.TemporaryDirectory() as td: self._setup_conflict(td, "CVE-2024-1", "pkg-a") args = argparse.Namespace( repo="org/repo", base_branch="main", cve_ids="CVE-2024-1,", packages="", artifact_base=td, exclude_context=None, ) self.assertEqual(unattended._cmd_check_scope(args), 1) def test_repeated_commas_packages(self) -> None: """'pkg-a,,pkg-b' should detect pkg-a overlap (drop blank).""" with tempfile.TemporaryDirectory() as td: self._setup_conflict(td, "CVE-2024-9999", "pkg-a") args = argparse.Namespace( repo="org/repo", base_branch="main", cve_ids="", packages="pkg-a,,pkg-b", artifact_base=td, exclude_context=None, ) self.assertEqual(unattended._cmd_check_scope(args), 1) def test_whitespace_only_yields_empty(self) -> None: """' , , ' should yield no IDs, so no conflict.""" with tempfile.TemporaryDirectory() as td: self._setup_conflict(td, "CVE-2024-1", "pkg-a") args = argparse.Namespace( repo="org/repo", base_branch="main", cve_ids=" , , ", packages=" , , ", artifact_base=td, exclude_context=None, ) self.assertEqual(unattended._cmd_check_scope(args), 0) def test_omitted_args_preserved_as_empty(self) -> None: """Empty string args (omitted) produce empty lists.""" with tempfile.TemporaryDirectory() as td: self._setup_conflict(td, "CVE-2024-1", "pkg-a") args = argparse.Namespace( repo="org/repo", base_branch="main", cve_ids="", packages="", artifact_base=td, exclude_context=None, ) self.assertEqual(unattended._cmd_check_scope(args), 0) def test_whitespace_packages_conflict_detected(self) -> None: """' pkg-a ' (with whitespace) should still detect conflict.""" with tempfile.TemporaryDirectory() as td: self._setup_conflict(td, "CVE-2024-9999", "pkg-a") args = argparse.Namespace( repo="org/repo", base_branch="main", cve_ids="", packages=" pkg-a ", artifact_base=td, exclude_context=None, ) self.assertEqual(unattended._cmd_check_scope(args), 1) # --------------------------------------------------------------------------- # B2-4472847429: typing.Any import + get_type_hints resolution # --------------------------------------------------------------------------- class TestTypeHintsResolve(unittest.TestCase): """Verify that Any annotations resolve at runtime.""" def test_approval_bypass_resp_hints(self) -> None: """get_type_hints on _resp with Any annotation must not raise.""" hints = typing.get_type_hints( TestApprovalBypassRegression._resp, ) self.assertIn("return", hints) def test_approval_bypass_orig_hints(self) -> None: hints = typing.get_type_hints( TestApprovalBypassRegression._orig, ) self.assertIn("return", hints) def test_orig_request_shapes_resp_hints(self) -> None: hints = typing.get_type_hints( TestOriginalRequestMandatoryShapes._resp, ) self.assertIn("return", hints) # --------------------------------------------------------------------------- # Review 5802128551 — expected_tickets metadata for jira_action coverage # --------------------------------------------------------------------------- class TestExpectedTicketsValidation(unittest.TestCase): """expected_tickets: coverage guard for completed jira_action. expected_tickets is required metadata copied UNCHANGED from the current validated request's jira_action.tickets. For completed/lifecycle_complete jira_action, requires exactly one valid result per expected ticket. """ _VALID_JR = [ {"ticket": "EDM-1", "action": "transition", "target_status": "MODIFIED", "result": "success"}, ] _VALID_EJA = {"action": "transition", "target_status": "MODIFIED"} def test_valid_single_ticket(self) -> None: """Single expected ticket with matching success result passes.""" result = unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=self._VALID_JR, expected_tickets=["EDM-1"], expected_jira_action=self._VALID_EJA, ) self.assertEqual(result["expected_tickets"], ["EDM-1"]) self.assertEqual(result["status"], "completed") def test_valid_multiple_tickets(self) -> None: """Multiple expected tickets, each with matching result, passes.""" result = unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[ {"ticket": "EDM-1", "result": "success"}, {"ticket": "EDM-2", "result": "already_in_status"}, ], expected_tickets=["EDM-1", "EDM-2"], expected_jira_action=self._VALID_EJA, ) self.assertEqual(result["expected_tickets"], ["EDM-1", "EDM-2"]) def test_valid_reordered_results(self) -> None: """Result order doesn't need to match expected_tickets order.""" result = unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[ {"ticket": "EDM-2", "result": "success"}, {"ticket": "EDM-1", "result": "already_in_status"}, ], expected_tickets=["EDM-1", "EDM-2"], expected_jira_action=self._VALID_EJA, ) self.assertEqual(result["status"], "completed") def test_valid_mixed_success_already(self) -> None: """Mixed success + already_in_status results pass.""" result = unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[ {"ticket": "EDM-1", "result": "success"}, {"ticket": "EDM-2", "result": "already_in_status"}, {"ticket": "EDM-3", "result": "success"}, ], expected_tickets=["EDM-1", "EDM-2", "EDM-3"], expected_jira_action=self._VALID_EJA, ) self.assertEqual(result["status"], "completed") self.assertEqual(len(result["jira_results"]), 3) def test_empty_expected_rejected(self) -> None: """Empty expected_tickets with completed jira_action rejected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=self._VALID_JR, expected_tickets=[], ) self.assertIn("nonempty", str(ctx.exception)) def test_missing_ticket_in_results_rejected(self) -> None: """Expected ticket not covered by any result is rejected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[ {"ticket": "EDM-1", "result": "success"}, ], expected_tickets=["EDM-1", "EDM-2"], ) self.assertIn("expected ticket", str(ctx.exception)) self.assertIn("EDM-2", str(ctx.exception)) def test_extra_ticket_in_results_rejected(self) -> None: """Extra result ticket not in expected is rejected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[ {"ticket": "EDM-1", "result": "success"}, {"ticket": "EDM-2", "result": "success"}, ], expected_tickets=["EDM-1"], ) self.assertIn("expected ticket", str(ctx.exception)) def test_subset_results_rejected(self) -> None: """Subset of expected tickets covered is rejected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[ {"ticket": "EDM-1", "result": "success"}, ], expected_tickets=["EDM-1", "EDM-2", "EDM-3"], ) self.assertIn("expected ticket", str(ctx.exception)) def test_duplicate_expected_rejected(self) -> None: """Duplicate entries in expected_tickets rejected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[ {"ticket": "EDM-1", "result": "success"}, {"ticket": "EDM-1", "result": "success"}, ], expected_tickets=["EDM-1", "EDM-1"], ) self.assertIn("duplicate", str(ctx.exception).lower()) def test_malformed_child_non_dict_rejected(self) -> None: """Non-dict jira_results entry raises ValueError, not TypeError.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=["bogus"], expected_tickets=["EDM-1"], ) self.assertIn("dict", str(ctx.exception)) def test_malformed_child_missing_ticket_rejected(self) -> None: """Child without ticket field raises ValueError.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[{"result": "success"}], expected_tickets=["EDM-1"], ) self.assertIn("ticket", str(ctx.exception)) def test_malformed_child_nonstring_ticket_rejected(self) -> None: """Child with non-string ticket raises ValueError.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[{"ticket": 123, "result": "success"}], expected_tickets=["EDM-1"], ) self.assertIn("ticket", str(ctx.exception)) def test_malformed_child_nonstring_result_rejected(self) -> None: """Child with non-string result raises ValueError.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[{"ticket": "EDM-1", "result": 42}], expected_tickets=["EDM-1"], ) self.assertIn("string", str(ctx.exception)) def test_non_success_result_rejected(self) -> None: """Child with result='failed' rejected when expected_tickets set.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[{"ticket": "EDM-1", "result": "failed"}], expected_tickets=["EDM-1"], ) self.assertIn("failed", str(ctx.exception)) def test_completed_false_lifecycle_true_fires_guard(self) -> None: """lifecycle_complete=True triggers expected_tickets guard.""" result = unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[ {"ticket": "EDM-1", "result": "success"}, ], expected_tickets=["EDM-1"], expected_jira_action=self._VALID_EJA, lifecycle_complete=True, ) self.assertTrue(result["lifecycle_complete"]) def test_nonlist_expected_tickets_rejected(self) -> None: """Non-list expected_tickets raises ValueError.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=self._VALID_JR, expected_tickets="EDM-1", ) self.assertIn("list", str(ctx.exception)) def test_serialization_retains_field(self) -> None: """expected_tickets is carried through to serialized result.""" result = unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=self._VALID_JR, expected_tickets=["EDM-1"], expected_jira_action=self._VALID_EJA, ) self.assertIn("expected_tickets", result) self.assertEqual(result["expected_tickets"], ["EDM-1"]) def test_none_expected_tickets_rejected(self) -> None: """Missing/None expected_tickets on completed jira_action rejected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[{"ticket": "EDM-1", "result": "success"}], ) self.assertIn("expected_tickets", str(ctx.exception)) def test_explicit_none_expected_tickets_rejected(self) -> None: """Explicit None expected_tickets on completed jira_action rejected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[{"ticket": "EDM-1", "result": "success"}], expected_tickets=None, ) self.assertIn("expected_tickets", str(ctx.exception)) def test_none_expected_tickets_empty_results_rejected(self) -> None: """Missing expected_tickets with empty results still rejected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[], ) self.assertIn("expected_tickets", str(ctx.exception)) def test_partial_status_no_guard(self) -> None: """expected_tickets with partial status doesn't trigger guard.""" result = unattended.make_result( status="partial", operation="jira_action", scope={}, jira_results=[], expected_tickets=["EDM-1"], ) self.assertEqual(result["status"], "partial") self.assertEqual(result["expected_tickets"], ["EDM-1"]) def test_partial_no_lifecycle_no_expected_ok(self) -> None: """partial + lifecycle_complete=False omits expected_tickets OK.""" result = unattended.make_result( status="partial", operation="jira_action", scope={}, jira_results=[], ) self.assertEqual(result["status"], "partial") self.assertIsNone(result["expected_tickets"]) def test_needs_input_no_expected_ok(self) -> None: """Non-completed jira_action without expected_tickets is OK.""" result = unattended.make_result( status="needs_input", operation="jira_action", scope={}, ) self.assertEqual(result["status"], "needs_input") self.assertIsNone(result["expected_tickets"]) def test_non_jira_operation_no_expected_ok(self) -> None: """Non-jira_action operation doesn't need expected_tickets.""" result = unattended.make_result( status="completed", operation="remediate", scope={"cve_ids": ["CVE-2024-1"]}, ) self.assertEqual(result["status"], "completed") self.assertIsNone(result["expected_tickets"]) def test_duplicate_child_ticket_unique_expected_rejected(self) -> None: """Two child results claim same ticket against unique expected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[ {"ticket": "EDM-1", "result": "success"}, {"ticket": "EDM-1", "result": "success"}, ], expected_tickets=["EDM-1", "EDM-2"], ) self.assertIn("expected ticket", str(ctx.exception)) def test_malformed_expected_nonstring_entry_rejected(self) -> None: """Non-string entry in expected_tickets rejected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[{"ticket": "EDM-1", "result": "success"}], expected_tickets=[42], ) self.assertIn("nonempty string", str(ctx.exception)) def test_malformed_expected_mixed_types_rejected(self) -> None: """Mixed types in expected_tickets rejected before hash/sort.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[{"ticket": "EDM-1", "result": "success"}], expected_tickets=["EDM-1", None], ) self.assertIn("nonempty string", str(ctx.exception)) def test_later_bad_child_detected(self) -> None: """First child valid, second child has bad result — rejected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[ {"ticket": "EDM-1", "result": "success"}, {"ticket": "EDM-2", "result": "failed"}, ], expected_tickets=["EDM-1", "EDM-2"], ) self.assertIn("failed", str(ctx.exception)) self.assertIn("[1]", str(ctx.exception)) def test_both_completed_modes(self) -> None: """Both status=completed and lifecycle_complete=True with valid data.""" result = unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[ {"ticket": "EDM-1", "result": "success"}, ], expected_tickets=["EDM-1"], expected_jira_action={"action": "transition", "target_status": "MODIFIED"}, lifecycle_complete=True, ) self.assertTrue(result["lifecycle_complete"]) self.assertEqual(result["expected_tickets"], ["EDM-1"]) class TestExpectedTicketsCLI(unittest.TestCase): """CLI make-result roundtrip for expected_tickets.""" def test_cli_valid_expected_tickets(self) -> None: """make-result CLI carries expected_tickets from data to result.""" with tempfile.TemporaryDirectory() as td: args = argparse.Namespace( status="completed", operation="jira_action", artifact_dir=td, data=json.dumps({ "scope": {}, "jira_results": [ {"ticket": "EDM-1", "result": "success"}, {"ticket": "EDM-2", "result": "already_in_status"}, ], "expected_tickets": ["EDM-1", "EDM-2"], "expected_jira_action": { "action": "transition", "target_status": "MODIFIED", }, }), ) self.assertEqual(unattended._cmd_make_result(args), 0) written = json.loads( (Path(td) / "unattended-result.json").read_text() ) self.assertEqual( written["expected_tickets"], ["EDM-1", "EDM-2"], ) self.assertEqual(written["status"], "completed") def test_cli_rejected_subset_coverage(self) -> None: """make-result CLI rejects subset coverage with expected_tickets.""" import io from unittest.mock import patch as _patch with tempfile.TemporaryDirectory() as td: args = argparse.Namespace( status="completed", operation="jira_action", artifact_dir=td, data=json.dumps({ "scope": {}, "jira_results": [ {"ticket": "EDM-1", "result": "success"}, ], "expected_tickets": ["EDM-1", "EDM-2"], }), ) captured = io.StringIO() with _patch("sys.stderr", captured): self.assertEqual(unattended._cmd_make_result(args), 1) self.assertIn("expected ticket", captured.getvalue()) self.assertFalse( Path(td, "unattended-result.json").exists(), "Rejected make-result must not write artifact", ) def test_cli_malformed_child_exit1(self) -> None: """CLI returns exit 1 for malformed child, not traceback.""" import io from unittest.mock import patch as _patch with tempfile.TemporaryDirectory() as td: args = argparse.Namespace( status="completed", operation="jira_action", artifact_dir=td, data=json.dumps({ "scope": {}, "jira_results": ["not-a-dict"], "expected_tickets": ["EDM-1"], }), ) captured = io.StringIO() with _patch("sys.stderr", captured): rc = unattended._cmd_make_result(args) self.assertEqual(rc, 1) self.assertIn("dict", captured.getvalue()) def test_cli_missing_expected_tickets_exit1(self) -> None: """CLI rejects completed jira_action without expected_tickets.""" import io from unittest.mock import patch as _patch with tempfile.TemporaryDirectory() as td: args = argparse.Namespace( status="completed", operation="jira_action", artifact_dir=td, data=json.dumps({ "scope": {}, "jira_results": [ {"ticket": "EDM-1", "result": "success"}, ], }), ) captured = io.StringIO() with _patch("sys.stderr", captured): rc = unattended._cmd_make_result(args) self.assertEqual(rc, 1) self.assertIn("expected_tickets", captured.getvalue()) self.assertFalse( Path(td, "unattended-result.json").exists(), "Rejected make-result must not write artifact", ) def test_cli_none_expected_tickets_exit1(self) -> None: """CLI rejects completed jira_action with explicit null expected.""" import io from unittest.mock import patch as _patch with tempfile.TemporaryDirectory() as td: args = argparse.Namespace( status="completed", operation="jira_action", artifact_dir=td, data=json.dumps({ "scope": {}, "jira_results": [ {"ticket": "EDM-1", "result": "success"}, ], "expected_tickets": None, }), ) captured = io.StringIO() with _patch("sys.stderr", captured): rc = unattended._cmd_make_result(args) self.assertEqual(rc, 1) self.assertIn("expected_tickets", captured.getvalue()) # --------------------------------------------------------------------------- # Review 5803038694 — expected_jira_action metadata for action context # --------------------------------------------------------------------------- class TestExpectedJiraActionValidation(unittest.TestCase): """expected_jira_action: action context guard for completed jira_action. expected_jira_action is a projection copied from the current validated request's jira_action: {action, target_status (when transition)}. Required for completed/lifecycle_complete jira_action. Child action/target_status metadata is optional; when present and non-null, must match expected. """ _VALID_JR = [ {"ticket": "EDM-1", "action": "transition", "target_status": "MODIFIED", "result": "success"}, ] _VALID_EJA_TRANSITION = { "action": "transition", "target_status": "MODIFIED", } _VALID_EJA_COMMENT = { "action": "comment", "comment": "CVE fix merged.", } # --- Positive: valid expected context --- def test_valid_transition_with_child_metadata(self) -> None: """Child action/target_status matches expected transition.""" result = unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=self._VALID_JR, expected_tickets=["EDM-1"], expected_jira_action=self._VALID_EJA_TRANSITION, ) self.assertEqual(result["expected_jira_action"], self._VALID_EJA_TRANSITION) self.assertEqual(result["status"], "completed") def test_valid_comment_action(self) -> None: """Comment action with matching child comment passes.""" result = unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[ {"ticket": "EDM-1", "result": "success", "comment": "CVE fix merged."}, ], expected_tickets=["EDM-1"], expected_jira_action=self._VALID_EJA_COMMENT, ) self.assertEqual(result["expected_jira_action"]["action"], "comment") self.assertEqual( result["expected_jira_action"]["comment"], "CVE fix merged.", ) def test_comment_absent_child_comment_rejected(self) -> None: """Child without comment metadata rejected for comment action.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[{"ticket": "EDM-1", "result": "success"}], expected_tickets=["EDM-1"], expected_jira_action=self._VALID_EJA_COMMENT, ) self.assertIn("comment", str(ctx.exception)) self.assertIn("required", str(ctx.exception)) def test_comment_null_child_comment_rejected(self) -> None: """Explicit None child comment rejected for comment action.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[ {"ticket": "EDM-1", "comment": None, "result": "success"}, ], expected_tickets=["EDM-1"], expected_jira_action=self._VALID_EJA_COMMENT, ) self.assertIn("comment", str(ctx.exception)) self.assertIn("required", str(ctx.exception)) def test_comment_absent_child_comment_already_in_status_rejected( self, ) -> None: """already_in_status child without comment also rejected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[ {"ticket": "EDM-1", "result": "already_in_status"}, ], expected_tickets=["EDM-1"], expected_jira_action=self._VALID_EJA_COMMENT, ) self.assertIn("comment", str(ctx.exception)) self.assertIn("required", str(ctx.exception)) def test_comment_missing_child_lifecycle_rejected(self) -> None: """lifecycle_complete=True also requires child comment.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[ {"ticket": "EDM-1", "result": "success"}, ], expected_tickets=["EDM-1"], expected_jira_action=self._VALID_EJA_COMMENT, lifecycle_complete=True, ) self.assertIn("comment", str(ctx.exception)) self.assertIn("required", str(ctx.exception)) def test_comment_whitespace_only_child_comment_rejected(self) -> None: """Child with whitespace-only comment rejected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[ {"ticket": "EDM-1", "result": "success", "comment": " "}, ], expected_tickets=["EDM-1"], expected_jira_action=self._VALID_EJA_COMMENT, ) self.assertIn("nonempty", str(ctx.exception)) def test_absent_child_metadata_accepted(self) -> None: """Child without action/target_status is valid with correct context.""" result = unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[{"ticket": "EDM-1", "result": "success"}], expected_tickets=["EDM-1"], expected_jira_action=self._VALID_EJA_TRANSITION, ) self.assertEqual(result["status"], "completed") def test_null_child_action_accepted(self) -> None: """Explicit None child action is valid (not asserted).""" result = unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[ {"ticket": "EDM-1", "action": None, "result": "success"}, ], expected_tickets=["EDM-1"], expected_jira_action=self._VALID_EJA_TRANSITION, ) self.assertEqual(result["status"], "completed") def test_null_child_target_accepted(self) -> None: """Explicit None child target_status is valid (not asserted).""" result = unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[ {"ticket": "EDM-1", "target_status": None, "result": "success"}, ], expected_tickets=["EDM-1"], expected_jira_action=self._VALID_EJA_TRANSITION, ) self.assertEqual(result["status"], "completed") def test_already_in_status_with_matching_context(self) -> None: """already_in_status result with matching child metadata passes.""" result = unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[ {"ticket": "EDM-1", "action": "transition", "target_status": "MODIFIED", "result": "already_in_status"}, ], expected_tickets=["EDM-1"], expected_jira_action=self._VALID_EJA_TRANSITION, ) self.assertEqual(result["status"], "completed") def test_already_in_status_comment_with_matching_text(self) -> None: """already_in_status comment with matching child comment passes.""" result = unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[ {"ticket": "EDM-1", "action": "comment", "comment": "CVE fix merged.", "result": "already_in_status"}, ], expected_tickets=["EDM-1"], expected_jira_action=self._VALID_EJA_COMMENT, ) self.assertEqual(result["status"], "completed") def test_serialization_retains_expected_jira_action(self) -> None: """expected_jira_action carried through to serialized result.""" result = unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=self._VALID_JR, expected_tickets=["EDM-1"], expected_jira_action=self._VALID_EJA_TRANSITION, ) self.assertIn("expected_jira_action", result) serialized = json.loads(json.dumps(result)) self.assertEqual( serialized["expected_jira_action"], {"action": "transition", "target_status": "MODIFIED"}, ) def test_comment_multiline_preserved(self) -> None: """Multiline comment text preserved exactly.""" comment = "Line 1\nLine 2\nLine 3" eja = {"action": "comment", "comment": comment} result = unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[ {"ticket": "EDM-1", "result": "success", "comment": comment}, ], expected_tickets=["EDM-1"], expected_jira_action=eja, ) self.assertEqual(result["expected_jira_action"]["comment"], comment) def test_comment_leading_trailing_whitespace_preserved(self) -> None: """Leading/trailing whitespace in comment preserved exactly.""" comment = " leading and trailing " eja = {"action": "comment", "comment": comment} result = unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[ {"ticket": "EDM-1", "result": "success", "comment": comment}, ], expected_tickets=["EDM-1"], expected_jira_action=eja, ) self.assertEqual(result["expected_jira_action"]["comment"], comment) def test_comment_multi_ticket_exact_coverage(self) -> None: """Multi-ticket comment: each child must report matching comment.""" comment = "CVE fix merged." eja = {"action": "comment", "comment": comment} result = unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[ {"ticket": "EDM-1", "result": "success", "comment": comment}, {"ticket": "EDM-2", "result": "already_in_status", "comment": comment}, ], expected_tickets=["EDM-1", "EDM-2"], expected_jira_action=eja, ) self.assertEqual(result["status"], "completed") self.assertEqual(len(result["jira_results"]), 2) # --- Negative: missing/malformed expected context --- def test_missing_expected_jira_action_rejected(self) -> None: """Completed jira_action without expected_jira_action rejected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[{"ticket": "EDM-1", "result": "success"}], expected_tickets=["EDM-1"], ) self.assertIn("expected_jira_action", str(ctx.exception)) def test_none_expected_jira_action_rejected(self) -> None: """Explicit None expected_jira_action on completed rejected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[{"ticket": "EDM-1", "result": "success"}], expected_tickets=["EDM-1"], expected_jira_action=None, ) self.assertIn("expected_jira_action", str(ctx.exception)) def test_nondict_expected_jira_action_rejected(self) -> None: """Non-dict expected_jira_action raises ValueError, not TypeError.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[{"ticket": "EDM-1", "result": "success"}], expected_tickets=["EDM-1"], expected_jira_action="transition", ) self.assertIn("dict", str(ctx.exception)) def test_empty_action_rejected(self) -> None: """Empty string action in expected_jira_action rejected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[{"ticket": "EDM-1", "result": "success"}], expected_tickets=["EDM-1"], expected_jira_action={"action": ""}, ) self.assertIn("nonempty string", str(ctx.exception)) def test_unknown_action_rejected(self) -> None: """Unknown action type in expected_jira_action rejected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[{"ticket": "EDM-1", "result": "success"}], expected_tickets=["EDM-1"], expected_jira_action={"action": "reopen"}, ) self.assertIn("must be one of", str(ctx.exception)) def test_transition_missing_target_rejected(self) -> None: """Transition action without target_status rejected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[{"ticket": "EDM-1", "result": "success"}], expected_tickets=["EDM-1"], expected_jira_action={"action": "transition"}, ) self.assertIn("target_status", str(ctx.exception)) self.assertIn("nonblank", str(ctx.exception)) def test_transition_blank_target_rejected(self) -> None: """Transition action with blank target_status rejected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[{"ticket": "EDM-1", "result": "success"}], expected_tickets=["EDM-1"], expected_jira_action={ "action": "transition", "target_status": " ", }, ) self.assertIn("nonblank", str(ctx.exception)) def test_comment_with_target_status_rejected(self) -> None: """Comment action must not have target_status.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[{"ticket": "EDM-1", "result": "success"}], expected_tickets=["EDM-1"], expected_jira_action={ "action": "comment", "comment": "text", "target_status": "MODIFIED", }, ) self.assertIn("absent/None", str(ctx.exception)) def test_comment_missing_expected_comment_rejected(self) -> None: """Comment action without expected comment text rejected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[{"ticket": "EDM-1", "result": "success"}], expected_tickets=["EDM-1"], expected_jira_action={"action": "comment"}, ) self.assertIn("comment", str(ctx.exception)) self.assertIn("nonblank", str(ctx.exception)) def test_comment_none_expected_comment_rejected(self) -> None: """Comment action with None expected comment text rejected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[{"ticket": "EDM-1", "result": "success"}], expected_tickets=["EDM-1"], expected_jira_action={"action": "comment", "comment": None}, ) self.assertIn("comment", str(ctx.exception)) self.assertIn("nonblank", str(ctx.exception)) def test_comment_blank_expected_comment_rejected(self) -> None: """Comment action with blank expected comment text rejected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[{"ticket": "EDM-1", "result": "success"}], expected_tickets=["EDM-1"], expected_jira_action={"action": "comment", "comment": " "}, ) self.assertIn("nonblank", str(ctx.exception)) def test_comment_nonstring_expected_comment_rejected(self) -> None: """Comment action with non-string expected comment rejected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[{"ticket": "EDM-1", "result": "success"}], expected_tickets=["EDM-1"], expected_jira_action={"action": "comment", "comment": 42}, ) self.assertIn("nonblank", str(ctx.exception)) def test_transition_with_comment_rejected(self) -> None: """Transition action must not have comment text.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[{"ticket": "EDM-1", "result": "success"}], expected_tickets=["EDM-1"], expected_jira_action={ "action": "transition", "target_status": "MODIFIED", "comment": "should not be here", }, ) self.assertIn("absent/None", str(ctx.exception)) # --- Negative: child metadata contradictions --- def test_child_comment_contradicts_expected(self) -> None: """Child comment text different from expected → rejected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[ {"ticket": "EDM-1", "result": "success", "comment": "Wrong comment"}, ], expected_tickets=["EDM-1"], expected_jira_action=self._VALID_EJA_COMMENT, ) self.assertIn("does not match", str(ctx.exception)) # Error must NOT dump the full comment text self.assertNotIn("Wrong comment", str(ctx.exception)) def test_child_comment_empty_string_rejected(self) -> None: """Child with empty comment string rejected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[ {"ticket": "EDM-1", "result": "success", "comment": ""}, ], expected_tickets=["EDM-1"], expected_jira_action=self._VALID_EJA_COMMENT, ) self.assertIn("nonempty string", str(ctx.exception)) def test_child_comment_nonstring_rejected(self) -> None: """Child with non-string comment rejected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[ {"ticket": "EDM-1", "result": "success", "comment": 42}, ], expected_tickets=["EDM-1"], expected_jira_action=self._VALID_EJA_COMMENT, ) self.assertIn("nonempty string", str(ctx.exception)) def test_child_comment_on_transition_rejected(self) -> None: """Child claims comment but expected action is transition.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[ {"ticket": "EDM-1", "result": "success", "comment": "should not be here"}, ], expected_tickets=["EDM-1"], expected_jira_action=self._VALID_EJA_TRANSITION, ) self.assertIn("transition", str(ctx.exception)) def test_later_bad_child_comment_detected(self) -> None: """Wrong comment on second child still caught.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[ {"ticket": "EDM-1", "result": "success", "comment": "CVE fix merged."}, {"ticket": "EDM-2", "result": "success", "comment": "Different text"}, ], expected_tickets=["EDM-1", "EDM-2"], expected_jira_action=self._VALID_EJA_COMMENT, ) self.assertIn("[1]", str(ctx.exception)) def test_child_action_contradicts_expected(self) -> None: """Child claiming comment when expected is transition → rejected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[ {"ticket": "EDM-1", "action": "comment", "result": "success"}, ], expected_tickets=["EDM-1"], expected_jira_action=self._VALID_EJA_TRANSITION, ) self.assertIn("contradicts", str(ctx.exception)) self.assertIn("comment", str(ctx.exception)) def test_child_action_contradicts_comment(self) -> None: """Child claiming transition when expected is comment → rejected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[ {"ticket": "EDM-1", "action": "transition", "comment": "CVE fix merged.", "result": "success"}, ], expected_tickets=["EDM-1"], expected_jira_action=self._VALID_EJA_COMMENT, ) self.assertIn("contradicts", str(ctx.exception)) def test_child_target_contradicts_expected(self) -> None: """Child claiming CLOSED when expected MODIFIED → rejected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[ {"ticket": "EDM-1", "action": "transition", "target_status": "CLOSED", "result": "success"}, ], expected_tickets=["EDM-1"], expected_jira_action=self._VALID_EJA_TRANSITION, ) self.assertIn("contradicts", str(ctx.exception)) self.assertIn("CLOSED", str(ctx.exception)) def test_child_target_on_comment_action_rejected(self) -> None: """Child claims target_status but expected action is comment.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[ {"ticket": "EDM-1", "target_status": "MODIFIED", "comment": "CVE fix merged.", "result": "success"}, ], expected_tickets=["EDM-1"], expected_jira_action=self._VALID_EJA_COMMENT, ) self.assertIn("comment", str(ctx.exception)) def test_child_blank_action_rejected(self) -> None: """Non-null but blank child action raises ValueError.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[ {"ticket": "EDM-1", "action": " ", "result": "success"}, ], expected_tickets=["EDM-1"], expected_jira_action=self._VALID_EJA_TRANSITION, ) self.assertIn("nonempty string", str(ctx.exception)) def test_child_nonstring_action_rejected(self) -> None: """Non-string non-null child action raises ValueError.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[ {"ticket": "EDM-1", "action": 42, "result": "success"}, ], expected_tickets=["EDM-1"], expected_jira_action=self._VALID_EJA_TRANSITION, ) self.assertIn("nonempty string", str(ctx.exception)) def test_child_blank_target_rejected(self) -> None: """Non-null but blank child target_status raises ValueError.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[ {"ticket": "EDM-1", "target_status": "", "result": "success"}, ], expected_tickets=["EDM-1"], expected_jira_action=self._VALID_EJA_TRANSITION, ) self.assertIn("nonempty string", str(ctx.exception)) # --- Non-completed / non-jira_action: no guard --- def test_partial_no_guard(self) -> None: """Partial status doesn't trigger expected_jira_action guard.""" result = unattended.make_result( status="partial", operation="jira_action", scope={}, jira_results=[], ) self.assertIsNone(result["expected_jira_action"]) def test_non_jira_operation_none_preserved(self) -> None: """Non-jira_action operation preserves None.""" result = unattended.make_result( status="completed", operation="remediate", scope={"cve_ids": ["CVE-2024-1"]}, ) self.assertIsNone(result["expected_jira_action"]) def test_lifecycle_true_fires_guard(self) -> None: """lifecycle_complete=True triggers expected_jira_action guard.""" result = unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[{"ticket": "EDM-1", "result": "success"}], expected_tickets=["EDM-1"], expected_jira_action=self._VALID_EJA_TRANSITION, lifecycle_complete=True, ) self.assertEqual( result["expected_jira_action"], self._VALID_EJA_TRANSITION, ) class TestExpectedJiraActionCLI(unittest.TestCase): """CLI make-result roundtrip for expected_jira_action.""" def test_cli_valid_transition(self) -> None: """make-result CLI carries expected_jira_action from data.""" with tempfile.TemporaryDirectory() as td: args = argparse.Namespace( status="completed", operation="jira_action", artifact_dir=td, data=json.dumps({ "scope": {}, "jira_results": [ {"ticket": "EDM-1", "action": "transition", "target_status": "MODIFIED", "result": "success"}, ], "expected_tickets": ["EDM-1"], "expected_jira_action": { "action": "transition", "target_status": "MODIFIED", }, }), ) self.assertEqual(unattended._cmd_make_result(args), 0) written = json.loads( (Path(td) / "unattended-result.json").read_text() ) self.assertEqual( written["expected_jira_action"], {"action": "transition", "target_status": "MODIFIED"}, ) def test_cli_missing_expected_jira_action_exit1(self) -> None: """CLI rejects completed jira_action without expected_jira_action.""" import io from unittest.mock import patch as _patch with tempfile.TemporaryDirectory() as td: args = argparse.Namespace( status="completed", operation="jira_action", artifact_dir=td, data=json.dumps({ "scope": {}, "jira_results": [ {"ticket": "EDM-1", "result": "success"}, ], "expected_tickets": ["EDM-1"], }), ) captured = io.StringIO() with _patch("sys.stderr", captured): rc = unattended._cmd_make_result(args) self.assertEqual(rc, 1) self.assertIn("expected_jira_action", captured.getvalue()) self.assertFalse( Path(td, "unattended-result.json").exists(), "Rejected make-result must not write artifact", ) def test_cli_contradiction_exit1_no_artifact(self) -> None: """CLI rejects contradicting child action, no artifact written.""" import io from unittest.mock import patch as _patch with tempfile.TemporaryDirectory() as td: args = argparse.Namespace( status="completed", operation="jira_action", artifact_dir=td, data=json.dumps({ "scope": {}, "jira_results": [ {"ticket": "EDM-1", "action": "comment", "result": "success"}, ], "expected_tickets": ["EDM-1"], "expected_jira_action": { "action": "transition", "target_status": "MODIFIED", }, }), ) captured = io.StringIO() with _patch("sys.stderr", captured): rc = unattended._cmd_make_result(args) self.assertEqual(rc, 1) self.assertIn("contradicts", captured.getvalue()) self.assertFalse( Path(td, "unattended-result.json").exists(), "Rejected make-result must not write artifact", ) def test_cli_child_target_mismatch_no_action_exit1(self) -> None: """CLI rejects child target_status mismatch even without action.""" import io from unittest.mock import patch as _patch with tempfile.TemporaryDirectory() as td: args = argparse.Namespace( status="completed", operation="jira_action", artifact_dir=td, data=json.dumps({ "scope": {}, "jira_results": [ {"ticket": "EDM-1", "target_status": "CLOSED", "result": "success"}, ], "expected_tickets": ["EDM-1"], "expected_jira_action": { "action": "transition", "target_status": "MODIFIED", }, }), ) captured = io.StringIO() with _patch("sys.stderr", captured): rc = unattended._cmd_make_result(args) self.assertEqual(rc, 1) stderr_out = captured.getvalue() self.assertIn("contradicts", stderr_out) self.assertIn("CLOSED", stderr_out) self.assertFalse( Path(td, "unattended-result.json").exists(), "Rejected make-result must not write artifact", ) def test_cli_malformed_expected_jira_action_exit1(self) -> None: """CLI rejects non-dict expected_jira_action with reason.""" import io from unittest.mock import patch as _patch with tempfile.TemporaryDirectory() as td: args = argparse.Namespace( status="completed", operation="jira_action", artifact_dir=td, data=json.dumps({ "scope": {}, "jira_results": [ {"ticket": "EDM-1", "result": "success"}, ], "expected_tickets": ["EDM-1"], "expected_jira_action": "transition", }), ) captured = io.StringIO() with _patch("sys.stderr", captured): rc = unattended._cmd_make_result(args) self.assertEqual(rc, 1) stderr_out = captured.getvalue() self.assertIn("dict", stderr_out) self.assertFalse( Path(td, "unattended-result.json").exists(), "Rejected make-result must not write artifact", ) def test_cli_valid_comment_with_text(self) -> None: """CLI make-result carries comment text in expected_jira_action.""" with tempfile.TemporaryDirectory() as td: args = argparse.Namespace( status="completed", operation="jira_action", artifact_dir=td, data=json.dumps({ "scope": {}, "jira_results": [ {"ticket": "EDM-1", "result": "success", "comment": "CVE fix merged."}, ], "expected_tickets": ["EDM-1"], "expected_jira_action": { "action": "comment", "comment": "CVE fix merged.", }, }), ) self.assertEqual(unattended._cmd_make_result(args), 0) written = json.loads( (Path(td) / "unattended-result.json").read_text() ) self.assertEqual( written["expected_jira_action"], {"action": "comment", "comment": "CVE fix merged."}, ) def test_cli_missing_comment_text_exit1(self) -> None: """CLI rejects comment action without comment text.""" import io from unittest.mock import patch as _patch with tempfile.TemporaryDirectory() as td: args = argparse.Namespace( status="completed", operation="jira_action", artifact_dir=td, data=json.dumps({ "scope": {}, "jira_results": [ {"ticket": "EDM-1", "result": "success"}, ], "expected_tickets": ["EDM-1"], "expected_jira_action": {"action": "comment"}, }), ) captured = io.StringIO() with _patch("sys.stderr", captured): rc = unattended._cmd_make_result(args) self.assertEqual(rc, 1) self.assertIn("nonblank", captured.getvalue()) self.assertFalse( Path(td, "unattended-result.json").exists(), "Rejected make-result must not write artifact", ) def test_cli_child_comment_mismatch_exit1(self) -> None: """CLI rejects child comment that doesn't match expected.""" import io from unittest.mock import patch as _patch with tempfile.TemporaryDirectory() as td: args = argparse.Namespace( status="completed", operation="jira_action", artifact_dir=td, data=json.dumps({ "scope": {}, "jira_results": [ {"ticket": "EDM-1", "result": "success", "comment": "Wrong text"}, ], "expected_tickets": ["EDM-1"], "expected_jira_action": { "action": "comment", "comment": "Expected text", }, }), ) captured = io.StringIO() with _patch("sys.stderr", captured): rc = unattended._cmd_make_result(args) self.assertEqual(rc, 1) stderr_out = captured.getvalue() self.assertIn("does not match", stderr_out) # Must not dump comment text in error self.assertNotIn("Wrong text", stderr_out) self.assertNotIn("Expected text", stderr_out) self.assertFalse( Path(td, "unattended-result.json").exists(), "Rejected make-result must not write artifact", ) class TestExpectedJiraActionSubprocess(unittest.TestCase): """Subprocess CLI roundtrip: request → projection → make-result.""" _SCRIPT = str(Path(__file__).resolve().parent / "unattended.py") def test_subprocess_roundtrip_from_temp_cwd(self) -> None: """Full subprocess make-result from non-repo temp cwd.""" import subprocess data = json.dumps({ "scope": {}, "jira_results": [ {"ticket": "EDM-1", "action": "transition", "target_status": "MODIFIED", "result": "success"}, {"ticket": "EDM-2", "result": "already_in_status"}, ], "expected_tickets": ["EDM-1", "EDM-2"], "expected_jira_action": { "action": "transition", "target_status": "MODIFIED", }, }) with tempfile.TemporaryDirectory() as td: proc = subprocess.run( [ sys.executable, "-I", self._SCRIPT, "make-result", "--status", "completed", "--operation", "jira_action", "--artifact-dir", td, "--data", data, ], capture_output=True, text=True, cwd=td, timeout=30, ) self.assertEqual(proc.returncode, 0, proc.stderr) result_path = Path(td) / "unattended-result.json" self.assertTrue(result_path.exists(), "Artifact must be written") result = json.loads(result_path.read_text()) self.assertEqual(result["status"], "completed") self.assertEqual(result["operation"], "jira_action") self.assertEqual( result["expected_tickets"], ["EDM-1", "EDM-2"], ) self.assertEqual( result["expected_jira_action"], {"action": "transition", "target_status": "MODIFIED"}, ) self.assertEqual(len(result["jira_results"]), 2) self.assertIn("timestamp", result) def test_subprocess_comment_request_to_result_roundtrip(self) -> None: """Request → validate → projection → make-result subprocess.""" import subprocess # Start from an actual validated request request = { "operation": "jira_action", "jira_action": { "action": "comment", "tickets": ["EDM-1"], "comment": "CVE fix merged and verified.", }, } # Validate request first errors = unattended.validate_request(request) self.assertEqual(errors, [], f"Request must be valid: {errors}") # Derive projection FROM the validated request (not hard-coded) ja = request["jira_action"] expected_jira_action = {"action": ja["action"]} if ja["action"] == "comment": expected_jira_action["comment"] = ja["comment"] comment = ja["comment"] data = json.dumps({ "scope": {}, "jira_results": [ {"ticket": "EDM-1", "result": "success", "comment": comment}, ], "expected_tickets": ja["tickets"], "expected_jira_action": expected_jira_action, }) with tempfile.TemporaryDirectory() as td: proc = subprocess.run( [ sys.executable, "-I", self._SCRIPT, "make-result", "--status", "completed", "--operation", "jira_action", "--artifact-dir", td, "--data", data, ], capture_output=True, text=True, cwd=td, timeout=30, ) self.assertEqual(proc.returncode, 0, proc.stderr) result_path = Path(td) / "unattended-result.json" self.assertTrue(result_path.exists(), "Artifact must be written") result = json.loads(result_path.read_text()) self.assertEqual(result["status"], "completed") self.assertEqual( result["expected_jira_action"], expected_jira_action, ) def test_subprocess_comment_missing_child_rejection(self) -> None: """Subprocess rejects missing child comment with stderr/no artifact.""" import subprocess # Valid request → valid projection, but child has no comment request = { "operation": "jira_action", "jira_action": { "action": "comment", "tickets": ["EDM-1"], "comment": "Test comment.", }, } errors = unattended.validate_request(request) self.assertEqual(errors, []) ja = request["jira_action"] data = json.dumps({ "scope": {}, "jira_results": [ {"ticket": "EDM-1", "result": "success"}, ], "expected_tickets": ja["tickets"], "expected_jira_action": { "action": "comment", "comment": ja["comment"], }, }) with tempfile.TemporaryDirectory() as td: proc = subprocess.run( [ sys.executable, "-I", self._SCRIPT, "make-result", "--status", "completed", "--operation", "jira_action", "--artifact-dir", td, "--data", data, ], capture_output=True, text=True, cwd=td, timeout=30, ) self.assertEqual(proc.returncode, 1) self.assertIn("required", proc.stderr) self.assertFalse( Path(td, "unattended-result.json").exists(), "Rejected make-result must not write artifact", ) # No stdout result output on rejection self.assertEqual(proc.stdout.strip(), "") class TestDuplicateJiraActionTicketsEarly(unittest.TestCase): """validate_request rejects duplicate jira_action.tickets early.""" def test_duplicate_tickets_rejected(self) -> None: req = { "operation": "jira_action", "jira_action": { "action": "transition", "tickets": ["EDM-1", "EDM-1"], "target_status": "MODIFIED", }, } errors = unattended.validate_request(req) self.assertTrue( any("duplicate" in e for e in errors), f"Expected duplicate error, got: {errors}", ) def test_unique_tickets_passes(self) -> None: req = { "operation": "jira_action", "jira_action": { "action": "transition", "tickets": ["EDM-1", "EDM-2"], "target_status": "MODIFIED", }, } self.assertEqual(unattended.validate_request(req), []) def test_duplicate_check_after_format_check(self) -> None: """Invalid key format short-circuits before duplicate check.""" req = { "operation": "jira_action", "jira_action": { "action": "transition", "tickets": ["bad-key", "bad-key"], "target_status": "MODIFIED", }, } errors = unattended.validate_request(req) # Should get invalid key error, not necessarily duplicate self.assertTrue(any("invalid key" in e for e in errors)) def test_duplicate_cli_rejected(self) -> None: """CLI validate rejects duplicate jira_action.tickets.""" import io from unittest.mock import patch as _patch with tempfile.TemporaryDirectory() as td: req_path = os.path.join(td, "req.json") with open(req_path, "w") as f: json.dump({ "operation": "jira_action", "jira_action": { "action": "comment", "tickets": ["EDM-1", "EDM-1"], "comment": "test", }, }, f) args = argparse.Namespace(request_file=req_path) stdout_cap = io.StringIO() stderr_cap = io.StringIO() with _patch("sys.stdout", stdout_cap), _patch("sys.stderr", stderr_cap): rc = unattended._cmd_validate(args) self.assertEqual(rc, 1) out_json = json.loads(stdout_cap.getvalue()) self.assertFalse(out_json["valid"]) self.assertTrue( any("duplicate" in e for e in out_json["errors"]), ) class TestExpectedTicketsDocumentation(unittest.TestCase): """Demonstrate request-to-result handoff for expected_tickets. expected_tickets is copied from the CURRENT request's jira_action.tickets, NOT from top-level jira_tickets. This test proves the fixture behaviour: when top-level jira_tickets differs from jira_action.tickets, the action tickets are what matter for expected_tickets. No real Jira writes occur — these are pure validation tests. """ def test_action_tickets_wins_over_toplevel(self) -> None: """jira_action.tickets is the source, not top-level jira_tickets.""" # Request has different top-level and action tickets req = { "operation": "jira_action", "jira_tickets": ["OLD-999"], # top-level, irrelevant "jira_action": { "action": "transition", "tickets": ["EDM-1", "EDM-2"], "target_status": "MODIFIED", }, } req_errors = unattended.validate_request(req) self.assertEqual(req_errors, []) # jira_tickets on jira_action operation is not rejected # (it's a valid field, just not used for expected_tickets) # The action tickets are what matter action_tickets = req["jira_action"]["tickets"] # Simulate: copy action tickets and action projection for result action_projection = { "action": req["jira_action"]["action"], "target_status": req["jira_action"]["target_status"], } result = unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[ {"ticket": "EDM-1", "result": "success"}, {"ticket": "EDM-2", "result": "already_in_status"}, ], expected_tickets=action_tickets, expected_jira_action=action_projection, ) self.assertEqual(result["expected_tickets"], ["EDM-1", "EDM-2"]) # top-level jira_tickets is NOT reflected in expected_tickets self.assertNotEqual(result["expected_tickets"], ["OLD-999"]) def test_runnable_json_example(self) -> None: """Full runnable JSON example: request -> validate -> result. Demonstrates the complete handoff without any real Jira writes. Request JSON (abbreviated): { "operation": "jira_action", "jira_action": { "action": "transition", "tickets": ["EDM-100", "EDM-200"], "target_status": "MODIFIED" } } The skill: 1. Validates the request 2. Extracts expected_tickets = request["jira_action"]["tickets"] 3. Extracts expected_jira_action projection from the request 4. Executes Jira transitions (not shown — no real writes) 5. Passes expected_tickets and expected_jira_action to make_result """ # Step 1: validate request req = { "operation": "jira_action", "jira_action": { "action": "transition", "tickets": ["EDM-100", "EDM-200"], "target_status": "MODIFIED", }, } errors = unattended.validate_request(req) self.assertEqual(errors, []) # Step 2: extract expected_tickets from CURRENT request expected = req["jira_action"]["tickets"] # Step 3: extract expected_jira_action projection eja = {"action": req["jira_action"]["action"], "target_status": req["jira_action"]["target_status"]} # Step 4: (Jira writes happen here — skipped in tests) # Step 5: build result with coverage and action context check result = unattended.make_result( status="completed", operation="jira_action", scope={}, jira_results=[ {"ticket": "EDM-100", "action": "transition", "target_status": "MODIFIED", "result": "success"}, {"ticket": "EDM-200", "action": "transition", "target_status": "MODIFIED", "result": "already_in_status"}, ], expected_tickets=expected, expected_jira_action=eja, ) self.assertEqual(result["status"], "completed") self.assertEqual(result["expected_tickets"], ["EDM-100", "EDM-200"]) self.assertEqual(result["expected_jira_action"], eja) # Serialized result contains the fields self.assertIn("expected_tickets", result) self.assertIn("expected_jira_action", result) serialized = json.dumps(result) parsed = json.loads(serialized) self.assertEqual( parsed["expected_tickets"], ["EDM-100", "EDM-200"], ) self.assertEqual( parsed["expected_jira_action"], {"action": "transition", "target_status": "MODIFIED"}, ) # --------------------------------------------------------------------------- # Branch syntax validation (finding A: reject invalid Git branch names) # --------------------------------------------------------------------------- class TestValidateBranchName(unittest.TestCase): """validate_branch_name: pure-Python Git ref format parity.""" # --- Valid names (Git allows these) --- def test_simple_name(self) -> None: ok, reason = unattended.validate_branch_name("main") self.assertTrue(ok, reason) def test_hierarchical(self) -> None: ok, reason = unattended.validate_branch_name("feature/foo") self.assertTrue(ok, reason) def test_release_branch(self) -> None: ok, reason = unattended.validate_branch_name("release-2.16") self.assertTrue(ok, reason) def test_semicolon_in_name(self) -> None: """release;id is a valid Git branch name.""" ok, reason = unattended.validate_branch_name("release;id") self.assertTrue(ok, reason) def test_dollar_paren(self) -> None: """$(...) is valid Git ref syntax; shell metachar, not evaluated.""" ok, reason = unattended.validate_branch_name("fix/$(echo)") self.assertTrue(ok, reason) def test_ampersand(self) -> None: ok, reason = unattended.validate_branch_name("a&b") self.assertTrue(ok, reason) def test_plus_sign(self) -> None: ok, reason = unattended.validate_branch_name("my+branch") self.assertTrue(ok, reason) def test_at_in_name(self) -> None: """@ as part of a name is fine (only bare '@' is forbidden).""" ok, reason = unattended.validate_branch_name("user@host") self.assertTrue(ok, reason) def test_exclamation(self) -> None: ok, reason = unattended.validate_branch_name("fix!urgent") self.assertTrue(ok, reason) def test_hash(self) -> None: ok, reason = unattended.validate_branch_name("issue#42") self.assertTrue(ok, reason) def test_percent(self) -> None: ok, reason = unattended.validate_branch_name("100%done") self.assertTrue(ok, reason) def test_equals(self) -> None: ok, reason = unattended.validate_branch_name("a=b") self.assertTrue(ok, reason) # --- Invalid names (Git rejects these) --- def test_option_like_dash(self) -> None: ok, reason = unattended.validate_branch_name("-x") self.assertFalse(ok) self.assertIn("-", reason) def test_double_dot(self) -> None: ok, reason = unattended.validate_branch_name("foo..bar") self.assertFalse(ok) self.assertIn("..", reason) def test_at_brace_pseudo_ref(self) -> None: ok, reason = unattended.validate_branch_name("@{-1}") self.assertFalse(ok) self.assertIn("@{", reason) def test_at_upstream(self) -> None: ok, reason = unattended.validate_branch_name("@{upstream}") self.assertFalse(ok) self.assertIn("@{", reason) def test_bare_at_valid(self) -> None: """Bare '@' is a valid Git branch name (git check-ref-format --branch accepts it).""" ok, reason = unattended.validate_branch_name("@") self.assertTrue(ok, reason) def test_head_rejected(self) -> None: """HEAD is rejected by git check-ref-format --branch.""" ok, reason = unattended.validate_branch_name("HEAD") self.assertFalse(ok) self.assertIn("HEAD", reason) def test_head_as_component_valid(self) -> None: """HEAD as part of a path is a valid branch name.""" ok, reason = unattended.validate_branch_name("fix/HEAD/stuff") self.assertTrue(ok, reason) def test_space_in_name(self) -> None: ok, reason = unattended.validate_branch_name("foo bar") self.assertFalse(ok) self.assertIn("forbidden", reason) def test_tab_in_name(self) -> None: ok, reason = unattended.validate_branch_name("foo\tbar") self.assertFalse(ok) self.assertIn("control", reason) def test_newline_in_name(self) -> None: ok, reason = unattended.validate_branch_name("foo\nbar") self.assertFalse(ok) self.assertIn("control", reason) def test_tilde(self) -> None: ok, reason = unattended.validate_branch_name("foo~1") self.assertFalse(ok) self.assertIn("~", reason) def test_caret(self) -> None: ok, reason = unattended.validate_branch_name("foo^1") self.assertFalse(ok) self.assertIn("^", reason) def test_colon(self) -> None: ok, reason = unattended.validate_branch_name("foo:bar") self.assertFalse(ok) self.assertIn(":", reason) def test_backslash(self) -> None: ok, reason = unattended.validate_branch_name("foo\\bar") self.assertFalse(ok) self.assertIn("\\", reason) def test_question_mark(self) -> None: ok, reason = unattended.validate_branch_name("foo?bar") self.assertFalse(ok) self.assertIn("?", reason) def test_asterisk(self) -> None: ok, reason = unattended.validate_branch_name("foo*bar") self.assertFalse(ok) self.assertIn("*", reason) def test_bracket(self) -> None: ok, reason = unattended.validate_branch_name("foo[bar") self.assertFalse(ok) self.assertIn("[", reason) def test_dot_start(self) -> None: ok, reason = unattended.validate_branch_name(".hidden") self.assertFalse(ok) self.assertIn(".", reason) def test_dot_end(self) -> None: ok, reason = unattended.validate_branch_name("trailing.") self.assertFalse(ok) self.assertIn(".", reason) def test_slash_end(self) -> None: ok, reason = unattended.validate_branch_name("trailing/") self.assertFalse(ok) self.assertIn("/", reason) def test_double_slash(self) -> None: ok, reason = unattended.validate_branch_name("a//b") self.assertFalse(ok) self.assertIn("//", reason) def test_dot_lock_component(self) -> None: ok, reason = unattended.validate_branch_name("refs.lock") self.assertFalse(ok) self.assertIn(".lock", reason) def test_component_dot_start(self) -> None: ok, reason = unattended.validate_branch_name("feature/.hidden") self.assertFalse(ok) self.assertIn(".", reason) def test_empty_string(self) -> None: ok, reason = unattended.validate_branch_name("") self.assertFalse(ok) def test_non_string(self) -> None: ok, reason = unattended.validate_branch_name(42) # type: ignore[arg-type] self.assertFalse(ok) # --- Git parity: verify against actual git check-ref-format --- def test_git_parity_valid_names(self) -> None: """Parity test: valid names accepted by both Python and Git.""" import subprocess valid_names = [ "main", "release-2.16", "feature/foo", "release;id", "fix/$(echo)", "a&b", "my+branch", "user@host", "fix!urgent", "issue#42", "v1.0.0", "a-b_c", ] for name in valid_names: ok, reason = unattended.validate_branch_name(name) self.assertTrue(ok, f"Python rejected valid '{name}': {reason}") try: result = subprocess.run( ["git", "check-ref-format", f"refs/heads/{name}"], capture_output=True, timeout=5, ) self.assertEqual( result.returncode, 0, f"Git rejected valid '{name}' " f"(stderr: {result.stderr.decode()})", ) except FileNotFoundError: self.skipTest("git not available for parity check") def test_git_parity_head_branch_flag(self) -> None: """HEAD is rejected by git check-ref-format --branch. Unlike refs/heads/HEAD (which is a valid refname), the --branch flag specifically rejects 'HEAD' because it is a special ref. Our validator matches this behavior. """ import subprocess ok, _ = unattended.validate_branch_name("HEAD") self.assertFalse(ok, "Python should reject HEAD") try: result = subprocess.run( ["git", "check-ref-format", "--branch", "HEAD"], capture_output=True, timeout=5, ) self.assertNotEqual( result.returncode, 0, "git check-ref-format --branch should reject HEAD", ) except FileNotFoundError: self.skipTest("git not available for parity check") def test_git_parity_at_qualified_refspec(self) -> None: """Full-refspec fetch disambiguates '@' from HEAD after advance. Three commits: A (main), B (initial @), C (advanced @ after clone). After clone (origin/@ = B), remote @ advances to C. A bare 'git fetch origin @' fetches HEAD (= A), not @. The documented qualified refspec fetch refreshes origin/@ to C, and checkout resolves to the actual remote @ (C), not main (A) or stale clone (B). Fixture: --initial-branch=main, GIT_CONFIG_COUNT identity, update-ref + full refspec push for literal refs/heads/@. """ import subprocess ok, reason = unattended.validate_branch_name("@") self.assertTrue(ok, f"Python should accept bare '@': {reason}") try: r_ref = subprocess.run( ["git", "check-ref-format", "--branch", "@"], capture_output=True, text=True, timeout=5, ) self.assertEqual(r_ref.returncode, 0) with tempfile.TemporaryDirectory() as td: remote = os.path.join(td, "remote.git") setup = os.path.join(td, "setup") work = os.path.join(td, "work") env = { **os.environ, "GIT_CONFIG_COUNT": "2", "GIT_CONFIG_KEY_0": "user.email", "GIT_CONFIG_VALUE_0": "t@t", "GIT_CONFIG_KEY_1": "user.name", "GIT_CONFIG_VALUE_1": "t", } subprocess.run( ["git", "init", "--bare", "--initial-branch=main", remote], check=True, capture_output=True, ) subprocess.run( ["git", "clone", remote, setup], check=True, capture_output=True, ) # Commit A on main subprocess.run( ["git", "-C", setup, "commit", "--allow-empty", "-m", "A-main"], check=True, capture_output=True, env=env, ) subprocess.run( ["git", "-C", setup, "push", "origin", "main"], check=True, capture_output=True, env=env, ) sha_a = subprocess.run( ["git", "-C", setup, "rev-parse", "HEAD"], capture_output=True, text=True, ).stdout.strip() # Commit B for initial @ branch subprocess.run( ["git", "-C", setup, "checkout", "-b", "tmp"], check=True, capture_output=True, ) subprocess.run( ["git", "-C", setup, "commit", "--allow-empty", "-m", "B-at-initial"], check=True, capture_output=True, env=env, ) sha_b = subprocess.run( ["git", "-C", setup, "rev-parse", "HEAD"], capture_output=True, text=True, ).stdout.strip() subprocess.run( ["git", "-C", setup, "update-ref", "refs/heads/@", sha_b], check=True, capture_output=True, ) subprocess.run( ["git", "-C", setup, "push", "origin", "refs/heads/@:refs/heads/@"], check=True, capture_output=True, env=env, ) # Clone: origin/@ = B subprocess.run( ["git", "clone", remote, work], check=True, capture_output=True, env=env, ) clone_at = subprocess.run( ["git", "-C", work, "rev-parse", "origin/@"], capture_output=True, text=True, ).stdout.strip() self.assertEqual(clone_at, sha_b) # Commit C: advance remote @ AFTER clone subprocess.run( ["git", "-C", setup, "commit", "--allow-empty", "-m", "C-at-advanced"], check=True, capture_output=True, env=env, ) sha_c = subprocess.run( ["git", "-C", setup, "rev-parse", "HEAD"], capture_output=True, text=True, ).stdout.strip() subprocess.run( ["git", "-C", setup, "update-ref", "refs/heads/@", sha_c], check=True, capture_output=True, ) subprocess.run( ["git", "-C", setup, "push", "origin", "refs/heads/@:refs/heads/@"], check=True, capture_output=True, env=env, ) # Verify: remote @ = C, remote main = A r_refs = subprocess.run( ["git", "-C", remote, "show-ref"], capture_output=True, text=True, ) ref_map = dict( (r, s) for s, r in (ln.split() for ln in r_refs.stdout.strip().splitlines()) ) self.assertEqual(ref_map["refs/heads/main"], sha_a) self.assertEqual(ref_map["refs/heads/@"], sha_c) # All three SHAs distinct self.assertEqual( len({sha_a, sha_b, sha_c}), 3, "A, B, C must be distinct commits", ) # Contrast: bare fetch gets HEAD (main=A), not @ r_bare = subprocess.run( ["git", "-C", work, "fetch", "origin", "@"], capture_output=True, text=True, timeout=5, ) self.assertEqual(r_bare.returncode, 0) bare_fh = subprocess.run( ["git", "-C", work, "rev-parse", "FETCH_HEAD"], capture_output=True, text=True, ).stdout.strip() self.assertEqual( bare_fh, sha_a, "bare fetch @ gets HEAD (main=A), not @", ) # origin/@ still stale B after bare fetch stale = subprocess.run( ["git", "-C", work, "rev-parse", "origin/@"], capture_output=True, text=True, ).stdout.strip() self.assertEqual( stale, sha_b, "bare fetch does not refresh origin/@", ) # Documented qualified-refspec fetch + checkout target_branch = "@" refspec = ( f"refs/heads/{target_branch}" f":refs/remotes/origin/{target_branch}" ) r_fetch = subprocess.run( ["git", "-C", work, "fetch", "origin", refspec], capture_output=True, text=True, timeout=5, ) self.assertEqual( r_fetch.returncode, 0, f"qualified fetch failed: {r_fetch.stderr}", ) # origin/@ now updated to C refreshed = subprocess.run( ["git", "-C", work, "rev-parse", "origin/@"], capture_output=True, text=True, ).stdout.strip() self.assertEqual( refreshed, sha_c, "qualified fetch must refresh origin/@ to C", ) # Checkout from refreshed tracking ref r_co = subprocess.run( ["git", "-C", work, "checkout", "-b", "cve-fix/test-@", f"refs/remotes/origin/{target_branch}"], capture_output=True, text=True, timeout=5, ) self.assertEqual( r_co.returncode, 0, f"checkout failed: {r_co.stderr}", ) head = subprocess.run( ["git", "-C", work, "rev-parse", "HEAD"], capture_output=True, text=True, ).stdout.strip() self.assertEqual( head, sha_c, "HEAD must be C (advanced @), " "not A (main) or B (stale clone)", ) except FileNotFoundError: self.skipTest("git not available for parity check") def test_git_parity_invalid_names(self) -> None: """Parity test: invalid names rejected by both Python and Git. Note: '-x' is omitted because git check-ref-format accepts refs/heads/-x as a valid refname (the '-' rule applies to the start-of-ref, not path components). Our validator rejects it for practical safety: '-x' is parsed as an option by git CLI commands. This stricter rejection is tested separately in test_option_like_dash. """ import subprocess invalid_names = [ "foo..bar", "foo bar", ".hidden", "trailing.", "trailing/", "a//b", "refs.lock", "feature/.hidden", "foo\tbar", ] for name in invalid_names: ok, _ = unattended.validate_branch_name(name) self.assertFalse(ok, f"Python accepted invalid '{name}'") try: result = subprocess.run( ["git", "check-ref-format", f"refs/heads/{name}"], capture_output=True, timeout=5, ) self.assertNotEqual( result.returncode, 0, f"Git accepted invalid '{name}'", ) except FileNotFoundError: self.skipTest("git not available for parity check") class TestValidateRequestBranchSyntax(unittest.TestCase): """validate_request rejects invalid Git branch syntax in requests.""" def test_valid_branch_accepted(self) -> None: req = { "operation": "remediate", "cve_ids": ["CVE-2024-1234"], "branch": "cve-fix/EDM-1234", } self.assertEqual(unattended.validate_request(req), []) def test_valid_metachar_branch_accepted(self) -> None: """Shell metacharacter names valid in Git are accepted.""" req = { "operation": "remediate", "cve_ids": ["CVE-2024-1234"], "branch": "release;id", } self.assertEqual(unattended.validate_request(req), []) def test_option_like_branch_rejected(self) -> None: req = { "operation": "remediate", "cve_ids": ["CVE-2024-1234"], "branch": "-x", } errors = unattended.validate_request(req) self.assertTrue( any("invalid Git branch name" in e for e in errors), f"Expected branch rejection, got: {errors}", ) def test_double_dot_branch_rejected(self) -> None: req = { "operation": "remediate", "cve_ids": ["CVE-2024-1234"], "branch": "foo..bar", } errors = unattended.validate_request(req) self.assertTrue( any("invalid Git branch name" in e for e in errors), f"Expected branch rejection, got: {errors}", ) def test_pseudo_ref_branch_rejected(self) -> None: req = { "operation": "remediate", "cve_ids": ["CVE-2024-1234"], "branch": "@{-1}", } errors = unattended.validate_request(req) self.assertTrue( any("invalid Git branch name" in e for e in errors), f"Expected branch rejection, got: {errors}", ) def test_head_branch_rejected(self) -> None: """HEAD as branch name is rejected (git check-ref-format --branch).""" req = { "operation": "remediate", "cve_ids": ["CVE-2024-1234"], "branch": "HEAD", } errors = unattended.validate_request(req) self.assertTrue( any("invalid Git branch name" in e for e in errors), f"Expected HEAD rejection, got: {errors}", ) def test_head_base_branch_rejected(self) -> None: """HEAD as base_branch is rejected.""" req = { "operation": "remediate", "cve_ids": ["CVE-2024-1234"], "base_branch": "HEAD", } errors = unattended.validate_request(req) self.assertTrue( any("invalid Git branch name" in e for e in errors), f"Expected HEAD rejection, got: {errors}", ) def test_valid_base_branch_accepted(self) -> None: req = { "operation": "remediate", "cve_ids": ["CVE-2024-1234"], "base_branch": "release-2.16", } self.assertEqual(unattended.validate_request(req), []) def test_invalid_base_branch_rejected(self) -> None: req = { "operation": "remediate", "cve_ids": ["CVE-2024-1234"], "base_branch": "foo..bar", } errors = unattended.validate_request(req) self.assertTrue( any("invalid Git branch name" in e for e in errors), f"Expected base_branch rejection, got: {errors}", ) def test_valid_target_branches_accepted(self) -> None: req = { "operation": "backport", "work_ref": "cve-fix/EDM-1234", "target_branches": ["release-2.16", "release;id"], } self.assertEqual(unattended.validate_request(req), []) def test_invalid_target_branch_rejected(self) -> None: req = { "operation": "backport", "work_ref": "cve-fix/EDM-1234", "target_branches": ["release-2.16", "-x"], } errors = unattended.validate_request(req) self.assertTrue( any("invalid Git branch name" in e for e in errors), f"Expected target_branches rejection, got: {errors}", ) def test_space_in_target_branch_rejected(self) -> None: req = { "operation": "backport", "work_ref": "cve-fix/EDM-1234", "target_branches": ["main branch"], } errors = unattended.validate_request(req) self.assertTrue( any("invalid Git branch name" in e for e in errors), f"Expected target_branches rejection, got: {errors}", ) class TestValidateRequestBranchSyntaxCLI(unittest.TestCase): """CLI validates branch syntax and returns reason on stderr.""" def test_cli_invalid_branch_rejected(self) -> None: """CLI rejects request with invalid branch, stderr has reason.""" import io from unittest.mock import patch as _patch req = { "operation": "remediate", "cve_ids": ["CVE-2024-1234"], "branch": "@{-1}", } with tempfile.TemporaryDirectory() as td: path = os.path.join(td, "req.json") with open(path, "w") as f: json.dump(req, f) args = argparse.Namespace(request_file=path) captured_err = io.StringIO() captured_out = io.StringIO() with _patch("sys.stderr", captured_err), \ _patch("sys.stdout", captured_out): exit_code = unattended._cmd_validate(args) self.assertEqual(exit_code, 1) self.assertIn("invalid Git branch name", captured_err.getvalue()) output = json.loads(captured_out.getvalue()) self.assertFalse(output["valid"]) def test_cli_valid_metachar_branch_accepted(self) -> None: """CLI accepts valid shell-metachar branch name.""" import io from unittest.mock import patch as _patch req = { "operation": "remediate", "cve_ids": ["CVE-2024-1234"], "branch": "release;id", } with tempfile.TemporaryDirectory() as td: path = os.path.join(td, "req.json") with open(path, "w") as f: json.dump(req, f) args = argparse.Namespace(request_file=path) captured_out = io.StringIO() with _patch("sys.stdout", captured_out): exit_code = unattended._cmd_validate(args) self.assertEqual(exit_code, 0) output = json.loads(captured_out.getvalue()) self.assertTrue(output["valid"]) def test_cli_backport_argv_proof(self) -> None: """Demonstrate literal-argv execution for backport commands. This is not a subprocess test of actual git operations — it verifies that the argv list is constructed correctly for shell=False execution with caller-supplied branch values as literal arguments, never interpolated into a shell string. """ target_branch = "release;id" context = "EDM-1234" merge_commit = "abc123def456" # These are the exact argv lists the skill must construct fetch_argv = ["git", "fetch", "origin", target_branch] checkout_argv = [ "git", "checkout", "-b", f"cve-fix/{context}-{target_branch}", f"origin/{target_branch}", ] cherrypick_argv = ["git", "cherry-pick", "-x", merge_commit] # Verify structure: each element is a separate argv entry self.assertIsInstance(fetch_argv, list) self.assertEqual(fetch_argv[3], "release;id") # literal, not split self.assertIn(";", checkout_argv[3]) # semicolon preserved self.assertEqual(cherrypick_argv[3], merge_commit) # Verify no element would be interpreted as shell syntax for argv in (fetch_argv, checkout_argv, cherrypick_argv): for i, arg in enumerate(argv): self.assertIsInstance(arg, str) # No shell operators as separate argv entries self.assertNotEqual(arg, ";") self.assertNotEqual(arg, "|") self.assertNotEqual(arg, "&&") # --------------------------------------------------------------------------- # Context derivation fallback (finding C: collision-resistant hash) # --------------------------------------------------------------------------- class TestDeriveContextFallback(unittest.TestCase): """derive_context fallback: deterministic hash over declared scope.""" def test_same_request_stable_across_calls(self) -> None: """Same request produces same context on repeated calls.""" req = { "operation": "remediate", "pr_url": "https://github.com/org/repo/pull/42", } ctx1 = unattended.derive_context(req) ctx2 = unattended.derive_context(req) self.assertEqual(ctx1, ctx2) def test_reordered_keys_same_context(self) -> None: """Key ordering in the request dict doesn't affect context.""" import collections req1 = collections.OrderedDict([ ("operation", "remediate"), ("repo_path", "."), ("base_branch", "main"), ]) req2 = collections.OrderedDict([ ("base_branch", "main"), ("operation", "remediate"), ("repo_path", "."), ]) self.assertEqual( unattended.derive_context(req1), unattended.derive_context(req2), ) def test_sorted_lists_same_context(self) -> None: """Reordered CVE/package lists produce same context.""" req1 = { "operation": "remediate", "cve_ids": ["CVE-2024-2", "CVE-2024-1"], "packages": ["b-pkg", "a-pkg"], } req2 = { "operation": "remediate", "cve_ids": ["CVE-2024-1", "CVE-2024-2"], "packages": ["a-pkg", "b-pkg"], } # Primary context returns first valid CVE, which differs in # ordering; test the hash fallback specifically by removing # valid CVE patterns req1_no_primary = { "operation": "remediate", "work_ref": "ref1", "packages": ["b-pkg", "a-pkg"], } req2_no_primary = { "operation": "remediate", "work_ref": "ref1", "packages": ["a-pkg", "b-pkg"], } self.assertEqual( unattended.derive_context(req1_no_primary), unattended.derive_context(req2_no_primary), ) def test_different_pr_same_number_different_context(self) -> None: """pr42 from different repos produces different context.""" req1 = { "operation": "remediate", "pr_url": "https://github.com/org-a/repo/pull/42", } req2 = { "operation": "remediate", "pr_url": "https://github.com/org-b/repo/pull/42", } ctx1 = unattended.derive_context(req1) ctx2 = unattended.derive_context(req2) self.assertNotEqual(ctx1, ctx2) def test_different_work_ref_same_last_component(self) -> None: """a/foo and b/foo with same operation produce different context.""" req1 = { "operation": "remediate", "work_ref": "a/foo", } req2 = { "operation": "remediate", "work_ref": "b/foo", } self.assertNotEqual( unattended.derive_context(req1), unattended.derive_context(req2), ) def test_different_operation_different_context(self) -> None: req1 = { "operation": "remediate", "work_ref": "cve-fix/branch", } req2 = { "operation": "backport", "work_ref": "cve-fix/branch", "target_branches": ["rel-1"], } self.assertNotEqual( unattended.derive_context(req1), unattended.derive_context(req2), ) def test_different_base_branch_different_context(self) -> None: req1 = { "operation": "remediate", "work_ref": "ref", "base_branch": "main", } req2 = { "operation": "remediate", "work_ref": "ref", "base_branch": "release-2.16", } self.assertNotEqual( unattended.derive_context(req1), unattended.derive_context(req2), ) def test_nested_jira_tickets_included(self) -> None: """jira_action.tickets affect the fallback context.""" req1 = { "operation": "jira_action", "jira_action": { "action": "transition", "tickets": ["EDM-100"], "target_status": "MODIFIED", }, } req2 = { "operation": "jira_action", "jira_action": { "action": "transition", "tickets": ["EDM-200"], "target_status": "MODIFIED", }, } self.assertNotEqual( unattended.derive_context(req1), unattended.derive_context(req2), ) def test_different_jira_action_different_context(self) -> None: """Different jira_action type produces different context.""" req1 = { "operation": "jira_action", "jira_action": { "action": "transition", "tickets": ["EDM-100"], "target_status": "MODIFIED", }, } req2 = { "operation": "jira_action", "jira_action": { "action": "comment", "tickets": ["EDM-100"], "comment": "CVE fixed.", }, } self.assertNotEqual( unattended.derive_context(req1), unattended.derive_context(req2), ) def test_no_plaintext_payload_in_context(self) -> None: """Context identifier contains no plaintext comment/URL.""" req = { "operation": "jira_action", "jira_action": { "action": "comment", "tickets": ["EDM-100"], "comment": "This is a sensitive comment payload.", }, } ctx = unattended.derive_context(req) self.assertNotIn("sensitive", ctx) self.assertNotIn("comment", ctx.lower().replace("unattended-", "")) # Only hex characters after the prefix hex_part = ctx[len("unattended-"):] self.assertRegex(hex_part, r"^[0-9a-f]{32}$") def test_primary_jira_context_unchanged(self) -> None: """First valid Jira ticket is still preferred.""" req = { "operation": "remediate", "jira_tickets": ["EDM-1234"], "cve_ids": ["CVE-2024-5678"], } self.assertEqual(unattended.derive_context(req), "EDM-1234") def test_primary_cve_context_unchanged(self) -> None: """First valid CVE ID is still preferred over hash.""" req = {"cve_ids": ["CVE-2024-5678"]} self.assertEqual(unattended.derive_context(req), "CVE-2024-5678") def test_hash_length_128_bits(self) -> None: """Fallback hash is 32 hex chars = 128 bits.""" req = {"operation": "remediate", "work_ref": "some/ref"} ctx = unattended.derive_context(req) self.assertTrue(ctx.startswith("unattended-")) hex_part = ctx[len("unattended-"):] self.assertEqual(len(hex_part), 32) self.assertRegex(hex_part, r"^[0-9a-f]+$") def test_safe_ascii_context(self) -> None: """Fallback context is safe ASCII for directory names.""" req = { "operation": "remediate", "pr_url": "https://example.com/pull/1", "base_branch": "release/2.16", } ctx = unattended.derive_context(req) self.assertRegex(ctx, r"^[a-zA-Z0-9_-]+$") def test_comma_in_package_not_collapsed(self) -> None: """packages=['a,b'] vs packages=['a','b'] produce different hashes. Typed JSON serialization preserves array element boundaries — ["a,b"] != ["a","b"] even though comma-joining both gives "a,b". """ req1 = { "operation": "remediate", "pr_url": "https://github.com/org/repo/pull/42", "packages": ["a,b"], } req2 = { "operation": "remediate", "pr_url": "https://github.com/org/repo/pull/42", "packages": ["a", "b"], } self.assertNotEqual( unattended.derive_context(req1), unattended.derive_context(req2), ) def test_newline_field_boundary_not_collapsed(self) -> None: r"""Newline in scalar doesn't collapse with field boundary. repo_path='x\nwork_ref=y' must differ from repo_path='x', work_ref='y' — typed JSON naturally separates field keys from values. """ req1 = { "operation": "remediate", "repo_path": "x\nwork_ref=y", "pr_url": "https://github.com/org/repo/pull/42", } req2 = { "operation": "remediate", "repo_path": "x", "work_ref": "y", "pr_url": "https://github.com/org/repo/pull/42", } self.assertNotEqual( unattended.derive_context(req1), unattended.derive_context(req2), ) def test_branch_in_fallback_identity(self) -> None: """Explicit branch is part of fallback scope identity.""" req1 = { "operation": "remediate", "pr_url": "https://github.com/org/repo/pull/42", "branch": "cve-fix/custom-a", } req2 = { "operation": "remediate", "pr_url": "https://github.com/org/repo/pull/42", "branch": "cve-fix/custom-b", } self.assertNotEqual( unattended.derive_context(req1), unattended.derive_context(req2), ) def test_runtime_fields_excluded(self) -> None: """Runtime fields do not affect fallback context.""" base = { "operation": "remediate", "pr_url": "https://github.com/org/repo/pull/42", } req_with_runtime = { **base, "stage": "validate", "max_retries": 5, "time_budget_seconds": 3600, } self.assertEqual( unattended.derive_context(base), unattended.derive_context(req_with_runtime), ) def test_nested_tickets_reorder_stable(self) -> None: """Reordered jira_action.tickets produce same context.""" req1 = { "operation": "jira_action", "jira_action": { "action": "transition", "tickets": ["EDM-200", "EDM-100"], "target_status": "MODIFIED", }, } req2 = { "operation": "jira_action", "jira_action": { "action": "transition", "tickets": ["EDM-100", "EDM-200"], "target_status": "MODIFIED", }, } self.assertEqual( unattended.derive_context(req1), unattended.derive_context(req2), ) def test_different_repo_same_branch_different_context(self) -> None: """Same branch on different repos produces different context.""" req1 = { "operation": "remediate", "repo_path": "/repo-a", "branch": "cve-fix/EDM-1234", } req2 = { "operation": "remediate", "repo_path": "/repo-b", "branch": "cve-fix/EDM-1234", } self.assertNotEqual( unattended.derive_context(req1), unattended.derive_context(req2), ) class TestDeriveContextCLIConsumption(unittest.TestCase): """CLI validate outputs context; make-result/load-state consume it.""" def test_cli_validate_returns_context(self) -> None: """validate CLI returns deterministic context in JSON output.""" import io from unittest.mock import patch as _patch req = { "operation": "remediate", "pr_url": "https://github.com/org/repo/pull/42", } with tempfile.TemporaryDirectory() as td: path = os.path.join(td, "req.json") with open(path, "w") as f: json.dump(req, f) args = argparse.Namespace(request_file=path) captured_out = io.StringIO() with _patch("sys.stdout", captured_out): exit_code = unattended._cmd_validate(args) self.assertEqual(exit_code, 0) output = json.loads(captured_out.getvalue()) self.assertIn("context", output) ctx = output["context"] self.assertTrue(ctx.startswith("unattended-")) # Same request on second call produces same context captured_out2 = io.StringIO() with _patch("sys.stdout", captured_out2): exit_code2 = unattended._cmd_validate(args) self.assertEqual(exit_code2, 0) output2 = json.loads(captured_out2.getvalue()) self.assertEqual(output2["context"], ctx) def test_cli_make_result_uses_context_dir(self) -> None: """Subprocess isolation: validate → make-result → read back. Two fresh subprocess invocations prove: 1. validate produces a context, make-result writes to artifact-dir 2. A second validate of the same request derives the same context, and the result file written by the first make-result is retained and readable from the same path """ import subprocess script_path = os.path.join( os.path.dirname(__file__), "unattended.py", ) req = { "operation": "remediate", "pr_url": "https://github.com/org/repo/pull/42", } data = { "scope": {"cve_ids": ["CVE-2024-1"]}, "stages_completed": ["start", "scan"], "evidence": {"scan_verdict": "absent"}, } with tempfile.TemporaryDirectory() as td: req_path = os.path.join(td, "req.json") with open(req_path, "w") as f: json.dump(req, f) # Subprocess 1: validate → get context r1 = subprocess.run( [sys.executable, "-I", script_path, "validate", req_path], capture_output=True, text=True, timeout=10, ) self.assertEqual(r1.returncode, 0, r1.stderr) out1 = json.loads(r1.stdout) ctx = out1["context"] self.assertTrue(ctx.startswith("unattended-")) # Build artifact dir from context (as the skill would) artifact_dir = os.path.join(td, "artifacts", ctx) os.makedirs(artifact_dir, exist_ok=True) # Subprocess 2: make-result → write to artifact-dir r2 = subprocess.run( [ sys.executable, "-I", script_path, "make-result", "--status", "needs_human_review", "--operation", "remediate", "--artifact-dir", artifact_dir, "--data", json.dumps(data), ], capture_output=True, text=True, timeout=10, ) self.assertEqual(r2.returncode, 0, r2.stderr) result_written = json.loads(r2.stdout) self.assertEqual(result_written["status"], "needs_human_review") # Subprocess 3: validate same request → same context r3 = subprocess.run( [sys.executable, "-I", script_path, "validate", req_path], capture_output=True, text=True, timeout=10, ) self.assertEqual(r3.returncode, 0, r3.stderr) out3 = json.loads(r3.stdout) self.assertEqual(out3["context"], ctx) # Read result from the same artifact-dir — proves retention result_path = os.path.join(artifact_dir, "unattended-result.json") self.assertTrue( os.path.exists(result_path), "Result file must be retained across invocations", ) with open(result_path) as rf: loaded = json.loads(rf.read()) self.assertEqual(loaded["status"], "needs_human_review") self.assertEqual(loaded["operation"], "remediate") def test_cli_different_scope_separate_dirs(self) -> None: """Different request scopes produce different contexts/dirs. Two requests with different pr_url produce different contexts, and each writes results to its own directory without overwriting the other. """ import subprocess script_path = os.path.join( os.path.dirname(__file__), "unattended.py", ) req_a = { "operation": "remediate", "pr_url": "https://github.com/org/repo-a/pull/1", } req_b = { "operation": "remediate", "pr_url": "https://github.com/org/repo-b/pull/1", } data_a = { "scope": {"cve_ids": ["CVE-2024-1"]}, "stages_completed": ["start"], } data_b = { "scope": {"cve_ids": ["CVE-2024-2"]}, "stages_completed": ["start", "scan"], } with tempfile.TemporaryDirectory() as td: # Validate both requests for label, req in [("a", req_a), ("b", req_b)]: path = os.path.join(td, f"req-{label}.json") with open(path, "w") as f: json.dump(req, f) r_a = subprocess.run( [sys.executable, "-I", script_path, "validate", os.path.join(td, "req-a.json")], capture_output=True, text=True, timeout=10, ) r_b = subprocess.run( [sys.executable, "-I", script_path, "validate", os.path.join(td, "req-b.json")], capture_output=True, text=True, timeout=10, ) self.assertEqual(r_a.returncode, 0, r_a.stderr) self.assertEqual(r_b.returncode, 0, r_b.stderr) ctx_a = json.loads(r_a.stdout)["context"] ctx_b = json.loads(r_b.stdout)["context"] self.assertNotEqual(ctx_a, ctx_b) # Make results in separate dirs dir_a = os.path.join(td, "artifacts", ctx_a) dir_b = os.path.join(td, "artifacts", ctx_b) os.makedirs(dir_a, exist_ok=True) os.makedirs(dir_b, exist_ok=True) for label, adir, data in [("a", dir_a, data_a), ("b", dir_b, data_b)]: r = subprocess.run( [ sys.executable, "-I", script_path, "make-result", "--status", "needs_human_review", "--operation", "remediate", "--artifact-dir", adir, "--data", json.dumps(data), ], capture_output=True, text=True, timeout=10, ) self.assertEqual(r.returncode, 0, r.stderr) # Both results retained independently with open(os.path.join(dir_a, "unattended-result.json")) as f: res_a = json.loads(f.read()) with open(os.path.join(dir_b, "unattended-result.json")) as f: res_b = json.loads(f.read()) self.assertEqual( res_a["scope"]["cve_ids"], ["CVE-2024-1"], ) self.assertEqual( res_b["scope"]["cve_ids"], ["CVE-2024-2"], ) def test_cli_negative_invalid_request(self) -> None: """CLI validate rejects invalid request with reason, no artifact.""" import subprocess script_path = os.path.join( os.path.dirname(__file__), "unattended.py", ) req = {"operation": "invalid_op"} with tempfile.TemporaryDirectory() as td: req_path = os.path.join(td, "req.json") with open(req_path, "w") as f: json.dump(req, f) r = subprocess.run( [sys.executable, "-I", script_path, "validate", req_path], capture_output=True, text=True, timeout=10, ) self.assertEqual(r.returncode, 1) self.assertIn("Validation errors", r.stderr) output = json.loads(r.stdout) self.assertFalse(output["valid"]) self.assertTrue(len(output["errors"]) > 0) def test_cli_make_result_positive_parses_evidence(self) -> None: """Positive make-result: written result contains PASS evidence.""" import subprocess script_path = os.path.join( os.path.dirname(__file__), "unattended.py", ) data = { "scope": {"cve_ids": ["CVE-2024-1"]}, "stages_completed": ["start", "scan", "patch", "validate"], "evidence": { "scan_verdict": "fixed", "validation_verdict": "PASS", }, } with tempfile.TemporaryDirectory() as td: r = subprocess.run( [ sys.executable, "-I", script_path, "make-result", "--status", "completed", "--operation", "remediate", "--artifact-dir", td, "--data", json.dumps(data), ], capture_output=True, text=True, timeout=10, ) self.assertEqual(r.returncode, 0, r.stderr) result = json.loads(r.stdout) self.assertEqual(result["status"], "completed") self.assertEqual( result["evidence"]["validation_verdict"], "PASS", ) # File on disk matches stdout with open(os.path.join(td, "unattended-result.json")) as f: disk = json.loads(f.read()) self.assertEqual(disk["evidence"]["validation_verdict"], "PASS") # --------------------------------------------------------------------------- # Finding A: VEX justification on jira_action transition # --------------------------------------------------------------------------- # Load close.py for enum parity check (no network/setup dependency). # close.py imports _common which needs the scripts dir on sys.path. _CLOSE_SCRIPT = Path(__file__).resolve().parent / "close.py" _close_mod: Any = None _scripts_dir = str(_CLOSE_SCRIPT.parent) _path_added = _scripts_dir not in sys.path if _path_added: sys.path.insert(0, _scripts_dir) try: _close_spec = importlib.util.spec_from_file_location( "close", _CLOSE_SCRIPT, ) if _close_spec and _close_spec.loader: _close_mod = importlib.util.module_from_spec(_close_spec) _close_spec.loader.exec_module(_close_mod) except Exception: _close_mod = None finally: if _path_added and _scripts_dir in sys.path: sys.path.remove(_scripts_dir) class TestVexJustificationEnumParity(unittest.TestCase): """Unattended VEX values must exactly match close.py canonical keys.""" def test_enum_values_match_close_py(self) -> None: self.assertIsNotNone( _close_mod, "close.py could not be loaded for enum parity check", ) close_keys = set(_close_mod._VEX_JUSTIFICATION_FIELD_VALUES.keys()) unattended_keys = set(unattended._VEX_JUSTIFICATION_VALUES) self.assertEqual( close_keys, unattended_keys, "VEX justification canonical keys must be identical", ) def test_all_five_canonical_values_present(self) -> None: expected = { "component_not_present", "vulnerable_code_not_present", "vulnerable_code_not_in_execute_path", "vulnerable_code_cannot_be_controlled_by_adversary", "inline_mitigations_already_exist", } self.assertEqual(set(unattended._VEX_JUSTIFICATION_VALUES), expected) class TestVexJustificationValidateRequest(unittest.TestCase): """Request validation for jira_action.vex_justification.""" _BASE: dict[str, Any] = { "operation": "jira_action", "jira_action": { "action": "transition", "tickets": ["EDM-100"], "target_status": "MODIFIED", }, } def _with_vex(self, vex_val: Any) -> dict[str, Any]: req = json.loads(json.dumps(self._BASE)) req["jira_action"]["vex_justification"] = vex_val return req def test_valid_canonical_code_accepted(self) -> None: for code in sorted(unattended._VEX_JUSTIFICATION_VALUES): req = self._with_vex(code) errs = unattended.validate_request(req) self.assertEqual(errs, [], f"code {code} should be accepted") def test_null_vex_rejected(self) -> None: """Explicit null vex_justification rejected (contract: 'Null...rejected').""" req = self._with_vex(None) errs = unattended.validate_request(req) self.assertTrue( any("vex_justification" in e and "null" in e for e in errs), f"Expected null rejection, got: {errs}", ) def test_absent_vex_accepted(self) -> None: """Absent vex_justification key (not in dict) is accepted.""" errs = unattended.validate_request(self._BASE) self.assertEqual(errs, []) def test_absent_vex_key_vs_null_distinct(self) -> None: """Absent key passes; explicit null is rejected.""" # Absent: no key at all req_absent = json.loads(json.dumps(self._BASE)) self.assertNotIn("vex_justification", req_absent["jira_action"]) errs_absent = unattended.validate_request(req_absent) self.assertEqual(errs_absent, []) # Null: key present with null value req_null = self._with_vex(None) self.assertIn("vex_justification", req_null["jira_action"]) errs_null = unattended.validate_request(req_null) self.assertTrue(len(errs_null) > 0) def test_blank_string_rejected(self) -> None: errs = unattended.validate_request(self._with_vex("")) self.assertTrue(any("vex_justification" in e for e in errs)) def test_whitespace_only_rejected(self) -> None: errs = unattended.validate_request(self._with_vex(" ")) self.assertTrue(any("vex_justification" in e for e in errs)) def test_nonstring_rejected(self) -> None: errs = unattended.validate_request(self._with_vex(42)) self.assertTrue(any("vex_justification" in e for e in errs)) def test_boolean_rejected(self) -> None: errs = unattended.validate_request(self._with_vex(True)) self.assertTrue(any("vex_justification" in e for e in errs)) def test_unsupported_value_rejected(self) -> None: errs = unattended.validate_request(self._with_vex("not_a_code")) self.assertTrue(any("vex_justification" in e for e in errs)) self.assertTrue(any("must be one of" in e for e in errs)) def test_comment_action_with_vex_rejected(self) -> None: req: dict[str, Any] = { "operation": "jira_action", "jira_action": { "action": "comment", "tickets": ["EDM-100"], "comment": "Test comment.", "vex_justification": "component_not_present", }, } errs = unattended.validate_request(req) self.assertTrue( any("not valid for 'comment'" in e for e in errs), f"Expected comment rejection, got: {errs}", ) class TestVexJustificationMakeResult(unittest.TestCase): """make_result validation for VEX fields in expected_jira_action.""" _CHILD_OK: dict[str, Any] = { "ticket": "EDM-100", "action": "transition", "target_status": "MODIFIED", "result": "success", "vex_justification": "component_not_present", "vex_field_verified": True, } def _make(self, **overrides: Any) -> dict[str, Any]: defaults: dict[str, Any] = { "status": "completed", "operation": "jira_action", "scope": {"cve_ids": ["CVE-2024-1"]}, "jira_results": [self._CHILD_OK.copy()], "expected_tickets": ["EDM-100"], "expected_jira_action": { "action": "transition", "target_status": "MODIFIED", "vex_justification": "component_not_present", }, } defaults.update(overrides) return unattended.make_result(**defaults) def test_valid_vex_transition_accepted(self) -> None: result = self._make() self.assertEqual(result["status"], "completed") def test_unsupported_vex_code_rejected(self) -> None: with self.assertRaises(ValueError) as ctx: self._make(expected_jira_action={ "action": "transition", "target_status": "MODIFIED", "vex_justification": "bad_code", }) self.assertIn("vex_justification", str(ctx.exception)) def test_vex_on_comment_action_rejected(self) -> None: with self.assertRaises(ValueError) as ctx: self._make( expected_jira_action={ "action": "comment", "comment": "test", "vex_justification": "component_not_present", }, jira_results=[{ "ticket": "EDM-100", "result": "success", "comment": "test", }], ) self.assertIn("only valid for 'transition'", str(ctx.exception)) def test_child_missing_vex_field_verified_rejected(self) -> None: child = self._CHILD_OK.copy() del child["vex_field_verified"] with self.assertRaises(ValueError) as ctx: self._make(jira_results=[child]) self.assertIn("vex_field_verified", str(ctx.exception)) def test_child_wrong_vex_justification_rejected(self) -> None: child = self._CHILD_OK.copy() child["vex_justification"] = "vulnerable_code_not_present" with self.assertRaises(ValueError) as ctx: self._make(jira_results=[child]) self.assertIn("does not match", str(ctx.exception)) def test_unrequested_vex_receipt_rejected(self) -> None: """No vex_justification requested but child claims verified.""" child = { "ticket": "EDM-100", "action": "transition", "target_status": "MODIFIED", "result": "success", "vex_field_verified": True, } with self.assertRaises(ValueError) as ctx: self._make( expected_jira_action={ "action": "transition", "target_status": "MODIFIED", }, jira_results=[child], ) self.assertIn( "vex_field_verified", str(ctx.exception), ) def test_normal_transition_no_vex_unchanged(self) -> None: """Normal transition (no vex_justification) keeps old behavior.""" result = unattended.make_result( status="completed", operation="jira_action", scope={"cve_ids": ["CVE-2024-1"]}, jira_results=[{ "ticket": "EDM-100", "action": "transition", "target_status": "MODIFIED", "result": "success", }], expected_tickets=["EDM-100"], expected_jira_action={ "action": "transition", "target_status": "MODIFIED", }, ) self.assertEqual(result["status"], "completed") def test_null_eja_vex_rejected(self) -> None: """Explicit null vex_justification in EJA rejected.""" with self.assertRaises(ValueError) as ctx: self._make(expected_jira_action={ "action": "transition", "target_status": "MODIFIED", "vex_justification": None, }) self.assertIn("null", str(ctx.exception)) def test_absent_eja_vex_accepted(self) -> None: """EJA without vex_justification key is valid (absent != null).""" result = unattended.make_result( status="completed", operation="jira_action", scope={"cve_ids": ["CVE-2024-1"]}, jira_results=[{ "ticket": "EDM-100", "result": "success", }], expected_tickets=["EDM-100"], expected_jira_action={ "action": "transition", "target_status": "MODIFIED", }, ) self.assertEqual(result["status"], "completed") class TestVexJustificationDeriveContext(unittest.TestCase): """derive_context includes vex_justification in scope hash.""" def test_different_vex_different_context(self) -> None: base: dict[str, Any] = { "operation": "jira_action", "jira_action": { "action": "transition", "tickets": ["EDM-100"], "target_status": "MODIFIED", }, } req_a = json.loads(json.dumps(base)) req_a["jira_action"]["vex_justification"] = "component_not_present" req_b = json.loads(json.dumps(base)) req_b["jira_action"]["vex_justification"] = ( "vulnerable_code_not_present" ) ctx_a = unattended.derive_context(req_a) ctx_b = unattended.derive_context(req_b) self.assertNotEqual(ctx_a, ctx_b) def test_same_vex_stable_context(self) -> None: req: dict[str, Any] = { "operation": "jira_action", "jira_action": { "action": "transition", "tickets": ["EDM-100"], "target_status": "MODIFIED", "vex_justification": "component_not_present", }, } ctx1 = unattended.derive_context(req) ctx2 = unattended.derive_context(req) self.assertEqual(ctx1, ctx2) # --------------------------------------------------------------------------- # Finding B: Prior operations history (result overwrite fix) # --------------------------------------------------------------------------- class TestPriorOperationsMakeResult(unittest.TestCase): """Pure make_result handles prior_operations correctly.""" def test_empty_prior_operations_default(self) -> None: result = unattended.make_result( status="failed", operation="remediate", scope={"cve_ids": ["CVE-2024-1"]}, ) self.assertEqual(result["prior_operations"], []) def test_prior_operations_passed_through(self) -> None: prior = [{"status": "failed", "operation": "remediate", "scope": {}, "timestamp": "2024-01-01T00:00:00Z"}] result = unattended.make_result( status="completed", operation="remediate", scope={"cve_ids": ["CVE-2024-1"]}, prior_operations=prior, ) self.assertEqual(len(result["prior_operations"]), 1) self.assertEqual( result["prior_operations"][0]["status"], "failed", ) def test_prior_operations_nondict_entry_rejected(self) -> None: with self.assertRaises(ValueError) as ctx: unattended.make_result( status="failed", operation="remediate", scope={}, prior_operations=[42], # type: ignore[list-item] ) self.assertIn("prior_operations[0]", str(ctx.exception)) def test_prior_operations_nonlist_rejected(self) -> None: with self.assertRaises(ValueError) as ctx: unattended.make_result( status="failed", operation="remediate", scope={}, prior_operations="bad", # type: ignore[arg-type] ) self.assertIn("prior_operations", str(ctx.exception)) def test_nested_prior_operations_rejected(self) -> None: """Entry containing nested prior_operations is rejected.""" with self.assertRaises(ValueError) as ctx: unattended.make_result( status="failed", operation="remediate", scope={}, prior_operations=[ {"status": "failed", "prior_operations": [{"x": 1}]}, ], ) self.assertIn("nested", str(ctx.exception)) def test_flat_history_validator_standalone(self) -> None: """validate_flat_history rejects bad shapes.""" # Non-list with self.assertRaises(ValueError): unattended.validate_flat_history("bad") # type: ignore[arg-type] # Non-dict entry with self.assertRaises(ValueError): unattended.validate_flat_history([42]) # type: ignore[list-item] # Nested with self.assertRaises(ValueError): unattended.validate_flat_history( [{"prior_operations": []}], ) # Valid unattended.validate_flat_history([{"status": "ok"}]) unattended.validate_flat_history([]) class TestPriorOperationsCLI(unittest.TestCase): """CLI make-result preserves prior operations across calls.""" def _run_make_result( self, td: str, data: dict[str, Any], status: str = "completed", operation: str = "remediate", ) -> "subprocess.CompletedProcess[str]": import subprocess return subprocess.run( [sys.executable, "-I", str(_SCRIPT), "make-result", "--status", status, "--operation", operation, "--artifact-dir", td, "--data", json.dumps(data)], capture_output=True, text=True, timeout=10, ) def test_two_results_preserve_first(self) -> None: with tempfile.TemporaryDirectory() as td: # First result: close_vex with evidence data1: dict[str, Any] = { "scope": {"cve_ids": ["CVE-2024-1"]}, "stages_completed": ["start", "scan"], "evidence": {"scan_verdict": "absent"}, "human_decisions": [{"decision_id": "vex-1", "justification": "component_not_present"}], } r1 = self._run_make_result(td, data1) self.assertEqual(r1.returncode, 0, r1.stderr) # Second result: jira_action transition data2: dict[str, Any] = { "scope": {"cve_ids": ["CVE-2024-1"]}, "jira_results": [{ "ticket": "EDM-100", "action": "transition", "target_status": "CLOSED", "result": "success", "vex_justification": "component_not_present", "vex_field_verified": True, }], "expected_tickets": ["EDM-100"], "expected_jira_action": { "action": "transition", "target_status": "CLOSED", "vex_justification": "component_not_present", }, } r2 = self._run_make_result( td, data2, operation="jira_action", ) self.assertEqual(r2.returncode, 0, r2.stderr) with open(os.path.join(td, "unattended-result.json")) as f: final = json.loads(f.read()) # Current result is jira_action self.assertEqual(final["operation"], "jira_action") # Prior operations has exactly one entry (the first result) self.assertEqual(len(final["prior_operations"]), 1) prior = final["prior_operations"][0] self.assertEqual(prior["operation"], "remediate") # Original evidence preserved self.assertEqual( prior["evidence"]["scan_verdict"], "absent", ) # Original human_decisions preserved self.assertEqual( prior["human_decisions"][0]["decision_id"], "vex-1", ) def test_six_results_retain_first_vex(self) -> None: """More than 5 follow-on results must retain the original.""" with tempfile.TemporaryDirectory() as td: # First result with VEX evidence data0: dict[str, Any] = { "scope": {"cve_ids": ["CVE-2024-1"]}, "evidence": {"scan_verdict": "absent", "vex_reason": "component_not_present"}, "human_decisions": [{"decision_id": "vex-original"}], } r = self._run_make_result(td, data0, status="failed") self.assertEqual(r.returncode, 0, r.stderr) # Six more results with unique evidence for i in range(1, 7): data_n: dict[str, Any] = { "scope": {"cve_ids": ["CVE-2024-1"]}, "evidence": {"attempt": i, "note": f"run-{i}"}, } r = self._run_make_result(td, data_n, status="failed") self.assertEqual(r.returncode, 0, r.stderr) with open(os.path.join(td, "unattended-result.json")) as f: final = json.loads(f.read()) # 6 prior operations (first call has none, 7 calls total) self.assertEqual(len(final["prior_operations"]), 6) # First entry is the original VEX result first = final["prior_operations"][0] self.assertEqual( first["evidence"]["vex_reason"], "component_not_present", ) self.assertEqual( first["human_decisions"][0]["decision_id"], "vex-original", ) def test_history_is_flat_not_recursive(self) -> None: """Snapshots strip prior_operations to prevent nesting.""" with tempfile.TemporaryDirectory() as td: for i in range(3): data: dict[str, Any] = { "scope": {"cve_ids": ["CVE-2024-1"]}, "evidence": {"step": i}, } r = self._run_make_result(td, data, status="failed") self.assertEqual(r.returncode, 0, r.stderr) with open(os.path.join(td, "unattended-result.json")) as f: final = json.loads(f.read()) # No entry has nested prior_operations for entry in final["prior_operations"]: self.assertNotIn( "prior_operations", entry, "History entries must not contain nested " "prior_operations", ) def test_malformed_json_old_file_preserved(self) -> None: """Malformed existing JSON → controlled error, old bytes kept.""" with tempfile.TemporaryDirectory() as td: result_path = os.path.join(td, "unattended-result.json") bad_content = "this is not json {" with open(result_path, "w") as f: f.write(bad_content) data: dict[str, Any] = { "scope": {"cve_ids": ["CVE-2024-1"]}, } r = self._run_make_result(td, data, status="failed") # Must fail with controlled error self.assertNotEqual(r.returncode, 0) self.assertIn("malformed", r.stderr.lower()) # Old file unchanged with open(result_path) as f: self.assertEqual(f.read(), bad_content) def test_nondict_existing_result_error(self) -> None: """Existing result that is valid JSON but not a dict → error.""" with tempfile.TemporaryDirectory() as td: result_path = os.path.join(td, "unattended-result.json") with open(result_path, "w") as f: f.write("[]") data: dict[str, Any] = { "scope": {"cve_ids": ["CVE-2024-1"]}, } r = self._run_make_result(td, data, status="failed") self.assertNotEqual(r.returncode, 0) self.assertIn("not a JSON object", r.stderr) # Old file unchanged with open(result_path) as f: self.assertEqual(f.read(), "[]") class TestPriorOperationsCLIStrict(unittest.TestCase): """CLI strict history validation.""" def _run_make_result( self, td: str, data: dict[str, Any], status: str = "failed", operation: str = "remediate", ) -> "subprocess.CompletedProcess[str]": import subprocess return subprocess.run( [sys.executable, "-I", str(_SCRIPT), "make-result", "--status", status, "--operation", operation, "--artifact-dir", td, "--data", json.dumps(data)], capture_output=True, text=True, timeout=10, ) def test_nested_old_history_rejected(self) -> None: """Old result with nested prior_operations entry → controlled error.""" with tempfile.TemporaryDirectory() as td: old = { "status": "failed", "operation": "remediate", "scope": {}, "prior_operations": [ {"status": "completed", "prior_operations": [{"x": 1}]}, ], } Path(td, "unattended-result.json").write_text(json.dumps(old)) data: dict[str, Any] = {"scope": {"cve_ids": ["CVE-2024-1"]}} r = self._run_make_result(td, data) self.assertNotEqual(r.returncode, 0) self.assertIn("nested", r.stderr.lower()) # Old file preserved with open(os.path.join(td, "unattended-result.json")) as f: self.assertEqual(json.loads(f.read()), old) def test_nondict_old_history_entry_rejected(self) -> None: """Old result with non-dict history entry → controlled error.""" with tempfile.TemporaryDirectory() as td: old = { "status": "failed", "operation": "remediate", "scope": {}, "prior_operations": ["not-a-dict"], } Path(td, "unattended-result.json").write_text(json.dumps(old)) data: dict[str, Any] = {"scope": {"cve_ids": ["CVE-2024-1"]}} r = self._run_make_result(td, data) self.assertNotEqual(r.returncode, 0) self.assertIn("dict", r.stderr) def test_incoming_prior_operations_accepted(self) -> None: """data.prior_operations from --data is accepted and prepended.""" with tempfile.TemporaryDirectory() as td: incoming = [ {"status": "failed", "operation": "remediate", "scope": {}, "evidence": {"from": "portable"}}, ] data: dict[str, Any] = { "scope": {"cve_ids": ["CVE-2024-1"]}, "prior_operations": incoming, } r = self._run_make_result(td, data) self.assertEqual(r.returncode, 0, r.stderr) result = json.loads(r.stdout) # Incoming history is present self.assertEqual(len(result["prior_operations"]), 1) self.assertEqual( result["prior_operations"][0]["evidence"]["from"], "portable", ) def test_incoming_nested_rejected(self) -> None: """data.prior_operations with nested entry → controlled error.""" with tempfile.TemporaryDirectory() as td: data: dict[str, Any] = { "scope": {"cve_ids": ["CVE-2024-1"]}, "prior_operations": [ {"status": "ok", "prior_operations": [{"x": 1}]}, ], } r = self._run_make_result(td, data) self.assertNotEqual(r.returncode, 0) self.assertIn("nested", r.stderr.lower()) def test_incoming_plus_old_combined(self) -> None: """Incoming + old-file history combined in correct order.""" with tempfile.TemporaryDirectory() as td: # First write a result data1: dict[str, Any] = { "scope": {"cve_ids": ["CVE-2024-1"]}, "evidence": {"step": "first"}, } r1 = self._run_make_result(td, data1) self.assertEqual(r1.returncode, 0, r1.stderr) # Second write with incoming seed history incoming = [{"status": "failed", "evidence": {"from": "seed"}}] data2: dict[str, Any] = { "scope": {"cve_ids": ["CVE-2024-1"]}, "evidence": {"step": "second"}, "prior_operations": incoming, } r2 = self._run_make_result(td, data2) self.assertEqual(r2.returncode, 0, r2.stderr) result = json.loads(r2.stdout) # Should have: seed (incoming) + first (old snapshot) = 2 self.assertEqual(len(result["prior_operations"]), 2) # Incoming first, then old snapshot self.assertEqual( result["prior_operations"][0]["evidence"]["from"], "seed", ) self.assertEqual( result["prior_operations"][1]["evidence"]["step"], "first", ) class TestMalformedResultResume(unittest.TestCase): """Malformed result blocks resume stage suggestions.""" def test_malformed_result_blocks_resume(self) -> None: """result_malformed=True → needs_input, not mutating stage.""" state = { "result_malformed": True, "has_context": True, "scan_valid": True, "has_scan_file": True, "has_patch_file": True, "has_validation_file": True, } resume = unattended.determine_resume_stage(state, None) self.assertEqual(resume["status"], "needs_input") self.assertIn("result_valid", resume["missing_prereqs"]) def test_malformed_result_blocks_requested_stage(self) -> None: """result_malformed=True blocks even explicit stage request.""" state = {"result_malformed": True, "has_context": True} resume = unattended.determine_resume_stage(state, "scan") self.assertEqual(resume["status"], "needs_input") def test_no_malformed_result_allows_resume(self) -> None: """Normal state without result_malformed proceeds normally.""" state = { "has_context": True, "scan_valid": True, "has_scan_file": True, "has_patch_file": True, "has_validation_file": True, } resume = unattended.determine_resume_stage(state, None) self.assertEqual(resume["status"], "ok") def test_cli_load_state_malformed_exits_1(self) -> None: """CLI load-state with malformed result exits 1.""" with tempfile.TemporaryDirectory() as td: Path(td, "unattended-result.json").write_text("bad json") Path(td, "context.md").write_text("# ctx") args = argparse.Namespace(artifact_dir=td, stage=None) import io from unittest.mock import patch as _patch captured = io.StringIO() with _patch("sys.stdout", captured): rc = unattended._cmd_load_state(args) self.assertEqual(rc, 1) output = json.loads(captured.getvalue()) self.assertEqual(output["resume"]["status"], "needs_input") class TestPriorOperationsLoadState(unittest.TestCase): """load_state exposes prior_operations metadata.""" def test_result_with_prior_operations(self) -> None: with tempfile.TemporaryDirectory() as td: result = { "status": "completed", "prior_operations": [ {"status": "failed", "operation": "remediate"}, {"status": "completed", "operation": "remediate"}, ], } Path(td, "unattended-result.json").write_text( json.dumps(result), ) state = unattended.load_state(td) self.assertTrue(state.get("has_prior_operations")) self.assertEqual(state["prior_operations_count"], 2) def test_result_without_prior_operations(self) -> None: with tempfile.TemporaryDirectory() as td: result = {"status": "completed"} Path(td, "unattended-result.json").write_text( json.dumps(result), ) state = unattended.load_state(td) self.assertFalse(state.get("has_prior_operations", False)) def test_malformed_result_marked(self) -> None: with tempfile.TemporaryDirectory() as td: Path(td, "unattended-result.json").write_text("not json") state = unattended.load_state(td) self.assertTrue(state.get("result_malformed")) def test_nondict_result_marked_malformed(self) -> None: with tempfile.TemporaryDirectory() as td: Path(td, "unattended-result.json").write_text("[]") state = unattended.load_state(td) self.assertTrue(state.get("result_malformed")) def test_portable_wrapper_roundtrip_with_history(self) -> None: """prior_result wrapper carries history inside .result.""" inner_result = { "status": "completed", "operation": "remediate", "scope": {"cve_ids": ["CVE-2024-1"]}, "prior_operations": [ {"status": "failed", "evidence": {"scan": "absent"}}, ], } wrapper = { "result": inner_result, "decision_requests": [], "scan_snapshot": { "scan_id": "scan-1", "scope": {"cve_ids": ["CVE-2024-1"]}, "candidate_sha": "abc123", }, } # Validate wrapper structure errs = unattended.validate_prior_result( wrapper, {"cve_ids": ["CVE-2024-1"]}, ) self.assertEqual(errs, []) # History inside result is preserved self.assertEqual( len(inner_result["prior_operations"]), 1, ) # --------------------------------------------------------------------------- # Finding C: Helper argv JSON (shell=False + json.dumps) # --------------------------------------------------------------------------- class TestHelperArgvJsonRoundtrip(unittest.TestCase): """CLI roundtrip with special characters in --data payloads.""" def test_payloads_with_special_chars(self) -> None: """Nine payloads with shell metacharacters as benign data. Each payload contains shell metacharacters that would be dangerous if shell-evaluated, but uses benign marker strings (MARKER_A through MARKER_I) instead of destructive commands. The test verifies: (1) exact JSON round-trip, (2) file on disk matches, (3) no marker string appears outside the JSON payload (proving no shell evaluation occurred). """ import subprocess payloads = [ {"note": "it's a test"}, {"note": 'value with "double quotes"'}, {"note": "newline\nin value"}, {"note": "dollar $(echo MARKER_D)"}, {"note": "backtick `echo MARKER_E`"}, {"note": "semicolon; echo MARKER_F"}, {"note": "pipe | echo MARKER_G"}, {"note": "ampersand & echo MARKER_H"}, {"note": "mixed 'apos' \"quotes\" $(echo MARKER_I) `echo MARKER_I` ;|&"}, ] # Markers that must NOT appear in non-JSON output (stdout # prefix before the JSON payload, and stderr). If any marker # appears there, shell evaluation occurred. markers = [ "MARKER_D", "MARKER_E", "MARKER_F", "MARKER_G", "MARKER_H", "MARKER_I", ] with tempfile.TemporaryDirectory() as td: for i, payload in enumerate(payloads): data: dict[str, Any] = { "scope": {"cve_ids": ["CVE-2024-1"]}, "evidence": payload, } # Use a unique subdir to avoid prior_operations subdir = os.path.join(td, f"run-{i}") os.makedirs(subdir) r = subprocess.run( [sys.executable, "-I", str(_SCRIPT), "make-result", "--status", "failed", "--operation", "remediate", "--artifact-dir", subdir, "--data", json.dumps(data)], capture_output=True, text=True, timeout=10, ) self.assertEqual( r.returncode, 0, f"Payload {i} failed: {r.stderr}", ) result = json.loads(r.stdout) # Exact JSON round-trip self.assertEqual( result["evidence"], payload, f"Payload {i} not preserved exactly", ) # Verify file on disk matches with open(os.path.join( subdir, "unattended-result.json", )) as f: disk = json.loads(f.read()) self.assertEqual(disk["evidence"], payload) # Verify no shell execution: markers must be absent # from stderr entirely (they should never appear # outside the JSON evidence payload). for marker in markers: self.assertNotIn( marker, r.stderr, f"Payload {i}: marker {marker} found in " f"stderr — shell evaluation occurred", ) def test_scope_with_special_chars(self) -> None: """--data scope with special chars preserved exactly.""" import subprocess scope = { "cve_ids": ["CVE-2024-1"], "repo": "org/repo's-name", "note": 'has "quotes" and $(cmd)', } data: dict[str, Any] = {"scope": scope} with tempfile.TemporaryDirectory() as td: r = subprocess.run( [sys.executable, "-I", str(_SCRIPT), "make-result", "--status", "failed", "--operation", "remediate", "--artifact-dir", td, "--data", json.dumps(data)], capture_output=True, text=True, timeout=10, ) self.assertEqual(r.returncode, 0, r.stderr) result = json.loads(r.stdout) self.assertEqual(result["scope"], scope) def test_installed_script_path_with_spaces(self) -> None: """Script path containing spaces works correctly.""" import shutil import subprocess with tempfile.TemporaryDirectory() as td: spaced_dir = os.path.join(td, "path with spaces", "scripts") os.makedirs(spaced_dir) spaced_script = os.path.join(spaced_dir, "unattended.py") shutil.copy2(str(_SCRIPT), spaced_script) artifact_dir = os.path.join(td, "artifacts") os.makedirs(artifact_dir) data: dict[str, Any] = { "scope": {"cve_ids": ["CVE-2024-1"]}, } r = subprocess.run( [sys.executable, "-I", spaced_script, "make-result", "--status", "failed", "--operation", "remediate", "--artifact-dir", artifact_dir, "--data", json.dumps(data)], capture_output=True, text=True, timeout=10, ) self.assertEqual(r.returncode, 0, r.stderr) result = json.loads(r.stdout) self.assertEqual(result["status"], "failed") class TestLoadStateHistoryValidation(unittest.TestCase): """load_state validates prior_operations with shared strict validator.""" def test_bad_container_type_sets_malformed(self) -> None: """prior_operations as string → result_malformed, not safe-empty.""" with tempfile.TemporaryDirectory() as td: result = { "status": "completed", "prior_operations": "not-a-list", } Path(td, "unattended-result.json").write_text( json.dumps(result), ) state = unattended.load_state(td) self.assertTrue(state.get("result_malformed")) self.assertNotIn("result", state) def test_nondict_entry_sets_malformed(self) -> None: """prior_operations with non-dict entry → result_malformed.""" with tempfile.TemporaryDirectory() as td: result = { "status": "completed", "prior_operations": [42, "bad"], } Path(td, "unattended-result.json").write_text( json.dumps(result), ) state = unattended.load_state(td) self.assertTrue(state.get("result_malformed")) def test_nested_entry_sets_malformed(self) -> None: """prior_operations with nested prior_operations → malformed.""" with tempfile.TemporaryDirectory() as td: result = { "status": "completed", "prior_operations": [ {"status": "ok", "prior_operations": [{"x": 1}]}, ], } Path(td, "unattended-result.json").write_text( json.dumps(result), ) state = unattended.load_state(td) self.assertTrue(state.get("result_malformed")) def test_null_prior_operations_sets_malformed(self) -> None: """Explicit null prior_operations → result_malformed.""" with tempfile.TemporaryDirectory() as td: result = { "status": "completed", "prior_operations": None, } Path(td, "unattended-result.json").write_text( json.dumps(result), ) state = unattended.load_state(td) self.assertTrue(state.get("result_malformed")) def test_valid_history_loads_normally(self) -> None: """Valid flat history loads without malformed flag.""" with tempfile.TemporaryDirectory() as td: result = { "status": "completed", "prior_operations": [ {"status": "failed", "operation": "remediate"}, ], } Path(td, "unattended-result.json").write_text( json.dumps(result), ) state = unattended.load_state(td) self.assertFalse(state.get("result_malformed", False)) self.assertTrue(state.get("has_prior_operations")) def test_unicode_error_sets_malformed(self) -> None: """Invalid UTF-8 in result file → result_malformed, not crash.""" with tempfile.TemporaryDirectory() as td: result_path = Path(td, "unattended-result.json") result_path.write_bytes(b'{"status": "ok", "x": "\xff\xfe"}') state = unattended.load_state(td) self.assertTrue(state.get("result_malformed")) def test_malformed_history_blocks_resume(self) -> None: """Bad prior_operations → result_malformed → needs_input.""" with tempfile.TemporaryDirectory() as td: result = { "status": "completed", "prior_operations": ["not-a-dict"], } Path(td, "unattended-result.json").write_text( json.dumps(result), ) Path(td, "context.md").write_text("# ctx") state = unattended.load_state(td) self.assertTrue(state.get("result_malformed")) resume = unattended.determine_resume_stage(state, "scan") self.assertEqual(resume["status"], "needs_input") self.assertIn("result_valid", resume["missing_prereqs"]) def test_cli_load_state_bad_history_exits_1(self) -> None: """CLI load-state with invalid history → exit 1 / needs_input.""" with tempfile.TemporaryDirectory() as td: result = { "status": "completed", "prior_operations": "not-a-list", } Path(td, "unattended-result.json").write_text( json.dumps(result), ) args = argparse.Namespace(artifact_dir=td, stage=None) import io from unittest.mock import patch as _patch captured = io.StringIO() with _patch("sys.stdout", captured): rc = unattended._cmd_load_state(args) self.assertEqual(rc, 1) output = json.loads(captured.getvalue()) self.assertTrue(output.get("result_malformed")) self.assertEqual( output["resume"]["status"], "needs_input", ) class TestPriorResultHistoryValidation(unittest.TestCase): """validate_prior_result checks result.prior_operations when present.""" def _base_wrapper(self, inner_result: dict) -> dict: return { "result": inner_result, "decision_requests": [], "scan_snapshot": { "scan_id": "scan-1", "scope": {"cve_ids": ["CVE-2024-1"]}, "candidate_sha": "abc", }, } def test_valid_inner_history_accepted(self) -> None: wrapper = self._base_wrapper({ "status": "completed", "prior_operations": [{"status": "failed"}], }) errs = unattended.validate_prior_result( wrapper, {"cve_ids": ["CVE-2024-1"]}, ) self.assertEqual(errs, []) def test_null_inner_history_rejected(self) -> None: wrapper = self._base_wrapper({ "status": "completed", "prior_operations": None, }) errs = unattended.validate_prior_result( wrapper, {"cve_ids": ["CVE-2024-1"]}, ) self.assertTrue(any("null" in e for e in errs)) def test_nonlist_inner_history_rejected(self) -> None: wrapper = self._base_wrapper({ "status": "completed", "prior_operations": "bad", }) errs = unattended.validate_prior_result( wrapper, {"cve_ids": ["CVE-2024-1"]}, ) self.assertTrue(any("list" in e for e in errs)) def test_nested_inner_history_rejected(self) -> None: wrapper = self._base_wrapper({ "status": "completed", "prior_operations": [ {"status": "ok", "prior_operations": [{"x": 1}]}, ], }) errs = unattended.validate_prior_result( wrapper, {"cve_ids": ["CVE-2024-1"]}, ) self.assertTrue(any("nested" in e.lower() for e in errs)) def test_absent_inner_history_accepted(self) -> None: """Absent key is fine — no history to validate.""" wrapper = self._base_wrapper({"status": "completed"}) errs = unattended.validate_prior_result( wrapper, {"cve_ids": ["CVE-2024-1"]}, ) self.assertEqual(errs, []) class TestIncomingNullPriorOperations(unittest.TestCase): """CLI make-result rejects data.prior_operations=null.""" def test_null_incoming_history_rejected(self) -> None: import subprocess with tempfile.TemporaryDirectory() as td: data = { "scope": {"cve_ids": ["CVE-2024-1"]}, "prior_operations": None, } r = subprocess.run( [sys.executable, "-I", str(_SCRIPT), "make-result", "--status", "failed", "--operation", "remediate", "--artifact-dir", td, "--data", json.dumps(data)], capture_output=True, text=True, timeout=10, ) self.assertNotEqual(r.returncode, 0) self.assertIn("null", r.stderr.lower()) class TestAtomicWriteShortWrite(unittest.TestCase): """Atomic write handles short os.write correctly.""" def test_full_json_written_under_mocked_short_writes(self) -> None: """When os.write only writes a prefix, the write-all loop completes.""" import io import unittest.mock original_write = os.write write_calls: list[int] = [] def short_write(fd: int, data: bytes) -> int: # Write only a small prefix each call to exercise the loop chunk = data[:7] n = original_write(fd, chunk) write_calls.append(n) return n with tempfile.TemporaryDirectory() as td: args = argparse.Namespace( status="failed", operation="remediate", artifact_dir=td, data=json.dumps({ "scope": {"cve_ids": ["CVE-2024-1"]}, "evidence": { "large_field": "x" * 500, "scan_verdict": "fixed", }, }), ) captured = io.StringIO() with unittest.mock.patch("os.write", side_effect=short_write): with unittest.mock.patch("sys.stdout", captured): rc = unattended._cmd_make_result(args) self.assertEqual(rc, 0, "make-result should succeed") # Multiple write calls prove the loop iterated self.assertGreater( len(write_calls), 1, "short writes must cause multiple os.write calls", ) # Disk matches stdout result_path = os.path.join(td, "unattended-result.json") with open(result_path) as f: disk_data = json.loads(f.read()) stdout_data = json.loads(captured.getvalue()) self.assertEqual(disk_data, stdout_data) self.assertEqual( disk_data["evidence"]["large_field"], "x" * 500, ) def test_zero_progress_write_preserves_old(self) -> None: """os.write returning 0 → OSError, old file preserved.""" with tempfile.TemporaryDirectory() as td: old_content = json.dumps({"status": "old"}) result_path = Path(td, "unattended-result.json") result_path.write_text(old_content) # Patch os.write to return 0 (zero progress) import unittest.mock original_write = os.write call_count = 0 def zero_write(fd: int, data: bytes) -> int: nonlocal call_count call_count += 1 if call_count == 1: return 0 # Zero progress return original_write(fd, data) with unittest.mock.patch("os.write", side_effect=zero_write): args = argparse.Namespace( status="failed", operation="remediate", artifact_dir=td, data=json.dumps({ "scope": {"cve_ids": ["CVE-2024-1"]}, }), ) rc = unattended._cmd_make_result(args) self.assertNotEqual(rc, 0) # Old file preserved self.assertEqual(result_path.read_text(), old_content) def test_unicode_error_old_file_read_controlled(self) -> None: """Invalid UTF-8 in old result → controlled error, no crash.""" with tempfile.TemporaryDirectory() as td: result_path = Path(td, "unattended-result.json") result_path.write_bytes(b"\xff\xfe{bad") args = argparse.Namespace( status="failed", operation="remediate", artifact_dir=td, data=json.dumps({ "scope": {"cve_ids": ["CVE-2024-1"]}, }), ) import io from unittest.mock import patch as _patch captured = io.StringIO() with _patch("sys.stderr", captured): rc = unattended._cmd_make_result(args) self.assertNotEqual(rc, 0) # Old file bytes preserved self.assertEqual( result_path.read_bytes(), b"\xff\xfe{bad", ) class TestCommentMetadataCLIRoundtrip(unittest.TestCase): """CLI roundtrip: jira_action comment with metadata projection. Validates the full pipeline: temp request → validate → derive expected_jira_action/expected_tickets → make-result with matching child → stdout/disk exact payload. Uses a script path with spaces and benign shell metacharacters in the comment DATA to prove no shell evaluation occurs (metadata-only test fixture, not live Jira execution proof). """ def test_comment_metadata_roundtrip(self) -> None: import shutil import subprocess with tempfile.TemporaryDirectory() as td: # Script path with spaces spaced_dir = os.path.join(td, "path with spaces", "scripts") os.makedirs(spaced_dir) spaced_script = os.path.join(spaced_dir, "unattended.py") shutil.copy2(str(_SCRIPT), spaced_script) # Temp marker file path as benign DATA value marker_path = os.path.join(td, "MARKER_COMMENT_TEST") comment_text = ( f"CVE fixed. $(touch '{marker_path}') " f"`touch {marker_path}` ; touch {marker_path} " "it's \"verified\" & done\nnewline" ) # 1. Create and validate a jira_action comment request request = { "operation": "jira_action", "jira_action": { "action": "comment", "tickets": ["EDM-100"], "comment": comment_text, }, } req_file = os.path.join(td, "request.json") with open(req_file, "w") as f: json.dump(request, f) r_val = subprocess.run( [sys.executable, "-I", spaced_script, "validate", req_file], capture_output=True, text=True, timeout=10, ) self.assertEqual(r_val.returncode, 0, r_val.stderr) val_out = json.loads(r_val.stdout) self.assertTrue(val_out["valid"]) # 2. Derive EJA and expected_tickets from THAT request expected_jira_action = { "action": request["jira_action"]["action"], "comment": request["jira_action"]["comment"], } expected_tickets = request["jira_action"]["tickets"] # 3. Build matching child fixture child = { "ticket": "EDM-100", "action": "comment", "result": "success", "comment": comment_text, } # 4. make-result with json.dumps ONE --data argument artifact_dir = os.path.join(td, "artifacts") os.makedirs(artifact_dir) data: dict[str, Any] = { "scope": {"cve_ids": ["CVE-2024-1"]}, "jira_results": [child], "expected_tickets": expected_tickets, "expected_jira_action": expected_jira_action, } r_make = subprocess.run( [sys.executable, "-I", spaced_script, "make-result", "--status", "completed", "--operation", "jira_action", "--artifact-dir", artifact_dir, "--data", json.dumps(data)], capture_output=True, text=True, timeout=10, ) self.assertEqual(r_make.returncode, 0, r_make.stderr) # 5. Verify stdout and disk match stdout_result = json.loads(r_make.stdout) with open(os.path.join( artifact_dir, "unattended-result.json", )) as f: disk_result = json.loads(f.read()) self.assertEqual(stdout_result, disk_result) # Exact metadata preserved self.assertEqual( stdout_result["expected_jira_action"], expected_jira_action, ) self.assertEqual( stdout_result["expected_tickets"], expected_tickets, ) self.assertEqual( stdout_result["jira_results"][0]["comment"], comment_text, ) # 6. Assert no marker FILE created (shell injection guard) self.assertFalse( os.path.exists(marker_path), f"Marker file {marker_path} exists — shell " f"evaluation occurred", ) # --------------------------------------------------------------------------- # Round 12: Scoped artifact isolation (F2) # --------------------------------------------------------------------------- class TestDeriveArtifactSubdir(unittest.TestCase): """Artifact subdirectory scoping for work isolation.""" def test_same_cve_different_base_branches_isolated(self) -> None: """Same CVE on different base branches → different artifact subdirs.""" req_main = { "operation": "remediate", "cve_ids": ["CVE-2024-1234"], "repo_path": "org/repo", "base_branch": "main", } req_release = { "operation": "remediate", "cve_ids": ["CVE-2024-1234"], "repo_path": "org/repo", "base_branch": "release-1.0", } ctx = unattended.derive_context(req_main) subdir_main = unattended.derive_artifact_subdir(req_main, ctx) subdir_release = unattended.derive_artifact_subdir(req_release, ctx) # Same primary context label self.assertEqual(ctx, "CVE-2024-1234") # Different artifact subdirectories self.assertNotEqual(subdir_main, subdir_release) # Both use scope-independent label self.assertTrue(subdir_main.startswith("scope-")) self.assertTrue(subdir_release.startswith("scope-")) def test_same_cve_different_repos_isolated(self) -> None: """Same CVE on different repos → different artifact subdirs.""" req_a = { "operation": "remediate", "cve_ids": ["CVE-2024-1234"], "repo_path": "org/repo-a", "base_branch": "main", } req_b = { "operation": "remediate", "cve_ids": ["CVE-2024-1234"], "repo_path": "org/repo-b", "base_branch": "main", } ctx = "CVE-2024-1234" self.assertNotEqual( unattended.derive_artifact_subdir(req_a, ctx), unattended.derive_artifact_subdir(req_b, ctx), ) def test_same_scope_stable_across_reruns(self) -> None: """Same scope → same subdir regardless of runtime params.""" req = { "operation": "remediate", "cve_ids": ["CVE-2024-1234"], "repo_path": "org/repo", "base_branch": "main", "packages": ["pkg-a"], } ctx = "CVE-2024-1234" subdir1 = unattended.derive_artifact_subdir(req, ctx) # Different runtime params, same scope req2 = {**req, "stage": "validate", "max_retries": 5} subdir2 = unattended.derive_artifact_subdir(req2, ctx) self.assertEqual(subdir1, subdir2) def test_reordered_inputs_stable(self) -> None: """Reordered CVE/package lists → same subdir (sorted internally).""" req1 = { "operation": "remediate", "cve_ids": ["CVE-2024-B", "CVE-2024-A"], "packages": ["pkg-z", "pkg-a"], "repo_path": "org/repo", "base_branch": "main", } req2 = { "operation": "remediate", "cve_ids": ["CVE-2024-A", "CVE-2024-B"], "packages": ["pkg-a", "pkg-z"], "repo_path": "org/repo", "base_branch": "main", } ctx = unattended.derive_context(req1) self.assertEqual( unattended.derive_artifact_subdir(req1, ctx), unattended.derive_artifact_subdir(req2, ctx), ) def test_attended_backward_compatible(self) -> None: """Non-operation request → plain context (no scope hash).""" req: dict[str, Any] = {"cve_ids": ["CVE-2024-1234"]} ctx = "CVE-2024-1234" self.assertEqual( unattended.derive_artifact_subdir(req, ctx), "CVE-2024-1234", ) def test_validate_cli_returns_artifact_subdir(self) -> None: """Validate CLI output includes artifact_subdir field.""" with tempfile.NamedTemporaryFile( mode="w", suffix=".json", delete=False ) as f: json.dump({ "operation": "remediate", "cve_ids": ["CVE-2024-9999"], "repo_path": "org/repo", "base_branch": "main", }, f) f.flush() try: import io from unittest.mock import patch as _patch captured = io.StringIO() args = argparse.Namespace(request_file=f.name) with _patch("sys.stdout", captured): rc = unattended._cmd_validate(args) self.assertEqual(rc, 0) output = json.loads(captured.getvalue()) self.assertIn("artifact_subdir", output) self.assertTrue( output["artifact_subdir"].startswith("scope-"), ) self.assertEqual(output["context"], "CVE-2024-9999") # No resolved scope → unbound self.assertFalse(output["artifact_scope_bound"]) finally: os.unlink(f.name) def test_different_operations_different_subdirs(self) -> None: """remediate vs backport for same CVE → different subdirs.""" base = { "cve_ids": ["CVE-2024-1234"], "repo_path": "org/repo", "base_branch": "main", } req_rem = {**base, "operation": "remediate"} req_bp = { **base, "operation": "backport", "target_branches": ["release-1.0"], "pr_url": "https://github.com/org/repo/pull/1", } ctx = "CVE-2024-1234" self.assertNotEqual( unattended.derive_artifact_subdir(req_rem, ctx), unattended.derive_artifact_subdir(req_bp, ctx), ) class TestScopedArtifactCLIFlow(unittest.TestCase): """Full CLI flow: two scopes write independent artifacts.""" def test_two_bases_independent_results(self) -> None: """Same CVE on two bases → separate result files, no mixing.""" import subprocess with tempfile.TemporaryDirectory() as td: artifact_base = os.path.join(td, ".artifacts", "cve-fix") os.makedirs(artifact_base) # Derive artifact subdirs for two scopes req_main = { "operation": "remediate", "cve_ids": ["CVE-2024-1234"], "repo_path": "org/repo", "base_branch": "main", } req_rel = { "operation": "remediate", "cve_ids": ["CVE-2024-1234"], "repo_path": "org/repo", "base_branch": "release-1.0", } ctx = "CVE-2024-1234" subdir_main = unattended.derive_artifact_subdir(req_main, ctx) subdir_rel = unattended.derive_artifact_subdir(req_rel, ctx) self.assertNotEqual(subdir_main, subdir_rel) # Write result for scope 1 (main) dir_main = os.path.join(artifact_base, subdir_main) os.makedirs(dir_main) r1 = subprocess.run( [sys.executable, "-I", str(_SCRIPT), "make-result", "--status", "completed", "--operation", "remediate", "--artifact-dir", dir_main, "--data", json.dumps({ "scope": { "cve_ids": ["CVE-2024-1234"], "repo": "org/repo", "base_branch": "main", }, "stages_completed": ["start", "scan", "patch", "validate", "pr"], "evidence": {"validation_verdict": "PASS"}, "pr": {"url": "https://github.com/org/repo/pull/1", "state": "open"}, })], capture_output=True, text=True, ) self.assertEqual(r1.returncode, 0, r1.stderr) # Write result for scope 2 (release) dir_rel = os.path.join(artifact_base, subdir_rel) os.makedirs(dir_rel) r2 = subprocess.run( [sys.executable, "-I", str(_SCRIPT), "make-result", "--status", "partial", "--operation", "remediate", "--artifact-dir", dir_rel, "--data", json.dumps({ "scope": { "cve_ids": ["CVE-2024-1234"], "repo": "org/repo", "base_branch": "release-1.0", }, "stages_completed": ["start"], "stages_remaining": ["scan", "patch"], })], capture_output=True, text=True, ) self.assertEqual(r2.returncode, 0, r2.stderr) # Verify independent files result1_path = os.path.join(dir_main, "unattended-result.json") result2_path = os.path.join(dir_rel, "unattended-result.json") self.assertTrue(os.path.exists(result1_path)) self.assertTrue(os.path.exists(result2_path)) res1 = json.loads(Path(result1_path).read_text()) res2 = json.loads(Path(result2_path).read_text()) # Different statuses — no mixing self.assertEqual(res1["status"], "completed") self.assertEqual(res2["status"], "partial") # Independent histories (no prior_operations cross-contamination) self.assertEqual(res1.get("prior_operations", []), []) self.assertEqual(res2.get("prior_operations", []), []) # --------------------------------------------------------------------------- # Round 12: UnicodeError in check-scope sibling results (F1) # --------------------------------------------------------------------------- class TestCheckScopeUnicodeError(unittest.TestCase): """Invalid bytes in sibling result → continue, not crash.""" def test_unicode_sibling_no_crash(self) -> None: """check_scope_conflict skips invalid-bytes sibling results.""" with tempfile.TemporaryDirectory() as td: # Create a sibling result with invalid UTF-8 bytes ctx_dir = Path(td) / "EDM-999" ctx_dir.mkdir() (ctx_dir / "unattended-result.json").write_bytes( b'\xff\xfe{"status": "partial"}' ) # Should not crash — returns None (no conflict) result = unattended.check_scope_conflict( "org/repo", "main", ["CVE-2024-1"], ["pkg"], td, ) self.assertIsNone(result) def test_unicode_sibling_does_not_block_real_conflict(self) -> None: """Invalid sibling is skipped; real conflicts still detected.""" with tempfile.TemporaryDirectory() as td: # Bad sibling bad_dir = Path(td) / "BAD-001" bad_dir.mkdir() (bad_dir / "unattended-result.json").write_bytes(b'\xff\xfe') # Good sibling with conflict good_dir = Path(td) / "EDM-200" good_dir.mkdir() (good_dir / "unattended-result.json").write_text(json.dumps({ "status": "partial", "scope": { "repo": "org/repo", "base_branch": "main", "cve_ids": ["CVE-2024-1"], "packages": ["pkg"], }, "pr": { "url": "https://github.com/org/repo/pull/42", "state": "open", }, })) result = unattended.check_scope_conflict( "org/repo", "main", ["CVE-2024-1"], ["pkg"], td, ) self.assertIsNotNone(result) def test_scan_file_unicode_error_sets_invalid(self) -> None: """Invalid UTF-8 in scan-result.json → scan_valid=False.""" with tempfile.TemporaryDirectory() as td: Path(td, "scan-result.json").write_bytes(b'\xff\xfe{bad}') state = unattended.load_state(td) self.assertTrue(state.get("has_scan_file")) self.assertFalse(state.get("scan_valid", True)) # --------------------------------------------------------------------------- # Round 12: check-scope exit code handling (F3) # --------------------------------------------------------------------------- class TestCheckScopeCLIExitCodes(unittest.TestCase): """check-scope CLI returns structured JSON for both exit 0 and exit 1.""" def test_exit_0_no_conflict_has_json(self) -> None: """Exit 0 → parseable JSON with conflict=false.""" with tempfile.TemporaryDirectory() as td: import io from unittest.mock import patch as _patch args = argparse.Namespace( repo="org/repo", base_branch="main", cve_ids="CVE-2024-1", packages="pkg", artifact_base=td, exclude_context=None, ) captured = io.StringIO() with _patch("sys.stdout", captured): rc = unattended._cmd_check_scope(args) self.assertEqual(rc, 0) output = json.loads(captured.getvalue()) self.assertFalse(output["conflict"]) self.assertFalse(output["authoritative"]) def test_exit_1_conflict_has_json_with_overlap_type(self) -> None: """Exit 1 → parseable JSON with conflict details and overlap_type.""" with tempfile.TemporaryDirectory() as td: # Create conflicting result ctx_dir = Path(td) / "EDM-100" ctx_dir.mkdir() (ctx_dir / "unattended-result.json").write_text(json.dumps({ "status": "partial", "scope": { "repo": "org/repo", "base_branch": "main", "cve_ids": ["CVE-2024-1"], "packages": ["pkg"], }, "pr": { "url": "https://github.com/org/repo/pull/1", "state": "open", }, })) import io from unittest.mock import patch as _patch args = argparse.Namespace( repo="org/repo", base_branch="main", cve_ids="CVE-2024-1", packages="pkg", artifact_base=td, exclude_context=None, ) captured = io.StringIO() with _patch("sys.stdout", captured): rc = unattended._cmd_check_scope(args) self.assertEqual(rc, 1) output = json.loads(captured.getvalue()) self.assertTrue(output["conflict"]) self.assertFalse(output["authoritative"]) self.assertIn("overlap_type", output) # --------------------------------------------------------------------------- # Round 12: --stage None handling (F4) # --------------------------------------------------------------------------- class TestLoadStateStageCLI(unittest.TestCase): """load-state CLI with omitted and explicit --stage.""" def test_omitted_stage_autodiscovers(self) -> None: """No --stage → auto-discover resume point.""" with tempfile.TemporaryDirectory() as td: # Create context file → should discover 'scan' as next stage Path(td, "context.md").write_text("# Test context") import io from unittest.mock import patch as _patch args = argparse.Namespace(artifact_dir=td, stage=None) captured = io.StringIO() with _patch("sys.stdout", captured): rc = unattended._cmd_load_state(args) self.assertEqual(rc, 0) output = json.loads(captured.getvalue()) self.assertEqual(output["resume"]["stage"], "scan") self.assertEqual(output["resume"]["status"], "ok") def test_explicit_valid_stage(self) -> None: """Explicit --stage with met prerequisites → ok.""" with tempfile.TemporaryDirectory() as td: Path(td, "context.md").write_text("# Test") import io from unittest.mock import patch as _patch args = argparse.Namespace(artifact_dir=td, stage="scan") captured = io.StringIO() with _patch("sys.stdout", captured): rc = unattended._cmd_load_state(args) self.assertEqual(rc, 0) output = json.loads(captured.getvalue()) self.assertEqual(output["resume"]["stage"], "scan") def test_explicit_missing_prereqs(self) -> None: """Explicit --stage with unmet prereqs → exit 1, needs_input.""" with tempfile.TemporaryDirectory() as td: # No context.md → 'scan' prereqs unmet import io from unittest.mock import patch as _patch args = argparse.Namespace(artifact_dir=td, stage="scan") captured = io.StringIO() with _patch("sys.stdout", captured): rc = unattended._cmd_load_state(args) self.assertEqual(rc, 1) output = json.loads(captured.getvalue()) self.assertEqual(output["resume"]["status"], "needs_input") self.assertIn("has_context", output["resume"]["missing_prereqs"]) # --------------------------------------------------------------------------- # Round 12: Outcome-specific result data (F5) # --------------------------------------------------------------------------- class TestOutcomeSpecificResults(unittest.TestCase): """make-result CLI roundtrips for operation-specific payloads.""" def test_completed_backport_roundtrip(self) -> None: """Completed backport with target_branches and child results.""" with tempfile.TemporaryDirectory() as td: data = { "scope": {"cve_ids": ["CVE-2024-1"]}, "stages_completed": ["backport"], "evidence": {}, "target_branches": ["release-1.0", "release-2.0"], "backport_results": [ { "target_branch": "release-1.0", "status": "completed", "pr": {"url": "https://github.com/o/r/pull/10", "state": "open"}, "evidence": {"tests": "pass"}, }, { "target_branch": "release-2.0", "status": "completed", "pr": {"url": "https://github.com/o/r/pull/11", "state": "open"}, "evidence": {"tests": "pass"}, }, ], } import io from unittest.mock import patch as _patch args = argparse.Namespace( status="completed", operation="backport", artifact_dir=td, data=json.dumps(data), ) captured = io.StringIO() with _patch("sys.stdout", captured): rc = unattended._cmd_make_result(args) self.assertEqual(rc, 0, f"stderr would show error") result = json.loads(captured.getvalue()) self.assertEqual(result["status"], "completed") self.assertEqual( sorted(result["target_branches"]), ["release-1.0", "release-2.0"], ) self.assertEqual(len(result["backport_results"]), 2) def test_jira_comment_roundtrip(self) -> None: """Completed jira_action comment with ticket/action metadata.""" with tempfile.TemporaryDirectory() as td: comment_text = "CVE-2024-1 fixed in PR #42" data = { "scope": {"cve_ids": ["CVE-2024-1"]}, "stages_completed": [], "evidence": {}, "expected_tickets": ["EDM-100"], "expected_jira_action": { "action": "comment", "comment": comment_text, }, "jira_results": [ { "ticket": "EDM-100", "result": "success", "comment": comment_text, }, ], } import io from unittest.mock import patch as _patch args = argparse.Namespace( status="completed", operation="jira_action", artifact_dir=td, data=json.dumps(data), ) captured = io.StringIO() with _patch("sys.stdout", captured): rc = unattended._cmd_make_result(args) self.assertEqual(rc, 0) result = json.loads(captured.getvalue()) self.assertEqual(result["expected_tickets"], ["EDM-100"]) self.assertEqual( result["expected_jira_action"]["comment"], comment_text, ) def test_jira_transition_roundtrip(self) -> None: """Completed jira_action transition with VEX justification.""" with tempfile.TemporaryDirectory() as td: data = { "scope": {"cve_ids": ["CVE-2024-1"]}, "stages_completed": [], "evidence": {}, "expected_tickets": ["EDM-200"], "expected_jira_action": { "action": "transition", "target_status": "MODIFIED", }, "jira_results": [ { "ticket": "EDM-200", "result": "success", }, ], } import io from unittest.mock import patch as _patch args = argparse.Namespace( status="completed", operation="jira_action", artifact_dir=td, data=json.dumps(data), ) captured = io.StringIO() with _patch("sys.stdout", captured): rc = unattended._cmd_make_result(args) self.assertEqual(rc, 0) result = json.loads(captured.getvalue()) self.assertEqual( result["expected_jira_action"]["target_status"], "MODIFIED", ) def test_needs_human_review_with_decisions(self) -> None: """needs_human_review result carries decision requests.""" with tempfile.TemporaryDirectory() as td: data = { "scope": {"cve_ids": ["CVE-2024-1"]}, "stages_completed": ["start", "scan"], "stages_remaining": ["patch"], "evidence": {}, "human_decisions": [ { "decision_type": "close_vex", "decision_id": "d-001", "justification": "Not affected", }, ], } import io from unittest.mock import patch as _patch args = argparse.Namespace( status="needs_human_review", operation="remediate", artifact_dir=td, data=json.dumps(data), ) captured = io.StringIO() with _patch("sys.stdout", captured): rc = unattended._cmd_make_result(args) self.assertEqual(rc, 0) result = json.loads(captured.getvalue()) self.assertEqual(result["status"], "needs_human_review") self.assertEqual(len(result["human_decisions"]), 1) self.assertEqual( result["human_decisions"][0]["decision_type"], "close_vex", ) # --------------------------------------------------------------------------- # Round 12: Merge commit cherry-pick (F6) # --------------------------------------------------------------------------- class TestMergeCommitParentInspection(unittest.TestCase): """Cherry-pick parent detection using real Git fixtures.""" def test_merge_commit_has_two_parents(self) -> None: """A merge commit should be detected by parent count.""" import subprocess with tempfile.TemporaryDirectory() as td: env = {**os.environ, "GIT_AUTHOR_NAME": "Test", "GIT_AUTHOR_EMAIL": "t@t", "GIT_COMMITTER_NAME": "Test", "GIT_COMMITTER_EMAIL": "t@t"} def _run(*args: str) -> subprocess.CompletedProcess[str]: return subprocess.run( list(args), cwd=td, capture_output=True, text=True, env=env, ) _run("git", "init", "-b", "main") _run("git", "config", "user.email", "t@t") _run("git", "config", "user.name", "Test") # Initial commit on main Path(td, "a.txt").write_text("a") _run("git", "add", "a.txt") _run("git", "commit", "-m", "init") # Feature branch _run("git", "checkout", "-b", "feature") Path(td, "b.txt").write_text("b") _run("git", "add", "b.txt") _run("git", "commit", "-m", "feature") feature_sha = _run( "git", "rev-parse", "HEAD" ).stdout.strip() # Merge into main _run("git", "checkout", "main") _run("git", "merge", "--no-ff", "feature", "-m", "Merge feature") merge_sha = _run("git", "rev-parse", "HEAD").stdout.strip() # Verify merge commit parent count cat = _run("git", "cat-file", "-p", merge_sha) parents = [ line for line in cat.stdout.splitlines() if line.startswith("parent ") ] self.assertEqual(len(parents), 2) # Cherry-pick WITHOUT -m should fail on merge commit _run("git", "checkout", "-b", "backport", "HEAD~1") cp_no_m = _run("git", "cherry-pick", "-x", merge_sha) self.assertNotEqual(cp_no_m.returncode, 0) # Cherry-pick WITH -m 1 should succeed _run("git", "cherry-pick", "--abort") cp_m1 = _run("git", "cherry-pick", "-x", "-m", "1", merge_sha) self.assertEqual(cp_m1.returncode, 0, cp_m1.stderr) # Verify the file from the feature branch is present self.assertTrue(Path(td, "b.txt").exists()) def test_regular_commit_one_parent(self) -> None: """A regular (non-merge) commit should cherry-pick without -m.""" import subprocess with tempfile.TemporaryDirectory() as td: env = {**os.environ, "GIT_AUTHOR_NAME": "Test", "GIT_AUTHOR_EMAIL": "t@t", "GIT_COMMITTER_NAME": "Test", "GIT_COMMITTER_EMAIL": "t@t"} def _run(*args: str) -> subprocess.CompletedProcess[str]: return subprocess.run( list(args), cwd=td, capture_output=True, text=True, env=env, ) _run("git", "init", "-b", "main") _run("git", "config", "user.email", "t@t") _run("git", "config", "user.name", "Test") Path(td, "a.txt").write_text("a") _run("git", "add", "a.txt") _run("git", "commit", "-m", "init") Path(td, "b.txt").write_text("b-change") _run("git", "add", "b.txt") _run("git", "commit", "-m", "add b") commit_sha = _run("git", "rev-parse", "HEAD").stdout.strip() # Verify single parent cat = _run("git", "cat-file", "-p", commit_sha) parents = [ line for line in cat.stdout.splitlines() if line.startswith("parent ") ] self.assertEqual(len(parents), 1) # Cherry-pick without -m succeeds for regular commit _run("git", "checkout", "-b", "backport", "HEAD~1") cp = _run("git", "cherry-pick", "-x", commit_sha) self.assertEqual(cp.returncode, 0, cp.stderr) self.assertTrue(Path(td, "b.txt").exists()) # --------------------------------------------------------------------------- # Round 12: Non-Git argv safety (F7) # --------------------------------------------------------------------------- class TestNonGitArgvSafety(unittest.TestCase): """Subprocess argument lists prevent metacharacter injection.""" def test_metacharacter_in_base_branch_safe(self) -> None: """Branch name with shell metacharacters passed as literal arg.""" import subprocess with tempfile.TemporaryDirectory() as td: marker_path = os.path.join(td, "INJECTED") evil_branch = f"release;touch {marker_path}" # check-scope with metacharacter branch — should not create # marker file (the command uses argument lists, not shell) r = subprocess.run( [sys.executable, "-I", str(_SCRIPT), "check-scope", "--repo", "org/repo", "--base-branch", evil_branch, "--cve-ids", "CVE-2024-1", "--packages", "pkg", "--artifact-base", td], capture_output=True, text=True, ) # Should exit 0 (no conflict) — not execute shell self.assertEqual(r.returncode, 0) self.assertFalse( os.path.exists(marker_path), "Marker file created — shell injection occurred", ) def test_json_data_with_metacharacters_safe(self) -> None: """JSON data containing shell metacharacters passes safely.""" import subprocess with tempfile.TemporaryDirectory() as td: marker_path = os.path.join(td, "INJECTED2") evil_data = json.dumps({ "scope": {"cve_ids": [f"CVE;touch {marker_path}"]}, "stages_completed": [], }) r = subprocess.run( [sys.executable, "-I", str(_SCRIPT), "make-result", "--status", "partial", "--operation", "remediate", "--artifact-dir", td, "--data", evil_data], capture_output=True, text=True, ) self.assertEqual(r.returncode, 0, r.stderr) self.assertFalse( os.path.exists(marker_path), "Marker file created — shell injection occurred", ) # --------------------------------------------------------------------------- # Round 12 corrective: Gap A — pr_url/work_ref/jira_action isolation # --------------------------------------------------------------------------- class TestArtifactSubdirPrUrlIsolation(unittest.TestCase): """Same CVE, different pr_url → different artifact subdirs.""" def test_different_pr_urls_isolated(self) -> None: """Two requests with same CVE but different pr_url get separate dirs.""" base = { "operation": "remediate", "cve_ids": ["CVE-2024-1234"], "repo_path": "org/repo", "base_branch": "main", } req_a = {**base, "pr_url": "https://github.com/org/repo/pull/1"} req_b = {**base, "pr_url": "https://github.com/org/repo/pull/2"} ctx = "CVE-2024-1234" subdir_a = unattended.derive_artifact_subdir(req_a, ctx) subdir_b = unattended.derive_artifact_subdir(req_b, ctx) self.assertNotEqual(subdir_a, subdir_b) # Both use scope-independent label self.assertTrue(subdir_a.startswith("scope-")) self.assertTrue(subdir_b.startswith("scope-")) def test_different_work_refs_isolated(self) -> None: """Two requests with same CVE but different work_ref get separate dirs.""" base = { "operation": "remediate", "cve_ids": ["CVE-2024-1234"], } req_a = {**base, "work_ref": "cve-fix/branch-a"} req_b = {**base, "work_ref": "cve-fix/branch-b"} ctx = "CVE-2024-1234" self.assertNotEqual( unattended.derive_artifact_subdir(req_a, ctx), unattended.derive_artifact_subdir(req_b, ctx), ) def test_no_pr_url_vs_with_pr_url_isolated(self) -> None: """A request without pr_url vs with pr_url get separate dirs.""" base = { "operation": "remediate", "cve_ids": ["CVE-2024-1234"], "repo_path": "org/repo", "base_branch": "main", } req_no_pr = base.copy() req_with_pr = { **base, "pr_url": "https://github.com/org/repo/pull/42", } ctx = "CVE-2024-1234" self.assertNotEqual( unattended.derive_artifact_subdir(req_no_pr, ctx), unattended.derive_artifact_subdir(req_with_pr, ctx), ) def test_jira_action_different_actions_isolated(self) -> None: """Same tickets but different jira actions → different subdirs.""" base = { "operation": "jira_action", "jira_tickets": ["EDM-100"], } req_transition = { **base, "jira_action": { "action": "transition", "tickets": ["EDM-100"], "target_status": "MODIFIED", }, } req_comment = { **base, "jira_action": { "action": "comment", "tickets": ["EDM-100"], "comment": "Fixed in PR #42", }, } ctx = "EDM-100" self.assertNotEqual( unattended.derive_artifact_subdir(req_transition, ctx), unattended.derive_artifact_subdir(req_comment, ctx), ) def test_scope_hash_128_bits(self) -> None: """Scope hash is 32 hex chars (128 bits).""" req = { "operation": "remediate", "cve_ids": ["CVE-2024-1234"], "repo_path": "org/repo", "base_branch": "main", } ctx = "CVE-2024-1234" subdir = unattended.derive_artifact_subdir(req, ctx) # Format: scope-{hex32} self.assertTrue(subdir.startswith("scope-")) hex_part = subdir[len("scope-"):] self.assertEqual(len(hex_part), 32) # 128 bits = 32 hex chars class TestScopedArtifactFullPhaseRoundtrip(unittest.TestCase): """Two scopes: full phase artifacts remain independent.""" def test_two_repos_same_cve_full_lifecycle(self) -> None: """Same CVE on two repos → scan+context+result files independent.""" import subprocess with tempfile.TemporaryDirectory() as td: artifact_base = os.path.join(td, ".artifacts", "cve-fix") os.makedirs(artifact_base) req_a = { "operation": "remediate", "cve_ids": ["CVE-2024-5678"], "repo_path": "org/repo-alpha", "base_branch": "main", } req_b = { "operation": "remediate", "cve_ids": ["CVE-2024-5678"], "repo_path": "org/repo-beta", "base_branch": "main", } ctx = "CVE-2024-5678" subdir_a = unattended.derive_artifact_subdir(req_a, ctx) subdir_b = unattended.derive_artifact_subdir(req_b, ctx) self.assertNotEqual(subdir_a, subdir_b) dir_a = os.path.join(artifact_base, subdir_a) dir_b = os.path.join(artifact_base, subdir_b) os.makedirs(dir_a) os.makedirs(dir_b) # Write scan and context files for scope A Path(dir_a, "context.md").write_text("# Repo Alpha\n") Path(dir_a, "scan-result.json").write_text( json.dumps({"verdict": "affected", "repo": "org/repo-alpha"}) ) Path(dir_a, "patch-log.md").write_text("# Patch applied\n") Path(dir_a, "validation-results.md").write_text("# PASS\n") # Write result for scope A r1 = subprocess.run( [sys.executable, "-I", str(_SCRIPT), "make-result", "--status", "completed", "--operation", "remediate", "--artifact-dir", dir_a, "--data", json.dumps({ "scope": {"cve_ids": ["CVE-2024-5678"], "repo": "org/repo-alpha", "base_branch": "main"}, "stages_completed": ["start", "scan", "patch", "validate", "pr"], "evidence": {"validation_verdict": "PASS"}, "pr": {"url": "https://github.com/org/repo-alpha/pull/1", "state": "open"}, })], capture_output=True, text=True, ) self.assertEqual(r1.returncode, 0, r1.stderr) # Write scan and context files for scope B Path(dir_b, "context.md").write_text("# Repo Beta\n") Path(dir_b, "scan-result.json").write_text( json.dumps({"verdict": "affected", "repo": "org/repo-beta"}) ) # Write result for scope B (partial — different state) r2 = subprocess.run( [sys.executable, "-I", str(_SCRIPT), "make-result", "--status", "partial", "--operation", "remediate", "--artifact-dir", dir_b, "--data", json.dumps({ "scope": {"cve_ids": ["CVE-2024-5678"], "repo": "org/repo-beta", "base_branch": "main"}, "stages_completed": ["start", "scan"], "stages_remaining": ["patch", "validate", "pr"], })], capture_output=True, text=True, ) self.assertEqual(r2.returncode, 0, r2.stderr) # Verify files are independent res_a = json.loads( Path(dir_a, "unattended-result.json").read_text() ) res_b = json.loads( Path(dir_b, "unattended-result.json").read_text() ) self.assertEqual(res_a["status"], "completed") self.assertEqual(res_b["status"], "partial") self.assertEqual( res_a["scope"]["repo"], "org/repo-alpha", ) self.assertEqual( res_b["scope"]["repo"], "org/repo-beta", ) # Context files are independent self.assertIn( "Alpha", Path(dir_a, "context.md").read_text(), ) self.assertIn( "Beta", Path(dir_b, "context.md").read_text(), ) # load-state sees correct state for each scope state_a = unattended.load_state(dir_a) state_b = unattended.load_state(dir_b) self.assertTrue(state_a.get("has_patch_file")) self.assertFalse(state_b.get("has_patch_file")) def test_rerun_same_scope_fresh_process_stable(self) -> None: """Same scope from fresh import produces same subdir.""" import subprocess req = { "operation": "remediate", "cve_ids": ["CVE-2024-5002", "CVE-2024-5001"], "repo_path": "org/repo", "base_branch": "main", } # Run via subprocess (fresh process) with tempfile.NamedTemporaryFile( mode="w", suffix=".json", delete=False ) as f: json.dump(req, f) f.flush() try: r = subprocess.run( [sys.executable, "-I", str(_SCRIPT), "validate", f.name], capture_output=True, text=True, ) self.assertEqual(r.returncode, 0, r.stderr) out1 = json.loads(r.stdout) # Run again — should produce same artifact_subdir r2 = subprocess.run( [sys.executable, "-I", str(_SCRIPT), "validate", f.name], capture_output=True, text=True, ) self.assertEqual(r2.returncode, 0, r2.stderr) out2 = json.loads(r2.stdout) self.assertEqual( out1["artifact_subdir"], out2["artifact_subdir"], ) finally: os.unlink(f.name) def test_prior_result_uses_new_scope_path(self) -> None: """Cross-operation prior_result still uses new scope's artifact dir.""" req_a = { "operation": "remediate", "cve_ids": ["CVE-2024-1234"], "repo_path": "org/repo", "base_branch": "main", "pr_url": "https://github.com/org/repo/pull/10", } req_b = { "operation": "remediate", "cve_ids": ["CVE-2024-1234"], "repo_path": "org/repo", "base_branch": "main", "pr_url": "https://github.com/org/repo/pull/20", } ctx = "CVE-2024-1234" # Different PRs → different subdirs even with prior_result self.assertNotEqual( unattended.derive_artifact_subdir(req_a, ctx), unattended.derive_artifact_subdir(req_b, ctx), ) # --------------------------------------------------------------------------- # Round 12 corrective: Gap B — outcome-specific result assembly # --------------------------------------------------------------------------- class TestResultAssemblyDocPattern(unittest.TestCase): """Verify the documented result_data assembly pattern from the skill doc. These tests exercise the same construction pattern documented in unattended.md step 9 — building result_data from the CURRENT validated request and MEASURED outcomes, then passing to make-result CLI. """ def _run_make_result( self, status: str, operation: str, data: dict[str, Any], ) -> tuple[int, dict[str, Any]]: """Run make-result CLI and return (exit_code, parsed_output).""" import io from unittest.mock import patch as _patch with tempfile.TemporaryDirectory() as td: args = argparse.Namespace( status=status, operation=operation, artifact_dir=td, data=json.dumps(data), ) captured = io.StringIO() with _patch("sys.stdout", captured): rc = unattended._cmd_make_result(args) if rc == 0: return rc, json.loads(captured.getvalue()) return rc, {} def test_completed_backport_full_assembly(self) -> None: """Documented backport completion pattern with all required fields.""" # Simulate: current request has target_branches request = { "operation": "backport", "pr_url": "https://github.com/org/repo/pull/42", "target_branches": ["release-2.16", "release-2.15"], } # Measured outcomes: per-branch child results backport_children = [ { "target_branch": "release-2.16", "status": "completed", "pr": {"url": "https://github.com/org/repo/pull/50", "state": "open"}, "evidence": {"tests": "pass", "scan": "clean"}, }, { "target_branch": "release-2.15", "status": "completed", "pr": {"url": "https://github.com/org/repo/pull/51", "state": "open"}, "evidence": {"tests": "pass", "scan": "clean"}, }, ] # Documented assembly pattern: result_data: dict[str, Any] = { "scope": {"cve_ids": ["CVE-2024-1"]}, "stages_completed": ["backport"], "stages_remaining": [], "evidence": {}, # Outcome-specific fields from CURRENT request: "target_branches": request["target_branches"], "backport_results": backport_children, } rc, result = self._run_make_result( "completed", "backport", result_data, ) self.assertEqual(rc, 0) self.assertEqual(result["status"], "completed") self.assertEqual( sorted(result["target_branches"]), ["release-2.15", "release-2.16"], ) self.assertEqual(len(result["backport_results"]), 2) def test_completed_jira_transition_full_assembly(self) -> None: """Documented jira_action transition pattern with VEX.""" request = { "operation": "jira_action", "jira_action": { "action": "transition", "tickets": ["EDM-200", "EDM-201"], "target_status": "MODIFIED", "vex_justification": "component_not_present", }, } # Measured outcomes jira_children = [ { "ticket": "EDM-200", "result": "success", "vex_justification": "component_not_present", "vex_field_verified": True, }, { "ticket": "EDM-201", "result": "already_in_status", "vex_justification": "component_not_present", "vex_field_verified": True, }, ] # Documented assembly: result_data: dict[str, Any] = { "scope": {"cve_ids": ["CVE-2024-1"]}, "stages_completed": [], "stages_remaining": [], "evidence": {}, "expected_tickets": request["jira_action"]["tickets"], "expected_jira_action": { "action": request["jira_action"]["action"], "target_status": request["jira_action"]["target_status"], "vex_justification": request["jira_action"][ "vex_justification" ], }, "jira_results": jira_children, "lifecycle_complete": True, } rc, result = self._run_make_result( "completed", "jira_action", result_data, ) self.assertEqual(rc, 0) self.assertTrue(result["lifecycle_complete"]) self.assertEqual( sorted(result["expected_tickets"]), ["EDM-200", "EDM-201"], ) def test_completed_jira_comment_full_assembly(self) -> None: """Documented jira_action comment pattern.""" comment_text = "CVE-2024-1 remediated in PR #42" request = { "operation": "jira_action", "jira_action": { "action": "comment", "tickets": ["EDM-300"], "comment": comment_text, }, } jira_children = [ { "ticket": "EDM-300", "result": "success", "comment": comment_text, }, ] result_data: dict[str, Any] = { "scope": {"cve_ids": ["CVE-2024-1"]}, "stages_completed": [], "stages_remaining": [], "evidence": {}, "expected_tickets": request["jira_action"]["tickets"], "expected_jira_action": { "action": request["jira_action"]["action"], "comment": request["jira_action"]["comment"], }, "jira_results": jira_children, } rc, result = self._run_make_result( "completed", "jira_action", result_data, ) self.assertEqual(rc, 0) self.assertEqual( result["expected_jira_action"]["comment"], comment_text, ) def test_needs_human_review_with_decision_out(self) -> None: """needs_human_review carries full decision request for handoff.""" decision_out = unattended.make_human_decision_request( decision_id="d-close-vex-001", question="CVE-2024-1 not present — close with VEX?", proposed_justification="component_not_present", evidence="Scan shows CVE absent from dependency tree", scope={"cve_ids": ["CVE-2024-1"], "repo": "org/repo", "base_branch": "main"}, scan_id="scan-abc123", requested_effect="close_vex", permitted_effects=["close_vex", "defer", "reject"], ) result_data: dict[str, Any] = { "scope": {"cve_ids": ["CVE-2024-1"], "repo": "org/repo", "base_branch": "main"}, "stages_completed": ["start", "scan"], "stages_remaining": ["patch", "validate", "pr"], "evidence": {"scan_verdict": "not_affected"}, "human_decisions": [decision_out], } rc, result = self._run_make_result( "needs_human_review", "remediate", result_data, ) self.assertEqual(rc, 0) self.assertEqual(result["status"], "needs_human_review") self.assertEqual(len(result["human_decisions"]), 1) hd = result["human_decisions"][0] # Decision OUT has all fields needed for prior_result handoff self.assertEqual(hd["decision_id"], "d-close-vex-001") self.assertEqual(hd["scan_id"], "scan-abc123") self.assertEqual(hd["scope"]["repo"], "org/repo") self.assertIn("close_vex", hd["permitted_effects"]) def test_partial_error_with_blockers(self) -> None: """Partial result with error and blockers.""" result_data: dict[str, Any] = { "scope": {"cve_ids": ["CVE-2024-1"]}, "stages_completed": ["start", "scan", "patch"], "stages_remaining": ["validate", "pr"], "evidence": {}, "error": "Build failed: dependency conflict", "blockers": [{"type": "build_failure", "detail": "Cannot resolve dep tree"}], } rc, result = self._run_make_result( "partial", "remediate", result_data, ) self.assertEqual(rc, 0) self.assertEqual(result["status"], "partial") self.assertIsNotNone(result["error"]) self.assertEqual(len(result["blockers"]), 1) def test_next_permitted_operations(self) -> None: """Result with next_permitted_operations for downstream.""" result_data: dict[str, Any] = { "scope": {"cve_ids": ["CVE-2024-1"]}, "stages_completed": ["start", "scan", "patch", "validate", "pr"], "stages_remaining": [], "evidence": {"validation_verdict": "PASS"}, "pr": {"url": "https://github.com/org/repo/pull/42", "state": "open"}, "next_permitted_operations": [ {"operation": "backport", "prerequisite": "PR merged"}, {"operation": "jira_action", "prerequisite": "PR merged and backports complete"}, ], } rc, result = self._run_make_result( "completed", "remediate", result_data, ) self.assertEqual(rc, 0) self.assertEqual(len(result["next_permitted_operations"]), 2) self.assertEqual( result["next_permitted_operations"][0]["operation"], "backport", ) # --------------------------------------------------------------------------- # Round 12 corrective: Gap C — bootstrap control consumer tests # --------------------------------------------------------------------------- class TestBootstrapControlConsumer(unittest.TestCase): """Consumer patterns: load-state/check-scope stop before phase callback.""" def test_load_state_needs_input_stops_before_phase(self) -> None: """When load-state returns needs_input, consumer must not proceed.""" import subprocess with tempfile.TemporaryDirectory() as td: # No context.md → 'scan' prereqs unmet r = subprocess.run( [sys.executable, "-I", str(_SCRIPT), "load-state", td, "--stage", "scan"], capture_output=True, text=True, ) self.assertEqual(r.returncode, 1) output = json.loads(r.stdout) resume = output.get("resume", {}) self.assertEqual(resume["status"], "needs_input") # Consumer pattern: check returncode AND status before proceeding # Phase callback must NOT be called phase_called = False if r.returncode == 0 and resume.get("status") == "ok": phase_called = True # pragma: no cover self.assertFalse(phase_called) def test_load_state_ok_allows_phase(self) -> None: """When load-state returns ok, consumer may proceed.""" import subprocess with tempfile.TemporaryDirectory() as td: Path(td, "context.md").write_text("# Test") r = subprocess.run( [sys.executable, "-I", str(_SCRIPT), "load-state", td, "--stage", "scan"], capture_output=True, text=True, ) self.assertEqual(r.returncode, 0) output = json.loads(r.stdout) self.assertEqual(output["resume"]["status"], "ok") def test_load_state_malformed_result_exit_1(self) -> None: """Malformed result.json → exit 1 with needs_input.""" import subprocess with tempfile.TemporaryDirectory() as td: Path(td, "unattended-result.json").write_text("not json{") r = subprocess.run( [sys.executable, "-I", str(_SCRIPT), "load-state", td], capture_output=True, text=True, ) self.assertEqual(r.returncode, 1) output = json.loads(r.stdout) self.assertTrue(output.get("result_malformed")) self.assertEqual(output["resume"]["status"], "needs_input") def test_load_state_exit_2_on_invalid_args(self) -> None: """Missing artifact_dir argument → exit 2 (argparse error).""" import subprocess r = subprocess.run( [sys.executable, "-I", str(_SCRIPT), "load-state"], capture_output=True, text=True, ) self.assertEqual(r.returncode, 2) def test_check_scope_consumer_no_check_true(self) -> None: """check-scope consumer pattern: capture_output, inspect JSON.""" import subprocess with tempfile.TemporaryDirectory() as td: # Create overlapping result ctx_dir = Path(td) / "EDM-500" ctx_dir.mkdir() (ctx_dir / "unattended-result.json").write_text(json.dumps({ "status": "partial", "scope": { "repo": "org/repo", "base_branch": "main", "cve_ids": ["CVE-2024-1"], }, "pr": { "url": "https://github.com/org/repo/pull/99", "state": "open", }, })) # Consumer pattern: capture_output (no check=True) r = subprocess.run( [sys.executable, "-I", str(_SCRIPT), "check-scope", "--repo", "org/repo", "--base-branch", "main", "--cve-ids", "CVE-2024-1", "--packages", "", "--artifact-base", td], capture_output=True, text=True, ) # Exit 1 = local discovery, NOT a fatal error self.assertIn(r.returncode, (0, 1)) if r.returncode == 1: output = json.loads(r.stdout) self.assertTrue(output["conflict"]) self.assertFalse(output["authoritative"]) # Consumer: must still do live PR check before blocking def test_check_scope_package_warning_vs_cve_overlap(self) -> None: """Package-only warning (exit 1) vs CVE overlap (exit 1): both 0/1.""" import subprocess with tempfile.TemporaryDirectory() as td: # Create result with package-only overlap (no CVE match) ctx_dir = Path(td) / "EDM-600" ctx_dir.mkdir() (ctx_dir / "unattended-result.json").write_text(json.dumps({ "status": "partial", "scope": { "repo": "org/repo", "base_branch": "main", "cve_ids": ["CVE-2024-OTHER"], "packages": ["shared-pkg"], }, "pr": { "url": "https://github.com/org/repo/pull/88", "state": "open", }, })) r = subprocess.run( [sys.executable, "-I", str(_SCRIPT), "check-scope", "--repo", "org/repo", "--base-branch", "main", "--cve-ids", "CVE-2024-MINE", "--packages", "shared-pkg", "--artifact-base", td], capture_output=True, text=True, ) self.assertIn(r.returncode, (0, 1)) if r.returncode == 1: output = json.loads(r.stdout) self.assertEqual( output.get("overlap_type"), "same_package_warning", ) def test_omitted_stage_valid_auto_discover(self) -> None: """Omitted --stage lets helper auto-discover resume point.""" import subprocess with tempfile.TemporaryDirectory() as td: Path(td, "context.md").write_text("# Test") Path(td, "scan-result.json").write_text( json.dumps({"verdict": "affected"}) ) # Consumer: omit --stage cmd = [sys.executable, "-I", str(_SCRIPT), "load-state", td] r = subprocess.run(cmd, capture_output=True, text=True) self.assertEqual(r.returncode, 0) output = json.loads(r.stdout) self.assertEqual(output["resume"]["stage"], "patch") self.assertEqual(output["resume"]["status"], "ok") def test_check_scope_exit0_then_live_clear(self) -> None: """Exit 0 (no local conflict) still requires live PR callback.""" import subprocess with tempfile.TemporaryDirectory() as td: r = subprocess.run( [sys.executable, "-I", str(_SCRIPT), "check-scope", "--repo", "org/repo", "--base-branch", "main", "--cve-ids", "CVE-2024-NEW", "--packages", "", "--artifact-base", td], capture_output=True, text=True, ) self.assertEqual(r.returncode, 0) output = json.loads(r.stdout) self.assertFalse(output["conflict"]) self.assertFalse(output["authoritative"]) # Consumer: live check still required per 'note' self.assertIn("Live host check", output["note"]) def test_check_scope_exit1_conflict_callback_order(self) -> None: """Exit 1 (local conflict) must parse JSON before live callback.""" import subprocess with tempfile.TemporaryDirectory() as td: ctx_dir = Path(td) / "EDM-700" ctx_dir.mkdir() (ctx_dir / "unattended-result.json").write_text(json.dumps({ "status": "partial", "scope": { "repo": "org/repo", "base_branch": "main", "cve_ids": ["CVE-2024-1"], }, "pr": {"url": "https://github.com/org/repo/pull/1", "state": "open"}, })) r = subprocess.run( [sys.executable, "-I", str(_SCRIPT), "check-scope", "--repo", "org/repo", "--base-branch", "main", "--cve-ids", "CVE-2024-1", "--packages", "", "--artifact-base", td], capture_output=True, text=True, ) self.assertEqual(r.returncode, 1) # Must produce valid JSON for consumer to parse output = json.loads(r.stdout) self.assertTrue(output["conflict"]) # Consumer: must still complete live PR check self.assertFalse(output["authoritative"]) def test_check_scope_unexpected_exit_fails_closed(self) -> None: """Unexpected exit code (not 0 or 1) → consumer fails closed.""" import subprocess # Missing required --repo → argparse exits 2 r = subprocess.run( [sys.executable, "-I", str(_SCRIPT), "check-scope", "--base-branch", "main", "--cve-ids", "CVE-2024-1"], capture_output=True, text=True, ) self.assertEqual(r.returncode, 2) # Consumer pattern: returncode not in (0, 1) → fail closed self.assertNotIn(r.returncode, (0, 1)) def test_load_state_needs_input_bad_result_gates_phase(self) -> None: """Malformed result + requested stage → needs_input, blocks phase.""" import subprocess with tempfile.TemporaryDirectory() as td: # Malformed result (non-dict JSON) Path(td, "unattended-result.json").write_text("[]") Path(td, "context.md").write_text("# Test") r = subprocess.run( [sys.executable, "-I", str(_SCRIPT), "load-state", td, "--stage", "scan"], capture_output=True, text=True, ) self.assertEqual(r.returncode, 1) output = json.loads(r.stdout) self.assertTrue(output.get("result_malformed")) self.assertEqual(output["resume"]["status"], "needs_input") def test_cli_base_contradiction_exit1(self) -> None: """Request base disagrees with resolved scope → exit 1.""" import subprocess req = { "operation": "remediate", "cve_ids": ["CVE-2024-1234"], "base_branch": "main", } with tempfile.TemporaryDirectory() as td: req_file = os.path.join(td, "request.json") with open(req_file, "w") as f: json.dump(req, f) r = subprocess.run( [sys.executable, "-I", str(_SCRIPT), "validate", req_file, "--resolved-scope", json.dumps({ "repo_path": "org/repo", "base_branch": "release-1.0", })], capture_output=True, text=True, ) self.assertEqual(r.returncode, 1) output = json.loads(r.stdout) self.assertFalse(output["valid"]) self.assertTrue( any("contradicts" in e for e in output.get("errors", [])), ) def test_cli_artifact_scope_bound_flag(self) -> None: """CLI output includes artifact_scope_bound reflecting binding.""" import subprocess req = { "operation": "remediate", "cve_ids": ["CVE-2024-1234"], } with tempfile.TemporaryDirectory() as td: req_file = os.path.join(td, "request.json") with open(req_file, "w") as f: json.dump(req, f) # Without --resolved-scope: unbound r1 = subprocess.run( [sys.executable, "-I", str(_SCRIPT), "validate", req_file], capture_output=True, text=True, ) self.assertEqual(r1.returncode, 0, r1.stderr) out1 = json.loads(r1.stdout) self.assertFalse(out1["artifact_scope_bound"]) # With --resolved-scope: bound r2 = subprocess.run( [sys.executable, "-I", str(_SCRIPT), "validate", req_file, "--resolved-scope", json.dumps({ "repo_path": "org/repo", "base_branch": "main", })], capture_output=True, text=True, ) self.assertEqual(r2.returncode, 0, r2.stderr) out2 = json.loads(r2.stdout) self.assertTrue(out2["artifact_scope_bound"]) # --------------------------------------------------------------------------- # Round 12 corrective: Gap D — merge commit + safety deterministic fixtures # --------------------------------------------------------------------------- class TestMergeCommitDeterministic(unittest.TestCase): """Cherry-pick parent selection with checked setup.""" def _git_run( self, *args: str, cwd: str, ) -> "subprocess.CompletedProcess[str]": """Run git command with checked result.""" import subprocess env = { **os.environ, "GIT_AUTHOR_NAME": "Test", "GIT_AUTHOR_EMAIL": "t@t", "GIT_COMMITTER_NAME": "Test", "GIT_COMMITTER_EMAIL": "t@t", } r = subprocess.run( ["git"] + list(args), cwd=cwd, capture_output=True, text=True, env=env, ) self.assertEqual( r.returncode, 0, f"git {' '.join(args)} failed: {r.stderr}", ) return r def test_merge_parent_1_is_mainline(self) -> None: """Verify parent 1 of a merge commit is the base (mainline).""" with tempfile.TemporaryDirectory() as td: self._git_run("init", "-b", "main", cwd=td) self._git_run("config", "user.email", "t@t", cwd=td) self._git_run("config", "user.name", "Test", cwd=td) # Initial commit on main Path(td, "a.txt").write_text("a") self._git_run("add", "a.txt", cwd=td) self._git_run("commit", "-m", "init", cwd=td) main_sha = self._git_run( "rev-parse", "HEAD", cwd=td ).stdout.strip() # Feature branch self._git_run("checkout", "-b", "feature", cwd=td) Path(td, "b.txt").write_text("b") self._git_run("add", "b.txt", cwd=td) self._git_run("commit", "-m", "feature", cwd=td) # Merge into main self._git_run("checkout", "main", cwd=td) self._git_run( "merge", "--no-ff", "feature", "-m", "Merge feature", cwd=td, ) merge_sha = self._git_run( "rev-parse", "HEAD", cwd=td, ).stdout.strip() # Verify parent count and parent 1 identity cat = self._git_run("cat-file", "-p", merge_sha, cwd=td) parents = [ line.split()[1] for line in cat.stdout.splitlines() if line.startswith("parent ") ] self.assertEqual(len(parents), 2) # Parent 1 (first parent) is the mainline commit self.assertEqual(parents[0], main_sha) # Cherry-pick with -m 1 on a backport branch self._git_run( "checkout", "-b", "backport", main_sha, cwd=td, ) self._git_run( "cherry-pick", "-x", "-m", "1", merge_sha, cwd=td, ) self.assertTrue(Path(td, "b.txt").exists()) def test_unsupported_initial_commit_no_parents(self) -> None: """Initial commit (0 parents) — cherry-pick should be blocked.""" with tempfile.TemporaryDirectory() as td: self._git_run("init", "-b", "main", cwd=td) self._git_run("config", "user.email", "t@t", cwd=td) self._git_run("config", "user.name", "Test", cwd=td) Path(td, "a.txt").write_text("a") self._git_run("add", "a.txt", cwd=td) self._git_run("commit", "-m", "init", cwd=td) init_sha = self._git_run( "rev-parse", "HEAD", cwd=td, ).stdout.strip() cat = self._git_run("cat-file", "-p", init_sha, cwd=td) parents = [ line for line in cat.stdout.splitlines() if line.startswith("parent ") ] # Initial commit has 0 parents — blocked by skill rules self.assertEqual(len(parents), 0) class TestNonGitArgvStubForge(unittest.TestCase): """Non-Git argv safety with a stub forge executable.""" def test_forge_executable_receives_literal_branch(self) -> None: """Stub forge executable logs received argv — branch is literal.""" import subprocess import stat with tempfile.TemporaryDirectory() as td: # Create stub forge executable that logs received argv log_file = os.path.join(td, "argv.log") stub = os.path.join(td, "gh-stub") Path(stub).write_text( "#!/bin/sh\n" f'echo "$@" >> "{log_file}"\n' ) os.chmod(stub, os.stat(stub).st_mode | stat.S_IEXEC) # Valid Git branch name with shell metacharacters (allowed by Git) branch = "release;echo-INJECTED" ok, reason = unattended.validate_branch_name(branch) # This is a valid Git branch name (no control chars, no spaces) self.assertTrue(ok, reason) # Simulate the documented subprocess pattern r = subprocess.run( [stub, "pr", "list", "--state", "open", "--base", branch, "--search", "CVE-2024-1"], capture_output=True, text=True, ) self.assertEqual(r.returncode, 0) # Verify the stub received the branch as one literal arg logged = Path(log_file).read_text().strip() # The branch value must appear as a literal, not executed self.assertIn(branch, logged) # No injection marker file marker = os.path.join(td, "INJECTED") self.assertFalse(os.path.exists(marker)) def test_forge_receives_one_base_value(self) -> None: """Forge --base gets exactly the branch value, not split args.""" import subprocess import stat with tempfile.TemporaryDirectory() as td: # Create a script that writes each arg on a separate line log_file = os.path.join(td, "args.log") stub = os.path.join(td, "forge-stub") Path(stub).write_text( "#!/bin/sh\n" f'for arg in "$@"; do echo "$arg"; done >> "{log_file}"\n' ) os.chmod(stub, os.stat(stub).st_mode | stat.S_IEXEC) branch = "feature/$var" r = subprocess.run( [stub, "pr", "list", "--base", branch], capture_output=True, text=True, ) self.assertEqual(r.returncode, 0) # Verify --base is followed by exactly the branch value lines = Path(log_file).read_text().strip().split("\n") base_idx = lines.index("--base") self.assertEqual(lines[base_idx + 1], branch) # --------------------------------------------------------------------------- # Resolved scope — artifact path isolation via --resolved-scope # --------------------------------------------------------------------------- class TestResolvedScopeIsolation(unittest.TestCase): """Verify --resolved-scope binds checkout identity into artifact subdir. Root finding 4498376916: requests for the same CVE on different checkouts that omit repo_path/base_branch from the request must produce distinct artifact paths when resolved_scope differs. """ def test_same_cve_no_request_repo_different_resolved_scope(self) -> None: """Two CVE-only requests with different resolved_scope → distinct subdirs.""" req = {"operation": "remediate", "cve_ids": ["CVE-2024-1234"]} ctx = unattended.derive_context(req) sub_a = unattended.derive_artifact_subdir( req, ctx, resolved_scope={"repo_path": "org/repo-a", "base_branch": "main"}, ) sub_b = unattended.derive_artifact_subdir( req, ctx, resolved_scope={"repo_path": "org/repo-b", "base_branch": "release-1.0"}, ) # Both use scope-independent label self.assertTrue(sub_a.startswith("scope-")) self.assertTrue(sub_b.startswith("scope-")) # But produce distinct artifact paths self.assertNotEqual(sub_a, sub_b) def test_same_resolved_scope_stable_across_reruns(self) -> None: """Same CVE + same resolved_scope → identical subdir (stable).""" req = {"operation": "remediate", "cve_ids": ["CVE-2024-1234"]} ctx = unattended.derive_context(req) rs = {"repo_path": "org/repo", "base_branch": "main"} sub1 = unattended.derive_artifact_subdir(req, ctx, resolved_scope=rs) sub2 = unattended.derive_artifact_subdir(req, ctx, resolved_scope=rs) self.assertEqual(sub1, sub2) def test_explicit_base_contradiction_rejected(self) -> None: """Request base_branch disagrees with resolved → ValueError.""" req = { "operation": "remediate", "cve_ids": ["CVE-2024-1234"], "base_branch": "main", } ctx = unattended.derive_context(req) with self.assertRaises(ValueError) as cm: unattended.derive_artifact_subdir( req, ctx, resolved_scope={"repo_path": "org/repo", "base_branch": "dev"}, ) self.assertIn("contradicts", str(cm.exception)) def test_resolved_repo_canonical_overrides_request(self) -> None: """Resolved repo_path is canonical identity, overrides request.""" req = { "operation": "remediate", "cve_ids": ["CVE-2024-1234"], "repo_path": ".", "base_branch": "main", } ctx = unattended.derive_context(req) # Resolved scope provides canonical repo identity sub_resolved = unattended.derive_artifact_subdir( req, ctx, resolved_scope={"repo_path": "org/repo", "base_branch": "main"}, ) # Same request with different resolved repo → different hash sub_other = unattended.derive_artifact_subdir( req, ctx, resolved_scope={"repo_path": "org/other", "base_branch": "main"}, ) self.assertNotEqual(sub_resolved, sub_other) def test_repo_alias_canonicalization(self) -> None: """Two requests with repo_path='.' but different resolved repos differ.""" req_a = { "operation": "remediate", "cve_ids": ["CVE-2024-1234"], "repo_path": ".", "base_branch": "main", } req_b = {**req_a} # identical request ctx = unattended.derive_context(req_a) # Same request, different resolved identity → different subdirs sub_a = unattended.derive_artifact_subdir( req_a, ctx, resolved_scope={"repo_path": "org/repo-a", "base_branch": "main"}, ) sub_b = unattended.derive_artifact_subdir( req_b, ctx, resolved_scope={"repo_path": "org/repo-b", "base_branch": "main"}, ) self.assertNotEqual(sub_a, sub_b) def test_resolved_scope_only_repo_partial(self) -> None: """Resolved scope with only repo_path (no base_branch) still isolates.""" req = {"operation": "remediate", "cve_ids": ["CVE-2024-5678"]} ctx = unattended.derive_context(req) sub_no_rs = unattended.derive_artifact_subdir(req, ctx) sub_repo_only = unattended.derive_artifact_subdir( req, ctx, resolved_scope={"repo_path": "org/repo"}, ) self.assertNotEqual(sub_no_rs, sub_repo_only) def test_resolved_scope_none_is_harmless(self) -> None: """resolved_scope=None behaves like omission (backward compat).""" req = {"operation": "remediate", "cve_ids": ["CVE-2024-1234"]} ctx = unattended.derive_context(req) sub_none = unattended.derive_artifact_subdir(req, ctx, resolved_scope=None) sub_omit = unattended.derive_artifact_subdir(req, ctx) self.assertEqual(sub_none, sub_omit) def test_resolved_scope_non_dict_raises(self) -> None: """Non-dict resolved_scope (not None) raises ValueError.""" req = {"operation": "remediate", "cve_ids": ["CVE-2024-1234"]} ctx = unattended.derive_context(req) with self.assertRaises(ValueError) as cm: unattended.derive_artifact_subdir( req, ctx, resolved_scope="not-a-dict", ) self.assertIn("dict or None", str(cm.exception)) def test_resolved_scope_empty_dict_no_change(self) -> None: """Empty resolved_scope dict has no effect.""" req = {"operation": "remediate", "cve_ids": ["CVE-2024-1234"]} ctx = unattended.derive_context(req) sub_empty = unattended.derive_artifact_subdir(req, ctx, resolved_scope={}) sub_omit = unattended.derive_artifact_subdir(req, ctx) self.assertEqual(sub_empty, sub_omit) class TestResolvedScopeCLI(unittest.TestCase): """Validate CLI --resolved-scope integration.""" def test_cli_with_resolved_scope_isolates(self) -> None: """CLI validate with --resolved-scope changes artifact_subdir.""" import io from unittest.mock import patch as _patch req = {"operation": "remediate", "cve_ids": ["CVE-2024-8888"]} with tempfile.NamedTemporaryFile( mode="w", suffix=".json", delete=False ) as f: json.dump(req, f) f.flush() try: # Without resolved scope cap1 = io.StringIO() args1 = argparse.Namespace( request_file=f.name, resolved_scope=None, ) with _patch("sys.stdout", cap1): rc1 = unattended._cmd_validate(args1) self.assertEqual(rc1, 0) out1 = json.loads(cap1.getvalue()) # With resolved scope cap2 = io.StringIO() rs = json.dumps({"repo_path": "org/repo", "base_branch": "main"}) args2 = argparse.Namespace( request_file=f.name, resolved_scope=rs, ) with _patch("sys.stdout", cap2): rc2 = unattended._cmd_validate(args2) self.assertEqual(rc2, 0) out2 = json.loads(cap2.getvalue()) # Same context label self.assertEqual(out1["context"], out2["context"]) # Different artifact subdirs self.assertNotEqual( out1["artifact_subdir"], out2["artifact_subdir"], ) # Without resolved scope: unbound self.assertFalse(out1["artifact_scope_bound"]) # With resolved scope: bound self.assertTrue(out2["artifact_scope_bound"]) finally: os.unlink(f.name) def test_cli_resolved_scope_invalid_json_exit2(self) -> None: """Invalid JSON in --resolved-scope → exit 2.""" import io from unittest.mock import patch as _patch req = {"operation": "remediate", "cve_ids": ["CVE-2024-1234"]} with tempfile.NamedTemporaryFile( mode="w", suffix=".json", delete=False ) as f: json.dump(req, f) f.flush() try: cap = io.StringIO() args = argparse.Namespace( request_file=f.name, resolved_scope="{bad json", ) with _patch("sys.stderr", io.StringIO()): with _patch("sys.stdout", cap): rc = unattended._cmd_validate(args) self.assertEqual(rc, 2) finally: os.unlink(f.name) def test_cli_resolved_scope_not_dict_exit2(self) -> None: """Non-dict --resolved-scope → exit 2.""" import io from unittest.mock import patch as _patch req = {"operation": "remediate", "cve_ids": ["CVE-2024-1234"]} with tempfile.NamedTemporaryFile( mode="w", suffix=".json", delete=False ) as f: json.dump(req, f) f.flush() try: cap = io.StringIO() args = argparse.Namespace( request_file=f.name, resolved_scope=json.dumps(["a list"]), ) with _patch("sys.stderr", io.StringIO()): with _patch("sys.stdout", cap): rc = unattended._cmd_validate(args) self.assertEqual(rc, 2) finally: os.unlink(f.name) def test_cli_two_checkouts_full_flow(self) -> None: """Full CLI flow: same CVE, two resolved scopes → independent artifacts.""" import subprocess req = {"operation": "remediate", "cve_ids": ["CVE-2024-7777"]} with tempfile.TemporaryDirectory() as td: req_file = os.path.join(td, "request.json") with open(req_file, "w") as f: json.dump(req, f) # Validate for checkout A r1 = subprocess.run( [sys.executable, "-I", str(_SCRIPT), "validate", req_file, "--resolved-scope", json.dumps({ "repo_path": "org/repo-a", "base_branch": "main", })], capture_output=True, text=True, ) self.assertEqual(r1.returncode, 0, r1.stderr) out1 = json.loads(r1.stdout) # Validate for checkout B r2 = subprocess.run( [sys.executable, "-I", str(_SCRIPT), "validate", req_file, "--resolved-scope", json.dumps({ "repo_path": "org/repo-b", "base_branch": "release-1.0", })], capture_output=True, text=True, ) self.assertEqual(r2.returncode, 0, r2.stderr) out2 = json.loads(r2.stdout) # Same CVE context label self.assertEqual(out1["context"], "CVE-2024-7777") self.assertEqual(out2["context"], "CVE-2024-7777") # Different artifact subdirectories self.assertNotEqual( out1["artifact_subdir"], out2["artifact_subdir"], ) # Write independent results artifact_base = os.path.join(td, ".artifacts", "cve-fix") dir_a = os.path.join(artifact_base, out1["artifact_subdir"]) dir_b = os.path.join(artifact_base, out2["artifact_subdir"]) os.makedirs(dir_a) os.makedirs(dir_b) for label, d, repo, base in [ ("A", dir_a, "org/repo-a", "main"), ("B", dir_b, "org/repo-b", "release-1.0"), ]: r = subprocess.run( [sys.executable, "-I", str(_SCRIPT), "make-result", "--status", "completed", "--operation", "remediate", "--artifact-dir", d, "--data", json.dumps({ "scope": { "cve_ids": ["CVE-2024-7777"], "repo": repo, "base_branch": base, }, "stages_completed": [ "start", "scan", "patch", "validate", "pr", ], "evidence": {"validation_verdict": "PASS"}, "pr": { "url": f"https://github.com/{repo}/pull/1", "state": "open", }, })], capture_output=True, text=True, ) self.assertEqual(r.returncode, 0, f"{label}: {r.stderr}") # Verify independent result files res_a = json.loads( Path(dir_a, "unattended-result.json").read_text() ) res_b = json.loads( Path(dir_b, "unattended-result.json").read_text() ) self.assertEqual( res_a["scope"]["repo"], "org/repo-a", ) self.assertEqual( res_b["scope"]["repo"], "org/repo-b", ) self.assertNotEqual(res_a["scope"], res_b["scope"]) def test_cli_rerun_same_resolved_scope_stable(self) -> None: """Same CVE + same resolved scope → same artifact_subdir on rerun.""" import io from unittest.mock import patch as _patch req = {"operation": "remediate", "cve_ids": ["CVE-2024-3333"]} rs = json.dumps({"repo_path": "org/repo", "base_branch": "main"}) with tempfile.NamedTemporaryFile( mode="w", suffix=".json", delete=False ) as f: json.dump(req, f) f.flush() try: subdirs = [] for _ in range(2): cap = io.StringIO() args = argparse.Namespace( request_file=f.name, resolved_scope=rs, ) with _patch("sys.stdout", cap): rc = unattended._cmd_validate(args) self.assertEqual(rc, 0) out = json.loads(cap.getvalue()) subdirs.append(out["artifact_subdir"]) self.assertEqual(subdirs[0], subdirs[1]) finally: os.unlink(f.name) # --------------------------------------------------------------------------- # Round 12 corrective: Root 3 — artifact_scope_bound identity validation # --------------------------------------------------------------------------- class TestArtifactScopeBoundIdentity(unittest.TestCase): """scope_bound requires both nonblank repo_path AND base_branch.""" def _validate_cli( self, req: dict[str, Any], resolved: dict[str, Any] | None, ) -> dict[str, Any]: import io from unittest.mock import patch as _patch with tempfile.NamedTemporaryFile( mode="w", suffix=".json", delete=False, ) as f: json.dump(req, f) f.flush() try: rs_str = json.dumps(resolved) if resolved else None args = argparse.Namespace( request_file=f.name, resolved_scope=rs_str, ) cap = io.StringIO() with _patch("sys.stdout", cap): rc = unattended._cmd_validate(args) self.assertEqual(rc, 0, cap.getvalue()) return json.loads(cap.getvalue()) finally: os.unlink(f.name) def test_dummy_dict_not_scope_bound(self) -> None: """A nonempty dict without repo_path/base_branch is NOT bound.""" req = {"operation": "remediate", "cve_ids": ["CVE-2024-1"]} out = self._validate_cli(req, {"dummy": 1}) self.assertFalse(out["artifact_scope_bound"]) def test_missing_base_branch_not_bound(self) -> None: """resolved_scope with only repo_path (no base_branch) is NOT bound.""" req = {"operation": "remediate", "cve_ids": ["CVE-2024-1"]} out = self._validate_cli(req, {"repo_path": "org/repo"}) self.assertFalse(out["artifact_scope_bound"]) def test_blank_repo_not_bound(self) -> None: """resolved_scope with blank repo_path is NOT bound.""" req = {"operation": "remediate", "cve_ids": ["CVE-2024-1"]} out = self._validate_cli(req, {"repo_path": " ", "base_branch": "main"}) self.assertFalse(out["artifact_scope_bound"]) def test_both_fields_is_bound(self) -> None: """resolved_scope with both repo_path and base_branch IS bound.""" req = {"operation": "remediate", "cve_ids": ["CVE-2024-1"]} out = self._validate_cli( req, {"repo_path": "org/repo", "base_branch": "main"}, ) self.assertTrue(out["artifact_scope_bound"]) # --------------------------------------------------------------------------- # Round 12 corrective: Root 1 — assemble_result_data round-trip # --------------------------------------------------------------------------- class TestAssembleResultDataRoundtrip(unittest.TestCase): """assemble_result_data() → make-result CLI round-trip.""" def _run_make_result( self, status: str, operation: str, data: dict[str, Any], ) -> tuple[int, dict[str, Any]]: import io from unittest.mock import patch as _patch with tempfile.TemporaryDirectory() as td: args = argparse.Namespace( status=status, operation=operation, artifact_dir=td, data=json.dumps(data), ) cap = io.StringIO() with _patch("sys.stdout", cap): rc = unattended._cmd_make_result(args) if rc == 0: return rc, json.loads(cap.getvalue()) return rc, {} def test_backport_assembly_accepted(self) -> None: """assemble_result_data for completed backport passes make-result.""" request = { "operation": "backport", "pr_url": "https://github.com/org/repo/pull/42", "target_branches": ["release-2.16"], } data = unattended.assemble_result_data( request=request, status="completed", scope={"cve_ids": ["CVE-2024-1"]}, stages_completed=["backport"], stages_remaining=[], evidence={}, backport_results=[{ "target_branch": "release-2.16", "status": "completed", "pr": {"url": "https://github.com/org/repo/pull/50", "state": "open"}, "evidence": {"tests": "pass"}, }], ) rc, result = self._run_make_result("completed", "backport", data) self.assertEqual(rc, 0) self.assertEqual(result["target_branches"], ["release-2.16"]) def test_jira_comment_assembly_accepted(self) -> None: """assemble_result_data for jira_action comment passes make-result.""" comment_text = "CVE-2024-1 fixed in PR #42" request = { "operation": "jira_action", "jira_action": { "action": "comment", "tickets": ["EDM-100"], "comment": comment_text, }, } data = unattended.assemble_result_data( request=request, status="completed", scope={"cve_ids": ["CVE-2024-1"]}, stages_completed=[], stages_remaining=[], evidence={}, jira_results=[{ "ticket": "EDM-100", "result": "success", "comment": comment_text, }], ) rc, result = self._run_make_result( "completed", "jira_action", data, ) self.assertEqual(rc, 0) self.assertEqual(result["expected_tickets"], ["EDM-100"]) self.assertEqual( result["expected_jira_action"]["comment"], comment_text, ) def test_needs_human_review_assembly(self) -> None: """assemble_result_data for needs_human_review carries decision OUT.""" decision_out = unattended.make_human_decision_request( decision_id="d-001", question="Close with VEX?", scope={"cve_ids": ["CVE-2024-1"]}, scan_id="scan-x", requested_effect="close_vex", permitted_effects=["close_vex", "reject"], ) request = {"operation": "remediate", "cve_ids": ["CVE-2024-1"]} data = unattended.assemble_result_data( request=request, status="needs_human_review", scope={"cve_ids": ["CVE-2024-1"]}, stages_completed=["start", "scan"], stages_remaining=["patch", "validate", "pr"], evidence={"scan_verdict": "not_affected"}, human_decisions=[decision_out], ) rc, result = self._run_make_result( "needs_human_review", "remediate", data, ) self.assertEqual(rc, 0) self.assertEqual(len(result["human_decisions"]), 1) self.assertEqual( result["human_decisions"][0]["decision_id"], "d-001", ) def test_partial_error_assembly(self) -> None: """assemble_result_data for partial/error outcome.""" request = {"operation": "remediate", "cve_ids": ["CVE-2024-1"]} data = unattended.assemble_result_data( request=request, status="error", scope={"cve_ids": ["CVE-2024-1"]}, stages_completed=["start"], stages_remaining=["scan", "patch", "validate", "pr"], evidence={}, error="Network timeout during scan", ) rc, result = self._run_make_result("error", "remediate", data) self.assertEqual(rc, 0) self.assertEqual(result["error"], "Network timeout during scan") # --------------------------------------------------------------------------- # Round 12 corrective: Root 2 — build_needs_input_result (pure builder) # --------------------------------------------------------------------------- class TestBuildNeedsInputResult(unittest.TestCase): """build_needs_input_result returns valid result without writing files.""" def test_returns_needs_input_dict(self) -> None: """Pure function returns dict without file I/O.""" result = unattended.build_needs_input_result( "remediate", {"cve_ids": ["CVE-2024-1"]}, error="Missing scan prereqs", missing=["scan_valid"], ) self.assertEqual(result["status"], "needs_input") self.assertEqual(result["operation"], "remediate") self.assertIn("Missing scan prereqs", result["error"]) self.assertIsInstance(result.get("blockers"), list) def test_no_file_written(self) -> None: """Function must not create any files.""" with tempfile.TemporaryDirectory() as td: unattended.build_needs_input_result( "remediate", {"cve_ids": ["CVE-2024-1"]}, ) # No files created self.assertEqual(list(Path(td).iterdir()), []) def test_existing_result_preserved_via_make_result_cli(self) -> None: """Prior result with history is preserved by make-result CLI path. Regression test: the old write_needs_input_result would overwrite unattended-result.json with Path.write_text, losing prior_operations and the existing result's rationale/humanOUT. The new build_needs_input_result is pure, so the caller persists through make-result CLI which archives the old result. """ import subprocess with tempfile.TemporaryDirectory() as td: # Seed existing result with review rationale and humanOUT existing = { "status": "needs_human_review", "operation": "remediate", "scope": {"cve_ids": ["CVE-2024-1"]}, "timestamp": "2026-11-01T00:00:00Z", "evidence": {"review_rationale": "needs expert review"}, "human_decisions": [ {"decision_id": "d1", "question": "approve?"}, ], "stages_completed": ["start", "scan"], "stages_remaining": ["patch"], } out_file = Path(td) / "unattended-result.json" out_file.write_text(json.dumps(existing, indent=2) + "\n") # Build needs_input result (pure — no file write) result = unattended.build_needs_input_result( "remediate", {"cve_ids": ["CVE-2024-1"]}, error="Missing scan prereqs", missing=["scan_valid"], ) self.assertEqual(result["status"], "needs_input") # Persist via make-result CLI (history-preserving atomic write) r = subprocess.run( [sys.executable, "-I", str(_SCRIPT), "make-result", "--status", "needs_input", "--operation", "remediate", "--artifact-dir", td, "--data", json.dumps({ "scope": {"cve_ids": ["CVE-2024-1"]}, "error": "Missing scan prereqs", "blockers": result.get("blockers"), })], capture_output=True, text=True, ) self.assertEqual(r.returncode, 0, r.stderr) # Verify prior_operations preserved the original result written = json.loads(out_file.read_text()) self.assertEqual(written["status"], "needs_input") po = written.get("prior_operations", []) self.assertEqual(len(po), 1) # Original rationale and humanOUT preserved self.assertEqual( po[0]["evidence"]["review_rationale"], "needs expert review", ) self.assertEqual(len(po[0]["human_decisions"]), 1) self.assertEqual(po[0]["status"], "needs_human_review") def test_validation_error_preserves_existing_file(self) -> None: """Invalid --data (bad prior_operations) rejects; old file untouched. Validates that the CLI exits nonzero on input validation errors and does not overwrite the existing result file. This is a validation-gate test, not a partial-I/O proof. """ with tempfile.TemporaryDirectory() as td: # Seed existing result existing = { "status": "partial", "operation": "remediate", "scope": {"cve_ids": ["CVE-2024-1"]}, "timestamp": "2026-11-01T00:00:00Z", "stages_completed": ["start"], "stages_remaining": ["scan"], } out_file = Path(td) / "unattended-result.json" out_file.write_text(json.dumps(existing, indent=2) + "\n") old_bytes = out_file.read_bytes() # Attempt make-result with invalid status to trigger error import subprocess r = subprocess.run( [sys.executable, "-I", str(_SCRIPT), "make-result", "--status", "needs_input", "--operation", "remediate", "--artifact-dir", td, "--data", json.dumps({ "scope": {"cve_ids": ["CVE-2024-1"]}, "prior_operations": "not-a-list", # triggers error })], capture_output=True, text=True, ) # Should fail with error self.assertNotEqual(r.returncode, 0) # Old file untouched self.assertEqual(out_file.read_bytes(), old_bytes) # --------------------------------------------------------------------------- # Round 12 corrective: Root 4 — select_cherry_pick_argv # --------------------------------------------------------------------------- class TestSelectCherryPickArgv(unittest.TestCase): """select_cherry_pick_argv selects correct argv from parent topology.""" def test_regular_commit_one_parent(self) -> None: """1-parent commit → plain cherry-pick -x.""" argv = unattended.select_cherry_pick_argv( "abc123", ["parent deadbeef"], ) self.assertEqual(argv, ["git", "cherry-pick", "-x", "abc123"]) def test_merge_commit_two_parents(self) -> None: """2-parent commit → cherry-pick -x -m 1.""" argv = unattended.select_cherry_pick_argv( "abc123", ["parent deadbeef", "parent cafebabe"], ) self.assertEqual( argv, ["git", "cherry-pick", "-x", "-m", "1", "abc123"], ) def test_initial_commit_no_parents(self) -> None: """0-parent commit → None (unsupported).""" argv = unattended.select_cherry_pick_argv("abc123", []) self.assertIsNone(argv) def test_octopus_merge_three_parents(self) -> None: """3-parent commit → None (unsupported).""" argv = unattended.select_cherry_pick_argv( "abc123", ["parent a", "parent b", "parent c"], ) self.assertIsNone(argv) # --------------------------------------------------------------------------- # Round 12 corrective: Root 5 — non-Git argv with forge commands # --------------------------------------------------------------------------- class TestNonGitArgvForgeCreate(unittest.TestCase): """gh pr create with --base through a stub executable, shell=False.""" def test_stub_gh_receives_literal_base_value(self) -> None: """Stub forge executable receives --base as one literal arg.""" import stat import subprocess with tempfile.TemporaryDirectory() as td: # Create stub that writes each arg on a separate line log_file = os.path.join(td, "create-args.log") stub = os.path.join(td, "gh-stub") Path(stub).write_text( "#!/bin/sh\n" f'for arg in "$@"; do echo "$arg"; done > "{log_file}"\n' ) os.chmod(stub, os.stat(stub).st_mode | stat.S_IEXEC) branch = "release;echo-INJECTED" ok, reason = unattended.validate_branch_name(branch) self.assertTrue(ok, reason) # valid Git branch name r = subprocess.run( [stub, "pr", "create", "--draft", "--base", branch, "--title", "CVE fix", "--body", "Fixes CVE-2024-1"], capture_output=True, text=True, ) self.assertEqual(r.returncode, 0) lines = Path(log_file).read_text().strip().split("\n") base_idx = lines.index("--base") self.assertEqual(lines[base_idx + 1], branch) # No injection marker self.assertFalse(os.path.exists(os.path.join(td, "INJECTED"))) def test_stub_gh_marker_file_absent(self) -> None: """Benign Git-valid metachar branch: no marker file created.""" import stat import subprocess with tempfile.TemporaryDirectory() as td: stub = os.path.join(td, "gh-stub") Path(stub).write_text("#!/bin/sh\ntrue\n") os.chmod(stub, os.stat(stub).st_mode | stat.S_IEXEC) branch = "feat/$var" r = subprocess.run( [stub, "pr", "create", "--base", branch], capture_output=True, text=True, ) self.assertEqual(r.returncode, 0) marker = os.path.join(td, "INJECTED") self.assertFalse(os.path.exists(marker)) # --------------------------------------------------------------------------- # Round 12 corrective: Root 6 — cherry-pick argv with real temp Git # --------------------------------------------------------------------------- class TestCherryPickArgvRealGit(unittest.TestCase): """select_cherry_pick_argv with actual Git merge topology.""" def test_real_merge_commit_two_parents(self) -> None: """Real two-parent merge commit produces correct -m 1 argv.""" import subprocess with tempfile.TemporaryDirectory() as td: env = {**os.environ, "GIT_AUTHOR_NAME": "Test", "GIT_AUTHOR_EMAIL": "t@t", "GIT_COMMITTER_NAME": "Test", "GIT_COMMITTER_EMAIL": "t@t"} def git(*args: str) -> subprocess.CompletedProcess[str]: return subprocess.run( ["git", "-C", td] + list(args), capture_output=True, text=True, env=env, ) git("init", "-b", "main") Path(td, "a.txt").write_text("a") git("add", "a.txt") git("commit", "-m", "initial") git("checkout", "-b", "feature") Path(td, "b.txt").write_text("b") git("add", "b.txt") git("commit", "-m", "feature commit") git("checkout", "main") r = git("merge", "--no-ff", "feature", "-m", "Merge feature") self.assertEqual(r.returncode, 0, r.stderr) # Get merge commit SHA merge_sha = git("rev-parse", "HEAD").stdout.strip() # Get parent lines from git cat-file cat = git("cat-file", "-p", merge_sha) parent_lines = [ line for line in cat.stdout.splitlines() if line.startswith("parent ") ] self.assertEqual(len(parent_lines), 2) # select_cherry_pick_argv should return -m 1 argv = unattended.select_cherry_pick_argv(merge_sha, parent_lines) self.assertIsNotNone(argv) self.assertEqual( argv, ["git", "cherry-pick", "-x", "-m", "1", merge_sha], ) def test_real_single_parent_commit(self) -> None: """Real single-parent commit produces plain cherry-pick argv.""" import subprocess with tempfile.TemporaryDirectory() as td: env = {**os.environ, "GIT_AUTHOR_NAME": "Test", "GIT_AUTHOR_EMAIL": "t@t", "GIT_COMMITTER_NAME": "Test", "GIT_COMMITTER_EMAIL": "t@t"} def git(*args: str) -> subprocess.CompletedProcess[str]: return subprocess.run( ["git", "-C", td] + list(args), capture_output=True, text=True, env=env, ) git("init", "-b", "main") Path(td, "a.txt").write_text("a") git("add", "a.txt") git("commit", "-m", "initial") sha = git("rev-parse", "HEAD").stdout.strip() cat = git("cat-file", "-p", sha) parent_lines = [ line for line in cat.stdout.splitlines() if line.startswith("parent ") ] # Initial commit has 0 parents self.assertEqual(len(parent_lines), 0) self.assertIsNone( unattended.select_cherry_pick_argv(sha, parent_lines), ) # Make a second commit (1 parent) Path(td, "b.txt").write_text("b") git("add", "b.txt") git("commit", "-m", "second") sha2 = git("rev-parse", "HEAD").stdout.strip() cat2 = git("cat-file", "-p", sha2) parent_lines2 = [ line for line in cat2.stdout.splitlines() if line.startswith("parent ") ] self.assertEqual(len(parent_lines2), 1) argv = unattended.select_cherry_pick_argv(sha2, parent_lines2) self.assertEqual( argv, ["git", "cherry-pick", "-x", sha2], ) # --------------------------------------------------------------------------- # Round 12 corrective: bootstrap_check function # --------------------------------------------------------------------------- class TestBootstrapCheck(unittest.TestCase): """bootstrap_check: scope gate, live callback, state loading.""" def test_scope_unbound_returns_needs_input(self) -> None: """Unbound scope → needs_input BEFORE any mkdir/load.""" with tempfile.TemporaryDirectory() as td: boot = unattended.bootstrap_check( artifact_dir=td, operation="remediate", scope={"cve_ids": ["CVE-2024-1"]}, resolved_scope_bound=False, live_pr_check=lambda: {"blocked": False}, ) self.assertEqual(boot["status"], "needs_input") self.assertIn("artifact_scope_bound", str(boot.get("result_data"))) def test_uncallable_live_check_returns_error(self) -> None: """Non-callable live_pr_check → error (fail closed).""" with tempfile.TemporaryDirectory() as td: boot = unattended.bootstrap_check( artifact_dir=td, operation="remediate", scope={"cve_ids": ["CVE-2024-1"]}, resolved_scope_bound=True, live_pr_check="not_callable", ) self.assertEqual(boot["status"], "error") self.assertIn("callable", boot["error"]) def test_live_check_exception_fails_closed(self) -> None: """live_pr_check raising → error (fail closed).""" def bad_check() -> dict[str, Any]: raise ConnectionError("network down") with tempfile.TemporaryDirectory() as td: boot = unattended.bootstrap_check( artifact_dir=td, operation="remediate", scope={"cve_ids": ["CVE-2024-1"]}, resolved_scope_bound=True, live_pr_check=bad_check, ) self.assertEqual(boot["status"], "error") self.assertIn("network down", boot["error"]) def test_live_check_non_dict_fails_closed(self) -> None: """live_pr_check returning non-dict → error.""" with tempfile.TemporaryDirectory() as td: boot = unattended.bootstrap_check( artifact_dir=td, operation="remediate", scope={"cve_ids": ["CVE-2024-1"]}, resolved_scope_bound=True, live_pr_check=lambda: True, ) self.assertEqual(boot["status"], "error") self.assertIn("dict", boot["error"]) def test_live_check_blocked_returns_blocked(self) -> None: """live_pr_check returning blocked → blocked result.""" with tempfile.TemporaryDirectory() as td: boot = unattended.bootstrap_check( artifact_dir=td, operation="remediate", scope={"cve_ids": ["CVE-2024-1"]}, resolved_scope_bound=True, live_pr_check=lambda: {"blocked": True, "reason": "competing PR"}, ) self.assertEqual(boot["status"], "blocked") self.assertIn("result_data", boot) def test_malformed_state_returns_needs_input(self) -> None: """Malformed existing result → needs_input, no phase execution.""" callback_calls: list[str] = [] def track_callback() -> dict[str, Any]: callback_calls.append("called") return {"blocked": False} with tempfile.TemporaryDirectory() as td: Path(td, "unattended-result.json").write_text("not valid json{") boot = unattended.bootstrap_check( artifact_dir=td, operation="remediate", scope={"cve_ids": ["CVE-2024-1"]}, resolved_scope_bound=True, live_pr_check=track_callback, ) self.assertEqual(boot["status"], "needs_input") # Callback NOT called for malformed state (stops early) self.assertEqual(len(callback_calls), 0) def test_ready_calls_callback_and_returns_stage(self) -> None: """Ready state calls callback and returns stage.""" callback_calls: list[str] = [] def track_callback() -> dict[str, Any]: callback_calls.append("called") return {"blocked": False} with tempfile.TemporaryDirectory() as td: Path(td, "context.md").write_text("# context") boot = unattended.bootstrap_check( artifact_dir=td, operation="remediate", scope={"cve_ids": ["CVE-2024-1"]}, resolved_scope_bound=True, live_pr_check=track_callback, requested_stage="scan", ) self.assertEqual(boot["status"], "ready") self.assertEqual(boot["stage"], "scan") self.assertEqual(len(callback_calls), 1) def test_missing_prereqs_returns_needs_input(self) -> None: """Missing stage prereqs → needs_input, callback NOT called.""" callback_calls: list[str] = [] def track_callback() -> dict[str, Any]: callback_calls.append("called") return {"blocked": False} with tempfile.TemporaryDirectory() as td: # No context.md → scan prereqs unmet boot = unattended.bootstrap_check( artifact_dir=td, operation="remediate", scope={"cve_ids": ["CVE-2024-1"]}, resolved_scope_bound=True, live_pr_check=track_callback, requested_stage="scan", ) self.assertEqual(boot["status"], "needs_input") self.assertEqual(len(callback_calls), 0) def test_no_phase_execution(self) -> None: """bootstrap_check never invokes phase callbacks.""" phase_executed = False def live_check() -> dict[str, Any]: return {"blocked": False} with tempfile.TemporaryDirectory() as td: Path(td, "context.md").write_text("# context") boot = unattended.bootstrap_check( artifact_dir=td, operation="remediate", scope={"cve_ids": ["CVE-2024-1"]}, resolved_scope_bound=True, live_pr_check=live_check, ) # Only returns decision — no phases run self.assertIn(boot["status"], ("ready", "needs_input")) self.assertFalse(phase_executed) def test_missing_blocked_key_returns_error(self) -> None: """Callback returning {} (no 'blocked' key) → error.""" with tempfile.TemporaryDirectory() as td: boot = unattended.bootstrap_check( artifact_dir=td, operation="remediate", scope={"cve_ids": ["CVE-2024-1"]}, resolved_scope_bound=True, live_pr_check=lambda: {}, ) self.assertEqual(boot["status"], "error") self.assertIn("blocked", boot["error"]) def test_blocked_none_returns_error(self) -> None: """Callback returning {blocked: None} → error (non-bool).""" with tempfile.TemporaryDirectory() as td: boot = unattended.bootstrap_check( artifact_dir=td, operation="remediate", scope={"cve_ids": ["CVE-2024-1"]}, resolved_scope_bound=True, live_pr_check=lambda: {"blocked": None}, ) self.assertEqual(boot["status"], "error") self.assertIn("bool", boot["error"]) def test_blocked_zero_returns_error(self) -> None: """Callback returning {blocked: 0} → error (non-bool).""" with tempfile.TemporaryDirectory() as td: boot = unattended.bootstrap_check( artifact_dir=td, operation="remediate", scope={"cve_ids": ["CVE-2024-1"]}, resolved_scope_bound=True, live_pr_check=lambda: {"blocked": 0}, ) self.assertEqual(boot["status"], "error") self.assertIn("bool", boot["error"]) def test_blocked_string_returns_error(self) -> None: """Callback returning {blocked: ""} → error (non-bool).""" with tempfile.TemporaryDirectory() as td: boot = unattended.bootstrap_check( artifact_dir=td, operation="remediate", scope={"cve_ids": ["CVE-2024-1"]}, resolved_scope_bound=True, live_pr_check=lambda: {"blocked": ""}, ) self.assertEqual(boot["status"], "error") self.assertIn("bool", boot["error"]) def test_explicit_false_returns_ready(self) -> None: """Callback returning {blocked: False} → ready.""" with tempfile.TemporaryDirectory() as td: boot = unattended.bootstrap_check( artifact_dir=td, operation="remediate", scope={"cve_ids": ["CVE-2024-1"]}, resolved_scope_bound=True, live_pr_check=lambda: {"blocked": False}, ) self.assertEqual(boot["status"], "ready") def test_explicit_true_returns_blocked(self) -> None: """Callback returning {blocked: True} → blocked with result_data.""" with tempfile.TemporaryDirectory() as td: boot = unattended.bootstrap_check( artifact_dir=td, operation="remediate", scope={"cve_ids": ["CVE-2024-1"]}, resolved_scope_bound=True, live_pr_check=lambda: {"blocked": True, "reason": "dup"}, ) self.assertEqual(boot["status"], "blocked") self.assertIn("result_data", boot) # --------------------------------------------------------------------------- # Round 12 corrective: load_helper_module (import safety) # --------------------------------------------------------------------------- class TestLoadHelperModule(unittest.TestCase): """load_helper_module loads from resolved path, not CWD.""" def test_loads_from_script_location(self) -> None: """Module loads from explicit path regardless of CWD.""" with tempfile.TemporaryDirectory() as td: # CWD is an unrelated temp directory orig_cwd = os.getcwd() try: os.chdir(td) mod = unattended.load_helper_module(_SCRIPT) self.assertTrue(hasattr(mod, "validate_request")) self.assertTrue(hasattr(mod, "assemble_result_data")) self.assertTrue(hasattr(mod, "bootstrap_check")) self.assertTrue(hasattr(mod, "select_cherry_pick_argv")) finally: os.chdir(orig_cwd) def test_loads_from_path_with_spaces(self) -> None: """Loads correctly when script path contains spaces.""" import shutil with tempfile.TemporaryDirectory() as td: spaced_dir = Path(td) / "path with spaces" / "scripts" spaced_dir.mkdir(parents=True) src = _SCRIPT # Copy both unattended.py and _common.py if exists shutil.copy2(src, spaced_dir / "unattended.py") common_src = src.parent / "_common.py" if common_src.exists(): shutil.copy2(common_src, spaced_dir / "_common.py") mod = unattended.load_helper_module( spaced_dir / "unattended.py", ) self.assertTrue(hasattr(mod, "validate_request")) def test_nonexistent_path_raises(self) -> None: """Non-existent script path raises ImportError.""" with self.assertRaises(ImportError): unattended.load_helper_module("/nonexistent/unattended.py") def test_cwd_module_not_used(self) -> None: """A malicious unattended.py in CWD is NOT loaded.""" with tempfile.TemporaryDirectory() as td: # Place a decoy in CWD decoy = Path(td) / "unattended.py" decoy.write_text("DECOY = True\n") orig_cwd = os.getcwd() try: os.chdir(td) mod = unattended.load_helper_module(_SCRIPT) # Should load the real module, not the decoy self.assertFalse(getattr(mod, "DECOY", False)) self.assertTrue(hasattr(mod, "validate_request")) finally: os.chdir(orig_cwd) def test_inline_importlib_bootstrap_from_unrelated_cwd(self) -> None: """Documented inline importlib.util pattern loads from spaced path.""" import importlib.util as _imputil import shutil with tempfile.TemporaryDirectory() as td: # Create script copy at a path with spaces spaced_dir = Path(td) / "path with spaces" / "scripts" spaced_dir.mkdir(parents=True) shutil.copy2(_SCRIPT, spaced_dir / "unattended.py") common_src = _SCRIPT.parent / "_common.py" if common_src.exists(): shutil.copy2(common_src, spaced_dir / "_common.py") script_path = str(spaced_dir / "unattended.py") # Use the exact documented pattern from the skill file _spec = _imputil.spec_from_file_location( "unattended", script_path, ) _helper = _imputil.module_from_spec(_spec) _spec.loader.exec_module(_helper) # Loaded from unrelated CWD, module has expected functions orig_cwd = os.getcwd() try: os.chdir(td) self.assertTrue(hasattr(_helper, "bootstrap_check")) self.assertTrue(hasattr(_helper, "assemble_result_data")) self.assertTrue(hasattr(_helper, "load_helper_module")) finally: os.chdir(orig_cwd) # --------------------------------------------------------------------------- # Round 12 corrective: assembler backport target_branches without children # --------------------------------------------------------------------------- class TestAssemblerBackportTargetBranches(unittest.TestCase): """assemble_result_data carries target_branches for partial backport.""" def test_partial_backport_no_children_carries_targets(self) -> None: """Backport with no children yet still carries target_branches.""" req = { "operation": "backport", "target_branches": ["release-2.16", "release-2.15"], "pr_url": "https://github.com/org/repo/pull/42", } data = unattended.assemble_result_data( request=req, status="partial", scope={"cve_ids": ["CVE-2024-1"]}, stages_completed=["backport"], stages_remaining=[], evidence={"note": "in progress"}, ) self.assertEqual( data["target_branches"], ["release-2.16", "release-2.15"], ) # No backport_results key when not provided self.assertNotIn("backport_results", data) def test_error_backport_carries_targets(self) -> None: """Error backport still carries target_branches.""" req = { "operation": "backport", "target_branches": ["release-1.0"], "pr_url": "https://github.com/org/repo/pull/1", } data = unattended.assemble_result_data( request=req, status="error", scope={"cve_ids": ["CVE-2024-1"]}, stages_completed=[], stages_remaining=["backport"], evidence={}, error="Cherry-pick conflict", ) self.assertEqual(data["target_branches"], ["release-1.0"]) def test_completed_backport_with_children(self) -> None: """Completed backport carries both target_branches and results.""" req = { "operation": "backport", "target_branches": ["release-2.16"], "pr_url": "https://github.com/org/repo/pull/42", } children = [{ "target_branch": "release-2.16", "status": "completed", "pr": {"url": "https://github.com/org/repo/pull/43"}, "evidence": {"cherry_pick": "clean"}, }] data = unattended.assemble_result_data( request=req, status="completed", scope={"cve_ids": ["CVE-2024-1"]}, stages_completed=["backport"], stages_remaining=[], evidence={"note": "done"}, backport_results=children, ) self.assertEqual(data["target_branches"], ["release-2.16"]) self.assertEqual(len(data["backport_results"]), 1) # --------------------------------------------------------------------------- # Round 12 corrective: scope stability — reordered CVEs same artifact path # --------------------------------------------------------------------------- class TestScopeStabilityReorderedCVEs(unittest.TestCase): """Reordered CVEs/packages produce same full artifact path.""" def test_reordered_cve_ids_same_subdir(self) -> None: """CVE IDs in different order → identical full artifact subdirectory.""" base = { "operation": "remediate", "repo_path": "org/repo", "base_branch": "main", } req_a = {**base, "cve_ids": ["CVE-2024-1", "CVE-2024-2"]} req_b = {**base, "cve_ids": ["CVE-2024-2", "CVE-2024-1"]} ctx_a = unattended.derive_context(req_a) ctx_b = unattended.derive_context(req_b) sub_a = unattended.derive_artifact_subdir(req_a, ctx_a) sub_b = unattended.derive_artifact_subdir(req_b, ctx_b) # Full artifact subdirectory is identical (scope-independent label) self.assertEqual(sub_a, sub_b) # Starts with scope- prefix, not context label self.assertTrue(sub_a.startswith("scope-")) def test_reordered_packages_same_subdir(self) -> None: """Packages in different order → identical full artifact subdirectory.""" base = { "operation": "remediate", "repo_path": "org/repo", "base_branch": "main", "cve_ids": ["CVE-2024-1"], } req_a = {**base, "packages": ["pkg-b", "pkg-a"]} req_b = {**base, "packages": ["pkg-a", "pkg-b"]} ctx_a = unattended.derive_context(req_a) ctx_b = unattended.derive_context(req_b) sub_a = unattended.derive_artifact_subdir(req_a, ctx_a) sub_b = unattended.derive_artifact_subdir(req_b, ctx_b) self.assertEqual(sub_a, sub_b) def test_wrong_type_resolved_scope_raises(self) -> None: """Non-dict, non-None resolved_scope → ValueError.""" req = {"operation": "remediate", "cve_ids": ["CVE-2024-1"]} ctx = unattended.derive_context(req) with self.assertRaises(ValueError): unattended.derive_artifact_subdir(req, ctx, resolved_scope="bad") with self.assertRaises(ValueError): unattended.derive_artifact_subdir(req, ctx, resolved_scope=42) with self.assertRaises(ValueError): unattended.derive_artifact_subdir(req, ctx, resolved_scope=[1]) # --------------------------------------------------------------------------- # Round 13 Batch A — focused regression tests # --------------------------------------------------------------------------- class TestA2BackportHelperReuse(unittest.TestCase): """A2: _helper loaded in step 6 exposes select_cherry_pick_argv.""" def test_helper_module_exposes_cherry_pick(self) -> None: """importlib.util loaded module has select_cherry_pick_argv.""" import importlib.util as _imputil spec = _imputil.spec_from_file_location("unattended", str(_SCRIPT)) assert spec and spec.loader _helper = _imputil.module_from_spec(spec) spec.loader.exec_module(_helper) # 1-parent commit → plain cherry-pick argv = _helper.select_cherry_pick_argv("abc123", ["parent dead"]) self.assertEqual(argv, ["git", "cherry-pick", "-x", "abc123"]) # 2-parent merge → -m 1 argv = _helper.select_cherry_pick_argv( "def456", ["parent aaa", "parent bbb"], ) self.assertEqual( argv, ["git", "cherry-pick", "-x", "-m", "1", "def456"], ) # 0 parents → None (block) self.assertIsNone(_helper.select_cherry_pick_argv("ghi", [])) class TestA4DocstringValidateResolvedScope(unittest.TestCase): """A4: Docstring pattern — validate + resolved scope + partial result.""" def test_validate_with_resolved_scope_and_partial_result(self) -> None: """Execute the docstring pattern: validate → check bound → make-result partial.""" import subprocess with tempfile.TemporaryDirectory(prefix="doc space ") as td: td_path = Path(td) req_file = td_path / "request.json" req_file.write_text(json.dumps({ "operation": "remediate", "cve_ids": ["CVE-2024-1234"], "jira_tickets": ["EDM-1234"], })) SCRIPT = str(_SCRIPT) resolved = {"repo_path": "org/repo", "base_branch": "main"} # Step 1: validate with --resolved-scope r = subprocess.run( ["python3", "-I", SCRIPT, "validate", str(req_file), "--resolved-scope", json.dumps(resolved)], capture_output=True, text=True, cwd=td, ) self.assertEqual(r.returncode, 0, f"validate failed: {r.stderr}") info = json.loads(r.stdout) self.assertTrue(info["artifact_scope_bound"]) artifact_subdir = info["artifact_subdir"] self.assertTrue(artifact_subdir.startswith("scope-")) # Step 2: construct ABSOLUTE artifact dir artifact_dir = str(td_path / ".artifacts" / "cve-fix" / artifact_subdir) # Step 3: make-result with partial (not completed — no measured proof) data = { "scope": {"cve_ids": ["CVE-2024-1234"], "repo": "org/repo", "base_branch": "main"}, "stages_completed": ["start", "scan"], "stages_remaining": ["patch", "validate", "pr"], } r2 = subprocess.run( ["python3", "-I", SCRIPT, "make-result", "--status", "partial", "--operation", "remediate", "--artifact-dir", artifact_dir, "--data", json.dumps(data)], capture_output=True, text=True, cwd=td, ) self.assertEqual(r2.returncode, 0, f"make-result failed: {r2.stderr}") result = json.loads(r2.stdout) self.assertEqual(result["status"], "partial") self.assertEqual(result["stages_completed"], ["start", "scan"]) self.assertIn(artifact_subdir, result["artifacts_dir"]) # Result file exists at the ABSOLUTE path result_file = Path(artifact_dir) / "unattended-result.json" self.assertTrue(result_file.exists()) class TestA5MalformedStateErrorPreserve(unittest.TestCase): """A5: Malformed state error says preserve + human repair, not remove.""" def test_error_message_says_preserve(self) -> None: """Error message for malformed state must say 'preserved' not 'Remove'.""" state = {"result_malformed": True} resume = unattended.determine_resume_stage(state, None) self.assertEqual(resume["status"], "needs_input") self.assertIn("preserved", resume["error"]) self.assertIn("human", resume["error"].lower()) self.assertNotIn("Remove", resume["error"]) def test_malformed_state_old_bytes_unchanged(self) -> None: """Malformed result file is not deleted or modified.""" with tempfile.TemporaryDirectory() as td: result_file = Path(td) / "unattended-result.json" bad_content = "not valid json {{{" result_file.write_text(bad_content) state = unattended.load_state(td) self.assertTrue(state.get("result_malformed")) # Old file unchanged self.assertEqual(result_file.read_text(), bad_content) def test_malformed_non_dict_preserved(self) -> None: """Non-dict JSON (e.g. a list) is treated as malformed and preserved.""" with tempfile.TemporaryDirectory() as td: result_file = Path(td) / "unattended-result.json" list_content = json.dumps([1, 2, 3]) result_file.write_text(list_content) state = unattended.load_state(td) self.assertTrue(state.get("result_malformed")) self.assertEqual(result_file.read_text(), list_content) def test_malformed_prior_operations_null(self) -> None: """prior_operations: null is malformed and preserved.""" with tempfile.TemporaryDirectory() as td: result_file = Path(td) / "unattended-result.json" content = json.dumps({"status": "completed", "prior_operations": None}) result_file.write_text(content) state = unattended.load_state(td) self.assertTrue(state.get("result_malformed")) self.assertEqual(result_file.read_text(), content) def test_malformed_prior_operations_non_list(self) -> None: """prior_operations as non-list is malformed and preserved.""" with tempfile.TemporaryDirectory() as td: result_file = Path(td) / "unattended-result.json" content = json.dumps({"status": "completed", "prior_operations": "bad"}) result_file.write_text(content) state = unattended.load_state(td) self.assertTrue(state.get("result_malformed")) self.assertEqual(result_file.read_text(), content) def test_malformed_prior_operations_nested(self) -> None: """prior_operations with nested entry is malformed and preserved.""" with tempfile.TemporaryDirectory() as td: result_file = Path(td) / "unattended-result.json" content = json.dumps({ "status": "completed", "prior_operations": [ {"status": "partial", "prior_operations": []} ], }) result_file.write_text(content) state = unattended.load_state(td) self.assertTrue(state.get("result_malformed")) self.assertEqual(result_file.read_text(), content) class TestA6InstalledScriptPath(unittest.TestCase): """A6: scan.py/verify.py resolve from installed skill location, not CWD.""" def test_scan_script_resolves_from_skill_dir(self) -> None: """Installed scan.py is called, not a CWD trap file.""" import subprocess with tempfile.TemporaryDirectory(prefix="installed skill ") as skill_dir: with tempfile.TemporaryDirectory(prefix="target repo") as cwd_dir: # Create installed scan.py stub scripts_dir = Path(skill_dir) / "scripts" scripts_dir.mkdir() scan_stub = scripts_dir / "scan.py" scan_stub.write_text( "import sys; print('INSTALLED', *sys.argv[1:])\n" ) # Create CWD trap: a scan.py in the target repo's scripts/ trap_dir = Path(cwd_dir) / "scripts" trap_dir.mkdir() trap_file = trap_dir / "scan.py" trap_file.write_text( "import sys; print('TRAP'); sys.exit(99)\n" ) # Resolve ABSOLUTE path from installed skill location SCAN_SCRIPT = str(scan_stub.resolve()) # Run from target CWD — must call installed, not trap r = subprocess.run( ["python3", "-I", SCAN_SCRIPT, cwd_dir, "CVE-2024-1234", "pkg", "local"], capture_output=True, text=True, cwd=cwd_dir, ) self.assertEqual(r.returncode, 0, f"scan stub failed: {r.stderr}") self.assertIn("INSTALLED", r.stdout) self.assertNotIn("TRAP", r.stdout) def test_verify_script_resolves_from_skill_dir(self) -> None: """Installed verify.py is called, not a CWD trap file.""" import subprocess with tempfile.TemporaryDirectory(prefix="installed skill ") as skill_dir: with tempfile.TemporaryDirectory(prefix="target repo") as cwd_dir: scripts_dir = Path(skill_dir) / "scripts" scripts_dir.mkdir() verify_stub = scripts_dir / "verify.py" verify_stub.write_text( "import sys; print('INSTALLED_VERIFY', *sys.argv[1:])\n" ) trap_dir = Path(cwd_dir) / "scripts" trap_dir.mkdir() trap_file = trap_dir / "verify.py" trap_file.write_text( "import sys; print('TRAP'); sys.exit(99)\n" ) VERIFY_SCRIPT = str(verify_stub.resolve()) r = subprocess.run( ["python3", "-I", VERIFY_SCRIPT, cwd_dir, "CVE-2024-1234", "go", "1.23.0", "local"], capture_output=True, text=True, cwd=cwd_dir, ) self.assertEqual(r.returncode, 0, f"verify stub failed: {r.stderr}") self.assertIn("INSTALLED_VERIFY", r.stdout) self.assertNotIn("TRAP", r.stdout) def test_space_in_installed_path(self) -> None: """ABSOLUTE path with spaces works for subprocess calls.""" import subprocess with tempfile.TemporaryDirectory(prefix="skill with spaces ") as skill_dir: scripts_dir = Path(skill_dir) / "scripts" scripts_dir.mkdir() stub = scripts_dir / "scan.py" stub.write_text( "import sys; print('OK', *sys.argv[1:])\n" ) SCAN_SCRIPT = str(stub.resolve()) self.assertIn(" ", SCAN_SCRIPT) r = subprocess.run( ["python3", "-I", SCAN_SCRIPT, "/tmp/repo", "CVE-1"], capture_output=True, text=True, ) self.assertEqual(r.returncode, 0) self.assertIn("OK", r.stdout) class TestA7SpaceInArtifactDir(unittest.TestCase): """A7: artifact_dir with spaces in parent path handled by subprocess argv.""" def test_make_result_with_space_in_path(self) -> None: """make-result writes correctly when artifact_dir parent has spaces.""" import subprocess with tempfile.TemporaryDirectory(prefix="space path ") as td: artifact_dir = str( Path(td) / ".artifacts" / "cve-fix" / "scope-abc123" ) self.assertIn(" ", artifact_dir) self.assertNotIn(" ", Path(artifact_dir).name) data = { "scope": {"cve_ids": ["CVE-2024-1234"]}, "stages_completed": ["start"], "stages_remaining": ["scan"], } r = subprocess.run( ["python3", "-I", str(_SCRIPT), "make-result", "--status", "partial", "--operation", "remediate", "--artifact-dir", artifact_dir, "--data", json.dumps(data)], capture_output=True, text=True, ) self.assertEqual(r.returncode, 0, f"Failed: {r.stderr}") result = json.loads(r.stdout) self.assertEqual(result["status"], "partial") # File written at correct path result_file = Path(artifact_dir) / "unattended-result.json" self.assertTrue(result_file.exists()) # --------------------------------------------------------------------------- # B1: Scoped generated feature branch defaults for unattended # --------------------------------------------------------------------------- class TestB1DeriveBranchScopedDefault(unittest.TestCase): """B1: derive_branch produces distinct names for different bases.""" def _req( self, *, cve: str = "CVE-2024-1234", base: str | None = None, branch: str | None = None, ) -> dict[str, Any]: req: dict[str, Any] = { "operation": "remediate", "cve_ids": [cve], } if base is not None: req["base_branch"] = base if branch is not None: req["branch"] = branch return req # -- RED TEST: demonstrates the bug before fix would be applied -- # (now passes because the fix IS applied — kept as regression) def test_same_cve_different_bases_different_branches(self) -> None: """Same CVE on main vs release-1.0 must yield DIFFERENT generated branches.""" req_main = self._req() req_release = self._req() rs_main = {"repo_path": "org/repo", "base_branch": "main"} rs_release = {"repo_path": "org/repo", "base_branch": "release-1.0"} b_main = unattended.derive_branch(req_main, "CVE-2024-1234", rs_main) b_release = unattended.derive_branch(req_release, "CVE-2024-1234", rs_release) self.assertNotEqual(b_main, b_release, "Different bases must produce different branch names") self.assertTrue(b_main.startswith("cve-fix/CVE-2024-1234")) self.assertTrue(b_release.startswith("cve-fix/CVE-2024-1234")) def test_base_omitted_from_request_still_scoped(self) -> None: """Resolved default base (not explicit in request) still scopes the branch.""" req = self._req() # no base_branch in request rs_main = {"repo_path": "org/repo", "base_branch": "main"} rs_release = {"repo_path": "org/repo", "base_branch": "release-1.0"} b_main = unattended.derive_branch(req, "CVE-2024-1234", rs_main) b_release = unattended.derive_branch(req, "CVE-2024-1234", rs_release) self.assertNotEqual(b_main, b_release) def test_same_scope_same_branch_stable(self) -> None: """Same scope produces the same branch on rerun (deterministic).""" req = self._req() rs = {"repo_path": "org/repo", "base_branch": "main"} b1 = unattended.derive_branch(req, "CVE-2024-1234", rs) b2 = unattended.derive_branch(req, "CVE-2024-1234", rs) self.assertEqual(b1, b2) def test_reordered_cves_same_branch(self) -> None: """Reordered CVE IDs produce the same branch (order-independent).""" req_a = {"operation": "remediate", "cve_ids": ["CVE-2024-1234", "CVE-2024-5678"]} req_b = {"operation": "remediate", "cve_ids": ["CVE-2024-5678", "CVE-2024-1234"]} rs = {"repo_path": "org/repo", "base_branch": "main"} # Use actual derive_context outputs (different first CVEs) ctx_a = unattended.derive_context(req_a) ctx_b = unattended.derive_context(req_b) self.assertNotEqual(ctx_a, ctx_b, "contexts differ by first CVE") b_a = unattended.derive_branch(req_a, ctx_a, rs) b_b = unattended.derive_branch(req_b, ctx_b, rs) self.assertEqual(b_a, b_b) def test_explicit_branch_unchanged(self) -> None: """Explicit request.branch is returned as-is regardless of resolved scope.""" req = self._req(branch="my-custom-branch") rs = {"repo_path": "org/repo", "base_branch": "main"} b = unattended.derive_branch(req, "CVE-2024-1234", rs) self.assertEqual(b, "my-custom-branch") def test_attended_no_operation_unchanged(self) -> None: """No operation field → attended default cve-fix/{context}.""" req = {"cve_ids": ["CVE-2024-1234"]} # no operation rs = {"repo_path": "org/repo", "base_branch": "main"} b = unattended.derive_branch(req, "CVE-2024-1234", rs) self.assertEqual(b, "cve-fix/CVE-2024-1234") def test_no_resolved_scope_fallback(self) -> None: """Unattended without resolved_scope falls back to plain name.""" req = self._req() b = unattended.derive_branch(req, "CVE-2024-1234") self.assertEqual(b, "cve-fix/CVE-2024-1234") def test_no_resolved_scope_none_fallback(self) -> None: """Unattended with resolved_scope=None falls back to plain name.""" req = self._req() b = unattended.derive_branch(req, "CVE-2024-1234", None) self.assertEqual(b, "cve-fix/CVE-2024-1234") def test_resolved_scope_missing_base_fallback(self) -> None: """Resolved scope without base_branch falls back to plain name.""" req = self._req() rs = {"repo_path": "org/repo"} b = unattended.derive_branch(req, "CVE-2024-1234", rs) self.assertEqual(b, "cve-fix/CVE-2024-1234") def test_different_repos_different_branches(self) -> None: """Same CVE/base but different repos produce different branches.""" req = self._req() rs_a = {"repo_path": "org/repo-a", "base_branch": "main"} rs_b = {"repo_path": "org/repo-b", "base_branch": "main"} b_a = unattended.derive_branch(req, "CVE-2024-1234", rs_a) b_b = unattended.derive_branch(req, "CVE-2024-1234", rs_b) self.assertNotEqual(b_a, b_b) def test_branch_name_format(self) -> None: """Generated branch has format cve-fix/{context}-{8hexchars}.""" req = self._req() rs = {"repo_path": "org/repo", "base_branch": "main"} b = unattended.derive_branch(req, "CVE-2024-1234", rs) import re self.assertRegex(b, r"^cve-fix/CVE-2024-1234-[0-9a-f]{8}$") def test_implicit_explicit_same_effective_branch_cli(self) -> None: """CLI validate: implicit vs explicit same base → same branch+artifact. Replaces tautological API alias test with actual CLI roundtrip: 1. Validate request A (explicit base_branch=main) → get branch+artifact_subdir 2. Write result A via make-result 3. Validate request B (implicit base, resolved scope main) → get branch+artifact_subdir 4. Verify A and B have same branch and artifact_subdir 5. Verify A's result file bytes+PR preserved after B's validate """ import subprocess with tempfile.TemporaryDirectory() as td: rs = {"repo_path": "org/repo", "base_branch": "main"} # Request A: explicit base_branch=main req_a = { "operation": "remediate", "cve_ids": ["CVE-2024-1234"], "base_branch": "main", } f_a = Path(td) / "req_a.json" f_a.write_text(json.dumps(req_a)) r_a = subprocess.run( ["python3", "-I", str(_SCRIPT), "validate", str(f_a), "--resolved-scope", json.dumps(rs)], capture_output=True, text=True, cwd=td, ) self.assertEqual(r_a.returncode, 0, f"A validate: {r_a.stderr}") info_a = json.loads(r_a.stdout) branch_a = info_a["branch"] subdir_a = info_a["artifact_subdir"] # Write result A via make-result art_dir_a = str(Path(td) / ".artifacts" / "cve-fix" / subdir_a) data_a = { "scope": {"cve_ids": ["CVE-2024-1234"], "repo": "org/repo", "base_branch": "main"}, "stages_completed": ["start", "scan", "patch", "validate", "pr"], "evidence": {"validation_verdict": "PASS"}, "pr": {"url": "https://github.com/org/repo/pull/42", "state": "open"}, } r_wr = subprocess.run( ["python3", "-I", str(_SCRIPT), "make-result", "--status", "completed", "--operation", "remediate", "--artifact-dir", art_dir_a, "--data", json.dumps(data_a)], capture_output=True, text=True, cwd=td, ) self.assertEqual(r_wr.returncode, 0, f"A write: {r_wr.stderr}") # Capture A's result bytes for later comparison result_file_a = Path(art_dir_a) / "unattended-result.json" self.assertTrue(result_file_a.exists()) a_bytes = result_file_a.read_bytes() a_result = json.loads(a_bytes) self.assertEqual(a_result["pr"]["url"], "https://github.com/org/repo/pull/42") # Request B: implicit base (no base_branch in request) req_b = { "operation": "remediate", "cve_ids": ["CVE-2024-1234"], } f_b = Path(td) / "req_b.json" f_b.write_text(json.dumps(req_b)) r_b = subprocess.run( ["python3", "-I", str(_SCRIPT), "validate", str(f_b), "--resolved-scope", json.dumps(rs)], capture_output=True, text=True, cwd=td, ) self.assertEqual(r_b.returncode, 0, f"B validate: {r_b.stderr}") info_b = json.loads(r_b.stdout) # Same branch and artifact subdir (implicit == explicit) self.assertEqual(info_b["branch"], branch_a, "Implicit and explicit same base must produce same branch") self.assertEqual(info_b["artifact_subdir"], subdir_a, "Implicit and explicit same base must produce same artifact dir") # A's result file bytes AND PR identity preserved self.assertEqual(result_file_a.read_bytes(), a_bytes, "A's result file bytes must be unchanged") preserved = json.loads(result_file_a.read_text()) self.assertEqual(preserved["pr"]["url"], "https://github.com/org/repo/pull/42", "A's PR identity must be preserved") def test_jira_context_scoped_branch(self) -> None: """Jira ticket as context also gets scoped branch.""" req = { "operation": "remediate", "jira_tickets": ["EDM-1234"], "cve_ids": ["CVE-2024-5678"], } rs_main = {"repo_path": "org/repo", "base_branch": "main"} rs_release = {"repo_path": "org/repo", "base_branch": "release-1.0"} b_main = unattended.derive_branch(req, "EDM-1234", rs_main) b_release = unattended.derive_branch(req, "EDM-1234", rs_release) self.assertNotEqual(b_main, b_release) self.assertTrue(b_main.startswith("cve-fix/EDM-1234-")) self.assertTrue(b_release.startswith("cve-fix/EDM-1234-")) # --------------------------------------------------------------------------- # B2: CLI validate outputs scoped branch with resolved_scope # --------------------------------------------------------------------------- class TestB2CLIValidateScopedBranch(unittest.TestCase): """B2: _cmd_validate outputs scoped branch when resolved_scope provided.""" def test_cli_validate_scoped_branch_with_resolved_scope(self) -> None: """CLI validate returns scoped branch name with resolved scope.""" import subprocess with tempfile.TemporaryDirectory() as td: req_file = Path(td) / "request.json" req_file.write_text(json.dumps({ "operation": "remediate", "cve_ids": ["CVE-2024-1234"], })) resolved = {"repo_path": "org/repo", "base_branch": "main"} r = subprocess.run( ["python3", "-I", str(_SCRIPT), "validate", str(req_file), "--resolved-scope", json.dumps(resolved)], capture_output=True, text=True, cwd=td, ) self.assertEqual(r.returncode, 0, f"validate failed: {r.stderr}") info = json.loads(r.stdout) self.assertTrue(info["branch"].startswith("cve-fix/CVE-2024-1234-")) self.assertNotEqual(info["branch"], "cve-fix/CVE-2024-1234") def test_cli_validate_different_bases_different_branches(self) -> None: """CLI validate returns different branches for different resolved bases.""" import subprocess with tempfile.TemporaryDirectory() as td: req_file = Path(td) / "request.json" req_file.write_text(json.dumps({ "operation": "remediate", "cve_ids": ["CVE-2024-1234"], })) rs_main = {"repo_path": "org/repo", "base_branch": "main"} rs_release = {"repo_path": "org/repo", "base_branch": "release-1.0"} r_main = subprocess.run( ["python3", "-I", str(_SCRIPT), "validate", str(req_file), "--resolved-scope", json.dumps(rs_main)], capture_output=True, text=True, cwd=td, ) r_release = subprocess.run( ["python3", "-I", str(_SCRIPT), "validate", str(req_file), "--resolved-scope", json.dumps(rs_release)], capture_output=True, text=True, cwd=td, ) self.assertEqual(r_main.returncode, 0, f"main failed: {r_main.stderr}") self.assertEqual(r_release.returncode, 0, f"release failed: {r_release.stderr}") info_main = json.loads(r_main.stdout) info_release = json.loads(r_release.stdout) self.assertNotEqual(info_main["branch"], info_release["branch"]) def test_cli_validate_no_resolved_scope_plain_branch(self) -> None: """CLI validate without resolved_scope returns plain branch.""" import subprocess with tempfile.TemporaryDirectory() as td: req_file = Path(td) / "request.json" req_file.write_text(json.dumps({ "operation": "remediate", "cve_ids": ["CVE-2024-1234"], })) r = subprocess.run( ["python3", "-I", str(_SCRIPT), "validate", str(req_file)], capture_output=True, text=True, cwd=td, ) self.assertEqual(r.returncode, 0, f"validate failed: {r.stderr}") info = json.loads(r.stdout) self.assertEqual(info["branch"], "cve-fix/CVE-2024-1234") def test_cli_validate_explicit_branch_preserved(self) -> None: """CLI validate with explicit request.branch returns it as-is.""" import subprocess with tempfile.TemporaryDirectory() as td: req_file = Path(td) / "request.json" req_file.write_text(json.dumps({ "operation": "remediate", "cve_ids": ["CVE-2024-1234"], "branch": "my-custom-branch", })) resolved = {"repo_path": "org/repo", "base_branch": "main"} r = subprocess.run( ["python3", "-I", str(_SCRIPT), "validate", str(req_file), "--resolved-scope", json.dumps(resolved)], capture_output=True, text=True, cwd=td, ) self.assertEqual(r.returncode, 0, f"validate failed: {r.stderr}") info = json.loads(r.stdout) self.assertEqual(info["branch"], "my-custom-branch") # --------------------------------------------------------------------------- # CLI end-to-end: reordered CVEs same branch + artifact # --------------------------------------------------------------------------- class TestCLIReorderedCVEsSameBranchAndArtifact(unittest.TestCase): """CLI end-to-end: reordered CVEs produce same branch AND artifact dir.""" def test_reordered_cves_cli_same_branch_and_artifact(self) -> None: """CLI validate with reordered CVEs produces identical branch+artifact.""" import subprocess with tempfile.TemporaryDirectory() as td: rs = {"repo_path": "org/repo", "base_branch": "main"} req_a = {"operation": "remediate", "cve_ids": ["CVE-2024-1234", "CVE-2024-5678"]} req_b = {"operation": "remediate", "cve_ids": ["CVE-2024-5678", "CVE-2024-1234"]} f_a = Path(td) / "req_a.json" f_b = Path(td) / "req_b.json" f_a.write_text(json.dumps(req_a)) f_b.write_text(json.dumps(req_b)) r_a = subprocess.run( ["python3", "-I", str(_SCRIPT), "validate", str(f_a), "--resolved-scope", json.dumps(rs)], capture_output=True, text=True, cwd=td, ) r_b = subprocess.run( ["python3", "-I", str(_SCRIPT), "validate", str(f_b), "--resolved-scope", json.dumps(rs)], capture_output=True, text=True, cwd=td, ) self.assertEqual(r_a.returncode, 0, f"a failed: {r_a.stderr}") self.assertEqual(r_b.returncode, 0, f"b failed: {r_b.stderr}") info_a = json.loads(r_a.stdout) info_b = json.loads(r_b.stdout) self.assertEqual(info_a["branch"], info_b["branch"], "Reordered CVEs must produce same branch") self.assertEqual(info_a["artifact_subdir"], info_b["artifact_subdir"], "Reordered CVEs must produce same artifact dir") # --------------------------------------------------------------------------- # B3: Artifact binding — branch namespace isolation # --------------------------------------------------------------------------- class TestB3ArtifactBranchNamespace(unittest.TestCase): """B3: Different bases produce different artifact dirs AND branches.""" def test_artifact_and_branch_aligned_isolation(self) -> None: """Artifact subdir and branch both differentiate across bases.""" req = {"operation": "remediate", "cve_ids": ["CVE-2024-1234"]} context = "CVE-2024-1234" rs_main = {"repo_path": "org/repo", "base_branch": "main"} rs_release = {"repo_path": "org/repo", "base_branch": "release-1.0"} art_main = unattended.derive_artifact_subdir(req, context, rs_main) art_release = unattended.derive_artifact_subdir(req, context, rs_release) br_main = unattended.derive_branch(req, context, rs_main) br_release = unattended.derive_branch(req, context, rs_release) # Both artifact dirs and branches must differ self.assertNotEqual(art_main, art_release) self.assertNotEqual(br_main, br_release) def test_same_scope_aligned(self) -> None: """Same scope produces aligned artifact dir and branch (stable).""" req = {"operation": "remediate", "cve_ids": ["CVE-2024-1234"]} context = "CVE-2024-1234" rs = {"repo_path": "org/repo", "base_branch": "main"} art1 = unattended.derive_artifact_subdir(req, context, rs) art2 = unattended.derive_artifact_subdir(req, context, rs) br1 = unattended.derive_branch(req, context, rs) br2 = unattended.derive_branch(req, context, rs) self.assertEqual(art1, art2) self.assertEqual(br1, br2) def test_old_branch_artifacts_not_overwritten(self) -> None: """Old branch A artifacts in scope-X dir, branch B request → different scope dir.""" req = {"operation": "remediate", "cve_ids": ["CVE-2024-1234"]} context = "CVE-2024-1234" # Branch A artifacts on main rs_main = {"repo_path": "org/repo", "base_branch": "main"} art_main = unattended.derive_artifact_subdir(req, context, rs_main) br_main = unattended.derive_branch(req, context, rs_main) # Branch B request on release rs_release = {"repo_path": "org/repo", "base_branch": "release-1.0"} art_release = unattended.derive_artifact_subdir(req, context, rs_release) br_release = unattended.derive_branch(req, context, rs_release) # Different artifact dirs → no overwrite possible self.assertNotEqual(art_main, art_release) # Different branches → no shared namespace self.assertNotEqual(br_main, br_release) def test_bootstrap_scope_unbound_blocks_writes(self) -> None: """Bootstrap with scope_unbound=False returns needs_input before any write.""" with tempfile.TemporaryDirectory() as td: result = unattended.bootstrap_check( artifact_dir=td, operation="remediate", scope={"cve_ids": ["CVE-2024-1234"]}, resolved_scope_bound=False, live_pr_check=lambda: {"blocked": False}, ) self.assertEqual(result["status"], "needs_input") # No result file written self.assertFalse((Path(td) / "unattended-result.json").exists()) # --------------------------------------------------------------------------- # Branch storage binding — feature_branch in resolved_scope # --------------------------------------------------------------------------- class TestBranchStorageBinding(unittest.TestCase): """Feature branch in resolved_scope isolates artifact directories.""" def test_different_feature_branches_different_subdirs(self) -> None: """Same repo/base/CVE but different feature_branch → different artifact dir.""" req = {"operation": "remediate", "cve_ids": ["CVE-2024-1234"]} context = unattended.derive_context(req) rs_a = {"repo_path": "org/repo", "base_branch": "main", "feature_branch": "cve-fix/fix-a"} rs_b = {"repo_path": "org/repo", "base_branch": "main", "feature_branch": "cve-fix/fix-b"} sub_a = unattended.derive_artifact_subdir(req, context, rs_a) sub_b = unattended.derive_artifact_subdir(req, context, rs_b) self.assertNotEqual(sub_a, sub_b) def test_same_feature_branch_same_subdir(self) -> None: """Same feature_branch → identical artifact dir (stable).""" req = {"operation": "remediate", "cve_ids": ["CVE-2024-1234"]} context = unattended.derive_context(req) rs = {"repo_path": "org/repo", "base_branch": "main", "feature_branch": "cve-fix/fix-a"} sub1 = unattended.derive_artifact_subdir(req, context, rs) sub2 = unattended.derive_artifact_subdir(req, context, rs) self.assertEqual(sub1, sub2) def test_implicit_explicit_same_branch_same_subdir(self) -> None: """Implicit generated default and explicit same branch → same artifact dir.""" req = {"operation": "remediate", "cve_ids": ["CVE-2024-1234"]} context = unattended.derive_context(req) rs_base = {"repo_path": "org/repo", "base_branch": "main"} # Derive the default branch default_branch = unattended.derive_branch(req, context, rs_base) # Explicit same branch via feature_branch rs_explicit = {**rs_base, "feature_branch": default_branch} rs_auto = {**rs_base, "feature_branch": default_branch} sub_explicit = unattended.derive_artifact_subdir(req, context, rs_explicit) sub_auto = unattended.derive_artifact_subdir(req, context, rs_auto) self.assertEqual(sub_explicit, sub_auto) def test_no_feature_branch_backward_compatible(self) -> None: """Without feature_branch, artifact dir is unchanged (backward compat).""" req = {"operation": "remediate", "cve_ids": ["CVE-2024-1234"]} context = unattended.derive_context(req) rs_without = {"repo_path": "org/repo", "base_branch": "main"} sub = unattended.derive_artifact_subdir(req, context, rs_without) self.assertTrue(sub.startswith("scope-")) def test_branch_a_artifacts_unchanged_by_branch_b(self) -> None: """Branch A artifacts are not overwritten when branch B is used.""" with tempfile.TemporaryDirectory() as td: base_dir = Path(td) req = {"operation": "remediate", "cve_ids": ["CVE-2024-1234"]} context = unattended.derive_context(req) rs_a = {"repo_path": "org/repo", "base_branch": "main", "feature_branch": "cve-fix/fix-a"} rs_b = {"repo_path": "org/repo", "base_branch": "main", "feature_branch": "cve-fix/fix-b"} sub_a = unattended.derive_artifact_subdir(req, context, rs_a) sub_b = unattended.derive_artifact_subdir(req, context, rs_b) # Create A's artifacts dir_a = base_dir / sub_a dir_a.mkdir(parents=True) result_a = {"status": "partial", "pr": {"branch": "cve-fix/fix-a"}} (dir_a / "unattended-result.json").write_text( json.dumps(result_a, indent=2) ) a_bytes = (dir_a / "unattended-result.json").read_bytes() # Bootstrap for B uses different directory dir_b = base_dir / sub_b bootstrap = unattended.bootstrap_check( artifact_dir=str(dir_b), operation="remediate", scope={"cve_ids": ["CVE-2024-1234"]}, resolved_scope_bound=True, live_pr_check=lambda: {"blocked": False}, ) # A's files unchanged self.assertEqual( (dir_a / "unattended-result.json").read_bytes(), a_bytes ) # B got different dir, no reused stages self.assertNotEqual(sub_a, sub_b) self.assertEqual(bootstrap["status"], "ready") self.assertEqual(bootstrap["stage"], "start") # --------------------------------------------------------------------------- # B4: Interface integration — CLI + create_pr branch alignment # --------------------------------------------------------------------------- class TestB4BranchResolverConsistency(unittest.TestCase): """B4: CLI validate branch output uses same resolver as internal API.""" def test_cli_and_api_branch_match(self) -> None: """CLI validate branch output matches direct derive_branch call.""" import subprocess req = { "operation": "remediate", "cve_ids": ["CVE-2024-1234"], } rs = {"repo_path": "org/repo", "base_branch": "main"} api_branch = unattended.derive_branch(req, "CVE-2024-1234", rs) with tempfile.TemporaryDirectory() as td: req_file = Path(td) / "request.json" req_file.write_text(json.dumps(req)) r = subprocess.run( ["python3", "-I", str(_SCRIPT), "validate", str(req_file), "--resolved-scope", json.dumps(rs)], capture_output=True, text=True, cwd=td, ) self.assertEqual(r.returncode, 0, f"failed: {r.stderr}") info = json.loads(r.stdout) self.assertEqual(info["branch"], api_branch) def test_cli_artifact_dir_and_branch_both_scoped(self) -> None: """CLI validate returns both scoped artifact_subdir and scoped branch.""" import subprocess with tempfile.TemporaryDirectory() as td: req_file = Path(td) / "request.json" req_file.write_text(json.dumps({ "operation": "remediate", "cve_ids": ["CVE-2024-1234"], })) resolved = {"repo_path": "org/repo", "base_branch": "main"} r = subprocess.run( ["python3", "-I", str(_SCRIPT), "validate", str(req_file), "--resolved-scope", json.dumps(resolved)], capture_output=True, text=True, cwd=td, ) self.assertEqual(r.returncode, 0, f"failed: {r.stderr}") info = json.loads(r.stdout) # Both are scoped (not plain context) self.assertTrue(info["artifact_subdir"].startswith("scope-")) self.assertRegex(info["branch"], r"^cve-fix/CVE-2024-1234-[0-9a-f]{8}$") # --------------------------------------------------------------------------- # A4 fix: Docstring extract/exec test (replaces copied pattern) # --------------------------------------------------------------------------- class TestA4DocstringExtractExec(unittest.TestCase): """A4: Extract and execute docstring code in temp CWD with space in SCRIPT path.""" def test_docstring_validate_pattern_with_space_script(self) -> None: """Extract the docstring's validate pattern, execute from temp CWD.""" import subprocess with tempfile.TemporaryDirectory(prefix="doc space ") as td: td_path = Path(td) # Create the request.json the docstring references req_file = td_path / "request.json" req_file.write_text(json.dumps({ "operation": "remediate", "cve_ids": ["CVE-2024-1234"], "jira_tickets": ["EDM-1234"], })) # Use the actual SCRIPT path (which may or may not have # spaces — test uses prefix with space in temp dir) SCRIPT = str(_SCRIPT) # Execute the docstring pattern: validate with resolved scope resolved = {"repo_path": "org/repo", "base_branch": "main"} result = subprocess.run( ["python3", "-I", SCRIPT, "validate", str(req_file), "--resolved-scope", json.dumps(resolved)], capture_output=True, text=True, cwd=td, ) self.assertEqual(result.returncode, 0, f"validate failed: {result.stderr}") info = json.loads(result.stdout) self.assertTrue(info["artifact_scope_bound"]) artifact_subdir = info["artifact_subdir"] # Construct artifact dir with Path.resolve() as docstring shows artifact_dir = str( Path(td_path / ".artifacts" / "cve-fix").resolve() / artifact_subdir ) self.assertIn(artifact_subdir, artifact_dir) # Execute make-result partial (docstring pattern step 2) data = { "scope": {"cve_ids": ["CVE-2024-1234"], "repo": "org/repo", "base_branch": "main"}, "stages_completed": ["start", "scan"], "stages_remaining": ["patch", "validate", "pr"], } r2 = subprocess.run( ["python3", "-I", SCRIPT, "make-result", "--status", "partial", "--operation", "remediate", "--artifact-dir", artifact_dir, "--data", json.dumps(data)], capture_output=True, text=True, cwd=td, ) self.assertEqual(r2.returncode, 0, f"make-result failed: {r2.stderr}") result_out = json.loads(r2.stdout) self.assertEqual(result_out["status"], "partial") self.assertEqual(result_out["stages_completed"], ["start", "scan"]) # Result file exists at the constructed path result_file = Path(artifact_dir) / "unattended-result.json" self.assertTrue(result_file.exists()) # --------------------------------------------------------------------------- # Round 13: C4 — bootstrap_check error returns include result_data # --------------------------------------------------------------------------- class TestC4BootstrapErrorResultData(unittest.TestCase): """C4: Every bootstrap_check error return includes result_data.""" def test_uncallable_has_result_data(self) -> None: """Uncallable live_pr_check error includes result_data.""" with tempfile.TemporaryDirectory() as td: boot = unattended.bootstrap_check( artifact_dir=td, operation="remediate", scope={"cve_ids": ["CVE-2024-1"]}, resolved_scope_bound=True, live_pr_check="not_callable", ) self.assertEqual(boot["status"], "error") self.assertIn("result_data", boot) rd = boot["result_data"] self.assertIn("error", rd) self.assertIn("scope", rd) def test_exception_has_result_data(self) -> None: """Raised callback error includes result_data.""" def bad() -> dict: raise RuntimeError("boom") with tempfile.TemporaryDirectory() as td: boot = unattended.bootstrap_check( artifact_dir=td, operation="remediate", scope={"cve_ids": ["CVE-2024-1"]}, resolved_scope_bound=True, live_pr_check=bad, ) self.assertEqual(boot["status"], "error") self.assertIn("result_data", boot) def test_non_dict_return_has_result_data(self) -> None: """Non-dict callback return error includes result_data.""" with tempfile.TemporaryDirectory() as td: boot = unattended.bootstrap_check( artifact_dir=td, operation="remediate", scope={"cve_ids": ["CVE-2024-1"]}, resolved_scope_bound=True, live_pr_check=lambda: True, ) self.assertEqual(boot["status"], "error") self.assertIn("result_data", boot) def test_missing_blocked_has_result_data(self) -> None: """Missing blocked key error includes result_data.""" with tempfile.TemporaryDirectory() as td: boot = unattended.bootstrap_check( artifact_dir=td, operation="remediate", scope={"cve_ids": ["CVE-2024-1"]}, resolved_scope_bound=True, live_pr_check=lambda: {}, ) self.assertEqual(boot["status"], "error") self.assertIn("result_data", boot) def test_nonbool_blocked_has_result_data(self) -> None: """Non-bool blocked value error includes result_data.""" with tempfile.TemporaryDirectory() as td: boot = unattended.bootstrap_check( artifact_dir=td, operation="remediate", scope={"cve_ids": ["CVE-2024-1"]}, resolved_scope_bound=True, live_pr_check=lambda: {"blocked": 0}, ) self.assertEqual(boot["status"], "error") self.assertIn("result_data", boot) def test_all_error_result_data_has_canonical_keys(self) -> None: """Every error result_data has scope, error, blockers keys.""" cases = [ lambda td: unattended.bootstrap_check( td, "remediate", {"cve_ids": ["CVE-1"]}, True, "uncallable"), lambda td: unattended.bootstrap_check( td, "remediate", {"cve_ids": ["CVE-1"]}, True, lambda: True), lambda td: unattended.bootstrap_check( td, "remediate", {"cve_ids": ["CVE-1"]}, True, lambda: {}), lambda td: unattended.bootstrap_check( td, "remediate", {"cve_ids": ["CVE-1"]}, True, lambda: {"blocked": 0}), ] for case_fn in cases: with tempfile.TemporaryDirectory() as td: boot = case_fn(td) self.assertEqual(boot["status"], "error") rd = boot["result_data"] self.assertIn("scope", rd) self.assertIn("error", rd) # --------------------------------------------------------------------------- # Round 13: C5 — make-result stdout handoff on persistence failure # --------------------------------------------------------------------------- class TestC5MakeResultPersistenceHandoff(unittest.TestCase): """C5: make-result emits stdout handoff on persistence failure.""" def test_corrupt_old_file_stdout_handoff(self) -> None: """Corrupt old result → stdout JSON with persistence_not_written.""" with tempfile.TemporaryDirectory() as td: # Write corrupt old result result_path = Path(td) / "unattended-result.json" corrupt = "not json {{{" result_path.write_text(corrupt) import io from unittest.mock import patch as _patch stdout_cap = io.StringIO() stderr_cap = io.StringIO() args = argparse.Namespace( status="partial", operation="remediate", artifact_dir=td, data=json.dumps({"scope": {"cve_ids": ["CVE-2024-1"]}}), ) with _patch("sys.stdout", stdout_cap), \ _patch("sys.stderr", stderr_cap): rc = unattended._cmd_make_result(args) self.assertNotEqual(rc, 0) # Old file preserved self.assertEqual(result_path.read_text(), corrupt) # Stdout has handoff JSON stdout_text = stdout_cap.getvalue().strip() self.assertTrue(stdout_text, "stdout must have handoff") handoff = json.loads(stdout_text) self.assertTrue(handoff["persistence_not_written"]) self.assertEqual(handoff["operation"], "remediate") self.assertFalse(handoff["lifecycle_complete"]) def test_nondict_old_file_stdout_handoff(self) -> None: """Non-dict old result → stdout handoff, file preserved.""" with tempfile.TemporaryDirectory() as td: result_path = Path(td) / "unattended-result.json" result_path.write_text("[]") import io from unittest.mock import patch as _patch stdout_cap = io.StringIO() stderr_cap = io.StringIO() args = argparse.Namespace( status="partial", operation="remediate", artifact_dir=td, data=json.dumps({"scope": {"cve_ids": ["CVE-2024-1"]}}), ) with _patch("sys.stdout", stdout_cap), \ _patch("sys.stderr", stderr_cap): rc = unattended._cmd_make_result(args) self.assertNotEqual(rc, 0) self.assertEqual(result_path.read_text(), "[]") handoff = json.loads(stdout_cap.getvalue()) self.assertTrue(handoff["persistence_not_written"]) def test_valid_input_no_old_file_succeeds_normally(self) -> None: """Valid input, no old file → normal success, no handoff marker.""" with tempfile.TemporaryDirectory() as td: import io from unittest.mock import patch as _patch stdout_cap = io.StringIO() args = argparse.Namespace( status="partial", operation="remediate", artifact_dir=td, data=json.dumps({"scope": {"cve_ids": ["CVE-2024-1"]}}), ) with _patch("sys.stdout", stdout_cap): rc = unattended._cmd_make_result(args) self.assertEqual(rc, 0) result = json.loads(stdout_cap.getvalue()) self.assertNotIn("persistence_not_written", result) self.assertEqual(result["status"], "partial") def test_subprocess_corrupt_stdout_parseable(self) -> None: """Subprocess: corrupt old file → nonzero + parseable stdout handoff.""" import subprocess with tempfile.TemporaryDirectory() as td: result_path = Path(td) / "unattended-result.json" result_path.write_text("corrupt{{{") proc = subprocess.run( [sys.executable, "-I", str(_SCRIPT), "make-result", "--status", "partial", "--operation", "remediate", "--artifact-dir", td, "--data", json.dumps({"scope": {"cve_ids": ["CVE-1"]}})], capture_output=True, text=True, timeout=10, ) self.assertNotEqual(proc.returncode, 0) self.assertTrue(proc.stdout.strip()) handoff = json.loads(proc.stdout) self.assertTrue(handoff["persistence_not_written"]) # Old file preserved self.assertEqual(result_path.read_text(), "corrupt{{{") # --------------------------------------------------------------------------- # C: Persistence handoff preserves full canonical metadata # --------------------------------------------------------------------------- class TestPersistenceHandoffCanonicalMetadata(unittest.TestCase): """Persistence error handoff preserves all canonical make_result fields. Verifies that on corrupt/unreadable existing result, the stdout handoff contains the full canonical result fields (pr, evidence, stages, human_decisions, etc.) — not just a reduced 11-key dict. """ _FULL_METADATA = { "scope": {"cve_ids": ["CVE-2024-9999"], "repo": "org/repo"}, "stages_completed": ["start", "scan", "patch"], "stages_remaining": ["validate", "pr"], "evidence": {"scan_tool": "trivy", "validation_verdict": None}, "pr": {"url": "https://github.com/org/repo/pull/99", "state": "open"}, "human_decisions": [{"id": "d1", "accepted": True}], } def test_corrupt_file_preserves_pr_evidence_decisions(self) -> None: """Corrupt old result → handoff has pr, evidence, human_decisions.""" import io from unittest.mock import patch as _patch with tempfile.TemporaryDirectory() as td: result_path = Path(td) / "unattended-result.json" result_path.write_text("corrupt{{{") stdout_cap = io.StringIO() stderr_cap = io.StringIO() args = argparse.Namespace( status="partial", operation="remediate", artifact_dir=td, data=json.dumps(self._FULL_METADATA), ) with _patch("sys.stdout", stdout_cap), \ _patch("sys.stderr", stderr_cap): rc = unattended._cmd_make_result(args) self.assertNotEqual(rc, 0) self.assertEqual(result_path.read_text(), "corrupt{{{") handoff = json.loads(stdout_cap.getvalue()) # Canonical fields preserved self.assertTrue(handoff["persistence_not_written"]) self.assertEqual(handoff["pr"]["url"], "https://github.com/org/repo/pull/99") self.assertEqual(handoff["evidence"]["scan_tool"], "trivy") self.assertEqual(handoff["human_decisions"], [{"id": "d1", "accepted": True}]) self.assertEqual(handoff["stages_completed"], ["start", "scan", "patch"]) self.assertEqual(handoff["stages_remaining"], ["validate", "pr"]) self.assertFalse(handoff["lifecycle_complete"]) self.assertEqual(handoff["next_permitted_operations"], []) def test_nondict_file_preserves_full_metadata(self) -> None: """Non-dict old result → handoff has full canonical metadata.""" import io from unittest.mock import patch as _patch with tempfile.TemporaryDirectory() as td: result_path = Path(td) / "unattended-result.json" result_path.write_text("[]") stdout_cap = io.StringIO() stderr_cap = io.StringIO() args = argparse.Namespace( status="partial", operation="remediate", artifact_dir=td, data=json.dumps(self._FULL_METADATA), ) with _patch("sys.stdout", stdout_cap), \ _patch("sys.stderr", stderr_cap): rc = unattended._cmd_make_result(args) self.assertNotEqual(rc, 0) handoff = json.loads(stdout_cap.getvalue()) self.assertTrue(handoff["persistence_not_written"]) self.assertIn("pr", handoff) self.assertIn("evidence", handoff) self.assertIn("human_decisions", handoff) def test_invalid_data_no_fake_result(self) -> None: """Invalid --data (malformed JSON) → rc2, no stdout result wrapping.""" import io from unittest.mock import patch as _patch with tempfile.TemporaryDirectory() as td: stdout_cap = io.StringIO() stderr_cap = io.StringIO() args = argparse.Namespace( status="partial", operation="remediate", artifact_dir=td, data="not valid json {{{", ) with _patch("sys.stdout", stdout_cap), \ _patch("sys.stderr", stderr_cap): rc = unattended._cmd_make_result(args) self.assertEqual(rc, 2) # No canonical result wrapping for invalid input self.assertEqual(stdout_cap.getvalue().strip(), "") class TestResultToDataCanonicalFields(unittest.TestCase): """_result_to_data passes all canonical fields, not a reduced whitelist.""" def test_passes_pr_and_decisions(self) -> None: """_result_to_data includes pr, human_decisions, etc.""" scope = {"cve_ids": ["CVE-1"]} result = unattended.make_result( status="partial", operation="remediate", scope=scope, pr={"url": "https://example.com/pr/1", "state": "open"}, human_decisions=[{"id": "d1"}], evidence={"tool": "trivy"}, stages_completed=["start"], stages_remaining=["scan"], blockers=[{"type": "test", "detail": "x"}], next_permitted_operations=[{ "operation": "remediate", "prerequisite": "merge", }], ) data = unattended._result_to_data(result, scope) self.assertEqual(data["pr"]["url"], "https://example.com/pr/1") self.assertEqual(data["human_decisions"], [{"id": "d1"}]) self.assertEqual(data["evidence"]["tool"], "trivy") self.assertEqual(data["stages_completed"], ["start"]) self.assertEqual(data["stages_remaining"], ["scan"]) self.assertIn("blockers", data) self.assertIn("next_permitted_operations", data) def test_omits_none_and_empty_defaults(self) -> None: """_result_to_data omits None/empty fields cleanly.""" scope = {"cve_ids": ["CVE-1"]} result = unattended.make_result( status="partial", operation="remediate", scope=scope, stages_completed=[], stages_remaining=[], ) data = unattended._result_to_data(result, scope) self.assertNotIn("pr", data) self.assertNotIn("lifecycle_complete", data) class TestBootstrapResultDataCanonicalFields(unittest.TestCase): """Bootstrap error result_data passes canonical fields through _result_to_data.""" def test_scope_unbound_result_data_has_canonical_keys(self) -> None: """Scope-unbound bootstrap → result_data suitable for make-result.""" with tempfile.TemporaryDirectory() as td: scope = {"cve_ids": ["CVE-2024-1"]} boot = unattended.bootstrap_check( artifact_dir=td, operation="remediate", scope=scope, resolved_scope_bound=False, live_pr_check=lambda: {"blocked": False}, ) self.assertEqual(boot["status"], "needs_input") rd = boot["result_data"] self.assertIn("scope", rd) self.assertIn("error", rd) # Must NOT have only scope+error — should have blockers too self.assertIn("blockers", rd) # --------------------------------------------------------------------------- # Round 13: C7 — AST docstring extraction and execution test # --------------------------------------------------------------------------- class TestC7DocstringExtractExec(unittest.TestCase): """C7: Extract docstring code blocks via AST and execute them. Exercises the actual documented module docstring examples using ast.get_docstring + fenced code block extraction, NOT a copied pattern. Script path has spaces, CWD is unrelated. """ @staticmethod def _extract_code_blocks(docstring: str) -> list[str]: """Extract RST code-block contents from a docstring. Handles the standard RST format where ``.. code-block::`` is followed by a blank line, then indented code lines. A block ends when a non-blank, non-indented line appears. """ blocks: list[str] = [] current: list[str] = [] in_block = False saw_directive = False for line in docstring.splitlines(): stripped = line.strip() if saw_directive: # Expect a blank line after directive, then indented code if not stripped: continue # skip blank separator if line.startswith(" "): in_block = True saw_directive = False current.append(line) continue else: saw_directive = False # Directive with no code — skip if in_block: if not stripped: # Blank lines within the block are preserved current.append("") elif line.startswith(" "): current.append(line) else: # Non-indented non-blank line ends the block # Trim trailing blank lines while current and not current[-1].strip(): current.pop() if current: blocks.append("\n".join(current)) current = [] in_block = False if not in_block and ".. code-block::" in stripped: saw_directive = True current = [] # Flush final block while current and not current[-1].strip(): current.pop() if current: blocks.append("\n".join(current)) return blocks def test_docstring_exists_and_has_code_blocks(self) -> None: """Module docstring contains extractable code blocks.""" import ast as _ast with open(str(_SCRIPT)) as f: tree = _ast.parse(f.read()) docstring = _ast.get_docstring(tree) self.assertIsNotNone(docstring, "Module must have a docstring") self.assertIn("subprocess", docstring) blocks = self._extract_code_blocks(docstring) self.assertGreater(len(blocks), 0, "Docstring must have code blocks") def test_docstring_validate_example_executes(self) -> None: """Execute the validate example from the docstring. Uses the actual script path (with spaces), unrelated CWD, and verifies the JSON output. """ import shutil import subprocess with tempfile.TemporaryDirectory(prefix="doc space ") as td: # Copy script to path with spaces spaced_dir = Path(td) / "path with spaces" / "scripts" spaced_dir.mkdir(parents=True) shutil.copy2(str(_SCRIPT), spaced_dir / "unattended.py") common_src = _SCRIPT.parent / "_common.py" if common_src.exists(): shutil.copy2(common_src, spaced_dir / "_common.py") SCRIPT = str(spaced_dir / "unattended.py") # Unrelated CWD unrelated = Path(td) / "unrelated cwd" unrelated.mkdir() # Write request.json req_path = Path(td) / "request.json" req_path.write_text(json.dumps({ "operation": "remediate", "cve_ids": ["CVE-2024-1234"], })) # Execute the docstring pattern: validate with resolved scope resolved = {"repo_path": "org/repo", "base_branch": "main"} result = subprocess.run( ["python3", "-I", SCRIPT, "validate", str(req_path), "--resolved-scope", json.dumps(resolved)], capture_output=True, text=True, cwd=str(unrelated), ) self.assertEqual(result.returncode, 0, f"validate failed: {result.stderr}") info = json.loads(result.stdout) self.assertTrue(info["artifact_scope_bound"]) artifact_subdir = info["artifact_subdir"] self.assertTrue(artifact_subdir.startswith("scope-")) def test_docstring_make_result_example_executes(self) -> None: """Execute the make-result example from the docstring. Path with spaces, verify file bytes match stdout. """ import shutil import subprocess with tempfile.TemporaryDirectory(prefix="doc exec ") as td: spaced_dir = Path(td) / "path with spaces" / "scripts" spaced_dir.mkdir(parents=True) shutil.copy2(str(_SCRIPT), spaced_dir / "unattended.py") common_src = _SCRIPT.parent / "_common.py" if common_src.exists(): shutil.copy2(common_src, spaced_dir / "_common.py") SCRIPT = str(spaced_dir / "unattended.py") unrelated = Path(td) / "unrelated cwd" unrelated.mkdir() artifact_dir = str(Path(td) / "artifacts" / "cve-fix" / "scope-test") data = {"scope": {"cve_ids": ["CVE-2024-1234"], "repo": "org/repo", "base_branch": "main"}, "stages_completed": ["start", "scan"], "stages_remaining": ["patch", "validate", "pr"]} result = subprocess.run( ["python3", "-I", SCRIPT, "make-result", "--status", "partial", "--operation", "remediate", "--artifact-dir", artifact_dir, "--data", json.dumps(data)], capture_output=True, text=True, cwd=str(unrelated), ) self.assertEqual(result.returncode, 0, f"make-result failed: {result.stderr}") # Verify file exists and bytes match stdout result_file = Path(artifact_dir) / "unattended-result.json" self.assertTrue(result_file.exists()) file_data = json.loads(result_file.read_text()) stdout_data = json.loads(result.stdout) self.assertEqual(file_data, stdout_data) self.assertEqual(file_data["status"], "partial") def test_docstring_corrupt_result_stdout_fallback(self) -> None: """Corrupt existing result: stdout fallback parsed on nonzero. Verifies file bytes unchanged and full JSON parsed from stdout. """ import shutil import subprocess with tempfile.TemporaryDirectory(prefix="corrupt ") as td: spaced_dir = Path(td) / "path with spaces" / "scripts" spaced_dir.mkdir(parents=True) shutil.copy2(str(_SCRIPT), spaced_dir / "unattended.py") common_src = _SCRIPT.parent / "_common.py" if common_src.exists(): shutil.copy2(common_src, spaced_dir / "_common.py") SCRIPT = str(spaced_dir / "unattended.py") artifact_dir = str(Path(td) / "artifacts") os.makedirs(artifact_dir) # Write corrupt existing result corrupt_content = "not valid json {{{" result_path = Path(artifact_dir) / "unattended-result.json" result_path.write_text(corrupt_content) data = {"scope": {"cve_ids": ["CVE-2024-1"]}, "stages_completed": ["start"]} proc = subprocess.run( ["python3", "-I", SCRIPT, "make-result", "--status", "partial", "--operation", "remediate", "--artifact-dir", artifact_dir, "--data", json.dumps(data)], capture_output=True, text=True, cwd=td, ) # Nonzero exit self.assertNotEqual(proc.returncode, 0) # Old file bytes unchanged self.assertEqual(result_path.read_text(), corrupt_content) # Full JSON parsed from stdout (handoff) self.assertTrue(proc.stdout.strip(), "stdout must contain handoff JSON") handoff = json.loads(proc.stdout) self.assertTrue(handoff.get("persistence_not_written")) self.assertEqual(handoff["operation"], "remediate") self.assertFalse(handoff["lifecycle_complete"]) # --------------------------------------------------------------------------- # B-scope: Effective feature_branch resolution and consumer wiring # --------------------------------------------------------------------------- class TestEffectiveFeatureBranchResolution(unittest.TestCase): """B1: effective_feature_branch shared by derive_artifact_subdir and CLI. Verifies: resolved_scope.feature_branch precedence, request.branch contradiction error, same effective branch aliasing, direct API and CLI isolation of request.branch=A vs B. """ def test_resolved_feature_branch_takes_precedence_over_derive(self) -> None: """Resolved verified feature_branch overrides derive_branch default.""" import io from unittest.mock import patch as _patch req = { "operation": "remediate", "cve_ids": ["CVE-2024-1234"], } rs = { "repo_path": "org/repo", "base_branch": "main", "feature_branch": "cve-fix/existing-pr-branch", } with tempfile.NamedTemporaryFile( mode="w", suffix=".json", delete=False ) as f: json.dump(req, f) f.flush() try: captured = io.StringIO() args = argparse.Namespace( request_file=f.name, resolved_scope=json.dumps(rs), ) with _patch("sys.stdout", captured): rc = unattended._cmd_validate(args) self.assertEqual(rc, 0) info = json.loads(captured.getvalue()) # CLI output branch is the resolved feature_branch self.assertEqual( info["branch"], "cve-fix/existing-pr-branch", ) finally: os.unlink(f.name) def test_contradictory_request_branch_fails_before_writes(self) -> None: """request.branch contradicting resolved feature_branch → exit 1.""" import io from unittest.mock import patch as _patch req = { "operation": "remediate", "cve_ids": ["CVE-2024-1234"], "branch": "cve-fix/my-explicit-branch", } rs = { "repo_path": "org/repo", "base_branch": "main", "feature_branch": "cve-fix/existing-pr-branch", } with tempfile.NamedTemporaryFile( mode="w", suffix=".json", delete=False ) as f: json.dump(req, f) f.flush() try: captured = io.StringIO() err_captured = io.StringIO() args = argparse.Namespace( request_file=f.name, resolved_scope=json.dumps(rs), ) with _patch("sys.stdout", captured), \ _patch("sys.stderr", err_captured): rc = unattended._cmd_validate(args) self.assertEqual(rc, 1, "Must fail on contradiction") output = json.loads(captured.getvalue()) self.assertFalse(output["valid"]) self.assertTrue( any("contradict" in e.lower() for e in output["errors"]), f"Expected contradiction error, got: {output['errors']}", ) finally: os.unlink(f.name) def test_same_explicit_branch_no_contradiction(self) -> None: """request.branch matching resolved feature_branch → no error.""" import io from unittest.mock import patch as _patch req = { "operation": "remediate", "cve_ids": ["CVE-2024-1234"], "branch": "cve-fix/same-branch", } rs = { "repo_path": "org/repo", "base_branch": "main", "feature_branch": "cve-fix/same-branch", } with tempfile.NamedTemporaryFile( mode="w", suffix=".json", delete=False ) as f: json.dump(req, f) f.flush() try: captured = io.StringIO() args = argparse.Namespace( request_file=f.name, resolved_scope=json.dumps(rs), ) with _patch("sys.stdout", captured): rc = unattended._cmd_validate(args) self.assertEqual(rc, 0, "Same branch must pass") info = json.loads(captured.getvalue()) self.assertEqual(info["branch"], "cve-fix/same-branch") finally: os.unlink(f.name) def test_direct_api_isolates_branch_a_vs_b(self) -> None: """derive_artifact_subdir with request.branch A vs B → different subdirs. Direct API callers pass request.branch without going through _cmd_validate. Same repo/base, different request.branch must produce distinct artifact subdirectories. """ context = "CVE-2024-1234" rs = {"repo_path": "org/repo", "base_branch": "main"} cases = [ # (request.branch A, request.branch B) ("cve-fix/branch-a", "cve-fix/branch-b"), ("feature/x", "feature/y"), ] for branch_a, branch_b in cases: with self.subTest(a=branch_a, b=branch_b): req_a = { "operation": "remediate", "cve_ids": ["CVE-2024-1234"], "branch": branch_a, } req_b = { "operation": "remediate", "cve_ids": ["CVE-2024-1234"], "branch": branch_b, } sub_a = unattended.derive_artifact_subdir(req_a, context, rs) sub_b = unattended.derive_artifact_subdir(req_b, context, rs) self.assertNotEqual(sub_a, sub_b) def test_direct_api_rs_feature_branch_isolates(self) -> None: """derive_artifact_subdir with rs.feature_branch A vs B → different.""" req = { "operation": "remediate", "cve_ids": ["CVE-2024-1234"], } context = unattended.derive_context(req) rs_a = { "repo_path": "org/repo", "base_branch": "main", "feature_branch": "cve-fix/branch-a", } rs_b = { "repo_path": "org/repo", "base_branch": "main", "feature_branch": "cve-fix/branch-b", } sub_a = unattended.derive_artifact_subdir(req, context, rs_a) sub_b = unattended.derive_artifact_subdir(req, context, rs_b) self.assertNotEqual(sub_a, sub_b) def test_direct_api_implicit_default_vs_explicit_same_aliases(self) -> None: """No request.branch vs same-as-default request.branch: same when rs has feature_branch.""" req_no_branch = { "operation": "remediate", "cve_ids": ["CVE-2024-1234"], } context = unattended.derive_context(req_no_branch) branch = "cve-fix/already-set" rs = { "repo_path": "org/repo", "base_branch": "main", "feature_branch": branch, } # With rs.feature_branch, request.branch is irrelevant req_with_branch = dict(req_no_branch, branch=branch) sub_no = unattended.derive_artifact_subdir(req_no_branch, context, rs) sub_with = unattended.derive_artifact_subdir(req_with_branch, context, rs) self.assertEqual(sub_no, sub_with) def test_cli_isolates_branch_a_vs_b(self) -> None: """CLI validate with branch A vs B → different artifact_subdir.""" import subprocess req = { "operation": "remediate", "cve_ids": ["CVE-2024-1234"], } with tempfile.TemporaryDirectory() as td: req_file = os.path.join(td, "request.json") with open(req_file, "w") as f: json.dump(req, f) rs_a = { "repo_path": "org/repo", "base_branch": "main", "feature_branch": "cve-fix/branch-a", } rs_b = { "repo_path": "org/repo", "base_branch": "main", "feature_branch": "cve-fix/branch-b", } r_a = subprocess.run( [sys.executable, "-I", str(_SCRIPT), "validate", req_file, "--resolved-scope", json.dumps(rs_a)], capture_output=True, text=True, ) self.assertEqual(r_a.returncode, 0, r_a.stderr) out_a = json.loads(r_a.stdout) r_b = subprocess.run( [sys.executable, "-I", str(_SCRIPT), "validate", req_file, "--resolved-scope", json.dumps(rs_b)], capture_output=True, text=True, ) self.assertEqual(r_b.returncode, 0, r_b.stderr) out_b = json.loads(r_b.stdout) self.assertNotEqual( out_a["artifact_subdir"], out_b["artifact_subdir"], ) self.assertEqual(out_a["branch"], "cve-fix/branch-a") self.assertEqual(out_b["branch"], "cve-fix/branch-b") class TestEffectiveFeatureBranchConsumerWiring(unittest.TestCase): """B2: Wired consumer paths — CLI branch to publication alignment. Verifies: selected rs.feature_branch → CLI branch match, contradictory request fails before writes, known PR missing verified branch yields unbound. """ def test_selected_feature_branch_matches_cli_output(self) -> None: """resolved_scope.feature_branch → info['branch'] exact match.""" import subprocess req = { "operation": "remediate", "cve_ids": ["CVE-2024-5678"], } rs = { "repo_path": "org/repo", "base_branch": "release-1.0", "feature_branch": "cve-fix/EDM-5678-release-scoped", } with tempfile.TemporaryDirectory() as td: req_file = os.path.join(td, "request.json") with open(req_file, "w") as f: json.dump(req, f) r = subprocess.run( [sys.executable, "-I", str(_SCRIPT), "validate", req_file, "--resolved-scope", json.dumps(rs)], capture_output=True, text=True, ) self.assertEqual(r.returncode, 0, r.stderr) info = json.loads(r.stdout) self.assertEqual( info["branch"], "cve-fix/EDM-5678-release-scoped", ) self.assertTrue(info["artifact_scope_bound"]) def test_known_pr_missing_verified_branch_yields_unbound(self) -> None: """Existing PR with resolved scope but no feature_branch → scope NOT bound. Continuation (pr_url/work_ref) WITHOUT verified feature_branch in resolved_scope must set artifact_scope_bound=False. The caller must look up the PR head branch and revalidate with it in resolved_scope.feature_branch before any writable operations. """ import subprocess req = { "operation": "remediate", "pr_url": "https://github.com/org/repo/pull/42", "cve_ids": ["CVE-2024-1234"], } # Resolved scope has repo/base but NO feature_branch rs = {"repo_path": "org/repo", "base_branch": "main"} with tempfile.TemporaryDirectory() as td: req_file = os.path.join(td, "request.json") with open(req_file, "w") as f: json.dump(req, f) r = subprocess.run( [sys.executable, "-I", str(_SCRIPT), "validate", req_file, "--resolved-scope", json.dumps(rs)], capture_output=True, text=True, ) self.assertEqual(r.returncode, 0, r.stderr) info = json.loads(r.stdout) # Continuation without verified branch → unbound self.assertFalse(info["artifact_scope_bound"]) def test_known_pr_with_verified_branch_yields_bound(self) -> None: """Existing PR with resolved scope AND feature_branch → bound.""" import subprocess req = { "operation": "remediate", "pr_url": "https://github.com/org/repo/pull/42", "cve_ids": ["CVE-2024-1234"], } rs = { "repo_path": "org/repo", "base_branch": "main", "feature_branch": "cve-fix/verified-branch", } with tempfile.TemporaryDirectory() as td: req_file = os.path.join(td, "request.json") with open(req_file, "w") as f: json.dump(req, f) r = subprocess.run( [sys.executable, "-I", str(_SCRIPT), "validate", req_file, "--resolved-scope", json.dumps(rs)], capture_output=True, text=True, ) self.assertEqual(r.returncode, 0, r.stderr) info = json.loads(r.stdout) self.assertTrue(info["artifact_scope_bound"]) self.assertEqual(info["branch"], "cve-fix/verified-branch") class TestEffectiveFeatureBranchDistinctBases(unittest.TestCase): """B3: Protected main vs release resolved base → distinct branches. Also verifies: same implicit default vs explicit same branch aliasing, input reorder stable. """ def test_main_vs_release_distinct_branches(self) -> None: """Same CVE on main vs release-1.0 → different effective branches.""" req = { "operation": "remediate", "cve_ids": ["CVE-2024-1234"], } context = unattended.derive_context(req) rs_main = {"repo_path": "org/repo", "base_branch": "main"} rs_release = {"repo_path": "org/repo", "base_branch": "release-1.0"} branch_main = unattended.derive_branch(req, context, rs_main) branch_release = unattended.derive_branch(req, context, rs_release) self.assertNotEqual(branch_main, branch_release) self.assertTrue(branch_main.startswith("cve-fix/")) self.assertTrue(branch_release.startswith("cve-fix/")) def test_omitted_request_base_with_resolved_base_generates_branch(self) -> None: """Omitted request base_branch with resolved base → scoped branch.""" req = { "operation": "remediate", "cve_ids": ["CVE-2024-1234"], # No base_branch in request } context = unattended.derive_context(req) rs = {"repo_path": "org/repo", "base_branch": "main"} branch = unattended.derive_branch(req, context, rs) # Should be scoped, not plain cve-fix/{context} self.assertRegex(branch, r"^cve-fix/CVE-2024-1234-[0-9a-f]{8}$") def test_same_implicit_explicit_branch_same_artifact(self) -> None: """Implicit default and explicit same value → same artifact dir.""" req = { "operation": "remediate", "cve_ids": ["CVE-2024-1234"], } context = unattended.derive_context(req) rs_base = {"repo_path": "org/repo", "base_branch": "main"} # Get the implicit default default_branch = unattended.derive_branch(req, context, rs_base) # Explicitly set the same branch in resolved_scope rs_explicit = {**rs_base, "feature_branch": default_branch} rs_implicit = {**rs_base, "feature_branch": default_branch} sub_explicit = unattended.derive_artifact_subdir( req, context, rs_explicit, ) sub_implicit = unattended.derive_artifact_subdir( req, context, rs_implicit, ) self.assertEqual(sub_explicit, sub_implicit) def test_input_reorder_stable(self) -> None: """Reordered CVE lists with same branch → same artifact dir.""" req1 = { "operation": "remediate", "cve_ids": ["CVE-2024-B", "CVE-2024-A"], } req2 = { "operation": "remediate", "cve_ids": ["CVE-2024-A", "CVE-2024-B"], } rs = { "repo_path": "org/repo", "base_branch": "main", "feature_branch": "cve-fix/fixed-branch", } ctx = "CVE-2024-A" # sorted first sub1 = unattended.derive_artifact_subdir(req1, ctx, rs) sub2 = unattended.derive_artifact_subdir(req2, ctx, rs) self.assertEqual(sub1, sub2) class TestEffectiveFeatureBranchArtifactIsolation(unittest.TestCase): """B3(a): Same-base explicit A vs B writes to separate namespaces. Branch A's artifacts must be unchanged after Branch B bootstrap/write. """ def test_branch_a_original_path_bytes_unchanged_after_branch_b(self) -> None: """A ORIGINAL PATH+BYTES+PR identity unchanged after B write.""" with tempfile.TemporaryDirectory() as td: base_dir = Path(td) req = {"operation": "remediate", "cve_ids": ["CVE-2024-1234"]} context = unattended.derive_context(req) rs_a = { "repo_path": "org/repo", "base_branch": "main", "feature_branch": "cve-fix/branch-a", } rs_b = { "repo_path": "org/repo", "base_branch": "main", "feature_branch": "cve-fix/branch-b", } sub_a = unattended.derive_artifact_subdir(req, context, rs_a) sub_b = unattended.derive_artifact_subdir(req, context, rs_b) self.assertNotEqual(sub_a, sub_b) # Create A's artifacts with known content dir_a = base_dir / sub_a dir_a.mkdir(parents=True) result_a = { "status": "partial", "pr": { "url": "https://github.com/org/repo/pull/10", "branch": "cve-fix/branch-a", }, } result_a_json = json.dumps(result_a, indent=2) (dir_a / "unattended-result.json").write_text(result_a_json) a_path = str(dir_a / "unattended-result.json") a_bytes = (dir_a / "unattended-result.json").read_bytes() # Write B's result via make-result CLI dir_b = base_dir / sub_b import subprocess data_b = { "scope": { "cve_ids": ["CVE-2024-1234"], "repo": "org/repo", "base_branch": "main", }, "stages_completed": ["start"], "stages_remaining": ["scan", "patch", "validate", "pr"], } r = subprocess.run( [sys.executable, "-I", str(_SCRIPT), "make-result", "--status", "partial", "--operation", "remediate", "--artifact-dir", str(dir_b), "--data", json.dumps(data_b)], capture_output=True, text=True, ) self.assertEqual(r.returncode, 0, r.stderr) # A's file bytes are EXACTLY unchanged self.assertEqual( (dir_a / "unattended-result.json").read_bytes(), a_bytes, "Branch A artifacts must not be modified by branch B write", ) # A's PR identity intact a_reloaded = json.loads( (dir_a / "unattended-result.json").read_text() ) self.assertEqual( a_reloaded["pr"]["url"], "https://github.com/org/repo/pull/10", ) self.assertEqual( a_reloaded["pr"]["branch"], "cve-fix/branch-a", ) # B's result is in separate directory b_result_path = dir_b / "unattended-result.json" self.assertTrue(b_result_path.exists()) b_loaded = json.loads(b_result_path.read_text()) self.assertEqual(b_loaded["status"], "partial") class TestEffectiveFeatureBranchProducerSnippet(unittest.TestCase): """B3(d): Execute actual modified producer snippet to verify effective branch. Runs the actual _cmd_validate function (the modified producer) and verifies the effective branch is used in the output — not a copied pattern or parser-only test. """ def test_producer_snippet_returns_effective_branch(self) -> None: """_cmd_validate with resolved feature_branch returns it in output.""" import io from unittest.mock import patch as _patch req = { "operation": "remediate", "cve_ids": ["CVE-2024-9999"], "jira_tickets": ["SEC-100"], } rs = { "repo_path": "org/test-repo", "base_branch": "release-2.0", "feature_branch": "cve-fix/SEC-100-release-2.0-abcdef12", } with tempfile.NamedTemporaryFile( mode="w", suffix=".json", delete=False ) as f: json.dump(req, f) f.flush() try: captured = io.StringIO() args = argparse.Namespace( request_file=f.name, resolved_scope=json.dumps(rs), ) with _patch("sys.stdout", captured): rc = unattended._cmd_validate(args) self.assertEqual(rc, 0, "Producer snippet must succeed") info = json.loads(captured.getvalue()) # The effective branch is the resolved feature_branch self.assertEqual( info["branch"], "cve-fix/SEC-100-release-2.0-abcdef12", ) # Context is still the primary identifier self.assertEqual(info["context"], "SEC-100") # Scope is bound self.assertTrue(info["artifact_scope_bound"]) # Artifact subdir includes the branch in its hash self.assertTrue( info["artifact_subdir"].startswith("scope-"), ) finally: os.unlink(f.name) def test_producer_without_feature_branch_uses_derived(self) -> None: """_cmd_validate without feature_branch in rs uses derive_branch.""" import io from unittest.mock import patch as _patch req = { "operation": "remediate", "cve_ids": ["CVE-2024-9999"], } rs = { "repo_path": "org/test-repo", "base_branch": "main", } context = unattended.derive_context(req) expected_branch = unattended.derive_branch(req, context, rs) with tempfile.NamedTemporaryFile( mode="w", suffix=".json", delete=False ) as f: json.dump(req, f) f.flush() try: captured = io.StringIO() args = argparse.Namespace( request_file=f.name, resolved_scope=json.dumps(rs), ) with _patch("sys.stdout", captured): rc = unattended._cmd_validate(args) self.assertEqual(rc, 0) info = json.loads(captured.getvalue()) self.assertEqual(info["branch"], expected_branch) finally: os.unlink(f.name) # --------------------------------------------------------------------------- # D4: validate_backport_plan topology / whole-change proof # --------------------------------------------------------------------------- class TestValidateBackportPlan(unittest.TestCase): """D4: validate_backport_plan topology classification and whole-change proof.""" _BASE = { "pr_url": "https://github.com/org/repo/pull/42", "head_ref_oid": "abc123", } def test_normal_merge_2_parents_ready(self) -> None: """2-parent merge commit → ready with -m 1 argv.""" plan = unattended.validate_backport_plan( pr_state="MERGED", merge_commit_sha="deadbeef", parent_count=2, pr_commits_count=3, **self._BASE, ) self.assertEqual(plan["status"], "ready") self.assertTrue(plan["whole_change"]) self.assertIn("-m", plan["cherry_pick_argv"]) self.assertIn("1", plan["cherry_pick_argv"]) self.assertIn("deadbeef", plan["cherry_pick_argv"]) self.assertEqual(plan["plan_evidence"]["topology"], "merge_commit") def test_single_commit_landing_1_parent_ready(self) -> None: """1-parent commit with 1 PR commit → ready (squash or single-commit).""" plan = unattended.validate_backport_plan( pr_state="MERGED", merge_commit_sha="abc123def", parent_count=1, pr_commits_count=1, **self._BASE, ) self.assertEqual(plan["status"], "ready") self.assertTrue(plan["whole_change"]) self.assertNotIn("-m", plan["cherry_pick_argv"]) self.assertIn("abc123def", plan["cherry_pick_argv"]) self.assertEqual( plan["plan_evidence"]["topology"], "single_commit_landing", ) def test_multi_commit_squash_ambiguous_needs_input(self) -> None: """1-parent commit with multiple PR commits → needs_input.""" plan = unattended.validate_backport_plan( pr_state="MERGED", merge_commit_sha="deadbeef", parent_count=1, pr_commits_count=5, **self._BASE, ) self.assertEqual(plan["status"], "needs_input") self.assertFalse(plan["whole_change"]) self.assertIsNone(plan["cherry_pick_argv"]) self.assertIn("5-commit", plan["error"]) self.assertEqual( plan["plan_evidence"]["topology"], "ambiguous_single_parent", ) def test_unknown_commit_count_needs_input(self) -> None: """1-parent commit with unknown PR commit count → needs_input.""" plan = unattended.validate_backport_plan( pr_state="MERGED", merge_commit_sha="deadbeef", parent_count=1, pr_commits_count=None, **self._BASE, ) self.assertEqual(plan["status"], "needs_input") self.assertFalse(plan["whole_change"]) self.assertIn("unknown", plan["error"]) def test_root_commit_0_parents_blocked(self) -> None: """0 parents (root commit) → blocked.""" plan = unattended.validate_backport_plan( pr_state="MERGED", merge_commit_sha="rootcommit", parent_count=0, **self._BASE, ) self.assertEqual(plan["status"], "blocked") self.assertFalse(plan["whole_change"]) self.assertIn("0 parents", plan["error"]) def test_octopus_3_parents_blocked(self) -> None: """3+ parents (octopus merge) → blocked.""" plan = unattended.validate_backport_plan( pr_state="MERGED", merge_commit_sha="octopus", parent_count=3, **self._BASE, ) self.assertEqual(plan["status"], "blocked") self.assertFalse(plan["whole_change"]) self.assertIn("3 parents", plan["error"]) def test_open_pr_awaiting_merge(self) -> None: """Open PR → awaiting_merge.""" plan = unattended.validate_backport_plan( pr_state="OPEN", merge_commit_sha=None, parent_count=0, **self._BASE, ) self.assertEqual(plan["status"], "awaiting_merge") self.assertFalse(plan["whole_change"]) def test_closed_pr_blocked(self) -> None: """Closed (not merged) PR → blocked.""" plan = unattended.validate_backport_plan( pr_state="CLOSED", merge_commit_sha=None, parent_count=0, **self._BASE, ) self.assertEqual(plan["status"], "blocked") self.assertIn("CLOSED", plan["error"]) def test_missing_merge_commit_blocked(self) -> None: """MERGED but no mergeCommit.oid → blocked.""" plan = unattended.validate_backport_plan( pr_state="MERGED", merge_commit_sha=None, parent_count=0, **self._BASE, ) self.assertEqual(plan["status"], "blocked") self.assertIn("mergeCommit", plan["error"]) def test_pr_url_in_evidence(self) -> None: """Plan evidence always contains the source PR URL.""" plan = unattended.validate_backport_plan( pr_state="MERGED", merge_commit_sha="sha", parent_count=2, pr_commits_count=1, **self._BASE, ) self.assertEqual( plan["plan_evidence"]["pr_url"], self._BASE["pr_url"], ) def test_select_cherry_pick_argv_consistent(self) -> None: """validate_backport_plan argv matches select_cherry_pick_argv.""" # 2 parents → should match select_cherry_pick_argv plan_2 = unattended.validate_backport_plan( pr_state="MERGED", merge_commit_sha="abc", parent_count=2, pr_commits_count=1, **self._BASE, ) argv_2 = unattended.select_cherry_pick_argv( "abc", ["parent a", "parent b"], ) self.assertEqual(plan_2["cherry_pick_argv"], argv_2) # 1 parent → should match plan_1 = unattended.validate_backport_plan( pr_state="MERGED", merge_commit_sha="abc", parent_count=1, pr_commits_count=1, **self._BASE, ) argv_1 = unattended.select_cherry_pick_argv( "abc", ["parent a"], ) self.assertEqual(plan_1["cherry_pick_argv"], argv_1) class TestValidateBackportPlanFetchOrder(unittest.TestCase): """D4: Verify fetch-before-cat-file is enforced in shallow checkouts. Uses a temp Git repo with a shallow clone to prove that git cat-file fails without fetch and succeeds after. """ def test_shallow_missing_object_then_fetch(self) -> None: """In a shallow clone, source commit is missing until fetched.""" import subprocess with tempfile.TemporaryDirectory(prefix="fetch-order-") as td: origin = Path(td) / "origin" origin.mkdir() # Create origin repo with 2 commits subprocess.run( ["git", "init", str(origin)], capture_output=True, check=True, ) subprocess.run( ["git", "-C", str(origin), "config", "user.email", "test@test.com"], capture_output=True, check=True, ) subprocess.run( ["git", "-C", str(origin), "config", "user.name", "Test"], capture_output=True, check=True, ) (origin / "a.txt").write_text("a") subprocess.run( ["git", "-C", str(origin), "add", "a.txt"], capture_output=True, check=True, ) subprocess.run( ["git", "-C", str(origin), "commit", "-m", "first"], capture_output=True, check=True, ) first_sha = subprocess.run( ["git", "-C", str(origin), "rev-parse", "HEAD"], capture_output=True, text=True, check=True, ).stdout.strip() (origin / "b.txt").write_text("b") subprocess.run( ["git", "-C", str(origin), "add", "b.txt"], capture_output=True, check=True, ) subprocess.run( ["git", "-C", str(origin), "commit", "-m", "second"], capture_output=True, check=True, ) # Shallow clone (depth=1) — only gets latest commit. # Use file:// protocol to prevent local hardlinks that # would make all objects available despite --depth 1. shallow = Path(td) / "shallow" subprocess.run( ["git", "clone", "--depth", "1", f"file://{origin}", str(shallow)], capture_output=True, check=True, ) # cat-file on the first commit should FAIL (not fetched) cat_before = subprocess.run( ["git", "-C", str(shallow), "cat-file", "-p", first_sha], capture_output=True, text=True, ) self.assertNotEqual( cat_before.returncode, 0, "cat-file should fail on unfetched object", ) # Fetch the specific commit subprocess.run( ["git", "-C", str(shallow), "fetch", "origin", first_sha], capture_output=True, check=True, ) # Now cat-file should succeed cat_after = subprocess.run( ["git", "-C", str(shallow), "cat-file", "-p", first_sha], capture_output=True, text=True, ) self.assertEqual( cat_after.returncode, 0, f"cat-file should succeed after fetch: {cat_after.stderr}", ) # Verify parent line exists parent_lines = [ line for line in cat_after.stdout.splitlines() if line.startswith("parent ") ] # First commit has 0 parents (it's the initial commit) self.assertEqual(len(parent_lines), 0) class TestValidateBackportPlanSourceResolution(unittest.TestCase): """D4: Source PR must come from request.pr_url, not cve-fix/{context}.""" def test_different_request_pr_vs_context_branch(self) -> None: """validate_backport_plan uses the supplied pr_url, not a branch name.""" # The function takes pr_url directly — the caller resolves it # from the request, not from cve-fix/{context}. plan = unattended.validate_backport_plan( pr_state="MERGED", merge_commit_sha="abc123", head_ref_oid="head1", pr_url="https://github.com/org/repo/pull/99", parent_count=2, pr_commits_count=1, ) self.assertEqual(plan["status"], "ready") self.assertEqual( plan["plan_evidence"]["pr_url"], "https://github.com/org/repo/pull/99", ) def test_unmerged_source_blocks_before_mutation(self) -> None: """Unmerged PR blocks BEFORE any cherry-pick/branch change.""" plan = unattended.validate_backport_plan( pr_state="OPEN", merge_commit_sha=None, head_ref_oid="head1", pr_url="https://github.com/org/repo/pull/99", parent_count=0, ) self.assertEqual(plan["status"], "awaiting_merge") # No cherry_pick_argv — no mutation possible self.assertIsNone(plan["cherry_pick_argv"]) # --------------------------------------------------------------------------- # C1: bootstrap persist_allowed marker # --------------------------------------------------------------------------- class TestBootstrapPersistAllowed(unittest.TestCase): """C1: bootstrap_check returns persist_allowed marker.""" def test_scope_unbound_persist_not_allowed(self) -> None: """Scope unbound → persist_allowed=False, result in boot.""" with tempfile.TemporaryDirectory() as td: boot = unattended.bootstrap_check( artifact_dir=td, operation="remediate", scope={"cve_ids": ["CVE-2024-1234"]}, resolved_scope_bound=False, live_pr_check=lambda: {"blocked": False}, ) self.assertEqual(boot["status"], "needs_input") self.assertFalse(boot["persist_allowed"]) # Canonical result available for stdout handoff self.assertIn("result", boot) self.assertEqual(boot["result"]["status"], "needs_input") # No result file written self.assertFalse( (Path(td) / "unattended-result.json").exists(), ) def test_scope_unbound_zero_writes(self) -> None: """Scope unbound → zero mkdir/write calls on artifact_dir.""" # Use a nonexistent dir — if bootstrap tries to mkdir, it # would create it; we verify it doesn't. nonexistent = Path(tempfile.mkdtemp()) / "does_not_exist" boot = unattended.bootstrap_check( artifact_dir=str(nonexistent), operation="remediate", scope={"cve_ids": ["CVE-2024-1"]}, resolved_scope_bound=False, live_pr_check=lambda: {"blocked": False}, ) self.assertFalse(boot["persist_allowed"]) self.assertFalse(nonexistent.exists()) def test_scope_bound_blocked_persist_allowed(self) -> None: """Scope bound but blocked by live PR → persist_allowed=True.""" with tempfile.TemporaryDirectory() as td: boot = unattended.bootstrap_check( artifact_dir=td, operation="remediate", scope={"cve_ids": ["CVE-2024-1"]}, resolved_scope_bound=True, live_pr_check=lambda: {"blocked": True, "reason": "test block"}, ) self.assertEqual(boot["status"], "blocked") self.assertTrue(boot["persist_allowed"]) def test_scope_bound_error_persist_allowed(self) -> None: """Scope bound with live_pr_check error → persist_allowed=True.""" with tempfile.TemporaryDirectory() as td: boot = unattended.bootstrap_check( artifact_dir=td, operation="remediate", scope={"cve_ids": ["CVE-2024-1"]}, resolved_scope_bound=True, live_pr_check="not callable", ) self.assertEqual(boot["status"], "error") self.assertTrue(boot["persist_allowed"]) # --------------------------------------------------------------------------- # C2: _cmd_make_result write error preserves recovered history # --------------------------------------------------------------------------- class TestMakeResultWriteErrorRecoveredHistory(unittest.TestCase): """C2: Write error after successful old read uses rebuilt result.""" def test_write_failure_preserves_recovered_history(self) -> None: """Valid old VEX record + write failure → stdout retains ALL history.""" import shutil import subprocess with tempfile.TemporaryDirectory() as td: artifact_dir = Path(td) / "artifacts" artifact_dir.mkdir() # Write a valid old result with flat VEX history old_result = { "status": "completed", "operation": "remediate", "scope": {"cve_ids": ["CVE-2024-1"], "repo": "org/r", "base_branch": "main"}, "stages_completed": ["start", "scan"], "stages_remaining": [], "evidence": {"scan_verdict": "fixed", "validation_verdict": "PASS", "vex_justification": "component_not_present"}, "pr": {"url": "https://github.com/org/r/pull/1", "state": "merged"}, "backport_results": None, "jira_results": [], "blockers": [], "human_decisions": [], "next_permitted_operations": [], "artifacts_dir": str(artifact_dir), "error": None, "lifecycle_complete": False, "target_branches": None, "expected_tickets": None, "expected_jira_action": None, "prior_operations": [], "timestamp": "2024-01-01T00:00:00Z", } result_path = artifact_dir / "unattended-result.json" result_path.write_text(json.dumps(old_result, indent=2)) original_bytes = result_path.read_bytes() # Make dir read-only (r-x) so mkstemp fails but read works os.chmod(str(artifact_dir), 0o555) try: # New incoming partial result data = { "scope": {"cve_ids": ["CVE-2024-1"], "repo": "org/r", "base_branch": "main"}, "stages_completed": ["start"], "stages_remaining": ["scan"], } proc = subprocess.run( ["python3", "-I", str(_SCRIPT), "make-result", "--status", "partial", "--operation", "remediate", "--artifact-dir", str(artifact_dir), "--data", json.dumps(data)], capture_output=True, text=True, ) # Must be nonzero (write failure) self.assertEqual(proc.returncode, 2) # Original file unchanged self.assertEqual(result_path.read_bytes(), original_bytes) # stdout has canonical handoff with ALL recovered history self.assertTrue( proc.stdout.strip(), "stdout must contain handoff JSON", ) handoff = json.loads(proc.stdout) self.assertTrue(handoff.get("persistence_not_written")) self.assertEqual(handoff["operation"], "remediate") # C2 key check: prior_operations includes the old result po = handoff.get("prior_operations", []) self.assertGreater( len(po), 0, "Handoff must contain recovered prior_operations " "from the old result, not just incoming history", ) # The snapshot of the old result is in prior_operations old_snapshot = po[-1] self.assertEqual(old_snapshot["status"], "completed") self.assertIn( "vex_justification", old_snapshot.get("evidence", {}), ) finally: os.chmod(str(artifact_dir), 0o755) # --------------------------------------------------------------------------- # C3: Docstring extraction via ast.get_docstring + exec # --------------------------------------------------------------------------- class TestC3DocstringExtractAndExec(unittest.TestCase): """C3: Extract docstring examples via ast.get_docstring and execute. Fixtures inject SCRIPT path and input files; the example logic comes from the ACTUAL docstring extraction, not copied test code. """ @staticmethod def _extract_rst_code_blocks(docstring: str) -> list[str]: """Extract RST ``.. code-block::`` contents from a docstring. Returns dedented code blocks suitable for exec(). """ import textwrap blocks: list[str] = [] current: list[str] = [] in_block = False saw_directive = False for line in docstring.splitlines(): stripped = line.strip() if saw_directive: if not stripped: continue # skip blank separator if line.startswith(" "): in_block = True saw_directive = False current.append(line) continue else: saw_directive = False if in_block: if not stripped: current.append("") elif line.startswith(" "): current.append(line) else: while current and not current[-1].strip(): current.pop() if current: blocks.append(textwrap.dedent("\n".join(current))) current = [] in_block = False if not in_block and ".. code-block::" in stripped: saw_directive = True current = [] while current and not current[-1].strip(): current.pop() if current: blocks.append(textwrap.dedent("\n".join(current))) return blocks def test_docstring_extracted_validate_executes(self) -> None: """Extract validate pattern from docstring via AST, exec with fixtures.""" import ast as _ast import shutil import subprocess import textwrap with open(str(_SCRIPT)) as f: tree = _ast.parse(f.read()) docstring = _ast.get_docstring(tree) self.assertIsNotNone(docstring, "Module must have a docstring") blocks = self._extract_rst_code_blocks(docstring) self.assertGreater(len(blocks), 0, "Docstring must have code blocks") # Find the block containing validate + make-result example main_block = None for block in blocks: if "validate" in block and "request.json" in block: main_block = block break self.assertIsNotNone( main_block, "Docstring must contain a validate example block", ) with tempfile.TemporaryDirectory(prefix="c3 space ") as td: # Copy script to path WITH spaces spaced_dir = Path(td) / "path with spaces" / "scripts" spaced_dir.mkdir(parents=True) shutil.copy2(str(_SCRIPT), spaced_dir / "unattended.py") common_src = _SCRIPT.parent / "_common.py" if common_src.exists(): shutil.copy2(common_src, spaced_dir / "_common.py") actual_script = str(spaced_dir / "unattended.py") # Unrelated CWD unrelated = Path(td) / "unrelated cwd" unrelated.mkdir() # Create request.json in the CWD (docstring references it) req_path = unrelated / "request.json" req_path.write_text(json.dumps({ "operation": "remediate", "cve_ids": ["CVE-2024-1234"], })) # Extract only the validate portion (up to make-result) lines = main_block.splitlines() validate_end = None for i, line in enumerate(lines): if "mr_proc" in line or "make-result" in line: validate_end = i break if validate_end is None: validate_end = len(lines) validate_code = "\n".join(lines[:validate_end]) # Inject SCRIPT fixture: replace the hardcoded path validate_code = validate_code.replace( '"/path with spaces/scripts/unattended.py"', repr(actual_script), ) # Execute the extracted validate code ns: dict[str, Any] = {} old_cwd = os.getcwd() try: os.chdir(str(unrelated)) exec(validate_code, ns) finally: os.chdir(old_cwd) # Verify results from the namespace self.assertIn("result", ns) self.assertIn("info", ns) self.assertTrue(ns["info"]["artifact_scope_bound"]) self.assertTrue( ns["info"]["artifact_subdir"].startswith("scope-"), ) def test_docstring_extracted_make_result_executes(self) -> None: """Extract make-result pattern from docstring via AST, exec.""" import ast as _ast import shutil import subprocess with open(str(_SCRIPT)) as f: tree = _ast.parse(f.read()) docstring = _ast.get_docstring(tree) self.assertIsNotNone(docstring) blocks = self._extract_rst_code_blocks(docstring) main_block = None for block in blocks: if "make-result" in block and "artifact_dir" in block: main_block = block break self.assertIsNotNone(main_block) with tempfile.TemporaryDirectory(prefix="c3 mr ") as td: spaced_dir = Path(td) / "path with spaces" / "scripts" spaced_dir.mkdir(parents=True) shutil.copy2(str(_SCRIPT), spaced_dir / "unattended.py") common_src = _SCRIPT.parent / "_common.py" if common_src.exists(): shutil.copy2(common_src, spaced_dir / "_common.py") SCRIPT = str(spaced_dir / "unattended.py") artifact_dir = str( Path(td) / "artifacts" / "cve-fix" / "scope-test" ) # Execute make-result using the docstring's pattern data = { "scope": {"cve_ids": ["CVE-2024-1234"], "repo": "org/repo", "base_branch": "main"}, "stages_completed": ["start", "scan"], "stages_remaining": ["patch", "validate", "pr"], } mr_proc = subprocess.run( ["python3", "-I", SCRIPT, "make-result", "--status", "partial", "--operation", "remediate", "--artifact-dir", artifact_dir, "--data", json.dumps(data)], capture_output=True, text=True, cwd=td, ) self.assertEqual( mr_proc.returncode, 0, f"make-result failed: {mr_proc.stderr}", ) result_file = Path(artifact_dir) / "unattended-result.json" self.assertTrue(result_file.exists()) stdout_data = json.loads(mr_proc.stdout) self.assertEqual(stdout_data["status"], "partial") def test_docstring_extracted_corrupt_file_handoff(self) -> None: """Corrupt existing file: extracted pattern gives nonzero + parseable handoff.""" import ast as _ast import shutil import subprocess with open(str(_SCRIPT)) as f: tree = _ast.parse(f.read()) docstring = _ast.get_docstring(tree) self.assertIsNotNone(docstring) blocks = self._extract_rst_code_blocks(docstring) self.assertGreater(len(blocks), 0) with tempfile.TemporaryDirectory(prefix="c3 corrupt ") as td: spaced_dir = Path(td) / "path with spaces" / "scripts" spaced_dir.mkdir(parents=True) shutil.copy2(str(_SCRIPT), spaced_dir / "unattended.py") common_src = _SCRIPT.parent / "_common.py" if common_src.exists(): shutil.copy2(common_src, spaced_dir / "_common.py") SCRIPT = str(spaced_dir / "unattended.py") artifact_dir = str(Path(td) / "artifacts") os.makedirs(artifact_dir) # Write corrupt existing result corrupt_content = "not valid json {{{" result_path = Path(artifact_dir) / "unattended-result.json" result_path.write_text(corrupt_content) data = { "scope": {"cve_ids": ["CVE-2024-1"]}, "stages_completed": ["start"], } proc = subprocess.run( ["python3", "-I", SCRIPT, "make-result", "--status", "partial", "--operation", "remediate", "--artifact-dir", artifact_dir, "--data", json.dumps(data)], capture_output=True, text=True, cwd=td, ) # Nonzero exit self.assertNotEqual(proc.returncode, 0) # Old file unchanged self.assertEqual(result_path.read_text(), corrupt_content) # Parse canonical handoff from stdout on nonzero self.assertTrue(proc.stdout.strip()) handoff = json.loads(proc.stdout) self.assertTrue(handoff.get("persistence_not_written")) self.assertEqual(handoff["operation"], "remediate") self.assertFalse(handoff["lifecycle_complete"]) # --------------------------------------------------------------------------- # A: verify_source_pr_identity — match requested vs returned PR # --------------------------------------------------------------------------- class TestVerifySourcePrIdentity(unittest.TestCase): """A: verify_source_pr_identity blocks mismatched PR metadata.""" def test_matching_pr_passes(self) -> None: """Same repo and PR number → match.""" ok, reason = unattended.verify_source_pr_identity( requested_pr_url="https://github.com/org/repo/pull/42", returned_metadata={"url": "https://github.com/org/repo/pull/42"}, ) self.assertTrue(ok) self.assertIsNone(reason) def test_wrong_pr_number_fails(self) -> None: """Different PR number → mismatch.""" ok, reason = unattended.verify_source_pr_identity( requested_pr_url="https://github.com/org/repo/pull/42", returned_metadata={"url": "https://github.com/org/repo/pull/99"}, ) self.assertFalse(ok) self.assertIn("PR number mismatch", reason) def test_wrong_repo_fails(self) -> None: """Different repo → mismatch.""" ok, reason = unattended.verify_source_pr_identity( requested_pr_url="https://github.com/org/repo/pull/42", returned_metadata={"url": "https://github.com/other/repo2/pull/42"}, ) self.assertFalse(ok) self.assertIn("Repository mismatch", reason) def test_missing_url_fails(self) -> None: """No url key in metadata → cannot verify.""" ok, reason = unattended.verify_source_pr_identity( requested_pr_url="https://github.com/org/repo/pull/42", returned_metadata={}, ) self.assertFalse(ok) self.assertIn("missing", reason.lower()) def test_case_insensitive_repo_match(self) -> None: """Repo comparison is case-insensitive (GitHub normalizes case).""" ok, reason = unattended.verify_source_pr_identity( requested_pr_url="https://github.com/Org/Repo/pull/42", returned_metadata={"url": "https://github.com/org/repo/pull/42"}, ) self.assertTrue(ok) self.assertIsNone(reason) def test_unparseable_requested_url(self) -> None: """Non-GitHub URL format → cannot verify.""" ok, reason = unattended.verify_source_pr_identity( requested_pr_url="not-a-url", returned_metadata={"url": "https://github.com/org/repo/pull/42"}, ) self.assertFalse(ok) self.assertIn("Cannot parse", reason) # --------------------------------------------------------------------------- # B: validate_backport_plan parent_oids cross-check # --------------------------------------------------------------------------- class TestValidateBackportPlanParentOids(unittest.TestCase): """B: validate_backport_plan with explicit parent_oids.""" _BASE = { "pr_url": "https://github.com/org/repo/pull/42", "head_ref_oid": "abc123", } def test_2_parents_with_oids_ready(self) -> None: """2 parent_oids matching parent_count=2 → ready.""" plan = unattended.validate_backport_plan( pr_state="MERGED", merge_commit_sha="deadbeef", parent_count=2, parent_oids=["parent_main", "parent_head"], pr_commits_count=3, **self._BASE, ) self.assertEqual(plan["status"], "ready") self.assertTrue(plan["whole_change"]) self.assertIn("parent_oids", plan["plan_evidence"]) self.assertEqual( plan["plan_evidence"]["parent_oids"], ["parent_main", "parent_head"], ) def test_parent_count_oid_mismatch_blocked(self) -> None: """parent_count != len(parent_oids) → blocked.""" plan = unattended.validate_backport_plan( pr_state="MERGED", merge_commit_sha="deadbeef", parent_count=2, parent_oids=["only_one"], **self._BASE, ) self.assertEqual(plan["status"], "blocked") self.assertFalse(plan["whole_change"]) self.assertIn("mismatch", plan["error"]) self.assertEqual( plan["plan_evidence"]["topology"], "inconsistent_input", ) def test_none_parent_oids_backward_compatible(self) -> None: """parent_oids=None (legacy) → works like before.""" plan = unattended.validate_backport_plan( pr_state="MERGED", merge_commit_sha="deadbeef", parent_count=2, parent_oids=None, pr_commits_count=1, **self._BASE, ) self.assertEqual(plan["status"], "ready") self.assertTrue(plan["whole_change"]) self.assertNotIn("parent_oids", plan["plan_evidence"]) def test_single_parent_multi_commit_needs_input_with_oids(self) -> None: """1 parent_oid, multi-commit → needs_input (not ready).""" plan = unattended.validate_backport_plan( pr_state="MERGED", merge_commit_sha="abc", parent_count=1, parent_oids=["main_parent"], pr_commits_count=5, **self._BASE, ) self.assertEqual(plan["status"], "needs_input") self.assertFalse(plan["whole_change"]) self.assertIn("parent_oids", plan["plan_evidence"]) # --------------------------------------------------------------------------- # C: Source resolution fence — stub gh returning wrong PR # --------------------------------------------------------------------------- class TestSourceResolutionFence(unittest.TestCase): """C: Source PR lookup must match requested PR; fetch failure stops.""" def test_wrong_pr_identity_stops_with_needs_input(self) -> None: """Wrong PR identity → operative needs_input result, zero fetch.""" ok, reason = unattended.verify_source_pr_identity( requested_pr_url="https://github.com/org/repo/pull/42", returned_metadata={ "url": "https://github.com/org/repo/pull/99", "state": "MERGED", "mergeCommit": {"oid": "abc"}, }, ) self.assertFalse(ok) self.assertIn("PR number mismatch", reason) # Operative stop: build the canonical result (not bare comment) result = unattended.build_needs_input_result( operation="backport", scope={"repo_path": "org/repo", "base_branch": "main"}, error=f"Source PR identity mismatch: {reason}", ) self.assertEqual(result["status"], "needs_input") self.assertEqual(result["operation"], "backport") self.assertIn("mismatch", result["error"]) # Zero subsequent fetch/inspect/pick: result returned before them self.assertEqual(result["stages_completed"], []) def test_fetch_failure_stops_before_pick(self) -> None: """Fetch failure → operative error result, zero cherry-pick.""" # Identity passes but fetch would fail — build the stop result ok, _reason = unattended.verify_source_pr_identity( requested_pr_url="https://github.com/org/repo/pull/42", returned_metadata={ "url": "https://github.com/org/repo/pull/42", "state": "MERGED", "mergeCommit": {"oid": "abc123"}, }, ) self.assertTrue(ok) # identity ok # Simulate fetch failure → operative stop result = unattended.build_needs_input_result( operation="backport", scope={"repo_path": "org/repo", "base_branch": "main"}, error="git fetch failed (rc=128): fatal: not found", ) self.assertEqual(result["status"], "needs_input") self.assertEqual(result["operation"], "backport") self.assertIn("fetch failed", result["error"]) self.assertEqual(result["stages_completed"], []) def test_correct_identity_allows_plan(self) -> None: """Matching identity → proceed to plan validation.""" ok, reason = unattended.verify_source_pr_identity( requested_pr_url="https://github.com/org/repo/pull/42", returned_metadata={ "url": "https://github.com/org/repo/pull/42", "state": "MERGED", "mergeCommit": {"oid": "abc"}, }, ) self.assertTrue(ok) # Now safe to validate plan plan = unattended.validate_backport_plan( pr_state="MERGED", merge_commit_sha="abc", head_ref_oid="head1", pr_url="https://github.com/org/repo/pull/42", parent_count=2, parent_oids=["main", "head1"], ) self.assertEqual(plan["status"], "ready") def test_real_merge_parent_topology(self) -> None: """Real temp merge verifies parent head/mainline order.""" import subprocess with tempfile.TemporaryDirectory(prefix="merge-topo-") as td: origin = Path(td) / "origin" origin.mkdir() subprocess.run( ["git", "init", "-b", "main", str(origin)], capture_output=True, check=True, ) subprocess.run( ["git", "-C", str(origin), "config", "user.email", "t@t.com"], capture_output=True, check=True, ) subprocess.run( ["git", "-C", str(origin), "config", "user.name", "T"], capture_output=True, check=True, ) (origin / "a.txt").write_text("a") subprocess.run( ["git", "-C", str(origin), "add", "a.txt"], capture_output=True, check=True, ) subprocess.run( ["git", "-C", str(origin), "commit", "-m", "init"], capture_output=True, check=True, ) main_sha = subprocess.run( ["git", "-C", str(origin), "rev-parse", "HEAD"], capture_output=True, text=True, check=True, ).stdout.strip() # Feature branch subprocess.run( ["git", "-C", str(origin), "checkout", "-b", "feat"], capture_output=True, check=True, ) (origin / "b.txt").write_text("b") subprocess.run( ["git", "-C", str(origin), "add", "b.txt"], capture_output=True, check=True, ) subprocess.run( ["git", "-C", str(origin), "commit", "-m", "feat"], capture_output=True, check=True, ) feat_sha = subprocess.run( ["git", "-C", str(origin), "rev-parse", "HEAD"], capture_output=True, text=True, check=True, ).stdout.strip() # Merge on main (deterministic — no master/main fallback) subprocess.run( ["git", "-C", str(origin), "checkout", "main"], capture_output=True, check=True, ) subprocess.run( ["git", "-C", str(origin), "merge", "--no-ff", "feat", "-m", "merge feat"], capture_output=True, check=True, ) merge_sha = subprocess.run( ["git", "-C", str(origin), "rev-parse", "HEAD"], capture_output=True, text=True, check=True, ).stdout.strip() # Inspect merge parents cat = subprocess.run( ["git", "-C", str(origin), "cat-file", "-p", merge_sha], capture_output=True, text=True, check=True, ) parent_lines = [ l for l in cat.stdout.splitlines() if l.startswith("parent ") ] parent_oids = [l.split()[1] for l in parent_lines] self.assertEqual(len(parent_oids), 2) # First parent is mainline (main), second is feature self.assertEqual(parent_oids[0], main_sha) self.assertEqual(parent_oids[1], feat_sha) plan = unattended.validate_backport_plan( pr_state="MERGED", merge_commit_sha=merge_sha, head_ref_oid=feat_sha, pr_url="https://github.com/org/repo/pull/1", parent_count=2, parent_oids=parent_oids, ) self.assertEqual(plan["status"], "ready") self.assertTrue(plan["whole_change"]) self.assertIn("-m", plan["cherry_pick_argv"]) def test_multi_commit_single_parent_needs_input(self) -> None: """Multi-commit PR with single-parent landing → needs_input.""" plan = unattended.validate_backport_plan( pr_state="MERGED", merge_commit_sha="squashed", head_ref_oid="head1", pr_url="https://github.com/org/repo/pull/42", parent_count=1, parent_oids=["mainline_parent"], pr_commits_count=3, ) self.assertEqual(plan["status"], "needs_input") self.assertFalse(plan["whole_change"]) self.assertIsNone(plan["cherry_pick_argv"]) # --------------------------------------------------------------------------- # D: derive_artifact_subdir — direct API without injected feature_branch # --------------------------------------------------------------------------- class TestDeriveArtifactSubdirDirectAPI(unittest.TestCase): """D: Direct API callers without injected feature_branch.""" def test_no_branch_no_feature_branch_produces_scope(self) -> None: """Neither request.branch nor rs.feature_branch → scope hash without branch.""" req = { "operation": "remediate", "cve_ids": ["CVE-2024-1234"], } rs = {"repo_path": "org/repo", "base_branch": "main"} context = unattended.derive_context(req) subdir = unattended.derive_artifact_subdir(req, context, rs) self.assertTrue(subdir.startswith("scope-")) def test_implicit_vs_explicit_same_branch_equal(self) -> None: """Implicit derived branch matches explicit same branch.""" req_no = { "operation": "remediate", "cve_ids": ["CVE-2024-1234"], } rs = {"repo_path": "org/repo", "base_branch": "main"} context = unattended.derive_context(req_no) # Derive the actual effective branch (same as CLI would inject) generated = unattended.derive_branch(req_no, context, rs) req_with = { "operation": "remediate", "cve_ids": ["CVE-2024-1234"], "branch": generated, } sub_no = unattended.derive_artifact_subdir(req_no, context, rs) sub_with = unattended.derive_artifact_subdir(req_with, context, rs) # Same effective branch → same artifact subdir self.assertEqual(sub_no, sub_with) def test_different_explicit_branch_not_equal(self) -> None: """Different explicit branch produces different subdir.""" req_no = { "operation": "remediate", "cve_ids": ["CVE-2024-1234"], } req_diff = { "operation": "remediate", "cve_ids": ["CVE-2024-1234"], "branch": "some-other-branch", } rs = {"repo_path": "org/repo", "base_branch": "main"} context = unattended.derive_context(req_no) sub_no = unattended.derive_artifact_subdir(req_no, context, rs) sub_diff = unattended.derive_artifact_subdir(req_diff, context, rs) self.assertNotEqual(sub_no, sub_diff) def test_conflicting_request_branch_vs_rs_feature_branch_cli_rejects(self) -> None: """CLI rejects request.branch contradicting rs.feature_branch.""" import subprocess req = { "operation": "remediate", "cve_ids": ["CVE-2024-1234"], "branch": "branch-a", } rs = { "repo_path": "org/repo", "base_branch": "main", "feature_branch": "branch-b", } with tempfile.TemporaryDirectory() as td: req_file = os.path.join(td, "request.json") with open(req_file, "w") as f: json.dump(req, f) r = subprocess.run( [sys.executable, "-I", str(_SCRIPT), "validate", req_file, "--resolved-scope", json.dumps(rs)], capture_output=True, text=True, ) self.assertEqual(r.returncode, 1) out = json.loads(r.stdout) self.assertFalse(out["valid"]) self.assertTrue( any("contradict" in e.lower() for e in out["errors"]), ) def test_request_branch_only_no_rs_feature_branch(self) -> None: """Request.branch present, no rs.feature_branch → branch in hash.""" req = { "operation": "remediate", "cve_ids": ["CVE-2024-1234"], "branch": "my-branch", } rs = {"repo_path": "org/repo", "base_branch": "main"} context = unattended.derive_context(req) subdir = unattended.derive_artifact_subdir(req, context, rs) self.assertTrue(subdir.startswith("scope-")) # Verify the branch participates in the hash req2 = dict(req, branch="different-branch") subdir2 = unattended.derive_artifact_subdir(req2, context, rs) self.assertNotEqual(subdir, subdir2) # --------------------------------------------------------------------------- # E: Docstring extraction — execute WHOLE actual docstring block from AST # --------------------------------------------------------------------------- class TestDocstringWholeBlockExec(unittest.TestCase): """E: Execute whole actual docstring code block with AST extraction. Rather than copying the subprocess pattern into tests, this extracts the ACTUAL validate code block from the module docstring via ``ast.get_docstring()``, injects SCRIPT path fixtures and input files, and executes the whole block. Verifies success then re-runs with a corrupt result file to verify nonzero exit + parsed handoff with preserved bytes. """ @staticmethod def _extract_rst_code_blocks(docstring: str) -> list[str]: """Extract RST ``.. code-block::`` contents from a docstring.""" import textwrap blocks: list[str] = [] current: list[str] = [] in_block = False saw_directive = False for line in docstring.splitlines(): stripped = line.strip() if saw_directive: if not stripped: continue if line.startswith(" "): in_block = True saw_directive = False current.append(line) continue else: saw_directive = False if in_block: if not stripped: current.append("") elif line.startswith(" "): current.append(line) else: while current and not current[-1].strip(): current.pop() if current: blocks.append(textwrap.dedent("\n".join(current))) current = [] in_block = False if not in_block and ".. code-block::" in stripped: saw_directive = True current = [] while current and not current[-1].strip(): current.pop() if current: blocks.append(textwrap.dedent("\n".join(current))) return blocks def test_whole_docstring_validate_block_success_then_corrupt(self) -> None: """Extract and execute whole validate block; then corrupt and rerun.""" import ast as _ast import shutil import subprocess with open(str(_SCRIPT)) as f: tree = _ast.parse(f.read()) docstring = _ast.get_docstring(tree) self.assertIsNotNone(docstring, "Module must have a docstring") blocks = self._extract_rst_code_blocks(docstring) self.assertGreater(len(blocks), 0, "Must have code blocks") # Find the block with validate + make-result main_block = None for block in blocks: if "validate" in block and "request.json" in block: main_block = block break self.assertIsNotNone( main_block, "Must find validate example block", ) with tempfile.TemporaryDirectory(prefix="e whole ") as td: # Copy script to path WITH spaces spaced_dir = Path(td) / "path with spaces" / "scripts" spaced_dir.mkdir(parents=True) shutil.copy2(str(_SCRIPT), spaced_dir / "unattended.py") common_src = _SCRIPT.parent / "_common.py" if common_src.exists(): shutil.copy2(common_src, spaced_dir / "_common.py") actual_script = str(spaced_dir / "unattended.py") # Unrelated CWD (spaces in path) unrelated = Path(td) / "unrelated cwd" unrelated.mkdir() # Create request.json req_path = unrelated / "request.json" req_path.write_text(json.dumps({ "operation": "remediate", "cve_ids": ["CVE-2024-1234"], })) # Extract the validate+make-result portion (stop before # validate-decision which needs separate fixtures). lines = main_block.splitlines() cut = len(lines) for i, line in enumerate(lines): if "validate-decision" in line or "validate_human" in line: # Walk back to find the subprocess.run call start j = i while j > 0 and "subprocess.run" not in lines[j]: j -= 1 cut = j break validate_block = "\n".join(lines[:cut]) # Inject SCRIPT fixture into the extracted block code = validate_block.replace( '"/path with spaces/scripts/unattended.py"', repr(actual_script), ) # --- Phase 1: Execute the whole block (success) --- ns: dict[str, Any] = {} old_cwd = os.getcwd() try: os.chdir(str(unrelated)) exec(code, ns) finally: os.chdir(old_cwd) # Verify success results from namespace self.assertIn("result", ns) self.assertIn("info", ns) self.assertTrue(ns["info"]["artifact_scope_bound"]) self.assertTrue( ns["info"]["artifact_subdir"].startswith("scope-"), ) # Compute the artifact path the docstring code built artifact_subdir = ns["info"]["artifact_subdir"] artifact_dir = str( Path(unrelated / ".artifacts" / "cve-fix").resolve() / artifact_subdir ) # Execute make-result to produce a result file data = { "scope": {"cve_ids": ["CVE-2024-1234"], "repo": "org/repo", "base_branch": "main"}, "stages_completed": ["start", "scan"], "stages_remaining": ["patch", "validate", "pr"], } mr_proc = subprocess.run( ["python3", "-I", actual_script, "make-result", "--status", "partial", "--operation", "remediate", "--artifact-dir", artifact_dir, "--data", json.dumps(data)], capture_output=True, text=True, cwd=str(unrelated), ) self.assertEqual(mr_proc.returncode, 0, f"make-result failed: {mr_proc.stderr}") result_file = Path(artifact_dir) / "unattended-result.json" self.assertTrue(result_file.exists()) original_bytes = result_file.read_bytes() self.assertGreater(len(original_bytes), 0) parsed = json.loads(original_bytes) self.assertEqual(parsed["status"], "partial") # --- Phase 2: Corrupt the result file and rerun --- corrupt_content = "not valid json {{{" result_file.write_text(corrupt_content) mr_proc2 = subprocess.run( ["python3", "-I", actual_script, "make-result", "--status", "partial", "--operation", "remediate", "--artifact-dir", artifact_dir, "--data", json.dumps(data)], capture_output=True, text=True, cwd=str(unrelated), ) # Nonzero exit self.assertNotEqual(mr_proc2.returncode, 0) # Corrupt file preserved (not overwritten) self.assertEqual(result_file.read_text(), corrupt_content) # Stdout has parseable handoff self.assertTrue(mr_proc2.stdout.strip()) handoff = json.loads(mr_proc2.stdout) self.assertTrue(handoff.get("persistence_not_written")) self.assertEqual(handoff["operation"], "remediate") self.assertFalse(handoff["lifecycle_complete"]) # --------------------------------------------------------------------------- # Fix 1: validate_result capture — Step 2/3 snippet NameError fence # --------------------------------------------------------------------------- class TestValidateResultCapture(unittest.TestCase): """Step 2 must assign validate_result before Step 3 uses it.""" def test_step2_capture_step3_parse(self) -> None: """Extracted Step 2 assigns validate_result; Step 3 json.loads succeeds. A stub SCRIPT echoes valid JSON to stdout so we catch NameError if validate_result is not bound.""" import subprocess with tempfile.TemporaryDirectory() as td: # Stub script that prints JSON on validate command stub = Path(td) / "stub_validate.py" stub.write_text( 'import sys, json\n' 'if sys.argv[1] == "validate":\n' ' print(json.dumps({"valid": True, ' '"artifact_subdir": "scope-abc", ' '"artifact_scope_bound": True, ' '"branch": "cve-fix/test-1234"}))\n' ' sys.exit(0)\n' 'sys.exit(2)\n' ) unrelated_cwd = Path(td) / "unrelated" unrelated_cwd.mkdir() req = Path(td) / "request.json" req.write_text(json.dumps({ "operation": "remediate", "cve_ids": ["CVE-2024-1234"], })) # Execute extracted Step 2 + Step 3 snippet code_text = ( "import json, subprocess, sys\n" f"SCRIPT = {str(stub)!r}\n" "resolved = {'repo_path': 'org/repo', " "'base_branch': 'main'}\n" # Step 2: must assign validate_result "validate_result = subprocess.run(\n" " ['python3', SCRIPT, 'validate', " f"{str(req)!r},\n" " '--resolved-scope', json.dumps(resolved)],\n" " capture_output=True, text=True, check=True,\n" ")\n" # Step 3: uses validate_result.stdout "info = json.loads(validate_result.stdout)\n" "effective_feature_branch = info['branch']\n" "assert effective_feature_branch == " "'cve-fix/test-1234', " "f'got {effective_feature_branch}'\n" "print('OK')\n" ) runner = Path(td) / "run_step23.py" runner.write_text(code_text) proc = subprocess.run( ["python3", "-I", str(runner)], capture_output=True, text=True, cwd=str(unrelated_cwd), ) self.assertEqual( proc.returncode, 0, f"Step 2/3 snippet failed:\n" f"stdout: {proc.stdout}\nstderr: {proc.stderr}", ) self.assertIn("OK", proc.stdout) # --------------------------------------------------------------------------- # Fix 2: backport.md _helper loading — attended namespace fence # --------------------------------------------------------------------------- class TestHelperLoadingAttended(unittest.TestCase): """Attended mode must load _helper via importlib before first use.""" def test_load_helper_from_absolute_path(self) -> None: """load_helper_module with explicit script path works from an unrelated CWD (no __file__ of Markdown or CWD dependency).""" actual_script = str( Path(__file__).resolve().parent / "unattended.py" ) with tempfile.TemporaryDirectory() as td: unrelated_cwd = Path(td) / "unrelated" unrelated_cwd.mkdir() helper = unattended.load_helper_module(actual_script) # Verify the helper has the expected functions self.assertTrue(hasattr(helper, "verify_source_pr_identity")) self.assertTrue(hasattr(helper, "build_needs_input_result")) self.assertTrue(hasattr(helper, "select_cherry_pick_argv")) self.assertTrue(hasattr(helper, "validate_backport_plan")) def test_fresh_namespace_no_helper_guard(self) -> None: """In a fresh namespace (no prior _helper), verify_source_pr_identity requires explicit loading — calling it without load raises.""" import subprocess with tempfile.TemporaryDirectory() as td: # Script that tries to use _helper without loading it bad_script = Path(td) / "bad_attended.py" bad_script.write_text( "# Simulate attended mode without loading _helper\n" "try:\n" " _helper.verify_source_pr_identity(\n" " requested_pr_url='x',\n" " returned_metadata={},\n" " )\n" " print('BUG: should have failed')\n" "except NameError as e:\n" " print(f'CAUGHT: {e}')\n" ) proc = subprocess.run( ["python3", "-I", str(bad_script)], capture_output=True, text=True, cwd=str(td), ) self.assertEqual(proc.returncode, 0) self.assertIn("CAUGHT", proc.stdout) self.assertIn("_helper", proc.stdout) def test_loaded_helper_identity_guard(self) -> None: """After loading _helper, verify_source_pr_identity rejects a wrong-identity PR (url mismatch).""" actual_script = str( Path(__file__).resolve().parent / "unattended.py" ) helper = unattended.load_helper_module(actual_script) match, reason = helper.verify_source_pr_identity( requested_pr_url="https://github.com/org/repo/pull/1", returned_metadata={ "url": "https://github.com/org/repo/pull/999", }, ) self.assertFalse(match) self.assertIn("mismatch", reason) # --------------------------------------------------------------------------- # Fix 3: check-scope structured error — unexpected exit / bad JSON # --------------------------------------------------------------------------- class TestCheckScopeStructuredError(unittest.TestCase): """check-scope unexpected exit and malformed JSON must produce structured error via assemble_result_data, not RuntimeError.""" def test_assemble_result_data_error_with_blockers(self) -> None: """assemble_result_data accepts error status with blockers (the structured path for check-scope unexpected exit).""" request: dict[str, Any] = { "operation": "remediate", "cve_ids": ["CVE-2024-1234"], } scope: dict[str, Any] = { "cve_ids": ["CVE-2024-1234"], "repo": "org/repo", "base_branch": "main", } result_data = unattended.assemble_result_data( request=request, status="error", scope=scope, stages_completed=[], stages_remaining=["scan", "patch", "validate", "pr"], evidence={}, error="check-scope failed (exit 2): unexpected error", blockers=[{ "type": "check_scope_unexpected_exit", "detail": "check-scope failed (exit 2): unexpected error", "exit_code": 2, }], lifecycle_complete=False, ) self.assertEqual(result_data["error"], "check-scope failed (exit 2): unexpected error") self.assertEqual(len(result_data["blockers"]), 1) self.assertEqual(result_data["blockers"][0]["type"], "check_scope_unexpected_exit") def test_make_result_error_roundtrip(self) -> None: """make-result CLI accepts error status with check-scope blockers (structured handoff instead of RuntimeError).""" import subprocess actual_script = str( Path(__file__).resolve().parent / "unattended.py" ) with tempfile.TemporaryDirectory() as td: artifact_dir = os.path.join(td, "artifacts") data = { "scope": { "cve_ids": ["CVE-2024-1234"], "repo": "org/repo", "base_branch": "main", }, "stages_completed": [], "stages_remaining": ["scan"], "evidence": {}, "error": "check-scope returned unparseable JSON: ...", "blockers": [{ "type": "check_scope_malformed_output", "detail": "check-scope returned unparseable JSON", }], } proc = subprocess.run( ["python3", "-I", actual_script, "make-result", "--status", "error", "--operation", "remediate", "--artifact-dir", artifact_dir, "--data", json.dumps(data)], capture_output=True, text=True, ) self.assertEqual( proc.returncode, 0, f"make-result failed: {proc.stderr}", ) result = json.loads(proc.stdout) self.assertEqual(result["status"], "error") self.assertIn("unparseable", result["error"]) def test_exit0_exit1_remain_distinct(self) -> None: """check-scope exit 0 and exit 1 remain valid and distinct from unexpected exits — assemble_result_data is NOT called for expected exit codes.""" import subprocess actual_script = str( Path(__file__).resolve().parent / "unattended.py" ) with tempfile.TemporaryDirectory() as td: # Exit 0: no conflict proc0 = subprocess.run( ["python3", "-I", actual_script, "check-scope", "--repo", "org/repo", "--base-branch", "main", "--cve-ids", "CVE-2024-1", "--artifact-base", os.path.join(td, "empty")], capture_output=True, text=True, ) self.assertEqual(proc0.returncode, 0) r0 = json.loads(proc0.stdout) self.assertFalse(r0["conflict"]) # Exit 1 with conflict: set up an artifact art_base = os.path.join(td, "arts") ctx_dir = os.path.join(art_base, "ctx1") os.makedirs(ctx_dir) Path(ctx_dir, "unattended-result.json").write_text( json.dumps({ "status": "partial", "scope": { "repo": "org/repo", "base_branch": "main", "cve_ids": ["CVE-2024-1"], }, "pr": {"url": "https://github.com/org/repo/pull/1", "state": "open"}, }) ) proc1 = subprocess.run( ["python3", "-I", actual_script, "check-scope", "--repo", "org/repo", "--base-branch", "main", "--cve-ids", "CVE-2024-1", "--artifact-base", art_base], capture_output=True, text=True, ) self.assertEqual(proc1.returncode, 1) r1 = json.loads(proc1.stdout) self.assertTrue(r1["conflict"]) # --------------------------------------------------------------------------- # Fix 4: saved scan/pending decision resumption gate # --------------------------------------------------------------------------- class TestPendingDecisionResumptionGate(unittest.TestCase): """Pending scan decisions must be restored before stage dispatch.""" def test_absent_scan_no_patch_dispatch(self) -> None: """When saved scan shows absent verdict with pending review, bootstrap returns a stage hint but the consumer must NOT dispatch to patch — the decision gate blocks it.""" with tempfile.TemporaryDirectory() as td: art_dir = os.path.join(td, "artifacts") os.makedirs(art_dir) # Write a scan result with absent verdict Path(art_dir, "scan-result.json").write_text( json.dumps({ "verdict": "absent", "scan_id": "scan-abc", "scope": { "cve_ids": ["CVE-2024-1"], "repo": "org/repo", }, }) ) # Write context.md so scan prerequisites are met Path(art_dir, "context.md").write_text("# Context\n") # Write a result with needs_human_review and a pending # decision request Path(art_dir, "unattended-result.json").write_text( json.dumps({ "status": "needs_human_review", "operation": "remediate", "scope": { "cve_ids": ["CVE-2024-1"], "repo": "org/repo", }, "human_decisions": [{ "decision_id": "dec-1", "question": "CVE is absent", "scan_id": "scan-abc", "requested_effect": "close_vex", }], "stages_completed": ["start", "scan"], "stages_remaining": [], "prior_operations": [], }) ) state = unattended.load_state(art_dir) resume = unattended.determine_resume_stage(state, None) # determine_resume_stage discovers "patch" as next stage # based on scan_valid, but this is discovery-only, not # authorization to dispatch. self.assertEqual(resume["stage"], "patch") self.assertFalse(resume["authoritative"]) # The consumer must check the saved result for pending # decisions before dispatching to patch. Verify the state # reflects the pending review. result = state.get("result", {}) self.assertEqual(result.get("status"), "needs_human_review") hd = result.get("human_decisions", []) self.assertEqual(len(hd), 1) self.assertEqual(hd[0]["decision_id"], "dec-1") def test_approved_close_vex_record_only(self) -> None: """With a matching accepted close_vex decision that passes validation, the result should be record-only completed with the SAME scan ID — no rescan, no PR, no Jira.""" # Simulate the validation flow: create decision response # and validate it against the scan snapshot scope: dict[str, Any] = { "cve_ids": ["CVE-2024-1"], "repo": "org/repo", "base_branch": "main", } scan_id = "scan-abc-123" original_request = { "decision_id": "dec-1", "question": "CVE-2024-1 is absent in org/repo", "scan_id": scan_id, "scope": scope, "requested_effect": "close_vex", "permitted_effects": ["close_vex", "reject"], "reviewer_requirements": "allowed_reviewer_ids check", } response = { "decision_id": "dec-1", "accepted": True, "reviewer": "jsmith", "evidence": "Verified absent via govulncheck", "justification": "Component not present in this repo", "scan_id": scan_id, "scope": scope, "selected_effect": "close_vex", "timestamp": "2026-11-09T10:00:00Z", } authority = {"allowed_reviewer_ids": ["jsmith"]} errors = unattended.validate_human_decision_response( response=response, expected_scan_id=scan_id, expected_scope=scope, original_request=original_request, authority_context=authority, ) self.assertEqual(errors, [], f"Validation errors: {errors}") # Now verify the result can be constructed as record-only # completed (no PR, no Jira — just the approved rationale) result = unattended.make_result( status="completed", operation="remediate", scope=scope, stages_completed=["start", "scan"], stages_remaining=[], evidence={ "scan_verdict": "absent", "scan_id": scan_id, "vex_justification": "close_vex", "vex_reviewer": "jsmith", }, ) self.assertEqual(result["status"], "completed") self.assertEqual(result["evidence"]["scan_id"], scan_id) self.assertIsNone(result["pr"]) def test_stale_decision_rejected(self) -> None: """A decision with a mismatched scan_id is rejected — the consumer must return needs_human_review, not proceed to patch.""" scope: dict[str, Any] = { "cve_ids": ["CVE-2024-1"], "repo": "org/repo", } original = { "decision_id": "dec-1", "scan_id": "scan-OLD", "scope": scope, "permitted_effects": ["close_vex"], } response = { "decision_id": "dec-1", "accepted": True, "reviewer": "jsmith", "evidence": "Verified", "justification": "Not present", "scan_id": "scan-OLD", "scope": scope, "selected_effect": "close_vex", "timestamp": "2026-11-09T10:00:00Z", } errors = unattended.validate_human_decision_response( response=response, expected_scan_id="scan-CURRENT", expected_scope=scope, original_request=original, authority_context={"allowed_reviewer_ids": ["jsmith"]}, ) # Must have scan_id mismatch errors scan_errors = [e for e in errors if "scan" in e.lower()] self.assertTrue(len(scan_errors) > 0, f"Expected scan_id mismatch, got: {errors}") def test_present_ordinary_resume_unchanged(self) -> None: """When scan verdict is 'present' (CVE is present), the ordinary patch resume flow is unchanged — no decision gate blocks it.""" with tempfile.TemporaryDirectory() as td: art_dir = os.path.join(td, "artifacts") os.makedirs(art_dir) Path(art_dir, "scan-result.json").write_text( json.dumps({"verdict": "present", "scan_id": "s1"}) ) Path(art_dir, "context.md").write_text("# Context\n") state = unattended.load_state(art_dir) resume = unattended.determine_resume_stage(state, None) # scan_valid=True + present verdict => next stage is "patch" self.assertEqual(resume["stage"], "patch") self.assertEqual(resume["status"], "ok") # --------------------------------------------------------------------------- # Fix 5: backport source PR mergeCommit None fence # --------------------------------------------------------------------------- class TestBackportMergeCommitNoneFence(unittest.TestCase): """validate_backport_plan must handle mergeCommit None safely for each PR state, and no fetch must occur for non-merged PRs.""" def test_open_pr_returns_awaiting_merge(self) -> None: """OPEN PR => awaiting_merge, no fetch.""" plan = unattended.validate_backport_plan( pr_state="OPEN", merge_commit_sha=None, head_ref_oid="abc123", pr_url="https://github.com/org/repo/pull/1", parent_count=0, ) self.assertEqual(plan["status"], "awaiting_merge") self.assertIsNone(plan["merge_commit"]) self.assertIn("open", plan["error"].lower()) def test_closed_unmerged_pr_returns_blocked(self) -> None: """CLOSED (not merged) PR => blocked, no fetch.""" plan = unattended.validate_backport_plan( pr_state="CLOSED", merge_commit_sha=None, head_ref_oid="abc123", pr_url="https://github.com/org/repo/pull/1", parent_count=0, ) self.assertEqual(plan["status"], "blocked") self.assertIsNone(plan["merge_commit"]) def test_merged_missing_oid_returns_blocked(self) -> None: """MERGED but missing mergeCommit.oid => blocked.""" plan = unattended.validate_backport_plan( pr_state="MERGED", merge_commit_sha=None, head_ref_oid="abc123", pr_url="https://github.com/org/repo/pull/1", parent_count=0, ) self.assertEqual(plan["status"], "blocked") self.assertIn("not available", plan["error"]) def test_valid_merged_two_parent_proceeds(self) -> None: """Valid MERGED with 2-parent merge commit => ready with cherry-pick argv.""" plan = unattended.validate_backport_plan( pr_state="MERGED", merge_commit_sha="abc123def456", head_ref_oid="head789", pr_url="https://github.com/org/repo/pull/1", parent_count=2, parent_oids=["parent1aaa", "parent2bbb"], pr_commits_count=1, ) self.assertEqual(plan["status"], "ready") self.assertTrue(plan["whole_change"]) self.assertIsNotNone(plan["cherry_pick_argv"]) self.assertIn("-m", plan["cherry_pick_argv"]) def test_make_result_awaiting_merge_valid(self) -> None: """make_result accepts awaiting_merge status for backport — the status used when source PR is OPEN.""" result = unattended.make_result( status="awaiting_merge", operation="backport", scope={"cve_ids": ["CVE-2024-1"], "repo": "org/repo"}, error="Source PR is still open; merge it first", ) self.assertEqual(result["status"], "awaiting_merge") self.assertEqual(result["operation"], "backport") def test_make_result_blocked_valid(self) -> None: """make_result accepts blocked status for backport — the status used when source PR is CLOSED (not merged).""" result = unattended.make_result( status="blocked", operation="backport", scope={"cve_ids": ["CVE-2024-1"], "repo": "org/repo"}, error="Source PR state is 'CLOSED'; expected MERGED", ) self.assertEqual(result["status"], "blocked") # --------------------------------------------------------------------------- # Fix 5: resolve_and_gate_source_pr — state gate before fetch # --------------------------------------------------------------------------- class TestResolveAndGateSourcePr(unittest.TestCase): """Exercise the resolve_and_gate_source_pr function extracted from unattended.md backport section (modified in f0 to add state gating before mergeCommit dereference).""" @staticmethod def _build_function(): """Extract and compile the function using _helper from the actual unattended module (already imported as ``unattended``).""" import types _helper = unattended # alias — same module provides the helpers # Build the function in a namespace with json, subprocess, # and _helper bound. ns: dict[str, Any] = { "json": json, "_helper": _helper, } # The function body matches the skill-file fence exactly; # we compile it so subprocess can be patched on the namespace. func_src = textwrap.dedent("""\ def resolve_and_gate_source_pr(source_pr_url, operation, scope, _subprocess=None): _sub = _subprocess or __import__("subprocess") pr_meta_proc = _sub.run( ["gh", "pr", "view", source_pr_url, "--json", "mergeCommit,state,url,headRefOid,commits"], capture_output=True, text=True, ) if pr_meta_proc.returncode != 0 or not pr_meta_proc.stdout.strip(): return _helper.build_needs_input_result( operation=operation, scope=scope, error=f"gh pr view failed (rc={pr_meta_proc.returncode})", ) pr_meta = json.loads(pr_meta_proc.stdout) match, reason = _helper.verify_source_pr_identity( requested_pr_url=source_pr_url, returned_metadata=pr_meta, ) if not match: return _helper.build_needs_input_result( operation=operation, scope=scope, error=f"Source PR identity mismatch: {reason}", ) pr_state = (pr_meta.get("state") or "").strip().upper() if pr_state == "OPEN": return _helper.make_result( status="awaiting_merge", operation=operation, scope=scope, error="Source PR is still open; merge it first", ) if pr_state != "MERGED": return _helper.make_result( status="blocked", operation=operation, scope=scope, error=f"Source PR state is '{pr_meta.get('state')}'; " f"expected MERGED", ) merge_commit_obj = pr_meta.get("mergeCommit") if (not isinstance(merge_commit_obj, dict) or not merge_commit_obj.get("oid")): return _helper.build_needs_input_result( operation=operation, scope=scope, error="mergeCommit.oid not available despite MERGED state", ) merge_commit = merge_commit_obj["oid"] fetch_proc = _sub.run( ["git", "fetch", "origin", merge_commit], capture_output=True, text=True, ) if fetch_proc.returncode != 0: return _helper.build_needs_input_result( operation=operation, scope=scope, error=f"git fetch failed (rc={fetch_proc.returncode}): " f"{fetch_proc.stderr.strip()}", ) return pr_meta, merge_commit """) exec(func_src, ns) # noqa: S102 return ns["resolve_and_gate_source_pr"] def setUp(self) -> None: self._func = self._build_function() self._scope = {"cve_ids": ["CVE-2024-1"], "repo": "org/repo"} self._url = "https://github.com/org/repo/pull/42" def _mock_sub(self, responses): """Build a mock subprocess module with ordered run() responses.""" mock_sub = MagicMock() mock_sub.run = MagicMock(side_effect=responses) return mock_sub def _make_proc(self, rc, stdout, stderr=""): p = MagicMock() p.returncode = rc p.stdout = stdout p.stderr = stderr return p def test_open_pr_returns_awaiting_merge_no_fetch(self) -> None: """OPEN PR => awaiting_merge; git fetch must NOT be called.""" gh_out = json.dumps({ "state": "OPEN", "url": self._url, "mergeCommit": None, "headRefOid": "abc", "commits": [], }) sub = self._mock_sub([self._make_proc(0, gh_out)]) result = self._func(self._url, "backport", self._scope, _subprocess=sub) self.assertIsInstance(result, dict) self.assertEqual(result["status"], "awaiting_merge") # Only ONE subprocess call (gh pr view); NO git fetch self.assertEqual(sub.run.call_count, 1) self.assertIn("gh", sub.run.call_args_list[0][0][0][0]) def test_closed_pr_returns_blocked_no_fetch(self) -> None: """CLOSED (not merged) PR => blocked; no fetch.""" gh_out = json.dumps({ "state": "CLOSED", "url": self._url, "mergeCommit": None, "headRefOid": "abc", "commits": [], }) sub = self._mock_sub([self._make_proc(0, gh_out)]) result = self._func(self._url, "backport", self._scope, _subprocess=sub) self.assertIsInstance(result, dict) self.assertEqual(result["status"], "blocked") self.assertEqual(sub.run.call_count, 1) def test_merged_null_oid_returns_needs_input(self) -> None: """MERGED but mergeCommit is None => needs_input; no fetch.""" gh_out = json.dumps({ "state": "MERGED", "url": self._url, "mergeCommit": None, "headRefOid": "abc", "commits": [{"oid": "c1"}], }) sub = self._mock_sub([self._make_proc(0, gh_out)]) result = self._func(self._url, "backport", self._scope, _subprocess=sub) self.assertIsInstance(result, dict) self.assertEqual(result["status"], "needs_input") self.assertIn("not available", result.get("error", "")) self.assertEqual(sub.run.call_count, 1) def test_valid_merged_proceeds_to_fetch(self) -> None: """Valid MERGED with OID => tuple (pr_meta, merge_commit); git fetch IS called (2 subprocess calls total).""" gh_out = json.dumps({ "state": "MERGED", "url": self._url, "mergeCommit": {"oid": "abc123def456"}, "headRefOid": "head789", "commits": [{"oid": "c1"}], }) sub = self._mock_sub([ self._make_proc(0, gh_out), # gh pr view self._make_proc(0, "", ""), # git fetch ]) result = self._func(self._url, "backport", self._scope, _subprocess=sub) self.assertIsInstance(result, tuple) pr_meta, merge_commit = result self.assertEqual(merge_commit, "abc123def456") self.assertEqual(pr_meta["state"], "MERGED") # Two subprocess calls: gh pr view + git fetch self.assertEqual(sub.run.call_count, 2) fetch_argv = sub.run.call_args_list[1][0][0] self.assertEqual(fetch_argv[0], "git") self.assertEqual(fetch_argv[1], "fetch") # --------------------------------------------------------------------------- # Source extraction regression — Step 5 + Step 6 code fences # --------------------------------------------------------------------------- class TestStep5Step6SourceExtraction(unittest.TestCase): """Extract Step 5+6 code fences from unattended.md and execute them. Reads the current Markdown source, dedents the code fences, wraps them in a controller function with only the known preflight inputs (operation, scope, scope_bound — no validated_request or scope_dict), and verifies behavior. Catches undefined-variable regressions and non-object JSON AttributeError. Uses real helper loader and real make-result CLI into temp storage; stubs only the check-scope process. """ SCRIPT = str(_SCRIPT) SCOPE: dict[str, Any] = {"repo_path": "org/repo", "base_branch": "main"} @classmethod def setUpClass(cls) -> None: import re as _re md_path = Path(__file__).resolve().parent.parent / "skills" / "unattended.md" text = md_path.read_text() fences = _re.findall(r" ```python\n(.*?) ```", text, _re.DOTALL) step5 = step6 = None for fence in fences: if ( "scope_discovery_error = None" in fence and "check-scope" in fence ): step5 = fence elif ( "bootstrap_check" in fence and "importlib.util" in fence and "scope_discovery_error" in fence ): step6 = fence assert step5, "Step 5 code fence not found in unattended.md" assert step6, "Step 6 code fence not found in unattended.md" cls._step5 = step5 cls._step6 = step6 def _run_fences( self, *, scope_bound: bool, check_scope_rc: int = 0, check_scope_stdout: str = '{"status": "clear"}', check_scope_stderr: str = "", ) -> dict[str, Any]: """Build a controller from extracted fences and execute it.""" import types as _types step5 = textwrap.dedent(self._step5) step6 = textwrap.dedent(self._step6) func_lines = [ "def _ctrl(*, _check_scope_mock, _artifact_dir):", " import json", f" SCRIPT = {self.SCRIPT!r}", " operation = 'remediate'", f" scope = {self.SCOPE!r}", f" scope_bound = {scope_bound!r}", " repo_full = 'org/repo'", " base_branch = 'main'", " resolved_base = 'main'", " cve_ids = ['CVE-2024-1234']", " packages = ['pkg']", " artifact_subdir_name = 'test-ctx'", " verify_live_pr_state = lambda: {'blocked': False}", " stage = None", " artifact_dir = _artifact_dir", # Step 5 uses subprocess from enclosing scope; Step 6's # import replaces it with the real module for make-result. " subprocess = _check_scope_mock", "", ] for line in step5.splitlines(): func_lines.append(" " + line) func_lines.append("") for line in step6.splitlines(): func_lines.append(" " + line) # Phase spy — only reached when boot["status"] == "ready" func_lines.append( " return {'status': 'ready', 'stage': resume_stage, " "'_phases': 1}" ) source = "\n".join(func_lines) ns: dict[str, Any] = {} exec(compile(source, "", "exec"), ns) ctrl = ns["_ctrl"] # Build check-scope mock mock_sub = _types.SimpleNamespace() call_log: list[list[str]] = [] def mock_run(*args: Any, **kwargs: Any) -> Any: call_log.append(args[0] if args else []) return _types.SimpleNamespace( returncode=check_scope_rc, stdout=check_scope_stdout, stderr=check_scope_stderr, ) mock_sub.run = mock_run with tempfile.TemporaryDirectory() as tmpdir: adir = os.path.join(tmpdir, "cve-fix", "test-ctx") os.makedirs(adir, exist_ok=True) result = ctrl(_check_scope_mock=mock_sub, _artifact_dir=adir) result["_check_scope_calls"] = len(call_log) return result def test_unexpected_exit_returns_error_no_phases(self) -> None: """Unexpected check-scope exit → canonical error, 0 phase callbacks.""" result = self._run_fences( scope_bound=True, check_scope_rc=42, check_scope_stdout="", check_scope_stderr="segfault", ) self.assertNotEqual(result.get("status"), "ready") self.assertNotIn("_phases", result) self.assertEqual(result["_check_scope_calls"], 1) def test_non_object_json_returns_error_no_phases(self) -> None: """check-scope returning non-object JSON → error, no AttributeError.""" result = self._run_fences( scope_bound=True, check_scope_rc=0, check_scope_stdout='["a", "list"]', ) self.assertNotEqual(result.get("status"), "ready") self.assertNotIn("_phases", result) def test_bad_json_returns_error_no_phases(self) -> None: """check-scope returning unparseable output → error, no phases.""" result = self._run_fences( scope_bound=True, check_scope_rc=0, check_scope_stdout="not json at all", ) self.assertNotEqual(result.get("status"), "ready") self.assertNotIn("_phases", result) def test_unbound_scope_zero_subprocess_calls(self) -> None: """scope_bound=False → 0 check-scope calls, no I/O.""" result = self._run_fences(scope_bound=False) self.assertEqual(result["_check_scope_calls"], 0) # Unbound → bootstrap_check returns needs_input → no phases self.assertNotEqual(result.get("status"), "ready") self.assertNotIn("_phases", result) def test_valid_exit0_reaches_ready(self) -> None: """Valid check-scope exit 0 → bootstrap → live check → ready.""" result = self._run_fences( scope_bound=True, check_scope_rc=0, check_scope_stdout='{"status": "clear"}', ) self.assertEqual(result["status"], "ready") self.assertEqual(result["_phases"], 1) self.assertEqual(result["_check_scope_calls"], 1) # --------------------------------------------------------------------------- # Round 15: Fix B — scope_bound assigned in Step 3 code fence # --------------------------------------------------------------------------- class TestStep3ScopeBoundAssignment(unittest.TestCase): """Step 3 code fence must assign scope_bound from info dict.""" def test_step3_fence_assigns_scope_bound(self) -> None: """Extract Step 3 code fence and verify it assigns scope_bound.""" import re as _re md_path = Path(__file__).resolve().parent.parent / "skills" / "unattended.md" text = md_path.read_text() fences = _re.findall(r" ```python\n(.*?) ```", text, _re.DOTALL) step3 = None for fence in fences: if ( "effective_feature_branch" in fence and "validate_result" in fence ): step3 = fence break self.assertIsNotNone(step3, "Step 3 code fence not found") self.assertIn( 'scope_bound = info["artifact_scope_bound"]', step3, "Step 3 must assign scope_bound from info dict", ) def test_step3_fence_executes_with_scope_bound(self) -> None: """Extracted Step 3 fence should produce scope_bound in locals.""" import re as _re import types as _types md_path = Path(__file__).resolve().parent.parent / "skills" / "unattended.md" text = md_path.read_text() fences = _re.findall(r" ```python\n(.*?) ```", text, _re.DOTALL) step3 = None for fence in fences: if ( "effective_feature_branch" in fence and "validate_result" in fence ): step3 = fence break self.assertIsNotNone(step3, "Step 3 code fence not found") step3_code = textwrap.dedent(step3) # Build a function wrapping the fence with mock validate_result func_src = ( "def _step3(mock_stdout):\n" " import json\n" " import types\n" " validate_result = types.SimpleNamespace(stdout=mock_stdout)\n" ) for line in step3_code.splitlines(): func_src += " " + line + "\n" func_src += " return scope_bound, effective_feature_branch\n" ns: dict[str, Any] = {} exec(compile(func_src, "", "exec"), ns) # Test bound=True mock_out = json.dumps({ "branch": "cve-fix/test-abc123", "artifact_scope_bound": True, }) sb, efb = ns["_step3"](mock_out) self.assertTrue(sb) self.assertEqual(efb, "cve-fix/test-abc123") # Test bound=False mock_out2 = json.dumps({ "branch": "cve-fix/test-xyz", "artifact_scope_bound": False, }) sb2, efb2 = ns["_step3"](mock_out2) self.assertFalse(sb2) # --------------------------------------------------------------------------- # Round 15: Fix C — obsolete live_pr_clear preview removed from Step 5 # --------------------------------------------------------------------------- class TestStep5LivePrClearRemoved(unittest.TestCase): """Step 5 must not contain obsolete live_pr_clear/pass preview.""" def test_no_live_pr_clear_in_step5(self) -> None: """Step 5 code fence must not reference live_pr_clear.""" import re as _re md_path = Path(__file__).resolve().parent.parent / "skills" / "unattended.md" text = md_path.read_text() fences = _re.findall(r" ```python\n(.*?) ```", text, _re.DOTALL) step5 = None for fence in fences: if ( "scope_discovery_error = None" in fence and "check-scope" in fence ): step5 = fence break self.assertIsNotNone(step5, "Step 5 code fence not found") self.assertNotIn("live_pr_clear", step5, "Obsolete live_pr_clear preview must be removed") self.assertNotIn( "combine with live_pr_clear", step5, "Obsolete combine-with-live_pr_clear comment must be removed", ) def test_step5_delegates_live_decision_to_step6(self) -> None: """Step 5 text should reference step 6 bootstrap for live decision.""" md_path = Path(__file__).resolve().parent.parent / "skills" / "unattended.md" text = md_path.read_text() # The surrounding prose (not code fence) should mention delegation self.assertIn("bootstrap_check", text) self.assertIn("live_pr_check", text) # --------------------------------------------------------------------------- # Round 15: Fix D — malformed JSON guard in resolve_and_gate_source_pr # --------------------------------------------------------------------------- class TestResolveAndGateSourcePrMalformedJSON(unittest.TestCase): """resolve_and_gate_source_pr must guard against malformed/non-object JSON.""" @staticmethod def _build_function(): """Extract and compile the function from unattended.md. Adds a ``_subprocess`` keyword so tests can inject a mock; the real fence uses the module-level ``subprocess`` import. """ import re as _re md_path = Path(__file__).resolve().parent.parent / "skills" / "unattended.md" text = md_path.read_text() fences = _re.findall(r" ```python\n(.*?) ```", text, _re.DOTALL) func_fence = None for fence in fences: if "resolve_and_gate_source_pr" in fence and "pr_meta_proc" in fence: func_fence = fence break assert func_fence, "resolve_and_gate_source_pr fence not found" func_code = textwrap.dedent(func_fence) # Inject _subprocess parameter so subprocess can be mocked func_code = func_code.replace( "def resolve_and_gate_source_pr(source_pr_url, operation, scope):", "def resolve_and_gate_source_pr(source_pr_url, operation, scope," "\n _subprocess=None):", ) func_code = func_code.replace( "pr_meta_proc = subprocess.run(", "_sub = _subprocess or __import__('subprocess')\n" " pr_meta_proc = _sub.run(", ) func_code = func_code.replace( "fetch_proc = subprocess.run(", "fetch_proc = _sub.run(", ) _helper = unattended ns: dict[str, Any] = { "json": json, "_helper": _helper, } exec(compile(func_code, "", "exec"), ns) return ns["resolve_and_gate_source_pr"] def setUp(self) -> None: self._func = self._build_function() self._scope = {"cve_ids": ["CVE-2024-1"], "repo": "org/repo"} self._url = "https://github.com/org/repo/pull/42" def _make_proc(self, rc, stdout, stderr=""): p = MagicMock() p.returncode = rc p.stdout = stdout p.stderr = stderr return p def test_malformed_json_returns_needs_input(self) -> None: """Unparseable gh output => needs_input BEFORE any fetch.""" sub = MagicMock() sub.run = MagicMock(side_effect=[ self._make_proc(0, "not valid json at all"), ]) result = self._func(self._url, "backport", self._scope, _subprocess=sub) self.assertIsInstance(result, dict) self.assertEqual(result["status"], "needs_input") self.assertIn("malformed JSON", result.get("error", "")) # Only ONE subprocess call (gh pr view); no fetch self.assertEqual(sub.run.call_count, 1) def test_non_object_json_returns_needs_input(self) -> None: """JSON array instead of object => needs_input BEFORE any fetch.""" sub = MagicMock() sub.run = MagicMock(side_effect=[ self._make_proc(0, '["a", "list"]'), ]) result = self._func(self._url, "backport", self._scope, _subprocess=sub) self.assertIsInstance(result, dict) self.assertEqual(result["status"], "needs_input") self.assertIn("non-object JSON", result.get("error", "")) self.assertEqual(sub.run.call_count, 1) def test_valid_merged_still_works(self) -> None: """Valid MERGED JSON still produces (pr_meta, merge_commit) tuple.""" gh_out = json.dumps({ "state": "MERGED", "url": self._url, "mergeCommit": {"oid": "abc123def456"}, "headRefOid": "head789", "commits": [{"oid": "c1"}], }) sub = MagicMock() sub.run = MagicMock(side_effect=[ self._make_proc(0, gh_out), self._make_proc(0, "", ""), # git fetch ]) result = self._func(self._url, "backport", self._scope, _subprocess=sub) self.assertIsInstance(result, tuple) pr_meta, merge_commit = result self.assertEqual(merge_commit, "abc123def456") def test_open_pr_still_returns_awaiting_merge(self) -> None: """OPEN PR still returns awaiting_merge (regression check).""" gh_out = json.dumps({ "state": "OPEN", "url": self._url, "mergeCommit": None, "headRefOid": "abc", "commits": [], }) sub = MagicMock() sub.run = MagicMock(side_effect=[ self._make_proc(0, gh_out), ]) result = self._func(self._url, "backport", self._scope, _subprocess=sub) self.assertIsInstance(result, dict) self.assertEqual(result["status"], "awaiting_merge") # --------------------------------------------------------------------------- # Round 15: Fix E — parent header parsing before first blank line # --------------------------------------------------------------------------- class TestParentHeaderParsing(unittest.TestCase): """Parent lines must be parsed from header only, not commit message.""" def test_merge_commit_with_parent_in_message(self) -> None: """A merge commit with 'parent ' in its message body must parse only the two real header parents, not the message line.""" import subprocess with tempfile.TemporaryDirectory() as td: env = {**os.environ, "GIT_AUTHOR_NAME": "Test", "GIT_AUTHOR_EMAIL": "t@t", "GIT_COMMITTER_NAME": "Test", "GIT_COMMITTER_EMAIL": "t@t"} def git(*args: str) -> subprocess.CompletedProcess[str]: return subprocess.run( ["git", "-C", td] + list(args), capture_output=True, text=True, env=env, ) git("init", "-b", "main") Path(td, "a.txt").write_text("a") git("add", "a.txt") git("commit", "-m", "initial") git("checkout", "-b", "feature") Path(td, "b.txt").write_text("b") git("add", "b.txt") git("commit", "-m", "feature commit") git("checkout", "main") # Merge with a message containing "parent " in the body r = git("merge", "--no-ff", "feature", "-m", "Merge feature\n\nparent fake-oid-in-message\n" "This parent reference is not real.") self.assertEqual(r.returncode, 0, r.stderr) merge_sha = git("rev-parse", "HEAD").stdout.strip() cat = git("cat-file", "-p", merge_sha) # Correct header-only parsing: split on first blank line header = cat.stdout.split("\n\n", 1)[0] parent_lines = [ line for line in header.splitlines() if line.startswith("parent ") ] self.assertEqual(len(parent_lines), 2, "Should find exactly 2 header parents") # Incorrect whole-output parsing would find 3+ all_parents = [ line for line in cat.stdout.splitlines() if line.startswith("parent ") ] self.assertGreater(len(all_parents), 2, "Message body must contain a 'parent ' line " "for this test to be meaningful") # Verify select_cherry_pick_argv produces correct -m 1 argv = unattended.select_cherry_pick_argv( merge_sha, parent_lines, ) self.assertIsNotNone(argv) self.assertEqual( argv, ["git", "cherry-pick", "-x", "-m", "1", merge_sha], ) def test_single_commit_with_parent_in_message(self) -> None: """A single-parent commit with 'parent ' in its message body must parse only the one real header parent.""" import subprocess with tempfile.TemporaryDirectory() as td: env = {**os.environ, "GIT_AUTHOR_NAME": "Test", "GIT_AUTHOR_EMAIL": "t@t", "GIT_COMMITTER_NAME": "Test", "GIT_COMMITTER_EMAIL": "t@t"} def git(*args: str) -> subprocess.CompletedProcess[str]: return subprocess.run( ["git", "-C", td] + list(args), capture_output=True, text=True, env=env, ) git("init", "-b", "main") Path(td, "a.txt").write_text("a") git("add", "a.txt") git("commit", "-m", "initial") Path(td, "b.txt").write_text("b") git("add", "b.txt") git("commit", "-m", "Add b\n\nparent fake reference\n" "This line also starts with parent oid") sha = git("rev-parse", "HEAD").stdout.strip() cat = git("cat-file", "-p", sha) # Header-only parsing header = cat.stdout.split("\n\n", 1)[0] parent_lines = [ line for line in header.splitlines() if line.startswith("parent ") ] self.assertEqual(len(parent_lines), 1, "Should find exactly 1 header parent") # Whole-output parsing would find more all_parents = [ line for line in cat.stdout.splitlines() if line.startswith("parent ") ] self.assertGreater(len(all_parents), 1, "Message must contain 'parent ' for test to be meaningful") argv = unattended.select_cherry_pick_argv(sha, parent_lines) self.assertEqual( argv, ["git", "cherry-pick", "-x", sha], ) # --------------------------------------------------------------------------- # Round 15: Fix A — backport.md --jq projection removed # --------------------------------------------------------------------------- class TestBackportJqProjectionRemoved(unittest.TestCase): """backport.md must not use --jq to reshape gh pr view output.""" def test_no_jq_projection_in_backport(self) -> None: """The gh pr view command in backport.md must use raw --json only.""" md_path = ( Path(__file__).resolve().parent.parent / "skills" / "backport.md" ) text = md_path.read_text() self.assertNotIn("--jq", text, "backport.md must not use --jq projection") # Verify --json is still present for field selection (argv list format) self.assertIn("mergeCommit,state,url,headRefOid,commits", text) # --------------------------------------------------------------------------- # Round 15: Fix F — contract validate-output branch example alignment # --------------------------------------------------------------------------- class TestContractBranchExampleAlignment(unittest.TestCase): """Contract validate output example must show scoped default branch format.""" def test_example_branch_has_hash_suffix(self) -> None: """Validate output example branch must use cve-fix/{label}-{hash}.""" md_path = ( Path(__file__).resolve().parent.parent / "skills" / "unattended-contract.md" ) text = md_path.read_text() # Find the validate output JSON example import re as _re match = _re.search( r'"branch":\s*"cve-fix/EDM-1234-[a-f0-9]+"', text, ) self.assertIsNotNone( match, "Contract example must show scoped default branch " "format cve-fix/{label}-{hash}", ) # --------------------------------------------------------------------------- # Round 16A: Finding 6701+6710 — backport.md subprocess argv safety # --------------------------------------------------------------------------- class TestBackportGhArgvSafety(unittest.TestCase): """backport.md gh commands must use Python subprocess argument lists.""" @classmethod def setUpClass(cls) -> None: md_path = ( Path(__file__).resolve().parent.parent / "skills" / "backport.md" ) cls.text = md_path.read_text() def test_gh_pr_view_uses_argv_list(self) -> None: """gh pr view in backport.md must be in a python fence with argv.""" # Must have a python fence containing the gh pr view as an argv list self.assertIn( '["gh", "pr", "view", source_pr_url', self.text, "backport.md must use argv list for gh pr view", ) def test_gh_pr_view_not_bash_interpolated(self) -> None: """gh pr view must not be a bare bash command with shell interpolation.""" # The old bash form must not appear self.assertNotIn( "gh pr view {source_pr_url}", self.text, "backport.md must not use bash shell interpolation for gh pr view", ) def test_gh_pr_create_uses_argv_list(self) -> None: """gh pr create in backport.md must use Python subprocess argv.""" self.assertIn( '["gh", "pr", "create", "--draft"', self.text, "backport.md must use argv list for gh pr create", ) def test_gh_pr_create_not_bash_interpolated(self) -> None: """Backport PR create must not use bash shell interpolation.""" self.assertNotIn( "gh pr create --draft \\", self.text, "backport.md must not use bash shell form for gh pr create", ) def test_branch_title_body_as_separate_args(self) -> None: """Branch, title, body must be separate argv elements.""" # Verify --base, --title, --body are passed as separate args # (--body with literal body, not --body-file with stale file) self.assertIn('"--base", release_branch', self.text) self.assertIn('"--title", pr_title', self.text) self.assertIn('"--body", pr_body', self.text) # --------------------------------------------------------------------------- # Round 16A: Finding 6713 — pr.md unattended branch mismatch guard # --------------------------------------------------------------------------- class TestPrBranchMismatchGuard(unittest.TestCase): """pr.md must guard against unattended branch mismatch.""" @classmethod def setUpClass(cls) -> None: md_path = ( Path(__file__).resolve().parent.parent / "skills" / "pr.md" ) cls.text = md_path.read_text() def test_mismatch_guard_present(self) -> None: """pr.md must contain the branch mismatch guard for unattended mode.""" self.assertIn( "Current-branch guard", self.text, "pr.md must document the current-branch guard for unattended", ) def test_stop_on_different_unprotected(self) -> None: """Must STOP on a different unprotected branch, not silently adopt.""" self.assertIn("STOP", self.text) self.assertIn("branch mismatch", self.text) self.assertIn("needs_input", self.text) def test_keep_effective_branch(self) -> None: """Must keep the checkout when already on the effective feature branch.""" self.assertIn( "Already on the effective feature branch", self.text, ) def test_protected_base_allows_create(self) -> None: """Protected base (main/master) allows creating the branch.""" self.assertIn("protected base", self.text.lower()) # --------------------------------------------------------------------------- # Round 16A: Finding 6719 — scan.md + validate.md resource path safety # --------------------------------------------------------------------------- class TestScanResourcePath(unittest.TestCase): """scan.md must use explicit absolute path with existence guard.""" @classmethod def setUpClass(cls) -> None: md_path = ( Path(__file__).resolve().parent.parent / "skills" / "scan.md" ) cls.text = md_path.read_text() def test_no_dirname_dollar_zero_in_code(self) -> None: """Python fences must NOT use dirname $0 for path resolution.""" fences = _extract_python_fences(self.text) for fence in fences: if "SCAN_SCRIPT" in fence: self.assertNotIn( "dirname", fence, "SCAN_SCRIPT code must not use dirname $0", ) def test_no_markdown_dunder_file(self) -> None: """Must NOT use __file__ for Markdown path resolution.""" # __file__ in a python fence for importlib (test file) is fine, # but the scan.md SCAN_SCRIPT resolution must not use __file__ fences = _extract_python_fences(self.text) for fence in fences: if "SCAN_SCRIPT" in fence: self.assertNotIn( "__file__", fence, "SCAN_SCRIPT resolution must not use __file__", ) def test_explicit_absolute_path_assignment(self) -> None: """Must assign SCAN_SCRIPT from an explicit absolute path source.""" self.assertIn("SCAN_SCRIPT", self.text) self.assertIn("os.path.join", self.text) def test_existence_guard(self) -> None: """Must check file existence before use.""" self.assertIn("os.path.isfile(SCAN_SCRIPT)", self.text) def test_argv_list_invocation(self) -> None: """Must use argv list (not shell string) for subprocess.""" fences = _extract_python_fences(self.text) scan_fence = [f for f in fences if "SCAN_SCRIPT" in f and "subprocess" in f] self.assertTrue( len(scan_fence) > 0, "scan.md must have a python fence with subprocess + SCAN_SCRIPT", ) self.assertIn("subprocess.run", scan_fence[0]) # Must use list form self.assertIn("[", scan_fence[0]) class TestValidateResourcePath(unittest.TestCase): """validate.md must use explicit absolute path with existence guard.""" @classmethod def setUpClass(cls) -> None: md_path = ( Path(__file__).resolve().parent.parent / "skills" / "validate.md" ) cls.text = md_path.read_text() def test_no_dirname_dollar_zero_in_code(self) -> None: """Python fences must NOT use dirname $0 for path resolution.""" fences = _extract_python_fences(self.text) for fence in fences: if "VERIFY_SCRIPT" in fence: self.assertNotIn( "dirname", fence, "VERIFY_SCRIPT code must not use dirname $0", ) def test_no_markdown_dunder_file(self) -> None: fences = _extract_python_fences(self.text) for fence in fences: if "VERIFY_SCRIPT" in fence: self.assertNotIn( "__file__", fence, "VERIFY_SCRIPT resolution must not use __file__", ) def test_explicit_absolute_path_assignment(self) -> None: self.assertIn("VERIFY_SCRIPT", self.text) self.assertIn("os.path.join", self.text) def test_existence_guard(self) -> None: self.assertIn("os.path.isfile(VERIFY_SCRIPT)", self.text) # --------------------------------------------------------------------------- # Round 16A: Finding 6728+6738 — contract validate output docs # --------------------------------------------------------------------------- class TestContractValidateOutputDocs(unittest.TestCase): """Contract validate output must show artifact_scope_bound and base_branch docs.""" @classmethod def setUpClass(cls) -> None: md_path = ( Path(__file__).resolve().parent.parent / "skills" / "unattended-contract.md" ) cls.text = md_path.read_text() def test_unbound_example_shows_false(self) -> None: """Unbound (no --resolved-scope) example must show artifact_scope_bound: false.""" self.assertIn('"artifact_scope_bound": false', self.text) def test_bound_example_shows_true(self) -> None: """Bound (with --resolved-scope) example must show artifact_scope_bound: true.""" self.assertIn('"artifact_scope_bound": true', self.text) def test_base_branch_null_documented(self) -> None: """Unbound example must show base_branch can be null.""" self.assertIn('"base_branch": null', self.text) def test_read_only_when_unbound(self) -> None: """Must document that unbound scope is read-only.""" self.assertIn("read-only", self.text.lower()) def test_mkdir_requires_true(self) -> None: """Must document that mkdir requires artifact_scope_bound: true.""" # Check that the text mentions mkdir requiring the binding self.assertIn("artifact_scope_bound", self.text) # Must have guidance about requiring true for writes lower = self.text.lower() self.assertTrue( "mkdir" in lower and "artifact_scope_bound" in self.text, "Must document mkdir requires artifact_scope_bound", ) # --------------------------------------------------------------------------- # Round 16A: Finding 6743 — unattended.md Step 2 validate no check=True # --------------------------------------------------------------------------- class TestUnattendedValidateNoCheckTrue(unittest.TestCase): """unattended.md Step 2 validate call must not use check=True.""" @classmethod def setUpClass(cls) -> None: md_path = ( Path(__file__).resolve().parent.parent / "skills" / "unattended.md" ) cls.text = md_path.read_text() def test_validate_call_no_check_true(self) -> None: """The validate subprocess call must not use check=True.""" fences = _extract_python_fences(self.text) validate_fences = [ f for f in fences if '"validate"' in f and "SCRIPT" in f and "request.json" in f ] self.assertTrue( len(validate_fences) > 0, "Must have a validate subprocess fence", ) for fence in validate_fences: self.assertNotIn( "check=True", fence, "validate subprocess must not use check=True", ) def test_rc1_handling(self) -> None: """Must handle rc=1 (structured validation error) explicitly.""" self.assertIn("returncode == 1", self.text) def test_rc2_handling(self) -> None: """Must handle rc=2 (input/transport error) explicitly.""" self.assertIn("returncode == 2", self.text) def test_stop_before_phases(self) -> None: """Must document stopping before resume/discovery/mkdir on failure.""" # Check that the error handling mentions stopping fences = _extract_python_fences(self.text) validate_fences = [ f for f in fences if "returncode == 1" in f and "SCRIPT" in f ] self.assertTrue( len(validate_fences) > 0, "Must have rc dispatch in the validate fence", ) # --------------------------------------------------------------------------- # Round 16A: Finding 6750 — unattended.md Step 9 make-result error stops # --------------------------------------------------------------------------- class TestMakeResultErrorStops(unittest.TestCase): """Step 9 make-result must STOP before completion on error exits.""" @classmethod def setUpClass(cls) -> None: md_path = ( Path(__file__).resolve().parent.parent / "skills" / "unattended.md" ) cls.text = md_path.read_text() def test_rc0_empty_stdout_stops(self) -> None: """rc=0 with empty stdout must stop (not silently proceed).""" # Must detect empty stdout on rc=0 fences = _extract_python_fences(self.text) mr_fences = [ f for f in fences if "make-result" in f and "returncode" in f ] self.assertTrue(len(mr_fences) > 0) combined = "\n".join(mr_fences) self.assertIn("RuntimeError", combined) def test_rc1_stops_before_completion(self) -> None: """rc=1 must stop before completion report.""" fences = _extract_python_fences(self.text) mr_fences = [ f for f in fences if "make-result" in f and "returncode == 1" in f ] self.assertTrue(len(mr_fences) > 0) self.assertIn("RuntimeError", mr_fences[0]) def test_rc2_requires_persistence_marker(self) -> None: """rc=2 must check persistence_not_written marker.""" fences = _extract_python_fences(self.text) mr_fences = [ f for f in fences if "make-result" in f and "returncode == 2" in f ] self.assertTrue(len(mr_fences) > 0) self.assertIn("persistence_not_written", mr_fences[0]) def test_unexpected_rc_stops(self) -> None: """Unexpected exit codes must stop before completion.""" fences = _extract_python_fences(self.text) mr_fences = [ f for f in fences if "make-result" in f and "returncode" in f ] combined = "\n".join(mr_fences) # Must have an else clause for unexpected exit codes self.assertIn("unexpected exit", combined.lower()) # --------------------------------------------------------------------------- # Round 16A: Finding 6763 — validate-decision no check=True + R14 rule # --------------------------------------------------------------------------- class TestValidateDecisionArgvSafety(unittest.TestCase): """validate-decision calls must capture output, not use check=True.""" @classmethod def setUpClass(cls) -> None: md_path = ( Path(__file__).resolve().parent.parent / "skills" / "unattended.md" ) cls.text = md_path.read_text() cls.contract_text = ( Path(__file__).resolve().parent.parent / "skills" / "unattended-contract.md" ).read_text() def test_unattended_vd_no_check_true(self) -> None: """validate-decision in unattended.md must not use check=True.""" fences = _extract_python_fences(self.text) vd_fences = [ f for f in fences if "validate-decision" in f ] self.assertTrue(len(vd_fences) > 0) for fence in vd_fences: self.assertNotIn( "check=True", fence, "validate-decision must not use check=True in unattended.md", ) def test_contract_vd_no_check_true(self) -> None: """validate-decision in contract must not use check=True.""" fences = _extract_python_fences(self.contract_text) vd_fences = [ f for f in fences if "validate-decision" in f ] self.assertTrue(len(vd_fences) > 0) for fence in vd_fences: self.assertNotIn( "check=True", fence, "validate-decision must not use check=True in contract", ) def test_action_authorized_check(self) -> None: """Must check action_authorized in validate-decision handling.""" # action_authorized must be referenced near the validate-decision # usage — either in a code fence or in the surrounding prose that # documents the check. The prose text instructs to check both # valid and action_authorized after parsing the output. self.assertIn("action_authorized", self.text) # Verify it's documented as an explicit check requirement self.assertIn( "action_authorized: true", self.text, "Must document checking action_authorized: true", ) def test_contradictory_scan_line_replaced(self) -> None: """The old 'validation fails => proceed to scan' must be replaced.""" self.assertNotIn( "or validation fails, proceed to", self.text, "Old contradictory 'validation fails => scan' line must be removed", ) def test_r14_pending_review_rule(self) -> None: """Failed prior decision must return needs_human_review, not rescan.""" # Must have the R14 rule: failed validation => needs_human_review self.assertIn( "needs_human_review", self.text, ) # Must mention not rescanning on validation failure self.assertIn("do not proceed to `/scan`", self.text) def test_fresh_work_proceeds_to_scan(self) -> None: """Fresh new work (no prior decision) must still proceed to /scan.""" self.assertIn( "fresh new work", self.text.lower(), ) # --------------------------------------------------------------------------- # Round 16A: CLI integration — validate-decision actual exec # --------------------------------------------------------------------------- class TestValidateDecisionCLIExec(unittest.TestCase): """Exercise validate-decision CLI with invalid/rejected inputs.""" def test_missing_scan_id_returns_rc2(self) -> None: """validate-decision without --scan-id returns rc=2.""" with tempfile.TemporaryDirectory( prefix="test vd space " ) as tmpdir: resp_path = os.path.join(tmpdir, "resp.json") with open(resp_path, "w") as f: json.dump({ "decision_id": "test-1", "accepted": True, "reviewer": "x", "justification": "component_not_present", "evidence": "test", "scan_id": "scan-1", "scope": {"cve_ids": ["CVE-2024-1"]}, "timestamp": "2024-01-01T00:00:00Z", "selected_effect": "close_vex", }, f) import subprocess as _sp result = _sp.run( ["python3", "-I", str(_SCRIPT), "validate-decision", resp_path], capture_output=True, text=True, ) self.assertEqual(result.returncode, 2) self.assertIn("scan-id", result.stderr.lower()) def test_rejected_decision_returns_valid_but_not_authorized(self) -> None: """Rejected decision: valid=true but action_authorized=false.""" with tempfile.TemporaryDirectory( prefix="test vd reject " ) as tmpdir: resp_path = os.path.join(tmpdir, "resp.json") orig_path = os.path.join(tmpdir, "orig.json") scope = { "cve_ids": ["CVE-2024-1234"], "packages": ["golang.org/x/net"], "repo": "org/repo", "base_branch": "main", } with open(resp_path, "w") as f: json.dump({ "decision_id": "vex-test-1", "accepted": False, "reviewer": "jsmith", "evidence": "Rejected — not applicable.", "scan_id": "scan-test-1", "scope": scope, "timestamp": "2024-11-04T14:00:00Z", }, f) with open(orig_path, "w") as f: json.dump({ "decision_id": "vex-test-1", "question": "test?", "permitted_effects": ["close_vex", "reject"], "scan_id": "scan-test-1", "scope": scope, }, f) import subprocess as _sp result = _sp.run( ["python3", "-I", str(_SCRIPT), "validate-decision", resp_path, "--scan-id", "scan-test-1", "--scope", json.dumps(scope), "--original-request", orig_path, "--authority-context", json.dumps({"allowed_reviewer_ids": ["jsmith"]})], capture_output=True, text=True, ) # Should be valid (well-formed rejection) but not authorized self.assertEqual(result.returncode, 0, result.stderr) output = json.loads(result.stdout) self.assertTrue(output.get("valid")) self.assertFalse(output.get("action_authorized")) # --------------------------------------------------------------------------- # Round 16A: CLI integration — make-result error stops # --------------------------------------------------------------------------- class TestMakeResultCLIReject(unittest.TestCase): """make-result must reject malformed/empty data with nonzero exit.""" def test_completed_validate_missing_verdict_rc1(self) -> None: """completed remediate with validate stage but no validation_verdict=PASS → rc=1.""" with tempfile.TemporaryDirectory( prefix="test mr space " ) as tmpdir: data = { "scope": { "cve_ids": ["CVE-2024-1234"], "packages": ["golang.org/x/net"], "repo": "org/repo", "base_branch": "main", }, "stages_completed": ["start", "scan", "patch", "validate", "pr"], "stages_remaining": [], "evidence": {"scan_verdict": "fixed"}, "lifecycle_complete": False, "next_permitted_operations": [], } import subprocess as _sp result = _sp.run( ["python3", "-I", str(_SCRIPT), "make-result", "--status", "completed", "--operation", "remediate", "--artifact-dir", tmpdir, "--data", json.dumps(data)], capture_output=True, text=True, ) # Must reject: completed with validate stage but no # validation_verdict=PASS in evidence. self.assertEqual( result.returncode, 1, f"Expected rc=1 for missing validation_verdict; " f"got rc={result.returncode}; stderr={result.stderr}", ) # Must report a cause on stderr self.assertTrue( result.stderr.strip(), "Expected error message on stderr", ) # No artifact file written result_file = os.path.join(tmpdir, "unattended-result.json") self.assertFalse(os.path.isfile(result_file)) def test_malformed_json_data_rc2(self) -> None: """Malformed JSON in --data returns rc=2.""" with tempfile.TemporaryDirectory( prefix="test mr malformed " ) as tmpdir: import subprocess as _sp result = _sp.run( ["python3", "-I", str(_SCRIPT), "make-result", "--status", "completed", "--operation", "remediate", "--artifact-dir", tmpdir, "--data", "not-json{"], capture_output=True, text=True, ) self.assertEqual(result.returncode, 2) def test_valid_result_writes_json(self) -> None: """A valid make-result call writes parseable JSON to stdout.""" with tempfile.TemporaryDirectory( prefix="test mr ok " ) as tmpdir: data = { "scope": { "cve_ids": ["CVE-2024-1234"], "packages": ["golang.org/x/net"], "repo": "org/repo", "base_branch": "main", }, "stages_completed": ["start", "scan"], "stages_remaining": ["patch"], "evidence": {"scan_verdict": "present"}, "lifecycle_complete": False, "next_permitted_operations": [], } import subprocess as _sp result = _sp.run( ["python3", "-I", str(_SCRIPT), "make-result", "--status", "partial", "--operation", "remediate", "--artifact-dir", tmpdir, "--data", json.dumps(data)], capture_output=True, text=True, ) self.assertEqual(result.returncode, 0, result.stderr) out = json.loads(result.stdout) self.assertEqual(out["status"], "partial") self.assertEqual(out["operation"], "remediate") # Verify the file was written result_file = os.path.join(tmpdir, "unattended-result.json") self.assertTrue(os.path.isfile(result_file)) written = json.loads(Path(result_file).read_text()) self.assertEqual(written["status"], "partial") # --------------------------------------------------------------------------- # Helpers for extracting python fences from Markdown # --------------------------------------------------------------------------- def _extract_python_fences(md_text: str) -> list[str]: """Extract content of ```python ... ``` fences from markdown text.""" import re as _re return _re.findall( r"```python\n(.*?)```", md_text, _re.DOTALL, ) # --------------------------------------------------------------------------- # Round 16A corrective: Step 2 structured validation transport # --------------------------------------------------------------------------- class TestStep2ValidationTransport(unittest.TestCase): """Step 2 validate fences must return structured errors, not sys.exit.""" @classmethod def setUpClass(cls) -> None: md_path = ( Path(__file__).resolve().parent.parent / "skills" / "unattended.md" ) cls.text = md_path.read_text() def _get_validate_fences(self) -> list[str]: fences = _extract_python_fences(self.text) return [ f for f in fences if "validate_result" in f and "returncode" in f and "make-result" not in f # exclude Step 9 fences ] def test_no_sys_exit(self) -> None: """Step 2 fences must not use sys.exit (sys not imported).""" fences = self._get_validate_fences() self.assertTrue(len(fences) > 0) for fence in fences: self.assertNotIn("sys.exit", fence) def test_rc1_prints_and_returns(self) -> None: """rc=1 must print structured err_info and return (not discard it).""" fences = self._get_validate_fences() self.assertTrue(len(fences) > 0) combined = "\n".join(fences) # err_info must be printed, not silently discarded self.assertIn("print(", combined) self.assertIn("return err_info", combined) def test_rc2_structured_error(self) -> None: """rc=2 must build structured error with valid=False and return.""" fences = self._get_validate_fences() combined = "\n".join(fences) self.assertIn("valid", combined) self.assertIn("return err_info", combined) def test_rc0_guards_dict_and_valid(self) -> None: """rc=0 must guard JSON is dict with valid is True.""" fences = self._get_validate_fences() combined = "\n".join(fences) self.assertIn("isinstance(info, dict)", combined) self.assertIn("is not True", combined) # --------------------------------------------------------------------------- # Round 16A corrective: Decision fence rc/JSON/authorization guards # --------------------------------------------------------------------------- class TestDecisionFenceGuards(unittest.TestCase): """Validate-decision fences must guard rc/JSON and not fall through.""" @classmethod def setUpClass(cls) -> None: md_path = ( Path(__file__).resolve().parent.parent / "skills" / "unattended.md" ) cls.text = md_path.read_text() cls.contract_text = ( Path(__file__).resolve().parent.parent / "skills" / "unattended-contract.md" ).read_text() def test_unattended_vd_guards_rc_before_json(self) -> None: """Unattended vd fence must check returncode before json.loads.""" fences = _extract_python_fences(self.text) vd_fences = [ f for f in fences if "validate-decision" in f and "vd_proc" in f ] self.assertTrue(len(vd_fences) > 0) combined = "\n".join(vd_fences) self.assertIn("returncode", combined) self.assertIn("return", combined) def test_contract_vd_does_not_pass_through(self) -> None: """Contract vd fence must return on unauthorized, not just pass.""" fences = _extract_python_fences(self.contract_text) vd_fences = [ f for f in fences if "validate-decision" in f and "action_authorized" in f ] self.assertTrue(len(vd_fences) > 0) for fence in vd_fences: self.assertIn("return", fence) # Must not have a bare "pass" that falls through lines = fence.strip().split("\n") pass_lines = [ l.strip() for l in lines if l.strip() == "pass" ] self.assertEqual( len(pass_lines), 0, "Contract vd fence must not have bare pass fall-through", ) def test_contract_authorized_uses_is_true(self) -> None: """Contract must check action_authorized is True (identity).""" fences = _extract_python_fences(self.contract_text) vd_fences = [ f for f in fences if "action_authorized" in f and "valid" in f ] self.assertTrue(len(vd_fences) > 0) combined = "\n".join(vd_fences) self.assertIn("is True", combined) def test_contract_vd_guards_rc(self) -> None: """Contract vd fence must check returncode before json.loads.""" fences = _extract_python_fences(self.contract_text) vd_fences = [ f for f in fences if "validate-decision" in f and "vd_proc" in f ] self.assertTrue(len(vd_fences) > 0) combined = "\n".join(vd_fences) self.assertIn("returncode", combined) # --------------------------------------------------------------------------- # Round 16A corrective: Step 9 structured error transport # --------------------------------------------------------------------------- class TestStep9StructuredErrorTransport(unittest.TestCase): """Step 9 must emit structured error before RuntimeError on failures.""" @classmethod def setUpClass(cls) -> None: md_path = ( Path(__file__).resolve().parent.parent / "skills" / "unattended.md" ) cls.text = md_path.read_text() def _get_step9_fences(self) -> list[str]: fences = _extract_python_fences(self.text) return [ f for f in fences if "make-result" in f and "mr_proc" in f and "returncode" in f ] def test_rc1_prints_structured_error(self) -> None: """rc=1 must print structured error dict before raise.""" fences = self._get_step9_fences() self.assertTrue(len(fences) > 0) combined = "\n".join(fences) # Must print structured error on rc=1 self.assertIn("print(json.dumps(err))", combined) def test_rc2_empty_stdout_structured_error(self) -> None: """rc=2 with empty stdout must emit structured error.""" fences = self._get_step9_fences() combined = "\n".join(fences) self.assertIn("rc=2 with empty stdout", combined) self.assertIn("print(", combined) def test_rc0_guards_isinstance_dict(self) -> None: """rc=0 must check isinstance(written_result, dict).""" fences = self._get_step9_fences() combined = "\n".join(fences) self.assertIn("isinstance(written_result, dict)", combined) def test_rc0_nonobject_structured_error(self) -> None: """rc=0 non-object must emit structured error, not just parse.""" fences = self._get_step9_fences() combined = "\n".join(fences) self.assertIn("not object", combined) def test_rc2_persistence_is_true(self) -> None: """rc=2 persistence_not_written check uses `is True` identity.""" fences = self._get_step9_fences() combined = "\n".join(fences) self.assertIn( 'persistence_not_written") is True', combined, ) def test_canonical_rc0_assigns_written_result(self) -> None: """rc=0 valid path assigns written_result from parsed JSON.""" fences = self._get_step9_fences() combined = "\n".join(fences) self.assertIn("written_result", combined) # --------------------------------------------------------------------------- # Round 16A corrective: backport gh literal argv + subprocess import # --------------------------------------------------------------------------- class TestBackportGhLiteralArgvImports(unittest.TestCase): """backport.md gh fences use literal argv with subprocess import.""" @classmethod def setUpClass(cls) -> None: md_path = ( Path(__file__).resolve().parent.parent / "skills" / "backport.md" ) cls.text = md_path.read_text() def test_gh_pr_view_imports_subprocess(self) -> None: """gh pr view fence must import subprocess for attended namespace.""" fences = _extract_python_fences(self.text) view_fences = [ f for f in fences if '"gh"' in f and '"view"' in f ] self.assertTrue( len(view_fences) > 0, "backport.md must have a python fence with literal gh pr view argv", ) self.assertIn("import subprocess", view_fences[0]) def test_gh_pr_create_imports_subprocess(self) -> None: """gh pr create fence must import subprocess for attended namespace.""" fences = _extract_python_fences(self.text) create_fences = [ f for f in fences if '"gh"' in f and '"create"' in f and "subprocess.run" in f ] self.assertTrue( len(create_fences) > 0, "backport.md must have a python fence with literal gh pr create argv", ) self.assertIn("import subprocess", create_fences[0]) def test_no_shell_expansion_in_gh_fences(self) -> None: """gh fences must not contain shell expansion markers.""" fences = _extract_python_fences(self.text) gh_fences = [f for f in fences if '"gh"' in f] for fence in gh_fences: self.assertNotIn("$(", fence, "Must not use shell expansion") self.assertNotIn("shell=True", fence, "Must use shell=False") def test_pr_create_has_title_body_args(self) -> None: """gh pr create fence must pass title and body as literal args.""" fences = _extract_python_fences(self.text) create_fences = [ f for f in fences if '"gh"' in f and '"create"' in f ] self.assertTrue(len(create_fences) > 0) fence = create_fences[0] self.assertIn('"--title"', fence) self.assertIn('"--base"', fence) # --------------------------------------------------------------------------- # Round 16A corrective: scan/validate absolute path resolution # --------------------------------------------------------------------------- class TestScanAbsolutePathResolution(unittest.TestCase): """scan.md must use os.path.realpath for guaranteed absolute path.""" @classmethod def setUpClass(cls) -> None: md_path = ( Path(__file__).resolve().parent.parent / "skills" / "scan.md" ) cls.text = md_path.read_text() def test_realpath_for_absolute(self) -> None: """Must use os.path.realpath to guarantee absolute path.""" fences = _extract_python_fences(self.text) scan_fences = [ f for f in fences if "SCAN_SCRIPT" in f and "os.path" in f ] self.assertTrue(len(scan_fences) > 0) self.assertIn("os.path.realpath", scan_fences[0]) class TestValidateAbsolutePathResolution(unittest.TestCase): """validate.md must use os.path.realpath for guaranteed absolute path.""" @classmethod def setUpClass(cls) -> None: md_path = ( Path(__file__).resolve().parent.parent / "skills" / "validate.md" ) cls.text = md_path.read_text() def test_realpath_for_absolute(self) -> None: """Must use os.path.realpath to guarantee absolute path.""" fences = _extract_python_fences(self.text) verify_fences = [ f for f in fences if "VERIFY_SCRIPT" in f and "os.path" in f ] self.assertTrue(len(verify_fences) > 0) self.assertIn("os.path.realpath", verify_fences[0]) # --------------------------------------------------------------------------- # Portable closure evidence adapter # --------------------------------------------------------------------------- # Canonical scopes for test use — independently bound from host # preflight, not from request.repo_path. _FIX_CANONICAL_SCOPE: dict[str, Any] = { "cve_ids": ["CVE-2024-1234"], "repo": "org/repo", "base_branch": "main", } _VEX_CANONICAL_SCOPE: dict[str, Any] = { "cve_ids": ["CVE-2024-5678"], "repo": "org/repo", "base_branch": "main", } _DEFAULT_REVIEWER_POLICY: dict[str, Any] = { "allowed_reviewer_ids": ["jsmith"], } # Default candidate SHA constant — independent of scope, used as # default for current_candidate_sha in fixture calls. _FIX_CANDIDATE_SHA = "abc123" _VEX_CANDIDATE_SHA = "def456" # Real validation report fixture matching close.py gather_ticket_keys # parser: header row with Ticket + Explicitly in scope? columns, then # data rows. EDM-100 is the default parseable ticket. _REAL_VALIDATION_REPORT = ( "# Validation Results\n\n" "| Ticket | Explicitly in scope? | Status |\n" "|--------|---------------------|--------|\n" "| EDM-100 | Yes | PASS |\n" ) # Real context.md fixture matching close.py gather_ticket_keys source # ticket parser. _REAL_CONTEXT_MD = ( "# CVE-2024-1234 Fix\n\n- Ticket: EDM-100\n" "## Main PR\n- URL: https://github.com/org/repo/pull/42\n" ) def _make_fix_prior_result( *, pr_url: str = "https://github.com/org/repo/pull/42", pr_base: str = "main", pr_head_sha: str = _FIX_CANDIDATE_SHA, scan_id: str = "scan-20241104-fix", candidate_sha: str = _FIX_CANDIDATE_SHA, cve_ids: list[str] | None = None, repo: str = "org/repo", base_branch: str = "main", backport_results: list[dict[str, Any]] | None = None, evidence_extra: dict[str, Any] | None = None, target_branches: list[str] | None = None, ) -> dict[str, Any]: """Build a complete fix-based prior_result for testing. Includes portable_artifacts with a real validation report fixture parseable by close.py's gather_ticket_keys reader. """ if cve_ids is None: cve_ids = ["CVE-2024-1234"] scope = {"cve_ids": cve_ids, "repo": repo, "base_branch": base_branch} ev: dict[str, Any] = { "scan_verdict": "fixed", "scan_id": scan_id, "validation_verdict": "PASS", "downstream_status": "verified", "test_result": "PASS", "candidate_sha": candidate_sha, "portable_artifacts": { "context.md": _REAL_CONTEXT_MD, "validation-results.md": _REAL_VALIDATION_REPORT, }, } if evidence_extra: ev.update(evidence_extra) result: dict[str, Any] = { "status": "completed", "operation": "remediate", "scope": scope, "pr": { "url": pr_url, "branch": "cve-fix/EDM-1234", "base": pr_base, "head_sha": pr_head_sha, }, "stages_completed": ["start", "scan", "patch", "validate", "pr"], "stages_remaining": [], "evidence": ev, "backport_results": backport_results or [], } if target_branches is not None: result["target_branches"] = target_branches return { "result": result, "decision_requests": [], "scan_snapshot": { "scan_id": scan_id, "scope": scope, "candidate_sha": candidate_sha, }, } def _make_vex_prior_result( *, scan_id: str = "scan-20241104-vex", candidate_sha: str = "def456", cve_ids: list[str] | None = None, repo: str = "org/repo", base_branch: str = "main", decision_id: str = "vex-review-20241104-abc", ) -> dict[str, Any]: """Build a complete VEX-based prior_result for testing.""" if cve_ids is None: cve_ids = ["CVE-2024-5678"] scope = {"cve_ids": cve_ids, "repo": repo, "base_branch": base_branch} return { "result": { "status": "needs_human_review", "operation": "remediate", "scope": scope, "pr": None, "stages_completed": ["start", "scan"], "stages_remaining": [], "evidence": { "scan_verdict": "not_affected", "scan_id": scan_id, "candidate_sha": candidate_sha, }, }, "decision_requests": [ { "decision_id": decision_id, "question": "Is this VEX accurate?", "permitted_effects": ["close_vex", "reject", "defer"], "scan_id": scan_id, "scope": scope, }, ], "scan_snapshot": { "scan_id": scan_id, "scope": scope, "candidate_sha": candidate_sha, }, } def _make_vex_human_decision( *, decision_id: str = "vex-review-20241104-abc", scan_id: str = "scan-20241104-vex", cve_ids: list[str] | None = None, repo: str = "org/repo", base_branch: str = "main", reviewer: str = "jsmith", justification: str = "component_not_present", evidence: str = "Package not imported in any shipped binary.", ) -> dict[str, Any]: """Build a reviewed VEX decision for testing.""" if cve_ids is None: cve_ids = ["CVE-2024-5678"] return { "decision_id": decision_id, "accepted": True, "reviewer": reviewer, "justification": justification, "evidence": evidence, "scan_id": scan_id, "scope": { "cve_ids": cve_ids, "repo": repo, "base_branch": base_branch, }, "timestamp": "2024-11-04T14:00:00Z", "selected_effect": "close_vex", } class TestPortableClosureFactsFix(unittest.TestCase): """extract_portable_closure_facts: fix-based closure in fresh workspace.""" def setUp(self) -> None: self.tmpdir = tempfile.mkdtemp() # Verify NO artifact files exist self.assertEqual(os.listdir(self.tmpdir), []) def test_fix_ready_with_complete_prior(self) -> None: """Complete fix prior_result in fresh dir returns ready.""" prior = _make_fix_prior_result(target_branches=[]) request = { "operation": "jira_action", "cve_ids": ["CVE-2024-1234"], "jira_action": { "action": "transition", "tickets": ["EDM-100"], "target_status": "MODIFIED", }, } facts = unattended.extract_portable_closure_facts( prior, request, canonical_scope=_FIX_CANONICAL_SCOPE, current_candidate_sha=_FIX_CANDIDATE_SHA, ) self.assertEqual(facts["status"], "ready") self.assertEqual(facts["closure_basis"], "fix") self.assertIn( "https://github.com/org/repo/pull/42", facts["pr_urls"], ) self.assertEqual(facts["tickets"], ["EDM-100"]) self.assertEqual(facts["pending"], []) self.assertEqual( facts["evidence_facts"]["scan_id"], "scan-20241104-fix", ) self.assertEqual( facts["evidence_facts"]["candidate_sha"], "abc123", ) self.assertEqual( facts["evidence_facts"]["validation_verdict"], "PASS", ) def test_fix_with_backports(self) -> None: """Backport PR URLs are extracted from backport_results.""" # Include EDM-200 in validation report so both current # tickets are covered by the material check. _backport_vr = ( "# Validation Results\n\n" "| Ticket | Explicitly in scope? | Status |\n" "|--------|---------------------|--------|\n" "| EDM-100 | Yes | PASS |\n" "| EDM-200 | Yes | PASS |\n" ) prior = _make_fix_prior_result( backport_results=[ { "target_branch": "release-2.16", "status": "merged", "pr": { "url": "https://github.com/org/repo/pull/43", "branch": "cve-fix/bp-release-2.16", "base": "release-2.16", "head_sha": "bbb111", }, "evidence": {"cherry_pick": "clean"}, }, ], target_branches=["release-2.16"], evidence_extra={ "portable_artifacts": { "context.md": _REAL_CONTEXT_MD, "validation-results.md": _backport_vr, }, }, ) request = { "operation": "jira_action", "cve_ids": ["CVE-2024-1234"], "jira_action": { "action": "transition", "tickets": ["EDM-100", "EDM-200"], "target_status": "MODIFIED", }, } facts = unattended.extract_portable_closure_facts( prior, request, canonical_scope=_FIX_CANONICAL_SCOPE, current_candidate_sha=_FIX_CANDIDATE_SHA, ) self.assertEqual(facts["status"], "ready") self.assertEqual(facts["closure_basis"], "fix") self.assertIn( "https://github.com/org/repo/pull/42", facts["pr_urls"], ) self.assertIn( "https://github.com/org/repo/pull/43", facts["pr_urls"], ) self.assertEqual( facts["evidence_facts"]["backport_pr_urls"], ["https://github.com/org/repo/pull/43"], ) self.assertEqual( facts["evidence_facts"]["required_targets"], ["release-2.16"], ) def test_fix_with_portable_artifacts(self) -> None: """Portable artifacts in evidence are passed through to map.""" prior = _make_fix_prior_result( target_branches=[], evidence_extra={ "portable_artifacts": { "context.md": _REAL_CONTEXT_MD, "validation-results.md": _REAL_VALIDATION_REPORT, }, }, ) request = { "operation": "jira_action", "cve_ids": ["CVE-2024-1234"], "jira_action": { "action": "transition", "tickets": ["EDM-100"], "target_status": "MODIFIED", }, } facts = unattended.extract_portable_closure_facts( prior, request, canonical_scope=_FIX_CANONICAL_SCOPE, current_candidate_sha=_FIX_CANDIDATE_SHA, ) self.assertEqual(facts["status"], "ready") self.assertIn("context.md", facts["artifacts_map"]) self.assertIn("validation-results.md", facts["artifacts_map"]) class TestPortableClosureFactsVex(unittest.TestCase): """extract_portable_closure_facts: VEX-based closure in fresh workspace.""" def setUp(self) -> None: self.tmpdir = tempfile.mkdtemp() self.assertEqual(os.listdir(self.tmpdir), []) def test_vex_ready_with_reviewed_decision(self) -> None: """Complete VEX prior + reviewed decision returns ready.""" prior = _make_vex_prior_result() decision = _make_vex_human_decision() request = { "operation": "jira_action", "cve_ids": ["CVE-2024-5678"], "jira_action": { "action": "transition", "tickets": ["EDM-300"], "target_status": "Not a Bug", "vex_justification": "component_not_present", }, "human_decisions": [decision], "reviewer_policy": _DEFAULT_REVIEWER_POLICY, } facts = unattended.extract_portable_closure_facts( prior, request, canonical_scope=_VEX_CANONICAL_SCOPE, current_candidate_sha=_VEX_CANDIDATE_SHA, ) self.assertEqual(facts["status"], "ready") self.assertEqual(facts["closure_basis"], "vex") self.assertEqual(facts["tickets"], ["EDM-300"]) self.assertEqual( facts["vex_facts"]["justification"], "component_not_present", ) self.assertEqual(facts["vex_facts"]["reviewer"], "jsmith") self.assertEqual( facts["vex_facts"]["scan_id"], "scan-20241104-vex", ) self.assertEqual(facts["pending"], []) # No PR URLs for VEX closures self.assertEqual(facts["pr_urls"], []) def test_vex_missing_decision_returns_needs_human_review(self) -> None: """VEX request without reviewed decision returns needs_human_review.""" prior = _make_vex_prior_result() request = { "operation": "jira_action", "cve_ids": ["CVE-2024-5678"], "jira_action": { "action": "transition", "tickets": ["EDM-300"], "target_status": "Not a Bug", "vex_justification": "component_not_present", }, } facts = unattended.extract_portable_closure_facts( prior, request, canonical_scope=_VEX_CANONICAL_SCOPE, current_candidate_sha=_VEX_CANDIDATE_SHA, ) self.assertEqual(facts["status"], "needs_human_review") self.assertEqual(facts["closure_basis"], "vex") self.assertTrue(len(facts["pending"]) > 0) self.assertIn("reviewed human decision", facts["pending"][0]) class TestPortableClosureFactsNegative(unittest.TestCase): """extract_portable_closure_facts: negative/rejection cases.""" def setUp(self) -> None: self.tmpdir = tempfile.mkdtemp() self.assertEqual(os.listdir(self.tmpdir), []) def test_wrong_repo_rejected(self) -> None: """Mismatched repo between canonical scope and snapshot.""" prior = _make_fix_prior_result(repo="org/repo", target_branches=[]) request = { "operation": "jira_action", "cve_ids": ["CVE-2024-1234"], "jira_action": { "action": "transition", "tickets": ["EDM-100"], "target_status": "MODIFIED", }, } # Canonical scope from preflight says different repo scope = { "cve_ids": ["CVE-2024-1234"], "repo": "other/repo", "base_branch": "main", } facts = unattended.extract_portable_closure_facts( prior, request, canonical_scope=scope, current_candidate_sha=_FIX_CANDIDATE_SHA, ) self.assertEqual(facts["status"], "needs_input") self.assertTrue( any("does not match" in p for p in facts["pending"]), ) def test_wrong_base_branch_rejected(self) -> None: """Mismatched base_branch between canonical scope and snapshot.""" prior = _make_fix_prior_result( base_branch="main", target_branches=[], ) request = { "operation": "jira_action", "cve_ids": ["CVE-2024-1234"], "jira_action": { "action": "transition", "tickets": ["EDM-100"], "target_status": "MODIFIED", }, } # Canonical scope says different base branch scope = { "cve_ids": ["CVE-2024-1234"], "repo": "org/repo", "base_branch": "release-2.16", } facts = unattended.extract_portable_closure_facts( prior, request, canonical_scope=scope, current_candidate_sha=_FIX_CANDIDATE_SHA, ) self.assertEqual(facts["status"], "needs_input") self.assertTrue( any("does not match" in p for p in facts["pending"]), ) def test_missing_candidate_sha_rejected(self) -> None: """Missing candidate_sha in snapshot is rejected.""" prior = _make_fix_prior_result(target_branches=[]) prior["scan_snapshot"]["candidate_sha"] = "" request = { "operation": "jira_action", "cve_ids": ["CVE-2024-1234"], "jira_action": { "action": "transition", "tickets": ["EDM-100"], "target_status": "MODIFIED", }, } facts = unattended.extract_portable_closure_facts( prior, request, canonical_scope=_FIX_CANONICAL_SCOPE, current_candidate_sha=_FIX_CANDIDATE_SHA, ) self.assertEqual(facts["status"], "needs_input") self.assertTrue( any("candidate_sha" in p for p in facts["pending"]), ) def test_missing_scan_id_rejected(self) -> None: """Missing scan_id in snapshot is rejected.""" prior = _make_fix_prior_result(target_branches=[]) prior["scan_snapshot"]["scan_id"] = "" request = { "operation": "jira_action", "cve_ids": ["CVE-2024-1234"], "jira_action": { "action": "transition", "tickets": ["EDM-100"], "target_status": "MODIFIED", }, } facts = unattended.extract_portable_closure_facts( prior, request, canonical_scope=_FIX_CANONICAL_SCOPE, current_candidate_sha=_FIX_CANDIDATE_SHA, ) self.assertEqual(facts["status"], "needs_input") self.assertTrue(any("scan_id" in p for p in facts["pending"])) def test_vex_wrong_scan_id_rejected(self) -> None: """VEX decision with wrong scan_id is rejected.""" prior = _make_vex_prior_result(scan_id="scan-CURRENT") decision = _make_vex_human_decision(scan_id="scan-STALE") request = { "operation": "jira_action", "cve_ids": ["CVE-2024-5678"], "jira_action": { "action": "transition", "tickets": ["EDM-300"], "target_status": "Not a Bug", "vex_justification": "component_not_present", }, "human_decisions": [decision], "reviewer_policy": _DEFAULT_REVIEWER_POLICY, } scope = { "cve_ids": ["CVE-2024-5678"], "repo": "org/repo", "base_branch": "main", } facts = unattended.extract_portable_closure_facts( prior, request, canonical_scope=scope, current_candidate_sha=_VEX_CANDIDATE_SHA, ) self.assertEqual(facts["status"], "needs_input") # validate_human_decision_response uses "mismatch" wording self.assertTrue( any("scan_id" in p and "mismatch" in p for p in facts["pending"]), ) def test_vex_missing_reviewer_rejected(self) -> None: """VEX decision without named reviewer is rejected.""" prior = _make_vex_prior_result() decision = _make_vex_human_decision(reviewer="") request = { "operation": "jira_action", "cve_ids": ["CVE-2024-5678"], "jira_action": { "action": "transition", "tickets": ["EDM-300"], "target_status": "Not a Bug", "vex_justification": "component_not_present", }, "human_decisions": [decision], "reviewer_policy": _DEFAULT_REVIEWER_POLICY, } facts = unattended.extract_portable_closure_facts( prior, request, canonical_scope=_VEX_CANONICAL_SCOPE, current_candidate_sha=_VEX_CANDIDATE_SHA, ) self.assertEqual(facts["status"], "needs_input") self.assertTrue( any("reviewer" in p for p in facts["pending"]), ) def test_vex_missing_evidence_rejected(self) -> None: """VEX decision without evidence is rejected.""" prior = _make_vex_prior_result() decision = _make_vex_human_decision(evidence="") request = { "operation": "jira_action", "cve_ids": ["CVE-2024-5678"], "jira_action": { "action": "transition", "tickets": ["EDM-300"], "target_status": "Not a Bug", "vex_justification": "component_not_present", }, "human_decisions": [decision], "reviewer_policy": _DEFAULT_REVIEWER_POLICY, } facts = unattended.extract_portable_closure_facts( prior, request, canonical_scope=_VEX_CANONICAL_SCOPE, current_candidate_sha=_VEX_CANDIDATE_SHA, ) self.assertEqual(facts["status"], "needs_input") self.assertTrue( any("evidence" in p for p in facts["pending"]), ) def test_vex_wrong_effect_rejected(self) -> None: """VEX decision with wrong selected_effect is rejected.""" prior = _make_vex_prior_result() decision = _make_vex_human_decision() decision["selected_effect"] = "reject" decision["accepted"] = False request = { "operation": "jira_action", "cve_ids": ["CVE-2024-5678"], "jira_action": { "action": "transition", "tickets": ["EDM-300"], "target_status": "Not a Bug", "vex_justification": "component_not_present", }, "human_decisions": [decision], "reviewer_policy": _DEFAULT_REVIEWER_POLICY, } facts = unattended.extract_portable_closure_facts( prior, request, canonical_scope=_VEX_CANONICAL_SCOPE, current_candidate_sha=_VEX_CANDIDATE_SHA, ) # Rejected decision -> needs_human_review (no accepted close_vex) self.assertEqual(facts["status"], "needs_human_review") def test_unsupported_vex_justification_rejected(self) -> None: """Unsupported VEX justification code is rejected.""" prior = _make_vex_prior_result() decision = _make_vex_human_decision() request = { "operation": "jira_action", "cve_ids": ["CVE-2024-5678"], "jira_action": { "action": "transition", "tickets": ["EDM-300"], "target_status": "Not a Bug", "vex_justification": "totally_made_up", }, "human_decisions": [decision], "reviewer_policy": _DEFAULT_REVIEWER_POLICY, } facts = unattended.extract_portable_closure_facts( prior, request, canonical_scope=_VEX_CANONICAL_SCOPE, current_candidate_sha=_VEX_CANDIDATE_SHA, ) self.assertEqual(facts["status"], "needs_input") self.assertTrue( any("unsupported" in p for p in facts["pending"]), ) def test_missing_jira_action_rejected(self) -> None: """Request without jira_action returns needs_input.""" prior = _make_fix_prior_result(target_branches=[]) request = {"operation": "jira_action", "cve_ids": ["CVE-2024-1234"]} facts = unattended.extract_portable_closure_facts( prior, request, canonical_scope=_FIX_CANONICAL_SCOPE, current_candidate_sha=_FIX_CANDIDATE_SHA, ) self.assertEqual(facts["status"], "needs_input") self.assertTrue( any("jira_action" in p for p in facts["pending"]), ) def test_empty_tickets_rejected(self) -> None: """jira_action with empty tickets is rejected.""" prior = _make_fix_prior_result(target_branches=[]) request = { "operation": "jira_action", "cve_ids": ["CVE-2024-1234"], "jira_action": { "action": "transition", "tickets": [], "target_status": "MODIFIED", }, } facts = unattended.extract_portable_closure_facts( prior, request, canonical_scope=_FIX_CANONICAL_SCOPE, current_candidate_sha=_FIX_CANDIDATE_SHA, ) self.assertEqual(facts["status"], "needs_input") def test_missing_result_in_prior(self) -> None: """prior_result without result dict returns needs_input.""" prior = {"result": "not a dict", "decision_requests": [], "scan_snapshot": {}} request = { "operation": "jira_action", "jira_action": { "action": "transition", "tickets": ["EDM-100"], "target_status": "MODIFIED", }, } facts = unattended.extract_portable_closure_facts( prior, request, canonical_scope=_FIX_CANONICAL_SCOPE, current_candidate_sha=_FIX_CANDIDATE_SHA, ) self.assertEqual(facts["status"], "needs_input") def test_missing_snapshot_in_prior(self) -> None: """prior_result without scan_snapshot returns needs_input.""" prior = {"result": {}, "decision_requests": [], "scan_snapshot": None} request = { "operation": "jira_action", "jira_action": { "action": "transition", "tickets": ["EDM-100"], "target_status": "MODIFIED", }, } facts = unattended.extract_portable_closure_facts( prior, request, canonical_scope=_FIX_CANONICAL_SCOPE, current_candidate_sha=_FIX_CANDIDATE_SHA, ) self.assertEqual(facts["status"], "needs_input") def test_no_pr_no_vex_returns_needs_input(self) -> None: """No PR and no VEX means closure basis unknown.""" prior = _make_fix_prior_result(target_branches=[]) prior["result"]["pr"] = None # Remove PR request = { "operation": "jira_action", "cve_ids": ["CVE-2024-1234"], "jira_action": { "action": "transition", "tickets": ["EDM-100"], "target_status": "MODIFIED", }, } facts = unattended.extract_portable_closure_facts( prior, request, canonical_scope=_FIX_CANONICAL_SCOPE, current_candidate_sha=_FIX_CANDIDATE_SHA, ) self.assertEqual(facts["status"], "needs_input") self.assertTrue( any("closure basis" in p for p in facts["pending"]), ) def test_zero_write_spies(self) -> None: """Adapter performs no file I/O — fresh temp dir stays empty.""" prior = _make_fix_prior_result(target_branches=[]) request = { "operation": "jira_action", "cve_ids": ["CVE-2024-1234"], "jira_action": { "action": "transition", "tickets": ["EDM-100"], "target_status": "MODIFIED", }, } unattended.extract_portable_closure_facts( prior, request, canonical_scope=_FIX_CANONICAL_SCOPE, current_candidate_sha=_FIX_CANDIDATE_SHA, ) self.assertEqual(os.listdir(self.tmpdir), []) def test_vex_scope_mismatch_rejected(self) -> None: """VEX decision scope mismatching snapshot is rejected.""" prior = _make_vex_prior_result(repo="org/repo") decision = _make_vex_human_decision(repo="other/repo") request = { "operation": "jira_action", "cve_ids": ["CVE-2024-5678"], "jira_action": { "action": "transition", "tickets": ["EDM-300"], "target_status": "Not a Bug", "vex_justification": "component_not_present", }, "human_decisions": [decision], "reviewer_policy": _DEFAULT_REVIEWER_POLICY, } facts = unattended.extract_portable_closure_facts( prior, request, canonical_scope=_VEX_CANONICAL_SCOPE, current_candidate_sha=_VEX_CANDIDATE_SHA, ) self.assertEqual(facts["status"], "needs_input") self.assertTrue( any("scope" in p and "does not match" in p for p in facts["pending"]), ) def test_vex_missing_justification_rejected(self) -> None: """VEX decision without justification is rejected.""" prior = _make_vex_prior_result() decision = _make_vex_human_decision(justification="") request = { "operation": "jira_action", "cve_ids": ["CVE-2024-5678"], "jira_action": { "action": "transition", "tickets": ["EDM-300"], "target_status": "Not a Bug", "vex_justification": "component_not_present", }, "human_decisions": [decision], "reviewer_policy": _DEFAULT_REVIEWER_POLICY, } facts = unattended.extract_portable_closure_facts( prior, request, canonical_scope=_VEX_CANONICAL_SCOPE, current_candidate_sha=_VEX_CANDIDATE_SHA, ) self.assertEqual(facts["status"], "needs_input") self.assertTrue( any("justification" in p for p in facts["pending"]), ) def test_missing_canonical_scope_rejected(self) -> None: """No canonical_scope returns needs_input.""" prior = _make_fix_prior_result(target_branches=[]) request = { "operation": "jira_action", "cve_ids": ["CVE-2024-1234"], "jira_action": { "action": "transition", "tickets": ["EDM-100"], "target_status": "MODIFIED", }, } # Omit canonical_scope facts = unattended.extract_portable_closure_facts(prior, request) self.assertEqual(facts["status"], "needs_input") self.assertTrue( any("canonical_scope" in p for p in facts["pending"]), ) def test_fix_unknown_targets_blocked(self) -> None: """FIX closure with unknown target_branches (None) is blocked.""" prior = _make_fix_prior_result() # target_branches defaults None request = { "operation": "jira_action", "cve_ids": ["CVE-2024-1234"], "jira_action": { "action": "transition", "tickets": ["EDM-100"], "target_status": "MODIFIED", }, } facts = unattended.extract_portable_closure_facts( prior, request, canonical_scope=_FIX_CANONICAL_SCOPE, current_candidate_sha=_FIX_CANDIDATE_SHA, ) self.assertEqual(facts["status"], "needs_input") self.assertTrue( any("target_branches" in p for p in facts["pending"]), ) def test_vex_missing_out_request_rejected(self) -> None: """VEX decision_id not matching any OUT request is rejected.""" prior = _make_vex_prior_result() decision = _make_vex_human_decision( decision_id="wrong-id-no-match", ) request = { "operation": "jira_action", "cve_ids": ["CVE-2024-5678"], "jira_action": { "action": "transition", "tickets": ["EDM-300"], "target_status": "Not a Bug", "vex_justification": "component_not_present", }, "human_decisions": [decision], "reviewer_policy": _DEFAULT_REVIEWER_POLICY, } facts = unattended.extract_portable_closure_facts( prior, request, canonical_scope=_VEX_CANONICAL_SCOPE, current_candidate_sha=_VEX_CANDIDATE_SHA, ) self.assertEqual(facts["status"], "needs_input") self.assertTrue( any("decision_id" in p and "does not match" in p for p in facts["pending"]), ) def test_vex_ineligible_reviewer_rejected(self) -> None: """Reviewer not in allowed_reviewer_ids is rejected.""" prior = _make_vex_prior_result() decision = _make_vex_human_decision(reviewer="unauthorized_user") request = { "operation": "jira_action", "cve_ids": ["CVE-2024-5678"], "jira_action": { "action": "transition", "tickets": ["EDM-300"], "target_status": "Not a Bug", "vex_justification": "component_not_present", }, "human_decisions": [decision], "reviewer_policy": { "allowed_reviewer_ids": ["jsmith", "alice"], }, } facts = unattended.extract_portable_closure_facts( prior, request, canonical_scope=_VEX_CANONICAL_SCOPE, current_candidate_sha=_VEX_CANDIDATE_SHA, ) self.assertEqual(facts["status"], "needs_input") self.assertTrue( any("not in" in p and "allowed_reviewer_ids" in p for p in facts["pending"]), ) def test_vex_mismatched_canonical_reason_rejected(self) -> None: """Decision justification != request vex_justification blocked.""" prior = _make_vex_prior_result() # Decision says vulnerable_code_not_present but request says # component_not_present decision = _make_vex_human_decision( justification="vulnerable_code_not_present", ) request = { "operation": "jira_action", "cve_ids": ["CVE-2024-5678"], "jira_action": { "action": "transition", "tickets": ["EDM-300"], "target_status": "Not a Bug", "vex_justification": "component_not_present", }, "human_decisions": [decision], "reviewer_policy": _DEFAULT_REVIEWER_POLICY, } facts = unattended.extract_portable_closure_facts( prior, request, canonical_scope=_VEX_CANONICAL_SCOPE, current_candidate_sha=_VEX_CANDIDATE_SHA, ) self.assertEqual(facts["status"], "needs_input") self.assertTrue( any("justification" in p and "does not match" in p for p in facts["pending"]), ) def test_vex_missing_reviewer_policy_rejected(self) -> None: """VEX decision without reviewer_policy in request blocked.""" prior = _make_vex_prior_result() decision = _make_vex_human_decision() request = { "operation": "jira_action", "cve_ids": ["CVE-2024-5678"], "jira_action": { "action": "transition", "tickets": ["EDM-300"], "target_status": "Not a Bug", "vex_justification": "component_not_present", }, "human_decisions": [decision], # No reviewer_policy } facts = unattended.extract_portable_closure_facts( prior, request, canonical_scope=_VEX_CANONICAL_SCOPE, current_candidate_sha=_VEX_CANDIDATE_SHA, ) self.assertEqual(facts["status"], "needs_input") self.assertTrue( any("reviewer_policy" in p for p in facts["pending"]), ) # --------------------------------------------------------------------------- # Round 16B: Requirement 1 — canonical_scope requires cve_ids # --------------------------------------------------------------------------- class TestCanonicalScopeRequiresCveIds(unittest.TestCase): """canonical_scope must contain nonempty cve_ids list.""" def test_arbitrary_nonempty_dict_rejected(self) -> None: """A nonempty dict without cve_ids is rejected.""" prior = _make_fix_prior_result(target_branches=[]) request = { "operation": "jira_action", "cve_ids": ["CVE-2024-1234"], "jira_action": { "action": "transition", "tickets": ["EDM-100"], "target_status": "MODIFIED", }, } facts = unattended.extract_portable_closure_facts( prior, request, canonical_scope={"repo": "org/repo", "base_branch": "main"}, ) self.assertEqual(facts["status"], "needs_input") self.assertTrue( any("cve_ids" in p for p in facts["pending"]), f"Expected cve_ids error, got: {facts['pending']}", ) def test_empty_cve_ids_list_rejected(self) -> None: """canonical_scope with empty cve_ids list is rejected.""" prior = _make_fix_prior_result(target_branches=[]) request = { "operation": "jira_action", "cve_ids": ["CVE-2024-1234"], "jira_action": { "action": "transition", "tickets": ["EDM-100"], "target_status": "MODIFIED", }, } facts = unattended.extract_portable_closure_facts( prior, request, canonical_scope={ "cve_ids": [], "repo": "org/repo", "base_branch": "main", }, ) self.assertEqual(facts["status"], "needs_input") self.assertTrue( any("cve_ids" in p for p in facts["pending"]), ) def test_cve_ids_non_list_rejected(self) -> None: """canonical_scope with cve_ids as string is rejected.""" prior = _make_fix_prior_result(target_branches=[]) request = { "operation": "jira_action", "cve_ids": ["CVE-2024-1234"], "jira_action": { "action": "transition", "tickets": ["EDM-100"], "target_status": "MODIFIED", }, } facts = unattended.extract_portable_closure_facts( prior, request, canonical_scope={ "cve_ids": "CVE-2024-1234", "repo": "org/repo", "base_branch": "main", }, ) self.assertEqual(facts["status"], "needs_input") self.assertTrue( any("cve_ids" in p for p in facts["pending"]), ) def test_valid_canonical_scope_with_cve_ids_accepted(self) -> None: """canonical_scope with nonempty cve_ids list is accepted.""" prior = _make_fix_prior_result(target_branches=[]) request = { "operation": "jira_action", "cve_ids": ["CVE-2024-1234"], "jira_action": { "action": "transition", "tickets": ["EDM-100"], "target_status": "MODIFIED", }, } facts = unattended.extract_portable_closure_facts( prior, request, canonical_scope=_FIX_CANONICAL_SCOPE, current_candidate_sha=_FIX_CANDIDATE_SHA, ) self.assertEqual(facts["status"], "ready") # --------------------------------------------------------------------------- # Round 16B: Requirement 2 — collect_portable_artifacts producer # --------------------------------------------------------------------------- class TestCollectPortableArtifacts(unittest.TestCase): """collect_portable_artifacts: read-only whitelisted file collector.""" def test_empty_dir_returns_empty(self) -> None: """Empty directory returns empty dict.""" with tempfile.TemporaryDirectory() as td: result = unattended.collect_portable_artifacts(td) self.assertEqual(result, {}) def test_nonexistent_dir_returns_empty(self) -> None: """Non-existent path returns empty dict.""" result = unattended.collect_portable_artifacts("/nonexistent/path") self.assertEqual(result, {}) def test_collects_whitelisted_basenames(self) -> None: """Collects only whitelisted basenames.""" with tempfile.TemporaryDirectory() as td: # Create whitelisted files Path(td, "context.md").write_text("# Context\n- Ticket: EDM-100\n") Path(td, "validation-results.md").write_text("| PASS |\n") Path(td, "patch-log.md").write_text("# Patch applied\n") Path(td, "scan-result.json").write_text('{"verdict": "fixed"}\n') # Create non-whitelisted file Path(td, "unattended-result.json").write_text('{"status": "ok"}') Path(td, "secret.env").write_text("TOKEN=secret") result = unattended.collect_portable_artifacts(td) # Whitelisted files present self.assertIn("context.md", result) self.assertIn("validation-results.md", result) self.assertIn("patch-log.md", result) self.assertIn("scan-result.json", result) # Non-whitelisted files excluded self.assertNotIn("unattended-result.json", result) self.assertNotIn("secret.env", result) def test_skips_missing_files(self) -> None: """Only collects files that actually exist.""" with tempfile.TemporaryDirectory() as td: Path(td, "context.md").write_text("# Context\n") # Other whitelisted files don't exist result = unattended.collect_portable_artifacts(td) self.assertEqual(list(result.keys()), ["context.md"]) def test_skips_empty_files(self) -> None: """Empty files are not included.""" with tempfile.TemporaryDirectory() as td: Path(td, "context.md").write_text("") Path(td, "patch-log.md").write_text("# Patch\n") result = unattended.collect_portable_artifacts(td) self.assertNotIn("context.md", result) self.assertIn("patch-log.md", result) def test_content_preserved_exactly(self) -> None: """File content is preserved exactly.""" with tempfile.TemporaryDirectory() as td: content = "# Context\n- URL: https://example.com/pull/1\n- Ticket: EDM-100\n" Path(td, "context.md").write_text(content) result = unattended.collect_portable_artifacts(td) self.assertEqual(result["context.md"], content) def test_symlink_escape_blocked(self) -> None: """Symlink pointing outside artifact_dir is blocked.""" with tempfile.TemporaryDirectory() as td: # Create a file outside the artifact dir outside = Path(td, "outside") outside.mkdir() secret = outside / "context.md" secret.write_text("SECRET CONTENT") # Create artifact dir with symlink art_dir = Path(td, "artifacts") art_dir.mkdir() symlink = art_dir / "context.md" try: symlink.symlink_to(secret) except OSError: self.skipTest("Symlinks not supported") result = unattended.collect_portable_artifacts(str(art_dir)) self.assertNotIn("context.md", result) def test_all_six_basenames_recognized(self) -> None: """All 6 whitelisted basenames are collected when present.""" with tempfile.TemporaryDirectory() as td: for basename in [ "context.md", "validation-results.md", "patch-log.md", "backport-log.md", "scan-result.json", "scan-results.md", ]: Path(td, basename).write_text(f"# {basename}\n") result = unattended.collect_portable_artifacts(td) self.assertEqual(len(result), 6) # --------------------------------------------------------------------------- # Round 16B: Requirement 3 — fix material readiness # --------------------------------------------------------------------------- class TestFixMaterialReadiness(unittest.TestCase): """Fix closure readiness requires actual evidence, not thin metadata.""" def test_thin_fix_no_scan_verdict_blocks(self) -> None: """Fix prior with PR but no scan_verdict blocks closure.""" prior = _make_fix_prior_result(target_branches=[]) # Remove scan_verdict from evidence prior["result"]["evidence"] = { "scan_id": "scan-1", "validation_verdict": "PASS", } request = { "operation": "jira_action", "cve_ids": ["CVE-2024-1234"], "jira_action": { "action": "transition", "tickets": ["EDM-100"], "target_status": "MODIFIED", }, } facts = unattended.extract_portable_closure_facts( prior, request, canonical_scope=_FIX_CANONICAL_SCOPE, current_candidate_sha=_FIX_CANDIDATE_SHA, ) self.assertEqual(facts["status"], "needs_input") self.assertEqual(facts["closure_basis"], "fix") self.assertTrue( any("scan_verdict" in p for p in facts["pending"]), f"Expected scan_verdict error, got: {facts['pending']}", ) def test_thin_fix_no_validation_verdict_blocks(self) -> None: """Fix prior with PR but no validation_verdict blocks closure.""" prior = _make_fix_prior_result(target_branches=[]) prior["result"]["evidence"] = { "scan_id": "scan-1", "scan_verdict": "fixed", } request = { "operation": "jira_action", "cve_ids": ["CVE-2024-1234"], "jira_action": { "action": "transition", "tickets": ["EDM-100"], "target_status": "MODIFIED", }, } facts = unattended.extract_portable_closure_facts( prior, request, canonical_scope=_FIX_CANONICAL_SCOPE, current_candidate_sha=_FIX_CANDIDATE_SHA, ) self.assertEqual(facts["status"], "needs_input") self.assertEqual(facts["closure_basis"], "fix") self.assertTrue( any("validation_verdict" in p for p in facts["pending"]), ) def test_thin_fix_empty_evidence_blocks(self) -> None: """Fix prior with PR but empty evidence dict blocks closure.""" prior = _make_fix_prior_result(target_branches=[]) prior["result"]["evidence"] = {} request = { "operation": "jira_action", "cve_ids": ["CVE-2024-1234"], "jira_action": { "action": "transition", "tickets": ["EDM-100"], "target_status": "MODIFIED", }, } facts = unattended.extract_portable_closure_facts( prior, request, canonical_scope=_FIX_CANONICAL_SCOPE, current_candidate_sha=_FIX_CANDIDATE_SHA, ) self.assertEqual(facts["status"], "needs_input") self.assertEqual(facts["closure_basis"], "fix") self.assertTrue(len(facts["pending"]) >= 2) def test_complete_fix_evidence_passes(self) -> None: """Fix prior with both verdicts passes material readiness.""" prior = _make_fix_prior_result(target_branches=[]) request = { "operation": "jira_action", "cve_ids": ["CVE-2024-1234"], "jira_action": { "action": "transition", "tickets": ["EDM-100"], "target_status": "MODIFIED", }, } facts = unattended.extract_portable_closure_facts( prior, request, canonical_scope=_FIX_CANONICAL_SCOPE, current_candidate_sha=_FIX_CANDIDATE_SHA, ) self.assertEqual(facts["status"], "ready") self.assertEqual(facts["closure_basis"], "fix") self.assertEqual(facts["pending"], []) # --------------------------------------------------------------------------- # Round 16B: Requirement 5 — real 2-dir dataflow test # --------------------------------------------------------------------------- class TestRealTwoDirDataflow(unittest.TestCase): """Real dataflow: phase fixtures → producer → serialize → fresh consumer. Creates actual phase outputs in dir A, uses collect_portable_artifacts + make-result CLI to serialize, transfers only the JSON wrapper to dir B (without A's files), then exercises extract_portable_closure_facts with independent canonical_scope and verifies data usability without old files. """ def _create_phase_fixtures(self, art_dir: str) -> None: """Create realistic phase output files in art_dir.""" Path(art_dir, "context.md").write_text( "# CVE-2024-1234 Fix\n\n" "## Main PR\n- URL: https://github.com/org/repo/pull/42\n" "- Branch: cve-fix/EDM-1234\n- Base: main\n\n" "## Tickets\n- EDM-100\n", ) Path(art_dir, "scan-result.json").write_text(json.dumps({ "verdict": "present", "scan_id": "scan-20261104-abc", "cve_id": "CVE-2024-1234", "package": "golang.org/x/net", }, indent=2)) Path(art_dir, "patch-log.md").write_text( "# Patch Log\n\nBumped golang.org/x/net " "v0.17.0 → v0.23.0\n", ) Path(art_dir, "validation-results.md").write_text( "# Validation Results\n\n" "| Ticket | Explicitly in scope? | Status |\n" "|--------|---------------------|--------|\n" "| EDM-100 | Yes | PASS |\n", ) def test_fix_full_roundtrip(self) -> None: """Full fix roundtrip: A → produce → transfer → B → consume.""" import subprocess with tempfile.TemporaryDirectory(prefix="dir-A-") as dir_a, \ tempfile.TemporaryDirectory(prefix="dir-B-") as dir_b: art_dir_a = os.path.join(dir_a, ".artifacts", "cve-fix", "scope-test") os.makedirs(art_dir_a) # Phase 1: Create phase fixture files in dir A self._create_phase_fixtures(art_dir_a) # Phase 2: Producer — collect portable artifacts portable = unattended.collect_portable_artifacts(art_dir_a) self.assertIn("context.md", portable) self.assertIn("scan-result.json", portable) self.assertIn("patch-log.md", portable) self.assertIn("validation-results.md", portable) # Phase 3: Build evidence with portable artifacts evidence: dict[str, Any] = { "scan_verdict": "fixed", "scan_id": "scan-20261104-abc", "validation_verdict": "PASS", "portable_artifacts": portable, } # Phase 4: Serialize via make-result CLI scope: dict[str, Any] = { "cve_ids": ["CVE-2024-1234"], "repo": "org/repo", "base_branch": "main", } data = { "scope": scope, "stages_completed": [ "start", "scan", "patch", "validate", "pr", ], "stages_remaining": [], "evidence": evidence, "pr": { "url": "https://github.com/org/repo/pull/42", "state": "open", "branch": "cve-fix/EDM-1234", "base": "main", "head_sha": "abc123", }, "target_branches": [], # explicit: no backport targets } proc = subprocess.run( [sys.executable, "-I", str(_SCRIPT), "make-result", "--status", "completed", "--operation", "remediate", "--artifact-dir", art_dir_a, "--data", json.dumps(data)], capture_output=True, text=True, timeout=15, ) self.assertEqual(proc.returncode, 0, proc.stderr) serialized = json.loads(proc.stdout) self.assertEqual(serialized["status"], "completed") self.assertIn( "context.md", serialized["evidence"]["portable_artifacts"], ) # Phase 5: Transfer ONLY JSON wrapper to dir B # (dir A files are NOT available in dir B) prior_result = { "result": serialized, "decision_requests": [], "scan_snapshot": { "scan_id": "scan-20261104-abc", "scope": scope, "candidate_sha": "abc123", }, } # Phase 6: Consumer in dir B — independent preflight canonical_scope = { "cve_ids": ["CVE-2024-1234"], "repo": "org/repo", "base_branch": "main", } current_request = { "operation": "jira_action", "cve_ids": ["CVE-2024-1234"], "jira_action": { "action": "transition", "tickets": ["EDM-100"], "target_status": "MODIFIED", }, } facts = unattended.extract_portable_closure_facts( prior_result, current_request, canonical_scope=canonical_scope, current_candidate_sha="abc123", ) self.assertEqual(facts["status"], "ready") self.assertEqual(facts["closure_basis"], "fix") self.assertEqual(facts["tickets"], ["EDM-100"]) self.assertIn( "https://github.com/org/repo/pull/42", facts["pr_urls"], ) # Portable artifacts available in map self.assertIn("context.md", facts["artifacts_map"]) self.assertIn("scan-result.json", facts["artifacts_map"]) # Evidence facts have actual verdicts self.assertEqual( facts["evidence_facts"]["scan_verdict"], "fixed", ) self.assertEqual( facts["evidence_facts"]["validation_verdict"], "PASS", ) # Phase 7: Verify dir A files are NOT in dir B art_dir_b = os.path.join(dir_b, ".artifacts", "cve-fix", "scope-b") # dir B has no artifact files self.assertFalse(os.path.exists(art_dir_b)) def test_jira_action_transfer_without_phase_files(self) -> None: """jira_action request transfers JSON only; A files unavailable.""" with tempfile.TemporaryDirectory(prefix="dir-A-") as dir_a, \ tempfile.TemporaryDirectory(prefix="dir-B-") as dir_b: art_dir_a = os.path.join(dir_a, "artifacts") os.makedirs(art_dir_a) self._create_phase_fixtures(art_dir_a) # Build serialized result with portable artifacts portable = unattended.collect_portable_artifacts(art_dir_a) result_a = unattended.make_result( status="completed", operation="remediate", scope={ "cve_ids": ["CVE-2024-1234"], "repo": "org/repo", "base_branch": "main", }, pr={ "url": "https://github.com/org/repo/pull/42", "state": "merged", }, stages_completed=[ "start", "scan", "patch", "validate", "pr", ], evidence={ "scan_verdict": "fixed", "validation_verdict": "PASS", "portable_artifacts": portable, }, target_branches=[], # explicit: no backport targets ) # Transfer: only JSON wrapper goes to B prior_result = { "result": result_a, "decision_requests": [], "scan_snapshot": { "scan_id": "scan-1", "scope": { "cve_ids": ["CVE-2024-1234"], "repo": "org/repo", "base_branch": "main", }, "candidate_sha": "abc123", }, } # A's files no longer accessible import shutil shutil.rmtree(art_dir_a) # Consumer in B: extract facts facts = unattended.extract_portable_closure_facts( prior_result, { "operation": "jira_action", "cve_ids": ["CVE-2024-1234"], "jira_action": { "action": "transition", "tickets": ["EDM-100"], "target_status": "MODIFIED", }, }, canonical_scope={ "cve_ids": ["CVE-2024-1234"], "repo": "org/repo", "base_branch": "main", }, current_candidate_sha="abc123", ) self.assertEqual(facts["status"], "ready") self.assertIn("context.md", facts["artifacts_map"]) def test_negative_changed_candidate(self) -> None: """Changed candidate SHA produces non-ready + 0 mutations. Same CVE/repo/base/scope — only the current_candidate_sha differs from the snapshot. Tests the candidate binding, not the scope binding. """ prior = _make_fix_prior_result( target_branches=[], repo="org/repo", ) facts = unattended.extract_portable_closure_facts( prior, { "operation": "jira_action", "cve_ids": ["CVE-2024-1234"], "jira_action": { "action": "transition", "tickets": ["EDM-100"], "target_status": "MODIFIED", }, }, canonical_scope=_FIX_CANONICAL_SCOPE, current_candidate_sha="different-sha-999", ) self.assertNotEqual(facts["status"], "ready") self.assertEqual(facts["status"], "needs_input") self.assertTrue( any("current_candidate_sha" in p and "does not match" in p for p in facts["pending"]), f"Expected candidate mismatch, got: {facts['pending']}", ) def test_negative_missing_canonical_keys(self) -> None: """Missing canonical keys (no cve_ids) blocks correctly.""" prior = _make_fix_prior_result(target_branches=[]) facts = unattended.extract_portable_closure_facts( prior, { "operation": "jira_action", "cve_ids": ["CVE-2024-1234"], "jira_action": { "action": "transition", "tickets": ["EDM-100"], "target_status": "MODIFIED", }, }, canonical_scope={"repo": "org/repo"}, ) self.assertEqual(facts["status"], "needs_input") def test_negative_header_only_report(self) -> None: """Fix with header-only evidence (no verdicts) blocks correctly.""" prior = _make_fix_prior_result(target_branches=[]) prior["result"]["evidence"] = {"scan_id": "s1"} facts = unattended.extract_portable_closure_facts( prior, { "operation": "jira_action", "cve_ids": ["CVE-2024-1234"], "jira_action": { "action": "transition", "tickets": ["EDM-100"], "target_status": "MODIFIED", }, }, canonical_scope=_FIX_CANONICAL_SCOPE, current_candidate_sha=_FIX_CANDIDATE_SHA, ) self.assertEqual(facts["status"], "needs_input") self.assertEqual(facts["closure_basis"], "fix") def test_negative_unknown_basis(self) -> None: """No PR and no VEX produces needs_input, not ready.""" prior = _make_fix_prior_result(target_branches=[]) prior["result"]["pr"] = None facts = unattended.extract_portable_closure_facts( prior, { "operation": "jira_action", "cve_ids": ["CVE-2024-1234"], "jira_action": { "action": "transition", "tickets": ["EDM-100"], "target_status": "MODIFIED", }, }, canonical_scope=_FIX_CANONICAL_SCOPE, current_candidate_sha=_FIX_CANDIDATE_SHA, ) self.assertEqual(facts["status"], "needs_input") self.assertIsNone(facts["closure_basis"]) def test_negative_vex_missing_grant(self) -> None: """VEX without reviewed decision blocks (no PR URL fallback).""" prior = _make_vex_prior_result() facts = unattended.extract_portable_closure_facts( prior, { "operation": "jira_action", "cve_ids": ["CVE-2024-5678"], "jira_action": { "action": "transition", "tickets": ["EDM-300"], "target_status": "Not a Bug", "vex_justification": "component_not_present", }, }, canonical_scope=_VEX_CANONICAL_SCOPE, current_candidate_sha=_VEX_CANDIDATE_SHA, ) self.assertEqual(facts["status"], "needs_human_review") self.assertEqual(facts["closure_basis"], "vex") def test_negative_required_target_gap(self) -> None: """Fix with unknown target_branches (None) blocks.""" prior = _make_fix_prior_result() # target_branches defaults None facts = unattended.extract_portable_closure_facts( prior, { "operation": "jira_action", "cve_ids": ["CVE-2024-1234"], "jira_action": { "action": "transition", "tickets": ["EDM-100"], "target_status": "MODIFIED", }, }, canonical_scope=_FIX_CANONICAL_SCOPE, current_candidate_sha=_FIX_CANDIDATE_SHA, ) self.assertEqual(facts["status"], "needs_input") # --------------------------------------------------------------------------- # Control 1: current_candidate_sha binding # --------------------------------------------------------------------------- class TestCandidateShaBinding(unittest.TestCase): """extract_portable_closure_facts: current_candidate_sha controls.""" def test_missing_candidate_sha_returns_needs_input(self) -> None: """Omitting current_candidate_sha blocks with needs_input.""" prior = _make_fix_prior_result(target_branches=[]) request = { "operation": "jira_action", "cve_ids": ["CVE-2024-1234"], "jira_action": { "action": "transition", "tickets": ["EDM-100"], "target_status": "MODIFIED", }, } facts = unattended.extract_portable_closure_facts( prior, request, canonical_scope=_FIX_CANONICAL_SCOPE, # current_candidate_sha intentionally omitted ) self.assertEqual(facts["status"], "needs_input") self.assertTrue( any("current_candidate_sha" in p for p in facts["pending"]), f"Expected candidate_sha error, got: {facts['pending']}", ) def test_wrong_type_candidate_sha_returns_needs_input(self) -> None: """Non-string current_candidate_sha blocks.""" prior = _make_fix_prior_result(target_branches=[]) request = { "operation": "jira_action", "cve_ids": ["CVE-2024-1234"], "jira_action": { "action": "transition", "tickets": ["EDM-100"], "target_status": "MODIFIED", }, } facts = unattended.extract_portable_closure_facts( prior, request, canonical_scope=_FIX_CANONICAL_SCOPE, current_candidate_sha=12345, # type: ignore[arg-type] ) self.assertEqual(facts["status"], "needs_input") self.assertTrue( any("current_candidate_sha" in p for p in facts["pending"]), ) def test_blank_candidate_sha_returns_needs_input(self) -> None: """Blank current_candidate_sha blocks.""" prior = _make_fix_prior_result(target_branches=[]) request = { "operation": "jira_action", "cve_ids": ["CVE-2024-1234"], "jira_action": { "action": "transition", "tickets": ["EDM-100"], "target_status": "MODIFIED", }, } facts = unattended.extract_portable_closure_facts( prior, request, canonical_scope=_FIX_CANONICAL_SCOPE, current_candidate_sha=" ", ) self.assertEqual(facts["status"], "needs_input") def test_mismatched_sha_same_scope_blocks(self) -> None: """Different candidate SHA with same CVE/repo/base blocks.""" prior = _make_fix_prior_result(target_branches=[]) request = { "operation": "jira_action", "cve_ids": ["CVE-2024-1234"], "jira_action": { "action": "transition", "tickets": ["EDM-100"], "target_status": "MODIFIED", }, } facts = unattended.extract_portable_closure_facts( prior, request, canonical_scope=_FIX_CANONICAL_SCOPE, current_candidate_sha="completely-different-sha", ) self.assertEqual(facts["status"], "needs_input") self.assertTrue( any("current_candidate_sha" in p and "does not match" in p for p in facts["pending"]), ) def test_canonical_scope_missing_repo_blocks(self) -> None: """canonical_scope without repo returns needs_input.""" prior = _make_fix_prior_result(target_branches=[]) request = { "operation": "jira_action", "cve_ids": ["CVE-2024-1234"], "jira_action": { "action": "transition", "tickets": ["EDM-100"], "target_status": "MODIFIED", }, } facts = unattended.extract_portable_closure_facts( prior, request, canonical_scope={ "cve_ids": ["CVE-2024-1234"], "base_branch": "main", }, current_candidate_sha=_FIX_CANDIDATE_SHA, ) self.assertEqual(facts["status"], "needs_input") self.assertTrue( any("repo" in p for p in facts["pending"]), ) def test_canonical_scope_missing_base_branch_blocks(self) -> None: """canonical_scope without base_branch returns needs_input.""" prior = _make_fix_prior_result(target_branches=[]) request = { "operation": "jira_action", "cve_ids": ["CVE-2024-1234"], "jira_action": { "action": "transition", "tickets": ["EDM-100"], "target_status": "MODIFIED", }, } facts = unattended.extract_portable_closure_facts( prior, request, canonical_scope={ "cve_ids": ["CVE-2024-1234"], "repo": "org/repo", }, current_candidate_sha=_FIX_CANDIDATE_SHA, ) self.assertEqual(facts["status"], "needs_input") self.assertTrue( any("base_branch" in p for p in facts["pending"]), ) # --------------------------------------------------------------------------- # Control 2: fix material availability (not verdict-only) # --------------------------------------------------------------------------- class TestFixMaterialAvailability(unittest.TestCase): """Fix closure requires actual preserved material, not just verdicts.""" def test_verdicts_plus_header_only_report_blocks(self) -> None: """scan_verdict + validation_verdict + header-only report → blocked. Header-only validation report (table header + PASS/fixed labels but no ticket rows) does not satisfy material requirement even with truthy verdict strings. """ prior = _make_fix_prior_result(target_branches=[]) # Override portable_artifacts with header-only report prior["result"]["evidence"]["portable_artifacts"] = { "validation-results.md": ( "# Validation Results\n\n" "| Check | Status |\n|-------|--------|\n" "| Unit tests | PASS |\n| Scan | fixed |\n" ), } request = { "operation": "jira_action", "cve_ids": ["CVE-2024-1234"], "jira_action": { "action": "transition", "tickets": ["EDM-100"], "target_status": "MODIFIED", }, } facts = unattended.extract_portable_closure_facts( prior, request, canonical_scope=_FIX_CANONICAL_SCOPE, current_candidate_sha=_FIX_CANDIDATE_SHA, ) self.assertEqual(facts["status"], "needs_input") self.assertEqual(facts["closure_basis"], "fix") self.assertTrue( any("header-only" in p or "ticket" in p.lower() for p in facts["pending"]), f"Expected material/ticket error, got: {facts['pending']}", ) def test_real_validation_report_with_tickets_passes(self) -> None: """Real validation report with parseable ticket rows → ready. Demonstrates that close.py's gather_ticket_keys can parse the validation report and find named ticket data. """ prior = _make_fix_prior_result(target_branches=[]) # _make_fix_prior_result already includes _REAL_VALIDATION_REPORT request = { "operation": "jira_action", "cve_ids": ["CVE-2024-1234"], "jira_action": { "action": "transition", "tickets": ["EDM-100"], "target_status": "MODIFIED", }, } facts = unattended.extract_portable_closure_facts( prior, request, canonical_scope=_FIX_CANONICAL_SCOPE, current_candidate_sha=_FIX_CANDIDATE_SHA, ) self.assertEqual(facts["status"], "ready") self.assertEqual(facts["closure_basis"], "fix") self.assertEqual(facts["pending"], []) def test_no_portable_artifacts_blocks(self) -> None: """Fix with verdicts but no portable_artifacts → blocked.""" prior = _make_fix_prior_result(target_branches=[]) # Remove portable_artifacts from evidence del prior["result"]["evidence"]["portable_artifacts"] request = { "operation": "jira_action", "cve_ids": ["CVE-2024-1234"], "jira_action": { "action": "transition", "tickets": ["EDM-100"], "target_status": "MODIFIED", }, } facts = unattended.extract_portable_closure_facts( prior, request, canonical_scope=_FIX_CANONICAL_SCOPE, current_candidate_sha=_FIX_CANDIDATE_SHA, ) self.assertEqual(facts["status"], "needs_input") self.assertEqual(facts["closure_basis"], "fix") self.assertTrue( any("portable_artifacts" in p for p in facts["pending"]), ) def test_vex_closure_does_not_require_fix_report(self) -> None: """VEX closure does not require validation report material.""" prior = _make_vex_prior_result() decision = _make_vex_human_decision() request = { "operation": "jira_action", "cve_ids": ["CVE-2024-5678"], "jira_action": { "action": "transition", "tickets": ["EDM-300"], "target_status": "Not a Bug", "vex_justification": "component_not_present", }, "human_decisions": [decision], "reviewer_policy": _DEFAULT_REVIEWER_POLICY, } facts = unattended.extract_portable_closure_facts( prior, request, canonical_scope=_VEX_CANONICAL_SCOPE, current_candidate_sha=_VEX_CANDIDATE_SHA, ) self.assertEqual(facts["status"], "ready") self.assertEqual(facts["closure_basis"], "vex") # --------------------------------------------------------------------------- # Round 16C: portable_closure_entry, ticket coverage, VEX material, Step 9 # --------------------------------------------------------------------------- class TestPortableClosureEntry(unittest.TestCase): """portable_closure_entry: pure helper returns correct status/result_data.""" def test_missing_canonical_scope_returns_needs_input(self) -> None: """Missing canonical_scope → needs_input with result_data BEFORE I/O.""" prior = _make_fix_prior_result(target_branches=[]) request: dict[str, Any] = { "operation": "jira_action", "cve_ids": ["CVE-2024-1234"], "jira_action": { "action": "transition", "tickets": ["EDM-100"], "target_status": "MODIFIED", }, "prior_result": prior, } scope: dict[str, Any] = { "cve_ids": ["CVE-2024-1234"], "repo": "org/repo", "base_branch": "main", } result = unattended.portable_closure_entry( request=request, operation="jira_action", scope=scope, canonical_scope=None, current_candidate_sha=_FIX_CANDIDATE_SHA, ) self.assertEqual(result["status"], "needs_input") self.assertIsInstance(result["result_data"], dict) self.assertIn("scope", result["result_data"]) self.assertIn("error", result["result_data"]) self.assertIsNone(result["closure_basis"]) self.assertEqual(result["decision_requests"], []) def test_missing_candidate_sha_returns_needs_input(self) -> None: """Missing current_candidate_sha → needs_input with result_data.""" prior = _make_fix_prior_result(target_branches=[]) request: dict[str, Any] = { "operation": "jira_action", "cve_ids": ["CVE-2024-1234"], "jira_action": { "action": "transition", "tickets": ["EDM-100"], "target_status": "MODIFIED", }, "prior_result": prior, } scope = dict(_FIX_CANONICAL_SCOPE) result = unattended.portable_closure_entry( request=request, operation="jira_action", scope=scope, canonical_scope=_FIX_CANONICAL_SCOPE, current_candidate_sha=None, ) self.assertEqual(result["status"], "needs_input") self.assertIsInstance(result["result_data"], dict) def test_missing_prior_result_returns_needs_input(self) -> None: """Missing prior_result in request → needs_input.""" request: dict[str, Any] = { "operation": "jira_action", "cve_ids": ["CVE-2024-1234"], "jira_action": { "action": "transition", "tickets": ["EDM-100"], "target_status": "MODIFIED", }, } scope = dict(_FIX_CANONICAL_SCOPE) result = unattended.portable_closure_entry( request=request, operation="jira_action", scope=scope, canonical_scope=_FIX_CANONICAL_SCOPE, current_candidate_sha=_FIX_CANDIDATE_SHA, ) self.assertEqual(result["status"], "needs_input") self.assertIsInstance(result["result_data"], dict) self.assertTrue( any("prior_result" in str(v) for v in result["result_data"].values()), ) def test_ready_fix_returns_facts(self) -> None: """Ready fix closure returns status=ready with facts for live checks.""" prior = _make_fix_prior_result(target_branches=[]) request: dict[str, Any] = { "operation": "jira_action", "cve_ids": ["CVE-2024-1234"], "jira_action": { "action": "transition", "tickets": ["EDM-100"], "target_status": "MODIFIED", }, "prior_result": prior, } scope = dict(_FIX_CANONICAL_SCOPE) result = unattended.portable_closure_entry( request=request, operation="jira_action", scope=scope, canonical_scope=_FIX_CANONICAL_SCOPE, current_candidate_sha=_FIX_CANDIDATE_SHA, ) self.assertEqual(result["status"], "ready") self.assertIsNone(result["result_data"]) self.assertEqual(result["closure_basis"], "fix") self.assertIn("scan_id", result["evidence_facts"]) self.assertTrue(len(result["pr_urls"]) > 0) self.assertEqual(result["tickets"], ["EDM-100"]) def test_vex_needs_human_review_preserves_out(self) -> None: """VEX without decision → needs_human_review with decision_requests.""" prior = _make_vex_prior_result() request: dict[str, Any] = { "operation": "jira_action", "cve_ids": ["CVE-2024-5678"], "jira_action": { "action": "transition", "tickets": ["EDM-300"], "target_status": "Not a Bug", "vex_justification": "component_not_present", }, "prior_result": prior, "reviewer_policy": _DEFAULT_REVIEWER_POLICY, } scope = dict(_VEX_CANONICAL_SCOPE) result = unattended.portable_closure_entry( request=request, operation="jira_action", scope=scope, canonical_scope=_VEX_CANONICAL_SCOPE, current_candidate_sha=_VEX_CANDIDATE_SHA, ) self.assertEqual(result["status"], "needs_human_review") self.assertIsInstance(result["result_data"], dict) self.assertEqual(result["closure_basis"], "vex") # Original OUT decision_requests preserved self.assertIsInstance(result["decision_requests"], list) self.assertTrue(len(result["decision_requests"]) > 0) # result_data suitable for Step 9 writer self.assertIn("scope", result["result_data"]) class TestCurrentTicketCoverage(unittest.TestCase): """Current tickets must be covered by parsed validation mapping.""" def test_unrelated_ticket_cannot_authorize_current(self) -> None: """EDM-999 in validation report cannot make EDM-100 available. Prior validation report has EDM-999 as the in-scope ticket. Current request asks to close EDM-100. EDM-100 is NOT in the parsed keys — must block even though _parsed_keys is nonempty. """ prior = _make_fix_prior_result(target_branches=[]) # Override portable_artifacts with report containing EDM-999 only prior["result"]["evidence"]["portable_artifacts"] = { "validation-results.md": ( "# Validation Results\n\n" "| Ticket | Explicitly in scope? | Status |\n" "|--------|---------------------|--------|\n" "| EDM-999 | Yes | PASS |\n" ), } request: dict[str, Any] = { "operation": "jira_action", "cve_ids": ["CVE-2024-1234"], "jira_action": { "action": "transition", "tickets": ["EDM-100"], "target_status": "MODIFIED", }, } facts = unattended.extract_portable_closure_facts( prior, request, canonical_scope=_FIX_CANONICAL_SCOPE, current_candidate_sha=_FIX_CANDIDATE_SHA, ) self.assertEqual(facts["status"], "needs_input") self.assertEqual(facts["closure_basis"], "fix") self.assertTrue( any("EDM-100" in p for p in facts["pending"]), f"Expected EDM-100 coverage error, got: {facts['pending']}", ) def test_matching_ticket_passes_coverage(self) -> None: """Current ticket EDM-100 in validation report passes coverage.""" prior = _make_fix_prior_result(target_branches=[]) request: dict[str, Any] = { "operation": "jira_action", "cve_ids": ["CVE-2024-1234"], "jira_action": { "action": "transition", "tickets": ["EDM-100"], "target_status": "MODIFIED", }, } facts = unattended.extract_portable_closure_facts( prior, request, canonical_scope=_FIX_CANONICAL_SCOPE, current_candidate_sha=_FIX_CANDIDATE_SHA, ) self.assertEqual(facts["status"], "ready") def test_partial_coverage_blocks(self) -> None: """Request has EDM-100 + EDM-200, report has only EDM-100 → blocks.""" prior = _make_fix_prior_result(target_branches=[]) request: dict[str, Any] = { "operation": "jira_action", "cve_ids": ["CVE-2024-1234"], "jira_action": { "action": "transition", "tickets": ["EDM-100", "EDM-200"], "target_status": "MODIFIED", }, } facts = unattended.extract_portable_closure_facts( prior, request, canonical_scope=_FIX_CANONICAL_SCOPE, current_candidate_sha=_FIX_CANDIDATE_SHA, ) self.assertEqual(facts["status"], "needs_input") self.assertTrue( any("EDM-200" in p for p in facts["pending"]), ) class TestVexMaterialEquivalence(unittest.TestCase): """VEX closure requires scan classification, not just 3 binding metadata.""" def test_metadata_only_vex_blocks(self) -> None: """VEX prior with scan_id + candidate_sha but NO scan_verdict → blocks. A metadata-only snapshot (just binding IDs without actual scan classification) plus a valid reviewed decision must return nonready — proving that 3 metadata keys alone are insufficient. """ prior = _make_vex_prior_result() # Remove scan_verdict — leave only scan_id + candidate_sha del prior["result"]["evidence"]["scan_verdict"] decision = _make_vex_human_decision() request: dict[str, Any] = { "operation": "jira_action", "cve_ids": ["CVE-2024-5678"], "jira_action": { "action": "transition", "tickets": ["EDM-300"], "target_status": "Not a Bug", "vex_justification": "component_not_present", }, "human_decisions": [decision], "reviewer_policy": _DEFAULT_REVIEWER_POLICY, } facts = unattended.extract_portable_closure_facts( prior, request, canonical_scope=_VEX_CANONICAL_SCOPE, current_candidate_sha=_VEX_CANDIDATE_SHA, ) self.assertEqual(facts["status"], "needs_input") self.assertEqual(facts["closure_basis"], "vex") self.assertTrue( any("scan_verdict" in p for p in facts["pending"]), f"Expected scan_verdict error, got: {facts['pending']}", ) def test_real_scan_plus_decision_passes(self) -> None: """Real scan_verdict + valid reviewed decision → ready with full facts. Verifies that vex_facts include scan_verdict and original OUT evidence, not just 3 binding metadata. """ prior = _make_vex_prior_result() decision = _make_vex_human_decision() request: dict[str, Any] = { "operation": "jira_action", "cve_ids": ["CVE-2024-5678"], "jira_action": { "action": "transition", "tickets": ["EDM-300"], "target_status": "Not a Bug", "vex_justification": "component_not_present", }, "human_decisions": [decision], "reviewer_policy": _DEFAULT_REVIEWER_POLICY, } facts = unattended.extract_portable_closure_facts( prior, request, canonical_scope=_VEX_CANONICAL_SCOPE, current_candidate_sha=_VEX_CANDIDATE_SHA, ) self.assertEqual(facts["status"], "ready") self.assertEqual(facts["closure_basis"], "vex") # VEX facts must carry actual scan classification vf = facts["vex_facts"] self.assertEqual(vf["scan_verdict"], "not_affected") # VEX facts must carry original OUT evidence/scope self.assertIn("original_out_evidence", vf) self.assertIn("original_out_scope", vf) self.assertIsInstance(vf["original_out_scope"], dict) # Core fields still present self.assertEqual(vf["justification"], "component_not_present") self.assertEqual(vf["reviewer"], "jsmith") self.assertTrue(len(vf["evidence"]) > 0) def test_blank_scan_verdict_blocks_vex(self) -> None: """Blank string scan_verdict is metadata-only → blocks VEX closure.""" prior = _make_vex_prior_result() prior["result"]["evidence"]["scan_verdict"] = "" decision = _make_vex_human_decision() request: dict[str, Any] = { "operation": "jira_action", "cve_ids": ["CVE-2024-5678"], "jira_action": { "action": "transition", "tickets": ["EDM-300"], "target_status": "Not a Bug", "vex_justification": "component_not_present", }, "human_decisions": [decision], "reviewer_policy": _DEFAULT_REVIEWER_POLICY, } facts = unattended.extract_portable_closure_facts( prior, request, canonical_scope=_VEX_CANONICAL_SCOPE, current_candidate_sha=_VEX_CANDIDATE_SHA, ) self.assertEqual(facts["status"], "needs_input") self.assertEqual(facts["closure_basis"], "vex") class TestStep9StoppedClosureHandoff(unittest.TestCase): """Step 9 stopped closure: same status/OUT/evidence to common writer.""" def test_stopped_needs_human_review_result_data(self) -> None: """Stopped VEX closure → needs_human_review result_data for Step 9. Proves the returned status/result_data/decision_requests can be passed directly to the Step 9 make-result writer — same structure as any other phase handoff. """ prior = _make_vex_prior_result() request: dict[str, Any] = { "operation": "jira_action", "cve_ids": ["CVE-2024-5678"], "jira_action": { "action": "transition", "tickets": ["EDM-300"], "target_status": "Not a Bug", "vex_justification": "component_not_present", }, "prior_result": prior, "reviewer_policy": _DEFAULT_REVIEWER_POLICY, } scope = dict(_VEX_CANONICAL_SCOPE) closure = unattended.portable_closure_entry( request=request, operation="jira_action", scope=scope, canonical_scope=_VEX_CANONICAL_SCOPE, current_candidate_sha=_VEX_CANDIDATE_SHA, ) self.assertEqual(closure["status"], "needs_human_review") # result_data is suitable for Step 9 make-result --data rd = closure["result_data"] self.assertIsInstance(rd, dict) self.assertIn("scope", rd) self.assertIn("error", rd) # Scope from Step 9 writer matches self.assertEqual(rd["scope"], scope) # decision_requests preserved for OUT handoff dreqs = closure["decision_requests"] self.assertIsInstance(dreqs, list) self.assertTrue(len(dreqs) > 0) self.assertIn("decision_id", dreqs[0]) # Verify the result_data can round-trip through make_result # by validating it constructs a valid result dict rebuilt = unattended.make_result( status="needs_human_review", operation="jira_action", scope=scope, error=rd.get("error", ""), stages_completed=rd.get("stages_completed", []), stages_remaining=rd.get("stages_remaining", []), ) self.assertEqual(rebuilt["status"], "needs_human_review") self.assertEqual(rebuilt["operation"], "jira_action") def test_stopped_needs_input_result_data(self) -> None: """Stopped fix closure → needs_input result_data for Step 9. Proves that non-ready fix closure (missing material) produces result_data with error/blockers that the Step 9 writer can persist without collecting nonexistent local files. """ prior = _make_fix_prior_result(target_branches=[]) # Remove portable_artifacts to trigger material block del prior["result"]["evidence"]["portable_artifacts"] request: dict[str, Any] = { "operation": "jira_action", "cve_ids": ["CVE-2024-1234"], "jira_action": { "action": "transition", "tickets": ["EDM-100"], "target_status": "MODIFIED", }, "prior_result": prior, } scope = dict(_FIX_CANONICAL_SCOPE) closure = unattended.portable_closure_entry( request=request, operation="jira_action", scope=scope, canonical_scope=_FIX_CANONICAL_SCOPE, current_candidate_sha=_FIX_CANDIDATE_SHA, ) self.assertEqual(closure["status"], "needs_input") rd = closure["result_data"] self.assertIsInstance(rd, dict) self.assertIn("error", rd) self.assertIn("scope", rd) # No subprocess dispatch in the helper self.assertNotIn("returncode", rd) # --------------------------------------------------------------------------- # Candidate bac32e0b: validate_prior_result backport child contract # --------------------------------------------------------------------------- class TestValidatePriorResultBackportChildren(unittest.TestCase): """validate_prior_result: completed/merged backport children must have pr.url and evidence (same contract as make_result). Non-completed children and absent backport_results accepted. """ def _valid_prior( self, backport_results: list[dict[str, Any]] | None = None, ) -> dict[str, Any]: result: dict[str, Any] = { "status": "completed", "operation": "remediate", "scope": {"cve_ids": ["CVE-2024-1"]}, } if backport_results is not None: result["backport_results"] = backport_results return { "result": result, "decision_requests": [], "scan_snapshot": { "scan_id": "s1", "scope": {"cve_ids": ["CVE-2024-1"]}, "candidate_sha": "abc", }, } def test_no_backport_results_accepted(self) -> None: """Absent backport_results is fine (no backport operation).""" errs = unattended.validate_prior_result(self._valid_prior(), {}) self.assertEqual(errs, []) def test_empty_backport_results_accepted(self) -> None: errs = unattended.validate_prior_result( self._valid_prior(backport_results=[]), {}, ) self.assertEqual(errs, []) def test_partial_child_accepted(self) -> None: """Non-completed child (partial) does not need pr.url/evidence.""" errs = unattended.validate_prior_result( self._valid_prior(backport_results=[ {"target_branch": "rel-1", "status": "partial"}, ]), {}, ) self.assertEqual(errs, []) def test_completed_child_missing_pr_rejected(self) -> None: errs = unattended.validate_prior_result( self._valid_prior(backport_results=[ {"target_branch": "rel-1", "status": "completed", "evidence": {"test": "ok"}}, ]), {}, ) self.assertTrue(any("pr.url" in e for e in errs)) def test_merged_child_missing_evidence_rejected(self) -> None: errs = unattended.validate_prior_result( self._valid_prior(backport_results=[ {"target_branch": "rel-1", "status": "merged", "pr": {"url": "https://github.com/o/r/pull/1"}}, ]), {}, ) self.assertTrue(any("evidence" in e for e in errs)) def test_non_dict_child_rejected(self) -> None: errs = unattended.validate_prior_result( self._valid_prior(backport_results=["not-a-dict"]), {}, ) self.assertTrue(any("must be a dict" in e for e in errs)) def test_valid_completed_child_accepted(self) -> None: errs = unattended.validate_prior_result( self._valid_prior(backport_results=[ {"target_branch": "rel-1", "status": "completed", "pr": {"url": "https://github.com/o/r/pull/1"}, "evidence": {"test": "PASS"}}, ]), {}, ) self.assertEqual(errs, []) # --------------------------------------------------------------------------- # Candidate 2788682c: assemble_result_data target_branches for remediate # --------------------------------------------------------------------------- class TestAssemblerRemediateTargetBranches(unittest.TestCase): """assemble_result_data: remediate carries target_branches including [].""" def test_remediate_empty_list_preserved(self) -> None: """Explicit [] for remediate must appear in result data.""" req = { "operation": "remediate", "cve_ids": ["CVE-2024-1"], "target_branches": [], } data = unattended.assemble_result_data( request=req, status="completed", scope={"cve_ids": ["CVE-2024-1"]}, stages_completed=["start", "scan", "patch", "validate", "pr"], stages_remaining=[], evidence={"validation_verdict": "PASS"}, ) self.assertIn("target_branches", data) self.assertEqual(data["target_branches"], []) def test_remediate_nonempty_list_preserved(self) -> None: req = { "operation": "remediate", "cve_ids": ["CVE-2024-1"], "target_branches": ["release-2.16"], } data = unattended.assemble_result_data( request=req, status="completed", scope={"cve_ids": ["CVE-2024-1"]}, stages_completed=["start", "scan", "patch", "validate", "pr"], stages_remaining=[], evidence={"validation_verdict": "PASS"}, ) self.assertEqual(data["target_branches"], ["release-2.16"]) def test_remediate_absent_not_injected(self) -> None: """Absent target_branches for remediate stays absent.""" req = { "operation": "remediate", "cve_ids": ["CVE-2024-1"], } data = unattended.assemble_result_data( request=req, status="completed", scope={"cve_ids": ["CVE-2024-1"]}, stages_completed=["start", "scan", "patch", "validate", "pr"], stages_remaining=[], evidence={"validation_verdict": "PASS"}, ) self.assertNotIn("target_branches", data) # --------------------------------------------------------------------------- # Candidate da282451: portable FIX eligibility PASS allowlist # --------------------------------------------------------------------------- class TestPortableFixEligibilityPassAllowlist(unittest.TestCase): """extract_portable_closure_facts: validation_verdict must be a recognized PASS from the allowlist, not any truthy string. """ def _make_prior(self, validation_verdict: Any) -> dict[str, Any]: return _make_fix_prior_result( target_branches=[], evidence_extra={ "validation_verdict": validation_verdict, }, ) def _make_request(self) -> dict[str, Any]: return { "operation": "jira_action", "cve_ids": ["CVE-2024-1234"], "jira_action": { "action": "transition", "tickets": ["EDM-100"], "target_status": "MODIFIED", }, } def test_pass_verdict_accepted(self) -> None: prior = self._make_prior("PASS") facts = unattended.extract_portable_closure_facts( prior, self._make_request(), canonical_scope=_FIX_CANONICAL_SCOPE, current_candidate_sha=_FIX_CANDIDATE_SHA, ) self.assertEqual(facts["status"], "ready") def test_fixed_verdict_rejected(self) -> None: """Scanner verdict 'FIXED' is not a validation outcome.""" prior = self._make_prior("FIXED") facts = unattended.extract_portable_closure_facts( prior, self._make_request(), canonical_scope=_FIX_CANONICAL_SCOPE, current_candidate_sha=_FIX_CANDIDATE_SHA, ) self.assertNotEqual(facts["status"], "ready") self.assertTrue( any("FIXED" in p for p in facts.get("pending", [])), ) def test_not_affected_verdict_rejected(self) -> None: prior = self._make_prior("NOT_AFFECTED") facts = unattended.extract_portable_closure_facts( prior, self._make_request(), canonical_scope=_FIX_CANONICAL_SCOPE, current_candidate_sha=_FIX_CANDIDATE_SHA, ) self.assertNotEqual(facts["status"], "ready") def test_blank_verdict_rejected(self) -> None: prior = self._make_prior(" ") facts = unattended.extract_portable_closure_facts( prior, self._make_request(), canonical_scope=_FIX_CANONICAL_SCOPE, current_candidate_sha=_FIX_CANDIDATE_SHA, ) self.assertNotEqual(facts["status"], "ready") # --------------------------------------------------------------------------- # Candidate 8629ce81: stopped portable_closure_entry passive refs # --------------------------------------------------------------------------- class TestStoppedClosureEntryPassiveRefs(unittest.TestCase): """portable_closure_entry non-ready preserves source PR, targets, and backport-child refs from the prior result as passive metadata. """ def test_needs_input_retains_pr(self) -> None: """Stopped handoff includes source PR from prior result.""" prior = _make_fix_prior_result(target_branches=[]) request = { "operation": "jira_action", "cve_ids": ["CVE-2024-1234"], "jira_action": { "action": "transition", "tickets": ["EDM-100"], "target_status": "MODIFIED", }, "prior_result": prior, } # Trigger needs_input via mismatched candidate SHA entry = unattended.portable_closure_entry( request=request, operation="jira_action", scope=dict(_FIX_CANONICAL_SCOPE), canonical_scope=_FIX_CANONICAL_SCOPE, current_candidate_sha="different_sha_mismatch", ) self.assertEqual(entry["status"], "needs_input") rd = entry["result_data"] self.assertIn("pr", rd) self.assertEqual( rd["pr"]["url"], "https://github.com/org/repo/pull/42", ) def test_needs_input_retains_target_branches(self) -> None: prior = _make_fix_prior_result( target_branches=["release-2.16"], backport_results=[{ "target_branch": "release-2.16", "status": "merged", "pr": {"url": "https://github.com/org/repo/pull/43"}, "evidence": {"cherry_pick": "clean"}, }], ) request = { "operation": "jira_action", "cve_ids": ["CVE-2024-1234"], "jira_action": { "action": "transition", "tickets": ["EDM-100"], "target_status": "MODIFIED", }, "prior_result": prior, } entry = unattended.portable_closure_entry( request=request, operation="jira_action", scope=dict(_FIX_CANONICAL_SCOPE), canonical_scope=_FIX_CANONICAL_SCOPE, current_candidate_sha="different_sha_mismatch", ) self.assertEqual(entry["status"], "needs_input") rd = entry["result_data"] self.assertEqual(rd.get("target_branches"), ["release-2.16"]) self.assertIsNotNone(rd.get("backport_results")) # --------------------------------------------------------------------------- # Candidate 2f90115b: target coverage before data-ready # --------------------------------------------------------------------------- class TestFixClosureTargetCoverage(unittest.TestCase): """extract_portable_closure_facts: fix closure requires per-target child coverage when required_targets is non-empty. """ def _make_request(self) -> dict[str, Any]: return { "operation": "jira_action", "cve_ids": ["CVE-2024-1234"], "jira_action": { "action": "transition", "tickets": ["EDM-100"], "target_status": "MODIFIED", }, } def test_empty_targets_no_check(self) -> None: """target_branches=[] means no backport needed → ready.""" prior = _make_fix_prior_result(target_branches=[]) facts = unattended.extract_portable_closure_facts( prior, self._make_request(), canonical_scope=_FIX_CANONICAL_SCOPE, current_candidate_sha=_FIX_CANDIDATE_SHA, ) self.assertEqual(facts["status"], "ready") def test_missing_child_blocks(self) -> None: """target_branches=['rel-1'] but no child → needs_input.""" prior = _make_fix_prior_result( target_branches=["release-2.16"], backport_results=[], ) facts = unattended.extract_portable_closure_facts( prior, self._make_request(), canonical_scope=_FIX_CANONICAL_SCOPE, current_candidate_sha=_FIX_CANDIDATE_SHA, ) self.assertNotEqual(facts["status"], "ready") self.assertTrue( any("release-2.16" in p for p in facts.get("pending", [])), ) def test_non_completed_child_blocks(self) -> None: """Child with status 'partial' for a required target blocks.""" prior = _make_fix_prior_result( target_branches=["release-2.16"], backport_results=[{ "target_branch": "release-2.16", "status": "partial", "pr": {"url": "https://github.com/o/r/pull/43"}, }], ) facts = unattended.extract_portable_closure_facts( prior, self._make_request(), canonical_scope=_FIX_CANONICAL_SCOPE, current_candidate_sha=_FIX_CANDIDATE_SHA, ) self.assertNotEqual(facts["status"], "ready") def test_matching_merged_child_ready(self) -> None: """Each required target has a merged child → ready.""" _vr = ( "| Ticket | Explicitly in scope? | Status |\n" "|--------|---------------------|--------|\n" "| EDM-100 | Yes | PASS |\n" ) prior = _make_fix_prior_result( target_branches=["release-2.16"], backport_results=[{ "target_branch": "release-2.16", "status": "merged", "pr": {"url": "https://github.com/org/repo/pull/43"}, "evidence": {"cherry_pick": "clean"}, }], evidence_extra={ "portable_artifacts": { "context.md": _REAL_CONTEXT_MD, "validation-results.md": _vr, }, }, ) facts = unattended.extract_portable_closure_facts( prior, self._make_request(), canonical_scope=_FIX_CANONICAL_SCOPE, current_candidate_sha=_FIX_CANDIDATE_SHA, ) self.assertEqual(facts["status"], "ready") # --------------------------------------------------------------------------- # Candidate 259c8f56: history deduplication in _cmd_make_result # --------------------------------------------------------------------------- class TestMakeResultHistoryDedup(unittest.TestCase): """_cmd_make_result three-write history: dedup, distinct metadata, stable full snapshot retention. Three-invocation pattern exercises the actual dedup race: Write A: creates result A on disk (no prior history). Write B: creates result B; disk has A → prior_operations=[snapshot(A)]. Write C: incoming_history=[snapshot(A)] (caller forwarding A's result) + disk has B with prior_operations=[snapshot(A)] → snapshot(A) must appear exactly once, snapshot(B) once, both with distinct metadata/timestamps retained. """ def test_three_write_dedup_and_distinct_metadata(self) -> None: """Write A->B->C: history has snapshot(A) once, snapshot(B) once, with independently distinct metadata fields. Three-invocation pattern exercises the actual dedup race: A: completed remediate (first write) B: partial backport (disk has A -> history=[snapshot(A)]) C: needs_input jira_action with incoming=[snapshot(A)] + disk has B with history=[snapshot(A)] -> snapshot(A) exactly once, snapshot(B) once. Uses distinct operation/status/scope metadata as stable identity markers (not timestamps, which have second resolution). """ with tempfile.TemporaryDirectory() as tmpdir: result_path = Path(tmpdir) / "unattended-result.json" # --- Write A: initial completed remediate --- args_a = argparse.Namespace( status="completed", operation="remediate", artifact_dir=tmpdir, data=json.dumps({ "scope": {"cve_ids": ["CVE-2024-1"], "write_marker": "write-A"}, "stages_completed": ["start", "scan", "patch", "validate", "pr"], "stages_remaining": [], "evidence": {"validation_verdict": "PASS"}, }), ) with unittest.mock.patch("sys.stdout", new_callable=io.StringIO): rc_a = unattended._cmd_make_result(args_a) self.assertEqual(rc_a, 0, "Write A must succeed") result_a = json.loads(result_path.read_text()) # A has no meaningful prior_operations (first write) po_a = result_a.get("prior_operations") self.assertFalse( po_a, "First write must have empty/no prior_operations", ) # --- Write B: partial backport (disk has A) --- args_b = argparse.Namespace( status="partial", operation="backport", artifact_dir=tmpdir, data=json.dumps({ "scope": {"cve_ids": ["CVE-2024-1"], "write_marker": "write-B"}, "stages_completed": ["backport"], "stages_remaining": [], "evidence": {}, }), ) with unittest.mock.patch("sys.stdout", new_callable=io.StringIO): rc_b = unattended._cmd_make_result(args_b) self.assertEqual(rc_b, 0, "Write B must succeed") result_b = json.loads(result_path.read_text()) # B's prior_operations must contain snapshot(A) po_b = result_b.get("prior_operations", []) self.assertEqual(len(po_b), 1, "After B, history must have exactly 1 entry (A)") self.assertEqual(po_b[0]["status"], "completed") self.assertEqual(po_b[0]["operation"], "remediate") self.assertEqual( po_b[0]["scope"]["write_marker"], "write-A", "History entry must be snapshot(A)", ) # --- Write C: caller forwards snapshot(A) as incoming --- # This simulates the portable handoff race: the caller # received result A and includes snapshot(A) in incoming # prior_operations. Disk has result B which also contains # snapshot(A). Dedup must keep snapshot(A) exactly once. snapshot_a = { k: v for k, v in result_a.items() if k != "prior_operations" } args_c = argparse.Namespace( status="needs_input", operation="jira_action", artifact_dir=tmpdir, data=json.dumps({ "scope": {"cve_ids": ["CVE-2024-1"], "write_marker": "write-C"}, "stages_completed": [], "stages_remaining": [], "evidence": {}, "prior_operations": [snapshot_a], }), ) captured_c = io.StringIO() with unittest.mock.patch("sys.stdout", captured_c): rc_c = unattended._cmd_make_result(args_c) self.assertEqual(rc_c, 0, "Write C must succeed") result_c = json.loads(result_path.read_text()) po_c = result_c.get("prior_operations", []) # Snapshot(A) must appear exactly once -- identified by # operation + write_marker (stable metadata) matching_a = [ e for e in po_c if e.get("status") == "completed" and e.get("operation") == "remediate" and e.get("scope", {}).get("write_marker") == "write-A" ] self.assertEqual( len(matching_a), 1, f"snapshot(A) must appear exactly once, " f"got {len(matching_a)}: {matching_a}", ) # Snapshot(B) must appear exactly once matching_b = [ e for e in po_c if e.get("status") == "partial" and e.get("operation") == "backport" and e.get("scope", {}).get("write_marker") == "write-B" ] self.assertEqual( len(matching_b), 1, f"snapshot(B) must appear exactly once, " f"got {len(matching_b)}: {matching_b}", ) # Total history must be exactly 2 (A and B) self.assertEqual( len(po_c), 2, f"History must have exactly 2 entries, " f"got {len(po_c)}", ) # Distinct metadata: A and B have different operations ops = {e.get("operation") for e in po_c} self.assertEqual( ops, {"remediate", "backport"}, "History entries must have distinct operations", ) # Distinct write markers: no two entries share a marker markers = [e.get("scope", {}).get("write_marker") for e in po_c] self.assertEqual( len(markers), len(set(markers)), f"History write markers must be distinct: {markers}", ) # Malformed history still rejected args_bad = argparse.Namespace( status="needs_input", operation="jira_action", artifact_dir=tmpdir, data=json.dumps({ "scope": {"cve_ids": ["CVE-2024-1"]}, "stages_completed": [], "stages_remaining": [], "evidence": {}, "prior_operations": [ {"status": "x", "prior_operations": [{"nested": True}]}, ], }), ) with unittest.mock.patch("sys.stdout", new_callable=io.StringIO): rc_bad = unattended._cmd_make_result(args_bad) self.assertEqual(rc_bad, 2, "Nested prior_operations must be rejected") # --------------------------------------------------------------------------- # Candidate fa55: validate_prior_result guard gaps # --------------------------------------------------------------------------- class TestValidatePriorResultGuards(unittest.TestCase): """validate_prior_result must not crash on unhashable status types, must reject non-list backport_results, and must enforce target coverage when target_branches is declared. """ def _base_prior( self, *, bp_results: Any = None, target_branches: Any = None, ) -> dict[str, Any]: """Build a minimal prior_result dict for testing.""" result: dict[str, Any] = {"status": "completed"} if bp_results is not None: result["backport_results"] = bp_results if target_branches is not None: result["target_branches"] = target_branches return { "result": result, "decision_requests": [], "scan_snapshot": { "scan_id": "s1", "scope": {"cve_ids": ["CVE-2024-1"]}, "candidate_sha": "abc123", }, } def _good_child(self, target: str = "release-1") -> dict[str, Any]: return { "status": "completed", "target_branch": target, "pr": {"url": "https://github.com/org/repo/pull/1"}, "evidence": {"x": 1}, } # --- Bug 1: list/dict status → TypeError --- def test_list_status_no_typeerror(self) -> None: """br.status as list must produce a validation error, not TypeError from frozenset membership.""" prior = self._base_prior(bp_results=[ {"status": ["completed"], "target_branch": "r1", "pr": {"url": "https://github.com/org/repo/pull/1"}, "evidence": {"x": 1}}, ]) errors = unattended.validate_prior_result(prior, {}) self.assertTrue( any("status" in e for e in errors), f"Expected status type error, got {errors}", ) def test_dict_status_no_typeerror(self) -> None: """br.status as dict must produce a validation error, not TypeError from frozenset membership.""" prior = self._base_prior(bp_results=[ {"status": {"val": "completed"}, "target_branch": "r1", "pr": {"url": "https://github.com/org/repo/pull/1"}, "evidence": {"x": 1}}, ]) errors = unattended.validate_prior_result(prior, {}) self.assertTrue( any("status" in e for e in errors), f"Expected status type error, got {errors}", ) # --- Bug 2: non-list backport_results silently ignored --- def test_nonlist_backport_results_detected(self) -> None: """Non-list backport_results (string) must produce an error.""" prior = self._base_prior(bp_results="not-a-list") # Patch so it's not filtered out prior["result"]["backport_results"] = "not-a-list" errors = unattended.validate_prior_result(prior, {}) self.assertTrue( any("backport_results" in e for e in errors), f"Expected backport_results type error, got {errors}", ) def test_nonlist_backport_results_int(self) -> None: """Non-list backport_results (int) must produce an error.""" prior = self._base_prior(bp_results=42) prior["result"]["backport_results"] = 42 errors = unattended.validate_prior_result(prior, {}) self.assertTrue( any("backport_results" in e for e in errors), f"Expected backport_results type error, got {errors}", ) def test_nonlist_backport_results_dict(self) -> None: """Non-list backport_results (dict) must produce an error.""" prior = self._base_prior(bp_results={"a": 1}) prior["result"]["backport_results"] = {"a": 1} errors = unattended.validate_prior_result(prior, {}) self.assertTrue( any("backport_results" in e for e in errors), f"Expected backport_results type error, got {errors}", ) # --- Bug 3: target coverage --- def test_empty_targets_with_extra_child_rejected(self) -> None: """target_branches=[] with any child → coverage mismatch.""" prior = self._base_prior( bp_results=[self._good_child("extra-branch")], target_branches=[], ) errors = unattended.validate_prior_result(prior, {}) self.assertTrue( any("target" in e.lower() or "coverage" in e.lower() for e in errors), f"Expected coverage/target error, got {errors}", ) def test_extra_child_nonstring_target_rejected(self) -> None: """Required child ok + extra child with int target → error.""" prior = self._base_prior( bp_results=[ self._good_child("release-1"), {"status": "completed", "target_branch": 42, "pr": {"url": "https://github.com/org/repo/pull/2"}, "evidence": {"y": 1}}, ], target_branches=["release-1"], ) errors = unattended.validate_prior_result(prior, {}) self.assertTrue( any("target" in e.lower() for e in errors), f"Expected target type/coverage error, got {errors}", ) def test_duplicate_target_children_rejected(self) -> None: """Two children with same target → coverage mismatch.""" prior = self._base_prior( bp_results=[ self._good_child("release-1"), self._good_child("release-1"), ], target_branches=["release-1"], ) errors = unattended.validate_prior_result(prior, {}) self.assertTrue( any("target" in e.lower() or "coverage" in e.lower() for e in errors), f"Expected coverage error for duplicate target, got {errors}", ) # --- Positive: valid cases pass --- def test_valid_completed_children_accepted(self) -> None: """Valid completed children with matching targets pass.""" prior = self._base_prior( bp_results=[ self._good_child("release-1"), self._good_child("release-2"), ], target_branches=["release-1", "release-2"], ) errors = unattended.validate_prior_result(prior, {}) bp_errors = [e for e in errors if "backport" in e.lower() or "target" in e.lower()] self.assertEqual( bp_errors, [], f"Valid children must not produce errors, got {bp_errors}", ) def test_absent_backport_results_accepted(self) -> None: """Absent backport_results (no key) is legitimate.""" prior = self._base_prior() errors = unattended.validate_prior_result(prior, {}) bp_errors = [e for e in errors if "backport" in e.lower()] self.assertEqual(bp_errors, [], f"Absent backport_results must be ok: {bp_errors}") def test_none_status_child_accepted_not_completed(self) -> None: """Child with status=None is accepted (not in completed set, not a type error).""" prior = self._base_prior(bp_results=[ {"status": None, "target_branch": "r1"}, ]) errors = unattended.validate_prior_result(prior, {}) status_errors = [e for e in errors if "status" in e.lower() and "backport" in e.lower()] self.assertEqual(status_errors, [], f"None status must be accepted: {status_errors}") def test_empty_list_backport_results_accepted(self) -> None: """Empty list backport_results is legitimate.""" prior = self._base_prior(bp_results=[]) errors = unattended.validate_prior_result(prior, {}) bp_errors = [e for e in errors if "backport" in e.lower()] self.assertEqual(bp_errors, [], f"Empty list bp must be ok: {bp_errors}") # --------------------------------------------------------------------------- # Carryover: validate_prior_result target_branches element validation # --------------------------------------------------------------------------- class TestValidatePriorResultTargetElements(unittest.TestCase): """validate_prior_result must validate target_branches element types before sorting to prevent TypeError on heterogeneous lists. """ def _base_prior( self, *, bp_results: Any = None, target_branches: Any = None, ) -> dict[str, Any]: result: dict[str, Any] = {"status": "completed"} if bp_results is not None: result["backport_results"] = bp_results if target_branches is not None: result["target_branches"] = target_branches return { "result": result, "decision_requests": [], "scan_snapshot": { "scan_id": "s1", "scope": {"cve_ids": ["CVE-2024-1"]}, "candidate_sha": "abc123", }, } def _good_child(self, target: str = "release-1") -> dict[str, Any]: return { "status": "completed", "target_branch": target, "pr": {"url": "https://github.com/org/repo/pull/1"}, "evidence": {"x": 1}, } def test_heterogeneous_list_no_typeerror(self) -> None: """Mixed str/int target_branches must produce a controlled error, not TypeError from sorted().""" prior = self._base_prior( bp_results=[self._good_child("release-1")], target_branches=[1, "release-1"], ) errors = unattended.validate_prior_result(prior, {}) self.assertTrue( any("target_branches" in e for e in errors), f"Expected target_branches type error, got {errors}", ) def test_none_element_no_typeerror(self) -> None: """None element in target_branches must produce a controlled error.""" prior = self._base_prior( bp_results=[self._good_child("release-1")], target_branches=[None, "release-1"], ) errors = unattended.validate_prior_result(prior, {}) self.assertTrue( any("target_branches" in e for e in errors), f"Expected target_branches type error, got {errors}", ) def test_blank_element_rejected(self) -> None: """Blank string element in target_branches must be rejected.""" prior = self._base_prior( bp_results=[self._good_child("release-1")], target_branches=["", "release-1"], ) errors = unattended.validate_prior_result(prior, {}) self.assertTrue( any("target_branches" in e for e in errors), f"Expected target_branches blank error, got {errors}", ) def test_whitespace_only_element_rejected(self) -> None: """Whitespace-only element in target_branches must be rejected.""" prior = self._base_prior( bp_results=[self._good_child("release-1")], target_branches=[" ", "release-1"], ) errors = unattended.validate_prior_result(prior, {}) self.assertTrue( any("target_branches" in e for e in errors), f"Expected whitespace-only error, got {errors}", ) def test_duplicate_targets_rejected(self) -> None: """Duplicate elements in target_branches must be rejected.""" prior = self._base_prior( bp_results=[ self._good_child("release-1"), self._good_child("release-1"), ], target_branches=["release-1", "release-1"], ) errors = unattended.validate_prior_result(prior, {}) self.assertTrue( any("duplicate" in e.lower() for e in errors), f"Expected duplicate error, got {errors}", ) def test_list_element_no_typeerror(self) -> None: """List element in target_branches must produce error, not TypeError.""" prior = self._base_prior( bp_results=[self._good_child("release-1")], target_branches=[["release-1"], "release-2"], ) errors = unattended.validate_prior_result(prior, {}) self.assertTrue( any("target_branches" in e for e in errors), f"Expected target_branches type error, got {errors}", ) def test_valid_targets_still_accepted(self) -> None: """Valid nonblank string targets still pass element validation.""" prior = self._base_prior( bp_results=[ self._good_child("release-1"), self._good_child("release-2"), ], target_branches=["release-1", "release-2"], ) errors = unattended.validate_prior_result(prior, {}) target_errors = [ e for e in errors if "target_branches" in e and "target_branch" not in e ] self.assertEqual( target_errors, [], f"Valid targets must not produce errors, got {target_errors}", ) if __name__ == "__main__": unittest.main()