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:
@@ -47,13 +47,13 @@
|
||||
class="msg-item-wrapper"
|
||||
>
|
||||
<div v-if="session.role === 'user'">
|
||||
<EmbedUserMessage
|
||||
:content="String(session.content || '')"
|
||||
:mentioned_items="asUnknownArray(session.mentioned_items)"
|
||||
:images="asEmbedImages(session.images)"
|
||||
:attachments="asEmbedAttachments(session.attachments)"
|
||||
:embeddedMode="true"
|
||||
:embed-channel-id="channelId"
|
||||
<EmbedUserMessage
|
||||
:content="String(session.content || '')"
|
||||
:mentioned_items="asUnknownArray(session.mentioned_items)"
|
||||
:images="asEmbedImages(session.images)"
|
||||
:attachments="asEmbedAttachments(session.attachments)"
|
||||
:embeddedMode="true"
|
||||
:embed-channel-id="channelId"
|
||||
:embed-token="token"
|
||||
/>
|
||||
</div>
|
||||
|
||||
@@ -118,7 +118,7 @@ func TestClient_Ping_403WrapsInvalidCredentials(t *testing.T) {
|
||||
func TestClient_TokenNeverLoggedInFull(t *testing.T) {
|
||||
t.Setenv("LOG_FORMAT", "")
|
||||
logger.ConfigureFromEnv()
|
||||
defer logger.ConfigureFromEnv()
|
||||
t.Cleanup(func() { logger.ConfigureFromEnv() })
|
||||
|
||||
// Redirect the project's internal logger to an in-memory buffer so we can
|
||||
// assert the raw token never appears in log output. Using stdlib `log`
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -98,6 +99,24 @@ func TestLanguageLocaleName(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestMCPOAuthNonInteractive(t *testing.T) {
|
||||
if IsMCPOAuthNonInteractive(nil) {
|
||||
t.Fatal("nil context should not be non-interactive")
|
||||
}
|
||||
if IsMCPOAuthNonInteractive(context.Background()) {
|
||||
t.Fatal("background context should not be non-interactive")
|
||||
}
|
||||
|
||||
ctx := WithMCPOAuthNonInteractive(context.Background())
|
||||
if !IsMCPOAuthNonInteractive(ctx) {
|
||||
t.Fatal("marked context should be non-interactive")
|
||||
}
|
||||
child := context.WithValue(ctx, LanguageContextKey, "en-US")
|
||||
if !IsMCPOAuthNonInteractive(child) {
|
||||
t.Fatal("child context should inherit non-interactive flag")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLanguageFromContext(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
|
||||
Reference in New Issue
Block a user