package collector import ( "context" "testing" "time" "git.tornberg.me/go-otel/internal/websocket" ) type mockStore struct { entries []websocket.HistoryEntry } func (m *mockStore) Append(ctx context.Context, entry websocket.HistoryEntry) error { m.entries = append(m.entries, entry) return nil } func (m *mockStore) GetHistory(ctx context.Context, limit int) ([]websocket.HistoryEntry, error) { return m.entries, nil } func (m *mockStore) Search(ctx context.Context, query string, limit int) ([]websocket.HistoryEntry, error) { return m.entries, nil } func TestCollectorWithStore(t *testing.T) { store := &mockStore{} coll := New(nil, 10, store) coll.addToHistory(websocket.DataTypeLogs, map[string]interface{}{"msg": "test"}) // Give it a moment since it's asynchronous timeout := time.After(100 * time.Millisecond) for { select { case <-timeout: t.Fatal("timed out waiting for entry to be appended to store") default: if len(store.entries) > 0 { goto OK } time.Sleep(10 * time.Millisecond) } } OK: if store.entries[0].Type != "logs" { t.Errorf("expected type 'logs', got %s", store.entries[0].Type) } } func TestCollectorHistoryLimit(t *testing.T) { coll := New(nil, 10, nil) for i := 0; i < 10; i++ { coll.addToHistory(websocket.DataTypeLogs, map[string]interface{}{"msg": i}) } history := coll.GetHistory(10) if len(history) != 10 { t.Errorf("expected 10 entries, got %d", len(history)) } history = coll.GetHistory(5) if len(history) != 5 { t.Errorf("expected 5 entries, got %d", len(history)) } // Should default to historySize (10) history = coll.GetHistory(0) if len(history) != 10 { t.Errorf("expected default limit to be historySize (10), got %d", len(history)) } // Should not crash on large limit, just return what it has history = coll.GetHistory(100) if len(history) != 10 { t.Errorf("expected 10 entries even with limit 100, got %d", len(history)) } }