orama/core/pkg/serverless/hostfuncs_test.go
anonpenguin23 9373c2ad92 feat(rqlite,serverless): add local read consistency and async invocation
- Introduce `BatchQueryConsistency` with `ReadConsistencyNone` to allow
  local SQLite reads, bypassing leader round-trips for performance.
- Add `function_invoke_async` host function to support non-blocking
  fire-and-forget function execution.
2026-06-01 19:59:30 +03:00

204 lines
5.8 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) PushSendV2(ctx context.Context, userID string, msgJSON []byte) ([]byte, error) {
return []byte(`{"ok":true,"devices_attempted":0,"devices_succeeded":0,"results":[]}`), nil
}
func (m *mockHostServices) TurnCredentials(ctx context.Context) ([]byte, error) {
return []byte(`{"configured":false}`), 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) FunctionInvokeAsync(ctx context.Context, name string, payload []byte) error {
return 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) AnyoneFetch(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})
}