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 grok
|
|
|
|
|
|
feat: add CI/CD workflows, persistent chat, shell completions, and testing
- Add Gitea CI workflow for testing, linting, and building
- Add release workflow for multi-platform builds and GitHub releases
- Implement persistent chat history with JSON storage
- Add shell completion generation for bash, zsh, fish, powershell
- Introduce custom error types and logging system
- Add interfaces for git and AI client for better testability
- Enhance config with temperature and timeout settings
- Add comprehensive unit tests for config, errors, git, grok, and logger
- Update README with installation, features, and development instructions
- Make model flag persistent across commands
- Add context timeouts to API requests
2026-03-01 12:17:22 +00:00
|
|
|
import (
|
2026-03-02 20:47:16 +00:00
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/http/httptest"
|
feat: add CI/CD workflows, persistent chat, shell completions, and testing
- Add Gitea CI workflow for testing, linting, and building
- Add release workflow for multi-platform builds and GitHub releases
- Implement persistent chat history with JSON storage
- Add shell completion generation for bash, zsh, fish, powershell
- Introduce custom error types and logging system
- Add interfaces for git and AI client for better testability
- Enhance config with temperature and timeout settings
- Add comprehensive unit tests for config, errors, git, grok, and logger
- Update README with installation, features, and development instructions
- Make model flag persistent across commands
- Add context timeouts to API requests
2026-03-01 12:17:22 +00:00
|
|
|
"os"
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
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 TestCleanCodeResponse(t *testing.T) {
|
|
|
|
|
tests := []struct {
|
|
|
|
|
name string
|
|
|
|
|
input string
|
|
|
|
|
expected string
|
|
|
|
|
}{
|
|
|
|
|
{
|
feat: add CI/CD workflows, persistent chat, shell completions, and testing
- Add Gitea CI workflow for testing, linting, and building
- Add release workflow for multi-platform builds and GitHub releases
- Implement persistent chat history with JSON storage
- Add shell completion generation for bash, zsh, fish, powershell
- Introduce custom error types and logging system
- Add interfaces for git and AI client for better testability
- Enhance config with temperature and timeout settings
- Add comprehensive unit tests for config, errors, git, grok, and logger
- Update README with installation, features, and development instructions
- Make model flag persistent across commands
- Add context timeouts to API requests
2026-03-01 12:17:22 +00:00
|
|
|
name: "removes markdown fences",
|
|
|
|
|
input: "```go\nfunc main() {}\n```",
|
|
|
|
|
expected: "func main() {}",
|
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
|
|
|
},
|
|
|
|
|
{
|
feat: add CI/CD workflows, persistent chat, shell completions, and testing
- Add Gitea CI workflow for testing, linting, and building
- Add release workflow for multi-platform builds and GitHub releases
- Implement persistent chat history with JSON storage
- Add shell completion generation for bash, zsh, fish, powershell
- Introduce custom error types and logging system
- Add interfaces for git and AI client for better testability
- Enhance config with temperature and timeout settings
- Add comprehensive unit tests for config, errors, git, grok, and logger
- Update README with installation, features, and development instructions
- Make model flag persistent across commands
- Add context timeouts to API requests
2026-03-01 12:17:22 +00:00
|
|
|
name: "removes language tag",
|
|
|
|
|
input: "```python\nprint('hello')\n```",
|
|
|
|
|
expected: "print('hello')",
|
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
|
|
|
},
|
|
|
|
|
{
|
feat: add CI/CD workflows, persistent chat, shell completions, and testing
- Add Gitea CI workflow for testing, linting, and building
- Add release workflow for multi-platform builds and GitHub releases
- Implement persistent chat history with JSON storage
- Add shell completion generation for bash, zsh, fish, powershell
- Introduce custom error types and logging system
- Add interfaces for git and AI client for better testability
- Enhance config with temperature and timeout settings
- Add comprehensive unit tests for config, errors, git, grok, and logger
- Update README with installation, features, and development instructions
- Make model flag persistent across commands
- Add context timeouts to API requests
2026-03-01 12:17:22 +00:00
|
|
|
name: "handles no fences",
|
|
|
|
|
input: "func main() {}",
|
|
|
|
|
expected: "func main() {}",
|
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
|
|
|
},
|
|
|
|
|
{
|
feat: add CI/CD workflows, persistent chat, shell completions, and testing
- Add Gitea CI workflow for testing, linting, and building
- Add release workflow for multi-platform builds and GitHub releases
- Implement persistent chat history with JSON storage
- Add shell completion generation for bash, zsh, fish, powershell
- Introduce custom error types and logging system
- Add interfaces for git and AI client for better testability
- Enhance config with temperature and timeout settings
- Add comprehensive unit tests for config, errors, git, grok, and logger
- Update README with installation, features, and development instructions
- Make model flag persistent across commands
- Add context timeouts to API requests
2026-03-01 12:17:22 +00:00
|
|
|
name: "preserves internal blank lines",
|
|
|
|
|
input: "```\nline1\n\nline2\n```",
|
|
|
|
|
expected: "line1\n\nline2",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "trims whitespace",
|
|
|
|
|
input: " \n```\ncode\n```\n ",
|
|
|
|
|
expected: "code",
|
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
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
feat: add CI/CD workflows, persistent chat, shell completions, and testing
- Add Gitea CI workflow for testing, linting, and building
- Add release workflow for multi-platform builds and GitHub releases
- Implement persistent chat history with JSON storage
- Add shell completion generation for bash, zsh, fish, powershell
- Introduce custom error types and logging system
- Add interfaces for git and AI client for better testability
- Enhance config with temperature and timeout settings
- Add comprehensive unit tests for config, errors, git, grok, and logger
- Update README with installation, features, and development instructions
- Make model flag persistent across commands
- Add context timeouts to API requests
2026-03-01 12:17:22 +00:00
|
|
|
result := CleanCodeResponse(tt.input)
|
|
|
|
|
if result != tt.expected {
|
|
|
|
|
t.Errorf("CleanCodeResponse() = %q, want %q", result, tt.expected)
|
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
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
feat: add CI/CD workflows, persistent chat, shell completions, and testing
- Add Gitea CI workflow for testing, linting, and building
- Add release workflow for multi-platform builds and GitHub releases
- Implement persistent chat history with JSON storage
- Add shell completion generation for bash, zsh, fish, powershell
- Introduce custom error types and logging system
- Add interfaces for git and AI client for better testability
- Enhance config with temperature and timeout settings
- Add comprehensive unit tests for config, errors, git, grok, and logger
- Update README with installation, features, and development instructions
- Make model flag persistent across commands
- Add context timeouts to API requests
2026-03-01 12:17:22 +00:00
|
|
|
|
2026-03-02 20:47:16 +00:00
|
|
|
func sseServer(chunks []string) *httptest.Server {
|
|
|
|
|
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
w.Header().Set("Content-Type", "text/event-stream")
|
|
|
|
|
for _, c := range chunks {
|
|
|
|
|
fmt.Fprintf(w, "data: {\"choices\":[{\"delta\":{\"content\":%q}}]}\n\n", c)
|
|
|
|
|
}
|
|
|
|
|
fmt.Fprintf(w, "data: [DONE]\n\n")
|
|
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestStreamSilent(t *testing.T) {
|
|
|
|
|
srv := sseServer([]string{"Hello", " ", "World"})
|
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
|
|
client := &Client{APIKey: "test-key", BaseURL: srv.URL}
|
|
|
|
|
got := client.StreamSilent([]map[string]string{{"role": "user", "content": "hi"}}, "test-model")
|
|
|
|
|
if got != "Hello World" {
|
|
|
|
|
t.Errorf("StreamSilent() = %q, want %q", got, "Hello World")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestStream(t *testing.T) {
|
|
|
|
|
srv := sseServer([]string{"foo", "bar"})
|
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
|
|
client := &Client{APIKey: "test-key", BaseURL: srv.URL}
|
|
|
|
|
got := client.Stream([]map[string]string{{"role": "user", "content": "hi"}}, "test-model")
|
|
|
|
|
if got != "foobar" {
|
|
|
|
|
t.Errorf("Stream() = %q, want %q", got, "foobar")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestStreamWithTemp(t *testing.T) {
|
|
|
|
|
srv := sseServer([]string{"response"})
|
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
|
|
client := &Client{APIKey: "test-key", BaseURL: srv.URL}
|
|
|
|
|
got := client.StreamWithTemp([]map[string]string{{"role": "user", "content": "hi"}}, "test-model", 0.5)
|
|
|
|
|
if got != "response" {
|
|
|
|
|
t.Errorf("StreamWithTemp() = %q, want %q", got, "response")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestStreamDoneSignal(t *testing.T) {
|
|
|
|
|
// Verifies that [DONE] stops processing and non-content chunks are skipped
|
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
w.Header().Set("Content-Type", "text/event-stream")
|
|
|
|
|
fmt.Fprintf(w, "data: {\"choices\":[{\"delta\":{\"content\":\"ok\"}}]}\n\n")
|
|
|
|
|
fmt.Fprintf(w, "data: [DONE]\n\n")
|
|
|
|
|
// This line should never be processed
|
|
|
|
|
fmt.Fprintf(w, "data: {\"choices\":[{\"delta\":{\"content\":\"extra\"}}]}\n\n")
|
|
|
|
|
}))
|
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
|
|
client := &Client{APIKey: "test-key", BaseURL: srv.URL}
|
|
|
|
|
got := client.StreamSilent(nil, "test-model")
|
|
|
|
|
if got != "ok" {
|
|
|
|
|
t.Errorf("got %q, want %q", got, "ok")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestStreamEmptyResponse(t *testing.T) {
|
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
w.Header().Set("Content-Type", "text/event-stream")
|
|
|
|
|
fmt.Fprintf(w, "data: [DONE]\n\n")
|
|
|
|
|
}))
|
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
|
|
client := &Client{APIKey: "test-key", BaseURL: srv.URL}
|
|
|
|
|
got := client.StreamSilent(nil, "test-model")
|
|
|
|
|
if got != "" {
|
|
|
|
|
t.Errorf("got %q, want empty string", got)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
feat: add CI/CD workflows, persistent chat, shell completions, and testing
- Add Gitea CI workflow for testing, linting, and building
- Add release workflow for multi-platform builds and GitHub releases
- Implement persistent chat history with JSON storage
- Add shell completion generation for bash, zsh, fish, powershell
- Introduce custom error types and logging system
- Add interfaces for git and AI client for better testability
- Enhance config with temperature and timeout settings
- Add comprehensive unit tests for config, errors, git, grok, and logger
- Update README with installation, features, and development instructions
- Make model flag persistent across commands
- Add context timeouts to API requests
2026-03-01 12:17:22 +00:00
|
|
|
func TestNewClient(t *testing.T) {
|
|
|
|
|
// Save and restore env
|
|
|
|
|
oldKey := os.Getenv("XAI_API_KEY")
|
|
|
|
|
defer func() {
|
|
|
|
|
if oldKey != "" {
|
|
|
|
|
os.Setenv("XAI_API_KEY", oldKey)
|
|
|
|
|
} else {
|
|
|
|
|
os.Unsetenv("XAI_API_KEY")
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
os.Setenv("XAI_API_KEY", "test-key")
|
|
|
|
|
client := NewClient()
|
|
|
|
|
|
|
|
|
|
if client.APIKey != "test-key" {
|
|
|
|
|
t.Errorf("NewClient() APIKey = %q, want %q", client.APIKey, "test-key")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if client.BaseURL != "https://api.x.ai/v1" {
|
|
|
|
|
t.Errorf("NewClient() BaseURL = %q, want %q", client.BaseURL, "https://api.x.ai/v1")
|
|
|
|
|
}
|
|
|
|
|
}
|