Skip to content

test: add unit tests for terminal ExecCommand detach behavior#181

Closed
mason5052 wants to merge 2 commits intovxcontrol:masterfrom
mason5052:test/terminal-detached-context
Closed

test: add unit tests for terminal ExecCommand detach behavior#181
mason5052 wants to merge 2 commits intovxcontrol:masterfrom
mason5052:test/terminal-detached-context

Conversation

@mason5052
Copy link
Contributor

@mason5052 mason5052 commented Mar 6, 2026

Dependency: Merge #179 first -- this PR tests the detached context isolation fix introduced in #179.

Description of the Change

Problem

The terminal ExecCommand function (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 full docker.DockerClient interface with configurable responses, timing delays, and error injection. Uses net.Pipe() for realistic streaming output simulation.
  • mockTermLogProvider: Implements TermLogProvider interface (no-op for tests).
  • Both include compile-time interface checks (var _ Interface = (*mock)(nil)).

Test cases:

  • TestExecCommandDetachReturnsQuickly: Verifies detach=true returns "Command started in background" within ~500ms even when the command takes 2s+
  • TestExecCommandDetachQuickCompletion: Verifies detach=true returns actual output when command finishes before the quick check timeout
  • TestExecCommandNonDetachWaitsForCompletion: Verifies detach=false blocks until command completes and returns full output
  • TestExecCommandContainerNotRunning: Verifies proper error handling when container is not running

The mock infrastructure is reusable for future terminal.go test expansion (ReadFile, WriteFile, etc.).

Related to #176
Adds test coverage for #179

Type of Change

  • Tests (adding or updating tests)

Areas Affected

  • Core Services (Backend API)

Testing and Verification

Test Configuration

  • PentAGI Version: master
  • Go Version: 1.24

Test Steps

  1. go test ./pkg/tools/ -run "TestExecCommand" -v -- all 4 tests pass
  2. go vet ./pkg/tools/ -- no warnings
  3. Pre-existing TestSploitusParseExploitsResponse/nginx failure 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

  • My code follows the project's coding standards
  • All new and existing tests pass
  • I have run go fmt and go vet
  • Security implications considered
  • Changes are backward compatible

Copilot AI review requested due to automatic review settings March 6, 2026 02:10
Copy link

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 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 mockDockerClient implementing docker.DockerClient with configurable attach delays and streamed output via net.Pipe().
  • Adds mockTermLogProvider implementing TermLogProvider for no-op logging in tests.
  • Adds four ExecCommand unit 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.

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>
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>
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 mason5052 force-pushed the test/terminal-detached-context branch from d9d5ad0 to 977d0e0 Compare March 10, 2026 15:34
mason5052 added a commit to mason5052/pentagi that referenced this pull request Mar 10, 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>
@asdek
Copy link
Contributor

asdek commented Mar 11, 2026

hey @mason5052

thank you for the PR!

there are some unresolved git conflicts left after the transfer, as well as the original terminal_context_test.go file keeps itself for now.
let me move them myself next commit

@asdek asdek closed this Mar 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants