69 lines
1.8 KiB
Go
69 lines
1.8 KiB
Go
package service
|
|
|
|
import (
|
|
"fmt"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/seifghazi/claude-code-monitor/internal/config"
|
|
"github.com/seifghazi/claude-code-monitor/internal/model"
|
|
)
|
|
|
|
func TestSQLiteStorageServiceGetRequestsUsesSQLPaginationAndFiltering(t *testing.T) {
|
|
dbPath := filepath.Join(t.TempDir(), "requests.db")
|
|
storage, err := NewSQLiteStorageService(&config.StorageConfig{DBPath: dbPath})
|
|
if err != nil {
|
|
t.Fatalf("NewSQLiteStorageService() error = %v", err)
|
|
}
|
|
|
|
sqliteStorage, ok := storage.(*sqliteStorageService)
|
|
if !ok {
|
|
t.Fatalf("unexpected storage type %T", storage)
|
|
}
|
|
defer sqliteStorage.Close()
|
|
|
|
requests := []struct {
|
|
id string
|
|
model string
|
|
}{
|
|
{id: "1", model: "claude-3-5-sonnet"},
|
|
{id: "2", model: "gpt-4o"},
|
|
{id: "3", model: "claude-3-5-sonnet"},
|
|
{id: "4", model: "gpt-4o-mini"},
|
|
}
|
|
|
|
for i, req := range requests {
|
|
_, err := storage.SaveRequest(&model.RequestLog{
|
|
RequestID: req.id,
|
|
Timestamp: time.Date(2026, 3, 19, 12, 0, i, 0, time.UTC).Format(time.RFC3339),
|
|
Method: "POST",
|
|
Endpoint: "/v1/messages",
|
|
Headers: map[string][]string{"Content-Type": {"application/json"}},
|
|
Body: map[string]string{"request": fmt.Sprintf("body-%d", i)},
|
|
Model: req.model,
|
|
UserAgent: "test",
|
|
ContentType: "application/json",
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("SaveRequest() error = %v", err)
|
|
}
|
|
}
|
|
|
|
got, total, err := storage.GetRequests(1, 1, "gpt")
|
|
if err != nil {
|
|
t.Fatalf("GetRequests() error = %v", err)
|
|
}
|
|
|
|
if total != 2 {
|
|
t.Fatalf("expected filtered total 2, got %d", total)
|
|
}
|
|
|
|
if len(got) != 1 {
|
|
t.Fatalf("expected 1 paginated result, got %d", len(got))
|
|
}
|
|
|
|
if got[0].RequestID != "4" {
|
|
t.Fatalf("expected newest filtered request ID 4, got %s", got[0].RequestID)
|
|
}
|
|
}
|