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

finishReason.test.ts 4.2 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
  1. import { describe, expect, it } from '@jest/globals';
  2. import { handleFinishReason } from '../../src/assertions/finishReason';
  3. import type { AssertionParams } from '../../src/types';
  4. describe('finishReason assertion', () => {
  5. it('should pass when finish reason matches', () => {
  6. const params: Partial<AssertionParams> = {
  7. assertion: { type: 'finish-reason', value: 'stop' },
  8. providerResponse: { finishReason: 'stop' },
  9. };
  10. const result = handleFinishReason(params as AssertionParams);
  11. expect(result.pass).toBe(true);
  12. expect(result.score).toBe(1);
  13. });
  14. it('should fail when stop reason does not match', () => {
  15. const params: Partial<AssertionParams> = {
  16. assertion: { type: 'finish-reason', value: 'stop' },
  17. providerResponse: { finishReason: 'length' },
  18. };
  19. const result = handleFinishReason(params as AssertionParams);
  20. expect(result.pass).toBe(false);
  21. expect(result.score).toBe(0);
  22. });
  23. it('should fail when provider does not return stop reason', () => {
  24. const params: Partial<AssertionParams> = {
  25. assertion: { type: 'finish-reason', value: 'stop' },
  26. providerResponse: {},
  27. };
  28. const result = handleFinishReason(params as AssertionParams);
  29. expect(result.pass).toBe(false);
  30. expect(result.score).toBe(0);
  31. expect(result.reason).toContain('did not supply');
  32. });
  33. it('should handle renderedValue override', () => {
  34. const params: Partial<AssertionParams> = {
  35. assertion: { type: 'finish-reason', value: 'stop' },
  36. renderedValue: 'length',
  37. providerResponse: { finishReason: 'length' },
  38. };
  39. const result = handleFinishReason(params as AssertionParams);
  40. expect(result.pass).toBe(true);
  41. expect(result.score).toBe(1);
  42. });
  43. it('should prioritize renderedValue over assertion.value', () => {
  44. const params: Partial<AssertionParams> = {
  45. assertion: { type: 'finish-reason', value: 'stop' },
  46. renderedValue: 'length',
  47. providerResponse: { finishReason: 'stop' },
  48. };
  49. const result = handleFinishReason(params as AssertionParams);
  50. expect(result.pass).toBe(false);
  51. expect(result.score).toBe(0);
  52. expect(result.reason).toContain('Expected finish reason "length" but got "stop"');
  53. });
  54. it('should throw for non-string assertion value', () => {
  55. const params: Partial<AssertionParams> = {
  56. assertion: { type: 'finish-reason', value: 123 as any },
  57. providerResponse: { finishReason: 'stop' },
  58. };
  59. expect(() => handleFinishReason(params as AssertionParams)).toThrow(
  60. '"finish-reason" assertion type must have a string value',
  61. );
  62. });
  63. it('should throw for non-string renderedValue', () => {
  64. const params: Partial<AssertionParams> = {
  65. assertion: { type: 'finish-reason', value: 'stop' },
  66. renderedValue: 123 as any,
  67. providerResponse: { finishReason: 'stop' },
  68. };
  69. expect(() => handleFinishReason(params as AssertionParams)).toThrow(
  70. '"finish-reason" assertion type must have a string value',
  71. );
  72. });
  73. it('should handle undefined finishReason gracefully', () => {
  74. const params: Partial<AssertionParams> = {
  75. assertion: { type: 'finish-reason', value: 'stop' },
  76. providerResponse: { finishReason: undefined },
  77. };
  78. const result = handleFinishReason(params as AssertionParams);
  79. expect(result.pass).toBe(false);
  80. expect(result.score).toBe(0);
  81. expect(result.reason).toContain('did not supply');
  82. });
  83. it('should perform case-insensitive matching', () => {
  84. const params: Partial<AssertionParams> = {
  85. assertion: { type: 'finish-reason', value: 'stop' },
  86. providerResponse: { finishReason: 'STOP' }, // Different case
  87. };
  88. const result = handleFinishReason(params as AssertionParams);
  89. expect(result.pass).toBe(true); // Now passes with case-insensitive comparison
  90. expect(result.score).toBe(1);
  91. expect(result.reason).toBe('Assertion passed');
  92. });
  93. it('should handle mixed case in both assertion and response', () => {
  94. const params: Partial<AssertionParams> = {
  95. assertion: { type: 'finish-reason', value: 'LENGTH' },
  96. providerResponse: { finishReason: 'length' },
  97. };
  98. const result = handleFinishReason(params as AssertionParams);
  99. expect(result.pass).toBe(true);
  100. expect(result.score).toBe(1);
  101. });
  102. });
Tip!

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

Comments

Loading...