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

providers.test.ts 3.4 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
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
  1. import request from 'supertest';
  2. import * as httpProvider from '../../src/providers/http';
  3. import { createApp } from '../../src/server/server';
  4. import { createMockResponse } from '../util/utils';
  5. describe('providersRouter', () => {
  6. const app = createApp();
  7. beforeEach(() => {
  8. jest.clearAllMocks();
  9. global.fetch = jest.fn() as unknown as typeof fetch;
  10. });
  11. it('should parse and load the provider with JSON body', async () => {
  12. const mockCallApi = jest.fn().mockResolvedValue({ output: 'Mocked response' });
  13. jest.spyOn(httpProvider.HttpProvider.prototype, 'callApi').mockImplementation(mockCallApi);
  14. jest.mocked(fetch).mockResolvedValue(
  15. createMockResponse({
  16. json: jest.fn().mockResolvedValue({
  17. changes_needed: true,
  18. changes_needed_reason: 'Test reason',
  19. changes_needed_suggestions: ['Test suggestion 1', 'Test suggestion 2'],
  20. }),
  21. }),
  22. );
  23. const testProvider = {
  24. id: 'http://example.com/api',
  25. config: {
  26. url: 'http://example.com/api',
  27. method: 'POST',
  28. headers: { 'Content-Type': 'application/json' },
  29. body: { key: '{{ prompt }}' },
  30. },
  31. };
  32. const res = await request(app).post('/api/providers/test').send(testProvider);
  33. expect(res.status).toBe(200);
  34. expect(res.body).toEqual({
  35. providerResponse: {
  36. output: 'Mocked response',
  37. },
  38. testResult: {
  39. changes_needed: true,
  40. changes_needed_reason: 'Test reason',
  41. changes_needed_suggestions: ['Test suggestion 1', 'Test suggestion 2'],
  42. },
  43. });
  44. });
  45. it('should parse and load the provider with YAML body', async () => {
  46. const mockCallApi = jest.fn().mockResolvedValue({ output: 'Mocked response' });
  47. jest.spyOn(httpProvider.HttpProvider.prototype, 'callApi').mockImplementation(mockCallApi);
  48. jest.mocked(fetch).mockResolvedValue(
  49. createMockResponse({
  50. json: jest.fn().mockResolvedValue({
  51. changes_needed: false,
  52. }),
  53. }),
  54. );
  55. const testProvider = {
  56. id: 'http://example.com/api',
  57. config: {
  58. url: 'http://example.com/api',
  59. method: 'POST',
  60. headers: { 'Content-Type': 'application/json' },
  61. body: `
  62. messages:
  63. - role: user
  64. content: "{{ prompt }}"
  65. `,
  66. },
  67. };
  68. const res = await request(app).post('/api/providers/test').send(testProvider);
  69. expect(res.status).toBe(200);
  70. expect(res.body.providerResponse.output).toBe('Mocked response');
  71. expect(mockCallApi).toHaveBeenCalledWith('Hello, world!', expect.any(Object));
  72. });
  73. it('should handle non-JSON content types correctly', async () => {
  74. const mockCallApi = jest.fn().mockResolvedValue({ output: 'Mocked response' });
  75. jest.spyOn(httpProvider.HttpProvider.prototype, 'callApi').mockImplementation(mockCallApi);
  76. jest.mocked(fetch).mockResolvedValue(
  77. createMockResponse({
  78. json: jest.fn().mockResolvedValue({
  79. changes_needed: false,
  80. }),
  81. }),
  82. );
  83. const testProvider = {
  84. id: 'http://example.com/api',
  85. config: {
  86. url: 'http://example.com/api',
  87. method: 'POST',
  88. headers: { 'Content-Type': 'text/plain' },
  89. body: 'Raw text body with {{ prompt }}',
  90. },
  91. };
  92. const res = await request(app).post('/api/providers/test').send(testProvider);
  93. expect(res.status).toBe(200);
  94. expect(res.body.providerResponse.output).toBe('Mocked response');
  95. });
  96. });
Tip!

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

Comments

Loading...