
    *j&                        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mZmZ dZ G d d          Z e            Zg dZd	eiZd
 Zd Zd Zd Zd Zd Zd Zd ZdS )uE   Tests for compiler/stages/index.py — offline using a stub embedder.    )annotationsN)
_sub_chunkbuild_indexquery_index    c                  "    e Zd ZdZdddddZdS )_HashEmbedderuM   Deterministic stub: text → fixed-dim unit vector derived from content hash.F)show_progress_barnormalize_embeddings	sentences	list[str]r
   boolr   return
np.ndarrayc                  g }|D ]}t          j        |                                                                          }|t          t          |          z  dz   z  d t                   }t          j        |t          j                  	                    t          j
                  dz
  }|r*t          j                            |          }	|	dk    r||	z  }|                    |           t          j        |          S )N   )dtypeg      `@r   )hashlibsha256encodedigestDIMlennp
frombufferuint8astypefloat32linalgnormappendstack)
selfr   r
   r   vecsshrawvecr    s
             B/Users/jameslopez/projects/MentorCore/tests/compiler/test_index.pyr   z_HashEmbedder.encode   s      		 		Aqxxzz**1133As1vv)*DSD1C-28444;;BJGG%OC#  y~~c**!884KCKKx~~    N)r   r   r
   r   r   r   r   r   )__name__
__module____qualname____doc__r    r*   r)   r	   r	      s@        WW #(%*       r*   r	   )z\You have power over your mind, not outside events. Realize this, and you will find strength.zQThe impediment to action advances action. What stands in the way becomes the way.z^Very little is needed to make a happy life; it is all within yourself in your way of thinking.zaAccept the things to which fate binds you and love the people with whom fate brings you together.zPDwell on the beauty of life. Watch the stars and see yourself running with them.stoic-textsc                 .   d} t          |           }| g}||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}}d S )	NzShort paragraph.==)z0%(py3)s
{%(py3)s = %(py0)s(%(py1)s)
} == %(py6)sr   textpy0py1py3py6assert %(py8)spy8)	r   
@pytest_ar_call_reprcompare@py_builtinslocals_should_repr_global_name	_safereprAssertionError_format_explanation)r4   @py_assert2@py_assert5@py_assert4@py_format7@py_format9s         r)    test_sub_chunk_short_passthroughrI   7   s/   Dd%v%v%%%%%%%%%%v%%%%%%%%%%%%:%%%%%:%%%%%%%%%%%%%d%%%%%d%%%%%%%%%%v%%%%%%%%%%%%%%%%%%%%%%%%%%%%%r*   c                 ~   d} t          | d          }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}}|D ]}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}}d S )Na  Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here. Sentence one is here.    )	max_charsr   >z/%(py3)s
{%(py3)s = %(py0)s(%(py1)s)
} > %(py6)sr   chunksr5   r:   r;   i,  )<=)z0%(py3)s
{%(py3)s = %(py0)s(%(py1)s)
} <= %(py6)schunk)
r   r   r<   r=   r>   r?   r@   rA   rB   rC   )	long_textrP   rD   rE   rF   rG   rH   rR   s           r)   test_sub_chunk_splits_long_textrT   <   sj   .I	S111Fv;;;?;33vv; ! !5zz S zS          zS            s     s             5     5      z    S                            ! !r*   c           	     f   t          t          | dz  t          d           dd l}|                    t          | dz                      }d}|j        } |            }|j        }||v }|st          j	        d|fd||f          t          j
        |          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}x}}|                    d          }
|
j        } |            }t          d         }t%          |          }||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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}x}}d S )NindexTembedder	overwriter   rP   in)zf%(py1)s in %(py9)s
{%(py9)s = %(py7)s
{%(py7)s = %(py5)s
{%(py5)s = %(py3)s.list_tables
}()
}.tables
}db)r7   r8   py5py7py9zassert %(py11)spy11r0   r2   )zi%(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.count_rows
}()
} == %(py10)s
{%(py10)s = %(py6)s(%(py8)s)
}tabler   )r6   py2py4r9   r;   py10zassert %(py12)spy12)r   	INDEXABLEFAKE_EMBEDDERlancedbconnectstrlist_tablestablesr<   r=   rA   r>   r?   r@   rB   rC   
open_table
count_rowsr   )tmp_pathrh   r\   @py_assert0rF   @py_assert6@py_assert8rD   @py_format10@py_format12ra   @py_assert1@py_assert3@py_assert7@py_assert9rE   @py_format11@py_format13s                     r)   test_build_index_creates_tabler{   G   s   	8g-QUVVVVNNN	X/00	1	1B.r~.~~''.'..8...........8......8...........r.....r......~....'....................................MM(##E>>Y}%=>%=!>!>>!>>>>>>>>>>>!>>>>>>>>>>>>>5>>>>>5>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>%=>>>>!>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>r*   c                   | dz  }t          t          |t          d           t          d|dt                    }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}}d}|d         }	||	v }|slt          j        d|fd||	f          t          j
        |          t          j
        |	          dz  }
dd|
iz  }t          t          j        |                    d x}x}}	d}|d         }	||	v }|slt          j        d|fd||	f          t          j
        |          t          j
        |	          dz  }
dd|
iz  }t          t          j        |                    d x}x}}	d}|d         }	||	v }|slt          j        d|fd||	f          t          j
        |          t          j
        |	          dz  }
dd|
iz  }t          t          j        |                    d x}x}}	d S )NrV   TrW   zmind and power   top_krX   r   rM   rO   r   resultsr5   r:   r;   chunk_idrZ   )z%(py1)s in %(py4)s)r7   rc   zassert %(py6)sr9   	source_idr4   r   rf   rg   r   r   r<   r=   r>   r?   r@   rA   rB   rC   )ro   	index_dirr   rD   rE   rF   rG   rH   rp   rv   @py_format5s              r)    test_query_index_returns_resultsr   Q   sJ   7"I	9}MMMM*IQWWWGw<<!<!<!33ww<!##:##########:#####:###############################$'!*$;*$$$$$$$$$$;*$$$$$;$$$$*$$$$$$$$$$$$$$$$$$$$$$$$$$$WQZ6Z6Z6Zr*   c                   t          d| dz  t                    }g }||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}}d S )
NanythingrV   rX   r2   )z%(py0)s == %(py3)sr   )r6   r8   zassert %(py5)sr]   )
r   rg   r<   r=   r>   r?   r@   rA   rB   rC   )ro   r   rD   ru   @py_format4@py_format6s         r)   $test_query_index_empty_returns_emptyr   [   s    Hw&  G 7b=7b77br*   c                   | dz  }t          t          |t          d           t          t          |t          d           t          d|dt                    }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}}d S )NrV   TrW   virtue   r~   r   rM   rO   r   r   r5   r:   r;   r   )ro   r   r   rD   rE   rF   rG   rH   s           r)   test_build_index_overwriter   b   sq   7"I	9}MMMM	9}MMMM(IQOOOGw<<!<!<!33ww<!r*   c                :    t          i | dz  t                     d S )NrV   r   )r   rg   )ro   s    r)   &test_build_index_empty_corpus_no_errorr   j   s"    Hw&??????r*   c                   | dz  }t          t          |t          d           dd l}|                    t          |                    }|                    d          }|                    dgt          z            	                    d          
                                }d |D             }t          |          }t          |          }t          |          }	||	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        t                    rt          j        t                    nddt          j                    v st          j        |          rt          j        |          ndt          j        |          t          j        |          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        |	          dz  }dd|iz  }t'          t          j        |                    d x}x}x}
}	d S )NrV   TrW   r   rP   g        i  c                    g | ]
}|d          S )r   r/   ).0rs     r)   
<listcomp>z-test_chunk_ids_are_unique.<locals>.<listcomp>v   s    ---1:---r*   r2   )zn%(py6)s
{%(py6)s = %(py0)s(%(py4)s
{%(py4)s = %(py1)s(%(py2)s)
})
} == %(py11)s
{%(py11)s = %(py8)s(%(py9)s)
}r   set	chunk_ids)r6   r7   rb   rc   r9   r;   r_   r`   zassert %(py13)spy13)r   rf   rg   rh   ri   rj   rm   searchr   limitto_listr   r   r<   r=   r>   r?   r@   rA   rB   rC   )ro   r   rh   r\   ra   rowsr   rv   rE   @py_assert10rw   rt   @py_format14s                r)   test_chunk_ids_are_uniquer   n   s   7"I	9}MMMMNNN	Y	(	(BMM(##E<<$$**40088::D-----I9~~03~0#i..0.0000000000.00000000000030000030000000000000s00000s00000000000009000009000000~000000000000000#00000#0000000000000i00000i000000.0000000000000000000000000000000r*   )r.   
__future__r   builtinsr>   _pytest.assertion.rewrite	assertionrewriter<   r   numpyr   compiler.stages.indexr   r   r   r   r	   rg   	_PASSAGESrf   rI   rT   r{   r   r   r   r   r   r/   r*   r)   <module>r      sF   K K " " " " " "                   F F F F F F F F F F       0 	 	 		 I&	& & &
! ! !? ? ?         @ @ @	1 	1 	1 	1 	1r*   