test: add unit tests for pkg/terminal package#200
test: add unit tests for pkg/terminal package#200mason5052 wants to merge 2 commits intovxcontrol:masterfrom
Conversation
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.
There was a problem hiding this comment.
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
IsMarkdownContentdetection 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.
| 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) | ||
| } |
There was a problem hiding this comment.
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".
backend/pkg/terminal/output_test.go
Outdated
|
|
||
| reader := strings.NewReader("") | ||
| _, err := GetYesNoInputContext(ctx, "Confirm?", reader) | ||
| assert.Error(t, err) |
There was a problem hiding this comment.
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).
| assert.Error(t, err) | |
| require.Error(t, err) | |
| assert.ErrorIs(t, err, ctx.Err()) |
| 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") | ||
| }) | ||
| } |
There was a problem hiding this comment.
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.
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.
Description of Change
Problem: The
pkg/terminalpackage 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
Areas Affected
Testing and Verification
Test Configuration
Test Steps
go test ./pkg/terminal/... -vTest Results
Checklist
go fmtandgo vetrun