Skip to content

SANDBOX-1809: test- add handler unit tests for sandbox agent - #9

Merged
fbm3307 merged 3 commits into
masterfrom
SANDBOX-1809-sandbox-agent-unit-tests
Jun 22, 2026
Merged

SANDBOX-1809: test- add handler unit tests for sandbox agent#9
fbm3307 merged 3 commits into
masterfrom
SANDBOX-1809-sandbox-agent-unit-tests

Conversation

@fbm3307

@fbm3307 fbm3307 commented Jun 17, 2026

Copy link
Copy Markdown
Collaborator
  • Cover auth enforcement: missing header, wrong scheme, wrong token
  • Cover assign state machine: unassigned→assigned, 409 on repeat
  • Cover health endpoint: 200 alive, 503 dead
  • Integration test for warm-pool flow (assign then exec)

Assisted by: Cursor

Summary by CodeRabbit

  • Tests
    • Added unit tests for the sandbox HTTP handler, covering /health, /exec authorization and input validation, /assign token assignment with conflict/validation behavior, and an end-to-end assign-then-exec flow that verifies status codes and key response fields (stdout/stderr/exitCode/durationMs).

- Cover auth enforcement: missing header, wrong scheme, wrong token
- Cover assign state machine: unassigned→assigned, 409 on repeat
- Cover health endpoint: 200 alive, 503 dead
- Integration test for warm-pool flow (assign then exec)

SANDBOX-1809

Co-authored-by: Cursor <cursoragent@cursor.com>
Signed-off-by: Feny Mehta <fbm3307@gmail.com>
@fbm3307
fbm3307 requested a review from rajivnathan June 17, 2026 08:43
@fbm3307

fbm3307 commented Jun 17, 2026

Copy link
Copy Markdown
Collaborator Author

@coderabbitai help

@fbm3307

fbm3307 commented Jun 17, 2026

Copy link
Copy Markdown
Collaborator Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jun 17, 2026

Copy link
Copy Markdown

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

CodeRabbit commands

These commands are invoked using PR/Issue comments.

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai rate limit to show your current review rate limit status.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai evaluate custom pre-merge check --instructions <custom-checks-instructions> --name <custom-checks-title> [--mode <error|warning>] to validate the custom pre-merge checks instructions. Defaults to error when --mode is omitted.
  • @coderabbitai ignore pre-merge checks to override pre-merge checks and get an approval on PR.
  • @coderabbitai run pre-merge checks to run pre-merge checks on the pull request.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai emit path instructions to emit generated path-specific review instructions for this repository.
  • @coderabbitai resolve merge conflict to automatically resolve merge conflicts.
  • @coderabbitai autofix to automatically fix issues identified in unresolved review comments.
  • @coderabbitai resolve to resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai generate configuration to create a PR that adds the current resolved configuration as .coderabbit.yaml (or show it if already present).
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore or @coderabbit ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Status, support, documentation and community

  • Visit our status page to check the current availability of CodeRabbit.
  • Create a ticket on our support page for assistance with any issues or questions.
  • Visit our documentation site for detailed information on how to use CodeRabbit.
  • Join our Discord community to connect with other users and get help from the community.
  • Follow us on X/Twitter for updates and announcements.

@coderabbitai

coderabbitai Bot commented Jun 17, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai

coderabbitai Bot commented Jun 17, 2026

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository YAML (base), Organization UI (inherited)

Review profile: CHILL

Plan: Enterprise

Run ID: f2f795a6-1f2b-4252-890f-7618581c656b

📥 Commits

Reviewing files that changed from the base of the PR and between 9e8c165 and 6e955a2.

📒 Files selected for processing (1)
  • pkg/sandbox/handler_test.go
🔗 Linked repositories identified

CodeRabbit considers these linked repositories for cross-repo context during reviews:

  • codeready-toolchain/mcp-common (manual)
  • codeready-toolchain/mcp-server-devsandbox (manual)
  • codeready-toolchain/api (manual)
  • codeready-toolchain/toolchain-common (manual)
  • codeready-toolchain/host-operator (manual)
  • codeready-toolchain/toolchain-e2e (manual)
🚧 Files skipped from review as they are similar to previous changes (1)
  • pkg/sandbox/handler_test.go

Walkthrough

Adds pkg/sandbox/handler_test.go with 365 lines of new tests validating the sandbox HTTP Handler endpoints. Four test functions cover /health liveness, /exec authorization and command execution, /assign token management, and an integration test confirming the assign-then-exec flow.

Changes

Sandbox Handler HTTP Endpoint Tests

Layer / File(s) Summary
Test infrastructure and health endpoint
pkg/sandbox/handler_test.go
Test imports (httptest, JSON, testify), testContext and newTestHandler helpers to construct a Handler with test session and initialized agent state, plus TestHandleHealth subtests verifying /health returns 200 with {status:"ok"} when alive and 503 with {status:"unhealthy"} when closed.
Exec endpoint authorization and validation
pkg/sandbox/handler_test.go
TestHandleExec subtests cover /exec rejection paths (missing/invalid Authorization, wrong bearer token, malformed/empty JSON body, agent-not-assigned state) and success cases asserting ExecResponse fields (stdout, stderr, exitCode, non-negative durationMs), including a non-zero exit code case.
Assign endpoint token assignment and conflicts
pkg/sandbox/handler_test.go
TestHandleAssign subtests verify successful token assignment for unassigned handlers, 409 conflict on second assign while preserving the original token, and 400 validation errors for invalid or empty request bodies.
Integration test: assign then exec flow
pkg/sandbox/handler_test.go
TestAssignThenExec validates that exec succeeds after successful assign using the assigned token, and exec returns 401 when a different token is used after assignment.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

Suggested reviewers

  • alexeykazakov
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly references the main change: adding unit tests for the sandbox agent handler component, with all tests in handler_test.go.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch SANDBOX-1809-sandbox-agent-unit-tests

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot added the test label Jun 17, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@pkg/sandbox/handler_test.go`:
- Around line 26-27: Replace all calls to httptest.NewRequest with
httptest.NewRequestWithContext throughout the test file to satisfy the
golangci-lint noctx rule. For each instance of httptest.NewRequest in the file
(including at lines 26, 43, 62, 80, 97, 112, 129, 147, 165, 185, 203, 223, 239,
256, 262, 276, 293, 312, 318, 337, and 343), change the function call to
httptest.NewRequestWithContext and add a context.Background() as the first
parameter before the existing parameters (http.MethodGet, URL path, and body).
This ensures all request constructions pass the required context parameter and
comply with the enforced linting rules.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository YAML (base), Organization UI (inherited)

Review profile: CHILL

Plan: Enterprise

Run ID: 2ae92b8f-565c-440b-bde4-63c6ff5bcd7f

📥 Commits

Reviewing files that changed from the base of the PR and between b026890 and 5f32793.

📒 Files selected for processing (1)
  • pkg/sandbox/handler_test.go
🔗 Linked repositories identified

CodeRabbit considers these linked repositories for cross-repo context during reviews:

  • codeready-toolchain/mcp-common (manual)
  • codeready-toolchain/mcp-server-devsandbox (manual)
  • codeready-toolchain/api (manual)
  • codeready-toolchain/toolchain-common (manual)
  • codeready-toolchain/host-operator (manual)
  • codeready-toolchain/toolchain-e2e (manual)
📜 Review details
🧰 Additional context used
📓 Path-based instructions (1)
**

⚙️ CodeRabbit configuration file

-Focus on major issues impacting performance, readability, maintainability and security. Avoid nitpicks and avoid verbosity.

Files:

  • pkg/sandbox/handler_test.go
🧬 Code graph analysis (1)
pkg/sandbox/handler_test.go (3)
pkg/sandbox/handler.go (10)
  • NewAgentState (25-30)
  • IsAssigned (33-37)
  • GetToken (40-44)
  • Error (63-63)
  • Handler (66-69)
  • NewHandler (72-74)
  • ErrorResponse (77-79)
  • HandleHealth (89-98)
  • HandleExec (102-147)
  • HandleAssign (151-168)
pkg/agent/types.go (3)
  • ExecRequest (5-8)
  • ExecResponse (11-16)
  • AssignRequest (19-21)
pkg/sandbox/bash.go (1)
  • Close (120-128)
🪛 golangci-lint (2.12.2)
pkg/sandbox/handler_test.go

[error] 26-26: net/http/httptest.NewRequest must not be called. use net/http/httptest.NewRequestWithContext

(noctx)


[error] 43-43: net/http/httptest.NewRequest must not be called. use net/http/httptest.NewRequestWithContext

(noctx)


[error] 62-62: net/http/httptest.NewRequest must not be called. use net/http/httptest.NewRequestWithContext

(noctx)

Comment thread pkg/sandbox/handler_test.go Outdated
Address CodeRabbit review comment — replace httptest.NewRequest with
httptest.NewRequestWithContext(context.Background(), ...) across all
test request constructions, consistent with the noctx linter enabled
in other codeready-toolchain repos.

Co-authored-by: Cursor <cursoragent@cursor.com>
Comment thread pkg/sandbox/handler_test.go Outdated
Comment thread pkg/sandbox/handler_test.go
Comment thread pkg/sandbox/handler_test.go
- Add testContext(t) helper using context.WithTimeout(t.Context(), 5s)
  instead of context.Background() to prevent tests from hanging
  indefinitely (rajivnathan review)
- Add error body assertion to "rejects malformed authorization header"
  test for consistency with other auth failure subtests (rajivnathan review)
- Rename "executes command with correct token" to "executes command and
  returns stdout" for clarity

Co-authored-by: Cursor <cursoragent@cursor.com>
@fbm3307
fbm3307 merged commit 9861a46 into master Jun 22, 2026
3 of 4 checks passed
@fbm3307
fbm3307 deleted the SANDBOX-1809-sandbox-agent-unit-tests branch June 22, 2026 09:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants