test: add unit tests for terminal ExecCommand detach behavior#181
Open
mason5052 wants to merge 2 commits intovxcontrol:masterfrom
Open
test: add unit tests for terminal ExecCommand detach behavior#181mason5052 wants to merge 2 commits intovxcontrol:masterfrom
mason5052 wants to merge 2 commits intovxcontrol:masterfrom
Conversation
Add test infrastructure (mock DockerClient + mock TermLogProvider) and 4 test cases covering terminal command execution paths, particularly the detached execution behavior related to context isolation (vxcontrol#176). TestExecCommandDetachReturnsQuickly: - Verifies detach=true returns within ~500ms even when command is slow TestExecCommandDetachQuickCompletion: - Verifies detach=true returns actual output when command finishes fast TestExecCommandNonDetachWaitsForCompletion: - Verifies detach=false waits for and returns full command output TestExecCommandContainerNotRunning: - Verifies proper error when container is not running Mock infrastructure implements the full docker.DockerClient interface with configurable responses and timing delays, reusable for future terminal.go test expansion. Signed-off-by: mason5052 <ehehwnwjs5052@gmail.com>
There was a problem hiding this comment.
Pull request overview
Adds unit test coverage for the terminal.ExecCommand detach and non-detach execution paths, using lightweight Docker/terminal log mocks to simulate exec output and timing behavior.
Changes:
- Introduces
mockDockerClientimplementingdocker.DockerClientwith configurable attach delays and streamed output vianet.Pipe(). - Adds
mockTermLogProviderimplementingTermLogProviderfor no-op logging in tests. - Adds four
ExecCommandunit tests covering detach quick-return, detach quick-completion, non-detach completion, and “container not running” error behavior.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
backend/pkg/tools/terminal_test.go
Outdated
|
|
||
| assert.NoError(t, err) | ||
| assert.Contains(t, output, "Command started in background") | ||
| assert.Less(t, elapsed, 2*time.Second, "detach should return within ~500ms, not wait for command") |
There was a problem hiding this comment.
TestExecCommandDetachReturnsQuickly claims detach should return within defaultQuickCheckTimeout (500ms), but the assertion only checks < 2s, which can let regressions through (e.g., returning after 1–1.5s would still pass). Consider asserting against defaultQuickCheckTimeout (plus a small scheduling margin) so the test actually enforces the intended contract.
Suggested change
| assert.Less(t, elapsed, 2*time.Second, "detach should return within ~500ms, not wait for command") | |
| assert.Less(t, elapsed, 750*time.Millisecond, "detach should return within defaultQuickCheckTimeout (~500ms) plus small margin, not wait for command") |
Reduce the timing assertion margin for TestExecCommandDetachReturnsQuickly from < 2s to < 1s. The quick check timeout is 500ms, so 1s provides sufficient buffer while catching regressions that 2s would miss. Signed-off-by: mason5052 <ehehwnwjs5052@gmail.com>
mason5052
added a commit
to mason5052/pentagi
that referenced
this pull request
Mar 6, 2026
Rename mockTermLogProvider to contextTestTermLogProvider in terminal_context_test.go to prevent redeclaration error when both PR vxcontrol#179 and PR vxcontrol#181 are merged into the same package. Signed-off-by: mason5052 <ehehwnwjs5052@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description of the Change
Problem
The terminal
ExecCommandfunction (408 lines) had zero test coverage. The detached execution path -- recently fixed for context isolation (#176, #179) -- particularly needed validation.Solution
Add mock infrastructure and 4 test cases for terminal command execution:
Mock infrastructure:
mockDockerClient: Implements fulldocker.DockerClientinterface with configurable responses, timing delays, and error injection. Usesnet.Pipe()for realistic streaming output simulation.mockTermLogProvider: ImplementsTermLogProviderinterface (no-op for tests).var _ Interface = (*mock)(nil)).Test cases:
TestExecCommandDetachReturnsQuickly: Verifiesdetach=truereturns "Command started in background" within ~500ms even when the command takes 2s+TestExecCommandDetachQuickCompletion: Verifiesdetach=truereturns actual output when command finishes before the quick check timeoutTestExecCommandNonDetachWaitsForCompletion: Verifiesdetach=falseblocks until command completes and returns full outputTestExecCommandContainerNotRunning: Verifies proper error handling when container is not runningThe mock infrastructure is reusable for future terminal.go test expansion (ReadFile, WriteFile, etc.).
Related to #176
Adds test coverage for #179
Type of Change
Areas Affected
Testing and Verification
Test Configuration
Test Steps
go test ./pkg/tools/ -run "TestExecCommand" -v-- all 4 tests passgo vet ./pkg/tools/-- no warningsTestSploitusParseExploitsResponse/nginxfailure is unrelated (Windows Defender file access issue on test data)Security Considerations
No security impact. Test-only change. Mock infrastructure does not interact with real Docker.
Checklist
go fmtandgo vet