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

contextUtils.test.ts 5.4 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
  1. import { resolveContext } from '../../src/assertions/contextUtils';
  2. import * as transformUtil from '../../src/util/transform';
  3. import type { Assertion, AtomicTestCase } from '../../src/types';
  4. jest.mock('../../src/util/transform');
  5. describe('resolveContext', () => {
  6. const mockTransform = jest.mocked(transformUtil.transform);
  7. beforeEach(() => {
  8. jest.clearAllMocks();
  9. });
  10. describe('with context variable', () => {
  11. it('should return context from test.vars.context', async () => {
  12. const assertion: Assertion = { type: 'context-faithfulness' };
  13. const test: AtomicTestCase = {
  14. vars: { context: 'test context' },
  15. options: {},
  16. };
  17. const result = await resolveContext(assertion, test, 'output', 'prompt');
  18. expect(result).toBe('test context');
  19. expect(mockTransform).not.toHaveBeenCalled();
  20. });
  21. it('should use fallback context when no context variable', async () => {
  22. const assertion: Assertion = { type: 'context-recall' };
  23. const test: AtomicTestCase = { vars: {}, options: {} };
  24. const result = await resolveContext(assertion, test, 'output', 'prompt', 'fallback context');
  25. expect(result).toBe('fallback context');
  26. expect(mockTransform).not.toHaveBeenCalled();
  27. });
  28. });
  29. describe('with contextTransform', () => {
  30. it('should transform output to get context', async () => {
  31. mockTransform.mockResolvedValue('transformed context' as any);
  32. const assertion: Assertion = {
  33. type: 'context-faithfulness',
  34. contextTransform: 'output.context',
  35. };
  36. const test: AtomicTestCase = { vars: {}, options: {} };
  37. const result = await resolveContext(assertion, test, { context: 'data' }, 'prompt');
  38. expect(result).toBe('transformed context');
  39. expect(mockTransform).toHaveBeenCalledWith(
  40. 'output.context',
  41. { context: 'data' },
  42. {
  43. vars: {},
  44. prompt: { label: 'prompt' },
  45. },
  46. );
  47. });
  48. it('should prioritize contextTransform over context variable', async () => {
  49. mockTransform.mockResolvedValue('transformed context' as any);
  50. const assertion: Assertion = {
  51. type: 'context-faithfulness',
  52. contextTransform: 'output.context',
  53. };
  54. const test: AtomicTestCase = {
  55. vars: { context: 'original context' },
  56. options: {},
  57. };
  58. const result = await resolveContext(assertion, test, { context: 'data' }, 'prompt');
  59. expect(result).toBe('transformed context');
  60. expect(mockTransform).toHaveBeenCalledWith(
  61. 'output.context',
  62. { context: 'data' },
  63. {
  64. vars: { context: 'original context' },
  65. prompt: { label: 'prompt' },
  66. },
  67. );
  68. });
  69. it('should throw error if transform returns non-string', async () => {
  70. mockTransform.mockResolvedValue(123 as any);
  71. const assertion: Assertion = {
  72. type: 'context-faithfulness',
  73. contextTransform: 'output.invalid',
  74. };
  75. const test: AtomicTestCase = { vars: {}, options: {} };
  76. await expect(resolveContext(assertion, test, 'output', 'prompt')).rejects.toThrow(
  77. 'contextTransform must return a string value. Got number. Check your transform expression: output.invalid',
  78. );
  79. });
  80. it('should throw error if transform fails', async () => {
  81. mockTransform.mockRejectedValue(new Error('Transform failed'));
  82. const assertion: Assertion = {
  83. type: 'context-faithfulness',
  84. contextTransform: 'output.invalid',
  85. };
  86. const test: AtomicTestCase = { vars: {}, options: {} };
  87. await expect(resolveContext(assertion, test, 'output', 'prompt')).rejects.toThrow(
  88. "Failed to transform context using expression 'output.invalid': Transform failed",
  89. );
  90. });
  91. });
  92. describe('error cases', () => {
  93. it('should throw error when no context is available', async () => {
  94. const assertion: Assertion = { type: 'context-faithfulness' };
  95. const test: AtomicTestCase = { vars: {}, options: {} };
  96. await expect(resolveContext(assertion, test, 'output', 'prompt')).rejects.toThrow(
  97. 'Context is required for context-based assertions. Provide either a "context" variable in your test case or use "contextTransform" to extract context from the provider response.',
  98. );
  99. });
  100. it('should throw error when context is empty string', async () => {
  101. const assertion: Assertion = { type: 'context-faithfulness' };
  102. const test: AtomicTestCase = {
  103. vars: { context: '' },
  104. options: {},
  105. };
  106. await expect(resolveContext(assertion, test, 'output', 'prompt')).rejects.toThrow(
  107. 'Context is required for context-based assertions. Provide either a "context" variable in your test case or use "contextTransform" to extract context from the provider response.',
  108. );
  109. });
  110. it('should throw error when contextTransform returns empty string', async () => {
  111. mockTransform.mockResolvedValue('' as any);
  112. const assertion: Assertion = {
  113. type: 'context-faithfulness',
  114. contextTransform: 'output.empty',
  115. };
  116. const test: AtomicTestCase = { vars: {}, options: {} };
  117. await expect(resolveContext(assertion, test, 'output', 'prompt')).rejects.toThrow(
  118. 'Context is required for context-based assertions. Provide either a "context" variable in your test case or use "contextTransform" to extract context from the provider response.',
  119. );
  120. });
  121. });
  122. });
Tip!

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

Comments

Loading...