
    'i_5                         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
Z
ddlZddlmZ ddlmZmZ  ed      Ze
j$                  j'                  d ee             e
j$                  j'                  d eedz  dz               e
j$                  j'                  d eedz  d	z  d
z                G d dej*                        Z G d dej*                        Z G d dej*                        Z G d dej*                        Z G d dej*                        Z G d dej*                        Z G d dej*                        Z G d dej*                        Zedk(  r3 ed        ed        ed        e         ej@                  d       yy) a  
RLM Integration Tests
======================
Tests for the Genesis RLM (Recursive Language Models) bloodstream system.

Tests verify:
1. Database connectivity (PostgreSQL, Redis)
2. RLM Gateway initialization
3. Constitutional AI loading
4. Interaction processing (shadow mode)
5. Preference pair collection
6. Feedback collection
7. Bloodstream pipeline dry-run
8. Elestio config methods

Author: Genesis RLM Specialist (Claude Opus 4.6)
Date: 2026-02-20
    N)Path)	MagicMockpatchz/mnt/e/genesis-systemdatazgenesis-memoryAIVAqueen_outputsrlmc                   (    e Zd ZdZd Zd Zd Zd Zy)TestElestioConfigzATest that all Elestio config classes expose the expected methods.c                    ddl m} |j                         }| j                  d|       | j                  d|       | j                  d|       | j                  d|       | j                  d|       | j	                  |d   t
               y )Nr   )PostgresConfighostportuserpassworddatabase)elestio_configr   get_connection_paramsassertInassertIsInstanceint)selfr   paramss      3/mnt/e/genesis-system/tests/test_rlm_integration.py.test_postgres_config_has_get_connection_paramsz@TestElestioConfig.test_postgres_config_has_get_connection_params+   sm    1557ff%ff%ff%j&)j&)fVnc2    c                     ddl m} |j                         }| j                  d|       | j                  d|       | j                  d|       | j                  d|       | j                  d|       y )Nr   RedisConfigr   r   usernamer   decode_responses)r   r   r   r   )r   r   r   s      r   +test_redis_config_has_get_connection_paramsz=TestElestioConfig.test_redis_config_has_get_connection_params5   sZ    .224ff%ff%j&)j&)(&1r   c                 t    ddl m} |j                         }|j                         }| j	                  ||       y)zEVerify that the get_client_params alias exists and returns same data.r   r   N)r   r   r   get_client_paramsassertEqual)r   r   connection_paramsclient_paramss       r   -test_redis_config_has_get_client_params_aliasz?TestElestioConfig.test_redis_config_has_get_client_params_alias>   s3    .'==?#557*M:r   c                 x    ddl m} |j                         }| j                  d|       | j                  d|       y )Nr   )QdrantConfigurlapi_key)r   r*   r$   r   )r   r*   r   s      r   (test_qdrant_config_has_get_client_paramsz:TestElestioConfig.test_qdrant_config_has_get_client_paramsE   s/    ///1eV$i(r   N)__name__
__module____qualname____doc__r   r"   r(   r-    r   r   r   r   (   s    K32;)r   r   c                   :    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
y	)
TestConstitutionz8Test that constitution.json is well-formed and complete.c                 &    t         dz  dz  | _        y )Nr   zconstitution.json)GENESIS_ROOTconstitution_pathr   s    r   setUpzTestConstitution.setUpS   s    !-!69L!Lr   c                 r    | j                  | j                  j                         d| j                          y )NzConstitution file not found at )
assertTruer7   existsr8   s    r   test_constitution_file_existsz.TestConstitution.test_constitution_file_existsV   s1    ..5579$:P:P9QR	Tr   c                     t        | j                        5 }t        j                  |      }d d d        | j	                  d       | j	                  d|       y # 1 sw Y   .xY w)N
principlesenforcementopenr7   jsonloadr   )r   fr   s      r   test_constitution_is_valid_jsonz0TestConstitution.test_constitution_is_valid_jsonZ   sP    $(() 	 Q99Q<D	 lD)mT*	  	 s   AA"c                     t        | j                        5 }t        j                  |      }d d d        d   }| j	                  t        |      dd       y # 1 sw Y   +xY w)Nr?      z.Constitution should have at least 5 principles)rB   r7   rC   rD   assertGreaterEquallen)r   rE   r   r?   s       r    test_constitution_has_principlesz1TestConstitution.test_constitution_has_principles`   sX    $(() 	 Q99Q<D	 ,'
J P	R	  	 s   AAc                 F   t        | j                        5 }t        j                  |      }d d d        d   D ]Z  }| j	                  d|d|        | j	                  d|d|        | j	                  d|d|        | j	                  d|d	|        \ y # 1 sw Y   lxY w)
Nr?   idzPrinciple missing 'id': namezPrinciple missing 'name': descriptionz!Principle missing 'description': severityzPrinciple missing 'severity': rA   )r   rE   r   ps       r   'test_each_principle_has_required_fieldsz8TestConstitution.test_each_principle_has_required_fieldsg   s    $(() 	 Q99Q<D	 l# 	OAMM$%=aS#ABMM&!'A!%EFMM-.OPQs,STMM*a+I!)MN		O	  	 s   BB c                    t        | j                        5 }t        j                  |      }d d d        d   D cg c]  }|d   	 }}| j	                  t        |      t        t        |            d|        y # 1 sw Y   PxY wc c}w )Nr?   rM   zDuplicate principle IDs found: )rB   r7   rC   rD   r%   rJ   set)r   rE   r   rQ   idss        r   test_principle_ids_are_uniquez.TestConstitution.test_principle_ids_are_uniquep   s{    $(() 	 Q99Q<D	  $\ 231qw33S3s3x=:3%@	B	  	 3s   A;B;Bc                     t        | j                        5 }t        j                  |      }d d d        d   }| j	                  d|       | j	                  d|       | j	                  d|       y # 1 sw Y   ExY w)Nr@   pre_send_checkself_critique_enabledrevision_loop_max_iterationsrA   )r   rE   r   r@   s       r   test_enforcement_configz(TestConstitution.test_enforcement_configw   sk    $(() 	 Q99Q<D	 =)&4-{;4kB	  	 s   A00A9N)r.   r/   r0   r1   r9   r=   rF   rK   rR   rV   r[   r2   r   r   r4   r4   P   s.    BMT+ROBCr   r4   c                       e Zd ZdZd Zd Zy)TestRLMGatewayImportz1Test that the RLM Gateway module can be imported.c                 j    	 ddl m}m} y# t        $ r}| j	                  d|        Y d}~yd}~ww xY w)z<Verify the rlm_gateway module can be imported without error.r   )
RLMGatewayget_gatewayzFailed to import RLM Gateway: N)AIVA.rlm_gatewayr_   r`   ImportErrorfail)r   r_   r`   es       r   test_gateway_module_importsz0TestRLMGatewayImport.test_gateway_module_imports   s4    	<@@ 	<II6qc:;;	<s    	2-2c                 |    ddl m}m} | j                  t	        |             | j                  t	        |             y )Nr   )process_aiva_outputcollect_feedback)ra   rg   rh   r;   callable)r   rg   rh   s      r   (test_gateway_convenience_functions_existz=TestRLMGatewayImport.test_gateway_convenience_functions_exist   s)    J!456!123r   N)r.   r/   r0   r1   re   rj   r2   r   r   r]   r]      s    ;<4r   r]   c                   4    e Zd ZdZd Zd Zd Zd Zd Zd Z	y)	TestSurpriseMemoryz:Test the surprise memory engine (core/surprise_memory.py).c                 H    ddl m}m} | j                  t	        |             y )Nr   )MemorySystemget_memory_system)core.surprise_memoryrn   ro   r;   ri   )r   rn   ro   s      r   test_memory_system_importsz-TestSurpriseMemory.test_memory_system_imports   s    H!234r   c                     ddl m}  |       }|j                  ddd      }| j                  d|       | j                  d|       | j                  d|d          y )	Nr   rn   zThe weather is normal today.testgeneralscoretiercomposite_score)rp   rn   evaluater   )r   rn   msresults       r   test_evaluate_routine_contentz0TestSurpriseMemory.test_evaluate_routine_content   sN    5^;VYOgv&ff%'9r   c                     ddl m}  |       }|j                  ddd      }| j                  d|       |d   d   }| j	                  |dd	       y )
Nr   rs   zHCRITICAL error: AIVA deployment failed unexpectedly during revenue call.system
operationsrv   rx   g333333?z2Critical content should have higher surprise score)rp   rn   ry   r   assertGreater)r   rn   rz   r{   rv   s        r   !test_evaluate_high_impact_contentz4TestSurpriseMemory.test_evaluate_high_impact_content   sT    5^Vl
 	gv&w 125#'[\r   c                     ddl m}  |       }|j                  ddd      }| j                  |t               |j                  |d      \  }}| j                  |d       y )	Nr   rs   rt   zTest passes?)domainexpected_outcome
confidencezTest failed unexpectedlyg        )rp   rn   make_predictionr   strresolve_predictionr   )r   rn   rz   pred_iderrorrv   s         r   test_prediction_workflowz+TestSurpriseMemory.test_prediction_workflow   sc    5^$$* % 

 	gs+,,W6PQu5#&r   c                     ddl m}  |       }|j                  ddddi      }| j                  |       | j                  |j                         y )Nr   rs   
call_endedzCaller booked an appointmentr   sales)rp   rn   observeassertIsNotNonerx   )r   rn   rz   rv   s       r   test_observe_compatibilityz-TestSurpriseMemory.test_observe_compatibility   sG    5^

<)G(T[I\]U#U223r   c                     ddl m}  |       }|j                         }| j                  d|       | j                  d|       y )Nr   rs   total_eventsavg_surprise)rp   rn   	get_statsr   )r   rn   rz   statss       r   test_get_statsz!TestSurpriseMemory.test_get_stats   s3    5^ne,ne,r   N)
r.   r/   r0   r1   rq   r|   r   r   r   r   r2   r   r   rl   rl      s$    D5:	]
'4-r   rl   c                   4    e Zd ZdZd Zd Zd Zd Zd Zd Z	y)	TestOutcomeInferencez3Test the _infer_outcome_label method of RLMGateway.c                 <    ddl m} |j                  |      | _        y)z6Create gateway instance for testing outcome inference.r   )r_   N)ra   r_   __new__gateway)r   r_   s     r   r9   zTestOutcomeInference.setUp   s     	0!))*5r   c                 f    d}| j                   j                  |dd      }| j                  |d       y )NzGThank you so much, that sounds perfect. Please book me in for Thursday.	completed   positiver   _infer_outcome_labelr%   r   
transcriptlabels      r   test_positive_outcomez*TestOutcomeInference.test_positive_outcome   s/    ^
11*k3O
+r   c                 f    d}| j                   j                  |dd      }| j                  |d       y )Nz1Forget it, this is useless. Do not call me again.r   -   negativer   r   s      r   test_negative_outcomez*TestOutcomeInference.test_negative_outcome   s/    H
11*k2N
+r   c                 h    d}| j                   j                  |dd      }| j                  |dd       y )NzHello?r      r   z Calls < 30s should lean negativer   r   s      r   test_short_call_negativez-TestOutcomeInference.test_short_call_negative   s4    
11*k2N
;	=r   c                 h    d}| j                   j                  |dd      }| j                  |dd       y )NzSomething went wrongr   <   r   z(Error outcomes should always be negativer   r   s      r   test_error_outcomez'TestOutcomeInference.test_error_outcome   s5    +
11*grJ
C	Er   c                 f    d}| j                   j                  |dd      }| j                  |d       y )Nz6I need to check on something and will call back later.r   Z   neutralr   r   s      r   test_neutral_outcomez)TestOutcomeInference.test_neutral_outcome   s/    M
11*k2N	*r   N)
r.   r/   r0   r1   r9   r   r   r   r   r   r2   r   r   r   r      s$    =6,
,
=E+r   r   c                   "    e Zd ZdZd Zd Zd Zy)TestCheckpointDirectoryz'Test that checkpoint directories exist.c                 h    t         dz  dz  dz  }| j                  |j                         d|        y )Nr   rlm_checkpointsreward_model	Missing: r6   r;   r<   r   paths     r   test_reward_model_dir_existsz4TestCheckpointDirectory.test_reward_model_dir_exists   2    f$'88>I4&'9:r   c                 h    t         dz  dz  dz  }| j                  |j                         d|        y )Nr   r   policyr   r   r   s     r   test_policy_dir_existsz.TestCheckpointDirectory.test_policy_dir_exists  s2    f$'888C4&'9:r   c                 h    t         dz  dz  dz  }| j                  |j                         d|        y )Nr   r   constitutionr   r   r   s     r   test_constitution_dir_existsz4TestCheckpointDirectory.test_constitution_dir_exists  r   r   N)r.   r/   r0   r1   r   r   r   r2   r   r   r   r      s    1;;;r   r   c                       e Zd ZdZd Zd Zy)TestSQLSchemaFilesz5Test that SQL schema files are present and non-empty.c                     t         dz  dz  }| j                  |j                                | j                  |j	                         j
                  dd       y )Nscriptszrlm_create_tables.sqli  z$rlm_create_tables.sql should be >1KBr6   r;   r<   r   statst_sizer   s     r   !test_rlm_create_tables_sql_existsz4TestSQLSchemaFiles.test_rlm_create_tables_sql_exists  sF    i'*AA&499;..A	Cr   c                     t         dz  dz  }| j                  |j                                | j                  |j	                         j
                  dd       y )Nr   zaiva_rlm_schema.sqli  z#aiva_rlm_schema.sql should be >500Br   r   s     r   test_aiva_rlm_schema_sql_existsz2TestSQLSchemaFiles.test_aiva_rlm_schema_sql_exists  sF    i'*??&499;..@	Br   N)r.   r/   r0   r1   r   r   r2   r   r   r   r     s    ?CBr   r   c                   (    e Zd ZdZd Zd Zd Zd Zy)TestBloodstreamPipelinezGTest that the bloodstream pipeline module loads and basic methods work.c                 D    ddl m} | j                  t        |             y )Nr   )BloodstreamPipeline)core.rlm_bloodstream_pipeliner   r;   ri   )r   r   s     r   test_pipeline_importsz-TestBloodstreamPipeline.test_pipeline_imports%  s    E!456r   c                     ddl m} t        dz  dz  }|j                         r< ||d      }| j	                  |t
               | j                  t        |      dd       y y )Nr   _scan_jsonl_dirKNOWLEDGE_GRAPHentitiesentityz)KG entities directory should have entries)r   r   r6   r<   r   listr   rJ   )r   r   entities_dirresultss       r   test_kg_directory_scanningz2TestBloodstreamPipeline.test_kg_directory_scanning)  s\    A#&77*D %lH=G!!'40s7|QJL	 !r   c                     ddl m} t        dz  dz  }|j                         r  ||d      }| j	                  |t
               y y )Nr   r   r   axiomsaxiom)r   r   r6   r<   r   r   )r   r   
axioms_dirr   s       r   test_axiom_directory_scanningz5TestBloodstreamPipeline.test_axiom_directory_scanning3  sB    A!$55@
%j':G!!'40 r   c                 `   ddl m} | j                   |d      d       | j                   |d      d       | j                   |d      d       | j                   |d      d	       | j                   |d       d       | j                   |d
      d       | j                   |d      d       y )Nr   )_parse_confidencer   HIGHg333333?	VERY_HIGHgffffff?LOWg?z0.7gffffff?95)r   r   assertAlmostEqual)r   r   s     r   test_confidence_parsingz/TestBloodstreamPipeline.test_confidence_parsing:  s    C05s;08$?0=tD07>06=07=06=r   N)r.   r/   r0   r1   r   r   r   r   r2   r   r   r   r   "  s    Q7L1	>r   r   __main__zF======================================================================z$  GENESIS RLM INTEGRATION TEST SUITE   )	verbosity)!r1   builtins@py_builtins_pytest.assertion.rewrite	assertionrewrite
@pytest_arasynciorC   ossysunittestpathlibr   unittest.mockr   r   r6   r   insertr   TestCaser   r4   r]   rl   r   r   r   r   r.   printmainr2   r   r   <module>r     se  &    	 
   * +, 3|$ % 3|f,/??@ A 3|f,>FG H!))) !)P-Cx(( -Ch48,, 4(2-** 2-r"+8,, "+R;h// ;(B** B(!>h// !>P z	(O	
01	(O	G HMMA r   