Skip to content

Commit bff79e3

Browse files
committed
test(hook): improve GenericRAGHook coverage for long_term_memory skip logic
Use scoreThreshold=0.0 to guarantee RAG retrieval triggers in the hook-interference test, ensuring the name-based skip branch is actually executed. Add edge-case test for when all USER messages are hook-injected (no genuine user query available).
1 parent 5353a18 commit bff79e3

1 file changed

Lines changed: 43 additions & 5 deletions

File tree

  • agentscope-extensions/agentscope-extensions-rag-simple/src/test/java/io/agentscope/core/rag/hook

agentscope-extensions/agentscope-extensions-rag-simple/src/test/java/io/agentscope/core/rag/hook/GenericRAGHookTest.java

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -327,9 +327,22 @@ void testFormatKnowledgeContent() {
327327
@Test
328328
@DisplayName("Should skip long_term_memory injected messages when extracting query")
329329
void testSkipLongTermMemoryInjectedMessage() {
330+
// Use scoreThreshold=0.0 to guarantee retrieval hits regardless of vector similarity
331+
TestMockEmbeddingModel embeddingModel = new TestMockEmbeddingModel(DIMENSIONS);
332+
InMemoryStore vectorStore = InMemoryStore.builder().dimensions(DIMENSIONS).build();
333+
Knowledge zeroThresholdKnowledge =
334+
SimpleKnowledge.builder()
335+
.embeddingModel(embeddingModel)
336+
.embeddingStore(vectorStore)
337+
.build();
338+
RetrieveConfig zeroThresholdConfig =
339+
RetrieveConfig.builder().limit(5).scoreThreshold(0.0).build();
340+
GenericRAGHook hookWithZeroThreshold =
341+
new GenericRAGHook(zeroThresholdKnowledge, zeroThresholdConfig);
342+
330343
// Simulate StaticLongTermMemoryHook having already injected a "long_term_memory" message
331344
Document doc = createDocument("doc1", "Refund policy: 30 days return");
332-
knowledge.addDocuments(List.of(doc)).block();
345+
zeroThresholdKnowledge.addDocuments(List.of(doc)).block();
333346

334347
List<Msg> inputMessages = new ArrayList<>();
335348
inputMessages.add(
@@ -349,20 +362,45 @@ void testSkipLongTermMemoryInjectedMessage() {
349362

350363
PreCallEvent event = new PreCallEvent(mockAgent, inputMessages);
351364

352-
StepVerifier.create(hook.onEvent(event))
365+
StepVerifier.create(hookWithZeroThreshold.onEvent(event))
353366
.assertNext(
354367
result -> {
355368
List<Msg> messages = result.getInputMessages();
356-
// Should have original user msg + long_term_memory + RAG knowledge
369+
// Should have: original user msg + long_term_memory + RAG knowledge
357370
assertEquals(3, messages.size());
358-
// The RAG hook should use the original user query, not the memory
359-
// message
371+
// The RAG hook must use the original user query, not the memory message
360372
assertEquals("retrieved_knowledge", messages.get(2).getName());
361373
assertTrue(messages.get(2).getTextContent().contains("knowledge base"));
362374
})
363375
.verifyComplete();
364376
}
365377

378+
@Test
379+
@DisplayName("Should return no results when all user messages are hook-injected")
380+
void testNoRealUserMessageWhenAllAreInjected() {
381+
// Only hook-injected USER messages exist — no genuine user query
382+
List<Msg> inputMessages = new ArrayList<>();
383+
inputMessages.add(
384+
Msg.builder()
385+
.role(MsgRole.USER)
386+
.name("long_term_memory")
387+
.content(
388+
TextBlock.builder()
389+
.text("<long_term_memory>some memory</long_term_memory>")
390+
.build())
391+
.build());
392+
393+
PreCallEvent event = new PreCallEvent(mockAgent, inputMessages);
394+
395+
StepVerifier.create(hook.onEvent(event))
396+
.assertNext(
397+
result -> {
398+
// No real user query found: messages should remain unchanged
399+
assertEquals(1, result.getInputMessages().size());
400+
})
401+
.verifyComplete();
402+
}
403+
366404
/**
367405
* Creates a test document.
368406
*/

0 commit comments

Comments
 (0)