test: Add test coverage for 16 files (40.3% → 45.5%) (#463)
* test: Add test coverage for 16 files (40.3% -> 45.5%) Add comprehensive tests for previously untested packages: - internal/agent/state_test.go - internal/cmd/errors_test.go - internal/crew/types_test.go - internal/doctor/errors_test.go - internal/dog/types_test.go - internal/mail/bd_test.go - internal/opencode/plugin_test.go - internal/rig/overlay_test.go - internal/runtime/runtime_test.go - internal/session/town_test.go - internal/style/style_test.go - internal/ui/markdown_test.go - internal/ui/terminal_test.go - internal/wisp/io_test.go - internal/wisp/types_test.go - internal/witness/types_test.go style_test.go uses func(...string) to match lipgloss variadic Render signature. * fix(lint): remove unused error return from buildCVSummary buildCVSummary always returned nil for its error value, causing golangci-lint to fail with "result 1 (error) is always nil". The function handles errors internally by returning partial data, so the error return was misleading. Removed it and updated caller.
This commit is contained in:
234
internal/ui/markdown_test.go
Normal file
234
internal/ui/markdown_test.go
Normal file
@@ -0,0 +1,234 @@
|
||||
package ui
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRenderMarkdown_AgentMode(t *testing.T) {
|
||||
oldAgentMode := os.Getenv("GT_AGENT_MODE")
|
||||
defer func() {
|
||||
if oldAgentMode != "" {
|
||||
os.Setenv("GT_AGENT_MODE", oldAgentMode)
|
||||
} else {
|
||||
os.Unsetenv("GT_AGENT_MODE")
|
||||
}
|
||||
}()
|
||||
|
||||
os.Setenv("GT_AGENT_MODE", "1")
|
||||
|
||||
markdown := "# Test Header\n\nSome content"
|
||||
result := RenderMarkdown(markdown)
|
||||
|
||||
if result != markdown {
|
||||
t.Errorf("RenderMarkdown() in agent mode should return raw markdown, got %q", result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderMarkdown_SimpleText(t *testing.T) {
|
||||
oldAgentMode := os.Getenv("GT_AGENT_MODE")
|
||||
oldNoColor := os.Getenv("NO_COLOR")
|
||||
defer func() {
|
||||
if oldAgentMode != "" {
|
||||
os.Setenv("GT_AGENT_MODE", oldAgentMode)
|
||||
} else {
|
||||
os.Unsetenv("GT_AGENT_MODE")
|
||||
}
|
||||
if oldNoColor != "" {
|
||||
os.Setenv("NO_COLOR", oldNoColor)
|
||||
} else {
|
||||
os.Unsetenv("NO_COLOR")
|
||||
}
|
||||
}()
|
||||
|
||||
os.Unsetenv("GT_AGENT_MODE")
|
||||
os.Setenv("NO_COLOR", "1") // Disable glamour rendering
|
||||
|
||||
markdown := "Simple text without formatting"
|
||||
result := RenderMarkdown(markdown)
|
||||
|
||||
// When color is disabled, should return raw markdown
|
||||
if result != markdown {
|
||||
t.Errorf("RenderMarkdown() with color disabled should return raw markdown, got %q", result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderMarkdown_EmptyString(t *testing.T) {
|
||||
oldAgentMode := os.Getenv("GT_AGENT_MODE")
|
||||
oldNoColor := os.Getenv("NO_COLOR")
|
||||
defer func() {
|
||||
if oldAgentMode != "" {
|
||||
os.Setenv("GT_AGENT_MODE", oldAgentMode)
|
||||
} else {
|
||||
os.Unsetenv("GT_AGENT_MODE")
|
||||
}
|
||||
if oldNoColor != "" {
|
||||
os.Setenv("NO_COLOR", oldNoColor)
|
||||
} else {
|
||||
os.Unsetenv("NO_COLOR")
|
||||
}
|
||||
}()
|
||||
|
||||
os.Unsetenv("GT_AGENT_MODE")
|
||||
os.Setenv("NO_COLOR", "1")
|
||||
|
||||
result := RenderMarkdown("")
|
||||
if result != "" {
|
||||
t.Errorf("RenderMarkdown() with empty string should return empty, got %q", result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderMarkdown_GracefulDegradation(t *testing.T) {
|
||||
oldAgentMode := os.Getenv("GT_AGENT_MODE")
|
||||
oldNoColor := os.Getenv("NO_COLOR")
|
||||
defer func() {
|
||||
if oldAgentMode != "" {
|
||||
os.Setenv("GT_AGENT_MODE", oldAgentMode)
|
||||
} else {
|
||||
os.Unsetenv("GT_AGENT_MODE")
|
||||
}
|
||||
if oldNoColor != "" {
|
||||
os.Setenv("NO_COLOR", oldNoColor)
|
||||
} else {
|
||||
os.Unsetenv("NO_COLOR")
|
||||
}
|
||||
}()
|
||||
|
||||
os.Unsetenv("GT_AGENT_MODE")
|
||||
os.Setenv("NO_COLOR", "1")
|
||||
|
||||
// Test that function doesn't panic and always returns something
|
||||
markdown := "# Test\n\nContent with **bold** and *italic*"
|
||||
result := RenderMarkdown(markdown)
|
||||
|
||||
if result == "" {
|
||||
t.Error("RenderMarkdown() should never return empty string for non-empty input")
|
||||
}
|
||||
// With NO_COLOR, should return raw markdown
|
||||
if !strings.Contains(result, "bold") {
|
||||
t.Error("RenderMarkdown() should contain original content")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetTerminalWidth(t *testing.T) {
|
||||
// This function is unexported, but we can test it indirectly
|
||||
// The function should return a reasonable width
|
||||
// Since we can't call it directly, we verify RenderMarkdown doesn't panic
|
||||
|
||||
oldAgentMode := os.Getenv("GT_AGENT_MODE")
|
||||
oldNoColor := os.Getenv("NO_COLOR")
|
||||
defer func() {
|
||||
if oldAgentMode != "" {
|
||||
os.Setenv("GT_AGENT_MODE", oldAgentMode)
|
||||
} else {
|
||||
os.Unsetenv("GT_AGENT_MODE")
|
||||
}
|
||||
if oldNoColor != "" {
|
||||
os.Setenv("NO_COLOR", oldNoColor)
|
||||
} else {
|
||||
os.Unsetenv("NO_COLOR")
|
||||
}
|
||||
}()
|
||||
|
||||
os.Unsetenv("GT_AGENT_MODE")
|
||||
os.Setenv("NO_COLOR", "1")
|
||||
|
||||
// This should not panic even if terminal width detection fails
|
||||
markdown := strings.Repeat("word ", 1000) // Long content
|
||||
result := RenderMarkdown(markdown)
|
||||
|
||||
if result == "" {
|
||||
t.Error("RenderMarkdown() should handle long content")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderMarkdown_Newlines(t *testing.T) {
|
||||
oldAgentMode := os.Getenv("GT_AGENT_MODE")
|
||||
oldNoColor := os.Getenv("NO_COLOR")
|
||||
defer func() {
|
||||
if oldAgentMode != "" {
|
||||
os.Setenv("GT_AGENT_MODE", oldAgentMode)
|
||||
} else {
|
||||
os.Unsetenv("GT_AGENT_MODE")
|
||||
}
|
||||
if oldNoColor != "" {
|
||||
os.Setenv("NO_COLOR", oldNoColor)
|
||||
} else {
|
||||
os.Unsetenv("NO_COLOR")
|
||||
}
|
||||
}()
|
||||
|
||||
os.Unsetenv("GT_AGENT_MODE")
|
||||
os.Setenv("NO_COLOR", "1")
|
||||
|
||||
markdown := "Line 1\n\nLine 2\n\nLine 3"
|
||||
result := RenderMarkdown(markdown)
|
||||
|
||||
// With color disabled, newlines should be preserved
|
||||
if !strings.Contains(result, "Line 1") {
|
||||
t.Error("RenderMarkdown() should preserve first line")
|
||||
}
|
||||
if !strings.Contains(result, "Line 2") {
|
||||
t.Error("RenderMarkdown() should preserve second line")
|
||||
}
|
||||
if !strings.Contains(result, "Line 3") {
|
||||
t.Error("RenderMarkdown() should preserve third line")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderMarkdown_CodeBlocks(t *testing.T) {
|
||||
oldAgentMode := os.Getenv("GT_AGENT_MODE")
|
||||
oldNoColor := os.Getenv("NO_COLOR")
|
||||
defer func() {
|
||||
if oldAgentMode != "" {
|
||||
os.Setenv("GT_AGENT_MODE", oldAgentMode)
|
||||
} else {
|
||||
os.Unsetenv("GT_AGENT_MODE")
|
||||
}
|
||||
if oldNoColor != "" {
|
||||
os.Setenv("NO_COLOR", oldNoColor)
|
||||
} else {
|
||||
os.Unsetenv("NO_COLOR")
|
||||
}
|
||||
}()
|
||||
|
||||
os.Unsetenv("GT_AGENT_MODE")
|
||||
os.Setenv("NO_COLOR", "1")
|
||||
|
||||
markdown := "```go\nfunc main() {}\n```"
|
||||
result := RenderMarkdown(markdown)
|
||||
|
||||
// Should contain the code
|
||||
if !strings.Contains(result, "func main") {
|
||||
t.Error("RenderMarkdown() should preserve code block content")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderMarkdown_Links(t *testing.T) {
|
||||
oldAgentMode := os.Getenv("GT_AGENT_MODE")
|
||||
oldNoColor := os.Getenv("NO_COLOR")
|
||||
defer func() {
|
||||
if oldAgentMode != "" {
|
||||
os.Setenv("GT_AGENT_MODE", oldAgentMode)
|
||||
} else {
|
||||
os.Unsetenv("GT_AGENT_MODE")
|
||||
}
|
||||
if oldNoColor != "" {
|
||||
os.Setenv("NO_COLOR", oldNoColor)
|
||||
} else {
|
||||
os.Unsetenv("NO_COLOR")
|
||||
}
|
||||
}()
|
||||
|
||||
os.Unsetenv("GT_AGENT_MODE")
|
||||
os.Setenv("NO_COLOR", "1")
|
||||
|
||||
markdown := "[link text](https://example.com)"
|
||||
result := RenderMarkdown(markdown)
|
||||
|
||||
// Should contain the link text or URL
|
||||
if !strings.Contains(result, "link text") && !strings.Contains(result, "example.com") {
|
||||
t.Error("RenderMarkdown() should preserve link information")
|
||||
}
|
||||
}
|
||||
247
internal/ui/terminal_test.go
Normal file
247
internal/ui/terminal_test.go
Normal file
@@ -0,0 +1,247 @@
|
||||
package ui
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestIsTerminal(t *testing.T) {
|
||||
// This test verifies the function doesn't panic
|
||||
// The actual result depends on the test environment
|
||||
result := IsTerminal()
|
||||
// In test environment, this is usually false
|
||||
// The important thing is it doesn't crash and returns a bool
|
||||
var _ bool = result
|
||||
}
|
||||
|
||||
func TestShouldUseColor_Default(t *testing.T) {
|
||||
// Clean environment for this test
|
||||
oldNoColor := os.Getenv("NO_COLOR")
|
||||
oldClicolor := os.Getenv("CLICOLOR")
|
||||
oldClicolorForce := os.Getenv("CLICOLOR_FORCE")
|
||||
defer func() {
|
||||
if oldNoColor != "" {
|
||||
os.Setenv("NO_COLOR", oldNoColor)
|
||||
} else {
|
||||
os.Unsetenv("NO_COLOR")
|
||||
}
|
||||
if oldClicolor != "" {
|
||||
os.Setenv("CLICOLOR", oldClicolor)
|
||||
} else {
|
||||
os.Unsetenv("CLICOLOR")
|
||||
}
|
||||
if oldClicolorForce != "" {
|
||||
os.Setenv("CLICOLOR_FORCE", oldClicolorForce)
|
||||
} else {
|
||||
os.Unsetenv("CLICOLOR_FORCE")
|
||||
}
|
||||
}()
|
||||
|
||||
os.Unsetenv("NO_COLOR")
|
||||
os.Unsetenv("CLICOLOR")
|
||||
os.Unsetenv("CLICOLOR_FORCE")
|
||||
|
||||
result := ShouldUseColor()
|
||||
// In non-TTY test environment, should be false
|
||||
_ = result
|
||||
}
|
||||
|
||||
func TestShouldUseColor_NO_COLOR(t *testing.T) {
|
||||
oldNoColor := os.Getenv("NO_COLOR")
|
||||
defer func() {
|
||||
if oldNoColor != "" {
|
||||
os.Setenv("NO_COLOR", oldNoColor)
|
||||
} else {
|
||||
os.Unsetenv("NO_COLOR")
|
||||
}
|
||||
}()
|
||||
|
||||
os.Setenv("NO_COLOR", "1")
|
||||
if ShouldUseColor() {
|
||||
t.Error("ShouldUseColor() should return false when NO_COLOR is set")
|
||||
}
|
||||
}
|
||||
|
||||
func TestShouldUseColor_NO_COLOR_AnyValue(t *testing.T) {
|
||||
oldNoColor := os.Getenv("NO_COLOR")
|
||||
defer func() {
|
||||
if oldNoColor != "" {
|
||||
os.Setenv("NO_COLOR", oldNoColor)
|
||||
} else {
|
||||
os.Unsetenv("NO_COLOR")
|
||||
}
|
||||
}()
|
||||
|
||||
// NO_COLOR with any value (even "0") should disable color
|
||||
os.Setenv("NO_COLOR", "0")
|
||||
if ShouldUseColor() {
|
||||
t.Error("ShouldUseColor() should return false when NO_COLOR is set to any value")
|
||||
}
|
||||
}
|
||||
|
||||
func TestShouldUseColor_CLICOLOR_0(t *testing.T) {
|
||||
oldNoColor := os.Getenv("NO_COLOR")
|
||||
oldClicolor := os.Getenv("CLICOLOR")
|
||||
defer func() {
|
||||
if oldNoColor != "" {
|
||||
os.Setenv("NO_COLOR", oldNoColor)
|
||||
} else {
|
||||
os.Unsetenv("NO_COLOR")
|
||||
}
|
||||
if oldClicolor != "" {
|
||||
os.Setenv("CLICOLOR", oldClicolor)
|
||||
} else {
|
||||
os.Unsetenv("CLICOLOR")
|
||||
}
|
||||
}()
|
||||
|
||||
os.Unsetenv("NO_COLOR")
|
||||
os.Setenv("CLICOLOR", "0")
|
||||
if ShouldUseColor() {
|
||||
t.Error("ShouldUseColor() should return false when CLICOLOR=0")
|
||||
}
|
||||
}
|
||||
|
||||
func TestShouldUseColor_CLICOLOR_FORCE(t *testing.T) {
|
||||
oldNoColor := os.Getenv("NO_COLOR")
|
||||
oldClicolorForce := os.Getenv("CLICOLOR_FORCE")
|
||||
defer func() {
|
||||
if oldNoColor != "" {
|
||||
os.Setenv("NO_COLOR", oldNoColor)
|
||||
} else {
|
||||
os.Unsetenv("NO_COLOR")
|
||||
}
|
||||
if oldClicolorForce != "" {
|
||||
os.Setenv("CLICOLOR_FORCE", oldClicolorForce)
|
||||
} else {
|
||||
os.Unsetenv("CLICOLOR_FORCE")
|
||||
}
|
||||
}()
|
||||
|
||||
os.Unsetenv("NO_COLOR")
|
||||
os.Setenv("CLICOLOR_FORCE", "1")
|
||||
if !ShouldUseColor() {
|
||||
t.Error("ShouldUseColor() should return true when CLICOLOR_FORCE is set")
|
||||
}
|
||||
}
|
||||
|
||||
func TestShouldUseEmoji_Default(t *testing.T) {
|
||||
oldNoEmoji := os.Getenv("GT_NO_EMOJI")
|
||||
defer func() {
|
||||
if oldNoEmoji != "" {
|
||||
os.Setenv("GT_NO_EMOJI", oldNoEmoji)
|
||||
} else {
|
||||
os.Unsetenv("GT_NO_EMOJI")
|
||||
}
|
||||
}()
|
||||
|
||||
os.Unsetenv("GT_NO_EMOJI")
|
||||
result := ShouldUseEmoji()
|
||||
_ = result // Result depends on test environment
|
||||
}
|
||||
|
||||
func TestShouldUseEmoji_GT_NO_EMOJI(t *testing.T) {
|
||||
oldNoEmoji := os.Getenv("GT_NO_EMOJI")
|
||||
defer func() {
|
||||
if oldNoEmoji != "" {
|
||||
os.Setenv("GT_NO_EMOJI", oldNoEmoji)
|
||||
} else {
|
||||
os.Unsetenv("GT_NO_EMOJI")
|
||||
}
|
||||
}()
|
||||
|
||||
os.Setenv("GT_NO_EMOJI", "1")
|
||||
if ShouldUseEmoji() {
|
||||
t.Error("ShouldUseEmoji() should return false when GT_NO_EMOJI is set")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsAgentMode_Default(t *testing.T) {
|
||||
oldAgentMode := os.Getenv("GT_AGENT_MODE")
|
||||
oldClaudeCode := os.Getenv("CLAUDE_CODE")
|
||||
defer func() {
|
||||
if oldAgentMode != "" {
|
||||
os.Setenv("GT_AGENT_MODE", oldAgentMode)
|
||||
} else {
|
||||
os.Unsetenv("GT_AGENT_MODE")
|
||||
}
|
||||
if oldClaudeCode != "" {
|
||||
os.Setenv("CLAUDE_CODE", oldClaudeCode)
|
||||
} else {
|
||||
os.Unsetenv("CLAUDE_CODE")
|
||||
}
|
||||
}()
|
||||
|
||||
os.Unsetenv("GT_AGENT_MODE")
|
||||
os.Unsetenv("CLAUDE_CODE")
|
||||
if IsAgentMode() {
|
||||
t.Error("IsAgentMode() should return false by default")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsAgentMode_GT_AGENT_MODE(t *testing.T) {
|
||||
oldAgentMode := os.Getenv("GT_AGENT_MODE")
|
||||
defer func() {
|
||||
if oldAgentMode != "" {
|
||||
os.Setenv("GT_AGENT_MODE", oldAgentMode)
|
||||
} else {
|
||||
os.Unsetenv("GT_AGENT_MODE")
|
||||
}
|
||||
}()
|
||||
|
||||
os.Setenv("GT_AGENT_MODE", "1")
|
||||
if !IsAgentMode() {
|
||||
t.Error("IsAgentMode() should return true when GT_AGENT_MODE=1")
|
||||
}
|
||||
|
||||
os.Setenv("GT_AGENT_MODE", "0")
|
||||
if IsAgentMode() {
|
||||
t.Error("IsAgentMode() should return false when GT_AGENT_MODE=0")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsAgentMode_CLAUDE_CODE(t *testing.T) {
|
||||
oldAgentMode := os.Getenv("GT_AGENT_MODE")
|
||||
oldClaudeCode := os.Getenv("CLAUDE_CODE")
|
||||
defer func() {
|
||||
if oldAgentMode != "" {
|
||||
os.Setenv("GT_AGENT_MODE", oldAgentMode)
|
||||
} else {
|
||||
os.Unsetenv("GT_AGENT_MODE")
|
||||
}
|
||||
if oldClaudeCode != "" {
|
||||
os.Setenv("CLAUDE_CODE", oldClaudeCode)
|
||||
} else {
|
||||
os.Unsetenv("CLAUDE_CODE")
|
||||
}
|
||||
}()
|
||||
|
||||
os.Unsetenv("GT_AGENT_MODE")
|
||||
os.Setenv("CLAUDE_CODE", "1")
|
||||
if !IsAgentMode() {
|
||||
t.Error("IsAgentMode() should return true when CLAUDE_CODE is set")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsAgentMode_CLAUDE_CODE_AnyValue(t *testing.T) {
|
||||
oldAgentMode := os.Getenv("GT_AGENT_MODE")
|
||||
oldClaudeCode := os.Getenv("CLAUDE_CODE")
|
||||
defer func() {
|
||||
if oldAgentMode != "" {
|
||||
os.Setenv("GT_AGENT_MODE", oldAgentMode)
|
||||
} else {
|
||||
os.Unsetenv("GT_AGENT_MODE")
|
||||
}
|
||||
if oldClaudeCode != "" {
|
||||
os.Setenv("CLAUDE_CODE", oldClaudeCode)
|
||||
} else {
|
||||
os.Unsetenv("CLAUDE_CODE")
|
||||
}
|
||||
}()
|
||||
|
||||
os.Unsetenv("GT_AGENT_MODE")
|
||||
os.Setenv("CLAUDE_CODE", "any-value")
|
||||
if !IsAgentMode() {
|
||||
t.Error("IsAgentMode() should return true when CLAUDE_CODE is set to any value")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user