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

templates.test.ts 10 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
  1. import nunjucks from 'nunjucks';
  2. import cliState from '../../src/cliState';
  3. import {
  4. extractVariablesFromTemplate,
  5. extractVariablesFromTemplates,
  6. getNunjucksEngine,
  7. } from '../../src/util/templates';
  8. describe('extractVariablesFromTemplate', () => {
  9. it('should extract simple variables', () => {
  10. const template = 'Hello {{ name }}, welcome to {{ place }}!';
  11. expect(extractVariablesFromTemplate(template)).toEqual(['name', 'place']);
  12. });
  13. it('should extract variables without spaces', () => {
  14. const template = 'Hello {{name}}, welcome to {{place}}!';
  15. expect(extractVariablesFromTemplate(template)).toEqual(['name', 'place']);
  16. });
  17. it('should extract variables with dot notation', () => {
  18. const template = 'Hello {{ user.name }}, your score is {{ game.score }}!';
  19. expect(extractVariablesFromTemplate(template)).toEqual(['user.name', 'game.score']);
  20. });
  21. it('should extract variables with underscores', () => {
  22. const template = 'Your order {{ order_id }} will arrive on {{ delivery_date }}.';
  23. expect(extractVariablesFromTemplate(template)).toEqual(['order_id', 'delivery_date']);
  24. });
  25. it('should extract variables with numbers', () => {
  26. const template = 'Player1: {{ player1 }}, Player2: {{ player2 }}';
  27. expect(extractVariablesFromTemplate(template)).toEqual(['player1', 'player2']);
  28. });
  29. it('should extract variables used in filters', () => {
  30. const template = '{{ name | capitalize }} - {{ date | date("yyyy-MM-dd") }}';
  31. expect(extractVariablesFromTemplate(template)).toEqual(['name', 'date']);
  32. });
  33. it('should extract variables used in complex expressions', () => {
  34. const template = '{% if user.age > 18 %}Welcome, {{ user.name }}!{% endif %}';
  35. expect(extractVariablesFromTemplate(template)).toEqual(['user.age', 'user.name']);
  36. });
  37. it('should extract variables from for loops', () => {
  38. const template = '{% for item in items %}{{ item.name }}{% endfor %}';
  39. expect(extractVariablesFromTemplate(template)).toEqual(['item.name', 'items']);
  40. });
  41. it('should extract variables with multiple occurrences', () => {
  42. const template = '{{ name }} {{ age }} {{ name }}';
  43. expect(extractVariablesFromTemplate(template)).toEqual(['name', 'age']);
  44. });
  45. it('should not extract variables from comments', () => {
  46. const template = '{# This is a comment with {{ variable }} #}{{ actual_variable }}';
  47. expect(extractVariablesFromTemplate(template)).toEqual(['actual_variable']);
  48. });
  49. it('should handle empty templates', () => {
  50. const template = '';
  51. expect(extractVariablesFromTemplate(template)).toEqual([]);
  52. });
  53. it('should handle templates without variables', () => {
  54. const template = 'This is a static template without variables.';
  55. expect(extractVariablesFromTemplate(template)).toEqual([]);
  56. });
  57. });
  58. describe('extractVariablesFromTemplates', () => {
  59. it('should extract variables from multiple templates', () => {
  60. const templates = [
  61. 'Hello {{ name }}, welcome to {{ place }}!',
  62. '{{ user.age }} years old',
  63. '{% for item in items %}{{ item.name }}{% endfor %}',
  64. ];
  65. const result = extractVariablesFromTemplates(templates);
  66. expect(result).toEqual(['name', 'place', 'user.age', 'item.name', 'items']);
  67. });
  68. it('should handle empty array of templates', () => {
  69. const templates: string[] = [];
  70. const result = extractVariablesFromTemplates(templates);
  71. expect(result).toEqual([]);
  72. });
  73. it('should deduplicate variables across templates', () => {
  74. const templates = ['Hello {{ name }}!', 'Welcome, {{ name }}!', '{{ age }} years old'];
  75. const result = extractVariablesFromTemplates(templates);
  76. expect(result).toEqual(['name', 'age']);
  77. });
  78. });
  79. describe('getNunjucksEngine', () => {
  80. const originalEnv = process.env;
  81. beforeEach(() => {
  82. jest.resetModules();
  83. process.env = { ...originalEnv };
  84. });
  85. afterEach(() => {
  86. process.env = originalEnv;
  87. });
  88. it('should return a nunjucks environment by default', () => {
  89. const engine = getNunjucksEngine();
  90. expect(engine).toBeInstanceOf(nunjucks.Environment);
  91. expect(engine.renderString('Hello {{ name }}', { name: 'World' })).toBe('Hello World');
  92. });
  93. it('should return a simple render function when PROMPTFOO_DISABLE_TEMPLATING is set', () => {
  94. process.env.PROMPTFOO_DISABLE_TEMPLATING = 'true';
  95. const engine = getNunjucksEngine();
  96. expect(engine.renderString('Hello {{ name }}', { name: 'World' })).toBe('Hello {{ name }}');
  97. });
  98. it('should return a nunjucks environment when isGrader is true, regardless of PROMPTFOO_DISABLE_TEMPLATING', () => {
  99. process.env.PROMPTFOO_DISABLE_TEMPLATING = 'true';
  100. const engine = getNunjucksEngine({}, false, true);
  101. expect(engine).toBeInstanceOf(nunjucks.Environment);
  102. expect(engine.renderString('Hello {{ name }}', { name: 'Grader' })).toBe('Hello Grader');
  103. });
  104. it('should use nunjucks when isGrader is true, even if PROMPTFOO_DISABLE_TEMPLATING is set', () => {
  105. process.env.PROMPTFOO_DISABLE_TEMPLATING = 'true';
  106. const engine = getNunjucksEngine({}, false, true);
  107. expect(engine).toBeInstanceOf(nunjucks.Environment);
  108. expect(engine.renderString('Hello {{ name }}', { name: 'Grader' })).toBe('Hello Grader');
  109. });
  110. it('should add custom filters when provided', () => {
  111. const customFilters = {
  112. uppercase: (str: string) => str.toUpperCase(),
  113. add: (a: number, b: number) => (a + b).toString(),
  114. };
  115. const engine = getNunjucksEngine(customFilters);
  116. expect(engine.renderString('{{ "hello" | uppercase }}', {})).toBe('HELLO');
  117. expect(engine.renderString('{{ 5 | add(3) }}', {})).toBe('8');
  118. });
  119. it('should add environment variables as globals under "env"', () => {
  120. process.env.TEST_VAR = 'test_value';
  121. const engine = getNunjucksEngine();
  122. expect(engine.renderString('{{ env.TEST_VAR }}', {})).toBe('test_value');
  123. });
  124. it('should throw an error when throwOnUndefined is true and a variable is undefined', () => {
  125. const engine = getNunjucksEngine({}, true);
  126. expect(() => {
  127. engine.renderString('{{ undefined_var }}', {});
  128. }).toThrow(/attempted to output null or undefined value/);
  129. });
  130. it('should not throw an error when throwOnUndefined is false and a variable is undefined', () => {
  131. const engine = getNunjucksEngine({}, false);
  132. expect(() => {
  133. engine.renderString('{{ undefined_var }}', {});
  134. }).not.toThrow();
  135. });
  136. it('should respect all parameters when provided', () => {
  137. process.env.PROMPTFOO_DISABLE_TEMPLATING = 'true';
  138. const customFilters = {
  139. double: (n: number) => (n * 2).toString(),
  140. };
  141. const engine = getNunjucksEngine(customFilters, true, true);
  142. expect(engine).toBeInstanceOf(nunjucks.Environment);
  143. expect(engine.renderString('{{ 5 | double }}', {})).toBe('10');
  144. expect(() => {
  145. engine.renderString('{{ undefined_var }}', {});
  146. }).toThrow(/attempted to output null or undefined value/);
  147. });
  148. describe('environment variables as globals', () => {
  149. const originalEnv = process.env;
  150. beforeEach(() => {
  151. jest.resetModules();
  152. process.env = { ...originalEnv };
  153. jest.isolateModules(() => {
  154. jest.doMock('../../src/cliState', () => ({
  155. default: {
  156. config: {},
  157. },
  158. }));
  159. });
  160. });
  161. afterEach(() => {
  162. process.env = originalEnv;
  163. jest.resetModules();
  164. });
  165. it('should add environment variables as globals by default', () => {
  166. process.env.TEST_VAR = 'test_value';
  167. const engine = getNunjucksEngine();
  168. expect(engine.renderString('{{ env.TEST_VAR }}', {})).toBe('test_value');
  169. });
  170. it('should merge cliState.config.env with process.env', () => {
  171. const initialConfig = { ...cliState.config };
  172. cliState.config = {
  173. env: {
  174. PROCESS_VAR: 'overridden_value',
  175. CONFIG_VAR: 'config_value',
  176. },
  177. };
  178. const engine = getNunjucksEngine();
  179. const rendered = engine.renderString('{{ env.PROCESS_VAR }}', {});
  180. expect(rendered).toBe('overridden_value');
  181. expect(engine.renderString('{{ env.CONFIG_VAR }}', {})).toBe('config_value');
  182. cliState.config = initialConfig;
  183. });
  184. it('should handle undefined cliState.config', () => {
  185. process.env.TEST_VAR = 'test_value';
  186. jest.isolateModules(() => {
  187. jest.doMock('../../src/cliState', () => ({
  188. default: {
  189. config: undefined,
  190. },
  191. }));
  192. });
  193. const engine = getNunjucksEngine();
  194. expect(engine.renderString('{{ env.TEST_VAR }}', {})).toBe('test_value');
  195. });
  196. it('should handle undefined cliState.config.env', () => {
  197. process.env.TEST_VAR = 'test_value';
  198. jest.isolateModules(() => {
  199. jest.doMock('../../src/cliState', () => ({
  200. default: {
  201. config: {
  202. env: undefined,
  203. },
  204. },
  205. }));
  206. });
  207. const engine = getNunjucksEngine();
  208. expect(engine.renderString('{{ env.TEST_VAR }}', {})).toBe('test_value');
  209. });
  210. it('should not add environment variables when PROMPTFOO_DISABLE_TEMPLATE_ENV_VARS is true', () => {
  211. process.env.TEST_VAR = 'test_value';
  212. process.env.PROMPTFOO_DISABLE_TEMPLATE_ENV_VARS = 'true';
  213. // Update cliState mock
  214. jest.mock('../../src/cliState', () => ({
  215. default: {
  216. config: {
  217. env: {
  218. CONFIG_VAR: 'config_value',
  219. },
  220. },
  221. },
  222. }));
  223. const engine = getNunjucksEngine();
  224. expect(engine.renderString('{{ env.TEST_VAR }}', {})).toBe('');
  225. expect(engine.renderString('{{ env.CONFIG_VAR }}', {})).toBe('');
  226. });
  227. it('should not add environment variables when in self-hosted mode by default', () => {
  228. process.env.TEST_VAR = 'test_value';
  229. process.env.PROMPTFOO_SELF_HOSTED = 'true';
  230. // Update cliState mock
  231. jest.mock('../../src/cliState', () => ({
  232. default: {
  233. config: {
  234. env: {
  235. CONFIG_VAR: 'config_value',
  236. },
  237. },
  238. },
  239. }));
  240. const engine = getNunjucksEngine();
  241. expect(engine.renderString('{{ env.TEST_VAR }}', {})).toBe('');
  242. expect(engine.renderString('{{ env.CONFIG_VAR }}', {})).toBe('');
  243. });
  244. });
  245. });
Tip!

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

Comments

Loading...