Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 107 additions & 8 deletions internal/auth/header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
)

func TestTruncateSecret(t *testing.T) {
assert := assert.New(t)

Comment on lines 12 to +14
Copy link

Copilot AI Feb 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The bound asserter is created with the parent test's t, but assertions are executed inside t.Run subtests. This can misattribute failures to the parent test and breaks subtest-scoped reporting. Create the asserter inside each t.Run (using the subtest t) or switch back to the unbound assert.Equal(t, ...) form for table-driven subtests.

This issue also appears in the following locations of the same file:

  • line 75
  • line 181
  • line 248
  • line 296
  • line 369

Copilot uses AI. Check for mistakes.
tests := []struct {
name string
input string
Expand Down Expand Up @@ -65,12 +67,15 @@ func TestTruncateSecret(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := sanitize.TruncateSecret(tt.input)
assert.Equal(t, tt.want, got)
assert.Equal(tt.want, got)
})
}
}

func TestParseAuthHeader(t *testing.T) {
assert := assert.New(t)
require := require.New(t)

tests := []struct {
name string
authHeader string
Expand Down Expand Up @@ -162,18 +167,20 @@ func TestParseAuthHeader(t *testing.T) {
gotAPIKey, gotAgentID, gotErr := ParseAuthHeader(tt.authHeader)

if tt.wantErr != nil {
require.ErrorIs(t, gotErr, tt.wantErr)
require.ErrorIs(gotErr, tt.wantErr)
} else {
require.NoError(t, gotErr)
require.NoError(gotErr)
}

assert.Equal(t, tt.wantAPIKey, gotAPIKey)
assert.Equal(t, tt.wantAgentID, gotAgentID)
assert.Equal(tt.wantAPIKey, gotAPIKey)
assert.Equal(tt.wantAgentID, gotAgentID)
})
}
}

func TestValidateAPIKey(t *testing.T) {
assert := assert.New(t)

tests := []struct {
name string
provided string
Expand Down Expand Up @@ -233,12 +240,14 @@ func TestValidateAPIKey(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := ValidateAPIKey(tt.provided, tt.expected)
assert.Equal(t, tt.want, got)
assert.Equal(tt.want, got)
})
}
}

func TestExtractAgentID(t *testing.T) {
assert := assert.New(t)

tests := []struct {
name string
authHeader string
Expand Down Expand Up @@ -279,12 +288,14 @@ func TestExtractAgentID(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := ExtractAgentID(tt.authHeader)
assert.Equal(t, tt.want, got)
assert.Equal(tt.want, got)
})
}
}

func TestExtractSessionID(t *testing.T) {
assert := assert.New(t)

tests := []struct {
name string
authHeader string
Expand Down Expand Up @@ -335,12 +346,100 @@ func TestExtractSessionID(t *testing.T) {
authHeader: " ",
want: " ",
},
{
name: "Agent format with multiple spaces (trimmed)",
Copy link

Copilot AI Feb 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test case name says "(trimmed)", but ExtractSessionID does not trim whitespace for the Agent format—it only removes the prefix. Either update the test name to match the actual behavior or (if trimming is desired) adjust the production code and expected value accordingly.

Suggested change
name: "Agent format with multiple spaces (trimmed)",
name: "Agent format with multiple spaces (prefix only removed)",

Copilot uses AI. Check for mistakes.
authHeader: "Agent agent-123 ",
want: " agent-123 ",
},
{
name: "Bearer with tab character",
authHeader: "Bearer\tmy-token",
want: "Bearer\tmy-token",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := ExtractSessionID(tt.authHeader)
assert.Equal(t, tt.want, got)
assert.Equal(tt.want, got)
})
}
}

func TestTruncateSessionID(t *testing.T) {
assert := assert.New(t)

tests := []struct {
name string
sessionID string
want string
}{
{
name: "Empty session ID returns (none)",
sessionID: "",
want: "(none)",
},
{
name: "Single character",
sessionID: "a",
want: "a",
},
{
name: "Short session ID (5 chars)",
sessionID: "abc12",
want: "abc12",
},
{
name: "Exactly 8 characters - not truncated",
sessionID: "abcd1234",
want: "abcd1234",
},
{
name: "Exactly 9 characters - truncated",
sessionID: "abcd12345",
want: "abcd1234...",
},
{
name: "Long session ID (>8 chars)",
sessionID: "my-session-id-12345",
want: "my-sessi...",
},
{
name: "Very long session ID",
sessionID: "my-super-long-session-id-with-many-characters-12345678901234567890",
want: "my-super...",
},
{
name: "Session ID with special characters",
sessionID: "key!@#$%^&*()",
want: "key!@#$%...",
},
{
name: "Session ID with unicode",
sessionID: "session-émojis-🔑",
want: "session-...",
},
{
name: "UUID format",
sessionID: "550e8400-e29b-41d4-a716-446655440000",
want: "550e8400...",
},
{
name: "Whitespace only (under 8 chars)",
sessionID: " ",
want: " ",
},
{
name: "Whitespace only (over 8 chars)",
sessionID: " ",
want: " ...",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := TruncateSessionID(tt.sessionID)
assert.Equal(tt.want, got)
})
}
}
Loading