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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
- import { fetchWithCache } from '../../src/cache';
- import logger from '../../src/logger';
- import { AI21ChatCompletionProvider } from '../../src/providers/ai21';
- jest.mock('../../src/cache');
- jest.mock('../../src/logger');
- describe('AI21ChatCompletionProvider', () => {
- let originalApiKey: string | undefined;
- beforeEach(() => {
- jest.clearAllMocks();
- jest.resetAllMocks();
- // Save original environment variable
- originalApiKey = process.env.AI21_API_KEY;
- // Ensure clean state for environment
- delete process.env.AI21_API_KEY;
- });
- afterEach(() => {
- // Restore original environment variable
- if (originalApiKey === undefined) {
- delete process.env.AI21_API_KEY;
- } else {
- process.env.AI21_API_KEY = originalApiKey;
- }
- jest.restoreAllMocks();
- });
- it('should construct with valid model name', () => {
- const provider = new AI21ChatCompletionProvider('jamba-1.5-mini');
- expect(provider.modelName).toBe('jamba-1.5-mini');
- });
- it('should warn when constructing with unknown model', () => {
- const mockWarn = jest.spyOn(logger, 'warn').mockImplementation();
- new AI21ChatCompletionProvider('unknown-model');
- expect(mockWarn).toHaveBeenCalledWith(expect.stringContaining('unknown-model'));
- mockWarn.mockRestore();
- });
- it('should get API key from config', () => {
- const provider = new AI21ChatCompletionProvider('jamba-1.5-mini', {
- config: { apiKey: 'test-key' },
- });
- expect(provider.getApiKey()).toBe('test-key');
- });
- it('should get API key from environment variable', () => {
- process.env.AI21_API_KEY = 'env-key';
- const provider = new AI21ChatCompletionProvider('jamba-1.5-mini');
- expect(provider.getApiKey()).toBe('env-key');
- });
- it('should get API URL from config', () => {
- const provider = new AI21ChatCompletionProvider('jamba-1.5-mini', {
- config: { apiBaseUrl: 'https://custom-api.ai21.com' },
- });
- expect(provider.getApiUrl()).toBe('https://custom-api.ai21.com');
- });
- it('should get default API URL when not configured', () => {
- const provider = new AI21ChatCompletionProvider('jamba-1.5-mini');
- expect(provider.getApiUrl()).toBe('https://api.ai21.com/studio/v1');
- });
- it('should throw error when API key is not set', async () => {
- const provider = new AI21ChatCompletionProvider('jamba-1.5-mini');
- await expect(provider.callApi('test prompt')).rejects.toThrow('AI21 API key is not set');
- });
- it('should handle successful API call', async () => {
- const mockResponse = {
- data: {
- choices: [
- {
- message: {
- content: 'test response',
- },
- },
- ],
- usage: {
- total_tokens: 10,
- prompt_tokens: 5,
- completion_tokens: 5,
- },
- },
- cached: false,
- status: 200,
- statusText: 'OK',
- };
- jest.mocked(fetchWithCache).mockResolvedValue(mockResponse);
- const provider = new AI21ChatCompletionProvider('jamba-1.5-mini', {
- config: { apiKey: 'test-key' },
- });
- const result = await provider.callApi('test prompt');
- expect(result.output).toBe('test response');
- expect(result.tokenUsage).toEqual({
- total: 10,
- prompt: 5,
- completion: 5,
- });
- });
- it('should handle API error response', async () => {
- const mockResponse = {
- data: {
- error: 'API error message',
- },
- cached: false,
- status: 400,
- statusText: 'Bad Request',
- };
- jest.mocked(fetchWithCache).mockResolvedValue(mockResponse);
- const provider = new AI21ChatCompletionProvider('jamba-1.5-mini', {
- config: { apiKey: 'test-key' },
- });
- const result = await provider.callApi('test prompt');
- expect(result.error).toBe('API call error: API error message');
- });
- it('should handle malformed API response', async () => {
- const mockResponse = {
- data: {
- choices: [],
- },
- cached: false,
- status: 200,
- statusText: 'OK',
- };
- jest.mocked(fetchWithCache).mockResolvedValue(mockResponse);
- const provider = new AI21ChatCompletionProvider('jamba-1.5-mini', {
- config: { apiKey: 'test-key' },
- });
- const result = await provider.callApi('test prompt');
- expect(result.error).toContain('Malformed response data');
- });
- it('should handle network errors', async () => {
- jest.mocked(fetchWithCache).mockRejectedValue(new Error('Network error'));
- const provider = new AI21ChatCompletionProvider('jamba-1.5-mini', {
- config: { apiKey: 'test-key' },
- });
- const result = await provider.callApi('test prompt');
- expect(result.error).toBe('API call error: Error: Network error');
- });
- it('should calculate cost correctly', async () => {
- const mockResponse = {
- data: {
- choices: [
- {
- message: {
- content: 'test response',
- },
- },
- ],
- usage: {
- total_tokens: 10,
- prompt_tokens: 5,
- completion_tokens: 5,
- },
- },
- cached: false,
- status: 200,
- statusText: 'OK',
- };
- jest.mocked(fetchWithCache).mockResolvedValue(mockResponse);
- const provider = new AI21ChatCompletionProvider('jamba-1.5-mini', {
- config: { apiKey: 'test-key' },
- });
- const result = await provider.callApi('test prompt');
- expect(result.cost).toBeDefined();
- });
- });
|