use cqs::embedder::Embedding;
use cqs::parser::{Chunk, ChunkType, Language};
use cqs::store::{ModelInfo, Store};
use std::path::PathBuf;
use tempfile::TempDir;
pub struct TestStore {
pub store: Store,
_dir: TempDir,
}
impl TestStore {
pub fn new() -> Self {
let dir = TempDir::new().expect("Failed to create temp dir");
let db_path = dir.path().join(cqs::INDEX_DB_FILENAME);
let store = Store::open(&db_path).expect("Failed to open store");
store
.init(&ModelInfo::default())
.expect("Failed to init store");
Self { store, _dir: dir }
}
#[allow(dead_code)]
pub fn db_path(&self) -> PathBuf {
self._dir.path().join(cqs::INDEX_DB_FILENAME)
}
#[allow(dead_code)]
pub fn with_model(model: &ModelInfo) -> Self {
let dir = TempDir::new().expect("Failed to create temp dir");
let db_path = dir.path().join(cqs::INDEX_DB_FILENAME);
let store = Store::open(&db_path).expect("Failed to open store");
store.init(model).expect("Failed to init store");
Self { store, _dir: dir }
}
}
impl std::ops::Deref for TestStore {
type Target = Store;
fn deref(&self) -> &Self::Target {
&self.store
}
}
#[allow(dead_code)]
pub fn test_chunk(name: &str, content: &str) -> Chunk {
let hash = blake3::hash(content.as_bytes()).to_hex().to_string();
Chunk {
id: format!("test.rs:1:{}", &hash[..8]),
file: PathBuf::from("test.rs"),
language: Language::Rust,
chunk_type: ChunkType::Function,
name: name.to_string(),
signature: format!("fn {}()", name),
content: content.to_string(),
doc: None,
line_start: 1,
line_end: 5,
content_hash: hash,
parent_id: None,
window_idx: None,
parent_type_name: None,
}
}
pub fn mock_embedding(seed: f32) -> Embedding {
let mut v = vec![seed; cqs::EMBEDDING_DIM];
let norm: f32 = v.iter().map(|x| x * x).sum::<f32>().sqrt();
if norm > 0.0 {
for x in &mut v {
*x /= norm;
}
}
Embedding::new(v)
}