
     Oi                     H   d Z ddlZddlmc mZ ddlZddlZddl	m
Z
 ddlZ e
e      j                         j                  j                  j                  Z ee      ej"                  vr"ej"                  j%                  d ee             edz  dz  Zd Zd Zd Zd	 Zd
 Zy)u  task-2503 회귀 테스트 파일 3 — dispatch.py main() hook 통합 검증.

회장 §6 회귀 테스트 (파일 3/3):
  - dispatch/__init__.py hook 코드 존재 검증 (grep 기반)
  - --override-merge-topology-gate argparse 등록 검증
  - BLOCK 시 sys.exit(1) 호출 검증
  - run_gate 호출 가능성 검증
  - AUDIT_LOG_PATH monkeypatch로 실제 audit 파일 오염 방지
    N)Pathdispatchz__init__.pyc                     	 ddl } d}| |u}|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}}	 dd
lm} t        |      }|st        j                  d      dz   dt        j                         v st        j
                  t              rt        j                  t              nddt        j                         v st        j
                  |      rt        j                  |      ndt        j                  |      dz  }t        t        j                  |            d}y# t        $ r#}t        j                  d	|        Y d}~d}~ww xY w# t         $ r"}t        j                  d|        Y d}~yd}~ww xY w)ur   dispatch/__init__.py를 import해도 에러 없음 + from utils.merge_topology_gate import run_gate 호출 가능.r   Nis notz%(py0)s is not %(py3)s_dispatch_modpy0py3zassert %(py5)spy5zdispatch module import failed: run_gatezrun_gate must be callablez.
>assert %(py3)s
{%(py3)s = %(py0)s(%(py1)s)
}callabler   )r   py1r   zrun_gate import failed: )r   
@pytest_ar_call_reprcompare@py_builtinslocals_should_repr_global_name	_safereprAssertionError_format_explanation	Exceptionpytestfailutils.merge_topology_gater   r   _format_assertmsgImportError)r	   @py_assert2@py_assert1@py_format4@py_format6er   s          Z/home/jay/workspace/tests/regression/test_merge_topology_gate_dispatch_integration_2503.py)test_dispatch_imports_merge_topology_gater&      s/   ;($((}D((((}D((((((}(((}(((D(((((((
46!>!>>#>>>>>>>x>>>x>>>>>>>>>>>>!>>>>>>  ;5aS9::;  4.qc2334s1   B)F ,C-G	 	G#GG		G4G//G4c                     t         j                  }  |        }|st        j                  dt                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} }t         j                  d      }d}||v }|st        j                  d	|fd
||f      t        j                  |      dt	        j
                         v st        j                  |      rt        j                  |      nddz  }t        j                  d      dz   d|iz  }t        t        j                  |            dx}}d}||v }|st        j                  d	|fd
||f      t        j                  |      dt	        j
                         v st        j                  |      rt        j                  |      nddz  }t        j                  d      dz   d|iz  }t        t        j                  |            dx}}y)u[   dispatch/__init__.py에 --override-merge-topology-gate 인자와 dest 등록 확인 (grep). dispatch/__init__.py not found: C
>assert %(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.exists
}()
}DISPATCH_FILEr   py2py4Nutf-8encodingz--override-merge-topology-gateinz%(py1)s in %(py3)scontentr   r   zK--override-merge-topology-gate flag must be registered in dispatch argparse
>assert %(py5)sr   z#dest="override_merge_topology_gate"zHdest="override_merge_topology_gate" must be set in argparse add_argumentr*   existsr   r   r   r   r   r   r   r   	read_textr   r!   @py_assert3@py_format5r4   @py_assert0r    r"   r#   s           r%   (test_dispatch_argparse_has_override_flagr>   /   s   U!U!UU%Em_#UUUUUUU=UUU=UUUUUU!UUUUUU%%w%7G+ +w6  +w    ,      07    07    	V     1 0G;  0G    1      5<    5<    	S        c                     t         j                  }  |        }|st        j                  dt                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} }t         j                  d      }t        j                  d|t        j                        }d}||u} | 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z   d|iz  }t        t        j                  |            dx} }d}||v }|st        j                  d|fd||f      t        j                  |      dt	        j
                         v st        j                  |      rt        j                  |      nddz  }t        j                  d      dz   d|iz  }t        t        j                  |            dx}}y)uU   dispatch/__init__.py의 merge-topology-gate hook 블록에 sys.exit(1) 존재 검증.r(   r)   r*   r+   Nr.   r/   z(\[merge-topology-gate\].*?sys\.exit\(1\)r   r   
hook_matchr
   zGdispatch/__init__.py hook block must call sys.exit(1) on BLOCK decisionr6   r   u   Merge Topology Gate 차단r1   r3   r4   r5   u=   "Merge Topology Gate 차단" message must exist in hook block)r*   r8   r   r   r   r   r   r   r   r   r9   researchDOTALLr   )	r!   r;   r<   r4   rA   r    r"   r#   r=   s	            r%   2test_dispatch_main_block_decision_exits_with_errorrE   ?   s   U!U!UU%Em_#UUUUUUU=UUU=UUUUUU!UUUUUU%%w%7G 3
		J
 " :T!  :T              "    	R    
 ( '72  '7    (      ,3    ,3    	H    r?   c                  D   t         j                  }  |        }|st        j                  dt                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} }t         j                  d      }d}||v }|st        j                  d	|fd
||f      t        j                  |      dt	        j
                         v st        j                  |      rt        j                  |      nddz  }t        j                  d      dz   d|iz  }t        t        j                  |            dx}}y)uc   dispatch/__init__.py에 from utils.merge_topology_gate import run_gate 존재 (hook 등록 확인).r(   r)   r*   r+   Nr.   r/   z.from utils.merge_topology_gate import run_gater1   r3   r4   r5   zHdispatch/__init__.py must import run_gate from utils.merge_topology_gater6   r   r7   r:   s           r%   *test_dispatch_main_allow_decision_proceedsrG   W   s%   U!U!UU%Em_#UUUUUUU=UUU=UUUUUU!UUUUUU%%w%7G; ;wF  ;w    <      @G    @G    	S    r?   c                    ddl m} | dz  }|j                  d|        |dd      \  }}|j                  }d}||k(  }|st	        j
                  d	|fd
||f      dt        j                         v st	        j                  |      rt	        j                  |      ndt	        j                  |      t	        j                  |      dz  }	t	        j                  d|j                         dz   d|	iz  }
t        t	        j                  |
            dx}x}}d}||u }|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z   d|iz  }	t        t	        j                  |	            dx}}y)uK   run_gate — empty task_desc → metadata 누락 → BLOCK + allowed=False.r   r   zmerge-topology-gate.jsonlz(utils.merge_topology_gate.AUDIT_LOG_PATHz
test-empty )task_id	task_descBLOCK)==)z0%(py2)s
{%(py2)s = %(py0)s.decision
} == %(py5)sdecision)r   r,   r   z*empty task_desc must result in BLOCK, got z
>assert %(py7)spy7NF)is)z%(py0)s is %(py3)sallowedr
   zexpected allowed=False, got r6   r   )r   r   setattrrN   r   r   r   r   r   r   r   r   r   )tmp_pathmonkeypatchr   
audit_filerN   rQ   r!   @py_assert4r;   r#   @py_format8r    r"   s                r%   (test_run_gate_callable_with_minimal_argsrX   d   sl   2 77J2
 !DHg  '                    !(    5X5F5F4GH     E7eEEE7eEEEEEE7EEE7EEEeEEE;G9EEEEEEEr?   )__doc__builtinsr   _pytest.assertion.rewrite	assertionrewriter   rB   syspathlibr   r   __file__resolveparent	WORKSPACEstrpathinsertr*   r&   r>   rE   rG   rX    r?   r%   <module>rh      s     	 
   N""$++2299	y>!HHOOAs9~&J&64(
 0Fr?   