
    0Li                     n   d Z ddlZddlmc mZ ddlZddlm	Z	 ddl
Z
ej                  j                  d e e	e      j                  j                  j                               ddlmZ  G d d      Z G d d      Z G d	 d
      Z G d d      Z G d d      Zedk(  r e
j0                  edg       yy)u)   utils/prompt_cache.py 테스트 스위트    N)Path)apply_cache_markersc                       e Zd ZdZd Zd Zy)TestApplyCacheMarkersEmptyu&   엣지 케이스: 빈 메시지 목록c                    g }t        |      }g }||k(  }|st        j                  d|fd||f      dt        j                         v st        j
                  t               rt        j                  t               ndt        j                  |      t        j                  |      t        j                  |      dz  }dd|iz  }t        t        j                  |            dx}x}x}}y)u   빈 리스트 → 빈 리스트==)z0%(py4)s
{%(py4)s = %(py0)s(%(py2)s)
} == %(py7)sr   )py0py2py4py7zassert %(py9)spy9N)	r   
@pytest_ar_call_reprcompare@py_builtinslocals_should_repr_global_name	_safereprAssertionError_format_explanation)self@py_assert1@py_assert3@py_assert6@py_assert5@py_format8@py_format10s          4/home/jay/workspace/utils/tests/test_prompt_cache.py!test_empty_messages_returns_emptyz<TestApplyCacheMarkersEmpty.test_empty_messages_returns_empty   s    #%,"2&,",&",,,,&",,,,,,",,,",,,2,,,&,,,",,,,,,,    c                    dddg}t        |      }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   }|j                  }d} ||      }	d}
|	|
k(  }|st        j                  d|fd|	|
f      t        j                  |      t        j                  |      t        j                  |      t        j                  |	      t        j                  |
      dz  }dd|iz  }t	        t        j
                  |            dx}x}x}x}	x}}
y)u:   원본 메시지 목록을 변경하지 않음 (deep copy)userhellorolecontentcache_controlr   not inz%(py1)s not in %(py4)spy1r   assert %(py6)spy6Nr&   r   zJ%(py7)s
{%(py7)s = %(py3)s
{%(py3)s = %(py1)s.get
}(%(py5)s)
} == %(py10)sr,   py3py5r   py10assert %(py12)spy12)r   r   r   r   r   r   get)r   messagesresult@py_assert0r   @py_assert2@py_format5@py_format7@py_assert4r   @py_assert9@py_assert8@py_format11@py_format13s                 r   test_returns_deep_copyz1TestApplyCacheMarkersEmpty.test_returns_deep_copy   s    #89$X.1hqk1k1111k111111k1111111{4{4y4y)4W4)W4444)W444{444444y444)444W44444444r    N)__name__
__module____qualname____doc__r   rB    r    r   r   r      s    0-5r    r   c                       e Zd ZdZd Zd Zy)!TestApplyCacheMarkersSystemPromptu+   시스템 프롬프트 캐시 마커 적용c                 0   ddddddg}t        |      }|d   }|j                  d      }t        |t              r|d   }|j                  }d	} ||      }d
di}	||	k(  }
|
st	        j
                  d|
fd||	f      t	        j                  |      t	        j                  |      t	        j                  |      t	        j                  |      t	        j                  |	      dz  }dd|iz  }t        t	        j                  |            dx}x}x}x}x}
}	y|j                  }d	} ||      }d
di}
||
k(  }|st	        j
                  d|fd||
f      dt        j                         v st	        j                  |      rt	        j                  |      ndt	        j                  |      t	        j                  |      t	        j                  |      t	        j                  |
      dz  }dd|iz  }t        t	        j                  |            dx}x}x}x}}
y)u:   system 역할 첫 번째 메시지에 cache_control 삽입systemzYou are a helpful assistant.r$   r"   Hellor   r&   r'   type	ephemeralr   r/   r0   r4   r5   NzI%(py6)s
{%(py6)s = %(py2)s
{%(py2)s = %(py0)s.get
}(%(py4)s)
} == %(py9)ssys_msgr
   r   r   r.   r   assert %(py11)spy11)r   r6   
isinstancelistr   r   r   r   r   r   r   r   )r   r7   r8   rQ   r&   r9   r:   r=   r   r>   r?   r@   rA   r   r   r   @py_assert7r   @py_format12s                      r   $test_system_prompt_gets_cache_markerzFTestApplyCacheMarkersSystemPrompt.test_system_prompt_gets_cache_marker!   s    *HI0
 %X.)++i(gt$2;L;??L?L??3L7LL37LLLLL37LLLL;LLL?LLL?LLL3LLL7LLLLLLLLL ;;HH;/HFK3HH/3HHHHH/3HHHHHHH7HHH7HHH;HHHHHH/HHH3HHHHHHHHHr    c                    dddg}t        |      }|d   j                  d      }t        |t              }|s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dt	        j
                         v st        j                  t              rt        j                  t              ndt        j                  |      d	z  }t        t        j                  |            d
}|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}}|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
)uI   문자열 content를 가진 system 메시지 → list 형식으로 변환rK   zSystem instruction here.r$   r   r&   5assert %(py4)s
{%(py4)s = %(py0)s(%(py1)s, %(py2)s)
}rU   rV   r
   r,   r   r   Ntextr   )z%(py1)s == %(py4)sr+   r-   r.   rN   )r   r6   rU   rV   r   r   r   r   r   r   r   r   )	r   r7   r8   r&   r   r;   r9   r:   r<   s	            r   ,test_system_string_content_converted_to_listzNTestApplyCacheMarkersSystemPrompt.test_system_string_content_converted_to_list1   sj    *DE
 %X.)--	*'4((((((((z(((z(((((('((('((((((4(((4((((((((((qz&!?%??!%?????!%????!???%????????qz&!+V+!V++++!V+++!+++V+++++++r    N)rC   rD   rE   rF   rY   r^   rG   r    r   rI   rI      s    5I 	,r    rI   c                   (    e Zd ZdZd Zd Zd Zd Zy)&TestApplyCacheMarkersNonSystemMessagesuA   비시스템 메시지 캐시 마커 적용 (system_and_3 전략)c                    dddg}t        |      }|d   j                  d      }t        |t              r|d   }|j                  }d} ||      }dd	i}||k(  }	|	st	        j
                  d
|	fd||f      t	        j                  |      t	        j                  |      t	        j                  |      t	        j                  |      t	        j                  |      dz  }
dd|
iz  }t        t	        j                  |            dx}x}x}x}x}	}y|d   }|j                  }d} ||      }dd	i}||k(  }	|	st	        j
                  d
|	fd||f      t	        j                  |      t	        j                  |      t	        j                  |      t	        j                  |      t	        j                  |      dz  }
dd|
iz  }t        t	        j                  |            dx}x}x}x}x}	}y)u)   사용자 메시지 1개에 마커 적용r"   rL   r$   r   r&   rM   r'   rN   rO   r   r/   r0   r4   r5   N)	r   r6   rU   rV   r   r   r   r   r   )r   r7   r8   r&   r9   r:   r=   r   r>   r?   r@   rA   s               r   $test_single_user_message_gets_markerzKTestApplyCacheMarkersNonSystemMessages.test_single_user_message_gets_marker@   s_   #89$X.)--	*gt$2;L;??L?L??3L7LL37LLLLL37LLLL;LLL?LLL?LLL3LLL7LLLLLLLLL!9J9==JJ=1Jfk5JJ15JJJJJ15JJJJ9JJJ=JJJJJJ1JJJ5JJJJJJJJJr    c           	         ddddddddddddddddd	ddd
dg}t        |      }dt        dt        fd}|d   } ||      }|sddt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      dz  }t        t        j                  |            dx}}|d   } ||      }|sddt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      dz  }t        t        j                  |            dx}}|d   } ||      }|sddt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      dz  }t        t        j                  |            dx}}|d   } ||      }| }|sddt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      dz  }t        t        j                  |            dx}x}}|d   } ||      }| }|sddt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      dz  }t        t        j                  |            dx}x}}y)u6   마지막 3개 비시스템 메시지에 마커 적용r"   msg1r$   	assistantresp1msg2resp2msg3resp3msg4msgreturnc                     | j                  d      }t        |t              r|r|d   j                  d      ddik(  S | j                  d      ddik(  S Nr&   rM   r'   rN   rO   r6   rU   rV   rl   r&   s     r   has_cache_markerzaTestApplyCacheMarkersNonSystemMessages.test_last_3_messages_get_markers.<locals>.has_cache_markerX   R    ggi(G'4(Wr{7FK;PPP77?+/DDDr       ,assert %(py4)s
{%(py4)s = %(py0)s(%(py2)s)
}rr   r
   r   r   N      r   0assert not %(py4)s
{%(py4)s = %(py0)s(%(py2)s)
}   
r   dictboolr   r   r   r   r   r   r   	r   r7   r8   rr   r   r   r;   r   @py_format6s	            r    test_last_3_messages_get_markerszGTestApplyCacheMarkersNonSystemMessages.test_last_3_messages_get_markersJ   sT    / W5/ W5/ W5/
 %X.	E$ 	E4 	E !'q	*	**************	********** &q	*	**************	********** &q	*	**************	**********$*1I.#I...........#...#...I..........$*1I.#I...........#...#...I..........r    c           	      T	   dddddddddddddd	ddd
ddddg}t        |      }dt        dt        fd}|d   } ||      }|sddt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      dz  }t        t        j                  |            dx}}|d   } ||      }|sddt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      dz  }t        t        j                  |            dx}}|d   } ||      }|sddt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      dz  }t        t        j                  |            dx}}|d   } ||      }|sddt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      dz  }t        t        j                  |            dx}}|d   } ||      }| }|sddt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      dz  }t        t        j                  |            dx}x}}|d   } ||      }| }|sddt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      dz  }t        t        j                  |            dx}x}}|d   } ||      }| }|sddt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      dz  }t        t        j                  |            dx}x}}y)u-   system + 3 메시지 = 최대 4개 breakpointrK   zSystem prompt.r$   r"   rd   re   rf   rg   rh   ri   rj   rl   rm   c                     | j                  d      }t        |t              r|r|d   j                  d      ddik(  S | j                  d      ddik(  S ro   rp   rq   s     r   rr   zfTestApplyCacheMarkersNonSystemMessages.test_system_and_3_total_4_breakpoints.<locals>.has_cache_markerr   rs   r    r   ru   rr   rv   Nrt   rw   rx   rz   ry         r{   r~   s	            r   %test_system_and_3_total_4_breakpointszLTestApplyCacheMarkersNonSystemMessages.test_system_and_3_total_4_breakpointse   s    *:;/ W5/ W5/ W5
 %X.	E$ 	E4 	E !'q	*	**************	********** &q	*	**************	********** &q	*	**************	********** &q	*	**************	********** %+1I.#I...........#...#...I..........$*1I.#I...........#...#...I..........$*1I.#I...........#...#...I..........r    c                    ddddddg}t        |      }dt        dt        fd}|d	   } ||      }|sd
dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      dz  }t        t        j                  |            dx}}|d   } ||      }|sd
dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      dz  }t        t        j                  |            dx}}y)u+   3개 미만 메시지: 모두 마커 적용r"   ar$   re   brl   rm   c                     | j                  d      }t        |t              r|r|d   j                  d      ddik(  S | j                  d      ddik(  S ro   rp   rq   s     r   rr   zkTestApplyCacheMarkersNonSystemMessages.test_fewer_than_3_messages_all_get_markers.<locals>.has_cache_marker   rs   r    r   ru   rr   rv   Nrz   r{   )r   r7   r8   rr   r   r   r;   s          r   *test_fewer_than_3_messages_all_get_markerszQTestApplyCacheMarkersNonSystemMessages.test_fewer_than_3_messages_all_get_markers   s     , S1
 %X.	E$ 	E4 	E !'q	*	**************	********** &q	*	**************	**********r    N)rC   rD   rE   rF   rb   r   r   r   rG   r    r   r`   r`   =   s    KK/6/<+r    r`   c                       e Zd ZdZd Zd Zy) TestApplyCacheMarkersListContentu.   list 형식 content를 가진 메시지 처리c                 8   dddddddgdg}t        |      }|d   d   }t        |t              }|s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dt        j                         v st        j                  t              rt        j                  t              ndt        j                  |      dz  }t        t        j                  |            d}|d   }|j                  }d} ||      }	ddi}
|	|
k(  }|st        j                  d|fd|	|
f      t        j                  |      t        j                  |      t        j                  |      t        j                  |	      t        j                  |
      dz  }dd|iz  }t        t        j                  |            dx}x}x}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}}y)u8   list content의 마지막 블록에 cache_control 추가r"   r]   firstrN   r]   secondr$   r   r&   r[   rU   rV   r\   NrM   r'   rN   rO   r   r/   r0   r4   r5   r(   r*   r+   r-   r.   )r   rU   rV   r   r   r   r   r   r   r   r6   r   )r   r7   r8   r&   r   r;   r9   r:   r=   r   r>   r?   r@   rA   r<   s                  r   &test_list_content_marker_on_last_blockzGTestApplyCacheMarkersListContent.test_list_content_marker_on_last_block   s    #W5#X6
 %X.)I&'4((((((((z(((z(((((('((('((((((4(((4((((((((((r{H{HH/HFK3HH/3HHHHH/3HHHH{HHHHHHHHH/HHH3HHHHHHHHH0gaj0j0000j000000j0000000r    c                 8   ddddgdg}t        |       d}|d   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}}y)u'   원본 메시지가 변경되지 않음r"   r]   r#   r   r$   r'   r   r&   r(   r*   r+   r-   r.   N)r   r   r   r   r   r   )r   r7   r9   r   r:   r;   r<   s          r   test_original_not_mutatedz:TestApplyCacheMarkersListContent.test_original_not_mutated   s     %+W=>
 	H%?hqk)&<Q&??&?????&???????&????????r    N)rC   rD   rE   rF   r   r   rG   r    r   r   r      s    81$
@r    r   c                       e Zd ZdZd Zy)"TestApplyCacheMarkersEphemeralTypeu   cache_control 형식 검증c                    dddg}t        |      }|d   j                  d      }t        |t              r|d   j                  di       }n|d   j                  di       }|j                  }d} ||      }d	}||k(  }	|	st	        j
                  d
|	fd||f      dt        j                         v st	        j                  |      rt	        j                  |      ndt	        j                  |      t	        j                  |      t	        j                  |      t	        j                  |      dz  }
dd|
iz  }t        t	        j                  |            dx}x}x}x}	}y)z!cache_control.type == 'ephemeral'r"   testr$   r   r&   rM   r'   rN   rO   r   rP   ccrR   rS   rT   N)r   r6   rU   rV   r   r   r   r   r   r   r   r   )r   r7   r8   r&   r   r   r   r   r?   rW   r   rX   s               r   $test_cache_control_type_is_ephemeralzGTestApplyCacheMarkersEphemeralType.test_cache_control_type_is_ephemeral   s    #78$X.)--	*gt$"5B3Bvv,f,vf~,,~,,,,~,,,,,,r,,,r,,,v,,,f,,,~,,,,,,,,,,,r    N)rC   rD   rE   rF   r   rG   r    r   r   r      s
    %	-r    r   __main__z-v)rF   builtinsr   _pytest.assertion.rewrite	assertionrewriter   syspathlibr   pytestpathinsertstr__file__parentutils.prompt_cacher   r   rI   r`   r   r   rC   mainrG   r    r   <module>r      s    /   
   3tH~,,33::; < 25 5 , ,>U+ U+p@ @D- - zFKK4 ! r    