
    i                    8   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ZddlmZ ddlZ ee      j%                         j&                  d   ZddZ ej,                         dd       ZddZdd	Zdd
ZddZddZddZddZddZddZ ddZ!ddZ"y)u
  tests/regression/test_automation_audit_helpers.py — task-2471+1 자동화 회귀.

회장 명령 (E): regression test 5건 — 자동화 헬퍼 함수 (audit jsonl 보존,
sha256 chunked, slugify) 의 회귀 차단.

검증 대상:
- ``_sha256_file`` chunked 처리 (대용량 OOM-safe)
- ``_append_audit_jsonl`` parent dir 자동 생성 + 한 줄당 한 record
- ``_slugify_reason`` archive path suffix 안전 변환
- ``ESCALATED_AUDIT_LOG`` / ``BRANCH_CLEANUP_LOG`` /
  ``CONSISTENCY_AUDIT_LOG`` 경로 상수 검증
    )annotationsN)Path   c                   | j                  dt        |             t        j                  j	                  dt        t
        dz  dz              }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$                  |j&                  <   |j                  j)                  |
       |
S )NWORKSPACE_ROOTtaskctl_helpers_isolatedscriptsz
taskctl.pyz%(py2)spy2specz#%(py6)s
{%(py6)s = %(py4)s.loader
})py4py6r   assert %(py9)spy9)setenvstr	importlibutilspec_from_file_location	WORKSPACEloader@py_builtinslocals
@pytest_ar_should_repr_global_name	_safereprappend_format_boolopAssertionError_format_explanationmodule_from_specsysmodulesnameexec_module)monkeypatchrootr   @py_assert1@py_assert0@py_assert5@py_format3@py_format7@py_format8@py_format10mods              a/home/jay/workspace/.worktrees/task-2471+1-dev2/tests/regression/test_automation_audit_helpers.py_load_taskctlr0      s   'T3>>11"I	!L01D  44DKKK444DDK
..
)
)$
/C CKK		KKC J    c                    t        ||       S )N)r0   )tmp_pathr%   s     r/   r.   r.   '   s    h//r1   c                   |dz  }d}|j                  |       t        j                  |      j                         }| j                  } ||      }||k(  }|s/t        j                  d|fd||f      dt        j                         v st        j                  |       rt        j                  |       ndt        j                  |      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      dt        j                         v st        j                  |      rt        j                  |      nddz  }d	d
|iz  }	t        t        j                  |	            d x}x}}y )Nz	small.binsx   hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
==zR%(py5)s
{%(py5)s = %(py2)s
{%(py2)s = %(py0)s._sha256_file
}(%(py3)s)
} == %(py7)sr.   pexpectedpy0r
   py3py5py7r   r   write_byteshashlibsha256	hexdigest_sha256_filer   _call_reprcomparer   r   r   r   r   r   
r.   r3   r8   datar9   r'   @py_assert4@py_assert6r,   r-   s
             r/   *test_sha256_file_matches_hashlib_for_smallrJ   ,   s    ;A DMM$~~d#--/H*A*(****(******3***3*********A***A*********(***(*******r1   c                "   |dz  }ddz  }|j                  |       t        j                  |      j                         }| j                  } ||      }||k(  }|s/t        j                  d|fd||f      dt        j                         v st        j                  |       rt        j                  |       ndt        j                  |      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      dt        j                         v st        j                  |      rt        j                  |      ndd	z  }d
d|iz  }	t        t        j                  |	            d x}x}}y )Nz	large.bin   ai  r5   r7   r.   r8   r9   r:   r   r   r?   rF   s
             r/   "test_sha256_file_chunked_for_largerM   4   s    ;A>"DMM$~~d#--/H*A*(****(******3***3*********A***A*********(***(*******r1   c                ~   |dz  }|j                          | j                  } ||      }d}||k(  }|st        j                  d|fd||f      dt	        j
                         v st        j                  |       rt        j                  |       ndt        j                  |      dt	        j
                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      dz  }dd	|iz  }t        t        j                  |            d x}x}x}}y )
Nz	empty.bin@e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855r5   )zR%(py5)s
{%(py5)s = %(py2)s
{%(py2)s = %(py0)s._sha256_file
}(%(py3)s)
} == %(py8)sr.   r8   )r;   r
   r<   r=   py8zassert %(py10)spy10)
touchrD   r   rE   r   r   r   r   r   r   )	r.   r3   r8   r'   rH   @py_assert7rI   @py_format9@py_format11s	            r/   test_sha256_empty_filerV   <   s&   ;AGGIMM	N  M     	   	   	           	   N      r1   c                   |dz  dz  dz  }|j                   }|j                  } |       }| }|sddt        j                         v st	        j
                  |      rt	        j                  |      ndt	        j                  |      t	        j                  |      t	        j                  |      dz  }t        t	        j                  |            d x}x}x}}| j                  |ddi       |j                  } |       }|sd	dt        j                         v st	        j
                  |      rt	        j                  |      ndt	        j                  |      t	        j                  |      d
z  }t        t	        j                  |            d x}}|j                  d      }	t        j                  |	j                               d   }
d}|
|k(  }|slt	        j                  d|fd|
|f      t	        j                  |
      t	        j                  |      dz  }dd|iz  }t        t	        j                  |            d x}
x}}y )Ndeeplynestedaudit.jsonlzaassert not %(py6)s
{%(py6)s = %(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.parent
}.exists
}()
}log)r;   r
   r   r   k   zAassert %(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.exists
}()
})r;   r
   r   utf-8encodingr5   z%(py1)s == %(py4)spy1r   assert %(py6)sr   )parentexistsr   r   r   r   r   r   r   _append_audit_jsonl	read_textjsonloadsstriprE   )r.   r3   r[   r'   @py_assert3r)   rS   r,   @py_format5liner(   @py_assert2r+   s                r/   *test_append_audit_jsonl_creates_parent_dirrp   E   sm   
X

(=
8Czz"z  " """""""""""s"""s"""z""" """"""""""C#q*:::<<33:<=='=*D::djjl#C(-A-(A----(A---(---A-------r1   c           	         |dz  }t        d      D ]  }| j                  ||d| d        |j                  d      j                         D cg c]  }|j	                         s| }}t        |      }d}||k(  }|st        j                  d|fd||f      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                  |      t        j                  |      dz  }	dd|	iz  }
t        t        j                  |
            d x}x}}|D cg c]  }t        j                  |       }}|D cg c]  }|d   	 }}d}t        |      }t!        |      }||k(  }|s&t        j                  d|fd||f      t        j                  |      dt        j                         v st        j                  t               rt        j                  t               nddt        j                         v st        j                  t               rt        j                  t               ndt        j                  |      t        j                  |      t        j                  |      dz  }dd|iz  }t        t        j                  |            d x}x}x}x}}y c c}w c c}w c c}w )NrZ      r)imsgr^   r_   r5   z0%(py3)s
{%(py3)s = %(py0)s(%(py1)s)
} == %(py6)slenlinesr;   rc   r<   r   assert %(py8)srP   rt   )zP%(py1)s == %(py10)s
{%(py10)s = %(py3)s(%(py8)s
{%(py8)s = %(py4)s(%(py6)s)
})
}listrange)rc   r<   r   r   rP   rQ   zassert %(py12)spy12)r|   rg   rh   
splitlinesrk   rw   r   rE   r   r   r   r   r   r   ri   rj   r{   )r.   r3   r[   rt   lrx   ro   r)   rH   r+   rT   parsedrs   r(   rS   @py_assert9rU   @py_format13s                     r/   'test_append_audit_jsonl_appends_recordsr   N   s   
]
"C1X ?1qW%=>? =='=2==?1779E  u::?:33uu:%*+djjm+F+"#qAcF#5#!5E!H5tH~5#~5555#~555#555555t555t555555E555E555!555H555~55555555 ,#s   L&L L"Lc                   |dz  }ddd}| j                  ||       |j                  d      }t        j                  |j	                               }|d   }d}||k(  }|slt        j                  d|fd	||f      t        j                  |      t        j                  |      d
z  }	dd|	iz  }
t        t        j                  |
            d x}x}}y )Nzaudit_unicode.jsonlu   한글 테스트 ✅z	task-test)reasontask_idr^   r_   r   r5   ra   rb   rd   r   )
rg   rh   ri   rj   rk   r   rE   r   r   r   )r.   r3   r[   recordtextr   r(   rl   ro   rm   r+   s              r/   $test_append_audit_jsonl_unicode_safer   Z   s    
*
*C.;GFC(=='=*DZZ

%F(55555555555555555555555r1   c                   | j                   }d} ||      }d}||k(  }|st        j                  d|fd||f      dt        j                         v st        j
                  |       rt        j                  |       ndt        j                  |      t        j                  |      t        j                  |      t        j                  |      dz  }dd|iz  }t        t        j                  |            d x}x}x}x}}| j                   }d	} ||      }d
}||k(  }|st        j                  d|fd||f      dt        j                         v st        j
                  |       rt        j                  |       ndt        j                  |      t        j                  |      t        j                  |      t        j                  |      dz  }dd|iz  }t        t        j                  |            d x}x}x}x}}| j                   }d} ||      }d}||k(  }|st        j                  d|fd||f      dt        j                         v st        j
                  |       rt        j                  |       ndt        j                  |      t        j                  |      t        j                  |      t        j                  |      dz  }dd|iz  }t        t        j                  |            d x}x}x}x}}| j                   }d} ||      }d}||k(  }|st        j                  d|fd||f      dt        j                         v st        j
                  |       rt        j                  |       ndt        j                  |      t        j                  |      t        j                  |      t        j                  |      dz  }dd|iz  }t        t        j                  |            d x}x}x}x}}y )Nzstale 30min!!!zstale-30minr5   )zU%(py6)s
{%(py6)s = %(py2)s
{%(py2)s = %(py0)s._slugify_reason
}(%(py4)s)
} == %(py9)sr.   )r;   r
   r   r   r   zassert %(py11)spy11z/path/with/slashzpath-with-slash archivedz!!!)	_slugify_reasonr   rE   r   r   r   r   r   r   )r.   r'   rl   r)   @py_assert8rS   r-   @py_format12s           r/   'test_slugify_reason_strips_unsafe_charsr   c   ss   A/A/0AMA0MAAAA0MAAAAAA3AAA3AAAAAA/AAA0AAAMAAAAAAAG1G12G6GG26GGGGG26GGGGGGG3GGG3GGGGGG1GGG2GGG6GGGGGGGG0r0r"0j0"j0000"j00000030003000000r000"000j00000003u3u%33%3333%33333333333333333u333%33333333333r1   c                J   d}| j                  |      }t        |      }d}||k  }|st        j                  d|fd||f      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                  |      t        j                  |      dz  }dd	|iz  }t        t        j                  |            d x}x}}y )
Naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(   )<=)z0%(py3)s
{%(py3)s = %(py0)s(%(py1)s)
} <= %(py6)srw   outry   rz   rP   )
r   rw   r   rE   r   r   r   r   r   r   )r.   long_reasonr   ro   r)   rH   r+   rT   s           r/   test_slugify_reason_max_lenr   j   s    K


k
*Cs8r8r>8r33ss8rr1   c                   dD ]  }t        | |      }|j                  }|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                  |      t        j                  |      dz  }t        j                  | d|       dz   d	|iz  }t        t        j                  |            d
x}x}x}}|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  }
dd|
iz  }t        t        j                  |            d
x}x}}	 y
)uO   3 audit jsonl 경로 모두 ``orchestration-audit/`` 하위에 위치 (정책).)ESCALATED_AUDIT_LOGBRANCH_CLEANUP_LOGCONSISTENCY_AUDIT_LOGzorchestration-auditr5   )zH%(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.parent
}.name
} == %(py7)spath)r;   r
   r   r>   z! not under orchestration-audit/: z
>assert %(py9)sr   Nz.jsonl)z.%(py2)s
{%(py2)s = %(py0)s.suffix
} == %(py5)s)r;   r
   r=   zassert %(py7)sr>   )getattrre   r#   r   rE   r   r   r   r   _format_assertmsgr   r   suffix)r.   
const_namer   r'   rl   rI   r)   r,   r-   rH   @py_format6s              r/   2test_audit_log_constants_under_orchestration_auditr   p   s    	'

 sJ'{{ 	
{ 	
#8 	
#88 	
 	
#8 	
 	
	6	
 	
   	
 	
 		  	
 	
 		  	
 	
 		   	
 	
 		 $9 	
 	
  l;D6B	
 	
 	
 	
 	
 	
 {{&h&{h&&&&{h&&&&&&t&&&t&&&{&&&h&&&&&&&	'r1   c                j   | j                   | j                  | j                  h}t        |      }d}||k(  }|st	        j
                  d|fd||f      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                  |      t	        j                  |      dz  }dd|iz  }t        t	        j                  |            d	x}x}}y	)
u4   3 audit log은 서로 다른 파일 (mixing 방지).   r5   rv   rw   pathsry   rz   rP   N)r   r   r   rw   r   rE   r   r   r   r   r   r   )r.   r   ro   r)   rH   r+   rT   s          r/   !test_audit_log_constants_distinctr   ~   s     	!!E
 u::?:33uu:r1   c                   | j                   }|D ch c]  \  }}|	 }}}h d}||k(  }|st        j                  d|fd||f      dt        j                         v st        j
                  |      rt        j                  |      nddt        j                         v st        j
                  |      rt        j                  |      nddz  }dd|iz  }t        t        j                  |            d	}y	c c}}w )
uW   ``_FINISH_TASK_STAGES`` 가 14정상 경로 중 자동화 가능 8단계 모두 포함.>   MERGEDPR_OPENVERIFIED	COMMITTED
CI_PENDINGREVIEW_READYGEMINI_PENDINGHUMAN_APPROVEDr5   )z%(py0)s == %(py2)s
src_statesr9   )r;   r
   zassert %(py4)sr   N)	_FINISH_TASK_STAGESr   rE   r   r   r   r   r   r   )	r.   stagess_r   r9   r'   r*   rm   s	            r/   )test_finish_task_stages_cover_normal_pathr      s    $$F &'1!'J'	H !!!!:!!!!!!:!!!:!!!!!!!!!!!!!!!! (s   C5)r%   pytest.MonkeyPatchr&   r   )r3   r   r%   r   )r3   r   returnNone)r   r   )#__doc__
__future__r   builtinsr   _pytest.assertion.rewrite	assertionrewriter   rA   importlib.utilr   ri   r!   pathlibr   pytest__file__resolveparentsr   r0   fixturer.   rJ   rM   rV   rp   r   r   r   r   r   r   r    r1   r/   <module>r      s    #      
  N""$,,Q/	
 0 0++.	664'"r1   