@@ -68,6 +68,30 @@ describe('Prompts', () => {
6868 )
6969 } )
7070
71+ it ( 'should fetch a specific prompt version when requested' , async ( ) => {
72+ mockFetch . mockResolvedValueOnce ( {
73+ ok : true ,
74+ status : 200 ,
75+ json : ( ) => Promise . resolve ( { ...mockPromptResponse , version : 2 , prompt : 'Version 2 prompt' } ) ,
76+ } )
77+
78+ const posthog = createMockPostHog ( )
79+ const prompts = new Prompts ( { posthog } )
80+
81+ const result = await prompts . get ( 'test-prompt' , { version : 2 } )
82+
83+ expect ( result ) . toBe ( 'Version 2 prompt' )
84+ expect ( mockFetch ) . toHaveBeenCalledWith (
85+ 'https://us.posthog.com/api/environments/@current/llm_prompts/name/test-prompt/?token=phc_test_key&version=2' ,
86+ {
87+ method : 'GET' ,
88+ headers : {
89+ Authorization : 'Bearer phx_test_key' ,
90+ } ,
91+ }
92+ )
93+ } )
94+
7195 it ( 'should return cached prompt when fresh' , async ( ) => {
7296 mockFetch . mockResolvedValueOnce ( {
7397 ok : true ,
@@ -92,6 +116,34 @@ describe('Prompts', () => {
92116 expect ( mockFetch ) . toHaveBeenCalledTimes ( 1 ) // No additional fetch
93117 } )
94118
119+ it ( 'should keep latest and versioned prompt caches separate' , async ( ) => {
120+ mockFetch
121+ . mockResolvedValueOnce ( {
122+ ok : true ,
123+ status : 200 ,
124+ json : ( ) => Promise . resolve ( { ...mockPromptResponse , version : 2 , prompt : 'Latest prompt' } ) ,
125+ } )
126+ . mockResolvedValueOnce ( {
127+ ok : true ,
128+ status : 200 ,
129+ json : ( ) => Promise . resolve ( { ...mockPromptResponse , version : 1 , prompt : 'Version 1 prompt' } ) ,
130+ } )
131+
132+ const posthog = createMockPostHog ( )
133+ const prompts = new Prompts ( { posthog } )
134+
135+ const latestResult = await prompts . get ( 'test-prompt' , { cacheTtlSeconds : 300 } )
136+ const versionedResult = await prompts . get ( 'test-prompt' , { cacheTtlSeconds : 300 , version : 1 } )
137+
138+ expect ( latestResult ) . toBe ( 'Latest prompt' )
139+ expect ( versionedResult ) . toBe ( 'Version 1 prompt' )
140+ expect ( mockFetch ) . toHaveBeenCalledTimes ( 2 )
141+
142+ await expect ( prompts . get ( 'test-prompt' , { cacheTtlSeconds : 300 } ) ) . resolves . toBe ( 'Latest prompt' )
143+ await expect ( prompts . get ( 'test-prompt' , { cacheTtlSeconds : 300 , version : 1 } ) ) . resolves . toBe ( 'Version 1 prompt' )
144+ expect ( mockFetch ) . toHaveBeenCalledTimes ( 2 )
145+ } )
146+
95147 it ( 'should refetch when cache is stale' , async ( ) => {
96148 const updatedPromptResponse = {
97149 ...mockPromptResponse ,
@@ -180,6 +232,20 @@ describe('Prompts', () => {
180232 await expect ( prompts . get ( 'test-prompt' ) ) . rejects . toThrow ( 'Network error' )
181233 } )
182234
235+ it ( 'should include the requested version in versioned fetch errors' , async ( ) => {
236+ mockFetch . mockResolvedValueOnce ( {
237+ ok : false ,
238+ status : 404 ,
239+ } )
240+
241+ const posthog = createMockPostHog ( )
242+ const prompts = new Prompts ( { posthog } )
243+
244+ await expect ( prompts . get ( 'nonexistent-prompt' , { version : 7 } ) ) . rejects . toThrow (
245+ '[PostHog Prompts] Prompt "nonexistent-prompt" version 7 not found'
246+ )
247+ } )
248+
183249 it ( 'should handle 404 response' , async ( ) => {
184250 mockFetch . mockResolvedValueOnce ( {
185251 ok : false ,
@@ -599,5 +665,74 @@ describe('Prompts', () => {
599665 await prompts . get ( 'other-prompt' )
600666 expect ( mockFetch ) . toHaveBeenCalledTimes ( 4 )
601667 } )
668+
669+ it ( 'should clear all cached versions for a prompt when a name is provided' , async ( ) => {
670+ mockFetch
671+ . mockResolvedValueOnce ( {
672+ ok : true ,
673+ status : 200 ,
674+ json : ( ) => Promise . resolve ( { ...mockPromptResponse , version : 2 , prompt : 'Latest prompt' } ) ,
675+ } )
676+ . mockResolvedValueOnce ( {
677+ ok : true ,
678+ status : 200 ,
679+ json : ( ) => Promise . resolve ( { ...mockPromptResponse , version : 1 , prompt : 'Version 1 prompt' } ) ,
680+ } )
681+ . mockResolvedValueOnce ( {
682+ ok : true ,
683+ status : 200 ,
684+ json : ( ) => Promise . resolve ( { ...mockPromptResponse , version : 2 , prompt : 'Latest prompt refreshed' } ) ,
685+ } )
686+ . mockResolvedValueOnce ( {
687+ ok : true ,
688+ status : 200 ,
689+ json : ( ) => Promise . resolve ( { ...mockPromptResponse , version : 1 , prompt : 'Version 1 prompt refreshed' } ) ,
690+ } )
691+
692+ const posthog = createMockPostHog ( )
693+ const prompts = new Prompts ( { posthog } )
694+
695+ await prompts . get ( 'test-prompt' )
696+ await prompts . get ( 'test-prompt' , { version : 1 } )
697+ expect ( mockFetch ) . toHaveBeenCalledTimes ( 2 )
698+
699+ prompts . clearCache ( 'test-prompt' )
700+
701+ await expect ( prompts . get ( 'test-prompt' ) ) . resolves . toBe ( 'Latest prompt refreshed' )
702+ await expect ( prompts . get ( 'test-prompt' , { version : 1 } ) ) . resolves . toBe ( 'Version 1 prompt refreshed' )
703+ expect ( mockFetch ) . toHaveBeenCalledTimes ( 4 )
704+ } )
705+
706+ it ( 'should not clear cache entries for other prompt names that share the same prefix' , async ( ) => {
707+ mockFetch
708+ . mockResolvedValueOnce ( {
709+ ok : true ,
710+ status : 200 ,
711+ json : ( ) => Promise . resolve ( { ...mockPromptResponse , name : 'foo' , prompt : 'Foo latest' } ) ,
712+ } )
713+ . mockResolvedValueOnce ( {
714+ ok : true ,
715+ status : 200 ,
716+ json : ( ) => Promise . resolve ( { ...mockPromptResponse , name : 'foo::bar' , prompt : 'Foo bar latest' } ) ,
717+ } )
718+ . mockResolvedValueOnce ( {
719+ ok : true ,
720+ status : 200 ,
721+ json : ( ) => Promise . resolve ( { ...mockPromptResponse , name : 'foo' , prompt : 'Foo latest refreshed' } ) ,
722+ } )
723+
724+ const posthog = createMockPostHog ( )
725+ const prompts = new Prompts ( { posthog } )
726+
727+ await prompts . get ( 'foo' )
728+ await prompts . get ( 'foo::bar' )
729+ expect ( mockFetch ) . toHaveBeenCalledTimes ( 2 )
730+
731+ prompts . clearCache ( 'foo' )
732+
733+ await expect ( prompts . get ( 'foo' ) ) . resolves . toBe ( 'Foo latest refreshed' )
734+ await expect ( prompts . get ( 'foo::bar' ) ) . resolves . toBe ( 'Foo bar latest' )
735+ expect ( mockFetch ) . toHaveBeenCalledTimes ( 3 )
736+ } )
602737 } )
603738} )
0 commit comments