
    0_j                    `   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
mZ ddlmZ ddlZ ee      j!                         j"                  d   Ze	j&                  j)                  d ee             e	j&                  j)                  d eedz               ddlZddZdd	Zd
 Zd Zd Zd Zd Zy)u  
anu_v2/tests/test_g4_integration_2562.py — task-2562 G4 통합 시나리오

회장 §명시 통합 검증:
- G3 PASS 직후 G4 호출 흐름 (finish-task.sh line 830 부근 hook)
- Lv.3+ hard 분기: action=PR_OPEN_BLOCKED → .g4-failed marker
- fix_loop counter 누적 후 cap
- scope_violation 즉시 ESCALATED (fix_loop 0)
- OAuth-personal 강제 (GEMINI_API_KEY 거부 어셀션)
    )annotationsN)Path)patch   scriptsc                t    | dz  }|dz  dz  j                  dd       |dz  dz  j                  dd       |S )NwsmemoryeventsT)parentsexist_oktasks)mkdir)tmp_pathr	   s     </home/jay/workspace/anu_v2/tests/test_g4_integration_2562.py_make_workspacer      sH    	DB(]X$$TD$A(]W##D4#@I    c                V    | dz  dz  | dz  }|j                  d| d| dd       |S )	Nr
   r   z.mdz# u   

## 레벨

utf-8encoding)
write_text)r	   task_idlevelps       r   _write_task_mdr   "   s?    
XWIS/1ALL2gYoeWB7'LJHr   c           	     2   | j                  dd       t        |      }t        |dd      }dddgg d	}t        j                  t
        d
|d f      5  t        j                  t        |      dgt        |      d      }d d d        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}}|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}}d}||z  }d}||z  }d}||z  }|j                  } |       }|sddt        j                         v st        j                   |      rt        j                  |      ndt        j                  |      t        j                  |      t        j                  |      t        j                  |      t        j                  |      dz  }t        t        j                  |            d x}x}x}x}x}x}x}}y # 1 sw Y   xY w)NGEMINI_API_KEYFraisingztask-2562-int-1Lv.3+criticalzctrl-plane riskseveritydescriptionriskssuggestions_run_gemini_clireturn_valuezx.py	task_fileaffected_filesworkspace_rootr   r   lv3plus==z%(py1)s == %(py4)spy1py4assert %(py6)spy6actionPR_OPEN_BLOCKEDr
   r   ztask-2562-int-1.g4-failediassert %(py13)s
{%(py13)s = %(py11)s
{%(py11)s = (((%(py0)s / %(py2)s) / %(py5)s) / %(py8)s).exists
}()
}r	   py0py2py5py8py11py13delenvr   r   r   objectg4gemini_cli_gate_checkstr
@pytest_ar_call_reprcompare	_safereprAssertionError_format_explanationexists@py_builtinslocals_should_repr_global_name)monkeypatchr   r	   r.   fakeres@py_assert0@py_assert3@py_assert2@py_format5@py_format7@py_assert1@py_assert4@py_assert6@py_assert7@py_assert9@py_assert10@py_assert12@py_format14s                      r   /test_lv3plus_hard_fail_creates_g4_failed_markerrc   +   s   '7		"Br#4g>I  *:KLMD 
b+4,	G 
&&)n"8r7%	

 w<$9$<9$$$$<9$$$<$$$9$$$$$$$x=---=-----=----=-----------LBMLHLMH$L'BL$'BBLBJJLJLLLLLLLLBLLLBLLLLLLHLLL'BLLLJLLLLLLLLLLL
 
s   -JJc           	        | j                  dd       t        |      }t        |dd      }dddgg d	}t        j                  t
        d
|d f      5  t        j                  t        |      g t        |      d      }t        j                  t        |      g t        |      d      }t        j                  t        |      g t        |      d      }d d d        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}
}	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}
}	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}
}	|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}
}	d}||z  }	d}|	|z  }d}||z  }|j                  } |       }|sddt        j                         v st        j                   |      rt        j                  |      ndt        j                  |      t        j                  |      t        j                  |      t        j                  |      t        j                  |      dz  }t        t        j                  |            d x}x}	x}x}x}x}x}}y # 1 sw Y   xY w)Nr   Fr    ztask-2562-int-2r"   r#   xr$   r'   r*   r+   r-   fix_loop_count   r2   r4   r5   r8   r9   r   r:   ESCALATED_OWNER_DECISIONgate_triggerfix_loop_capr
   r   z$task-2562-int-2.g4-fix-loop-cap.jsonr<   r	   r=   rD   )rS   r   r	   r.   rT   r1r2r3rV   rW   rX   rY   rZ   r[   r\   r]   r^   r_   r`   ra   rb   s                        r   *test_fix_loop_counter_increments_then_capsrn   C   s   '7		"Br#4g>I  *#>?D 
b+4,	G 
%%)nr7%	
 %%)nr7%	
 %%)nr7%	

& $1$1$$$$1$$$$$$1$$$$$$$$1$1$$$$1$$$$$$1$$$$$$$h<555<55555<5555<55555555555n///////////////////WBMWHWMH$W'MW$'MMWMUUWUWWWWWWWWBWWWBWWWWWWHWWW'MWWWUWWWWWWWWWWW1
 
s   BO!!O+c           	     6   | j                  dd       t        |      }t        |dd      }t        j                  t        |      ddgt        |      ddg      }|d	   }d
}||u }|slt        j                  d|fd||f      t        j                  |      t        j                  |      dz  }dd|iz  }	t        t        j                  |	            d x}x}}d}|d   }||v }|slt        j                  d|fd||f      t        j                  |      t        j                  |      dz  }dd|iz  }	t        t        j                  |	            d x}x}}|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}}|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}}|dz  dz  dz  }
|
j                         r%|
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                  |      t        j                  |      t        j                  |      dz  }d d!|iz  }t        t        j                  |            d x}x}x}x}x}}y y )"Nr   Fr    ztask-2562-int-3r"   za.pyzout_of_scope.py)r.   r/   r0   r   expected_filesscope_violationT)is)z%(py1)s is %(py4)sr5   r8   r9   scope_violation_extrasin)z%(py1)s in %(py4)sr:   rh   r2   r4   rf   r   r
   r   z!task-2562-int-3.g4-fix-loop-count0)z{%(py8)s
{%(py8)s = %(py6)s
{%(py6)s = %(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.read_text
}()
}.strip
}()
} == %(py11)scounter)r>   r?   r7   r9   rA   rB   zassert %(py13)srC   )rE   r   r   rG   rH   rI   rJ   rK   rL   rM   rN   rO   	read_textstriprP   rQ   rR   )rS   r   r	   r.   rU   rV   rW   rX   rY   rZ   rw   r[   @py_assert5r^   r`   r_   @py_format12rb   s                     r   ,test_scope_violation_no_fix_loop_progressionr|   j   s   '7		"Br#4g>I

"
"i. 122w!xC  !)T)!T))))!T)))!)))T)))))))=$< == ===== ======= ========x=666=66666=6666=66666666666 %A% A%%%% A%%% %%%A%%%%%%%8mh&)LLG~~  1 "1"((1(*1c1*c1111*c111111w111w111 111"111(111*111c11111111 r   c                p   | j                  dd       t        |      }t        |dd      }t        j                  t
              5 }t        j                  t        |      g t        |      d       d d d        d}j                  }t        |      }||v }|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                  |      rt        j                  |      nd
t        j                  |      t        j                  |      dz  }	dd|	iz  }
t!        t        j"                  |
            d x}x}x}}y # 1 sw Y   =xY w)Nr   zleak-from-ciztask-2562-int-4r"   r-   FORBIDDEN_CAPABILITY_USErt   )zK%(py1)s in %(py8)s
{%(py8)s = %(py3)s(%(py6)s
{%(py6)s = %(py4)s.value
})
}rI   exc)r6   py3r7   r9   rA   zassert %(py10)spy10)setenvr   r   pytestraisesRuntimeErrorrG   rH   rI   valuerJ   rK   rL   rP   rQ   rR   rM   rN   )rS   r   r	   r.   r   rV   rz   r^   rX   @py_format9@py_format11s              r   +test_oauth_personal_enforced_in_integrationr      s   '8		"Br#4g>I	|	$ 

  )nr7%		

 &7SYY7Y7%7777%777%777777777777777S777S777Y7777777777
 
s   ,F++F5c                 	   t         dz  dz  } | j                         st        j                  d       | 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}}|j                  d      }|j                  d      }|j                  d      }g }	d}
|
 }||k7  }|}|rd}| }||k7  }|}|rd}| }||k7  }|}|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                  |       |rt        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                        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}x}x}x}}||k  }	||k  }|	r|st        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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   
    finish-task.sh line 830 부근에 [G4-GATE] hook이 존재하는지 정적 검증.
    회장 §명시: G3 PASS 직후, Lv.4 보안 감사 이전.
    r   zfinish-task.shz(finish-task.sh not in this repo snapshotr   r   z	[G4-GATE]rt   )z%(py1)s in %(py3)scontent)r6   r   zG4-GATE hook missingz
>assert %(py5)sr@   Nz[G3-GATE] PASSu   Lv.4 보안 감사 검증rg   )!=)z%(py2)s != -%(py5)sidx_g3)r?   r@   z%(py8)srA   )z%(py10)s != -%(py13)sidx_g4)r   rC   z%(py16)spy16)z%(py18)s != -%(py21)sidx_lv4)py18py21z%(py24)spy24r   zassert %(py27)spy27)<r   )z%(py0)s < %(py3)sz%(py3)s < %(py4)s)r>   r   r7   u%   G3 PASS → G4 → Lv.4 순서 위배z
>assert %(py6)sr9   )ROOTrO   r   skiprx   rJ   rK   rL   rP   rQ   rR   _format_assertmsgrM   rN   findappend_format_boolop)script_pathr   rV   rX   @py_format4@py_format6r   r   r   r[   r\   r]   rW   ra   @py_assert14@py_assert11@py_assert20@py_assert22@py_assert19rZ   r   @py_format15@py_format17@py_format23@py_format25@py_format26@py_format28rY   s                               r   #test_finish_task_sh_g4_hook_presentr      s   
 "%55K>?##W#5G9;'!999;'999;999999'999'9999#9999999\\*+F\\+&Fll67G:a:aR:6R<:q:qb:FbL:::W]::::6R::::::6:::6:::a:::::::Fb::::::F:::F:::q:::::::W::::::W:::W::::::::::::::::::F$MFW$MMMM6FWMMMMMM6MMM6MMMMMMFMMMFMMMMMMWMMMWMMMM&MMMMMMMr   )r   r   returnr   )r	   r   r   rI   r   rI   r   r   )__doc__
__future__r   builtinsrP   _pytest.assertion.rewrite	assertionrewriterJ   syspathlibr   unittest.mockr   r   __file__resolver   r   pathinsertrI   rH   rG   r   r   rc   rn   r|   r   r    r   r   <module>r      s   	 #   
   H~''* 3t9  3ti'( ) "M0!XN248$Nr   