Ë
    ÛJêjd	  ã                  óŠ   — d Z ddlmZ ddlZddlmc mZ ddl	m
Z
 ddlmZ ddlmZ ddlmZmZ dd„Zdd	„Zdd
„Zdd„Zdd„Zy)z3Unit tests for the knowledge plane embedding slice.é    )ÚannotationsN)ÚPath)ÚTextChunker)ÚEmbeddingConfig)ÚKnowledgeVectorStoreÚVectorDocumentc                 óè  — t        «       } | j                  }d}||k(  }|s­t        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(  }|s­t        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(  }|s­t        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(  }|s­t        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(  }|s­t        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}}y )Ni   ©ú==)z2%(py2)s
{%(py2)s = %(py0)s.vector_dim
} == %(py5)sÚconfig)Úpy0Úpy2Úpy5zassert %(py7)sÚpy7i }  )z3%(py2)s
{%(py2)s = %(py0)s.max_context
} == %(py5)szqwen3-embedding:0.6b)z-%(py2)s
{%(py2)s = %(py0)s.model
} == %(py5)si   )z9%(py2)s
{%(py2)s = %(py0)s.chunk_size_tokens
} == %(py5)séd   )z<%(py2)s
{%(py2)s = %(py0)s.chunk_overlap_tokens
} == %(py5)s)r   Ú
vector_dimÚ
@pytest_arÚ_call_reprcompareÚ@py_builtinsÚlocalsÚ_should_repr_global_nameÚ	_safereprÚAssertionErrorÚ_format_explanationÚmax_contextÚmodelÚchunk_size_tokensÚchunk_overlap_tokens)r   Ú@py_assert1Ú@py_assert4Ú@py_assert3Ú@py_format6Ú@py_format8s         ú@/home/oem/CyberBrain26-unified/tests/test_knowledge_embedding.pyÚtest_embedding_config_defaultsr%      sC  € ÜÓ€FØ×ÑÐ$ Ð$Ð Ñ$×$×$Õ$Ð ×$×$×$×$×$Ñ$ˆ6×$×$Ó$ˆ6×$×$Ô$Ð×$×$Ò$ ×$×$×$×$×$×$Ó$Ø×ÑÐ' Ð'Ð Ñ'×'×'Õ'Ð ×'×'×'×'×'Ñ'ˆ6×'×'Ó'ˆ6×'×'Ô'Ð×'×'Ò' ×'×'×'×'×'×'Ó'Ø�<‰<Ð1Ð1Ð1ˆ<Ð1Ñ1×1×1Õ1ˆ<Ð1×1×1×1×1×1Ñ1ˆ6×1×1Ó1ˆ6×1×1Ô1ˆ<×1×1Ò1Ð1×1×1×1×1×1×1Ó1Ø×#Ñ#Ð* sÐ*Ð# sÑ*×*×*Õ*Ð# s×*×*×*×*×*Ñ*ˆ6×*×*Ó*ˆ6×*×*Ô*Ð#×*×*Ò* s×*×*×*×*×*×*Ó*Ø×&Ñ&Ð-¨#Ð-Ð&¨#Ñ-×-×-Õ-Ð&¨#×-×-×-×-×-Ñ-ˆ6×-×-Ó-ˆ6×-×-Ô-Ð&×-×-Ò-¨#×-×-×-×-×-×-Ô-ó    c           
     óÐ  — t        | dz  ¬«      }t        |«      }t        ddg|j                  z  ddddd	d
¬«      }|j	                  |g«       |j
                  } |«       }d}||k(  }|sÁt        j                  d|fd||f«      dt        j                  «       v st        j                  |«      rt        j                  |«      ndt        j                  |«      t        j                  |«      t        j                  |«      dœz  }dd|iz  }	t        t        j                  |	«      «      ‚d x}x}x}}|j                  dg|j                  z  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  }dd|iz  }t        t        j                  |«      «      ‚d x}x}}|
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}}y )NÚlancedb)Úlancedb_directoryztest-1gš™™™™™¹?zHello worldÚsrcz/tmp/test.txtg        Úabc123r   )ÚidÚvectorÚtextÚ	source_idÚpathÚmtimeÚsha256Úchunk_indexé   r
   )zD%(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.count
}()
} == %(py7)sÚstore)r   r   Úpy4r   zassert %(py9)sÚpy9)Úlimit©z0%(py3)s
{%(py3)s = %(py0)s(%(py1)s)
} == %(py6)sÚlenÚresults©r   Úpy1Úpy3Úpy6úassert %(py8)sÚpy8r.   )z%(py1)s == %(py4)s)r=   r6   zassert %(py6)sr?   )r   r   r   r   Úadd_documentsÚcountr   r   r   r   r   r   r   r   Úsearchr:   )Útmp_pathr   r5   Údocr   r!   Ú@py_assert6Ú@py_assert5r#   Ú@py_format10r;   Ú@py_assert2r    Ú@py_format7Ú@py_format9Ú@py_assert0Ú@py_format5s                    r$   Útest_vector_store_roundtriprO      sÜ  € Ü¨x¸)Ñ/CÔD€FÜ  Ó(€EÜ
ØØˆu�v×(Ñ(Ñ(ØØØØØØô	€Cð 
×Ñ˜˜ÔØ�;‰;Ð‰;‹=Ð˜AÐˆ=˜AÑ××Õˆ=˜A×××××Ñˆ5××Óˆ5××Ôˆ;××Òˆ=××Ò˜A××××××ÕØ�l‰l˜C˜5 6×#4Ñ#4Ñ4¸AˆlÓ>€GÜˆw‹<Ð˜1Ðˆ<˜1Ñ××Õˆ<˜1×××××ÑŒ3××ÓŒ3×××××Òˆw××Óˆw××Ôˆ<××Ò˜1××××××ÓØ�1‰:�fÑÐ. Ð.Ð Ñ.×.×.Õ.Ð ×.×.Ó.Ð×.×.Ò. ×.×.×.×.×.×.Ô.r&   c                óØ  ‡— | dz  Š‰j                  dd¬«       t        dd¬«      }t        |«      }|j                  ‰«      }t	        |«      }d}||kD  }|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  }dd|iz  }t        t        j                  |«      «      ‚d x}x}}d„ |D «       }	t        |	«      }
|
s•ddt        j                  «       v st        j                  t        «      rt        j                  t        «      ndt        j                  |	«      t        j                  |
«      dœz  }t        t        j                  |«      «      ‚d x}	}
ˆfd„|D «       }	t        |	«      }
|
s•ddt        j                  «       v st        j                  t        «      rt        j                  t        «      ndt        j                  |	«      t        j                  |
«      dœz  }t        t        j                  |«      «      ‚d x}	}
d„ t        |«      D «       }	t        |	«      }
|
s•ddt        j                  «       v st        j                  t        «      rt        j                  t        «      ndt        j                  |	«      t        j                  |
«      dœz  }t        t        j                  |«      «      ‚d x}	}
y )Nztest.txtzdword word word word word word word word word word word word word word word word word word word word zutf-8)Úencodingé   é   ©r   r   r4   ©ú>©z/%(py3)s
{%(py3)s = %(py0)s(%(py1)s)
} > %(py6)sr:   Úchunksr<   r@   rA   c              3  óP   K  — | ]  }t        |j                  t        «      –— Œ  y ­w©N)Ú
isinstancer.   Ústr)Ú.0Úcs     r$   ú	<genexpr>z+test_chunker_splits_text.<locals>.<genexpr>0   s   è ø€ Ò7¨1Œz˜!Ÿ&™&¤#×&Ñ7ùs   ‚$&z,assert %(py4)s
{%(py4)s = %(py0)s(%(py2)s)
}Úall)r   r   r6   c              3  óN   •K  — | ]  }|j                   t        ‰«      k(  –— Œ y ­wrZ   )Úsource_pathr\   )r]   r^   Ú	file_paths     €r$   r_   z+test_chunker_splits_text.<locals>.<genexpr>1   s   øè ø€ Ò?°1ˆq�}‰}¤ I£Õ.Ñ?ùs   ƒ"%c              3  ó@   K  — | ]  \  }}|j                   |k(  –— Œ y ­wrZ   )r3   )r]   Úir^   s      r$   r_   z+test_chunker_splits_text.<locals>.<genexpr>2   s   è ø€ Ò@¡d a¨ˆq�}‰} Õ!Ñ@ùs   ‚)Ú
write_textr   r   Ú
chunk_filer:   r   r   r   r   r   r   r   r   r`   Ú	enumerate)rE   r   ÚchunkerrX   rJ   rH   r    rK   rL   r   r!   rN   rc   s               @r$   Útest_chunker_splits_textrj   )   só  ø€ Ø˜:Ñ%€IØ×Ñ˜°ÐÔ8Ü¨qÀqÔI€FÜ˜&Ó!€GØ×Ñ 	Ó*€FÜˆv‹;Ð˜Ðˆ;˜‰?××Õˆ;˜×××××ÑŒ3××ÓŒ3×××××Òˆv××Óˆv××Ôˆ;××Ò˜××××××ÓÙ7°Ô7Ð7Œ3Ð7Ó7Ð7Ñ7×7×7×7×7Õ7Œ3×7×7Ó7Œ3×7×7Ô7Ð7×7×7Ò7Ð7×7×7×7×7×7Ò7Û?¸Ô?Ð?Œ3Ð?Ó?Ð?Ñ?×?×?×?×?Õ?Œ3×?×?Ó?Œ3×?×?Ô?Ð?×?×?Ò?Ð?×?×?×?×?×?Ò?Ù@¬i¸Ó.?Ô@Ð@Œ3Ð@Ó@Ð@Ñ@×@×@×@×@Õ@Œ3×@×@Ó@Œ3×@×@Ô@Ð@×@×@Ò@Ð@×@×@×@×@×@Ó@r&   c                 ó  — t        dd¬«      } t        | «      }|j                  dd¬«      }t        |«      }d}||kD  }|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  }dd|iz  }t        t	        j                  |«      «      ‚d x}x}}|d   }|j                  }d}||k(  }|s€t	        j
                  d|fd||f«      t	        j                  |«      t	        j                  |«      t	        j                  |«      dœz  }dd|iz  }t        t	        j                  |«      «      ‚d x}x}x}}|d   }|j                  }d}||k(  }|s€t	        j
                  d|fd||f«      t	        j                  |«      t	        j                  |«      t	        j                  |«      dœz  }dd|iz  }t        t	        j                  |«      «      ‚d x}x}x}}y )Né   r4   rT   zone two three four five sixú<memory>©rb   rU   rW   r:   rX   r<   r@   rA   r   zone two three fourr
   )z,%(py3)s
{%(py3)s = %(py1)s.text
} == %(py6)s)r=   r>   r?   zfour five six)r   r   Ú
chunk_textr:   r   r   r   r   r   r   r   r   r.   )	r   ri   rX   rJ   rH   r    rK   rL   rM   s	            r$   Útest_chunker_from_memoryrp   5   s�  € Ü¨qÀqÔI€FÜ˜&Ó!€GØ×ÑÐ =È:ÐÓV€FÜˆv‹;Ð˜Ðˆ;˜‰?××Õˆ;˜×××××ÑŒ3××ÓŒ3×××××Òˆv××Óˆv××Ôˆ;××Ò˜××××××ÓØ�!‰9Ð1ˆ9�>‰>Ð1Ð1Ð1ˆ>Ð1Ñ1×1×1Õ1ˆ>Ð1×1×1Ó1ˆ9×1×1Ò1ˆ>×1×1Ò1Ð1×1×1×1×1×1×1Õ1Ø�!‰9Ð,ˆ9�>‰>Ð,˜_Ð,ˆ>˜_Ñ,×,×,Õ,ˆ>˜_×,×,Ó,ˆ9×,×,Ò,ˆ>×,×,Ò,˜_×,×,×,×,×,×,Ö,r&   c                 óz  — t        dd¬«      } t        | «      }|j                  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  }dd|iz  }t        t	        j                  |«      «      ‚d x}x}}y )NrR   é
   rT   za b c d e f g h i jrm   rn   r4   r
   r9   r:   rX   r<   r@   rA   )r   r   ro   r:   r   r   r   r   r   r   r   r   )r   ri   rX   rJ   rH   r    rK   rL   s           r$   Ú)test_chunker_overlap_does_not_exceed_sizers   >   sº   € Ü¨qÀrÔJ€FÜ˜&Ó!€GØ×ÑÐ 5À:ÐÓN€FÜˆv‹;Ð˜!Ðˆ;˜!Ñ××Õˆ;˜!×××××ÑŒ3××ÓŒ3×××××Òˆv××Óˆv××Ôˆ;××Ò˜!××××××Ôr&   )ÚreturnÚNone)rE   r   rt   ru   )Ú__doc__Ú
__future__r   Úbuiltinsr   Ú_pytest.assertion.rewriteÚ	assertionÚrewriter   Úpathlibr   Ú$cyberbrain26.knowledge_plane.chunkerr   Ú-cyberbrain26.knowledge_plane.embedding_configr   Ú)cyberbrain26.knowledge_plane.vector_storer   r   r%   rO   rj   rp   rs   © r&   r$   ú<module>r�      s6   ðÙ 9å "ç  „ Õ å <Ý Iß Zó.ó/ó(	Aó-ôr&   