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

cloud.test.ts 4.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
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
  1. import { fetchWithProxy } from '../../src/fetch';
  2. import { API_HOST, CloudConfig, cloudConfig } from '../../src/globalConfig/cloud';
  3. import { readGlobalConfig, writeGlobalConfigPartial } from '../../src/globalConfig/globalConfig';
  4. import logger from '../../src/logger';
  5. jest.mock('../../src/fetch');
  6. jest.mock('../../src/logger');
  7. jest.mock('../../src/globalConfig/globalConfig');
  8. describe('CloudConfig', () => {
  9. let cloudConfigInstance: CloudConfig;
  10. beforeEach(() => {
  11. jest.resetAllMocks();
  12. jest.mocked(readGlobalConfig).mockReturnValue({
  13. id: 'test-id',
  14. cloud: {
  15. appUrl: 'https://test.app',
  16. apiHost: 'https://test.api',
  17. apiKey: 'test-key',
  18. },
  19. });
  20. cloudConfigInstance = new CloudConfig();
  21. });
  22. describe('constructor', () => {
  23. it('should initialize with default values when no saved config exists', () => {
  24. jest.mocked(readGlobalConfig).mockReturnValue({
  25. id: 'test-id',
  26. });
  27. const config = new CloudConfig();
  28. expect(config.getAppUrl()).toBe('https://www.promptfoo.app');
  29. expect(config.getApiHost()).toBe(API_HOST);
  30. expect(config.getApiKey()).toBeUndefined();
  31. });
  32. it('should initialize with saved config values', () => {
  33. expect(cloudConfigInstance.getAppUrl()).toBe('https://test.app');
  34. expect(cloudConfigInstance.getApiHost()).toBe('https://test.api');
  35. expect(cloudConfigInstance.getApiKey()).toBe('test-key');
  36. });
  37. });
  38. describe('isEnabled', () => {
  39. it('should return true when apiKey exists', () => {
  40. expect(cloudConfigInstance.isEnabled()).toBe(true);
  41. });
  42. it('should return false when apiKey does not exist', () => {
  43. jest.mocked(readGlobalConfig).mockReturnValue({
  44. id: 'test-id',
  45. });
  46. const config = new CloudConfig();
  47. expect(config.isEnabled()).toBe(false);
  48. });
  49. });
  50. describe('setters and getters', () => {
  51. it('should set and get apiHost', () => {
  52. cloudConfigInstance.setApiHost('https://new.api');
  53. expect(writeGlobalConfigPartial).toHaveBeenCalledWith({
  54. cloud: expect.objectContaining({
  55. apiHost: 'https://new.api',
  56. }),
  57. });
  58. });
  59. it('should set and get apiKey', () => {
  60. cloudConfigInstance.setApiKey('new-key');
  61. expect(writeGlobalConfigPartial).toHaveBeenCalledWith({
  62. cloud: expect.objectContaining({
  63. apiKey: 'new-key',
  64. }),
  65. });
  66. });
  67. it('should set and get appUrl', () => {
  68. cloudConfigInstance.setAppUrl('https://new.app');
  69. expect(writeGlobalConfigPartial).toHaveBeenCalledWith({
  70. cloud: expect.objectContaining({
  71. appUrl: 'https://new.app',
  72. }),
  73. });
  74. });
  75. });
  76. describe('delete', () => {
  77. it('should clear cloud config', () => {
  78. cloudConfigInstance.delete();
  79. expect(writeGlobalConfigPartial).toHaveBeenCalledWith({ cloud: {} });
  80. });
  81. });
  82. describe('validateAndSetApiToken', () => {
  83. const mockResponse = {
  84. user: {
  85. id: '1',
  86. name: 'Test User',
  87. email: 'test@example.com',
  88. createdAt: new Date(),
  89. updatedAt: new Date(),
  90. },
  91. organization: {
  92. id: '1',
  93. name: 'Test Org',
  94. createdAt: new Date(),
  95. updatedAt: new Date(),
  96. },
  97. app: {
  98. url: 'https://test.app',
  99. },
  100. };
  101. it('should validate token and update config on success', async () => {
  102. const mockFetchResponse = {
  103. ok: true,
  104. json: () => Promise.resolve(mockResponse),
  105. text: () => Promise.resolve(JSON.stringify(mockResponse)),
  106. } as Response;
  107. jest.mocked(fetchWithProxy).mockResolvedValue(mockFetchResponse);
  108. const result = await cloudConfigInstance.validateAndSetApiToken(
  109. 'test-token',
  110. 'https://test.api',
  111. );
  112. expect(result).toEqual(mockResponse);
  113. expect(writeGlobalConfigPartial).toHaveBeenCalledWith({
  114. cloud: expect.objectContaining({
  115. apiKey: 'test-token',
  116. apiHost: 'https://test.api',
  117. appUrl: 'https://test.app',
  118. }),
  119. });
  120. expect(logger.info).toHaveBeenCalledWith(expect.stringContaining('Successfully logged in'));
  121. });
  122. it('should throw error on failed validation', async () => {
  123. const mockErrorResponse = {
  124. ok: false,
  125. statusText: 'Unauthorized',
  126. json: () => Promise.reject(new Error('Unauthorized')),
  127. text: () => Promise.resolve('Unauthorized'),
  128. } as Response;
  129. jest.mocked(fetchWithProxy).mockResolvedValue(mockErrorResponse);
  130. await expect(
  131. cloudConfigInstance.validateAndSetApiToken('invalid-token', 'https://test.api'),
  132. ).rejects.toThrow('Failed to validate API token: Unauthorized');
  133. });
  134. });
  135. describe('cloudConfig singleton', () => {
  136. it('should be an instance of CloudConfig', () => {
  137. expect(cloudConfig).toBeInstanceOf(CloudConfig);
  138. });
  139. });
  140. });
Tip!

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

Comments

Loading...