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

util.config.default.test.ts 6.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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
  1. import path from 'path';
  2. import { loadDefaultConfig, configCache } from '../src/util/config/default';
  3. import { maybeReadConfig } from '../src/util/config/load';
  4. jest.mock('../src/util/config/load', () => ({
  5. maybeReadConfig: jest.fn(),
  6. }));
  7. describe('loadDefaultConfig', () => {
  8. beforeEach(() => {
  9. jest.resetAllMocks();
  10. jest.spyOn(process, 'cwd').mockImplementation(() => '/test/path');
  11. configCache.clear();
  12. });
  13. it('should return empty config when no config file is found', async () => {
  14. jest.mocked(maybeReadConfig).mockResolvedValue(undefined);
  15. const result = await loadDefaultConfig();
  16. expect(result).toEqual({
  17. defaultConfig: {},
  18. defaultConfigPath: undefined,
  19. });
  20. expect(maybeReadConfig).toHaveBeenCalledTimes(9);
  21. expect(maybeReadConfig).toHaveBeenNthCalledWith(
  22. 1,
  23. path.normalize('/test/path/promptfooconfig.yaml'),
  24. );
  25. });
  26. it('should return the first valid config file found', async () => {
  27. const mockConfig = { prompts: ['Some prompt'], providers: [], tests: [] };
  28. jest
  29. .mocked(maybeReadConfig)
  30. .mockResolvedValueOnce(undefined)
  31. .mockResolvedValueOnce(undefined)
  32. .mockResolvedValueOnce(mockConfig);
  33. const result = await loadDefaultConfig();
  34. expect(result).toEqual({
  35. defaultConfig: mockConfig,
  36. defaultConfigPath: path.normalize('/test/path/promptfooconfig.json'),
  37. });
  38. expect(maybeReadConfig).toHaveBeenCalledTimes(3);
  39. });
  40. it('should stop checking extensions after finding a valid config', async () => {
  41. const mockConfig = { prompts: ['Some prompt'], providers: [], tests: [] };
  42. jest.mocked(maybeReadConfig).mockResolvedValueOnce(undefined).mockResolvedValueOnce(mockConfig);
  43. await loadDefaultConfig();
  44. expect(maybeReadConfig).toHaveBeenCalledTimes(2);
  45. expect(maybeReadConfig).toHaveBeenNthCalledWith(
  46. 1,
  47. path.normalize('/test/path/promptfooconfig.yaml'),
  48. );
  49. expect(maybeReadConfig).toHaveBeenNthCalledWith(
  50. 2,
  51. path.normalize('/test/path/promptfooconfig.yml'),
  52. );
  53. });
  54. it('should use provided directory when specified', async () => {
  55. const mockConfig = { prompts: ['Some prompt'], providers: [], tests: [] };
  56. jest.mocked(maybeReadConfig).mockResolvedValueOnce(mockConfig);
  57. const customDir = '/custom/directory';
  58. const result = await loadDefaultConfig(customDir);
  59. expect(result).toEqual({
  60. defaultConfig: mockConfig,
  61. defaultConfigPath: path.join(customDir, 'promptfooconfig.yaml'),
  62. });
  63. expect(maybeReadConfig).toHaveBeenCalledWith(path.join(customDir, 'promptfooconfig.yaml'));
  64. });
  65. it('should use custom config name when provided', async () => {
  66. const mockConfig = { prompts: ['Custom config'], providers: [], tests: [] };
  67. jest.mocked(maybeReadConfig).mockResolvedValueOnce(mockConfig);
  68. const result = await loadDefaultConfig(undefined, 'redteam');
  69. expect(result).toEqual({
  70. defaultConfig: mockConfig,
  71. defaultConfigPath: path.normalize('/test/path/redteam.yaml'),
  72. });
  73. expect(maybeReadConfig).toHaveBeenCalledWith(path.normalize('/test/path/redteam.yaml'));
  74. });
  75. it('should use different caches for different config names', async () => {
  76. const mockConfig1 = { prompts: ['Config 1'], providers: [], tests: [] };
  77. const mockConfig2 = { prompts: ['Config 2'], providers: [], tests: [] };
  78. jest
  79. .mocked(maybeReadConfig)
  80. .mockResolvedValueOnce(mockConfig1)
  81. .mockResolvedValueOnce(mockConfig2);
  82. const result1 = await loadDefaultConfig(undefined, 'promptfooconfig');
  83. const result2 = await loadDefaultConfig(undefined, 'redteam');
  84. expect(result1).not.toEqual(result2);
  85. expect(result1.defaultConfig).toEqual(mockConfig1);
  86. expect(result2.defaultConfig).toEqual(mockConfig2);
  87. const cachedResult1 = await loadDefaultConfig(undefined, 'promptfooconfig');
  88. const cachedResult2 = await loadDefaultConfig(undefined, 'redteam');
  89. expect(cachedResult1).toEqual(result1);
  90. expect(cachedResult2).toEqual(result2);
  91. expect(maybeReadConfig).toHaveBeenCalledTimes(2);
  92. });
  93. it('should use different caches for different directories', async () => {
  94. const mockConfig1 = { prompts: ['Config 1'], providers: [], tests: [] };
  95. const mockConfig2 = { prompts: ['Config 2'], providers: [], tests: [] };
  96. jest
  97. .mocked(maybeReadConfig)
  98. .mockResolvedValueOnce(mockConfig1)
  99. .mockResolvedValueOnce(mockConfig2);
  100. const dir1 = '/dir1';
  101. const dir2 = '/dir2';
  102. const result1 = await loadDefaultConfig(dir1);
  103. const result2 = await loadDefaultConfig(dir2);
  104. expect(result1).not.toEqual(result2);
  105. expect(result1.defaultConfig).toEqual(mockConfig1);
  106. expect(result2.defaultConfig).toEqual(mockConfig2);
  107. const cachedResult1 = await loadDefaultConfig(dir1);
  108. const cachedResult2 = await loadDefaultConfig(dir2);
  109. expect(cachedResult1).toEqual(result1);
  110. expect(cachedResult2).toEqual(result2);
  111. expect(maybeReadConfig).toHaveBeenCalledTimes(2);
  112. });
  113. it('should use cache for subsequent calls with same parameters', async () => {
  114. const mockConfig = { prompts: ['Cached config'], providers: [], tests: [] };
  115. jest.mocked(maybeReadConfig).mockResolvedValueOnce(mockConfig);
  116. const result1 = await loadDefaultConfig();
  117. const result2 = await loadDefaultConfig();
  118. expect(result1).toEqual(result2);
  119. expect(maybeReadConfig).toHaveBeenCalledTimes(1);
  120. });
  121. it('should handle errors when reading config files', async () => {
  122. jest.mocked(maybeReadConfig).mockRejectedValue(new Error('Permission denied'));
  123. await expect(loadDefaultConfig()).rejects.toThrow('Permission denied');
  124. });
  125. it('should handle various config names', async () => {
  126. const mockConfig = { prompts: ['Test config'], providers: [], tests: [] };
  127. jest.mocked(maybeReadConfig).mockResolvedValue(mockConfig);
  128. const configNames = ['test1', 'test2', 'test3'];
  129. for (const name of configNames) {
  130. const result = await loadDefaultConfig(undefined, name);
  131. expect(result.defaultConfigPath).toContain(name);
  132. }
  133. });
  134. it('should handle interaction between configName and directory', async () => {
  135. const mockConfig = { prompts: ['Combined config'], providers: [], tests: [] };
  136. jest.mocked(maybeReadConfig).mockResolvedValue(mockConfig);
  137. const customDir = '/custom/dir';
  138. const customName = 'customconfig';
  139. const result = await loadDefaultConfig(customDir, customName);
  140. expect(result.defaultConfigPath).toEqual(path.join(customDir, `${customName}.yaml`));
  141. });
  142. });
Tip!

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

Comments

Loading...