5858import modelengine .jade .common .globalization .LocaleService ;
5959
6060import org .junit .jupiter .api .Assertions ;
61- import org .junit .jupiter .api .BeforeEach ;
6261import org .junit .jupiter .api .Disabled ;
6362import org .junit .jupiter .api .Test ;
6463import org .junit .jupiter .api .extension .ExtendWith ;
@@ -119,19 +118,6 @@ public class LlmComponentTest {
119118 @ Mock
120119 private AippModelCenter aippModelCenter ;
121120
122- @ BeforeEach
123- void setUp () {
124- Mockito .when (toolProvider .getTool (any ())).thenReturn (Collections .emptyList ());
125- doAnswer (invocationOnMock -> {
126- Object advice = invocationOnMock .getArgument (0 );
127- Object context = invocationOnMock .getArgument (1 );
128- return new PromptBuilderStub ().build (ObjectUtils .cast (advice ), ObjectUtils .cast (context ));
129- }).when (this .promptBuilderChain ).build (any (), any ());
130-
131- when (this .aippModelCenter .getModelAccessInfo (any (), any (), any ())).thenReturn (
132- ModelAccessInfo .builder ().tag ("tag" ).build ());
133- }
134-
135121 static class PromptBuilderStub implements PromptBuilder {
136122 @ Override
137123 public Optional <PromptMessage > build (UserAdvice userAdvice , Map <String , Object > context ) {
@@ -232,6 +218,7 @@ public List<ToolInfo> getTool(List<String> name) {
232218 @ Disabled ("多线程阻塞,无法唤醒" )
233219 void shouldOkWhenWaterFlowAgentWithoutAsyncTool () throws InterruptedException {
234220 // stub
221+ this .prepareModel ();
235222 AbstractAgent <Prompt , Prompt > agent = this .getWaterFlowAgent (this .buildChatStreamModel (null ), false );
236223 LlmComponent llmComponent = getLlmComponent (agent );
237224
@@ -253,6 +240,7 @@ void shouldOkWhenWaterFlowAgentWithoutAsyncTool() throws InterruptedException {
253240 @ Test
254241 void shouldFailWhenWaterFlowAgentThrowException () throws InterruptedException {
255242 // stub
243+ this .prepareModel ();
256244 AbstractAgent <Prompt , Prompt > agent = this .getWaterFlowAgent (this .buildChatStreamModel ("exceptionMsg" ), false );
257245 LlmComponent llmComponent = getLlmComponent (agent );
258246
@@ -268,6 +256,7 @@ void shouldFailWhenWaterFlowAgentThrowException() throws InterruptedException {
268256 @ Disabled ("多线程阻塞,无法唤醒" )
269257 void shouldOkWhenWaterFlowAgentWithAsyncTool () throws InterruptedException {
270258 // stub
259+ this .prepareModel ();
271260 AbstractAgent <Prompt , Prompt > agent = this .getWaterFlowAgent (this .buildChatStreamModel (null ), true );
272261 LlmComponent llmComponent = getLlmComponent (agent );
273262
@@ -305,6 +294,7 @@ void shouldOkWhenWaterFlowAgentWithAsyncTool() throws InterruptedException {
305294 @ Test
306295 void shouldOkWhenNoTool () throws InterruptedException {
307296 // stub
297+ this .prepareModel ();
308298 AiProcessFlow <Prompt , Prompt > testAgent = AiFlows .<Prompt >create ()
309299 .map (m -> ObjectUtils .<Prompt >cast (ChatMessages .from (new AiMessage ("bad" ))))
310300 .close ();
@@ -322,6 +312,7 @@ void shouldOkWhenNoTool() throws InterruptedException {
322312 @ Test
323313 void shouldFailedWhenNoTool () throws InterruptedException {
324314 // stub
315+ this .prepareModel ();
325316 AiProcessFlow <Prompt , Prompt > testAgent = AiFlows .<Prompt >create ().just (m -> {
326317 int err = 1 / 0 ;
327318 }).close ();
@@ -341,6 +332,7 @@ void shouldFailedWhenNoTool() throws InterruptedException {
341332 void shouldOkWhenUseWorkflowNoReturn () throws InterruptedException {
342333 AtomicReference <Prompt > prompt = new AtomicReference <>();
343334 // stub
335+ this .prepareModel ();
344336 AiProcessFlow <Prompt , Prompt > testAgent = AiFlows .<Prompt >create ()
345337 .just (prompt ::set )
346338 .map (m -> ObjectUtils .<Prompt >cast (ChatMessages .from (new ToolMessage ("1" , "\" tool_async\" " ))))
@@ -372,6 +364,7 @@ void shouldOkWhenUseWorkflowNoReturn() throws InterruptedException {
372364 @ Test
373365 void shouldOkWhenUseWorkflowNormalReturn () throws InterruptedException {
374366 // stub
367+ this .prepareModel ();
375368 AtomicBoolean flag = new AtomicBoolean (false );
376369 List <Prompt > prompts = new ArrayList <>();
377370 AiProcessFlow <Prompt , Prompt > testAgent = AiFlows .<Prompt >create ().just (m -> prompts .add (m )).map (m -> {
@@ -428,6 +421,7 @@ private void generateBusinessDataAndCallBack(String childInstanceId, Map<String,
428421 @ Test
429422 void shouldOkWhenUseMaxMemoryRounds () throws InterruptedException {
430423 // stub
424+ this .prepareModel ();
431425 AiProcessFlow <Prompt , Prompt > testAgent = AiFlows .<Prompt >create ().just (m -> {
432426 List <? extends ChatMessage > messages = m .messages ();
433427 Assertions .assertEquals (2 , messages .size ());
@@ -450,6 +444,7 @@ void shouldOkWhenUseMaxMemoryRounds() throws InterruptedException {
450444 @ Test
451445 void shouldFailLLmNodeWhenHandleGivenWorkflowException () throws InterruptedException {
452446 // given
447+ this .prepareModel ();
453448 AbstractAgent <Prompt , Prompt > agent = this .getWaterFlowAgent (this .buildChatStreamModel (null ), true );
454449 LlmComponent llmComponent = getLlmComponent (agent );
455450
@@ -501,4 +496,16 @@ private LlmComponent getLlmComponent(final AbstractAgent<Prompt, Prompt> agent)
501496 return new LlmComponent (flowInstanceService , metaInstanceService , toolProvider , agent , aippLogService ,
502497 aippLogStreamService , client , serializer , localeService , aippModelCenter , promptBuilderChain );
503498 }
499+
500+ private void prepareModel () {
501+ Mockito .when (toolProvider .getTool (any ())).thenReturn (Collections .emptyList ());
502+ doAnswer (invocationOnMock -> {
503+ Object advice = invocationOnMock .getArgument (0 );
504+ Object context = invocationOnMock .getArgument (1 );
505+ return new PromptBuilderStub ().build (ObjectUtils .cast (advice ), ObjectUtils .cast (context ));
506+ }).when (this .promptBuilderChain ).build (any (), any ());
507+
508+ when (this .aippModelCenter .getModelAccessInfo (any (), any (), any ())).thenReturn (
509+ ModelAccessInfo .builder ().tag ("tag" ).build ());
510+ }
504511}
0 commit comments