tests: add backend tests
This commit is contained in:
176
backend/internal/store/memory_test.go
Normal file
176
backend/internal/store/memory_test.go
Normal file
@@ -0,0 +1,176 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestInMemoryStore_SaveChat(t *testing.T) {
|
||||
store := NewInMemoryStore()
|
||||
|
||||
chat := &Chat{
|
||||
Title: "Test Chat",
|
||||
}
|
||||
|
||||
err := store.SaveChat(chat)
|
||||
require.NoError(t, err)
|
||||
assert.NotEqual(t, uuid.Nil, chat.ID)
|
||||
assert.False(t, chat.CreatedAt.IsZero())
|
||||
}
|
||||
|
||||
func TestInMemoryStore_GetChat(t *testing.T) {
|
||||
store := NewInMemoryStore()
|
||||
|
||||
// Create and save a chat
|
||||
chat := &Chat{
|
||||
Title: "Test Chat",
|
||||
}
|
||||
err := store.SaveChat(chat)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Retrieve the chat
|
||||
retrievedChat, err := store.GetChat(chat.ID)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, chat.ID, retrievedChat.ID)
|
||||
assert.Equal(t, chat.Title, retrievedChat.Title)
|
||||
}
|
||||
|
||||
func TestInMemoryStore_GetChat_NotFound(t *testing.T) {
|
||||
store := NewInMemoryStore()
|
||||
|
||||
_, err := store.GetChat(uuid.New())
|
||||
assert.ErrorIs(t, err, ErrChatNotFound)
|
||||
}
|
||||
|
||||
func TestInMemoryStore_DeleteChat(t *testing.T) {
|
||||
store := NewInMemoryStore()
|
||||
|
||||
// Create and save a chat
|
||||
chat := &Chat{
|
||||
Title: "Test Chat",
|
||||
}
|
||||
err := store.SaveChat(chat)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Delete the chat
|
||||
err = store.DeleteChat(chat.ID)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Try to retrieve the deleted chat
|
||||
_, err = store.GetChat(chat.ID)
|
||||
assert.ErrorIs(t, err, ErrChatNotFound)
|
||||
}
|
||||
|
||||
func TestInMemoryStore_DeleteChat_NotFound(t *testing.T) {
|
||||
store := NewInMemoryStore()
|
||||
|
||||
err := store.DeleteChat(uuid.New())
|
||||
assert.ErrorIs(t, err, ErrChatNotFound)
|
||||
}
|
||||
|
||||
func TestInMemoryStore_ListChats(t *testing.T) {
|
||||
store := NewInMemoryStore()
|
||||
|
||||
// Create and save multiple chats
|
||||
chat1 := &Chat{Title: "Chat 1"}
|
||||
chat2 := &Chat{Title: "Chat 2"}
|
||||
|
||||
err := store.SaveChat(chat1)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = store.SaveChat(chat2)
|
||||
require.NoError(t, err)
|
||||
|
||||
// List all chats
|
||||
chats, err := store.ListChats()
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, chats, 2)
|
||||
}
|
||||
|
||||
func TestInMemoryStore_SaveChatMessage(t *testing.T) {
|
||||
store := NewInMemoryStore()
|
||||
|
||||
// Create and save a chat
|
||||
chat := &Chat{Title: "Test Chat"}
|
||||
err := store.SaveChat(chat)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Create and save a message
|
||||
message := &Message{
|
||||
ChatID: chat.ID,
|
||||
Role: "user",
|
||||
Content: "Hello",
|
||||
}
|
||||
|
||||
err = store.SaveChatMessage(message)
|
||||
require.NoError(t, err)
|
||||
assert.NotEqual(t, uuid.Nil, message.ID)
|
||||
assert.False(t, message.CreatedAt.IsZero())
|
||||
}
|
||||
|
||||
func TestInMemoryStore_SaveChatMessage_InvalidChatID(t *testing.T) {
|
||||
store := NewInMemoryStore()
|
||||
|
||||
message := &Message{
|
||||
ChatID: uuid.Nil,
|
||||
Role: "user",
|
||||
Content: "Hello",
|
||||
}
|
||||
|
||||
err := store.SaveChatMessage(message)
|
||||
assert.ErrorIs(t, err, ErrNilChatID)
|
||||
}
|
||||
|
||||
func TestInMemoryStore_SaveChatMessage_ChatNotFound(t *testing.T) {
|
||||
store := NewInMemoryStore()
|
||||
|
||||
message := &Message{
|
||||
ChatID: uuid.New(),
|
||||
Role: "user",
|
||||
Content: "Hello",
|
||||
}
|
||||
|
||||
err := store.SaveChatMessage(message)
|
||||
assert.ErrorIs(t, err, ErrChatNotFound)
|
||||
}
|
||||
|
||||
func TestInMemoryStore_SaveSettings(t *testing.T) {
|
||||
store := NewInMemoryStore()
|
||||
|
||||
settings := &Settings{
|
||||
APIEndpoint: "http://example.com",
|
||||
ImageEditSelector: ".image-edit",
|
||||
ImageGenerationSelector: ".image-gen",
|
||||
TextGenerationSelector: ".text-gen",
|
||||
}
|
||||
|
||||
err := store.SaveSettings(settings)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestInMemoryStore_GetSettings(t *testing.T) {
|
||||
store := NewInMemoryStore()
|
||||
|
||||
// Get settings when none exist
|
||||
settings, err := store.GetSettings()
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, settings)
|
||||
|
||||
// Set some settings
|
||||
settings = &Settings{
|
||||
APIEndpoint: "http://example.com",
|
||||
ImageEditSelector: ".image-edit",
|
||||
ImageGenerationSelector: ".image-gen",
|
||||
TextGenerationSelector: ".text-gen",
|
||||
}
|
||||
err = store.SaveSettings(settings)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Get the settings
|
||||
settings, err = store.GetSettings()
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "http://example.com", settings.APIEndpoint)
|
||||
}
|
||||
252
backend/internal/store/storage_test.go
Normal file
252
backend/internal/store/storage_test.go
Normal file
@@ -0,0 +1,252 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestFileStore_NewFileStore(t *testing.T) {
|
||||
tempDir := t.TempDir()
|
||||
filePath := filepath.Join(tempDir, "test.json")
|
||||
|
||||
store, err := NewFileStore(filePath)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, store)
|
||||
assert.Equal(t, filePath, store.filePath)
|
||||
assert.Equal(t, filepath.Join(tempDir, "chats"), store.chatDir)
|
||||
}
|
||||
|
||||
func TestFileStore_SaveChat(t *testing.T) {
|
||||
tempDir := t.TempDir()
|
||||
filePath := filepath.Join(tempDir, "test.json")
|
||||
|
||||
store, err := NewFileStore(filePath)
|
||||
require.NoError(t, err)
|
||||
|
||||
chat := &Chat{
|
||||
Title: "Test Chat",
|
||||
}
|
||||
|
||||
err = store.SaveChat(chat)
|
||||
require.NoError(t, err)
|
||||
assert.NotEqual(t, uuid.Nil, chat.ID)
|
||||
assert.False(t, chat.CreatedAt.IsZero())
|
||||
|
||||
// Verify the file was created
|
||||
chatFile := filepath.Join(store.chatDir, chat.ID.String()+".json")
|
||||
_, err = os.Stat(chatFile)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestFileStore_GetChat(t *testing.T) {
|
||||
tempDir := t.TempDir()
|
||||
filePath := filepath.Join(tempDir, "test.json")
|
||||
|
||||
store, err := NewFileStore(filePath)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Create and save a chat
|
||||
chat := &Chat{
|
||||
Title: "Test Chat",
|
||||
}
|
||||
err = store.SaveChat(chat)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Retrieve the chat
|
||||
retrievedChat, err := store.GetChat(chat.ID)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, chat.ID, retrievedChat.ID)
|
||||
assert.Equal(t, chat.Title, retrievedChat.Title)
|
||||
}
|
||||
|
||||
func TestFileStore_GetChat_NotFound(t *testing.T) {
|
||||
tempDir := t.TempDir()
|
||||
filePath := filepath.Join(tempDir, "test.json")
|
||||
|
||||
store, err := NewFileStore(filePath)
|
||||
require.NoError(t, err)
|
||||
|
||||
_, err = store.GetChat(uuid.New())
|
||||
assert.ErrorIs(t, err, ErrChatNotFound)
|
||||
}
|
||||
|
||||
func TestFileStore_DeleteChat(t *testing.T) {
|
||||
tempDir := t.TempDir()
|
||||
filePath := filepath.Join(tempDir, "test.json")
|
||||
|
||||
store, err := NewFileStore(filePath)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Create and save a chat
|
||||
chat := &Chat{
|
||||
Title: "Test Chat",
|
||||
}
|
||||
err = store.SaveChat(chat)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Verify the file was created
|
||||
chatFile := filepath.Join(store.chatDir, chat.ID.String()+".json")
|
||||
_, err = os.Stat(chatFile)
|
||||
assert.NoError(t, err)
|
||||
|
||||
// Delete the chat
|
||||
err = store.DeleteChat(chat.ID)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Verify the file was deleted
|
||||
_, err = os.Stat(chatFile)
|
||||
assert.True(t, os.IsNotExist(err))
|
||||
}
|
||||
|
||||
func TestFileStore_DeleteChat_NotFound(t *testing.T) {
|
||||
tempDir := t.TempDir()
|
||||
filePath := filepath.Join(tempDir, "test.json")
|
||||
|
||||
store, err := NewFileStore(filePath)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = store.DeleteChat(uuid.New())
|
||||
assert.ErrorIs(t, err, ErrChatNotFound)
|
||||
}
|
||||
|
||||
func TestFileStore_ListChats(t *testing.T) {
|
||||
tempDir := t.TempDir()
|
||||
filePath := filepath.Join(tempDir, "test.json")
|
||||
|
||||
store, err := NewFileStore(filePath)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Create and save multiple chats
|
||||
chat1 := &Chat{Title: "Chat 1"}
|
||||
chat2 := &Chat{Title: "Chat 2"}
|
||||
|
||||
err = store.SaveChat(chat1)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = store.SaveChat(chat2)
|
||||
require.NoError(t, err)
|
||||
|
||||
// List all chats
|
||||
chats, err := store.ListChats()
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, chats, 2)
|
||||
}
|
||||
|
||||
func TestFileStore_SaveChatMessage(t *testing.T) {
|
||||
tempDir := t.TempDir()
|
||||
filePath := filepath.Join(tempDir, "test.json")
|
||||
|
||||
store, err := NewFileStore(filePath)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Create and save a chat
|
||||
chat := &Chat{Title: "Test Chat"}
|
||||
err = store.SaveChat(chat)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Create and save a message
|
||||
message := &Message{
|
||||
ChatID: chat.ID,
|
||||
Role: "user",
|
||||
Content: "Hello",
|
||||
}
|
||||
|
||||
err = store.SaveChatMessage(message)
|
||||
require.NoError(t, err)
|
||||
assert.NotEqual(t, uuid.Nil, message.ID)
|
||||
assert.False(t, message.CreatedAt.IsZero())
|
||||
|
||||
// Verify the chat file was updated
|
||||
chatFile := filepath.Join(store.chatDir, chat.ID.String()+".json")
|
||||
_, err = os.Stat(chatFile)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestFileStore_SaveChatMessage_InvalidChatID(t *testing.T) {
|
||||
tempDir := t.TempDir()
|
||||
filePath := filepath.Join(tempDir, "test.json")
|
||||
|
||||
store, err := NewFileStore(filePath)
|
||||
require.NoError(t, err)
|
||||
|
||||
message := &Message{
|
||||
ChatID: uuid.Nil,
|
||||
Role: "user",
|
||||
Content: "Hello",
|
||||
}
|
||||
|
||||
err = store.SaveChatMessage(message)
|
||||
assert.ErrorIs(t, err, ErrNilChatID)
|
||||
}
|
||||
|
||||
func TestFileStore_SaveChatMessage_ChatNotFound(t *testing.T) {
|
||||
tempDir := t.TempDir()
|
||||
filePath := filepath.Join(tempDir, "test.json")
|
||||
|
||||
store, err := NewFileStore(filePath)
|
||||
require.NoError(t, err)
|
||||
|
||||
message := &Message{
|
||||
ChatID: uuid.New(),
|
||||
Role: "user",
|
||||
Content: "Hello",
|
||||
}
|
||||
|
||||
err = store.SaveChatMessage(message)
|
||||
assert.ErrorIs(t, err, ErrChatNotFound)
|
||||
}
|
||||
|
||||
func TestFileStore_SaveSettings(t *testing.T) {
|
||||
tempDir := t.TempDir()
|
||||
filePath := filepath.Join(tempDir, "test.json")
|
||||
|
||||
store, err := NewFileStore(filePath)
|
||||
require.NoError(t, err)
|
||||
|
||||
settings := &Settings{
|
||||
APIEndpoint: "http://example.com",
|
||||
ImageEditSelector: ".image-edit",
|
||||
ImageGenerationSelector: ".image-gen",
|
||||
TextGenerationSelector: ".text-gen",
|
||||
}
|
||||
|
||||
err = store.SaveSettings(settings)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Verify the settings file was created
|
||||
_, err = os.Stat(filePath)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestFileStore_GetSettings(t *testing.T) {
|
||||
tempDir := t.TempDir()
|
||||
filePath := filepath.Join(tempDir, "test.json")
|
||||
|
||||
store, err := NewFileStore(filePath)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Get settings when none exist
|
||||
settings, err := store.GetSettings()
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, settings)
|
||||
|
||||
// Set some settings
|
||||
settings = &Settings{
|
||||
APIEndpoint: "http://example.com",
|
||||
ImageEditSelector: ".image-edit",
|
||||
ImageGenerationSelector: ".image-gen",
|
||||
TextGenerationSelector: ".text-gen",
|
||||
}
|
||||
err = store.SaveSettings(settings)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Get the settings
|
||||
settings, err = store.GetSettings()
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "http://example.com", settings.APIEndpoint)
|
||||
}
|
||||
Reference in New Issue
Block a user