orama/core/pkg/serverless/hostfuncs_test.go
anonpenguin23 ebc9d51167 feat(gateway): implement pubsub dispatcher and batch query support
- Integrate PubSubDispatcher to enable libp2p subscription for trigger patterns
- Add BatchQuery to rqlite client to reduce round-trips for multi-query operations
- Implement lifecycle management for dispatcher and add safety limits for batch queries
2026-05-17 16:27:05 +03:00

188 lines
5.2 KiB
Go

package serverless
import (
"context"
"testing"
"time"
"go.uber.org/zap"
)
func TestHostFunctions_Cache(t *testing.T) {
// Note: HostFunctions implementation has been moved to pkg/serverless/hostfunctions
// This test validates that the HostServices interface works correctly
db := NewMockRQLite()
ipfs := NewMockIPFSClient()
logger := zap.NewNop()
// Create a mock implementation that satisfies HostServices
var h HostServices = &mockHostServices{
db: db,
ipfs: ipfs,
logger: logger,
logs: make([]LogEntry, 0),
}
ctx := context.Background()
// Test Storage interface
cid, err := h.StoragePut(ctx, []byte("data"))
if err != nil {
t.Fatalf("StoragePut failed: %v", err)
}
data, err := h.StorageGet(ctx, cid)
if err != nil {
t.Fatalf("StorageGet failed: %v", err)
}
if string(data) != "data" {
t.Errorf("expected 'data', got %q", string(data))
}
}
// mockHostServices is a minimal mock for testing the HostServices interface
type mockHostServices struct {
db *MockRQLite
ipfs *MockIPFSClient
logger *zap.Logger
logs []LogEntry
}
func (m *mockHostServices) DBQuery(ctx context.Context, query string, args []interface{}) ([]byte, error) {
return nil, nil
}
func (m *mockHostServices) DBExecute(ctx context.Context, query string, args []interface{}) (int64, error) {
return 0, nil
}
func (m *mockHostServices) DBExecuteV2(ctx context.Context, query string, args []interface{}) ([]byte, error) {
return []byte(`{"rows_affected":0}`), nil
}
func (m *mockHostServices) DBQueryV2(ctx context.Context, query string, args []interface{}) ([]byte, error) {
return []byte(`{"rows":[]}`), nil
}
func (m *mockHostServices) DBQueryBatch(ctx context.Context, opsJSON []byte) ([]byte, error) {
return []byte(`{"results":[]}`), nil
}
func (m *mockHostServices) CacheGet(ctx context.Context, key string) ([]byte, error) {
return nil, nil
}
func (m *mockHostServices) CacheSet(ctx context.Context, key string, value []byte, ttlSeconds int64) error {
return nil
}
func (m *mockHostServices) CacheDelete(ctx context.Context, key string) error {
return nil
}
func (m *mockHostServices) CacheIncr(ctx context.Context, key string) (int64, error) {
return 0, nil
}
func (m *mockHostServices) CacheIncrBy(ctx context.Context, key string, delta int64) (int64, error) {
return 0, nil
}
func (m *mockHostServices) StoragePut(ctx context.Context, data []byte) (string, error) {
// Mock implementation - just return a fake CID
return "QmTest123", nil
}
func (m *mockHostServices) StorageGet(ctx context.Context, cid string) ([]byte, error) {
// Mock implementation - return the test data
return []byte("data"), nil
}
func (m *mockHostServices) PubSubPublish(ctx context.Context, topic string, data []byte) error {
return nil
}
func (m *mockHostServices) PubSubPublishBatch(ctx context.Context, msgsJSON []byte) error {
return nil
}
func (m *mockHostServices) PushSend(ctx context.Context, userID string, msgJSON []byte) error {
return nil
}
func (m *mockHostServices) DBTransaction(ctx context.Context, opsJSON []byte) ([]byte, error) {
return []byte(`{"committed":true,"results":[]}`), nil
}
func (m *mockHostServices) ExecAndPublish(ctx context.Context, opsJSON []byte, topic string, dataTemplate []byte) ([]byte, error) {
return []byte(`{"committed":true,"published":true,"seq":1,"results":[]}`), nil
}
func (m *mockHostServices) WSPubSubBridge(ctx context.Context, clientID, topic string) error {
return nil
}
func (m *mockHostServices) WSPubSubUnbridge(ctx context.Context, clientID, topic string) error {
return nil
}
func (m *mockHostServices) WSSend(ctx context.Context, clientID string, data []byte) error {
return nil
}
func (m *mockHostServices) WSBroadcast(ctx context.Context, topic string, data []byte) error {
return nil
}
func (m *mockHostServices) FunctionInvoke(ctx context.Context, name string, payload []byte) ([]byte, error) {
return nil, nil
}
func (m *mockHostServices) HTTPFetch(ctx context.Context, method, url string, headers map[string]string, body []byte) ([]byte, error) {
return nil, nil
}
func (m *mockHostServices) GetEnv(ctx context.Context, key string) (string, error) {
return "", nil
}
func (m *mockHostServices) GetSecret(ctx context.Context, name string) (string, error) {
return "", nil
}
func (m *mockHostServices) GetRequestID(ctx context.Context) string {
return ""
}
func (m *mockHostServices) GetCallerWallet(ctx context.Context) string {
return ""
}
func (m *mockHostServices) GetWSClientID(ctx context.Context) string {
return ""
}
func (m *mockHostServices) GetCallerClaim(ctx context.Context, name string) string {
return ""
}
func (m *mockHostServices) GetCallerJWTSubject(ctx context.Context) string {
return ""
}
func (m *mockHostServices) EnqueueBackground(ctx context.Context, functionName string, payload []byte) (string, error) {
return "", nil
}
func (m *mockHostServices) ScheduleOnce(ctx context.Context, functionName string, runAt time.Time, payload []byte) (string, error) {
return "", nil
}
func (m *mockHostServices) LogInfo(ctx context.Context, message string) {
m.logs = append(m.logs, LogEntry{Level: "info", Message: message})
}
func (m *mockHostServices) LogError(ctx context.Context, message string) {
m.logs = append(m.logs, LogEntry{Level: "error", Message: message})
}