feat: resumable sha256 embedding cache + cost estimate

This commit is contained in:
2026-05-18 18:41:27 +02:00
parent ff41d53155
commit 2686edab91
2 changed files with 112 additions and 1 deletions
+28
View File
@@ -0,0 +1,28 @@
use alpha_id::embed::{EmbeddingStore, estimate};
#[test]
fn cache_key_is_stable_sha256() {
let k1 = EmbeddingStore::key("BAAI/bge-m3", "diabetes");
let k2 = EmbeddingStore::key("BAAI/bge-m3", "diabetes");
assert_eq!(k1, k2);
assert_ne!(k1, EmbeddingStore::key("BAAI/bge-m3", "hypertonie"));
assert_eq!(k1.len(), 64); // hex sha256
}
#[test]
fn store_persists_and_reloads_vectors_resumably() {
let dir = tempfile::tempdir().unwrap();
let store = EmbeddingStore::open(dir.path().to_str().unwrap(), "BAAI/bge-m3").unwrap();
assert!(store.get("diabetes").is_none());
store.put("diabetes", &[0.1, 0.2, 0.3]).unwrap();
let reopened = EmbeddingStore::open(dir.path().to_str().unwrap(), "BAAI/bge-m3").unwrap();
assert_eq!(reopened.get("diabetes").unwrap(), vec![0.1, 0.2, 0.3]);
assert!(reopened.has("diabetes")); // resume: skip already-embedded
}
#[test]
fn estimate_reports_pending_count_and_chars() {
let est = estimate(&["abc".to_string(), "de".to_string()]);
assert_eq!(est.items, 2);
assert_eq!(est.chars, 5);
}