
    j                         d 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d Zd Zd	efd
Zd	efdZd Zy)u@  
test_cleanup_workspace_protection_2569.py
MT-T4: RC-2 + AD-5 검증
  - cleanup-workspace.py --report 실행 성공 (dry-run 기본)
  - is_protected() task md → True, tmp/old.txt → False
  - _audit_log() 호출 후 cleanup-audit.jsonl에 JSONL 형식 기록
  - protection-list.json valid JSON + 필수 키 존재
    N)Path   scriptszcleanup-workspace.pyc                  6   t         j                  j                  dt              } g }d}| |u}|}|r| 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  }dd|iz  }	|j                  |	       |rt        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  }|j                  |       t        j                  |d      i z  }t        j                  dt               dz   d|iz  }t        t        j                  |            dx}x}x}x}x}x}}t         j                  j!                  |       }| j                  j#                  |       |S )uL   importlib으로 cleanup-workspace.py 동적 로드 (파일명에 하이픈).cleanup_workspaceN)is not)z%(py2)s is not %(py5)sspec)py2py5z%(py7)spy7)z5%(py11)s
{%(py11)s = %(py9)s.loader
} is not %(py14)s)py9py11py14z%(py16)spy16r   u   spec 로드 실패: z
>assert %(py19)spy19)	importlibutilspec_from_file_locationCLEANUP_WS_PYloader
@pytest_ar_call_reprcompare@py_builtinslocals_should_repr_global_name	_safereprappend_format_boolop_format_assertmsgAssertionError_format_explanationmodule_from_specexec_module)r	   @py_assert1@py_assert4@py_assert3@py_assert0@py_assert10@py_assert13@py_assert12@py_format6@py_format8@py_format15@py_format17@py_format18@py_format20mods                  N/home/jay/workspace/tests/regression/test_cleanup_workspace_protection_2569.py_load_cleanup_workspacer3      s4   >>112E}UD_t_4t__4_4 7____4t______4___4___t_______4_______________4__________;OP]9_________
..
)
)$
/CKKC J    c            
         t        j                  dt        t              dgddt        t                    } | 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| j                  dd  d| j                  dd        dz   d|iz  }t        t        j                   |            dx}x}}y)uO   cleanup-workspace.py --report 실행이 returncode 0으로 성공해야 한다.python3z--reportT)capture_outputtextcwdr   ==)z2%(py2)s
{%(py2)s = %(py0)s.returncode
} == %(py5)sresult)py0r
   r   u1   cleanup-workspace.py --report 실패 (returncode=z).
stdout: Ni  z	
stderr: z
>assert %(py7)sr   )
subprocessrunstrr   	WORKSPACE
returncoder   r   r   r   r   r   r   stdoutstderrr    r!   )r<   r$   r%   r&   r+   r,   s         r2   &test_cleanup_workspace_dry_run_defaultrE   !   s*   ^^	C&
3	N	F   !  	   	  	    	    	    	 !"    <F<M<M;N O==#&' (==#&'	)   	  r4   tmp_pathc                    t               }t        |dd       | dz  dz  dz  }| dz  dz  }|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|  d      dz   d|iz  }t        t        j                  |            dx}}|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|  d      dz   d|iz  }t        t        j                  |            dx}}y)uQ   is_protected()가 task md → True, tmp/old.txt → False를 반환해야 한다._PROTECTION_CACHENmemorytasksztask-9999.mdtmpzold.txtT)is)z%(py0)s is %(py3)sresult_task)r=   py3zis_protected(z, u[   )가 False를 반환했습니다. memory/tasks/ 경로가 보호 대상이어야 합니다.z
>assert %(py5)sr   F
result_tmpuX   )가 True를 반환했습니다. tmp/old.txt는 보호 대상이 아니어야 합니다.)r3   setattris_protectedr   r   r   r   r   r   r   r    r!   )
rF   cwtask_mdold_txtrM   @py_assert2r$   @py_format4r+   rO   s
             r2   .test_cleanup_workspace_protected_paths_skippedrW   2   s   	 	"BB#T* !G+n<G*G
 //'84K ;$  ;$                  y8* -D 	D     (3J :  :                  y8* -B 	B    r4   c                 4
   t               }d}d}d}|j                  | |||       | dz  dz  dz  }|j                  } |       }|st        j                  d|       dz   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      j                         D 	cg c]#  }	|	j                         s|	j                         % }
}	t        |
      }d}||k\  }|st        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  }t        j                  d      dz   d|iz  }t        t        j                  |            dx}x}}t!        j"                  |
d         }h d}||j%                         z
  }| }|st        j                  d| d|       dz   ddt        j                         v st        j                  |      rt        j                  |      ndiz  }t        t        j                  |            d}|d   }||k(  }|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  }t        t        j                  |            dx}}|d%   }||k(  }|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  }t        t        j                  |            dx}}|d'   }||k(  }|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  }t        t        j                  |            dx}}yc c}	w ))ug   _audit_log() 호출 후 cleanup-audit.jsonl에 필수 키가 포함된 JSONL이 기록되어야 한다.zfake/path/to/file.txtskipped_protectedu    test reason — 회귀 테스트rI   logszcleanup-audit.jsonlu<   cleanup-audit.jsonl 파일이 생성되지 않았습니다: C
>assert %(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.exists
}()
}log_pathr=   r
   py4Nutf-8encoding   )>=)z0%(py3)s
{%(py3)s = %(py0)s(%(py1)s)
} >= %(py6)slenlines)r=   py1rN   py6u5   cleanup-audit.jsonl에 기록된 줄이 없습니다.z
>assert %(py8)spy8>   tsactionreason	candidateu4   cleanup-audit.jsonl 레코드에 필수 키 누락: u   . 실제 레코드: 
>assert not %(py0)sr=   missingrm   r:   )z%(py1)s == %(py3)sfake_candidate)rf   rN   zassert %(py5)sr   rk   fake_actionrl   fake_reason)r3   
_audit_logexistsr   r   r   r   r   r   r    r!   	read_text
splitlinesstriprd   r   jsonloadskeys)rF   rR   rp   rq   rr   r\   r$   r&   @py_format5linere   rU   @py_assert5r%   @py_format7@py_format9recordrequired_keysro   @py_format2r'   rV   r+   s                          r2   "test_audit_log_writes_jsonl_formatr   N   sU   	 	"B,N%K4KMM(NKE("V+.CCH??g?ggg \]e\fggggggg8ggg8ggg?ggggggggg&.&8&8'&8&J&U&U&Whd[_[e[e[gTZZ\hEhu:SS:?SSS:SSSSSS3SSS3SSSSSSuSSSuSSS:SSSSSSSSSSSSSS ZZb	"F;Mfkkm+G; ;   ?wi H#H	&               +0.0000.000000000.000.0000000(*{****{*********{***{*******(*{****{*********{***{******* is   T'Tc            	      $   t         dz  dz  dz  } | j                  } |       }|st        j                  d|        dz   dt	        j
                         v st        j                  |       rt        j                  |       ndt        j                  |      t        j                  |      dz  }t        t        j                  |            dx}}	 t        j                  | j                  d	
            }h d}|j!                         z
  }| }|st        j                  d| dt#        |j!                                      dz   ddt	        j
                         v st        j                  |      rt        j                  |      ndiz  }t        t        j                  |            d}|d   }t%        |t"              }	|	st        j                  d      dz   dt	        j
                         v st        j                  t$              rt        j                  t$              ndt        j                  |      dt	        j
                         v st        j                  t"              rt        j                  t"              ndt        j                  |	      dz  }
t        t        j                  |
            dx}}	|d   }t%        |t"              }	|	st        j                  d      dz   dt	        j
                         v st        j                  t$              rt        j                  t$              ndt        j                  |      dt	        j
                         v st        j                  t"              rt        j                  t"              ndt        j                  |	      dz  }
t        t        j                  |
            dx}}	|d   }t%        |t"              }	|	st        j                  d      dz   dt	        j
                         v st        j                  t$              rt        j                  t$              ndt        j                  |      dt	        j
                         v st        j                  t"              rt        j                  t"              ndt        j                  |	      dz  }
t        t        j                  |
            dx}}	|d   }t'        |      }d}||kD  }|st        j(                  d|fd||f      dt	        j
                         v st        j                  t&              rt        j                  t&              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}}y# t        j                  $ r#}t        j                  d|        Y d}~d}~ww xY w)$ul   memory/specs/protection-list.json이 valid JSON으로 파싱되고 필수 키가 모두 존재해야 한다.rI   specszprotection-list.jsonu   protection-list.json 없음: r[   
plist_pathr]   Nr_   r`   u)   protection-list.json JSON 파싱 실패: >   protected_filesprotected_pathspreserved_lifecycle_pathsu+   protection-list.json에 필수 키 누락: u   . 실제 키: rn   r=   ro   r   u(   protected_paths가 list가 아닙니다.z7
>assert %(py5)s
{%(py5)s = %(py0)s(%(py2)s, %(py3)s)
}
isinstancelist)r=   r
   rN   r   r   u(   protected_files가 list가 아닙니다.r   u2   preserved_lifecycle_paths가 list가 아닙니다.r   )>)z/%(py4)s
{%(py4)s = %(py0)s(%(py2)s)
} > %(py7)srd   )r=   r
   r^   r   u'   protected_paths가 비어 있습니다.z
>assert %(py9)sr   )rA   rt   r   r   r   r   r   r   r    r!   rx   ry   ru   JSONDecodeErrorpytestfailrz   r   r   rd   r   )r   r$   r&   r{   dataer   ro   r   r%   r+   @py_assert6r}   r,   @py_format10s                  r2   "test_protection_list_json_loads_okr   o   s	   X%/2HHJLLLL"?
| LLLLLLL:LLL:LLLLLLLLLLLLEzz*...@A XMdiik)G; ;   6gY ?DIIK()	+               ,-`:-t4`4``6```````:```:```-``````t```t```4``````,-`:-t4`4``6```````:```:```-``````t```t```4``````67 :7> >   	=             8      :>    :>    ?      %&V3&'V!V'!+VVV'!VVVVVV3VVV3VVV&VVV'VVV!VVV-VVVVVVVV!  E?sCDDEs   %W X,X

X)__doc__builtinsr   _pytest.assertion.rewrite	assertionrewriter   importlib.utilr   rx   r>   pathlibr   r   __file__resolveparentsrA   r   r3   rE   rW   r   r    r4   r2   <module>r      sv          N""$,,Q/	I%(>>
"T 8+ +BWr4   