
    i*                         d 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
mZmZmZ ej                  j                  dd       d Zd Zd Zd Zd	 Zd
 Zd Zd Zedk(  r e        yy)u@  
Tests for Story 2.02 (Track B): interceptor_jit_hydration — Master Function

BB1: After hydration, task_payload["system_injection"] present and contains "<ZERO_AMNESIA_STATE>"
BB2: On Redis timeout → hydration still completes with fallback content
BB3: Cold start (empty memory) → envelope has fallback strings
BB4: Original payload fields preserved after hydration

WB1: fast_extract called before scatter_gather_memory (verify call order via mock)
WB2: scatter_gather_memory called with timeout_ms=45
WB3: build_envelope called with MemoryContext from scatter_gather
    N)	AsyncMock	MagicMockpatchcallz/mnt/e/genesis-systemc                  \   ddl m}  ddlm}  |dddd      }ddd	}t	        d
t        |            5  t        j                   | |            }dd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   }	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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  }t        j                  d      dz   d|iz  }t        t        j                   |            dx}}t#        d       y# 1 sw Y   RxY w)zaBB1: After hydration, task_payload['system_injection'] present and contains <ZERO_AMNESIA_STATE>.r   interceptor_jit_hydrationMemoryContextNg      @working_statekg_topologylearned_constraints
latency_mszbb1-testz:Fix the redis connection in core/memory/redis_l1_schema.pytask_idprompt/core.memory.jit_hydration.scatter_gather_memoryreturn_valuenewsystem_injectioninz%(py1)s in %(py3)sresultpy1py3z8system_injection key must be present in returned payload
>assert %(py5)spy5<ZERO_AMNESIA_STATE>	injectionz(Expected <ZERO_AMNESIA_STATE> tag, got:    z</ZERO_AMNESIA_STATE>z*Expected closing </ZERO_AMNESIA_STATE> tagzFBB1 PASSED: system_injection present and contains <ZERO_AMNESIA_STATE>)core.memory.jit_hydrationr	   !core.memory.zero_amnesia_enveloper   r   r   asynciorun
@pytest_ar_call_reprcompare	_saferepr@py_builtinslocals_should_repr_global_name_format_assertmsgAssertionError_format_explanationprint)
r	   r   cold_contextpayloadr   @py_assert0@py_assert2@py_format4@py_format6r$   s
             6/mnt/e/genesis-system/tests/track_b/test_story_2_02.py+test_bb1_system_injection_present_and_validr;      s   C?  	L %0lmG	@i]iFj	k A6w?@A c'ccccccccccccccccccc)ccccccc)*I!l!Y.lll!Ylll!llllllYlllYllll2Z[deifi[jZk0lllllll"]"i/]]]"i]]]"]]]]]]i]]]i]]]]1]]]]]]]	
RSA As   J!!J+c                      ddl m}  ddlm}m}  |dddd      }ddd	}t        d
t        |            5  t        j                   | |            }dd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  }dd|iz  }	t        t        j                   |	            dx}}|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  }dd|iz  }	t        t        j                   |	            dx}}g }||
v }|}|sd}||
v }|}|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  }|j#                  |       |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  }|j#                  |       t        j$                  |d       i z  }t        j&                  d!|
       d"z   d#|iz  }t        t        j                   |            dx}x}x}x}}t)        d$       y# 1 sw Y   "xY w)%uJ   BB2: On Redis timeout → hydration still completes with fallback content.r   r   )r   FALLBACK_WORKINGNg     F@r   zbb2-timeoutzDeploy the redis cache layerr   r   r   r   r   r   r   r   r   assert %(py5)sr"   r#   r$   z
Cold start)z%(py2)s in %(py4)sr=   )py2py4z%(py6)spy6)z%(py9)s in %(py11)s)py9py11z%(py13)spy13   z7Expected fallback content for cold/timeout state, got: z
>assert %(py16)spy16zIBB2 PASSED: Timeout scenario still returns envelope with fallback content)r&   r	   r'   r   r=   r   r   r(   r)   r*   r+   r,   r-   r.   r/   r1   r2   append_format_boolopr0   r3   )r	   r   r=   degraded_contextr5   r   r6   r7   r8   r9   r$   @py_assert1@py_assert3@py_assert8@py_assert10@py_format5@py_format7@py_format12@py_format14@py_format15@py_format17s                        r:   .test_bb2_redis_timeout_completes_with_fallbackrT   1   s   CQ % 	 (3QRG	@i]mFn	o A6w?@A '''''''''''''''''''''''')*I!.!Y....!Y...!......Y...Y.......y( L LI,E   y                 )     )     LI    -9      =F    =F        B)M      

UVA As   M33M=c                  d   ddl m}  ddlm}m}m}m}  |dddd      }i }t        dt        |      	      5  t        j                   | |            }ddd       d
   }||v }	|	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  }
t        j"                  d| d      dz   d|
iz  }t%        t        j&                  |            d}	||v }	|	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  }
t        j"                  d| d      dz   d|
iz  }t%        t        j&                  |            d}	||v }	|	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  }
t        j"                  d| d      dz   d|
iz  }t%        t        j&                  |            d}	t)        d       y# 1 sw Y   xY w)uE   BB3: Cold start (empty memory) → envelope has all fallback strings.r   r   )r   r=   FALLBACK_KGFALLBACK_CONSTRAINTSNg      ?r   r   r   r   r   r   z%(py0)s in %(py2)sr=   r$   py0r?   z
Expected 'z' in envelope
>assert %(py4)sr@   rV   rW   zCBB3 PASSED: Cold start envelope contains all three fallback strings)r&   r	   r'   r   r=   rV   rW   r   r   r(   r)   r*   r+   r-   r.   r/   r,   r0   r1   r2   r3   )r	   r   r=   rV   rW   r4   r5   r   r$   rJ   @py_format3rN   s               r:   $test_bb3_cold_start_fallback_stringsr]   N   s   C  ! 	L G	@i]iFj	k A6w?@A )*Iy(VVVyVVVVVVVVVVVVVVVyVVVyVVVVJ7G6H*VVVVVVV)#LLL;)LLLLLL;LLL;LLLLLL)LLL)LLLLz+m%LLLLLLL9,^^^9^^^^^^^^^^^^^^^9^^^9^^^^
;O:PP].^^^^^^^	
OPA As   L%%L/c            
         ddl m}  ddlm}  |dddd      }dd	d
dddiddgd}t	        |      }t        dt        |            5  t        j                   | |            }ddd       |j                         D ]  \  }}|v }|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  }	t        j                   d| d      dz   d|	iz  }
t#        t        j$                  |
            d}||   }||k(  }|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"|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}}t'        d)       y# 1 sw Y   xY w)*z7BB4: Original payload fields preserved after hydration.r   r   r
   stateNg      @r   zbb4-preservez"Refactor the scatter_gather modulezgemini-2.0-flashgffffff?nestedTredisqdrant)r   r   modeltemperaturecustom_fieldtagsr   r   r   r   rX   keyr   rY   zOriginal key 'z' missing from resultr[   r@   ==)z%(py1)s == %(py3)svaluer   zValue for 'z' changed: expected z, got r!   r"   r   r   r>   zIBB4 PASSED: All original payload fields preserved, system_injection added)r&   r	   r'   r   dictr   r   r(   r)   itemsr*   r+   r-   r.   r/   r,   r0   r1   r2   r3   )r	   r   ctxoriginal_extrar5   r   rg   rj   rJ   r\   rN   r6   r7   r8   r9   s                  r:   *test_bb4_original_payload_fields_preservedro   i   s   C?
g4UYfi
jC "6#!4((#N >"G	@i]`Fa	b A6w?@A %**, k
Uf}IIIsfIIIIIIsIIIsIIIIIIfIIIfIIIIse3HIIIIIIIc{j{e#jjj{ejjj{jjjjjjejjjejjjj{3%7KE9TZ[abe[fZi%jjjjjjjjk
 ''''''''''''''''''''''''	
UVA As   K**K4c                    	 ddl m}  ddlm g 		fd}	fd}ddd}t	        d	|
      5  t	        d|
      5  t        j                   | |             ddd       ddd       ddg}	|k(  }|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z   d|iz  }t        t        j                  |            dx}}t!        d       y# 1 sw Y   xY w# 1 sw Y   xY w)zJWB1: fast_extract called before scatter_gather_memory (verify call order).r   r   r
   c                 0    j                  d       dgdfS )Nfast_extractra   z	fix redisrG   )r5   
call_orders    r:   mock_fast_extractzMtest_wb1_fast_extract_called_before_scatter_gather.<locals>.mock_fast_extract   s    .)	;''    c                 F   K   j                  d        d d d d      S w)Nscatter_gatherg      ?r   rs   )r   target_entitiesintent_string
timeout_msr   rt   s       r:   mock_scatter_gatherzOtest_wb1_fast_extract_called_before_scatter_gather.<locals>.mock_scatter_gather   s)     *+4TW[hklls   !wb1z
test redisr   z&core.memory.jit_hydration.fast_extractside_effectr   Nrr   rx   rh   z%(py0)s == %(py3)srt   rZ   r    z1Expected ['fast_extract', 'scatter_gather'], got r!   r"   z?WB1 PASSED: fast_extract is called before scatter_gather_memory)r&   r	   r'   r   r   r(   r)   r*   r+   r-   r.   r/   r,   r0   r1   r2   r3   )
r	   ru   r|   r5   r7   rJ   r8   r9   r   rt   s
           @@r:   2test_wb1_fast_extract_called_before_scatter_gatherr      s-   C?J(m  <8G	7EV	W 8	@Na	b8-g678 8 )*:; :;;  :;              <    <J<H     

KL8 8 8 8s"   ED5E5D>	:EE
c                  F   ddl m}  ddlm}  |dddd      }t	        |	      }d
dd}t        d|      5  t        j                   | |             ddd       |j                          |j                  \  }}|j                  j                  }|j                  j                  }|j                  dt        |      dkD  r|d   nd      }	d}
|	|
k(  }|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z   d|iz  }t+        t        j,                  |            dx}}
t/        d       y# 1 sw Y   ExY w)z5WB2: scatter_gather_memory called with timeout_ms=45.r   r   r
   wkcg      4@r   r   zwb2-taskzDeploy redis cacher   r   r   Nr{      -   rh   r   timeout_usedr   zExpected timeout_ms=45, got r!   r"   z;WB2 PASSED: scatter_gather_memory called with timeout_ms=45)r&   r	   r'   r   r   r   r(   r)   assert_called_once	call_argsargskwargsgetlenr*   r+   r-   r.   r/   r,   r0   r1   r2   r3   )r	   r   rm   mock_scatterr5   _r   all_args
all_kwargsr   r7   rJ   r8   r9   s                 r:   .test_wb2_scatter_gather_called_with_timeout_45r      s4   C?
csPS`d
eC#.L$0DEG	@l	S 8-g678 ##%&&IAv%%**H''..J>>,s8}q?PVZ[LL<2LLL<2LLLLLL<LLL<LLL2LLL!=l^LLLLLLL	GI8 8s   FF c                  ~   ddl m}  ddlm}  |dddd      }t	        |	      }g fd
}ddd}t        d|      5  t        d|      5  t        j                   | |             ddd       ddd       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}}d   }||u }|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  }t        j                  d!      d"z   d#|iz  }t!        t        j"                  |            d}|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}}|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}}t)        d)       y# 1 sw Y   xY w# 1 sw Y   xY w)*zBWB3: build_envelope called with MemoryContext from scatter_gather.r   r   r
   zWorking on tradie scraperu/   KG Topology:
redis: service — in-memory cachez2Learned Constraints:
Scar 1: never use free Geminig     6@r   r   c                 B    j                  |        ddlm}  ||       S )Nr   )build_envelope)rG   r'   r   )rm   
real_buildcaptured_contexts     r:   mock_build_envelopezOtest_wb3_build_envelope_called_with_memory_context.<locals>.mock_build_envelope   s    $R#rv   zwb3-taskzRedis scraperr   r   r   z(core.memory.jit_hydration.build_enveloper~   NrE   rh   )z0%(py3)s
{%(py3)s = %(py0)s(%(py1)s)
} == %(py6)sr   r   )rZ   r   r    rA   z,build_envelope should be called exactly oncez
>assert %(py8)spy8)is)z%(py0)s is %(py2)sctx_usedexpected_ctxrY   zHbuild_envelope must receive the MemoryContext returned by scatter_gatherr[   r@   )z5%(py2)s
{%(py2)s = %(py0)s.working_state
} == %(py5)s)rZ   r?   r"   zassert %(py7)spy7)z2%(py2)s
{%(py2)s = %(py0)s.latency_ms
} == %(py5)szNWB3 PASSED: build_envelope called with exact MemoryContext from scatter_gather)r&   r	   r'   r   r   r   r(   r)   r   r*   r+   r-   r.   r/   r,   r0   r1   r2   r   r   r3   )r	   r   r   r   r   r5   r7   @py_assert5@py_assert4rO   @py_format9r   rJ   r\   rN   rK   r9   @py_format8r   s                     @r:   2test_wb3_build_envelope_called_with_memory_contextr      s   C? 1FQ	L ,7L %@G	@l	S 8	9GZ	[8-g678 8  UAU A%UUU AUUUUUU3UUU3UUUUUUUUUUUU UUUAUUU'UUUUUUUU"H|#ooo8|oooooo8ooo8oooooo|ooo|oooo%ooooooo!!@%@@!%@@@@@!%@@@@@@@8@@@8@@@!@@@%@@@@@@@@&$&$&&&&$&&&&&&8&&&8&&&&&&$&&&&&&&	
Z[8 8 8 8s#   P2P%)P2%P/	*P22P<c                      t                t                t                t                t	                t                t                t        d       y )NuG   
ALL 7 TESTS PASSED — Story 2.02 (Track B): interceptor_jit_hydration)r;   rT   r]   ro   r   r   r   r3    rv   r:   test_allr      s5    /124(*.0682468	
TUrv   __main__)__doc__builtinsr-   _pytest.assertion.rewrite	assertionrewriter*   r(   sysunittest.mockr   r   r   r   pathinsertr;   rT   r]   ro   r   r   r   r   __name__r   rv   r:   <module>r      su      
 ; ; * +T0W:Q6WDM6J2 \NV zJ rv   