Files
go-otel/internal/storage/file_store_test.go
T
mats 0ed1cb4355
Build and Publish / Metadata (push) Has been cancelled
Build and Publish / BuildAndDeployAmd64 (push) Has been cancelled
some refinement
2026-04-07 12:44:55 +02:00

67 lines
1.5 KiB
Go

package storage
import (
"context"
"encoding/json"
"os"
"testing"
"git.tornberg.me/go-otel/internal/websocket"
)
func TestFileStore(t *testing.T) {
filename := "test-otel.log"
defer func() {
_ = os.Remove(filename)
}()
fs := NewFileStore(filename)
ctx := context.Background()
entry1 := websocket.HistoryEntry{
Type: "logs",
Timestamp: 100,
Data: json.RawMessage(`{"msg":"hello world"}`),
}
entry2 := websocket.HistoryEntry{
Type: "traces",
Timestamp: 200,
Data: json.RawMessage(`{"msg":"trace event"}`),
}
if err := fs.Append(ctx, entry1); err != nil {
t.Fatalf("failed to append entry1: %v", err)
}
if err := fs.Append(ctx, entry2); err != nil {
t.Fatalf("failed to append entry2: %v", err)
}
// Test GetHistory (limit 1)
history, err := fs.GetHistory(ctx, 1)
if err != nil {
t.Fatalf("GetHistory failed: %v", err)
}
if len(history) != 1 {
t.Errorf("expected 1 entry, got %d", len(history))
}
// Should be latest entry
if history[0].Type != "traces" {
t.Errorf("expected latest entry type 'traces', got %s", history[0].Type)
}
// Test Search
results, err := fs.Search(ctx, "hello", 10)
if err != nil {
t.Fatalf("Search failed: %v", err)
}
if len(results) != 1 {
t.Errorf("expected 1 result for search 'hello', got %d", len(results))
}
// Check content of result
data, _ := json.Marshal(results[0].Data)
if string(data) != `{"msg":"hello world"}` {
t.Errorf("unexpected data content: %s", string(data))
}
}