Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

aimlapi.test.ts 2.8 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
  1. import { fetchWithCache } from '../../src/cache';
  2. import {
  3. clearAimlApiModelsCache,
  4. createAimlApiProvider,
  5. fetchAimlApiModels,
  6. } from '../../src/providers/aimlapi';
  7. import { OpenAiChatCompletionProvider } from '../../src/providers/openai/chat';
  8. import { OpenAiCompletionProvider } from '../../src/providers/openai/completion';
  9. import { OpenAiEmbeddingProvider } from '../../src/providers/openai/embedding';
  10. jest.mock('../../src/providers/openai');
  11. jest.mock('../../src/cache');
  12. describe('createAimlApiProvider', () => {
  13. beforeEach(() => {
  14. jest.clearAllMocks();
  15. });
  16. it('creates chat completion provider when type is chat', () => {
  17. const provider = createAimlApiProvider('aimlapi:chat:model-name');
  18. expect(provider).toBeInstanceOf(OpenAiChatCompletionProvider);
  19. expect(OpenAiChatCompletionProvider).toHaveBeenCalledWith('model-name', expect.any(Object));
  20. });
  21. it('creates completion provider when type is completion', () => {
  22. const provider = createAimlApiProvider('aimlapi:completion:model-name');
  23. expect(provider).toBeInstanceOf(OpenAiCompletionProvider);
  24. expect(OpenAiCompletionProvider).toHaveBeenCalledWith('model-name', expect.any(Object));
  25. });
  26. it('creates embedding provider when type is embedding', () => {
  27. const provider = createAimlApiProvider('aimlapi:embedding:model-name');
  28. expect(provider).toBeInstanceOf(OpenAiEmbeddingProvider);
  29. expect(OpenAiEmbeddingProvider).toHaveBeenCalledWith('model-name', expect.any(Object));
  30. });
  31. it('defaults to chat provider when no type specified', () => {
  32. const provider = createAimlApiProvider('aimlapi:model-name');
  33. expect(provider).toBeInstanceOf(OpenAiChatCompletionProvider);
  34. expect(OpenAiChatCompletionProvider).toHaveBeenCalledWith('model-name', expect.any(Object));
  35. });
  36. });
  37. describe('fetchAimlApiModels', () => {
  38. beforeEach(() => {
  39. jest.clearAllMocks();
  40. clearAimlApiModelsCache();
  41. });
  42. it('fetches models from endpoint', async () => {
  43. jest.mocked(fetchWithCache).mockResolvedValue({
  44. data: { data: [{ id: 'model-a' }, { id: 'model-b' }] },
  45. cached: false,
  46. status: 200,
  47. statusText: 'OK',
  48. } as any);
  49. const models = await fetchAimlApiModels();
  50. expect(fetchWithCache).toHaveBeenCalledWith(
  51. 'https://api.aimlapi.com/models',
  52. { headers: {} },
  53. expect.any(Number),
  54. );
  55. expect(models).toEqual([{ id: 'model-a' }, { id: 'model-b' }]);
  56. });
  57. it('uses cache on subsequent calls', async () => {
  58. jest.mocked(fetchWithCache).mockResolvedValue({
  59. data: { data: [{ id: 'model-x' }] },
  60. cached: false,
  61. status: 200,
  62. statusText: 'OK',
  63. } as any);
  64. const first = await fetchAimlApiModels();
  65. jest.mocked(fetchWithCache).mockClear();
  66. const second = await fetchAimlApiModels();
  67. expect(first).toEqual(second);
  68. expect(fetchWithCache).not.toHaveBeenCalled();
  69. });
  70. });
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...