Files
Ed Zynda 2016570e2d test: add docstrings to rewritten tests and use t.Setenv
Addresses two CodeRabbit feedback items on PR #24:

* Docstring coverage warning (was 57.14%, threshold 80%): adds godoc
  comments to the four test functions added or substantially rewritten
  in this PR — TestLoadAndSaveManifest, TestAddAndRemoveFromManifest,
  TestFindInManifest, TestHighlightFileTokensInjectsANSI.
* Quick-win nitpick: replaces the manual os.Setenv/os.Unsetenv +
  defer pattern in TestFindInManifest with t.Setenv, which restores
  the env var automatically on cleanup even on panic or t.Fatal.

go test -race ./... still passes.
2026-05-07 13:16:03 +03:00

92 lines
2.7 KiB
Go

package render
import (
"strings"
"testing"
"github.com/mark3labs/kit/internal/ui/style"
)
func TestHighlightFileTokens(t *testing.T) {
theme := style.DefaultTheme()
tests := []struct {
name string
input string
wantHas []string // substrings that must be present in the output
wantNone []string // substrings that must NOT be present as plain text
}{
{
name: "no tokens",
input: "hello world",
wantHas: []string{"hello world"},
},
{
name: "single unquoted token",
input: "refactor @main.go please",
wantHas: []string{"@main.go", "refactor", "please"},
},
{
name: "quoted token with spaces",
input: `check @"path with spaces/file.txt" out`,
wantHas: []string{`@"path with spaces/file.txt"`, "check", "out"},
},
{
name: "multiple tokens",
input: "@main.go @utils.go refactor these",
wantHas: []string{"@main.go", "@utils.go", "refactor these"},
},
{
name: "path with directory",
input: "look at @internal/ui/render/blocks.go",
wantHas: []string{"@internal/ui/render/blocks.go", "look at"},
},
{
name: "empty string",
input: "",
wantHas: []string{""},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := HighlightFileTokens(tt.input, theme)
for _, want := range tt.wantHas {
if !strings.Contains(result, want) {
t.Errorf("HighlightFileTokens(%q) = %q, want substring %q", tt.input, result, want)
}
}
// If there were @tokens, the result should contain ANSI escape
// sequences (from lipgloss styling).
if fileTokenPattern.MatchString(tt.input) && !strings.Contains(result, "\x1b[") {
t.Errorf("HighlightFileTokens(%q) should contain ANSI escapes for @tokens but got %q", tt.input, result)
}
})
}
}
// TestHighlightFileTokensInjectsANSI verifies that HighlightFileTokens
// preserves the original @file references in the output and wraps each
// token with ANSI escape codes for the theme accent color.
func TestHighlightFileTokensInjectsANSI(t *testing.T) {
theme := style.DefaultTheme()
content := "refactor @main.go and @utils.go"
result := HighlightFileTokens(content, theme)
// The output should still contain both file references.
if !strings.Contains(result, "@main.go") {
t.Errorf("HighlightFileTokens output should contain @main.go, got:\n%s", result)
}
if !strings.Contains(result, "@utils.go") {
t.Errorf("HighlightFileTokens output should contain @utils.go, got:\n%s", result)
}
// Verify ANSI codes are present (the tokens are styled).
if !strings.Contains(result, "\x1b[") {
t.Errorf("HighlightFileTokens output should contain ANSI escape codes for styled @file tokens")
}
}