Skip to content

[Repo Assist] test(server): add direct unit tests for SessionIDFromContext and NewSession#2893

Merged
lpcox merged 1 commit intomainfrom
repo-assist/test-session-id-from-context-15c69a9d51530278
Mar 31, 2026
Merged

[Repo Assist] test(server): add direct unit tests for SessionIDFromContext and NewSession#2893
lpcox merged 1 commit intomainfrom
repo-assist/test-session-id-from-context-15c69a9d51530278

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

🤖 This PR was created by Repo Assist, an automated AI assistant.

Summary

Adds a dedicated test file internal/server/session_test.go with direct unit tests for two functions that were previously only exercised indirectly:

  • SessionIDFromContext — the exported canonical API for reading session IDs from context (per code comments, "the canonical place in the server package that reads SessionIDContextKey directly")
  • NewSession — the constructor for session objects

Why This Was Missing

SessionIDFromContext was tested only through the private UnifiedServer.getSessionID() wrapper in unified_test.go. While effective, this means the exported function's contract isn't documented directly in the test suite and external callers can't see what guarantees are made.

What the Tests Cover

TestSessionIDFromContext

Scenario Expected
No key in context "default"
Empty string value "default"
Non-string value (e.g., int) "default"
nil value "default"
Valid session ID returned unchanged
API-key-style session ID returned unchanged
Nested contexts (innermost wins) inner value returned
Inner empty string with valid outer "default" (empty not propagated to outer)
mcp.SessionIDContextKey vs server.SessionIDContextKey same slot verified

The last test case documents an important invariant: server.SessionIDContextKey == mcp.SessionIDContextKey, ensuring SDK middleware and server code share the same context slot.

TestNewSession, TestNewSession_EmptyToken, TestNewSession_GuardInitNotShared

  • All fields initialised correctly (SessionID, Token, StartTime, GuardInit)
  • StartTime is set (non-zero, within expected window)
  • GuardInit is non-nil and empty for new sessions
  • Each session gets its own independent GuardInit map (no aliasing)

Test Status

⚠️ Infrastructure limitation: This environment has Go 1.24.13 installed but go.mod requires Go ≥ 1.25.0, and proxy.golang.org is blocked by the network firewall. The tests cannot be built or run locally.

The test file has been reviewed manually for correctness:

  • All imports reference packages present in the module
  • Test cases match the actual implementation in session.go
  • All testify assertions use the correct call signatures
  • The package server declaration (not server_test) is correct since the tests need access to GuardSessionState which is unexported

CI will validate the build and tests on merge.

Generated by Repo Assist ·

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/repo-assist.md@851905c06e905bf362a9f6cc54f912e3df747d55

…ession

SessionIDFromContext is the canonical exported API for reading session IDs
from context, but was previously only exercised indirectly through the
private UnifiedServer.getSessionID() wrapper.

This commit adds a dedicated test file that:
- Tests all SessionIDFromContext edge cases directly (absent key, empty
  string, wrong type, nil, valid ID, nested context shadowing)
- Verifies that server.SessionIDContextKey and mcp.SessionIDContextKey
  are the same context slot (shared between SDK middleware and server code)
- Tests NewSession field initialisation (SessionID, Token, StartTime, GuardInit)
- Verifies GuardInit maps are not shared between sessions

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@lpcox lpcox marked this pull request as ready for review March 31, 2026 18:01
Copilot AI review requested due to automatic review settings March 31, 2026 18:01
@lpcox lpcox merged commit a697cd4 into main Mar 31, 2026
4 checks passed
@lpcox lpcox deleted the repo-assist/test-session-id-from-context-15c69a9d51530278 branch March 31, 2026 18:01
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds direct unit tests in the internal/server package for the exported session helpers, making their contracts explicit without relying on indirect coverage via UnifiedServer tests.

Changes:

  • Add table-driven unit tests for SessionIDFromContext defaulting behavior and context key interoperability (server.SessionIDContextKey re-exported from mcp).
  • Add unit tests for NewSession field initialization, StartTime behavior, and per-session GuardInit map independence.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +59 to +61
return context.WithValue(context.Background(), SessionIDContextKey, "ghp_abcdefghijklmnopqrstuvwxyz0123456789")
},
wantID: "ghp_abcdefghijklmnopqrstuvwxyz0123456789",
Copy link

Copilot AI Mar 31, 2026

Choose a reason for hiding this comment

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

The test uses a GitHub PAT-looking value ("ghp_" + 36+ alphanumerics). This matches common secret-detection patterns (and even this repo’s own sanitize.SecretPatterns regex), which can trigger secret scanning/push protection noise. Consider switching to a clearly fake, non-matching placeholder (e.g., shorter than typical PAT length or with non-alphanumerics) while still exercising the “looks like an API key” behavior.

Suggested change
return context.WithValue(context.Background(), SessionIDContextKey, "ghp_abcdefghijklmnopqrstuvwxyz0123456789")
},
wantID: "ghp_abcdefghijklmnopqrstuvwxyz0123456789",
return context.WithValue(context.Background(), SessionIDContextKey, "ghp_demo-placeholder-key-1234")
},
wantID: "ghp_demo-placeholder-key-1234",

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants