chore(build): add Makefile and tests for commands and utilities
- Introduced Makefile with targets for testing (all, coverage, agent-specific), building, installing, cleaning, and help
- Added unit and integration tests for agent command, edit command, and CleanCodeResponse function
- Refactored CleanCodeResponse to use regex for robust markdown fence removal in agent and client modules
- Ensured tests cover code cleaning, plan generation placeholders, and file editing functionality
2026-03-01 00:24:48 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"os"
|
|
|
|
|
"strings"
|
|
|
|
|
"testing"
|
2026-03-01 00:32:55 +00:00
|
|
|
|
|
|
|
|
"gmgauthier.com/grokkit/internal/grok"
|
chore(build): add Makefile and tests for commands and utilities
- Introduced Makefile with targets for testing (all, coverage, agent-specific), building, installing, cleaning, and help
- Added unit and integration tests for agent command, edit command, and CleanCodeResponse function
- Refactored CleanCodeResponse to use regex for robust markdown fence removal in agent and client modules
- Ensured tests cover code cleaning, plan generation placeholders, and file editing functionality
2026-03-01 00:24:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestEditCommand(t *testing.T) {
|
2026-03-01 00:32:55 +00:00
|
|
|
// Create temporary test file
|
chore(build): add Makefile and tests for commands and utilities
- Introduced Makefile with targets for testing (all, coverage, agent-specific), building, installing, cleaning, and help
- Added unit and integration tests for agent command, edit command, and CleanCodeResponse function
- Refactored CleanCodeResponse to use regex for robust markdown fence removal in agent and client modules
- Ensured tests cover code cleaning, plan generation placeholders, and file editing functionality
2026-03-01 00:24:48 +00:00
|
|
|
tmpfile, err := os.CreateTemp("", "test_edit_*.go")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
defer os.Remove(tmpfile.Name())
|
|
|
|
|
|
|
|
|
|
original := []byte("package main\n\nfunc hello() {}\n")
|
2026-03-01 14:10:24 +00:00
|
|
|
if err := os.WriteFile(tmpfile.Name(), original, 0644); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
chore(build): add Makefile and tests for commands and utilities
- Introduced Makefile with targets for testing (all, coverage, agent-specific), building, installing, cleaning, and help
- Added unit and integration tests for agent command, edit command, and CleanCodeResponse function
- Refactored CleanCodeResponse to use regex for robust markdown fence removal in agent and client modules
- Ensured tests cover code cleaning, plan generation placeholders, and file editing functionality
2026-03-01 00:24:48 +00:00
|
|
|
|
2026-03-01 00:32:55 +00:00
|
|
|
// Test the core editing logic directly (non-interactive)
|
|
|
|
|
instruction := "add a comment at the top"
|
|
|
|
|
client := grok.NewClient()
|
chore(build): add Makefile and tests for commands and utilities
- Introduced Makefile with targets for testing (all, coverage, agent-specific), building, installing, cleaning, and help
- Added unit and integration tests for agent command, edit command, and CleanCodeResponse function
- Refactored CleanCodeResponse to use regex for robust markdown fence removal in agent and client modules
- Ensured tests cover code cleaning, plan generation placeholders, and file editing functionality
2026-03-01 00:24:48 +00:00
|
|
|
|
2026-03-01 00:32:55 +00:00
|
|
|
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},
|
chore(build): add Makefile and tests for commands and utilities
- Introduced Makefile with targets for testing (all, coverage, agent-specific), building, installing, cleaning, and help
- Added unit and integration tests for agent command, edit command, and CleanCodeResponse function
- Refactored CleanCodeResponse to use regex for robust markdown fence removal in agent and client modules
- Ensured tests cover code cleaning, plan generation placeholders, and file editing functionality
2026-03-01 00:24:48 +00:00
|
|
|
}
|
|
|
|
|
|
2026-03-01 00:32:55 +00:00
|
|
|
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)
|
2026-03-01 14:10:24 +00:00
|
|
|
if err := os.WriteFile(tmpfile.Name(), []byte(newContent), 0644); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2026-03-01 00:32:55 +00:00
|
|
|
|
|
|
|
|
// Verify
|
chore(build): add Makefile and tests for commands and utilities
- Introduced Makefile with targets for testing (all, coverage, agent-specific), building, installing, cleaning, and help
- Added unit and integration tests for agent command, edit command, and CleanCodeResponse function
- Refactored CleanCodeResponse to use regex for robust markdown fence removal in agent and client modules
- Ensured tests cover code cleaning, plan generation placeholders, and file editing functionality
2026-03-01 00:24:48 +00:00
|
|
|
content, _ := os.ReadFile(tmpfile.Name())
|
2026-03-01 00:32:55 +00:00
|
|
|
if !strings.HasPrefix(strings.TrimSpace(string(content)), "//") {
|
chore(build): add Makefile and tests for commands and utilities
- Introduced Makefile with targets for testing (all, coverage, agent-specific), building, installing, cleaning, and help
- Added unit and integration tests for agent command, edit command, and CleanCodeResponse function
- Refactored CleanCodeResponse to use regex for robust markdown fence removal in agent and client modules
- Ensured tests cover code cleaning, plan generation placeholders, and file editing functionality
2026-03-01 00:24:48 +00:00
|
|
|
t.Errorf("Expected a comment at the very top. Got:\n%s", content)
|
|
|
|
|
}
|
|
|
|
|
}
|