
    1j                     f   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mZ  ee      j                         j                   d   Z ee      Zeej(                  v rej(                  j+                  e       ej(                  j-                  de        eej0                  j3                               D ]U  Zedk(  sej7                  d      s eej0                  e   dd	      xs d	Zeevs:ej0                  j=                  ed       W ed
z  Zd Z  G d d      Z! G d d      Z" G d d      Z#y)u  Top-level test: helper.selftest() + dispatch.py shim re-export verification.

task-2646 E10 — verifies (a) utils.callback_registration.selftest() passes and
(b) the dispatch.py SHIM file (NOT the dispatch/ package) re-exports the
single-helper symbols, satisfying the "dispatch.py path uses the same helper"
결선 contract.

Notes
-----
* Python resolves ``import dispatch`` to the package ``dispatch/__init__.py``
  rather than the ``dispatch.py`` shim when both coexist, so this test loads
  the shim explicitly via ``importlib.util.spec_from_file_location`` to assert
  the shim itself carries the wiring.
* The worktree root must be first on ``sys.path`` so that
  ``utils.callback_registration`` (new task-2646 module) resolves from the
  worktree and not from the live workspace which lacks it. We inject this at
  file load time because ``tests/conftest.py`` ahead-of-us inserts the LIVE
  workspace path; we must precede it.
    )annotationsN)Path   utilszutils.__file__ zdispatch.pyc                 N   t        t        dd      } | | S t        j                  j	                  dt        t                    }g }d}||u}|}|r|j                  }d}||u}|}|slt        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  }dd|iz  }t!        t        j"                  |            dx}x}x}x}x}x}}t        j                  j%                  |      }|j                  j'                  |       |t        _        |S )zLoad the dispatch.py SHIM file explicitly (not the dispatch/ package).

    Returns the loaded module object. Cached on the function attribute.
    _cachedN_task2646_dispatch_shim)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   zassert %(py19)spy19)getattr_load_dispatch_shim_module	importlibutilspec_from_file_locationstr_DISPATCH_PY_SHIMloader
@pytest_ar_call_reprcompare@py_builtinslocals_should_repr_global_name	_safereprappend_format_boolopAssertionError_format_explanationmodule_from_specexec_moduler
   )cachedr   @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                   a/home/jay/workspace/.worktrees/task-2646-dev3/tests/test_callback_registration_helper_selftest.pyr   r   .   sL   
 /DAF>>11!3'8#9D 8t74t77474 777774t77777747774777t77777774777777777777777477777777777777
..
)
)$
/CKKC ),&J    c                  (    e Zd ZdZd Zd Zd Zd Zy)TestHelperSelftestz0helper.selftest() must pass all internal checks.c                    ddl m}  |       }|d   }|sTt        j                  d|d         dz   dt        j                  |      iz  }t        t        j                  |            d }y )Nr   selftestokzselftest() failed with failuresz
>assert %(py1)spy1)utils.callback_registrationr?   r   _format_assertmsgr#   r&   r'   )selfr?   resultr.   @py_format2s        r9   test_selftest_passesz'TestHelperSelftest.test_selftest_passesC   sK    8d|M|MM6vj7I6LMMMM|MMMMMMr:   c                t   ddl m}m}  |       }|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 )
Nr   )r?   HELPER_SCHEMAschema==)z%(py1)s == %(py3)srJ   rB   py3assert %(py5)sr   )rC   r?   rJ   r   r   r#   r    r!   r"   r&   r'   )rE   r?   rJ   rF   r.   @py_assert2@py_format4r2   s           r9   test_selftest_returns_schemaz/TestHelperSelftest.test_selftest_returns_schemaH   sm    Gh0=0000=000000000=000=0000000r:   c                V   ddl m}  |       }|d   }g }||k(  }|st        j                  d|fd||f      t        j                  |      t        j                  |      dz  }t        j
                  d|d          dz   d	|iz  }t        t        j                  |            d x}x}}y )
Nr   r>   rA   rL   )z%(py1)s == %(py4)srB   py4zselftest failures: z
>assert %(py6)spy6)rC   r?   r   r   r#   rD   r&   r'   rE   r?   rF   r.   r-   rQ   @py_format5@py_format7s           r9   test_selftest_no_failuresz,TestHelperSelftest.test_selftest_no_failuresM   s    8j! 	
R 	
!R' 	
 	
!R 	
 	
 		 " 	
 	
 		 &( 	
 	
  "&"4!56	
 	
 	
 	
 	
 	
r:   c                   ddl m}  |       }|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 )
Nr   r>   	tests_run   )>=)z%(py1)s >= %(py4)srU   assert %(py6)srW   )rC   r?   r   r   r#   r&   r'   rX   s           r9   test_selftest_tests_run_countz0TestHelperSelftest.test_selftest_tests_run_countT   s^    8k"'a'"a''''"a'''"'''a'''''''r:   N)__name__
__module____qualname____doc__rH   rS   r[   ra    r:   r9   r<   r<   @   s    :N
1

(r:   r<   c                  :    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
y	)
TestDispatchPyShimReExportu"  dispatch.py SHIM file must re-export the helper (task-2646 ANCHOR-2).

    These tests assert the wiring on the SHIM file specifically — ``import
    dispatch`` resolves to the package and would mask a missing shim wiring.
    We therefore load the shim file directly with importlib.
    c                   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  }d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  }d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  }dd
|iz  }t        t        j                  |            dx}}y)zDText-grep proof: dispatch.py SHIM source contains the helper import.zutf-8)encodingz'from utils.callback_registration import)in)z%(py1)s in %(py3)ssrcrN   zQdispatch.py SHIM must contain the helper import line (task-2646 ANCHOR-2 wiring).z
>assert %(py5)sr   Nz1register_callback as register_callback_via_helperrP   z5verify_actual_owner as verify_actual_owner_via_helperz4HELPER_SCHEMA as CALLBACK_REGISTRATION_HELPER_SCHEMA)r   	read_textr   r   r#   r    r!   r"   rD   r&   r'   )rE   rl   r.   rQ   rR   r2   s         r9   1test_dispatch_py_shim_file_has_helper_import_textzLTestDispatchPyShimReExport.test_dispatch_py_shim_file_has_helper_import_textb   s   ))7);8 	
8C? 	
 	
8C 	
 	
 
	 9 	
 	
 
6	
 	
  =@ 	
 	
 
	 =@ 	
 	
 +	
 	
 	
 	
 	
 CIBcIIIIBcIIIBIIIIIIcIIIcIIIIIIIFMF#MMMMF#MMMFMMMMMM#MMM#MMMMMMMELELLLLELLLELLLLLLLLLLLLLLLLr:   c                   t               }d}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                  |      t        j                  |      dz  }t        t        j                  |            d x}}y )Nregister_callback_via_helperz<dispatch.py SHIM must re-export register_callback_via_helper7
>assert %(py5)s
{%(py5)s = %(py0)s(%(py1)s, %(py3)s)
}hasattrshimpy0rB   rO   r   
r   rr   r   rD   r    r!   r"   r#   r&   r'   rE   rs   rQ   r,   r2   s        r9   >test_dispatch_shim_module_exposes_register_callback_via_helperzYTestDispatchPyShimReExport.test_dispatch_shim_module_exposes_register_callback_via_helperm   s    )+; 	
wt;< 	
< 	
  K	
 	
	6	
 	
   	
 	
 		  	
 	
	6	
 	
   	
 	
 		  	
 	
 		 < 	
 	
 		 = 	
 	
 	
 	
 	
r:   c                   t               }d}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                  |      t        j                  |      dz  }t        t        j                  |            d x}}y )Nverify_actual_owner_via_helperz>dispatch.py SHIM must re-export verify_actual_owner_via_helperrq   rr   rs   rt   rv   rw   s        r9   @test_dispatch_shim_module_exposes_verify_actual_owner_via_helperz[TestDispatchPyShimReExport.test_dispatch_shim_module_exposes_verify_actual_owner_via_helpers   s    )+= 	
wt=> 	
> 	
  M	
 	
	6	
 	
   	
 	
 		  	
 	
	6	
 	
   	
 	
 		  	
 	
 		 > 	
 	
 		 ? 	
 	
 	
 	
 	
r:   c                   t               }d}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                  |      t        j                  |      dz  }t        t        j                  |            d x}}y )N#CALLBACK_REGISTRATION_HELPER_SCHEMAzCdispatch.py SHIM must re-export CALLBACK_REGISTRATION_HELPER_SCHEMArq   rr   rs   rt   rv   rw   s        r9   /test_dispatch_shim_module_exposes_helper_schemazJTestDispatchPyShimReExport.test_dispatch_shim_module_exposes_helper_schemay   s    )+B 	
wtBC 	
C 	
  R	
 	
	6	
 	
   	
 	
 		  	
 	
	6	
 	
   	
 	
 		  	
 	
 		 C 	
 	
 		 D 	
 	
 	
 	
 	
r:   c                   ddl m} t               }|j                  }||k(  }|st	        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dz  }dd	|iz  }t        t	        j                  |            d x}}y )
Nr   rJ   rL   )zK%(py2)s
{%(py2)s = %(py0)s.CALLBACK_REGISTRATION_HELPER_SCHEMA
} == %(py4)srs   rJ   )ru   r   rV   r`   rW   )rC   rJ   r   r}   r   r   r    r!   r"   r#   r&   r'   )rE   rJ   rs   r+   r-   rY   rZ   s          r9   .test_dispatch_shim_helper_schema_value_matcheszITestDispatchPyShimReExport.test_dispatch_shim_helper_schema_value_matches   s    =)+77H7=HHHH7=HHHHHHtHHHtHHH7HHHHHH=HHH=HHHHHHHr:   c                   t               }|j                  }t        |      }|sd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        t        j                  |            d x}}y )Nz^assert %(py5)s
{%(py5)s = %(py0)s(%(py3)s
{%(py3)s = %(py1)s.register_callback_via_helper
})
}callablers   rt   )
r   rp   r   r    r!   r   r"   r#   r&   r'   rw   s        r9   8test_dispatch_shim_register_callback_via_helper_callablezSTestDispatchPyShimReExport.test_dispatch_shim_register_callback_via_helper_callable   s    )+99:x9::::::::x:::x::::::::::::9::::::::::r:   c                   t               }|j                  }t        |      }|sd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        t        j                  |            d x}}y )Nz`assert %(py5)s
{%(py5)s = %(py0)s(%(py3)s
{%(py3)s = %(py1)s.verify_actual_owner_via_helper
})
}r   rs   rt   )
r   rz   r   r    r!   r   r"   r#   r&   r'   rw   s        r9   :test_dispatch_shim_verify_actual_owner_via_helper_callablezUTestDispatchPyShimReExport.test_dispatch_shim_verify_actual_owner_via_helper_callable   s    )+;;<x;<<<<<<<<x<<<x<<<<<<<<<<<<;<<<<<<<<<<r:   N)rb   rc   rd   re   rn   rx   r{   r~   r   r   r   rf   r:   r9   rh   rh   Z   s,    	M


I
;=r:   rh   c                      e Zd ZdZd Zd Zy)TestHelperSchemaConstantz$HELPER_SCHEMA and ANU_KEY constants.c                \   ddl m} d}||k(  }|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}}y )
Nr   r   zutils.callback_registration.v1rL   z%(py0)s == %(py3)srJ   ru   rO   rP   r   )
rC   rJ   r   r   r    r!   r"   r#   r&   r'   )rE   rJ   rQ   r+   rR   r2   s         r9   test_helper_schema_valuez1TestHelperSchemaConstant.test_helper_schema_value   sa    = @@} @@@@@} @@@@@@@}@@@}@@@ @@@@@@@@r:   c                \   ddl m} d}||k(  }|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}}y )
Nr   )ANU_KEYc119085addb0f8b7rL   r   r   r   rP   r   )
rC   r   r   r   r    r!   r"   r#   r&   r'   )rE   r   rQ   r+   rR   r2   s         r9   test_anu_key_constantz.TestHelperSchemaConstant.test_anu_key_constant   sa    7,,w,,,,,w,,,,,,,w,,,w,,,,,,,,,,,r:   N)rb   rc   rd   re   r   r   rf   r:   r9   r   r      s    .A-r:   r   )$re   
__future__r   builtinsr    _pytest.assertion.rewrite	assertionrewriter   importlib.utilr   syspathlibr   r   resolveparents_WORKTREE_ROOTr   _WORKTREE_ROOT_STRpathremoveinsertlistmoduleskeys	_mod_name
startswithr   _utils_filepopr   r   r<   rh   r   rf   r:   r9   <module>r      s  & #     
  h'')11!4( !HHOO&' % & ckk&&() -IGy33H=ckk)4j"EK[0KKOOIt,	- #]2 $( (40= 0=f	- 	-r:   