test: address PR review follow-ups for OAuth and logger cleanup
Restore logger config via t.Cleanup after t.Setenv so Yuque token tests do not leak global state, add unit tests for MCP OAuth non-interactive context helpers and IM auth notice formatting, and fix EmbedChatCore indentation.
This commit is contained in:
@@ -44,4 +44,8 @@ func TestWithIMIdentity(t *testing.T) {
|
||||
if principal.Type != types.PrincipalIMUser || principal.ID != "42:channel-1:feishu:open-id-1" {
|
||||
t.Fatalf("Principal = %#v, want im_user for the external IM user", principal)
|
||||
}
|
||||
|
||||
if !types.IsMCPOAuthNonInteractive(ctx) {
|
||||
t.Fatal("IM context should mark MCP OAuth as non-interactive")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
package im
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFormatIMMCPAuthNotice(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
input []string
|
||||
want string
|
||||
contains []string
|
||||
}{
|
||||
{
|
||||
name: "empty input",
|
||||
input: nil,
|
||||
want: "",
|
||||
},
|
||||
{
|
||||
name: "all blank names",
|
||||
input: []string{"", " ", "\t"},
|
||||
want: "",
|
||||
},
|
||||
{
|
||||
name: "single service",
|
||||
input: []string{"GitHub MCP"},
|
||||
contains: []string{"GitHub MCP", "OAuth 授权"},
|
||||
},
|
||||
{
|
||||
name: "dedupe and trim",
|
||||
input: []string{" A ", "A", " B", " ", "B"},
|
||||
contains: []string{"A、B"},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := formatIMMCPAuthNotice(tt.input)
|
||||
if tt.contains == nil {
|
||||
if got != tt.want {
|
||||
t.Fatalf("formatIMMCPAuthNotice() = %q, want %q", got, tt.want)
|
||||
}
|
||||
return
|
||||
}
|
||||
for _, sub := range tt.contains {
|
||||
if !strings.Contains(got, sub) {
|
||||
t.Fatalf("formatIMMCPAuthNotice() = %q, want substring %q", got, sub)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAppendIMAuthNotice(t *testing.T) {
|
||||
notice := "⚠️ 需要授权"
|
||||
tests := []struct {
|
||||
name string
|
||||
body string
|
||||
notice string
|
||||
want string
|
||||
}{
|
||||
{"empty notice", "answer", "", "answer"},
|
||||
{"empty body", "", notice, notice},
|
||||
{"whitespace body", " \n ", notice, notice},
|
||||
{"append with blank line", "answer", notice, "answer\n\n" + notice},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := appendIMAuthNotice(tt.body, tt.notice)
|
||||
if got != tt.want {
|
||||
t.Fatalf("appendIMAuthNotice(%q, %q) = %q, want %q", tt.body, tt.notice, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user