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

simulatedUser.test.ts 5.7 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
  1. import { SimulatedUser } from '../../src/providers/simulatedUser';
  2. import type { ApiProvider } from '../../src/types';
  3. import * as timeUtils from '../../src/util/time';
  4. jest.mock('../../src/util/time', () => ({
  5. sleep: jest.fn().mockResolvedValue(undefined),
  6. }));
  7. jest.mock('../../src/fetch');
  8. // Mock PromptfooSimulatedUserProvider
  9. const mockUserProviderCallApi = jest.fn().mockResolvedValue({ output: 'user response' });
  10. jest.mock('../../src/providers/promptfoo', () => {
  11. return {
  12. PromptfooSimulatedUserProvider: jest.fn().mockImplementation(() => ({
  13. callApi: mockUserProviderCallApi,
  14. id: jest.fn().mockReturnValue('mock-user-provider'),
  15. options: {},
  16. })),
  17. };
  18. });
  19. describe('SimulatedUser', () => {
  20. let simulatedUser: SimulatedUser;
  21. let originalProvider: ApiProvider;
  22. beforeEach(() => {
  23. mockUserProviderCallApi.mockClear();
  24. mockUserProviderCallApi.mockResolvedValue({ output: 'user response' });
  25. originalProvider = {
  26. id: () => 'test-agent',
  27. callApi: jest.fn().mockImplementation(async () => ({
  28. output: 'agent response',
  29. tokenUsage: { numRequests: 1 },
  30. })),
  31. };
  32. simulatedUser = new SimulatedUser({
  33. id: 'test-agent',
  34. config: {
  35. instructions: 'test instructions',
  36. maxTurns: 2,
  37. },
  38. });
  39. jest.clearAllMocks();
  40. });
  41. describe('id()', () => {
  42. it('should return the identifier', () => {
  43. expect(simulatedUser.id()).toBe('test-agent');
  44. });
  45. it('should use label as fallback identifier', () => {
  46. const userWithLabel = new SimulatedUser({
  47. label: 'label-agent',
  48. config: {},
  49. });
  50. expect(userWithLabel.id()).toBe('label-agent');
  51. });
  52. it('should use default identifier if no id or label provided', () => {
  53. const userWithoutId = new SimulatedUser({ config: {} });
  54. expect(userWithoutId.id()).toBe('agent-provider');
  55. });
  56. });
  57. describe('callApi()', () => {
  58. it('should simulate conversation between user and agent', async () => {
  59. const result = await simulatedUser.callApi('test prompt', {
  60. originalProvider,
  61. vars: { instructions: 'test instructions' },
  62. prompt: { raw: 'test', display: 'test', label: 'test' },
  63. });
  64. expect(result.output).toBeDefined();
  65. expect(result.output).toContain('User:');
  66. expect(result.output).toContain('Assistant:');
  67. expect(result.tokenUsage?.numRequests).toBe(2);
  68. expect(originalProvider.callApi).toHaveBeenCalledWith(
  69. expect.stringContaining('[{"role":"system","content":"test prompt"}'),
  70. );
  71. expect(timeUtils.sleep).not.toHaveBeenCalled();
  72. });
  73. it('should respect maxTurns configuration', async () => {
  74. const userWithMaxTurns = new SimulatedUser({
  75. config: {
  76. instructions: 'test instructions',
  77. maxTurns: 1,
  78. },
  79. });
  80. const result = await userWithMaxTurns.callApi('test prompt', {
  81. originalProvider,
  82. vars: { instructions: 'test instructions' },
  83. prompt: { raw: 'test', display: 'test', label: 'test' },
  84. });
  85. const messageCount = result.output?.split('---').length;
  86. expect(messageCount).toBe(2);
  87. expect(originalProvider.callApi).toHaveBeenCalledWith(
  88. expect.stringContaining('[{"role":"system","content":"test prompt"}'),
  89. );
  90. expect(timeUtils.sleep).not.toHaveBeenCalled();
  91. });
  92. it('should stop conversation when ###STOP### is received', async () => {
  93. // Set up an initial message exchange to have some conversation history
  94. // First call is regular exchange
  95. const mockedCallApi = jest.mocked(originalProvider.callApi);
  96. mockedCallApi.mockImplementationOnce(async () => ({
  97. output: 'initial agent response',
  98. tokenUsage: { numRequests: 1 },
  99. }));
  100. // Second call returns stop command
  101. mockUserProviderCallApi
  102. .mockResolvedValueOnce({ output: 'initial user response' }) // First user response
  103. .mockResolvedValueOnce({ output: 'stopping now ###STOP###' }); // Second user response with STOP
  104. const result = await simulatedUser.callApi('test prompt', {
  105. originalProvider,
  106. vars: { instructions: 'test instructions' },
  107. prompt: { raw: 'test', display: 'test', label: 'test' },
  108. });
  109. expect(result.output).not.toContain('stopping now ###STOP###');
  110. // The original provider should be called once for the first exchange
  111. expect(originalProvider.callApi).toHaveBeenCalledTimes(1);
  112. expect(timeUtils.sleep).not.toHaveBeenCalled();
  113. });
  114. it('should throw error if originalProvider is not provided', async () => {
  115. await expect(
  116. simulatedUser.callApi('test', {
  117. vars: {},
  118. prompt: { raw: 'test', display: 'test', label: 'test' },
  119. }),
  120. ).rejects.toThrow('Expected originalProvider to be set');
  121. });
  122. it('should handle provider delay', async () => {
  123. const providerWithDelay = {
  124. ...originalProvider,
  125. delay: 100,
  126. };
  127. const result = await simulatedUser.callApi(
  128. 'test prompt',
  129. {
  130. originalProvider: providerWithDelay,
  131. vars: { instructions: 'test instructions' },
  132. prompt: { raw: 'test', display: 'test', label: 'test' },
  133. },
  134. { includeLogProbs: false },
  135. );
  136. expect(result.output).toBeDefined();
  137. expect(providerWithDelay.callApi).toHaveBeenCalledWith(
  138. expect.stringContaining('[{"role":"system","content":"test prompt"}'),
  139. );
  140. expect(timeUtils.sleep).toHaveBeenCalledWith(100);
  141. });
  142. });
  143. describe('toString()', () => {
  144. it('should return correct string representation', () => {
  145. expect(simulatedUser.toString()).toBe('AgentProvider');
  146. });
  147. });
  148. });
Tip!

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

Comments

Loading...