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

html.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
  1. import { handleContainsHtml, handleIsHtml } from '../../src/assertions/html';
  2. import type { ApiProvider, AssertionParams, AtomicTestCase } from '../../src/types';
  3. const mockProvider: ApiProvider = {
  4. id: () => 'mock',
  5. callApi: async () => ({ output: 'mock' }),
  6. };
  7. const defaultParams = {
  8. baseType: 'contains-html' as const,
  9. context: {
  10. vars: {},
  11. test: {} as AtomicTestCase,
  12. prompt: 'test prompt',
  13. logProbs: undefined,
  14. provider: mockProvider,
  15. providerResponse: { output: 'test' },
  16. },
  17. output: 'test',
  18. providerResponse: { output: 'test' },
  19. test: {} as AtomicTestCase,
  20. };
  21. describe('handleContainsHtml', () => {
  22. it('should pass when output contains HTML tags', () => {
  23. const params: AssertionParams = {
  24. ...defaultParams,
  25. assertion: { type: 'contains-html' },
  26. outputString: '<div>Hello World</div>',
  27. inverse: false,
  28. };
  29. const result = handleContainsHtml(params);
  30. expect(result).toEqual({
  31. pass: true,
  32. score: 1,
  33. reason: 'Assertion passed',
  34. assertion: params.assertion,
  35. });
  36. });
  37. it('should fail when output does not contain HTML tags', () => {
  38. const params: AssertionParams = {
  39. ...defaultParams,
  40. assertion: { type: 'contains-html' },
  41. outputString: 'Just plain text without any HTML',
  42. inverse: false,
  43. };
  44. const result = handleContainsHtml(params);
  45. expect(result).toEqual({
  46. pass: false,
  47. score: 0,
  48. reason: 'Expected output to contain HTML content',
  49. assertion: params.assertion,
  50. });
  51. });
  52. it('should detect various HTML tags', () => {
  53. const htmlExamples = [
  54. '<p>paragraph</p>',
  55. '<a href="link">text</a>',
  56. '<img src="image.jpg" />',
  57. '<h1>heading</h1>',
  58. '<div class="container"><span>nested</span></div>',
  59. '<!DOCTYPE html><html><head><title>Test</title></head><body>Content</body></html>',
  60. 'Some text with <strong>emphasis</strong> in it',
  61. '<input type="text" value="test" />',
  62. '<br />',
  63. '<table><tr><td>cell</td></tr></table>',
  64. '<div>Text with &amp; entity</div>',
  65. '<p>Multiple &nbsp; HTML &lt; entities &gt;</p>',
  66. '<custom-element attr="value">Web component</custom-element>',
  67. '<div data-id="123" class="test">Modern HTML</div>',
  68. ];
  69. htmlExamples.forEach((html) => {
  70. const params: AssertionParams = {
  71. ...defaultParams,
  72. assertion: { type: 'contains-html' },
  73. outputString: html,
  74. inverse: false,
  75. };
  76. const result = handleContainsHtml(params);
  77. expect(result.pass).toBe(true);
  78. expect(result.score).toBe(1);
  79. });
  80. });
  81. it('should not detect false positives', () => {
  82. const nonHtmlExamples = [
  83. 'Just plain text',
  84. '2 < 3 and 4 > 1',
  85. 'email@example.com',
  86. 'Math: a < b > c',
  87. '< div >', // space after < means it's not a valid HTML tag
  88. 'Generic <example> text', // Single unpaired tag without HTML indicators
  89. 'if (a<b) { return c>d; }', // Code with comparison operators
  90. 'The price is <$50',
  91. 'Email me at <john@example.com>',
  92. ];
  93. nonHtmlExamples.forEach((text) => {
  94. const params: AssertionParams = {
  95. ...defaultParams,
  96. assertion: { type: 'contains-html' },
  97. outputString: text,
  98. inverse: false,
  99. };
  100. const result = handleContainsHtml(params);
  101. expect(result.pass).toBe(false);
  102. expect(result.score).toBe(0);
  103. });
  104. });
  105. it('should handle inverse assertion correctly', () => {
  106. const params: AssertionParams = {
  107. ...defaultParams,
  108. assertion: { type: 'not-contains-html' },
  109. outputString: 'Just plain text',
  110. inverse: true,
  111. };
  112. const result = handleContainsHtml(params);
  113. expect(result).toEqual({
  114. pass: true,
  115. score: 1,
  116. reason: 'Assertion passed',
  117. assertion: params.assertion,
  118. });
  119. });
  120. it('should handle inverse assertion when HTML is present', () => {
  121. const params: AssertionParams = {
  122. ...defaultParams,
  123. assertion: { type: 'not-contains-html' },
  124. outputString: '<div>HTML content</div>',
  125. inverse: true,
  126. };
  127. const result = handleContainsHtml(params);
  128. expect(result).toEqual({
  129. pass: false,
  130. score: 0,
  131. reason: 'Expected output to not contain HTML content',
  132. assertion: params.assertion,
  133. });
  134. });
  135. it('should detect HTML even with attributes and complex structures', () => {
  136. const complexHtml = `
  137. <div class="container" id="main">
  138. <h1 style="color: red;">Title</h1>
  139. <p data-value="123">Paragraph with <a href="/link">link</a></p>
  140. <img src="image.jpg" alt="description" width="100" height="100" />
  141. </div>
  142. `;
  143. const params: AssertionParams = {
  144. ...defaultParams,
  145. assertion: { type: 'contains-html' },
  146. outputString: complexHtml,
  147. inverse: false,
  148. };
  149. const result = handleContainsHtml(params);
  150. expect(result.pass).toBe(true);
  151. expect(result.score).toBe(1);
  152. });
  153. it('should handle edge cases with minimal HTML indicators', () => {
  154. // These should pass - have multiple HTML indicators
  155. const minimalHtmlPasses = [
  156. '<div>&nbsp;</div>', // tag + entity
  157. '<!-- comment --><span>text</span>', // comment + tag
  158. '<br/><hr/>', // multiple self-closing tags
  159. 'Text with <b>bold</b> and <i>italic</i>', // multiple paired tags
  160. ];
  161. minimalHtmlPasses.forEach((html) => {
  162. const params: AssertionParams = {
  163. ...defaultParams,
  164. assertion: { type: 'contains-html' },
  165. outputString: html,
  166. inverse: false,
  167. };
  168. const result = handleContainsHtml(params);
  169. expect(result.pass).toBe(true);
  170. });
  171. // These should fail - only one HTML indicator
  172. const minimalHtmlFails = [
  173. '<custom>', // single unpaired non-standard tag
  174. '&amp;', // just an entity
  175. '<!-- comment -->', // just a comment
  176. ];
  177. minimalHtmlFails.forEach((text) => {
  178. const params: AssertionParams = {
  179. ...defaultParams,
  180. assertion: { type: 'contains-html' },
  181. outputString: text,
  182. inverse: false,
  183. };
  184. const result = handleContainsHtml(params);
  185. expect(result.pass).toBe(false);
  186. });
  187. });
  188. });
  189. describe('handleIsHtml', () => {
  190. it('should pass when output is valid HTML', () => {
  191. const validHtmlExamples = [
  192. '<div>Hello World</div>',
  193. '<!DOCTYPE html><html><head><title>Test</title></head><body>Content</body></html>',
  194. '<p>Simple paragraph</p>',
  195. '<div><span>Nested</span></div>',
  196. '<img src="test.jpg" />',
  197. '<br />',
  198. '<div class="test" id="main">Content</div>',
  199. ' <div>With whitespace</div> ',
  200. ];
  201. validHtmlExamples.forEach((html) => {
  202. const params: AssertionParams = {
  203. ...defaultParams,
  204. assertion: { type: 'is-html' },
  205. outputString: html,
  206. inverse: false,
  207. };
  208. const result = handleIsHtml(params);
  209. expect(result.pass).toBe(true);
  210. expect(result.score).toBe(1);
  211. });
  212. });
  213. it('should fail when output is not valid HTML', () => {
  214. const invalidHtmlExamples = [
  215. { output: 'Just plain text', reason: 'Output must be wrapped in HTML tags' },
  216. {
  217. output: 'Some text with <strong>HTML</strong> inside',
  218. reason: 'Output must be wrapped in HTML tags',
  219. },
  220. {
  221. output: 'Text before <div>HTML</div>',
  222. reason: 'Output must be wrapped in HTML tags',
  223. },
  224. { output: '<div>HTML</div> Text after', reason: 'Output must be wrapped in HTML tags' },
  225. {
  226. output: '<?xml version="1.0"?><root>XML</root>',
  227. reason: 'Output appears to be XML, not HTML',
  228. },
  229. { output: '', reason: 'Output is empty' },
  230. { output: ' ', reason: 'Output is empty' },
  231. { output: '<notarealtag>', reason: 'Output does not contain recognized HTML elements' },
  232. { output: '2 < 3 and 4 > 1', reason: 'Output must be wrapped in HTML tags' },
  233. ];
  234. invalidHtmlExamples.forEach(({ output, reason }) => {
  235. const params: AssertionParams = {
  236. ...defaultParams,
  237. assertion: { type: 'is-html' },
  238. outputString: output,
  239. inverse: false,
  240. };
  241. const result = handleIsHtml(params);
  242. expect(result.pass).toBe(false);
  243. expect(result.score).toBe(0);
  244. expect(result.reason).toBe(reason);
  245. });
  246. });
  247. it('should handle inverse assertion correctly', () => {
  248. const params: AssertionParams = {
  249. ...defaultParams,
  250. assertion: { type: 'not-is-html' },
  251. outputString: 'Just plain text',
  252. inverse: true,
  253. };
  254. const result = handleIsHtml(params);
  255. expect(result).toEqual({
  256. pass: true,
  257. score: 1,
  258. reason: 'Assertion passed',
  259. assertion: params.assertion,
  260. });
  261. });
  262. it('should handle inverse assertion when HTML is present', () => {
  263. const params: AssertionParams = {
  264. ...defaultParams,
  265. assertion: { type: 'not-is-html' },
  266. outputString: '<div>Valid HTML</div>',
  267. inverse: true,
  268. };
  269. const result = handleIsHtml(params);
  270. expect(result).toEqual({
  271. pass: false,
  272. score: 0,
  273. reason: 'Output is valid HTML',
  274. assertion: params.assertion,
  275. });
  276. });
  277. it('should accept HTML fragments', () => {
  278. const fragments = [
  279. '<div>Fragment without doctype</div>',
  280. '<h1>Title</h1><p>Paragraph</p>',
  281. '<ul><li>Item 1</li><li>Item 2</li></ul>',
  282. '<form><input type="text" /><button>Submit</button></form>',
  283. ];
  284. fragments.forEach((html) => {
  285. const params: AssertionParams = {
  286. ...defaultParams,
  287. assertion: { type: 'is-html' },
  288. outputString: html,
  289. inverse: false,
  290. };
  291. const result = handleIsHtml(params);
  292. expect(result.pass).toBe(true);
  293. });
  294. });
  295. it('should reject mixed content', () => {
  296. const mixedContent = [
  297. 'Here is some HTML: <div>test</div>',
  298. '<div>HTML</div> and some text',
  299. 'Text <br /> more text',
  300. ];
  301. mixedContent.forEach((content) => {
  302. const params: AssertionParams = {
  303. ...defaultParams,
  304. assertion: { type: 'is-html' },
  305. outputString: content,
  306. inverse: false,
  307. };
  308. const result = handleIsHtml(params);
  309. expect(result.pass).toBe(false);
  310. });
  311. });
  312. });
Tip!

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

Comments

Loading...