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 2.5 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
  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, then call callApi', 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. ok: true,
  22. headers: new Headers(),
  23. redirected: false,
  24. status: 200,
  25. statusText: 'OK',
  26. type: 'basic',
  27. url: 'http://example.com',
  28. clone: jest.fn(),
  29. body: null,
  30. bodyUsed: false,
  31. arrayBuffer: jest.fn(),
  32. blob: jest.fn(),
  33. formData: jest.fn(),
  34. text: jest.fn(),
  35. }) as Response,
  36. );
  37. const testProvider = {
  38. id: 'http://example.com/api',
  39. config: {
  40. method: 'POST',
  41. headers: { 'Content-Type': 'application/json' },
  42. body: { key: '{{ prompt }}' },
  43. },
  44. };
  45. const res = await request(app).post('/api/providers/test').send(testProvider);
  46. expect(res.status).toBe(200);
  47. expect(res.body).toEqual({
  48. provider_response: {
  49. output: 'Mocked response',
  50. },
  51. test_result: {
  52. changes_needed: true,
  53. changes_needed_reason: 'Test reason',
  54. changes_needed_suggestions: ['Test suggestion 1', 'Test suggestion 2'],
  55. },
  56. });
  57. // Update this expectation to match the actual arguments
  58. expect(mockCallApi).toHaveBeenCalledWith('Hello, world!', {
  59. debug: true,
  60. prompt: {
  61. label: 'Hello, world!',
  62. raw: 'Hello, world!',
  63. },
  64. vars: expect.any(Object),
  65. });
  66. });
  67. it('should return 400 for invalid provider schema', async () => {
  68. const invalidProvider = {
  69. type: 'invalid',
  70. url: 'http://example.com/api',
  71. };
  72. const res = await request(app).post('/api/providers/test').send(invalidProvider);
  73. expect(res.status).toBe(400);
  74. expect(res.body).toHaveProperty('error');
  75. });
  76. });
Tip!

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

Comments

Loading...