vestige/tests/phase_1/embedder_trait.rs

44 lines
1.4 KiB
Rust
Raw Normal View History

feat(storage): phase 1 -- extract MemoryStore and Embedder traits (ADR 0001) Introduce two trait boundaries that the rest of the stack now sits above, landing Phase 1 of ADR 0001 (pluggable storage and network access). Rebased onto v2.1.22 Sanhedrin from the original April work. MemoryStore / LocalMemoryStore (crates/vestige-core/src/storage/memory_store.rs): One trait, ~25 methods, covering CRUD, hybrid / FTS / vector search, FSRS scheduling, graph edges, and the forthcoming domain surface. trait_variant::make generates a Send-bound MemoryStore alias over the base LocalMemoryStore so Arc<dyn MemoryStore> works under tokio/axum. Storage errors map through a dedicated MemoryStoreError. Embedder / LocalEmbedder (crates/vestige-core/src/embedder/): Pluggable text-to-vector encoder. FastembedEmbedder wraps the existing EmbeddingService; storage never calls fastembed directly anymore. Embedder::signature() produces the ModelSignature consumed by the store's embedding_model registry. SqliteMemoryStore (crates/vestige-core/src/storage/sqlite.rs): Storage renamed to SqliteMemoryStore; the old name lives on as a pub type alias so Arc<Storage> consumers in vestige-mcp stay intact. All existing inherent methods are untouched; the trait impl is purely additive and dispatches into them. The db_path field added by v2.1.1 portable-sync is preserved. Migration V14 (crates/vestige-core/src/storage/migrations.rs): Renumbered from V12 (the original April number) to V14 to slot in cleanly after upstream's V12 (v2.1.1 sync_tombstones) and V13 (v2.1.2 purge tombstones). - embedding_model registry table (CHECK id = 1, code enforces the single-row invariant). - knowledge_nodes.domains / domain_scores TEXT columns (JSON arrays default '[]' / '{}'), domains catalogue table, supporting indexes. Phase 4 populates these columns; Phase 1 just exposes the schema. Consolidation and other cognitive pathways now accept a &dyn LocalMemoryStore (sync) or Arc<dyn MemoryStore> (async) rather than a concrete Storage. Tests: - trait-method unit tests colocated in sqlite.rs and migrations.rs - embedder/fastembed.rs tests for name/dimension/hash stability - new integration crate tests/phase_1 (added to workspace members): trait_round_trip (8), embedding_model_registry (7), domain_column_migration (5), cognitive_module_isolation (4), send_bound_variant (2), embedder_trait (2). Acceptance gate post-rebase: - cargo build --workspace --all-targets: ok - cargo clippy --workspace --all-targets -- -D warnings: clean - cargo test -p vestige-core --lib: 428 pass - cargo test -p vestige-phase-1-tests: 28 pass - cargo test -p vestige-mcp --lib: 380 pass (Storage alias preserves every existing call site) Co-existence with v2.1.1 portable-sync: this trait extraction is additive. Portable-sync's tombstone migrations (V12, V13) remain on the concrete SqliteMemoryStore; Phase 2 (Postgres) will decide which of those surfaces graduate into the trait.
2026-04-21 21:43:52 +02:00
//! Phase 1 integration tests: Embedder trait and FastembedEmbedder.
use std::sync::Arc;
use tempfile::tempdir;
use vestige_core::embedder::{Embedder, FastembedEmbedder};
use vestige_core::storage::MemoryStore;
use vestige_core::storage::SqliteMemoryStore;
fn make_store() -> Arc<dyn MemoryStore> {
let dir = tempdir().unwrap();
let db = dir.path().join("test.db");
std::mem::forget(dir);
Arc::new(SqliteMemoryStore::new(Some(db)).expect("create"))
}
#[tokio::test]
async fn fastembed_implements_embedder_trait() {
// The key test: `Box<dyn Embedder>` compiles
let e: Box<dyn Embedder> = Box::new(FastembedEmbedder::new());
assert_eq!(e.dimension(), 256, "dimension must be 256");
assert!(!e.model_name().is_empty(), "model_name must not be empty");
assert!(!e.model_hash().is_empty(), "model_hash must not be empty");
assert_eq!(e.model_hash().len(), 64, "hash must be 64 hex chars");
}
#[tokio::test]
async fn signature_matches_memory_store_registry() {
let e = FastembedEmbedder::new();
let sig = e.signature();
let store = make_store();
store
.register_model(&sig)
.await
.expect("register via Embedder::signature");
let got = store
.registered_model()
.await
.expect("registered_model")
.expect("Some");
assert_eq!(got.name, sig.name);
assert_eq!(got.dimension, sig.dimension);
assert_eq!(got.hash, sig.hash);
}