
    MiiI                       d Z ddlmZ ddlZddlmc mZ ddl	Z
ddlZddlZddlmZ ddlZ ee      j#                         j$                  d   Zedz  dz  Z ej*                  d	      d
        ZddZddZddZddZddZddZy)uN   Tests for scripts/lock_in_verify.py — Lock-in First-line guard AST verifier.    )annotationsN)Path   scriptszlock_in_verify.pymodule)scopec                 *   t         j                  j                  dt              } g }| }| r| j                  }|}|sdddt        j                         v st        j                  |       rt        j                  |       ndiz  }|j                  |       | rlddt        j                         v st        j                  |       rt        j                  |       ndt        j                        dz  }|j                  |       t        j                  |d      i z  }dd	|iz  }t        t        j                  |            d x}x}}t         j                  j                  |       }|t        j                   d<   | j                  j#                  |       |S )
Nlock_in_verifyz%(py2)spy2specz#%(py6)s
{%(py6)s = %(py4)s.loader
})py4py6r   zassert %(py9)spy9)	importlibutilspec_from_file_locationSCRIPTloader@py_builtinslocals
@pytest_ar_should_repr_global_name	_safereprappend_format_boolopAssertionError_format_explanationmodule_from_specsysmodulesexec_module)	r   @py_assert1@py_assert0@py_assert5@py_format3@py_format7@py_format8@py_format10mods	            8/home/jay/workspace/tests/scripts/test_lock_in_verify.pylock_in_moduler+      s    >>112BFKD44DKKK444DDK
..
)
)$
/C$'CKK !KKC J    c                    | |z  }|j                   j                  dd       |j                  t        j                  |      d       |S )NT)parentsexist_okzutf-8)encoding)parentmkdir
write_texttextwrapdedent)tmp_pathnamebodyps       r*   _writer:      s=    4AHHNN4$N/LL&L9Hr,   c                   t        |dd      }| j                  |dd      }g }||k(  }|st        j                  d|fd||f      dt	        j
                         v st        j                  |      rt        j                  |      ndt        j                  |      dz  }d	d
|iz  }t        t        j                  |            d x}}y )Nzmain.pya7  
        import subprocess
        from pathlib import Path
        WORKSPACE_ROOT = "/x"
        def _execute_approve(task_num: int, pr_num: int) -> dict:
            _cancelled_marker = Path(WORKSPACE_ROOT) / "memory" / "events" / f"task-{task_num}.cancelled"
            if _cancelled_marker.exists():
                return {"ok": False}
            _guard_sh = Path(WORKSPACE_ROOT) / "scripts" / "guard.sh"
            _gp = subprocess.run(["bash", str(_guard_sh), "pre-push", f"task-{task_num}"], capture_output=True, text=True, timeout=60)
            if _gp.returncode != 0:
                return {"ok": False}
            cmd = ["gh", "pr", "merge", str(pr_num), "--merge"]
            proc = subprocess.run(cmd, capture_output=True, text=True, timeout=60)
            return {"ok": proc.returncode == 0}
        _execute_approveghprmerge==z%(py0)s == %(py3)serrspy0py3assert %(py5)spy5
r:   verify_functionr   _call_reprcomparer   r   r   r   r   r   r+   r6   r9   rD   @py_assert2r"   @py_format4@py_format6s           r*   test_pass_anu_patternrQ   !   s    x % 	A  ))!-?AVWD42:42442r,   c                   t        |dd      }| j                  |dd      }|s{t        j                  d      dz   ddt	        j
                         v st        j                  |      rt        j                  |      ndiz  }t        t        j                  |            d	j                  |      }g }d
}|j                  } |       }	||	v }
|
}|
sd}||v }|}|st        j                  d|
fd||	f      t        j                  |      dt	        j
                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |	      dz  }dd|iz  }|j                  |       |
st        j                  dfd|f      t        j                  |      dt	        j
                         v st        j                  |      rt        j                  |      nddz  }dd|iz  }|j                  |       t        j                  |d      i z  }dd|iz  }t        t        j                  |            d x}x}x}x}
x}x}	x}}y )Nzbad.pyaI  
        import subprocess
        from pathlib import Path
        def _execute_approve(task_num: int, pr_num: int) -> dict:
            cmd = ["gh", "pr", "merge", str(pr_num), "--merge"]
            proc = subprocess.run(cmd, capture_output=True, text=True, timeout=60)
            return {"ok": proc.returncode == 0}
        r<   r=   z,should have flagged missing first-line guardz
>assert %(py0)srF   rD    	cancelledzbody too short)in)zD%(py3)s in %(py9)s
{%(py9)s = %(py7)s
{%(py7)s = %(py5)s.lower
}()
}joined)rG   rI   py7r   z%(py11)spy11)z%(py14)s in %(py16)s)py14py16z%(py18)spy18   zassert %(py21)spy21)r:   rK   r   _format_assertmsgr   r   r   r   r   r   joinlowerrL   r   r   )r+   r6   r9   rD   @py_format1rV   r"   rN   @py_assert6@py_assert8@py_assert4r#   @py_assert13@py_assert15r(   @py_format12@py_format17@py_format19@py_format20@py_format22s                       r*   test_fail_no_first_line_guardrl   6   sf   x $ 	A ))!-?AVWD?????????4???4?????XXd^FF;F&,,F,.F;.(F,<F,<,FFFFF;.FFF;FFFFFF&FFF&FFF,FFF.FFFFFFF,<FFF,<FFFFFFFFFFFFFFFFFFFFFFFFr,   c                   t        |dd      }| j                  |dd      }|sedddt        j                         v st	        j
                  |      rt	        j                  |      ndiz  }t        t	        j                  |            d |D        }t        |      }|st	        j                  |      d	z   d
t        j                         v st	        j
                  t              rt	        j                  t              nd
t	        j                  |      t	        j                  |      dz  }t        t	        j                  |            d x}}y )Nzwrong_order.pya  
        import subprocess
        from pathlib import Path
        WORKSPACE_ROOT = "/x"
        def _execute_approve(task_num: int, pr_num: int) -> dict:
            _cancelled_marker = Path(WORKSPACE_ROOT) / "memory" / "events" / f"task-{task_num}.cancelled"
            if _cancelled_marker.exists():
                return {"ok": False}
            cmd = ["gh", "pr", "merge", str(pr_num), "--merge"]
            proc = subprocess.run(cmd, capture_output=True, text=True, timeout=60)
            _guard_sh = Path(WORKSPACE_ROOT) / "scripts" / "guard.sh"
            subprocess.run(["bash", str(_guard_sh), "pre-push", "x"])
            return {"ok": True}
        r<   r=   zassert %(py0)srF   rD   c              3  0   K   | ]  }d |v xs d|v   yw)u   Lock-in 위반u   늦음N ).0es     r*   	<genexpr>z.test_fail_guard_after_merge.<locals>.<genexpr>V   s"     D!1$5A5Ds   z.
>assert %(py4)s
{%(py4)s = %(py0)s(%(py2)s)
}any)rF   r   r   )r:   rK   r   r   r   r   r   r   r   rs   r^   )r+   r6   r9   rD   ra   r"   @py_assert3@py_format5s           r*   test_fail_guard_after_mergerv   E   s    x) , 	A ))!-?AVWDKKKKK4KKK4KKKKKDtDJ3DDJDJJdJJJJJJ3JJJ3JJJDJJJDJJJJJJr,   c                   t        |dd      }| j                  |dd      }g }||k(  }|st        j                  d|fd||f      dt	        j
                         v st        j                  |      rt        j                  |      ndt        j                  |      dz  }d	d
|iz  }t        t        j                  |            d x}}y )Nzauto_merge.pya  
        import subprocess
        from pathlib import Path
        class C:
            def __init__(self):
                self.workspace = Path("/x")
            def execute_merge(self, project_path, task_id, team_id):
                _cancelled_marker = self.workspace / "memory" / "events" / f"{task_id}.cancelled"
                if _cancelled_marker.exists():
                    raise RuntimeError("cancelled")
                _guard_sh = self.workspace / "scripts" / "guard.sh"
                _gp = subprocess.run(["bash", str(_guard_sh), "pre-push", task_id], capture_output=True, text=True, timeout=60)
                if _gp.returncode != 0:
                    raise RuntimeError("guard fail")
                worktree_manager = Path(__file__).parent / "worktree_manager.py"
                cmd = ["python3", str(worktree_manager), "finish", project_path, task_id, team_id, "--action", "merge"]
                result = subprocess.run(cmd, capture_output=True, text=True, timeout=120)
                return result
        execute_merge)worktree_managerfinishrA   rC   rD   rE   rH   rI   rJ   rM   s           r*   test_pass_auto_merge_patternr{   Y   s    x + 	A& ))!_>\]D42:42442r,   c                   | j                   D ]  }t        |d   z  }| j                  ||d   |d         }g }||k(  }|st        j                  d|fd||f      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      dz  }t        j                  |d    d|d    d	|       d
z   d|iz  }t        t        j                  |            dx}} y)z@Sanity: the real anu_confirm_bot/main.py and auto_merge.py PASS.filefuncmerge_signature_tokensrA   rC   rD   rE   z::z broke: z
>assert %(py5)srI   N)CHECKSROOTrK   r   rL   r   r   r   r   r^   r   r   )r+   r   targetrD   rN   r"   rO   rP   s           r*   test_real_workspace_paths_passr   q   s    %% KV$--fd6lDIaDbcJtrzJJJtrJJJJJJtJJJtJJJrJJJd6l^2d6l^8D6JJJJJJJKr,   )r6   r   r7   strr8   r   returnr   )r6   r   r   None)r   r   )__doc__
__future__r   builtinsr   _pytest.assertion.rewrite	assertionrewriter   importlib.utilr   r   r4   pathlibr   pytest__file__resolver.   r   r   fixturer+   r:   rQ   rl   rv   r{   r   ro   r,   r*   <module>r      s    T "     
   H~''*			/	/ h  *GK(0Kr,   