2026-03-01 12:44:20 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestGetChatHistoryFile(t *testing.T) {
|
|
|
|
|
// Save original HOME
|
|
|
|
|
oldHome := os.Getenv("HOME")
|
2026-03-02 21:33:11 +00:00
|
|
|
defer func() { _ = os.Setenv("HOME", oldHome) }()
|
2026-03-01 12:44:20 +00:00
|
|
|
|
|
|
|
|
tmpDir := t.TempDir()
|
2026-03-02 21:33:11 +00:00
|
|
|
if err := os.Setenv("HOME", tmpDir); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2026-03-01 12:44:20 +00:00
|
|
|
|
|
|
|
|
histFile := getChatHistoryFile()
|
|
|
|
|
expected := filepath.Join(tmpDir, ".config", "grokkit", "chat_history.json")
|
|
|
|
|
|
|
|
|
|
if histFile != expected {
|
|
|
|
|
t.Errorf("getChatHistoryFile() = %q, want %q", histFile, expected)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-02 21:33:11 +00:00
|
|
|
func setHomeForTest(t *testing.T, dir string) {
|
|
|
|
|
t.Helper()
|
|
|
|
|
if err := os.Setenv("HOME", dir); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-01 12:44:20 +00:00
|
|
|
func TestLoadChatHistory_NoFile(t *testing.T) {
|
|
|
|
|
tmpDir := t.TempDir()
|
|
|
|
|
oldHome := os.Getenv("HOME")
|
2026-03-02 21:33:11 +00:00
|
|
|
setHomeForTest(t, tmpDir)
|
|
|
|
|
defer func() { _ = os.Setenv("HOME", oldHome) }()
|
2026-03-01 12:44:20 +00:00
|
|
|
|
|
|
|
|
history := loadChatHistory()
|
|
|
|
|
if history != nil {
|
|
|
|
|
t.Errorf("loadChatHistory() expected nil for non-existent file, got %v", history)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestSaveAndLoadChatHistory(t *testing.T) {
|
|
|
|
|
tmpDir := t.TempDir()
|
|
|
|
|
oldHome := os.Getenv("HOME")
|
2026-03-02 21:33:11 +00:00
|
|
|
setHomeForTest(t, tmpDir)
|
|
|
|
|
defer func() { _ = os.Setenv("HOME", oldHome) }()
|
2026-03-01 12:44:20 +00:00
|
|
|
|
|
|
|
|
// Create test messages
|
|
|
|
|
messages := []map[string]string{
|
|
|
|
|
{"role": "system", "content": "test system message"},
|
|
|
|
|
{"role": "user", "content": "hello"},
|
|
|
|
|
{"role": "assistant", "content": "hi there"},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Save
|
|
|
|
|
err := saveChatHistory(messages)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("saveChatHistory() error: %v", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Load
|
|
|
|
|
loaded := loadChatHistory()
|
|
|
|
|
if loaded == nil {
|
|
|
|
|
t.Fatal("loadChatHistory() returned nil")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(loaded) != len(messages) {
|
|
|
|
|
t.Errorf("loadChatHistory() length = %d, want %d", len(loaded), len(messages))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Verify content
|
|
|
|
|
for i, msg := range loaded {
|
|
|
|
|
if msg["role"] != messages[i]["role"] {
|
|
|
|
|
t.Errorf("Message[%d] role = %q, want %q", i, msg["role"], messages[i]["role"])
|
|
|
|
|
}
|
|
|
|
|
if msg["content"] != messages[i]["content"] {
|
|
|
|
|
t.Errorf("Message[%d] content = %q, want %q", i, msg["content"], messages[i]["content"])
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestLoadChatHistory_InvalidJSON(t *testing.T) {
|
|
|
|
|
tmpDir := t.TempDir()
|
|
|
|
|
oldHome := os.Getenv("HOME")
|
2026-03-02 21:33:11 +00:00
|
|
|
setHomeForTest(t, tmpDir)
|
|
|
|
|
defer func() { _ = os.Setenv("HOME", oldHome) }()
|
2026-03-01 12:44:20 +00:00
|
|
|
|
|
|
|
|
// Create invalid JSON file
|
|
|
|
|
histDir := filepath.Join(tmpDir, ".config", "grokkit")
|
2026-03-01 14:10:24 +00:00
|
|
|
if err := os.MkdirAll(histDir, 0755); err != nil {
|
|
|
|
|
t.Fatalf("MkdirAll() error: %v", err)
|
|
|
|
|
}
|
2026-03-01 12:44:20 +00:00
|
|
|
histFile := filepath.Join(histDir, "chat_history.json")
|
2026-03-01 14:10:24 +00:00
|
|
|
if err := os.WriteFile(histFile, []byte("invalid json{{{"), 0644); err != nil {
|
|
|
|
|
t.Fatalf("WriteFile() error: %v", err)
|
|
|
|
|
}
|
2026-03-01 12:44:20 +00:00
|
|
|
|
|
|
|
|
history := loadChatHistory()
|
|
|
|
|
if history != nil {
|
|
|
|
|
t.Errorf("loadChatHistory() expected nil for invalid JSON, got %v", history)
|
|
|
|
|
}
|
|
|
|
|
}
|