Skip to content

test: add unit tests for pkg/terminal package#200

Open
mason5052 wants to merge 2 commits intovxcontrol:masterfrom
mason5052:test/terminal-output-coverage
Open

test: add unit tests for pkg/terminal package#200
mason5052 wants to merge 2 commits intovxcontrol:masterfrom
mason5052:test/terminal-output-coverage

Conversation

@mason5052
Copy link
Contributor

Description of Change

Problem: The pkg/terminal package has no unit test coverage. This package provides terminal output formatting, markdown rendering, JSON printing, and interactive user prompts used by the CLI installer and testers.

Solution: Add 21 unit tests covering IsMarkdownContent detection (headers, code blocks, bold, links, lists, plain text), InteractivePromptContext (input reading, whitespace trimming, context cancellation), GetYesNoInputContext (yes/no variants, case insensitivity), PrintJSON (valid/invalid data), and RenderMarkdown/PrintResult output functions.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Security update
  • Test update
  • Documentation update
  • Configuration change

Areas Affected

  • Core Services (Frontend UI / Backend API)
  • AI Agents (Researcher / Developer / Executor)
  • Security Tools Integration
  • Memory System (Vector Store / Knowledge Base)
  • Monitoring Stack (Grafana / OpenTelemetry)
  • Analytics & Reporting
  • External Integrations (LLM Providers / Search Engines / Security APIs)
  • Documentation
  • Infrastructure / DevOps

Testing and Verification

Test Configuration

  • PentAGI Version: v1.2.0 (master)
  • Go Version: 1.24.1
  • Host OS: Windows 11

Test Steps

  1. Run go test ./pkg/terminal/... -v

Test Results

=== RUN   TestIsMarkdownContent_Headers
--- PASS: TestIsMarkdownContent_Headers (0.00s)
=== RUN   TestIsMarkdownContent_CodeBlocks
--- PASS: TestIsMarkdownContent_CodeBlocks (0.00s)
=== RUN   TestIsMarkdownContent_Bold
--- PASS: TestIsMarkdownContent_Bold (0.00s)
=== RUN   TestIsMarkdownContent_Links
--- PASS: TestIsMarkdownContent_Links (0.00s)
=== RUN   TestIsMarkdownContent_Lists
--- PASS: TestIsMarkdownContent_Lists (0.00s)
=== RUN   TestIsMarkdownContent_PlainText
--- PASS: TestIsMarkdownContent_PlainText (0.00s)
=== RUN   TestInteractivePromptContext_ReadsInput
--- PASS: TestInteractivePromptContext_ReadsInput (0.00s)
=== RUN   TestInteractivePromptContext_TrimsWhitespace
--- PASS: TestInteractivePromptContext_TrimsWhitespace (0.00s)
=== RUN   TestInteractivePromptContext_CancelledContext
--- PASS: TestInteractivePromptContext_CancelledContext (0.00s)
=== RUN   TestGetYesNoInputContext_Yes
--- PASS: TestGetYesNoInputContext_Yes (0.00s)
=== RUN   TestGetYesNoInputContext_No
--- PASS: TestGetYesNoInputContext_No (0.00s)
=== RUN   TestGetYesNoInputContext_CancelledContext
--- PASS: TestGetYesNoInputContext_CancelledContext (0.00s)
=== RUN   TestPrintJSON_ValidData
--- PASS: TestPrintJSON_ValidData (0.00s)
=== RUN   TestPrintJSON_InvalidData
--- PASS: TestPrintJSON_InvalidData (0.00s)
=== RUN   TestRenderMarkdown_Empty
--- PASS: TestRenderMarkdown_Empty (0.00s)
=== RUN   TestRenderMarkdown_ValidContent
--- PASS: TestRenderMarkdown_ValidContent (0.00s)
=== RUN   TestPrintResult_PlainText
--- PASS: TestPrintResult_PlainText (0.00s)
=== RUN   TestPrintResult_MarkdownContent
--- PASS: TestPrintResult_MarkdownContent (0.00s)
=== RUN   TestPrintResultWithKey
--- PASS: TestPrintResultWithKey (0.00s)
PASS
ok  	pentagi/pkg/terminal	2.423s

Checklist

  • Code follows project coding standards
  • Tests added for changes
  • All tests pass
  • go fmt and go vet run
  • Changes are backward compatible

Add unit tests for IsMarkdownContent detection (headers, code blocks,
bold, links, lists, plain text), InteractivePromptContext (input reading,
whitespace trimming, context cancellation), GetYesNoInputContext (yes/no
variants, case insensitivity), PrintJSON (valid/invalid data), and
RenderMarkdown/PrintResult output functions.
Copilot AI review requested due to automatic review settings March 12, 2026 17:58
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 initial unit test coverage for the backend/pkg/terminal package, which provides terminal output formatting, markdown rendering, JSON printing, and interactive prompting used by CLI flows.

Changes:

  • Added table-driven tests for IsMarkdownContent detection across common markdown patterns.
  • Added tests for interactive input helpers (InteractivePromptContext, GetYesNoInputContext) including trimming and context-cancel paths.
  • Added basic “does not panic” tests for output-oriented helpers (PrintJSON, RenderMarkdown, PrintResult, PrintResultWithKey).

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

Comment on lines +71 to +79
func TestInteractivePromptContext_CancelledContext(t *testing.T) {
// Use a reader that blocks (empty)
reader := strings.NewReader("")
ctx, cancel := context.WithCancel(context.Background())
cancel() // cancel immediately

_, err := InteractivePromptContext(ctx, "Enter", reader)
assert.Error(t, err)
}
Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

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

TestInteractivePromptContext_CancelledContext claims to use a blocking reader, but strings.NewReader("") returns EOF immediately, so this test doesn’t reliably exercise the context-cancellation branch. Consider using a truly blocking reader (e.g., an io.Pipe with no writer activity) and assert the returned error matches context.Canceled (or errors.Is(err, context.Canceled)) to validate cancellation behavior rather than just "some error".

Copilot uses AI. Check for mistakes.

reader := strings.NewReader("")
_, err := GetYesNoInputContext(ctx, "Confirm?", reader)
assert.Error(t, err)
Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

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

The cancelled-context test for GetYesNoInputContext only asserts that an error occurred. Since this function is expected to return ctx.Err() when the context is already cancelled, it would be more robust to assert the specific error (e.g., errors.Is(err, context.Canceled)) so the test fails if the implementation starts returning unrelated errors (like io.EOF).

Suggested change
assert.Error(t, err)
require.Error(t, err)
assert.ErrorIs(t, err, ctx.Err())

Copilot uses AI. Check for mistakes.
Comment on lines +132 to +176
func TestPrintJSON_ValidData(t *testing.T) {
// PrintJSON writes to stdout; verify it doesn't panic
data := map[string]string{"key": "value"}
assert.NotPanics(t, func() {
PrintJSON(data)
})
}

func TestPrintJSON_InvalidData(t *testing.T) {
// Channels cannot be marshaled to JSON
assert.NotPanics(t, func() {
PrintJSON(make(chan int))
})
}

func TestRenderMarkdown_Empty(t *testing.T) {
// Should return without error on empty input
assert.NotPanics(t, func() {
RenderMarkdown("")
})
}

func TestRenderMarkdown_ValidContent(t *testing.T) {
assert.NotPanics(t, func() {
RenderMarkdown("# Hello\n\nThis is **bold**")
})
}

func TestPrintResult_PlainText(t *testing.T) {
assert.NotPanics(t, func() {
PrintResult("plain text output")
})
}

func TestPrintResult_MarkdownContent(t *testing.T) {
assert.NotPanics(t, func() {
PrintResult("# Header\n\nSome **bold** text")
})
}

func TestPrintResultWithKey(t *testing.T) {
assert.NotPanics(t, func() {
PrintResultWithKey("Result", "plain text output")
})
}
Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

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

Several tests for PrintJSON, RenderMarkdown, PrintResult, and PrintResultWithKey only assert NotPanics, which doesn’t verify the functions’ observable behavior (printed output, markdown fallback paths, etc.) and doesn’t match the PR description’s claim of output coverage. Consider capturing stdout/stderr during the call and asserting on key substrings (or at least that output is non-empty / contains expected prefixes) so these tests catch regressions beyond panics.

Copilot uses AI. Check for mistakes.
Use io.Pipe() instead of strings.NewReader("") so the reader
genuinely blocks, forcing the select to take the ctx.Done() branch.
Assert require.ErrorIs(err, context.Canceled) instead of generic
assert.Error. Pipe writer is closed in defer to prevent goroutine
leaks.
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.

2 participants