test(query-understand): pin fallback for unparsable LLM output

Add failing tests for query understanding: RewriteQuery must remain the
original user query if LLM output is unparsable.
main
MidoriKurage 2026-08-07 14:50:27 +08:00 committed by lyingbug
parent 5494e669bc
commit d763816ebf
1 changed files with 64 additions and 0 deletions

View File

@ -85,3 +85,67 @@ func TestApplyIntentPromptOverride_GlobalOnly(t *testing.T) {
t.Errorf("override: got %q, want %q", cm.SystemPromptOverride, "hi there")
}
}
// TestParseOutput_UnparsableFallsBackToOriginalQuery pins the degradation
// contract for query understanding: when the LLM returns output that cannot be
// parsed as the structured {"rewrite_query","intent",...} JSON, RewriteQuery
// must remain the original user query (which OnEvent sets before calling
// parseOutput) instead of leaking the raw model text into the downstream
// retrieval query.
func TestParseOutput_UnparsableFallsBackToOriginalQuery(t *testing.T) {
p := &PluginQueryUnderstand{}
cm := &types.ChatManage{
PipelineState: types.PipelineState{
RewriteQuery: "original user query",
Intent: types.IntentKBSearch,
},
}
p.parseOutput(cm, "The answer is: check the admin console")
if cm.RewriteQuery != "original user query" {
t.Fatalf("RewriteQuery = %q, want original user query", cm.RewriteQuery)
}
if cm.Intent != types.IntentKBSearch {
t.Errorf("Intent = %q, want kb_search", cm.Intent)
}
}
// TestParseOutput_UnparsableBlankDoesNotRewrite verifies that empty LLM output
// also leaves the original query untouched.
func TestParseOutput_UnparsableBlankDoesNotRewrite(t *testing.T) {
p := &PluginQueryUnderstand{}
cm := &types.ChatManage{
PipelineState: types.PipelineState{
RewriteQuery: "original user query",
Intent: types.IntentKBSearch,
},
}
p.parseOutput(cm, " \n\t ")
if cm.RewriteQuery != "original user query" {
t.Fatalf("RewriteQuery = %q, want original user query", cm.RewriteQuery)
}
}
// TestParseOutput_ValidJSONStillAppliesRewrite guards the happy path: a
// well-formed structured output still overrides RewriteQuery and Intent.
func TestParseOutput_ValidJSONStillAppliesRewrite(t *testing.T) {
p := &PluginQueryUnderstand{}
cm := &types.ChatManage{
PipelineState: types.PipelineState{
RewriteQuery: "original user query",
Intent: types.IntentKBSearch,
},
}
p.parseOutput(cm, `{"rewrite_query":"rewritten query","intent":"summarize"}`)
if cm.RewriteQuery != "rewritten query" {
t.Fatalf("RewriteQuery = %q, want rewritten query", cm.RewriteQuery)
}
if cm.Intent != types.IntentSummarize {
t.Errorf("Intent = %q, want summarize", cm.Intent)
}
}