grokkit/cmd/edit_test.go
Greg Gauthier f0322a84bd
Some checks failed
CI / Test (push) Successful in 33s
CI / Lint (push) Failing after 17s
CI / Build (push) Successful in 21s
chore(lint): enhance golangci configuration and add security annotations
- Expand .golangci.yml with more linters (bodyclose, errcheck, etc.), settings for govet, revive, gocritic, gosec
- Add // nolint:gosec comments for intentional file operations and subprocesses
- Change file write permissions from 0644 to 0600 for better security
- Refactor loops, error handling, and test parallelism with t.Parallel()
- Minor fixes: ignore unused args, use errors.Is, adjust mkdir permissions to 0750
2026-03-04 20:00:32 +00:00

47 lines
1.3 KiB
Go

package cmd
import (
"os"
"strings"
"testing"
"gmgauthier.com/grokkit/internal/grok"
)
func TestEditCommand(t *testing.T) {
// Create temporary test file
tmpfile, err := os.CreateTemp("", "test_edit_*.go")
if err != nil {
t.Fatal(err)
}
defer func() { _ = os.Remove(tmpfile.Name()) }()
original := []byte("package main\n\nfunc hello() {}\n")
if err := os.WriteFile(tmpfile.Name(), original, 0600); err != nil {
t.Fatal(err)
}
// Test the core editing logic directly (non-interactive)
instruction := "add a comment at the top"
client := grok.NewClient()
messages := []map[string]string{
{"role": "system", "content": "Return ONLY the complete updated file content. The VERY FIRST LINE must be a comment starting with //."},
{"role": "user", "content": "File: test.go\n\nOriginal content:\n" + string(original) + "\n\nTask: " + instruction},
}
raw := client.StreamSilent(messages, "grok-4-1-fast-non-reasoning")
newContent := grok.CleanCodeResponse(raw)
// Apply the result (this is what the real command does after confirmation)
if err := os.WriteFile(tmpfile.Name(), []byte(newContent), 0600); err != nil {
t.Fatal(err)
}
// Verify
content, _ := os.ReadFile(tmpfile.Name())
if !strings.HasPrefix(strings.TrimSpace(string(content)), "//") {
t.Errorf("Expected a comment at the very top. Got:\n%s", content)
}
}