
    j5                        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  e ee      j!                         j"                  d         Zej&                  rWej&                  d   ek7  rEeej&                  v rej&                  j)                  e       ej&                  j+                  de        eej.                        D ]U  Zedk(  sej3                  d      s eej.                  e   dd	      xs d	Zeevs:ej.                  j9                  ed       W dd
lmZ ddlm Z m!Z! ddl"m#Z#m$Z$ ddl%m&Z&m'Z'm(Z(  ee      j!                         j"                  d   dz  dz  dz  Z)d Z*d Z+d Z,d Z-d Z.d Z/d Z0y)u=  tests/regression/test_callback_vs_sendfile_separation.py

task-2635 — ANCHOR-3 sendfile/report 전송 ≠ callback 단언.

Spec §5: sendfile_only 만으로는 callback 충족 안 됨 단언 · envelope sendfile
함수 호출 ≠ cron 등록 함수 호출 (별개 함수) · 두 함수 시그니처/책임 분리.
    )annotationsN)Path   dispatchz	dispatch.__file__ )send_envelope_to_chat)decide_fallback_cancelexpected_collector_spawn)build_callback_enveloperegister_normal_callback)DeliveryMethod NormalCallbackRegistrationStatusis_callback_complete   fixturesnormal_callback_registrationsendfile_only_no_cronc                 6   t         t        u} | st        j                  d| fdt         t        f      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dz  }dd|iz  }t        t        j                  |            d } t         j                  } t        j                  }| |k7  }|st        j                  d|fd	| |f      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  }dd|iz  }t        t        j                  |            d x} x}}y )N)is not)z%(py0)s is not %(py2)sr	   r   )py0py2zassert %(py4)spy4)!=)zR%(py2)s
{%(py2)s = %(py0)s.__module__
} != %(py6)s
{%(py6)s = %(py4)s.__module__
})r   r   r   py6assert %(py8)spy8)r	   r   
@pytest_ar_call_reprcompare@py_builtinslocals_should_repr_global_name	_safereprAssertionError_format_explanation
__module__)@py_assert1@py_format3@py_format5@py_assert5@py_assert3@py_format7@py_format9s          L/home/jay/workspace/tests/regression/test_callback_vs_sendfile_separation.py>test_send_envelope_to_chat_is_distinct_function_from_registrarr/   8   s    (@@@@@ (@@@@@@@ @@@ @@@@@@(@@@@(@@@@@@@@ ++R/G/R/RR+/RRRRR+/RRRRRRR RRR RRR+RRRRRR/GRRR/GRRR/RRRRRRRR    c                    t        j                  t              } t        | j                  j                               }h d}|j                  } ||      }|st        j                  d||z         dz   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z  }t        t        j                  |            d x}}ddh}|j                  } ||      }|sd	t        j                  |      t        j                  |      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      d
z  }	t        t        j                  |	            d x}x}}y )N>   anu_keyschedule_iddelay_secondscron_schedule_idz.sendfile signature leaks registration params: zN
>assert %(py5)s
{%(py5)s = %(py2)s
{%(py2)s = %(py0)s.isdisjoint
}(%(py3)s)
}params	forbidden)r   r   py3py5envelopechat_idJassert %(py6)s
{%(py6)s = %(py3)s
{%(py3)s = %(py1)s.issubset
}(%(py4)s)
}py1r8   r   r   )inspect	signaturer	   set
parameterskeys
isdisjointr   _format_assertmsgr    r!   r"   r#   r$   r%   issubset)
sigr6   r7   r'   @py_assert4@py_format6@py_assert0@py_assert2r*   r,   s
             r.   Ktest_send_envelope_to_chat_signature_does_not_take_anu_key_or_schedule_argsrL   ?   s   


1
2C$$&'FOI Y' '   9)9K8LM                   '    '    (      	"3"++3+F33333"333+333333F333F3333333333r0   c                    t        j                  t              } t        | j                  j                               }h d}|j                  } ||      }|sdt        j                  |      t        j                  |      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      dz  }t        t        j                  |            d x}x}}y )N>   r2   r:   r4   r<   r6   r=   )r?   r@   r   rA   rB   rC   rF   r   r#   r    r!   r"   r$   r%   )rG   r6   rJ   rK   r*   r,   s         r.   ?test_register_normal_callback_signature_takes_anu_key_and_delayrN   K   s    


4
5C$$&'F3D3<<D<VDDDDD3DDD<DDDDDDVDDDVDDDDDDDDDDr0   c                 R   dddddd} t        d|       }t        |      }t        |d	      }|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}||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   }t        j                  }	|	j                  }
||
k(  }|st        j                  d|fd||
f      t        j
                  |      dt        j                         v st        j                  t              rt        j
                  t              ndt        j
                  |	      t        j
                  |
      dz  }dd|iz  }t        t        j                  |            dx}x}x}	}
y)zDCalling send_envelope_to_chat must NEVER mutate registration_status.zdispatch-executor-dev6z&memory/tasks/task-2635-sep.result.jsonzmemory/reports/task-2635-sep.md(0000000000000000000000000000000000000000@0fbd1dad1e110c49474dfbdf13a21fb3bdd9c7f094128004dba8472840bb832d)executor_nameresult_pathreport_path
commit_shaspec_sha256ztask-2635-sep
6937032012)r;   is_callback_substituteFisz%(py1)s is %(py4)sr>   r   assert %(py6)sr   Ndelivered_to==z%(py1)s == %(py4)sregistration_status)zQ%(py1)s == %(py7)s
{%(py7)s = %(py5)s
{%(py5)s = %(py3)s.NOT_REGISTERED
}.value
}r   r>   r8   r9   py7assert %(py9)spy9)r   dictr	   r   r   r#   r$   r%   r   NOT_REGISTEREDvaluer    r!   r"   )resultenvbeforeauditrJ   r+   rK   r)   r,   rH   @py_assert6@py_format8@py_format10s                r.   6test_sendfile_call_does_not_change_registration_statusrq   R   s    2?8YF "/6
:C#YF "#|<E)*3e3*e3333*e333*333e3333333 0L0 L0000 L000 000L0000000 $%F0E)FF%)FFFFF%)FFFF%FFF)FFFFFFFF$%^)I)X)X^)X)^)^^%)^^^^^%)^^^^%^^^^^^)I^^^)I^^^)X^^^)^^^^^^^^r0   c                    t        j                  t        dz  j                  d            } t        j                  t        dz  j                  d            }| d   }t        j
                  }|j                  }||k(  }|st        j                  d|fd||f      t        j                  |      dt        j                         v st        j                  t              rt        j                  t              ndt        j                  |      t        j                  |      d	z  }d
d|iz  }t        t        j                  |            d x}x}x}}| d   }t        j
                  }|j                  }||k(  }|st        j                  d|fd||f      t        j                  |      dt        j                         v st        j                  t              rt        j                  t              ndt        j                  |      t        j                  |      d	z  }d
d|iz  }t        t        j                  |            d x}x}x}}t!        |       }d}||u }|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%        |       }d}||u }|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}}t'        |       }|j(                  }d}||u }|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      dz   d|iz  }t        t        j                  |            d x}x}}|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}}y )$Nzevidence.jsonzutf-8)encodingzexpected.jsonrb   r_   )zP%(py1)s == %(py7)s
{%(py7)s = %(py5)s
{%(py5)s = %(py3)s.SENDFILE_ONLY
}.value
}r   rc   re   rf   delivery_methodr   FrY   )z0%(py3)s
{%(py3)s = %(py0)s(%(py1)s)
} is %(py6)sr   evidencer   r>   r8   r   zDsendfile_only fixture must NOT count as callback complete (ANCHOR-3)z
>assert %(py8)sr   r   r   )z7%(py2)s
{%(py2)s = %(py0)s.cancel_fallback
} is %(py5)sdecision)r   r   r9   z3sendfile_only must NEVER cancel fallback safety-netz
>assert %(py7)srd   callback_substitute_by_sendfiler[   r\   r]   r   )jsonloadsSENDFILE_FIXTURE	read_textr   SENDFILE_ONLYri   r   r   r#   r    r!   r"   r$   r%   r   r   rE   r   r
   cancel_fallback)ru   expectedrJ   rH   rn   rK   ro   rp   r*   r,   r-   rw   r'   r+   rI   r)   s                   r.   3test_sendfile_only_fixture_is_not_callback_completer   h   s   zz+o=HHRYHZ[Hzz+o=HHRYHZ[H)*b.N.\.\b.\.b.bb*.bbbbb*.bbbb*bbbbbb.Nbbb.Nbbb.\bbb.bbbbbbbb%&L.*F*FL*F*L*LL&*LLLLL&*LLLL&LLLLLL.LLL.LLL*FLLL*LLLLLLLL) U )U2   )U                  !)    !)    *    .3    	O     $H-66-6666-666666#666#666666H666H666-6666666666%h/H## u #u,  #u              $    (-    	>     56?%?6%????6%???6???%???????r0   c                 X  
 g 

fd} t        ddid|       }|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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}}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 )Nc                F    j                  t        |       |f       d|dS )NTokchat)appendrg   )rk   cidrunner_calleds     r.   	my_runnerzFtest_sendfile_with_custom_runner_returns_audit_only.<locals>.my_runner~   s%    d3i-.C((r0   task_idxrW   )sendfile_runnerrX   FrY   r[   r\   r]   r   outcomeTr   r_   ra   r   )z0%(py3)s
{%(py3)s = %(py0)s(%(py1)s)
} == %(py6)slenr   rv   r   r   )
r	   r   r   r#   r$   r%   r   r    r!   r"   )r   rm   rJ   r+   rK   r)   r,   r*   rH   r-   r   s             @r.   3test_sendfile_with_custom_runner_returns_audit_onlyr   {   s`   M) "9c"2LR[\E)*3e3*e3333*e333*333e3333333AdLAAAAAAAAAAAAAAAAAAAAAA}""""""""""""3"""3""""""}"""}"""""""""""""r0   c                    ddl } ddl}t        j                  t              }| j                  |j                  |            }dh}g }| j                  |      D ]  }t        || j                        s|j                  }t        || j                        r|j                  |j                         Xt        || j                        so|j                  |j                          t!        t#        |      |z        }| }	|	st%        j&                  d| d      dz   ddt)        j*                         v st%        j,                  |      rt%        j.                  |      ndiz  }
t1        t%        j2                  |
            d}	d	}||v}|st%        j4                  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  }t1        t%        j2                  |            dx}}t	        ddid      }|d   }d}||u }|slt%        j4                  d|fd||f      t%        j.                  |      t%        j.                  |      dz  }dd|iz  }t1        t%        j2                  |            dx}x}}y)a  sendfile must not invoke the registrar (ANCHOR-3 separation).

    We parse the AST of ``send_envelope_to_chat`` and check there is no
    Call node whose callable resolves to ``register_normal_callback``.
    A textual mention in the docstring (educational note) is allowed.
    r   Nr   z:send_envelope_to_chat calls forbidden registrar functions u"    — ANCHOR-3 separation violated.z
>assert not %(py0)sr   leakedz--cron)not in)z%(py1)s not in %(py3)ssrc)r>   r8   z$sendfile must not assemble cron argsz
>assert %(py5)sr9   r   r   rW   rX   FrY   r[   r\   r]   r   )asttextwrapr?   	getsourcer	   parsededentwalk
isinstanceCallfuncNamer   id	AttributeattrsortedrA   r   rE   r    r!   r"   r#   r$   r%   r   )r   r   r   treer7   called_namesnodecalleer   r'   @py_format2rJ   rK   @py_format4rI   rm   r+   r)   r,   s                      r.   Ctest_no_call_to_register_normal_callback_from_send_envelope_to_chatr      s    


1
2C99X__S)*D+,I L 1dCHH%YYF&#((+##FII.FCMM2##FKK01 C%	12F: :   EVH M( 	(               F83FFF83FFF8FFFFFF3FFF3FFFF FFFFFFF "9c"2LAE)*3e3*e3333*e333*333e3333333r0   )1__doc__
__future__r   builtinsr    _pytest.assertion.rewrite	assertionrewriter   r?   ry   syspathlibr   strr   resolveparents_WORKTREE_ROOTpathremoveinsertlistmodules_mod
startswithgetattr_cachedpopdispatch.finalize_hooksr	   utils.anu_callback_fallbackr
   r   utils.anu_callback_registrarr   r   utils.callback_envelope_schemar   r   r   r{   r/   rL   rN   rq   r   r   r    r0   r.   <module>r      sd   #     
  T(^++-55a8988~-!'HHOOA~& (DzT__[9#++d+Z<B(KKOOD$'	( :  	N$$Q'$%  S	4E_,@&
# 4r0   