#![cfg(all(target_family = "wasm", feature = "persistence"))]
use cubecl_environment::bytes::Bytes;
use cubecl_environment::environment;
use cubecl_environment::persistence::{CacheOption, Namespace, Store, StoreOptions};
use wasm_bindgen_test::*;
wasm_bindgen_test_configure!(run_in_dedicated_worker);
fn eager(namespace: &str) -> StoreOptions {
StoreOptions::new().storage(Namespace::scoped("browser", namespace))
}
fn lazy(namespace: &str) -> StoreOptions {
eager(namespace).cache(CacheOption::Lazy)
}
async fn open() {
environment::open().await;
}
fn assert_durable<K, V>(store: &Store<K, V>)
where
K: cubecl_environment::persistence::StoreKey,
V: cubecl_environment::persistence::StoreValue,
{
let description = store.to_string();
assert!(
description.contains("Turso cubecl-"),
"the store is not on the browser's database: {description}"
);
}
#[wasm_bindgen_test]
async fn entries_survive_a_reopen_of_the_store() {
open().await;
let mut store = Store::<String, u32>::new(eager("reopen"));
assert_durable(&store);
store.insert("key".to_string(), 7).unwrap();
drop(store);
let store = Store::<String, u32>::new(eager("reopen"));
assert_durable(&store);
assert_eq!(store.get(&"key".to_string()), Some(&7));
}
#[wasm_bindgen_test]
async fn a_lazy_store_reads_through() {
open().await;
let mut store = Store::<String, Bytes>::new(lazy("kernels"));
assert_durable(&store);
store
.insert("kernel".to_string(), Bytes::from_bytes_vec(vec![1, 2, 3]))
.unwrap();
assert!(store.is_empty(), "a lazy insert retains nothing");
let mut reopened = Store::<String, Bytes>::new(lazy("kernels"));
assert_eq!(
reopened
.remove(&"kernel".to_string())
.map(|bytes| bytes.to_vec()),
Some(vec![1, 2, 3])
);
}
#[wasm_bindgen_test]
async fn the_namespaces_are_listed() {
open().await;
let mut store = Store::<String, u32>::new(eager("listed"));
assert_durable(&store);
store.insert("key".to_string(), 1).unwrap();
let namespaces: Vec<String> = environment::namespaces()
.into_iter()
.map(|summary| summary.namespace)
.collect();
assert!(
namespaces
.iter()
.any(|namespace| namespace.ends_with("/listed")),
"{namespaces:?}"
);
}