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

apiHealth.test.ts 3.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
  1. import { getRemoteHealthUrl } from '../../src/redteam/remoteGeneration';
  2. import { checkRemoteHealth } from '../../src/util/apiHealth';
  3. const mockedFetch = jest.mocked(jest.fn());
  4. global.fetch = mockedFetch;
  5. const mockCloudConfig = {
  6. isEnabled: jest.fn().mockReturnValue(false),
  7. getApiHost: jest.fn().mockReturnValue('https://custom.api.com'),
  8. };
  9. jest.mock('../../src/globalConfig/cloud', () => ({
  10. CloudConfig: jest.fn().mockImplementation(() => mockCloudConfig),
  11. }));
  12. describe('API Health Utilities', () => {
  13. beforeEach(() => {
  14. jest.clearAllMocks();
  15. delete process.env.PROMPTFOO_DISABLE_REDTEAM_REMOTE_GENERATION;
  16. delete process.env.PROMPTFOO_REMOTE_GENERATION_URL;
  17. mockCloudConfig.isEnabled.mockReturnValue(false);
  18. mockCloudConfig.getApiHost.mockReturnValue('https://custom.api.com');
  19. });
  20. describe('getRemoteHealthUrl', () => {
  21. it('should return null when remote generation is disabled', () => {
  22. process.env.PROMPTFOO_DISABLE_REDTEAM_REMOTE_GENERATION = 'true';
  23. expect(getRemoteHealthUrl()).toBeNull();
  24. });
  25. it('should use custom URL when provided', () => {
  26. process.env.PROMPTFOO_REMOTE_GENERATION_URL = 'https://custom-api.example.com/task';
  27. expect(getRemoteHealthUrl()).toBe('https://custom-api.example.com/health');
  28. });
  29. it('should use cloud config when enabled', () => {
  30. mockCloudConfig.isEnabled.mockReturnValue(true);
  31. mockCloudConfig.getApiHost.mockReturnValue('https://cloud.example.com');
  32. expect(getRemoteHealthUrl()).toBe('https://cloud.example.com/health');
  33. });
  34. it('should use default URL when no configuration is provided', () => {
  35. mockCloudConfig.isEnabled.mockReturnValue(false);
  36. expect(getRemoteHealthUrl()).toBe('https://api.promptfoo.app/health');
  37. });
  38. });
  39. describe('checkRemoteHealth', () => {
  40. it('should return OK status when API is healthy', async () => {
  41. mockCloudConfig.isEnabled.mockReturnValue(false);
  42. mockedFetch.mockResolvedValueOnce({
  43. ok: true,
  44. json: () => Promise.resolve({ status: 'OK' }),
  45. } as Response);
  46. const result = await checkRemoteHealth('https://test.api/health');
  47. expect(result.status).toBe('OK');
  48. expect(result.message).toBe('Cloud API is healthy');
  49. });
  50. it('should include custom endpoint message when cloud config is enabled', async () => {
  51. mockCloudConfig.isEnabled.mockReturnValue(true);
  52. mockedFetch.mockResolvedValueOnce({
  53. ok: true,
  54. json: () => Promise.resolve({ status: 'OK' }),
  55. } as Response);
  56. const result = await checkRemoteHealth('https://test.api/health');
  57. expect(result.status).toBe('OK');
  58. expect(result.message).toBe('Cloud API is healthy (using custom endpoint)');
  59. });
  60. it('should handle non-OK response', async () => {
  61. mockedFetch.mockResolvedValueOnce({
  62. ok: false,
  63. status: 404,
  64. statusText: 'Not Found',
  65. } as Response);
  66. const result = await checkRemoteHealth('https://test.api/health');
  67. expect(result.status).toBe('ERROR');
  68. expect(result.message).toContain('Failed to connect');
  69. });
  70. it('should handle network errors', async () => {
  71. mockedFetch.mockRejectedValueOnce(new Error('Network error'));
  72. const result = await checkRemoteHealth('https://test.api/health');
  73. expect(result.status).toBe('ERROR');
  74. expect(result.message).toContain('Network error');
  75. });
  76. it('should handle malformed JSON', async () => {
  77. mockedFetch.mockResolvedValueOnce({
  78. ok: true,
  79. json: () => Promise.reject(new Error('Invalid JSON')),
  80. } as Response);
  81. const result = await checkRemoteHealth('https://test.api/health');
  82. expect(result.status).toBe('ERROR');
  83. expect(result.message).toContain('Invalid JSON');
  84. });
  85. });
  86. });
Tip!

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

Comments

Loading...