Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

28.3 KB
Raw Blame
use async_trait::async_trait;
use std::collections::HashMap;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
use std::time::Duration;

use crate::pipeline::{PipelineCommand, PipelineResult};
use crate::{Client, CustomRedisError, RedisValueFormat};

#[derive(Clone)]
pub struct MockRedisClient {
    zrangebyscore_ret: HashMap<String, Vec<String>>,
    hincrby_ret: HashMap<String, Result<(), CustomRedisError>>,
    get_ret: HashMap<String, Result<String, CustomRedisError>>,
    get_raw_bytes_ret: HashMap<String, Result<Vec<u8>, CustomRedisError>>,
    set_ret: HashMap<String, Result<(), CustomRedisError>>,
    set_nx_ex_ret: HashMap<String, Result<bool, CustomRedisError>>,
    batch_incr_by_expire_nx_ret: Option<Result<(), CustomRedisError>>,
    batch_incr_by_expire_ret: Option<Result<(), CustomRedisError>>,
    batch_incr_by_expire_at_ret: Option<Result<(), CustomRedisError>>,
    del_ret: HashMap<String, Result<(), CustomRedisError>>,
    hget_ret: HashMap<String, Result<String, CustomRedisError>>,
    scard_ret: HashMap<String, Result<u64, CustomRedisError>>,
    sadd_ret: HashMap<String, Result<(), CustomRedisError>>,
    expire_ret: HashMap<String, Result<bool, CustomRedisError>>,
    mget_ret: HashMap<String, Option<Vec<u8>>>,
    mget_error: Option<CustomRedisError>,
    pipeline_error: Option<CustomRedisError>,
    pipeline_block: Option<Duration>,
    batch_incr_by_expire_at_block: Option<Duration>,
    pipeline_errors_by_call: HashMap<usize, CustomRedisError>,
    /// Shared via `Arc` so clones of the mock agree on call sequencing.
    pipeline_call_counter: Arc<AtomicUsize>,
    mget_errors_by_call: HashMap<usize, CustomRedisError>,
    /// Shared via `Arc` so clones of the mock agree on call sequencing.
    mget_call_counter: Arc<AtomicUsize>,
    calls: Arc<Mutex<Vec<MockRedisCall>>>,
}

impl Default for MockRedisClient {
    fn default() -> Self {
        Self {
            zrangebyscore_ret: HashMap::new(),
            hincrby_ret: HashMap::new(),
            get_ret: HashMap::new(),
            get_raw_bytes_ret: HashMap::new(),
            set_ret: HashMap::new(),
            set_nx_ex_ret: HashMap::new(),
            batch_incr_by_expire_nx_ret: None,
            batch_incr_by_expire_ret: None,
            batch_incr_by_expire_at_ret: None,
            del_ret: HashMap::new(),
            hget_ret: HashMap::new(),
            scard_ret: HashMap::new(),
            sadd_ret: HashMap::new(),
            expire_ret: HashMap::new(),
            mget_ret: HashMap::new(),
            mget_error: None,
            pipeline_error: None,
            pipeline_block: None,
            batch_incr_by_expire_at_block: None,
            pipeline_errors_by_call: HashMap::new(),
            pipeline_call_counter: Arc::new(AtomicUsize::new(0)),
            mget_errors_by_call: HashMap::new(),
            mget_call_counter: Arc::new(AtomicUsize::new(0)),
            calls: Arc::new(Mutex::new(Vec::new())),
        }
    }
}

impl MockRedisClient {
    pub fn new() -> Self {
        Self::default()
    }

    // Helper method to safely lock the calls mutex
    fn lock_calls(&self) -> std::sync::MutexGuard<'_, Vec<MockRedisCall>> {
        match self.calls.lock() {
            Ok(guard) => guard,
            Err(poisoned) => poisoned.into_inner(),
        }
    }

    /// Record a call to the mock client for later inspection.
    fn record_call(&self, op: &str, key: impl Into<String>, value: MockRedisValue) {
        self.lock_calls().push(MockRedisCall {
            op: op.to_string(),
            key: key.into(),
            value,
        });
    }

    /// Lookup a result from a HashMap, returning NotFound if not configured.
    fn lookup_or_not_found<T: Clone>(
        map: &HashMap<String, Result<T, CustomRedisError>>,
        key: &str,
    ) -> Result<T, CustomRedisError> {
        map.get(key)
            .cloned()
            .unwrap_or(Err(CustomRedisError::NotFound))
    }

    /// Lookup a result from a HashMap, returning Ok(()) if not configured.
    fn lookup_or_ok(
        map: &HashMap<String, Result<(), CustomRedisError>>,
        key: &str,
    ) -> Result<(), CustomRedisError> {
        map.get(key).cloned().unwrap_or(Ok(()))
    }

    pub fn zrangebyscore_ret(&mut self, key: &str, ret: Vec<String>) -> Self {
        self.zrangebyscore_ret.insert(key.to_owned(), ret);
        self.clone()
    }

    pub fn hincrby_ret(&mut self, key: &str, ret: Result<(), CustomRedisError>) -> Self {
        self.hincrby_ret.insert(key.to_owned(), ret);

        self.clone()
    }

    pub fn get_ret(&mut self, key: &str, ret: Result<String, CustomRedisError>) -> Self {
        self.get_ret.insert(key.to_owned(), ret);
        self.clone()
    }

    pub fn get_raw_bytes_ret(&mut self, key: &str, ret: Result<Vec<u8>, CustomRedisError>) -> Self {
        self.get_raw_bytes_ret.insert(key.to_owned(), ret);
        self.clone()
    }

    pub fn set_ret(&mut self, key: &str, ret: Result<(), CustomRedisError>) -> Self {
        self.set_ret.insert(key.to_owned(), ret);
        self.clone()
    }

    pub fn del_ret(&mut self, key: &str, ret: Result<(), CustomRedisError>) -> Self {
        self.del_ret.insert(key.to_owned(), ret);
        self.clone()
    }

    pub fn hget_ret(&mut self, key: &str, ret: Result<String, CustomRedisError>) -> Self {
        self.hget_ret.insert(key.to_owned(), ret);
        self.clone()
    }

    pub fn scard_ret(&mut self, key: &str, ret: Result<u64, CustomRedisError>) -> Self {
        self.scard_ret.insert(key.to_owned(), ret);
        self.clone()
    }

    pub fn sadd_ret(&mut self, key: &str, ret: Result<(), CustomRedisError>) -> Self {
        self.sadd_ret.insert(key.to_owned(), ret);
        self.clone()
    }

    pub fn expire_ret(&mut self, key: &str, ret: Result<bool, CustomRedisError>) -> Self {
        self.expire_ret.insert(key.to_owned(), ret);
        self.clone()
    }

    pub fn get_calls(&self) -> Vec<MockRedisCall> {
        self.lock_calls().clone()
    }

    pub fn set_nx_ex_ret(&mut self, key: &str, ret: Result<bool, CustomRedisError>) -> Self {
        self.set_nx_ex_ret.insert(key.to_owned(), ret);
        self.clone()
    }

    pub fn batch_incr_by_expire_nx_ret(&mut self, ret: Result<(), CustomRedisError>) -> Self {
        self.batch_incr_by_expire_nx_ret = Some(ret);
        self.clone()
    }

    pub fn batch_incr_by_expire_ret(&mut self, ret: Result<(), CustomRedisError>) -> Self {
        self.batch_incr_by_expire_ret = Some(ret);
        self.clone()
    }

    pub fn batch_incr_by_expire_at_ret(&mut self, ret: Result<(), CustomRedisError>) -> Self {
        self.batch_incr_by_expire_at_ret = Some(ret);
        self.clone()
    }

    pub fn mget_ret(&mut self, key: &str, ret: Option<Vec<u8>>) -> Self {
        self.mget_ret.insert(key.to_owned(), ret);
        self.clone()
    }

    pub fn mget_error(&mut self, err: CustomRedisError) -> Self {
        self.mget_error = Some(err);
        self.clone()
    }

    /// Make every `execute_pipeline` call return the given connection-level
    /// error. Commands are still recorded before the error is returned, so
    /// tests can assert which chunks were attempted.
    pub fn pipeline_error(&mut self, err: CustomRedisError) -> Self {
        self.pipeline_error = Some(err);
        self.clone()
    }

    /// Block every `execute_pipeline` call for the given duration before
    /// resolving. Used to exercise shutdown-timeout paths.
    pub fn pipeline_block(&mut self, duration: Duration) -> Self {
        self.pipeline_block = Some(duration);
        self.clone()
    }

    /// Block every `batch_incr_by_expire_at` call for `duration` after recording it.
    pub fn batch_incr_by_expire_at_block(&mut self, duration: Duration) -> Self {
        self.batch_incr_by_expire_at_block = Some(duration);
        self.clone()
    }

    /// Fail the Nth `execute_pipeline` call (0-indexed across the lifetime of
    /// this mock) with the given error. Other calls proceed normally. Takes
    /// precedence over `pipeline_error`. Use to exercise partial-failure
    /// flush paths where some chunks succeed and others don't.
    pub fn pipeline_error_at_call(&mut self, call_index: usize, err: CustomRedisError) -> Self {
        self.pipeline_errors_by_call.insert(call_index, err);
        self.clone()
    }

    /// Fail the Nth `mget` call (0-indexed) with `err`; takes precedence over `mget_error`.
    pub fn mget_error_at_call(&mut self, call_index: usize, err: CustomRedisError) -> Self {
        self.mget_errors_by_call.insert(call_index, err);
        self.clone()
    }
}

#[derive(Debug, Clone)]
pub enum MockRedisValue {
    None,
    Error(CustomRedisError),
    String(String),
    StringWithTTL(String, u64),
    VecString(Vec<String>),
    I64(i64),
    MinMax(String, String),
    StringWithFormat(String, RedisValueFormat),
    StringWithTTLAndFormat(String, u64, RedisValueFormat),
    Bytes(Vec<u8>, Option<u64>),
    MemberScore(String, i64),
}

#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct MockRedisCall {
    pub op: String,
    pub key: String,
    pub value: MockRedisValue,
}

#[async_trait]
impl Client for MockRedisClient {
    async fn heal(&self) {
        self.record_call("heal", "", MockRedisValue::None);
    }

    async fn zrangebyscore(
        &self,
        key: String,
        min: String,
        max: String,
    ) -> Result<Vec<String>, CustomRedisError> {
        let mut calls = self.lock_calls();
        calls.push(MockRedisCall {
            op: "zrangebyscore".to_string(),
            key: key.clone(),
            value: MockRedisValue::MinMax(min, max),
        });

        match self.zrangebyscore_ret.get(&key) {
            Some(val) => Ok(val.clone()),
            None => Err(CustomRedisError::NotFound),
        }
    }

    /// Pages over the same `zrangebyscore_ret` list, so a test can check how a
    /// caller walks a set.
    async fn zrangebyscore_limit(
        &self,
        key: String,
        min: String,
        max: String,
        offset: isize,
        count: isize,
    ) -> Result<Vec<String>, CustomRedisError> {
        let mut calls = self.lock_calls();
        calls.push(MockRedisCall {
            op: "zrangebyscore_limit".to_string(),
            key: key.clone(),
            value: MockRedisValue::MinMax(min, max),
        });

        let Some(all) = self.zrangebyscore_ret.get(&key) else {
            return Err(CustomRedisError::NotFound);
        };
        let start = usize::try_from(offset).unwrap_or(0).min(all.len());
        let end = if count < 0 {
            all.len()
        } else {
            start.saturating_add(count as usize).min(all.len())
        };
        Ok(all[start..end].to_vec())
    }

    async fn zadd(&self, key: String, member: String, score: i64) -> Result<(), CustomRedisError> {
        let mut calls = self.lock_calls();
        calls.push(MockRedisCall {
            op: "zadd".to_string(),
            key,
            value: MockRedisValue::MemberScore(member, score),
        });
        Ok(())
    }

    async fn zadd_nx(
        &self,
        key: String,
        member: String,
        score: i64,
    ) -> Result<(), CustomRedisError> {
        self.record_call("zadd_nx", key, MockRedisValue::MemberScore(member, score));
        Ok(())
    }

    async fn zrem(&self, key: String, member: String) -> Result<(), CustomRedisError> {
        self.record_call("zrem", key, MockRedisValue::String(member));
        Ok(())
    }

    async fn hincrby(
        &self,
        key: String,
        field: String,
        count: i64,
    ) -> Result<(), CustomRedisError> {
        let mut calls = self.lock_calls();
        calls.push(MockRedisCall {
            op: "hincrby".to_string(),
            key: format!("{key}:{field}"),
            value: MockRedisValue::I64(count),
        });

        match self.hincrby_ret.get(&key) {
            Some(result) => result.clone(),
            None => Err(CustomRedisError::NotFound),
        }
    }

    async fn get(&self, key: String) -> Result<String, CustomRedisError> {
        let mut calls = self.lock_calls();
        calls.push(MockRedisCall {
            op: "get".to_string(),
            key: key.clone(),
            value: MockRedisValue::None,
        });

        match self.get_ret.get(&key) {
            Some(result) => result.clone(),
            None => Err(CustomRedisError::NotFound),
        }
    }

    async fn get_with_format(
        &self,
        key: String,
        format: RedisValueFormat,
    ) -> Result<String, CustomRedisError> {
        self.lock_calls().push(MockRedisCall {
            op: "get_with_format".to_string(),
            key: key.clone(),
            value: MockRedisValue::StringWithFormat("".to_string(), format),
        });

        self.get_ret
            .get(&key)
            .cloned()
            .unwrap_or(Err(CustomRedisError::NotFound))
    }

    async fn get_raw_bytes(&self, key: String) -> Result<Vec<u8>, CustomRedisError> {
        self.lock_calls().push(MockRedisCall {
            op: "get_raw_bytes".to_string(),
            key: key.clone(),
            value: MockRedisValue::String("".to_string()),
        });

        // First try the dedicated raw bytes storage
        if let Some(result) = self.get_raw_bytes_ret.get(&key) {
            return result.clone();
        }

        // Fall back to string conversion for backward compatibility
        match self
            .get_ret
            .get(&key)
            .cloned()
            .unwrap_or(Err(CustomRedisError::NotFound))
        {
            Ok(string_data) => Ok(string_data.into_bytes()),
            Err(e) => Err(e),
        }
    }

    async fn set_bytes(
        &self,
        key: String,
        value: Vec<u8>,
        ttl_seconds: Option<u64>,
    ) -> Result<(), CustomRedisError> {
        self.lock_calls().push(MockRedisCall {
            op: "set_bytes".to_string(),
            key: key.clone(),
            value: MockRedisValue::Bytes(value, ttl_seconds),
        });

        self.set_ret.get(&key).cloned().unwrap_or(Ok(()))
    }

    async fn set(&self, key: String, value: String) -> Result<(), CustomRedisError> {
        let mut calls = self.lock_calls();
        calls.push(MockRedisCall {
            op: "set".to_string(),
            key: key.clone(),
            value: MockRedisValue::String(value.clone()),
        });

        match self.set_ret.get(&key) {
            Some(result) => result.clone(),
            None => Err(CustomRedisError::NotFound),
        }
    }

    async fn set_with_format(
        &self,
        key: String,
        value: String,
        format: RedisValueFormat,
    ) -> Result<(), CustomRedisError> {
        self.lock_calls().push(MockRedisCall {
            op: "set_with_format".to_string(),
            key: key.clone(),
            value: MockRedisValue::StringWithFormat(value.clone(), format),
        });

        self.set_ret.get(&key).cloned().unwrap_or(Ok(()))
    }

    async fn setex(
        &self,
        key: String,
        value: String,
        seconds: u64,
    ) -> Result<(), CustomRedisError> {
        self.lock_calls().push(MockRedisCall {
            op: "setex".to_string(),
            key: key.clone(),
            value: MockRedisValue::StringWithTTL(value.clone(), seconds),
        });

        self.set_ret.get(&key).cloned().unwrap_or(Ok(()))
    }

    async fn setex_with_format(
        &self,
        key: String,
        value: String,
        seconds: u64,
        format: RedisValueFormat,
    ) -> Result<(), CustomRedisError> {
        self.lock_calls().push(MockRedisCall {
            op: "setex_with_format".to_string(),
            key: key.clone(),
            value: MockRedisValue::StringWithTTLAndFormat(value.clone(), seconds, format),
        });

        self.set_ret.get(&key).cloned().unwrap_or(Ok(()))
    }

    async fn set_nx_ex(
        &self,
        key: String,
        value: String,
        seconds: u64,
    ) -> Result<bool, CustomRedisError> {
        let mut calls = self.lock_calls();
        calls.push(MockRedisCall {
            op: "set_nx_ex".to_string(),
            key: key.clone(),
            value: MockRedisValue::StringWithTTL(value.clone(), seconds),
        });

        match self.set_nx_ex_ret.get(&key) {
            Some(result) => result.clone(),
            None => Err(CustomRedisError::NotFound),
        }
    }

    async fn set_nx_ex_with_format(
        &self,
        key: String,
        value: String,
        seconds: u64,
        format: RedisValueFormat,
    ) -> Result<bool, CustomRedisError> {
        self.lock_calls().push(MockRedisCall {
            op: "set_nx_ex_with_format".to_string(),
            key: key.clone(),
            value: MockRedisValue::StringWithTTLAndFormat(value.clone(), seconds, format),
        });

        self.set_nx_ex_ret
            .get(&key)
            .cloned()
            .unwrap_or(Err(CustomRedisError::NotFound))
    }

    async fn batch_incr_by_expire_nx(
        &self,
        items: Vec<(String, i64)>,
        ttl_seconds: usize,
    ) -> Result<(), CustomRedisError> {
        self.lock_calls().push(MockRedisCall {
            op: "batch_incr_by_expire_nx".to_string(),
            key: format!("items={};ttl={}", items.len(), ttl_seconds),
            value: MockRedisValue::None,
        });

        match &self.batch_incr_by_expire_nx_ret {
            Some(ret) => ret.clone(),
            None => Ok(()),
        }
    }

    async fn batch_incr_by_expire(
        &self,
        items: Vec<(String, i64)>,
        ttl_seconds: usize,
    ) -> Result<(), CustomRedisError> {
        self.lock_calls().push(MockRedisCall {
            op: "batch_incr_by_expire".to_string(),
            key: format!("items={};ttl={}", items.len(), ttl_seconds),
            value: MockRedisValue::None,
        });

        match &self.batch_incr_by_expire_ret {
            Some(ret) => ret.clone(),
            None => Ok(()),
        }
    }

    async fn batch_incr_by_expire_at(
        &self,
        items: Vec<(String, i64, i64)>,
    ) -> Result<(), CustomRedisError> {
        // Record each key's deadline so a test can assert the epoch math.
        let recorded = items
            .iter()
            .map(|(k, by, expire_at)| format!("{k}={by}@{expire_at}"))
            .collect::<Vec<_>>()
            .join(",");
        self.lock_calls().push(MockRedisCall {
            op: "batch_incr_by_expire_at".to_string(),
            key: recorded,
            value: MockRedisValue::None,
        });
        if let Some(duration) = self.batch_incr_by_expire_at_block {
            futures_timer::Delay::new(duration).await;
        }

        match &self.batch_incr_by_expire_at_ret {
            Some(ret) => ret.clone(),
            None => Ok(()),
        }
    }

    async fn del(&self, key: String) -> Result<(), CustomRedisError> {
        let mut calls = self.lock_calls();
        calls.push(MockRedisCall {
            op: "del".to_string(),
            key: key.clone(),
            value: MockRedisValue::None,
        });

        match self.del_ret.get(&key) {
            Some(result) => result.clone(),
            None => Err(CustomRedisError::NotFound),
        }
    }

    async fn hget(&self, key: String, field: String) -> Result<String, CustomRedisError> {
        let mut calls = self.lock_calls();
        calls.push(MockRedisCall {
            op: "hget".to_string(),
            key: format!("{key}:{field}"),
            value: MockRedisValue::None,
        });

        match self.hget_ret.get(&key) {
            Some(result) => result.clone(),
            None => Err(CustomRedisError::NotFound),
        }
    }

    async fn scard(&self, key: String) -> Result<u64, CustomRedisError> {
        let mut calls = self.lock_calls();
        calls.push(MockRedisCall {
            op: "scard".to_string(),
            key: key.to_string(),
            value: MockRedisValue::None,
        });

        match self.scard_ret.get(&key) {
            Some(result) => result.clone(),
            None => Err(CustomRedisError::NotFound),
        }
    }

    async fn mget(&self, keys: Vec<String>) -> Result<Vec<Option<Vec<u8>>>, CustomRedisError> {
        self.lock_calls().push(MockRedisCall {
            op: "mget".to_string(),
            key: format!("keys={}", keys.len()),
            value: MockRedisValue::VecString(keys.clone()),
        });

        let call_index = self.mget_call_counter.fetch_add(1, Ordering::SeqCst);
        if let Some(err) = self.mget_errors_by_call.get(&call_index) {
            return Err(err.clone());
        }
        if let Some(err) = &self.mget_error {
            return Err(err.clone());
        }

        let results: Vec<Option<Vec<u8>>> = keys
            .iter()
            .map(|k| self.mget_ret.get(k).and_then(|v| v.clone()))
            .collect();
        Ok(results)
    }

    async fn scard_multiple(&self, keys: Vec<String>) -> Result<Vec<u64>, CustomRedisError> {
        self.lock_calls().push(MockRedisCall {
            op: "scard_multiple".to_string(),
            key: format!("keys={}", keys.len()),
            value: MockRedisValue::VecString(keys.clone()),
        });

        let results: Vec<u64> = keys
            .iter()
            .map(|k| {
                self.scard_ret
                    .get(k)
                    .and_then(|r| r.clone().ok())
                    .unwrap_or(0)
            })
            .collect();
        Ok(results)
    }

    async fn batch_sadd_expire(
        &self,
        items: Vec<(String, String)>,
        _ttl_seconds: usize,
    ) -> Result<(), CustomRedisError> {
        self.lock_calls().push(MockRedisCall {
            op: "batch_sadd_expire".to_string(),
            key: format!("items={}", items.len()),
            value: MockRedisValue::None,
        });
        Ok(())
    }

    async fn batch_set_nx_ex(
        &self,
        items: Vec<(String, String, usize)>,
    ) -> Result<Vec<bool>, CustomRedisError> {
        let keys: Vec<String> = items.iter().map(|(k, _, _)| k.clone()).collect();
        self.lock_calls().push(MockRedisCall {
            op: "batch_set_nx_ex".to_string(),
            key: format!("items={}", items.len()),
            value: MockRedisValue::VecString(keys.clone()),
        });
        Ok(keys
            .iter()
            .map(|k| {
                self.set_nx_ex_ret
                    .get(k)
                    .and_then(|r| r.clone().ok())
                    .unwrap_or(false)
            })
            .collect())
    }

    async fn batch_del(&self, keys: Vec<String>) -> Result<(), CustomRedisError> {
        self.lock_calls().push(MockRedisCall {
            op: "batch_del".to_string(),
            key: format!("keys={}", keys.len()),
            value: MockRedisValue::VecString(keys),
        });
        Ok(())
    }

    async fn execute_pipeline(
        &self,
        commands: Vec<PipelineCommand>,
    ) -> Result<Vec<Result<PipelineResult, CustomRedisError>>, CustomRedisError> {
        if let Some(duration) = self.pipeline_block {
            futures_timer::Delay::new(duration).await;
        }
        let call_index = self.pipeline_call_counter.fetch_add(1, Ordering::SeqCst);
        // Per-call error takes precedence so tests can mix success and failure.
        let injected_error = self
            .pipeline_errors_by_call
            .get(&call_index)
            .cloned()
            .or_else(|| self.pipeline_error.clone());
        if let Some(err) = injected_error {
            // Record commands as attempted so tests can assert which chunks
            // were tried, then return the injected connection-level error.
            for cmd in commands {
                drop(self.execute_pipeline_command(cmd));
            }
            return Err(err);
        }
        let results = commands
            .into_iter()
            .map(|cmd| self.execute_pipeline_command(cmd))
            .collect();
        Ok(results)
    }
}

impl MockRedisClient {
    /// Execute a single pipeline command and return its result.
    fn execute_pipeline_command(
        &self,
        cmd: PipelineCommand,
    ) -> Result<PipelineResult, CustomRedisError> {
        match cmd {
            PipelineCommand::Get { key, .. } => {
                self.record_call("pipeline_get", &key, MockRedisValue::None);
                Self::lookup_or_not_found(&self.get_ret, &key).map(PipelineResult::String)
            }
            PipelineCommand::GetRawBytes { key } => {
                self.record_call("pipeline_get_raw_bytes", &key, MockRedisValue::None);
                // Try raw bytes first, fall back to string conversion
                if let Some(result) = self.get_raw_bytes_ret.get(&key) {
                    result.clone().map(PipelineResult::Bytes)
                } else {
                    Self::lookup_or_not_found(&self.get_ret, &key)
                        .map(|s| PipelineResult::Bytes(s.into_bytes()))
                }
            }
            PipelineCommand::Set { key, value, format } => {
                self.record_call(
                    "pipeline_set",
                    &key,
                    MockRedisValue::StringWithFormat(value, format),
                );
                Self::lookup_or_ok(&self.set_ret, &key).map(|_| PipelineResult::Ok)
            }
            PipelineCommand::SetEx {
                key,
                value,
                seconds,
                format,
            } => {
                self.record_call(
                    "pipeline_setex",
                    &key,
                    MockRedisValue::StringWithTTLAndFormat(value, seconds, format),
                );
                Self::lookup_or_ok(&self.set_ret, &key).map(|_| PipelineResult::Ok)
            }
            PipelineCommand::SetNxEx {
                key,
                value,
                seconds,
                format,
            } => {
                self.record_call(
                    "pipeline_set_nx_ex",
                    &key,
                    MockRedisValue::StringWithTTLAndFormat(value, seconds, format),
                );
                Self::lookup_or_not_found(&self.set_nx_ex_ret, &key).map(PipelineResult::Bool)
            }
            PipelineCommand::Del { key } => {
                self.record_call("pipeline_del", &key, MockRedisValue::None);
                Self::lookup_or_ok(&self.del_ret, &key).map(|_| PipelineResult::Ok)
            }
            PipelineCommand::HGet { key, field } => {
                self.record_call(
                    "pipeline_hget",
                    format!("{key}:{field}"),
                    MockRedisValue::None,
                );
                Self::lookup_or_not_found(&self.hget_ret, &key).map(PipelineResult::String)
            }
            PipelineCommand::HIncrBy { key, field, count } => {
                self.record_call(
                    "pipeline_hincrby",
                    format!("{key}:{field}"),
                    MockRedisValue::I64(count),
                );
                Self::lookup_or_ok(&self.hincrby_ret, &key).map(|_| PipelineResult::Ok)
            }
            PipelineCommand::Scard { key } => {
                self.record_call("pipeline_scard", &key, MockRedisValue::None);
                Self::lookup_or_not_found(&self.scard_ret, &key).map(PipelineResult::Count)
            }
            PipelineCommand::SAdd { key, member } => {
                self.record_call("pipeline_sadd", &key, MockRedisValue::String(member));
                Self::lookup_or_ok(&self.sadd_ret, &key).map(|_| PipelineResult::Count(1))
            }
            PipelineCommand::ZAdd { key, members } => {
                for (score, member) in members {
                    self.record_call(
                        "pipeline_zadd",
                        format!("{key}:{member}"),
                        MockRedisValue::I64(score),
                    );
                }
                Ok(PipelineResult::Ok)
            }
            PipelineCommand::Expire { key, seconds } => {
                let ttl_i64 = i64::try_from(seconds).unwrap_or(i64::MAX);
                self.record_call("pipeline_expire", &key, MockRedisValue::I64(ttl_i64));
                Self::lookup_or_not_found(&self.expire_ret, &key).map(PipelineResult::Bool)
            }
            PipelineCommand::ZRangeByScore { key, min, max } => {
                self.record_call(
                    "pipeline_zrangebyscore",
                    &key,
                    MockRedisValue::MinMax(min, max),
                );
                self.zrangebyscore_ret
                    .get(&key)
                    .cloned()
                    .map(PipelineResult::Strings)
                    .ok_or(CustomRedisError::NotFound)
            }
        }
    }
}