From 803cf60501e1581ee961baaea8d25cac48e4674a Mon Sep 17 00:00:00 2001 From: Geir Eilertsen Date: Sat, 21 Dec 2024 16:53:23 +0100 Subject: [PATCH] Refactored brain code into brain --- .../adapters/BrainSpringAiAdapter.java | 10 +- .../services/InteractionService.java | 65 +++------- .../services/LifecycleService.java | 8 +- .../marvin/robot/domain/brain/Brain.java | 97 +++++++++++++++ .../robot/domain/brain/BrainResponder.java | 40 ------- ...rain.java => ForInvokingIntelligence.java} | 4 +- .../marvin/robot/domain/brain/Teacher.java | 8 +- .../domain/brain/BrainResponderTest.java | 50 -------- .../marvin/robot/domain/brain/BrainTest.java | 113 ++++++++++++++++++ .../robot/domain/brain/TeacherTest.java | 6 +- 10 files changed, 246 insertions(+), 155 deletions(-) create mode 100644 marvin.robot.core/src/main/java/com/assetvisor/marvin/robot/domain/brain/Brain.java delete mode 100644 marvin.robot.core/src/main/java/com/assetvisor/marvin/robot/domain/brain/BrainResponder.java rename marvin.robot.core/src/main/java/com/assetvisor/marvin/robot/domain/brain/{ForInvokingBrain.java => ForInvokingIntelligence.java} (78%) delete mode 100644 marvin.robot.core/src/test/java/com/assetvisor/marvin/robot/domain/brain/BrainResponderTest.java create mode 100644 marvin.robot.core/src/test/java/com/assetvisor/marvin/robot/domain/brain/BrainTest.java diff --git a/marvin.brain.springai/src/main/java/com/assetvisor/marvin/brain/springai/adapters/BrainSpringAiAdapter.java b/marvin.brain.springai/src/main/java/com/assetvisor/marvin/brain/springai/adapters/BrainSpringAiAdapter.java index 0412453..77e6974 100644 --- a/marvin.brain.springai/src/main/java/com/assetvisor/marvin/brain/springai/adapters/BrainSpringAiAdapter.java +++ b/marvin.brain.springai/src/main/java/com/assetvisor/marvin/brain/springai/adapters/BrainSpringAiAdapter.java @@ -1,8 +1,8 @@ package com.assetvisor.marvin.brain.springai.adapters; import com.assetvisor.marvin.robot.domain.brain.AsleepException; -import com.assetvisor.marvin.robot.domain.brain.BrainResponder; -import com.assetvisor.marvin.robot.domain.brain.ForInvokingBrain; +import com.assetvisor.marvin.robot.domain.brain.Brain; +import com.assetvisor.marvin.robot.domain.brain.ForInvokingIntelligence; import com.assetvisor.marvin.toolkit.memory.ForRemembering; import com.assetvisor.marvin.robot.domain.environment.EnvironmentDescription; import com.assetvisor.marvin.robot.domain.tools.Tool; @@ -26,7 +26,7 @@ import org.springframework.stereotype.Component; @Component -public class BrainSpringAiAdapter implements ForInvokingBrain, ForRemembering { +public class BrainSpringAiAdapter implements ForInvokingIntelligence, ForRemembering { private final Log LOG = LogFactory.getLog(BrainSpringAiAdapter.class); @SuppressWarnings("FieldCanBeLocal") @@ -94,7 +94,7 @@ private Document map(EnvironmentDescription environmentDescription) { } @Override - public void invoke(String message, boolean reply, BrainResponder responder, String conversationId) { + public void invoke(String message, boolean reply, Brain brain, String conversationId) { if (chatClient == null) { throw new AsleepException("Brain is asleep, please wake it up first."); } @@ -110,7 +110,7 @@ public void invoke(String message, boolean reply, BrainResponder responder, Stri assert chatResponse != null; String responseString = chatResponse.getResult().getOutput().getContent(); if (reply) { - responder.respond(responseString, conversationId); + brain.respond(responseString, conversationId); } } diff --git a/marvin.robot.core/src/main/java/com/assetvisor/marvin/robot/application/services/InteractionService.java b/marvin.robot.core/src/main/java/com/assetvisor/marvin/robot/application/services/InteractionService.java index a5028db..2cf5f2b 100644 --- a/marvin.robot.core/src/main/java/com/assetvisor/marvin/robot/application/services/InteractionService.java +++ b/marvin.robot.core/src/main/java/com/assetvisor/marvin/robot/application/services/InteractionService.java @@ -1,17 +1,16 @@ package com.assetvisor.marvin.robot.application.services; -import com.assetvisor.marvin.robot.application.SomethingWasSaidUseCase; import com.assetvisor.marvin.robot.application.SomethingHappenedInTheEnvironmentUseCase; +import com.assetvisor.marvin.robot.application.SomethingWasSaidUseCase; import com.assetvisor.marvin.robot.application.SomethingWasTextedUseCase; -import com.assetvisor.marvin.robot.domain.brain.BrainResponder; -import com.assetvisor.marvin.robot.domain.brain.ForInvokingBrain; -import com.assetvisor.marvin.robot.domain.communication.ConversationMessage; +import com.assetvisor.marvin.robot.domain.brain.Brain; +import com.assetvisor.marvin.robot.domain.brain.ForInvokingIntelligence; import com.assetvisor.marvin.robot.domain.communication.ForCheckingIfAnybodyIsListening; import com.assetvisor.marvin.robot.domain.communication.ForConvertingSpeechToText; import com.assetvisor.marvin.robot.domain.communication.ForConvertingTextToSpeech; import com.assetvisor.marvin.robot.domain.communication.ForTexting; -import com.assetvisor.marvin.robot.domain.communication.SpeechMessage; import com.assetvisor.marvin.robot.domain.communication.SpeechBuffer; +import com.assetvisor.marvin.robot.domain.communication.SpeechMessage; import com.assetvisor.marvin.robot.domain.communication.TextMessage; import com.assetvisor.marvin.robot.domain.environment.Observation; import jakarta.annotation.Resource; @@ -28,7 +27,7 @@ public class InteractionService implements Log LOG = LogFactory.getLog(getClass()); @Resource - private ForInvokingBrain forInvokingBrain; + private ForInvokingIntelligence forInvokingIntelligence; @Resource private ForTexting forTexting; @Resource @@ -42,55 +41,27 @@ public class InteractionService implements @Override public void observe(Observation observation) { - LOG.info(observation); - forInvokingBrain.invoke( - observation.toString(), - true, - new BrainResponder( - forTexting, - forConvertingTextToSpeech, - forCheckingIfAnybodyIsListening, - speechBuffer - ), - ConversationMessage.DEFAULT_CONVERSATION_ID - ); + brain().observe(observation); } @Override public void read(TextMessage message, boolean echoToSender) { - LOG.info(message); - if(echoToSender) { - forTexting.text(message, true); - } - forInvokingBrain.invoke( - message.getContent(), - true, - new BrainResponder( - forTexting, - forConvertingTextToSpeech, - forCheckingIfAnybodyIsListening, - speechBuffer - ), - message.conversationId() - ); + brain().read(message, echoToSender); } @Override public void listenTo(SpeechMessage speech) { - String text = forConvertingSpeechToText.convert(speech.getAudio()); - LOG.info(text); - forTexting.text(new TextMessage(speech.getSender(), speech.conversationId(), text), true); - forInvokingBrain.invoke( - text, - true, - new BrainResponder( - forTexting, - forConvertingTextToSpeech, - forCheckingIfAnybodyIsListening, - speechBuffer - ), - speech.conversationId() - ); + brain().listenTo(speech); } + private Brain brain() { + return new Brain( + forTexting, + forConvertingTextToSpeech, + forConvertingSpeechToText, + forCheckingIfAnybodyIsListening, + speechBuffer, + forInvokingIntelligence + ); + } } diff --git a/marvin.robot.core/src/main/java/com/assetvisor/marvin/robot/application/services/LifecycleService.java b/marvin.robot.core/src/main/java/com/assetvisor/marvin/robot/application/services/LifecycleService.java index 213d39e..2524b5a 100644 --- a/marvin.robot.core/src/main/java/com/assetvisor/marvin/robot/application/services/LifecycleService.java +++ b/marvin.robot.core/src/main/java/com/assetvisor/marvin/robot/application/services/LifecycleService.java @@ -2,7 +2,7 @@ import com.assetvisor.marvin.robot.application.InitialiseUseCase; import com.assetvisor.marvin.robot.domain.brain.ForForgettingEverything; -import com.assetvisor.marvin.robot.domain.brain.ForInvokingBrain; +import com.assetvisor.marvin.robot.domain.brain.ForInvokingIntelligence; import com.assetvisor.marvin.robot.domain.brain.Teacher; import com.assetvisor.marvin.robot.domain.environment.ForGettingEnvironmentDescriptions; import com.assetvisor.marvin.robot.domain.environment.ForPersistingEnvironmentDescriptions; @@ -23,7 +23,7 @@ public class LifecycleService implements InitialiseUseCase { @Resource private ForForgettingEverything forForgettingEverything; @Resource - private ForInvokingBrain forInvokingBrain; + private ForInvokingIntelligence forInvokingIntelligence; @Resource private ForPersistingRobotDescription forPersistingRobotDescription; @Resource @@ -41,7 +41,7 @@ public void initialise() { forForgettingEverything.forgetEverything(); Teacher teacher = new Teacher( - forInvokingBrain, + forInvokingIntelligence, forPersistingEnvironmentDescriptions, forGettingEnvironmentDescriptions ); @@ -54,7 +54,7 @@ public void wakeUp() { forGettingEnvironmentTools, forGettingOwnTools ); - forInvokingBrain.wakeUp( + forInvokingIntelligence.wakeUp( forPersistingRobotDescription.read(), toolCollector.all() ); diff --git a/marvin.robot.core/src/main/java/com/assetvisor/marvin/robot/domain/brain/Brain.java b/marvin.robot.core/src/main/java/com/assetvisor/marvin/robot/domain/brain/Brain.java new file mode 100644 index 0000000..67b3265 --- /dev/null +++ b/marvin.robot.core/src/main/java/com/assetvisor/marvin/robot/domain/brain/Brain.java @@ -0,0 +1,97 @@ +package com.assetvisor.marvin.robot.domain.brain; + +import com.assetvisor.marvin.robot.domain.communication.ConversationMessage; +import com.assetvisor.marvin.robot.domain.communication.ForCheckingIfAnybodyIsListening; +import com.assetvisor.marvin.robot.domain.communication.ForConvertingSpeechToText; +import com.assetvisor.marvin.robot.domain.communication.ForConvertingTextToSpeech; +import com.assetvisor.marvin.robot.domain.communication.ForTexting; +import com.assetvisor.marvin.robot.domain.communication.SpeechMessage; +import com.assetvisor.marvin.robot.domain.communication.SpeechBuffer; +import com.assetvisor.marvin.robot.domain.communication.TextMessage; +import com.assetvisor.marvin.robot.domain.environment.Observation; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +public class Brain { + + private static final Log LOG = LogFactory.getLog(Brain.class); + + private final ForTexting forTexting; + private final ForConvertingTextToSpeech forConvertingTextToSpeech; + private final ForConvertingSpeechToText forConvertingSpeechToText; + private final ForCheckingIfAnybodyIsListening forCheckingIfAnybodyIsListening; + private final SpeechBuffer speechBuffer; + private final ForInvokingIntelligence forInvokingIntelligence; + + public Brain(ForTexting forTexting, + ForConvertingTextToSpeech forConvertingTextToSpeech, + ForConvertingSpeechToText forConvertingSpeechToText, + ForCheckingIfAnybodyIsListening forCheckingIfAnybodyIsListening, + SpeechBuffer speechBuffer, + ForInvokingIntelligence forInvokingIntelligence + ) { + this.forTexting = forTexting; + this.forConvertingTextToSpeech = forConvertingTextToSpeech; + this.forConvertingSpeechToText = forConvertingSpeechToText; + this.forCheckingIfAnybodyIsListening = forCheckingIfAnybodyIsListening; + this.speechBuffer = speechBuffer; + this.forInvokingIntelligence = forInvokingIntelligence; + } + + public void observe(Observation observation) { + LOG.info(observation); + forInvokingIntelligence.invoke( + observation.toString(), + true, + this, + ConversationMessage.DEFAULT_CONVERSATION_ID + ); + } + + public void read(TextMessage message, boolean echoToSender) { + LOG.info(message); + if(echoToSender) { + forTexting.text(message, true); + } + forInvokingIntelligence.invoke( + message.getContent(), + true, + this, + message.conversationId() + ); + } + + public void listenTo(SpeechMessage speech) { + String text = forConvertingSpeechToText.convert(speech.getAudio()); + LOG.info(text); + forTexting.text( + new TextMessage( + speech.getSender(), + speech.conversationId(), + text), + true + ); + forInvokingIntelligence.invoke( + text, + true, + this, + speech.conversationId() + ); + } + + public void respond(String message, String conversationId) { + message(message, conversationId); + if(forCheckingIfAnybodyIsListening.isAnybodyListening()) { + speak(message, conversationId); + } + } + + private void speak(String message, String conversationId) { + byte[] speech = forConvertingTextToSpeech.convert(message); + speechBuffer.add(new SpeechMessage("Marvin", conversationId, speech)); + } + + private void message(String message, String conversationId) { + forTexting.text(new TextMessage("Marvin", conversationId, message), false); + } +} diff --git a/marvin.robot.core/src/main/java/com/assetvisor/marvin/robot/domain/brain/BrainResponder.java b/marvin.robot.core/src/main/java/com/assetvisor/marvin/robot/domain/brain/BrainResponder.java deleted file mode 100644 index 8547ff4..0000000 --- a/marvin.robot.core/src/main/java/com/assetvisor/marvin/robot/domain/brain/BrainResponder.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.assetvisor.marvin.robot.domain.brain; - -import com.assetvisor.marvin.robot.domain.communication.ForCheckingIfAnybodyIsListening; -import com.assetvisor.marvin.robot.domain.communication.ForConvertingTextToSpeech; -import com.assetvisor.marvin.robot.domain.communication.ForTexting; -import com.assetvisor.marvin.robot.domain.communication.SpeechMessage; -import com.assetvisor.marvin.robot.domain.communication.SpeechBuffer; -import com.assetvisor.marvin.robot.domain.communication.TextMessage; - -public class BrainResponder { - private final ForTexting forMessaging; - private final ForConvertingTextToSpeech forConvertingTextToSpeech; - private final ForCheckingIfAnybodyIsListening forCheckingIfAnybodyIsListening; - private final SpeechBuffer speechBuffer; - - public BrainResponder(ForTexting forMessaging, ForConvertingTextToSpeech forConvertingTextToSpeech, - ForCheckingIfAnybodyIsListening forCheckingIfAnybodyIsListening, - SpeechBuffer speechBuffer) { - this.forMessaging = forMessaging; - this.forConvertingTextToSpeech = forConvertingTextToSpeech; - this.forCheckingIfAnybodyIsListening = forCheckingIfAnybodyIsListening; - this.speechBuffer = speechBuffer; - } - - public void respond(String message, String conversationId) { - message(message, conversationId); - if(forCheckingIfAnybodyIsListening.isAnybodyListening()) { - speak(message, conversationId); - } - } - - private void speak(String message, String conversationId) { - byte[] speech = forConvertingTextToSpeech.convert(message); - speechBuffer.add(new SpeechMessage("Marvin", conversationId, speech)); - } - - private void message(String message, String conversationId) { - forMessaging.text(new TextMessage("Marvin", conversationId, message), false); - } -} diff --git a/marvin.robot.core/src/main/java/com/assetvisor/marvin/robot/domain/brain/ForInvokingBrain.java b/marvin.robot.core/src/main/java/com/assetvisor/marvin/robot/domain/brain/ForInvokingIntelligence.java similarity index 78% rename from marvin.robot.core/src/main/java/com/assetvisor/marvin/robot/domain/brain/ForInvokingBrain.java rename to marvin.robot.core/src/main/java/com/assetvisor/marvin/robot/domain/brain/ForInvokingIntelligence.java index 55af08c..7963169 100644 --- a/marvin.robot.core/src/main/java/com/assetvisor/marvin/robot/domain/brain/ForInvokingBrain.java +++ b/marvin.robot.core/src/main/java/com/assetvisor/marvin/robot/domain/brain/ForInvokingIntelligence.java @@ -5,7 +5,7 @@ import com.assetvisor.marvin.robot.domain.jobdescription.RobotDescription; import java.util.List; -public interface ForInvokingBrain { +public interface ForInvokingIntelligence { void teach( List environmentDescriptions ); @@ -13,5 +13,5 @@ void wakeUp( RobotDescription robotDescription, List> environmentFunctions ); - void invoke(String message, boolean reply, BrainResponder responder, String conversationId); + void invoke(String message, boolean reply, Brain brain, String conversationId); } diff --git a/marvin.robot.core/src/main/java/com/assetvisor/marvin/robot/domain/brain/Teacher.java b/marvin.robot.core/src/main/java/com/assetvisor/marvin/robot/domain/brain/Teacher.java index 7ad3d9b..a8611d6 100644 --- a/marvin.robot.core/src/main/java/com/assetvisor/marvin/robot/domain/brain/Teacher.java +++ b/marvin.robot.core/src/main/java/com/assetvisor/marvin/robot/domain/brain/Teacher.java @@ -12,16 +12,16 @@ public class Teacher { private final Log LOG = LogFactory.getLog(getClass()); - private final ForInvokingBrain forInvokingBrain; + private final ForInvokingIntelligence forInvokingIntelligence; private final ForPersistingEnvironmentDescriptions forPersistingEnvironmentDescriptions; private final ForGettingEnvironmentDescriptions forGettingEnvironmentDescriptions; public Teacher( - ForInvokingBrain forInvokingBrain, + ForInvokingIntelligence forInvokingIntelligence, ForPersistingEnvironmentDescriptions forPersistingEnvironmentDescriptions, ForGettingEnvironmentDescriptions forGettingEnvironmentDescriptions ) { - this.forInvokingBrain = forInvokingBrain; + this.forInvokingIntelligence = forInvokingIntelligence; this.forPersistingEnvironmentDescriptions = forPersistingEnvironmentDescriptions; this.forGettingEnvironmentDescriptions = forGettingEnvironmentDescriptions; } @@ -30,7 +30,7 @@ public void teach() { LOG.info("Teaching brain about environment..."); List environmentDescriptions = new ArrayList<>(forPersistingEnvironmentDescriptions.load()); environmentDescriptions.addAll(forGettingEnvironmentDescriptions.getEnvironmentDescriptions()); - forInvokingBrain.teach( + forInvokingIntelligence.teach( environmentDescriptions ); LOG.info("Teaching done."); diff --git a/marvin.robot.core/src/test/java/com/assetvisor/marvin/robot/domain/brain/BrainResponderTest.java b/marvin.robot.core/src/test/java/com/assetvisor/marvin/robot/domain/brain/BrainResponderTest.java deleted file mode 100644 index 6674130..0000000 --- a/marvin.robot.core/src/test/java/com/assetvisor/marvin/robot/domain/brain/BrainResponderTest.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.assetvisor.marvin.robot.domain.brain; - -import static org.mockito.BDDMockito.given; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoMoreInteractions; - -import com.assetvisor.marvin.robot.domain.communication.SpeechMessage; -import com.assetvisor.marvin.robot.domain.communication.ForCheckingIfAnybodyIsListening; -import com.assetvisor.marvin.robot.domain.communication.ForConvertingTextToSpeech; -import com.assetvisor.marvin.robot.domain.communication.ForTexting; -import com.assetvisor.marvin.robot.domain.communication.SpeechBuffer; -import com.assetvisor.marvin.robot.domain.communication.TextMessage; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.Spy; -import org.mockito.junit.jupiter.MockitoExtension; - -@ExtendWith(MockitoExtension.class) -public class BrainResponderTest { - @Mock - private ForTexting forMessaging; - @Mock - private ForConvertingTextToSpeech forConvertingTextToSpeech; - @Mock - private ForCheckingIfAnybodyIsListening forCheckingIfAnybodyIsListening; - @Spy - private SpeechBuffer speechBuffer; - @InjectMocks - private BrainResponder brainResponder; - - @Test - public void shouldRespond() { - // Given - TextMessage message = new TextMessage("Marvin", "C1", "message"); - SpeechMessage speech = new SpeechMessage("Marvin", "C1", new byte[]{1, 2, 3}); - - given(forConvertingTextToSpeech.convert(message.getContent())).willReturn(speech.getAudio()); - given(forCheckingIfAnybodyIsListening.isAnybodyListening()).willReturn(true); - - // When - brainResponder.respond(message.getContent(), message.conversationId()); - // Then - verify(forConvertingTextToSpeech).convert(message.getContent()); - verify(speechBuffer).add(speech); - verify(forMessaging).text(message, false); - verifyNoMoreInteractions(forMessaging, forConvertingTextToSpeech, speechBuffer); - } -} \ No newline at end of file diff --git a/marvin.robot.core/src/test/java/com/assetvisor/marvin/robot/domain/brain/BrainTest.java b/marvin.robot.core/src/test/java/com/assetvisor/marvin/robot/domain/brain/BrainTest.java new file mode 100644 index 0000000..4ecda44 --- /dev/null +++ b/marvin.robot.core/src/test/java/com/assetvisor/marvin/robot/domain/brain/BrainTest.java @@ -0,0 +1,113 @@ +package com.assetvisor.marvin.robot.domain.brain; + +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoInteractions; +import static org.mockito.Mockito.verifyNoMoreInteractions; + +import com.assetvisor.marvin.robot.domain.communication.ConversationMessage; +import com.assetvisor.marvin.robot.domain.communication.ForConvertingSpeechToText; +import com.assetvisor.marvin.robot.domain.communication.SpeechMessage; +import com.assetvisor.marvin.robot.domain.communication.ForCheckingIfAnybodyIsListening; +import com.assetvisor.marvin.robot.domain.communication.ForConvertingTextToSpeech; +import com.assetvisor.marvin.robot.domain.communication.ForTexting; +import com.assetvisor.marvin.robot.domain.communication.SpeechBuffer; +import com.assetvisor.marvin.robot.domain.communication.TextMessage; +import com.assetvisor.marvin.robot.domain.environment.Observation; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Spy; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +public class BrainTest { + @Mock + private ForTexting forTexting; + @Mock + private ForConvertingTextToSpeech forConvertingTextToSpeech; + @Mock + private ForConvertingSpeechToText forConvertingSpeechToText; + @Mock + private ForCheckingIfAnybodyIsListening forCheckingIfAnybodyIsListening; + @Mock + private ForInvokingIntelligence forInvokingIntelligence; + @Spy + private SpeechBuffer speechBuffer; + @InjectMocks + private Brain brain; + + @Test + public void shouldRespond() { + // Given + TextMessage message = new TextMessage("Marvin", "C1", "message"); + SpeechMessage speech = new SpeechMessage("Marvin", "C1", new byte[]{1, 2, 3}); + + given(forConvertingTextToSpeech.convert(message.getContent())).willReturn(speech.getAudio()); + given(forCheckingIfAnybodyIsListening.isAnybodyListening()).willReturn(true); + + // When + brain.respond(message.getContent(), message.conversationId()); + // Then + verify(forConvertingTextToSpeech).convert(message.getContent()); + verify(speechBuffer).add(speech); + verify(forTexting).text(message, false); + verifyNoMoreInteractions(forTexting, forConvertingTextToSpeech, speechBuffer); + } + + @Test + public void shouldObserve() { + // Given + Observation observation = new Observation("Marvin", "I1", "2", "1", "observation"); + // When + brain.observe(observation); + // Then + verify(forInvokingIntelligence).invoke( + observation.toString(), + true, + brain, + ConversationMessage.DEFAULT_CONVERSATION_ID + ); + verifyNoMoreInteractions(forInvokingIntelligence); + verifyNoInteractions(forTexting, forConvertingTextToSpeech, speechBuffer); + } + + @Test + public void shouldRead() { + // Given + TextMessage textMessage = new TextMessage("Marvin", "C1", "message"); + // When + brain.read(textMessage, true); + // Then + verify(forInvokingIntelligence).invoke( + textMessage.getContent(), + true, + brain, + textMessage.conversationId() + ); + verify(forTexting).text(textMessage, true); + verifyNoMoreInteractions(forInvokingIntelligence, forTexting); + verifyNoInteractions(forConvertingTextToSpeech, speechBuffer); + } + + @Test + public void shouldListenTo() { + // Given + SpeechMessage speechMessage = new SpeechMessage("Marvin", "C1", new byte[]{1, 2, 3}); + given(forConvertingSpeechToText.convert(speechMessage.getAudio())).willReturn("text"); + // When + brain.listenTo(speechMessage); + // Then + verify(forConvertingSpeechToText).convert(speechMessage.getAudio()); + verify(forTexting).text(new TextMessage("Marvin", "C1", "text"), true); + verify(forInvokingIntelligence).invoke( + "text", + true, + brain, + speechMessage.conversationId() + ); + verifyNoMoreInteractions(forInvokingIntelligence, forTexting, forConvertingSpeechToText); + verifyNoInteractions(forConvertingTextToSpeech, speechBuffer); + } +} \ No newline at end of file diff --git a/marvin.robot.core/src/test/java/com/assetvisor/marvin/robot/domain/brain/TeacherTest.java b/marvin.robot.core/src/test/java/com/assetvisor/marvin/robot/domain/brain/TeacherTest.java index 02adda7..34f122d 100644 --- a/marvin.robot.core/src/test/java/com/assetvisor/marvin/robot/domain/brain/TeacherTest.java +++ b/marvin.robot.core/src/test/java/com/assetvisor/marvin/robot/domain/brain/TeacherTest.java @@ -19,7 +19,7 @@ @ExtendWith(MockitoExtension.class) public class TeacherTest { @Mock - private ForInvokingBrain forInvokingBrain; + private ForInvokingIntelligence forInvokingIntelligence; @Mock private ForPersistingEnvironmentDescriptions forPersistingEnvironmentDescriptions; @Mock @@ -43,10 +43,10 @@ public void shouldTeachBrain() { ArgumentCaptor> captor = ArgumentCaptor.forClass(List.class); teacher.teach(); // Then - verify(forInvokingBrain).teach(captor.capture()); + verify(forInvokingIntelligence).teach(captor.capture()); assertTrue(captor.getValue().containsAll(environmentDescriptionList)); verify(forPersistingEnvironmentDescriptions).load(); verify(forGettingEnvironmentDescriptions).getEnvironmentDescriptions(); - verifyNoMoreInteractions(forInvokingBrain, forPersistingEnvironmentDescriptions, forGettingEnvironmentDescriptions); + verifyNoMoreInteractions(forInvokingIntelligence, forPersistingEnvironmentDescriptions, forGettingEnvironmentDescriptions); } } \ No newline at end of file