mirror of
https://github.com/FJurmanovic/sugarr.git
synced 2026-11-11 07:41:08 +00:00
Add cached Langfuse PromptService
This commit is contained in:
64
test/core/langfuse/PromptService.test.ts
Normal file
64
test/core/langfuse/PromptService.test.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
import { PromptService } from '../../../src/core/langfuse/PromptService';
|
||||
import type { PromptFetcher, RawPrompt } from '../../../src/core/langfuse/PromptGateway';
|
||||
|
||||
function makeRawPrompt(name: string, version = 1): RawPrompt {
|
||||
return {
|
||||
name,
|
||||
version,
|
||||
config: { model: 'gpt-4o-mini' },
|
||||
compile: (variables) => [{ role: 'user', content: `hello ${String(variables.who)}` }],
|
||||
};
|
||||
}
|
||||
|
||||
describe('PromptService', () => {
|
||||
it('fetches once and reuses the cache on subsequent calls when caching is enabled', async () => {
|
||||
const fetch = vi.fn().mockResolvedValue(makeRawPrompt('greet'));
|
||||
const fetcher: PromptFetcher = { fetch };
|
||||
const service = new PromptService(fetcher, true);
|
||||
|
||||
await service.getCompiled('greet', { who: 'a' });
|
||||
const compiled = await service.getCompiled('greet', { who: 'b' });
|
||||
|
||||
expect(fetch).toHaveBeenCalledTimes(1);
|
||||
expect(compiled.messages).toEqual([{ role: 'user', content: 'hello b' }]);
|
||||
expect(compiled.config).toEqual({ model: 'gpt-4o-mini' });
|
||||
});
|
||||
|
||||
it('always refetches when caching is disabled', async () => {
|
||||
const fetch = vi.fn().mockResolvedValue(makeRawPrompt('greet'));
|
||||
const fetcher: PromptFetcher = { fetch };
|
||||
const service = new PromptService(fetcher, false);
|
||||
|
||||
await service.getCompiled('greet', { who: 'a' });
|
||||
await service.getCompiled('greet', { who: 'b' });
|
||||
|
||||
expect(fetch).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it('invalidate(name) forces a refetch for that name only', async () => {
|
||||
const fetch = vi.fn().mockResolvedValue(makeRawPrompt('greet'));
|
||||
const fetcher: PromptFetcher = { fetch };
|
||||
const service = new PromptService(fetcher, true);
|
||||
|
||||
await service.getCompiled('greet', {});
|
||||
service.invalidate('greet');
|
||||
await service.getCompiled('greet', {});
|
||||
|
||||
expect(fetch).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it('invalidate() with no name clears the whole cache', async () => {
|
||||
const fetch = vi.fn().mockImplementation((name: string) => Promise.resolve(makeRawPrompt(name)));
|
||||
const fetcher: PromptFetcher = { fetch };
|
||||
const service = new PromptService(fetcher, true);
|
||||
|
||||
await service.getCompiled('a', {});
|
||||
await service.getCompiled('b', {});
|
||||
service.invalidate();
|
||||
await service.getCompiled('a', {});
|
||||
await service.getCompiled('b', {});
|
||||
|
||||
expect(fetch).toHaveBeenCalledTimes(4);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user