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
|
- import request from 'supertest';
- import * as httpProvider from '../../src/providers/http';
- import { createApp } from '../../src/server/server';
- import { createMockResponse } from '../util/utils';
- describe('providersRouter', () => {
- const app = createApp();
- beforeEach(() => {
- jest.clearAllMocks();
- global.fetch = jest.fn() as unknown as typeof fetch;
- });
- it('should parse and load the provider with JSON body', async () => {
- const mockCallApi = jest.fn().mockResolvedValue({ output: 'Mocked response' });
- jest.spyOn(httpProvider.HttpProvider.prototype, 'callApi').mockImplementation(mockCallApi);
- jest.mocked(fetch).mockResolvedValue(
- createMockResponse({
- json: jest.fn().mockResolvedValue({
- changes_needed: true,
- changes_needed_reason: 'Test reason',
- changes_needed_suggestions: ['Test suggestion 1', 'Test suggestion 2'],
- }),
- }),
- );
- const testProvider = {
- id: 'http://example.com/api',
- config: {
- url: 'http://example.com/api',
- method: 'POST',
- headers: { 'Content-Type': 'application/json' },
- body: { key: '{{ prompt }}' },
- },
- };
- const res = await request(app).post('/api/providers/test').send(testProvider);
- expect(res.status).toBe(200);
- expect(res.body).toEqual({
- providerResponse: {
- output: 'Mocked response',
- },
- testResult: {
- changes_needed: true,
- changes_needed_reason: 'Test reason',
- changes_needed_suggestions: ['Test suggestion 1', 'Test suggestion 2'],
- },
- });
- });
- it('should parse and load the provider with YAML body', async () => {
- const mockCallApi = jest.fn().mockResolvedValue({ output: 'Mocked response' });
- jest.spyOn(httpProvider.HttpProvider.prototype, 'callApi').mockImplementation(mockCallApi);
- jest.mocked(fetch).mockResolvedValue(
- createMockResponse({
- json: jest.fn().mockResolvedValue({
- changes_needed: false,
- }),
- }),
- );
- const testProvider = {
- id: 'http://example.com/api',
- config: {
- url: 'http://example.com/api',
- method: 'POST',
- headers: { 'Content-Type': 'application/json' },
- body: `
- messages:
- - role: user
- content: "{{ prompt }}"
- `,
- },
- };
- const res = await request(app).post('/api/providers/test').send(testProvider);
- expect(res.status).toBe(200);
- expect(res.body.providerResponse.output).toBe('Mocked response');
- expect(mockCallApi).toHaveBeenCalledWith('Hello, world!', expect.any(Object));
- });
- it('should handle non-JSON content types correctly', async () => {
- const mockCallApi = jest.fn().mockResolvedValue({ output: 'Mocked response' });
- jest.spyOn(httpProvider.HttpProvider.prototype, 'callApi').mockImplementation(mockCallApi);
- jest.mocked(fetch).mockResolvedValue(
- createMockResponse({
- json: jest.fn().mockResolvedValue({
- changes_needed: false,
- }),
- }),
- );
- const testProvider = {
- id: 'http://example.com/api',
- config: {
- url: 'http://example.com/api',
- method: 'POST',
- headers: { 'Content-Type': 'text/plain' },
- body: 'Raw text body with {{ prompt }}',
- },
- };
- const res = await request(app).post('/api/providers/test').send(testProvider);
- expect(res.status).toBe(200);
- expect(res.body.providerResponse.output).toBe('Mocked response');
- });
- });
|