grokkit/internal/grok/client_test.go
Greg Gauthier e355142c05 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

73 lines
1.5 KiB
Go

package grok
import (
"os"
"testing"
)
func TestCleanCodeResponse(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{
name: "removes markdown fences",
input: "```go\nfunc main() {}\n```",
expected: "func main() {}",
},
{
name: "removes language tag",
input: "```python\nprint('hello')\n```",
expected: "print('hello')",
},
{
name: "handles no fences",
input: "func main() {}",
expected: "func main() {}",
},
{
name: "preserves internal blank lines",
input: "```\nline1\n\nline2\n```",
expected: "line1\n\nline2",
},
{
name: "trims whitespace",
input: " \n```\ncode\n```\n ",
expected: "code",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := CleanCodeResponse(tt.input)
if result != tt.expected {
t.Errorf("CleanCodeResponse() = %q, want %q", result, tt.expected)
}
})
}
}
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")
}
}