@elvis1513/auto-coding-skill 4.2.1 → 4.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,6 +2,7 @@
2
2
  from __future__ import annotations
3
3
 
4
4
  import argparse
5
+ import base64
5
6
  import contextlib
6
7
  import io
7
8
  import json
@@ -9,6 +10,8 @@ import os
9
10
  import subprocess
10
11
  import sys
11
12
  import tempfile
13
+ import time
14
+ import tomllib
12
15
  import unittest
13
16
  from pathlib import Path
14
17
  from unittest import mock
@@ -695,6 +698,118 @@ class AutoCodingProfileTests(unittest.TestCase):
695
698
  self.assertEqual(next_owner, result["next_owner"])
696
699
  ap._validate_orchestration_contract("result", result)
697
700
 
701
+ def test_reviewer_runtime_normalizes_presentation_fields_but_binds_identity(self) -> None:
702
+ assignment = self.reviewer_assignment()
703
+ result = ap._normalize_reviewer_runtime_result(
704
+ assignment,
705
+ {
706
+ "verdict": "approved",
707
+ "summary": "focused review complete",
708
+ "evidence": ["diff inspected"],
709
+ },
710
+ )
711
+ self.assertEqual("reviewer-1", result["node_id"])
712
+ self.assertEqual("a" * 64, result["diff_fingerprint"])
713
+ self.assertEqual(["fixer-1"], result["depends_on"])
714
+ self.assertEqual("focused review complete", result["summary"])
715
+
716
+ with self.assertRaisesRegex(APError, "does not match its assignment"):
717
+ ap._normalize_reviewer_runtime_result(
718
+ assignment,
719
+ {
720
+ "verdict": "approved",
721
+ "diff_fingerprint": "b" * 64,
722
+ },
723
+ )
724
+
725
+ def test_codex_reviewer_command_is_ephemeral_read_only_and_uses_managed_instructions(self) -> None:
726
+ repo, _ = self.make_repo()
727
+ assignment_path = repo / "assignment.json"
728
+ result_path = repo / "result.json"
729
+ with mock.patch.object(ap.shutil, "which", return_value="/usr/local/bin/codex"):
730
+ command = ap._codex_reviewer_command(repo, assignment_path, result_path)
731
+
732
+ self.assertEqual("/usr/local/bin/codex", command[0])
733
+ self.assertIn("--ephemeral", command)
734
+ self.assertIn("--ignore-user-config", command)
735
+ self.assertIn("read-only", command)
736
+ self.assertIn('model_reasoning_effort="xhigh"', command)
737
+ developer_override = next(
738
+ item for item in command if item.startswith("developer_instructions=")
739
+ )
740
+ tomllib.loads(developer_override)
741
+ self.assertEqual(str(result_path), command[command.index("-o") + 1])
742
+
743
+ def test_reviewer_process_timeout_terminates_its_process_group(self) -> None:
744
+ repo, _ = self.make_repo()
745
+ process = mock.MagicMock()
746
+ process.pid = 4321
747
+ process.poll.return_value = None
748
+ process.communicate.side_effect = [
749
+ subprocess.TimeoutExpired(cmd=["fake-reviewer"], timeout=0.01),
750
+ ("partial", "stopped"),
751
+ ]
752
+ with (
753
+ mock.patch.object(ap.subprocess, "Popen", return_value=process) as popen,
754
+ mock.patch.object(ap.os, "killpg") as killpg,
755
+ ):
756
+ with self.assertRaises(ap._ReviewerRuntimeTimeout):
757
+ ap._run_supervised_reviewer_process(
758
+ ["fake-reviewer", "; touch must-not-run"],
759
+ cwd=repo,
760
+ env={"PATH": os.environ.get("PATH", "")},
761
+ timeout_seconds=0.01,
762
+ )
763
+
764
+ self.assertEqual(["fake-reviewer", "; touch must-not-run"], popen.call_args.args[0])
765
+ self.assertTrue(popen.call_args.kwargs["start_new_session"])
766
+ self.assertEqual(mock.call(4321, ap.signal.SIGTERM), killpg.call_args)
767
+
768
+ def test_reviewer_assignment_timing_contract_and_fixed_budgets(self) -> None:
769
+ issued_at = "2026-07-16T00:00:00+00:00"
770
+ assignment = self.reviewer_assignment(
771
+ review_depth="focused",
772
+ timeout_seconds=90,
773
+ issued_at=issued_at,
774
+ deadline_at="2026-07-16T00:01:30+00:00",
775
+ scope_revision=1,
776
+ )
777
+ self.assertEqual(
778
+ assignment,
779
+ ap._validate_orchestration_contract("assignment", assignment),
780
+ )
781
+ self.assertEqual(("focused", 90), ap._normalized_task_review_policy(True))
782
+ self.assertEqual(
783
+ ("deep", 300),
784
+ ap._normalized_task_review_policy(True, "focused", "deep"),
785
+ )
786
+ self.assertEqual(("none", 0), ap._normalized_task_review_policy(False, "deep"))
787
+
788
+ invalid_contracts = [
789
+ self.reviewer_assignment(
790
+ review_depth="focused",
791
+ timeout_seconds=0,
792
+ issued_at=issued_at,
793
+ deadline_at="2026-07-16T00:01:30+00:00",
794
+ ),
795
+ self.reviewer_assignment(
796
+ review_depth="focused",
797
+ timeout_seconds=90,
798
+ issued_at="2026-07-16T00:00:00",
799
+ deadline_at="2026-07-16T00:01:30",
800
+ ),
801
+ self.reviewer_assignment(
802
+ review_depth="focused",
803
+ timeout_seconds=90,
804
+ issued_at=issued_at,
805
+ deadline_at="2026-07-16T00:01:00+00:00",
806
+ ),
807
+ ]
808
+ for payload in invalid_contracts:
809
+ with self.subTest(payload=payload):
810
+ with self.assertRaises(APError):
811
+ ap._validate_orchestration_contract("assignment", payload)
812
+
698
813
  def test_reviewer_result_template_rejects_wrong_role_self_review_and_empty_fingerprint(self) -> None:
699
814
  invalid_assignments = [
700
815
  self.reviewer_assignment(role="explorer"),
@@ -832,6 +947,32 @@ class AutoCodingProfileTests(unittest.TestCase):
832
947
  ap._final_gate_budget(cfg)
833
948
  self.assertIn("cannot exceed validation.max_total_seconds", str(context.exception))
834
949
 
950
+ def test_final_changed_scope_gate_runs_blocking_structure_check(self) -> None:
951
+ cfg = base_config()
952
+ cfg["structure"] = {
953
+ "enabled": True,
954
+ "enforcement": "blocking",
955
+ "max_file_lines_warn": 0,
956
+ "max_file_lines_block": 1,
957
+ "max_function_lines_warn": 0,
958
+ "layer_rules": {"enabled": False},
959
+ }
960
+ cfg["optimization"] = {"require_baseline_for_global_review": False}
961
+ repo, _ = self.make_repo("src/too-large.py", cfg)
962
+ (repo / "src" / "too-large.py").write_text("one\ntwo\n", encoding="utf-8")
963
+
964
+ with self.assertRaisesRegex(APError, "structure-check failed"):
965
+ ap.cmd_light_gate(
966
+ argparse.Namespace(
967
+ repo=str(repo),
968
+ scope="changed",
969
+ profile="",
970
+ mode="dev",
971
+ base="",
972
+ explain=False,
973
+ )
974
+ )
975
+
835
976
  def test_route_timeout_uses_smallest_matching_budget(self) -> None:
836
977
  cfg = base_config()
837
978
  cfg["commands"] = {"one": "true", "two": "true"}
@@ -1256,6 +1397,413 @@ class AutoCodingProfileTests(unittest.TestCase):
1256
1397
  with self.assertRaises(APError):
1257
1398
  ap.cmd_structure_check(args)
1258
1399
 
1400
+ def test_structure_block_warnings_matrix(self) -> None:
1401
+ cases = [
1402
+ ("blocking", True, True),
1403
+ ("blocking", False, False),
1404
+ ("advisory", True, False),
1405
+ ]
1406
+ for enforcement, configured_block_warnings, should_block in cases:
1407
+ with self.subTest(
1408
+ enforcement=enforcement,
1409
+ block_warnings=configured_block_warnings,
1410
+ ):
1411
+ cfg = base_config()
1412
+ cfg["structure"] = {
1413
+ "enabled": True,
1414
+ "enforcement": enforcement,
1415
+ "block_warnings": configured_block_warnings,
1416
+ "max_file_lines_warn": 4,
1417
+ "max_file_lines_block": 50,
1418
+ "max_function_lines_warn": 4,
1419
+ "max_added_lines_to_large_file": 50,
1420
+ "layer_rules": {"enabled": False},
1421
+ }
1422
+ cfg["optimization"] = {"require_baseline_for_global_review": False}
1423
+ repo, _ = self.make_repo("src/large.py", cfg)
1424
+ (repo / "src" / "large.py").write_text(
1425
+ "def oversized():\n"
1426
+ " value = 1\n"
1427
+ " value += 1\n"
1428
+ " value += 1\n"
1429
+ " return value\n"
1430
+ "tail = 1\n",
1431
+ encoding="utf-8",
1432
+ )
1433
+ output = io.StringIO()
1434
+ args = argparse.Namespace(
1435
+ repo=str(repo), scope="changed", base="", json=True
1436
+ )
1437
+ with mock.patch.object(ap, "_record_evidence"), contextlib.redirect_stdout(output):
1438
+ if should_block:
1439
+ with self.assertRaisesRegex(APError, "2 blocking issue"):
1440
+ ap.cmd_structure_check(args)
1441
+ else:
1442
+ ap.cmd_structure_check(args)
1443
+
1444
+ result = json.loads(output.getvalue())
1445
+ self.assertIs(should_block, result["block_warnings"])
1446
+ if should_block:
1447
+ self.assertEqual(2, len(result["blocking"]))
1448
+ else:
1449
+ self.assertEqual([], result["blocking"])
1450
+ self.assertEqual(2, len(result["warnings"]))
1451
+
1452
+ def test_structure_block_warnings_keeps_accepted_debt_size_findings_advisory(self) -> None:
1453
+ cfg = base_config()
1454
+ cfg["structure"] = {
1455
+ "enabled": True,
1456
+ "enforcement": "blocking",
1457
+ "block_warnings": True,
1458
+ "max_file_lines_warn": 4,
1459
+ "max_file_lines_block": 50,
1460
+ "max_function_lines_warn": 4,
1461
+ "max_added_lines_to_large_file": 50,
1462
+ "accepted_debt_paths": ["src/accepted.py"],
1463
+ "layer_rules": {"enabled": False},
1464
+ }
1465
+ cfg["optimization"] = {"require_baseline_for_global_review": False}
1466
+ repo, _ = self.make_repo("src/accepted.py", cfg)
1467
+ (repo / "src" / "accepted.py").write_text(
1468
+ "def accepted_debt():\n"
1469
+ " value = 1\n"
1470
+ " value += 1\n"
1471
+ " value += 1\n"
1472
+ " return value\n"
1473
+ "tail = 1\n",
1474
+ encoding="utf-8",
1475
+ )
1476
+ output = io.StringIO()
1477
+ with mock.patch.object(ap, "_record_evidence"), contextlib.redirect_stdout(output):
1478
+ ap.cmd_structure_check(
1479
+ argparse.Namespace(
1480
+ repo=str(repo), scope="changed", base="", json=True
1481
+ )
1482
+ )
1483
+
1484
+ result = json.loads(output.getvalue())
1485
+ self.assertIs(True, result["block_warnings"])
1486
+ self.assertEqual([], result["blocking"])
1487
+ self.assertEqual(2, len(result["warnings"]))
1488
+
1489
+ def test_changed_scope_light_gate_blocks_promoted_structure_warnings(self) -> None:
1490
+ cfg = base_config()
1491
+ cfg["structure"] = {
1492
+ "enabled": True,
1493
+ "enforcement": "blocking",
1494
+ "block_warnings": True,
1495
+ "max_file_lines_warn": 3,
1496
+ "max_file_lines_block": 50,
1497
+ "max_function_lines_warn": 0,
1498
+ "max_added_lines_to_large_file": 50,
1499
+ "layer_rules": {"enabled": False},
1500
+ }
1501
+ cfg["optimization"] = {"require_baseline_for_global_review": False}
1502
+ repo, _ = self.make_repo("src/warn-only.py", cfg)
1503
+ (repo / "src" / "warn-only.py").write_text(
1504
+ "one\ntwo\nthree\nfour\n",
1505
+ encoding="utf-8",
1506
+ )
1507
+
1508
+ with mock.patch.object(ap, "_record_evidence"):
1509
+ with self.assertRaisesRegex(APError, "structure-check failed"):
1510
+ ap.cmd_light_gate(
1511
+ argparse.Namespace(
1512
+ repo=str(repo),
1513
+ scope="changed",
1514
+ profile="",
1515
+ mode="dev",
1516
+ base="",
1517
+ explain=False,
1518
+ )
1519
+ )
1520
+
1521
+ def test_doctor_validates_structure_block_warnings_type_and_advises_on_no_effect(self) -> None:
1522
+ cfg = base_config()
1523
+ cfg["structure"] = {
1524
+ "enabled": True,
1525
+ "enforcement": "blocking",
1526
+ "block_warnings": "true",
1527
+ }
1528
+ repo, _ = self.make_repo(config=cfg)
1529
+ with self.assertRaisesRegex(
1530
+ APError,
1531
+ "structure.block_warnings must be a boolean",
1532
+ ):
1533
+ ap.cmd_doctor(argparse.Namespace(repo=str(repo)))
1534
+
1535
+ cfg = base_config()
1536
+ cfg["structure"] = {
1537
+ "enabled": True,
1538
+ "enforcement": "advisory",
1539
+ "block_warnings": True,
1540
+ }
1541
+ repo, _ = self.make_repo(config=cfg)
1542
+ output = io.StringIO()
1543
+ with mock.patch.object(ap, "_record_evidence"), contextlib.redirect_stdout(output):
1544
+ ap.cmd_doctor(argparse.Namespace(repo=str(repo)))
1545
+ self.assertIn(
1546
+ "structure.block_warnings has no effect unless structure.enforcement=blocking",
1547
+ output.getvalue(),
1548
+ )
1549
+ self.assertIn("[doctor] OK", output.getvalue())
1550
+
1551
+ def test_java_and_kotlin_semicolon_imports_trigger_layer_rules(self) -> None:
1552
+ structure_cfg = {
1553
+ "layer_rules": {
1554
+ "enabled": True,
1555
+ "block": True,
1556
+ "rules": [
1557
+ {
1558
+ "name": "domain",
1559
+ "paths": ["src/domain/**"],
1560
+ "forbidden_imports": ["**/infrastructure/**"],
1561
+ }
1562
+ ],
1563
+ }
1564
+ }
1565
+ for suffix in ["java", "kt"]:
1566
+ for import_line in [
1567
+ "import com.example.infrastructure.SecretStore;",
1568
+ "import com.example.infrastructure.SecretStore; // boundary",
1569
+ "import com.example.infrastructure.SecretStore; /* boundary */",
1570
+ "import com.example.infrastructure.SecretStore; /* boundary",
1571
+ "import com.example.infrastructure.SecretStore; /* boundary */ // trailing",
1572
+ ]:
1573
+ with self.subTest(suffix=suffix, import_line=import_line):
1574
+ path = f"src/domain/Order.{suffix}"
1575
+ issues = ap._structure_boundary_issues(
1576
+ path,
1577
+ [
1578
+ "package com.example.domain;",
1579
+ import_line,
1580
+ "",
1581
+ "class Order {}",
1582
+ ],
1583
+ structure_cfg,
1584
+ )
1585
+ self.assertEqual(1, len(issues))
1586
+ self.assertIn("layer 'domain'", issues[0])
1587
+ self.assertIn("com.example.infrastructure.SecretStore", issues[0])
1588
+
1589
+ def test_function_ranges_follow_language_syntax(self) -> None:
1590
+ fixtures = [
1591
+ (
1592
+ "repository.go",
1593
+ [
1594
+ "package sample",
1595
+ "",
1596
+ "func (r *Repository) Find() error {",
1597
+ " if r == nil {",
1598
+ " return nil",
1599
+ " }",
1600
+ " return nil",
1601
+ "}",
1602
+ "",
1603
+ "var after = 1",
1604
+ ],
1605
+ 3,
1606
+ 6,
1607
+ ),
1608
+ (
1609
+ "normalize.py",
1610
+ [
1611
+ "def normalize(value):",
1612
+ " if value:",
1613
+ " return value",
1614
+ " return None",
1615
+ "after = 1",
1616
+ ],
1617
+ 1,
1618
+ 4,
1619
+ ),
1620
+ (
1621
+ "block-arrow.ts",
1622
+ [
1623
+ "const block = () => {",
1624
+ ' const literal = "}";',
1625
+ " // } inside a comment is not the function end",
1626
+ " if (literal) {",
1627
+ " return literal;",
1628
+ " }",
1629
+ ' return "";',
1630
+ "};",
1631
+ "const after = 1;",
1632
+ ],
1633
+ 1,
1634
+ 8,
1635
+ ),
1636
+ (
1637
+ "expression-arrow.ts",
1638
+ [
1639
+ "const expression = value =>",
1640
+ " value",
1641
+ " .trim()",
1642
+ " .toLowerCase();",
1643
+ "const after = 1;",
1644
+ ],
1645
+ 1,
1646
+ 4,
1647
+ ),
1648
+ (
1649
+ "parenthesized-arrow.ts",
1650
+ [
1651
+ "const expression = value => (",
1652
+ " value",
1653
+ " .trim()",
1654
+ " .toLowerCase()",
1655
+ ");",
1656
+ "const after = 1;",
1657
+ ],
1658
+ 1,
1659
+ 5,
1660
+ ),
1661
+ (
1662
+ "conditional-arrow.ts",
1663
+ [
1664
+ "const choose = value => value ?",
1665
+ " first :",
1666
+ " second;",
1667
+ "const after = 1;",
1668
+ ],
1669
+ 1,
1670
+ 3,
1671
+ ),
1672
+ (
1673
+ "view-arrow.tsx",
1674
+ [
1675
+ "const View = () => <section>",
1676
+ " <span>value</span>",
1677
+ "</section>;",
1678
+ "const after = 1;",
1679
+ ],
1680
+ 1,
1681
+ 3,
1682
+ ),
1683
+ (
1684
+ "multiline-params.ts",
1685
+ [
1686
+ "const multi = (",
1687
+ " value: string,",
1688
+ " count: number,",
1689
+ ") => {",
1690
+ " return value.repeat(count);",
1691
+ "};",
1692
+ "const after = 1;",
1693
+ ],
1694
+ 1,
1695
+ 6,
1696
+ ),
1697
+ (
1698
+ "same-indent-plus.ts",
1699
+ [
1700
+ "const sum = value => value",
1701
+ "+ increment",
1702
+ "+ extra;",
1703
+ "const after = 1;",
1704
+ ],
1705
+ 1,
1706
+ 3,
1707
+ ),
1708
+ (
1709
+ "same-indent-chain.ts",
1710
+ [
1711
+ "const normalized = value => value",
1712
+ ".trim()",
1713
+ ".toLowerCase();",
1714
+ "const after = 1;",
1715
+ ],
1716
+ 1,
1717
+ 3,
1718
+ ),
1719
+ (
1720
+ "conditional-jsx.tsx",
1721
+ [
1722
+ "const Choice = value => (",
1723
+ " value ?",
1724
+ " <Accept /> :",
1725
+ " <Reject />",
1726
+ ");",
1727
+ "const after = 1;",
1728
+ ],
1729
+ 1,
1730
+ 5,
1731
+ ),
1732
+ ]
1733
+ trailing_top_level_lines = [f"top_level_{index} = {index}" for index in range(20)]
1734
+ for path, lines, start_line, expected_size in fixtures:
1735
+ with self.subTest(path=path):
1736
+ warnings = ap._function_size_warnings(
1737
+ path,
1738
+ lines + trailing_top_level_lines,
1739
+ expected_size - 1,
1740
+ )
1741
+ self.assertEqual(1, len(warnings))
1742
+ self.assertIn(f"{path}:{start_line}", warnings[0])
1743
+ self.assertIn(f"has {expected_size} lines", warnings[0])
1744
+
1745
+ def test_python_inline_suite_does_not_absorb_top_level_code(self) -> None:
1746
+ lines = ["def ready(): return True"] + [f"top_level_{index} = {index}" for index in range(200)]
1747
+ self.assertEqual([], ap._function_size_warnings("inline.py", lines, 120))
1748
+
1749
+ def test_function_ranges_ignore_type_braces_regex_literals_and_prior_assignments(self) -> None:
1750
+ long_body = [f" value_{index} := {index}" for index in range(130)]
1751
+ go_lines = [
1752
+ "package sample",
1753
+ "func build() struct {",
1754
+ " Value string",
1755
+ "} {",
1756
+ *long_body,
1757
+ " return struct { Value string }{}",
1758
+ "}",
1759
+ ]
1760
+ self.assertEqual(1, len(ap._function_size_warnings("builder.go", go_lines, 120)))
1761
+
1762
+ js_lines = [
1763
+ "const block = () => {",
1764
+ " const matcher = /}/;",
1765
+ *[f" const value_{index} = {index};" for index in range(130)],
1766
+ "};",
1767
+ ]
1768
+ self.assertEqual(1, len(ap._function_size_warnings("regex.js", js_lines, 120)))
1769
+
1770
+ separate_declarations = [
1771
+ 'const label = "ready"',
1772
+ "const block = () => {",
1773
+ " return label;",
1774
+ "};",
1775
+ ]
1776
+ self.assertEqual([], ap._function_size_warnings("separate.ts", separate_declarations, 3))
1777
+
1778
+ def test_function_range_scan_is_bounded_and_gate_deadline_is_enforced(self) -> None:
1779
+ depth = 4000
1780
+ lines = [f"function nested_{index}() {{" for index in range(depth)]
1781
+ lines.extend("}" for _ in range(depth))
1782
+ started = time.perf_counter()
1783
+ warnings = ap._function_size_warnings("nested.js", lines, 10)
1784
+ elapsed = time.perf_counter() - started
1785
+ self.assertEqual(depth - 5, len(warnings))
1786
+ self.assertLess(elapsed, 3.0, f"function scan must stay threshold-bounded, got {elapsed:.3f}s")
1787
+
1788
+ cfg = base_config()
1789
+ cfg["structure"] = {
1790
+ "enabled": True,
1791
+ "enforcement": "blocking",
1792
+ "max_function_lines_warn": 10,
1793
+ "layer_rules": {"enabled": False},
1794
+ }
1795
+ cfg["optimization"] = {"require_baseline_for_global_review": False}
1796
+ repo, _ = self.make_repo("src/app.js", cfg)
1797
+ with self.assertRaisesRegex(APError, "timed out during structure check"):
1798
+ ap._run_structure_check_for_gate(
1799
+ repo,
1800
+ cfg,
1801
+ "changed",
1802
+ "",
1803
+ deadline=time.monotonic() - 1,
1804
+ command_timeout_s=30,
1805
+ )
1806
+
1259
1807
  def test_structure_scan_excludes_non_authoritative_agent_history_and_metadata(self) -> None:
1260
1808
  repo, _ = self.make_repo()
1261
1809
  files = {
@@ -1451,6 +1999,231 @@ class AutoCodingProfileTests(unittest.TestCase):
1451
1999
  ap.cmd_doctor(argparse.Namespace(repo=str(repo)))
1452
2000
  ap.cmd_verify_api_docs(argparse.Namespace(repo=str(repo)))
1453
2001
 
2002
+ def test_root_and_nested_package_json_are_high_risk_and_require_review(self) -> None:
2003
+ repo, cfg = self.make_repo()
2004
+ for path in ["package.json", "frontend/admin/package.json"]:
2005
+ with self.subTest(path=path):
2006
+ plan = self.plan(
2007
+ repo,
2008
+ cfg,
2009
+ planned_paths=[path],
2010
+ requested_task_kind="change",
2011
+ )
2012
+ self.assertEqual("high-risk", plan["profile"])
2013
+ self.assertTrue(plan["review_required"])
2014
+ self.assertIn("release_or_tooling", plan["categories"])
2015
+
2016
+ def test_explicit_full_structure_scope_inspects_clean_tracked_files(self) -> None:
2017
+ cfg = base_config()
2018
+ cfg["structure"] = {
2019
+ "enabled": True,
2020
+ "enforcement": "advisory",
2021
+ "max_file_lines_warn": 0,
2022
+ "max_file_lines_block": 0,
2023
+ "max_function_lines_warn": 0,
2024
+ "layer_rules": {"enabled": False},
2025
+ }
2026
+ cfg["optimization"] = {"require_baseline_for_global_review": False}
2027
+ repo, _ = self.make_repo("src/app.py", cfg)
2028
+ run(repo, "git", "add", "-A")
2029
+ run(repo, "git", "commit", "-qm", "add tracked structure fixture")
2030
+
2031
+ output = io.StringIO()
2032
+ with mock.patch.object(ap, "_record_evidence"), contextlib.redirect_stdout(output):
2033
+ ap.cmd_structure_check(
2034
+ argparse.Namespace(repo=str(repo), scope="full", base="", json=True)
2035
+ )
2036
+
2037
+ result = json.loads(output.getvalue())
2038
+ self.assertEqual("full", result["scope"])
2039
+ self.assertGreaterEqual(result["inspected_files"], 1)
2040
+
2041
+ def test_verify_jenkins_accepts_access_schema_and_uses_selected_identity(self) -> None:
2042
+ repo, cfg = self.make_repo()
2043
+ (repo / "Jenkinsfile").write_text("pipeline {}\n", encoding="utf-8")
2044
+
2045
+ with mock.patch.object(ap, "_record_evidence"):
2046
+ ap.cmd_verify_jenkins(argparse.Namespace(repo=str(repo), component="all"))
2047
+
2048
+ header = ap._jenkins_basic_auth_headers(cfg, component="backend")["Authorization"]
2049
+ decoded = base64.b64decode(header.removeprefix("Basic ")).decode("utf-8")
2050
+ self.assertEqual("back:back-pass", decoded)
2051
+
2052
+ def test_jenkins_component_is_ambiguous_with_two_access_endpoints(self) -> None:
2053
+ cfg = base_config()
2054
+ with self.assertRaisesRegex(APError, "Multiple access.jenkins endpoints"):
2055
+ ap._resolve_jenkins_component(cfg)
2056
+
2057
+ cfg["access"]["jenkins"]["backend"] = dict(cfg["access"]["jenkins"]["frontend"])
2058
+ self.assertEqual("frontend", ap._resolve_jenkins_component(cfg))
2059
+
2060
+ def test_verify_jenkins_build_uses_one_selected_access_component(self) -> None:
2061
+ repo, _ = self.make_repo()
2062
+ args = argparse.Namespace(
2063
+ repo=str(repo),
2064
+ component="backend",
2065
+ git_ref="HEAD",
2066
+ job_name=None,
2067
+ job_url=None,
2068
+ multibranch_root_job=None,
2069
+ branch_name=None,
2070
+ build_number=7,
2071
+ max_builds=20,
2072
+ timeout_sec=1,
2073
+ poll_sec=1,
2074
+ allow_no_deploy=True,
2075
+ )
2076
+ payload = {
2077
+ "number": 7,
2078
+ "result": "SUCCESS",
2079
+ "building": False,
2080
+ "description": "deployed",
2081
+ "url": "https://jenkins-back.test/7/",
2082
+ }
2083
+ with (
2084
+ mock.patch.object(ap, "_jenkins_api_get_json", return_value=payload) as api_get,
2085
+ mock.patch.object(ap, "_record_evidence"),
2086
+ ):
2087
+ ap.cmd_verify_jenkins_build(args)
2088
+
2089
+ self.assertEqual("backend", api_get.call_args.kwargs["component"])
2090
+ self.assertIn("https://jenkins-back.test/7/api/json", api_get.call_args.args[0])
2091
+
2092
+ def test_verify_jenkins_preserves_legacy_configuration(self) -> None:
2093
+ cfg = base_config()
2094
+ cfg["access"]["jenkins"] = {}
2095
+ cfg["jenkins"] = {
2096
+ "base_url": "https://legacy-jenkins.test",
2097
+ "job_url": "https://legacy-jenkins.test/job/backend",
2098
+ "trigger_branch": "dev",
2099
+ "image_repository": "registry.test/project/backend",
2100
+ "image_tag_strategy": "commit",
2101
+ "deploy_env": "production",
2102
+ "api_user": "legacy-user",
2103
+ "api_password": "legacy-pass",
2104
+ }
2105
+ cfg["target_env"] = {
2106
+ "health_base_url": "https://legacy-target.test",
2107
+ "health_path": "/healthz",
2108
+ }
2109
+ repo, _ = self.make_repo(config=cfg)
2110
+ (repo / "Jenkinsfile").write_text("pipeline {}\n", encoding="utf-8")
2111
+
2112
+ with mock.patch.object(ap, "_record_evidence"):
2113
+ ap.cmd_verify_jenkins(argparse.Namespace(repo=str(repo), component="all"))
2114
+
2115
+ self.assertEqual("", ap._resolve_jenkins_component(cfg))
2116
+ header = ap._jenkins_basic_auth_headers(cfg)["Authorization"]
2117
+ decoded = base64.b64decode(header.removeprefix("Basic ")).decode("utf-8")
2118
+ self.assertEqual("legacy-user:legacy-pass", decoded)
2119
+
2120
+ def test_jenkins_crumb_cache_is_scoped_by_identity(self) -> None:
2121
+ cfg = base_config()
2122
+ cfg["access"]["jenkins"]["frontend"].update(
2123
+ {"url": "https://jenkins.test/job/frontend", "username": "front-user"}
2124
+ )
2125
+ cfg["access"]["jenkins"]["backend"].update(
2126
+ {"url": "https://jenkins.test/job/backend", "username": "back-user"}
2127
+ )
2128
+
2129
+ def response(crumb: str) -> mock.MagicMock:
2130
+ value = mock.MagicMock()
2131
+ value.__enter__.return_value.read.return_value = json.dumps(
2132
+ {"crumbRequestField": "Jenkins-Crumb", "crumb": crumb}
2133
+ ).encode("utf-8")
2134
+ return value
2135
+
2136
+ ap._JENKINS_CRUMB_CACHE.clear()
2137
+ self.addCleanup(ap._JENKINS_CRUMB_CACHE.clear)
2138
+ with mock.patch.object(
2139
+ ap.urllib.request,
2140
+ "urlopen",
2141
+ side_effect=[response("crumb-front"), response("crumb-back")],
2142
+ ) as urlopen:
2143
+ frontend = ap._jenkins_crumb_headers(cfg, component="frontend")
2144
+ backend = ap._jenkins_crumb_headers(cfg, component="backend")
2145
+
2146
+ self.assertEqual({"Jenkins-Crumb": "crumb-front"}, frontend)
2147
+ self.assertEqual({"Jenkins-Crumb": "crumb-back"}, backend)
2148
+ self.assertEqual(2, urlopen.call_count)
2149
+ self.assertEqual(2, len(ap._JENKINS_CRUMB_CACHE))
2150
+
2151
+ def test_verify_target_uses_access_project_urls_without_legacy_wait(self) -> None:
2152
+ repo, _ = self.make_repo()
2153
+ args = argparse.Namespace(
2154
+ repo=str(repo),
2155
+ backend_path=["/healthz"],
2156
+ frontend_path=["/"],
2157
+ backend_basic_auth=False,
2158
+ frontend_basic_auth=False,
2159
+ )
2160
+ with (
2161
+ mock.patch.object(ap, "_http_get", return_value=(200, "ok")) as http_get,
2162
+ mock.patch.object(ap, "_wait_for_health_url") as wait_health,
2163
+ mock.patch.object(ap, "_record_evidence"),
2164
+ ):
2165
+ ap.cmd_verify_target(args)
2166
+
2167
+ self.assertEqual(
2168
+ ["https://project-back.test/healthz", "https://project-front.test/"],
2169
+ [call.args[0] for call in http_get.call_args_list],
2170
+ )
2171
+ wait_health.assert_not_called()
2172
+
2173
+ def test_verify_target_missing_access_url_fails_before_network(self) -> None:
2174
+ cfg = base_config()
2175
+ cfg["access"]["project"]["backend"]["url"] = ""
2176
+ repo, _ = self.make_repo(config=cfg)
2177
+ args = argparse.Namespace(
2178
+ repo=str(repo),
2179
+ backend_path=["/healthz"],
2180
+ frontend_path=None,
2181
+ backend_basic_auth=False,
2182
+ frontend_basic_auth=False,
2183
+ )
2184
+ with (
2185
+ mock.patch.object(ap, "_http_get") as http_get,
2186
+ mock.patch.object(ap, "http_get_status") as get_status,
2187
+ mock.patch.object(ap.time, "sleep") as sleep,
2188
+ ):
2189
+ with self.assertRaisesRegex(APError, "access.project.backend.url"):
2190
+ ap.cmd_verify_target(args)
2191
+
2192
+ http_get.assert_not_called()
2193
+ get_status.assert_not_called()
2194
+ sleep.assert_not_called()
2195
+
2196
+ def test_verify_target_preserves_legacy_endpoint_and_health_preflight(self) -> None:
2197
+ cfg = base_config()
2198
+ cfg["access"]["project"] = {}
2199
+ cfg["jenkins"] = {"deploy_timeout_sec": 7}
2200
+ cfg["target_env"] = {
2201
+ "backend_base_url": "https://legacy-target.test/api",
2202
+ "backend_username": "legacy-user",
2203
+ "backend_password": "legacy-pass",
2204
+ "health_base_url": "https://legacy-target.test",
2205
+ "health_path": "/healthz",
2206
+ }
2207
+ repo, _ = self.make_repo(config=cfg)
2208
+ args = argparse.Namespace(
2209
+ repo=str(repo),
2210
+ backend_path=["/ready"],
2211
+ frontend_path=None,
2212
+ backend_basic_auth=False,
2213
+ frontend_basic_auth=False,
2214
+ )
2215
+ with (
2216
+ mock.patch.object(ap, "_wait_for_health_url") as wait_health,
2217
+ mock.patch.object(ap, "_http_get", return_value=(200, "ok")) as http_get,
2218
+ mock.patch.object(ap, "_record_evidence"),
2219
+ ):
2220
+ ap.cmd_verify_target(args)
2221
+
2222
+ wait_health.assert_called_once_with(
2223
+ "target", "https://legacy-target.test/healthz", 7
2224
+ )
2225
+ self.assertEqual("https://legacy-target.test/api/ready", http_get.call_args.args[0])
2226
+
1454
2227
  def test_dev_closure_rejects_owner_acceptance_pass(self) -> None:
1455
2228
  with self.assertRaises(APError):
1456
2229
  ap._validate_closure_result("dev", "PASS")