use std::sync::Arc;
use grit_core::{Budget, GraphOp, Grit, ManualClock, Options, Query, import_jsonl};
use serde_json::json;
fn open(dir: &tempfile::TempDir, name: &str) -> Grit {
Grit::open(
dir.path().join(name),
Options::new("audit").clock(Arc::new(ManualClock::new(1_000_000))),
)
.unwrap()
}
#[test]
fn bundled_sqlite_version_is_pinned() {
let dir = tempfile::tempdir().unwrap();
let g = open(&dir, "v.db");
drop(g);
let conn = rusqlite::Connection::open(dir.path().join("v.db")).unwrap();
let version: String = conn
.query_row("SELECT sqlite_version()", [], |r| r.get(0))
.unwrap();
assert_eq!(
version, "3.53.2",
"bundled SQLite changed — if intentional, update this pin AND the \
rusqlite `=` requirement in Cargo.toml together"
);
}
#[test]
fn memoryish_paths_are_rejected() {
for path in [
":memory:",
"",
"file::memory:?cache=shared",
"file:x.db?mode=memory",
] {
assert!(
Grit::open(path, Options::new("dev")).is_err(),
"path {path:?} must be rejected"
);
}
}
#[test]
fn embedding_after_purge_is_a_noop() {
let dir = tempfile::tempdir().unwrap();
let g = open(&dir, "e.db");
g.register_embedding_model("m", 4, "1").unwrap();
let node = g.new_id();
g.apply(GraphOp::AddNode {
id: node,
kind: "k".into(),
name: "secret".into(),
summary: String::new(),
attrs: json!({}),
group_id: String::new(),
})
.unwrap();
g.apply(GraphOp::Purge { ids: vec![node] }).unwrap();
g.set_node_embedding(node, vec![1.0, 0.0, 0.0, 0.0])
.unwrap();
assert!(
g.search(
Query::text("")
.vector(vec![1.0, 0.0, 0.0, 0.0])
.budget(Budget::items(5))
)
.unwrap()
.is_empty()
);
drop(g);
let conn = rusqlite::Connection::open(dir.path().join("e.db")).unwrap();
let rows: i64 = conn
.query_row("SELECT COUNT(*) FROM vec_nodes", [], |r| r.get(0))
.unwrap();
assert_eq!(
rows, 0,
"purged content must leave no derived vectors behind"
);
}
#[test]
fn import_rejects_malformed_uuids() {
let dir = tempfile::tempdir().unwrap();
let stream = format!(
"{}\n{}\n",
r#"{"grit_export":1,"schema_version":1}"#,
r#"{"t":"node","id":"not-a-uuid","kind":"k","name":"n","summary":"","attrs":{},"group_id":"","created_at":1,"expired_at":null,"merged_into":null,"hlc":"0000000000000001-00000000-a"}"#
);
let err = import_jsonl(dir.path().join("i.db"), stream.as_bytes()).unwrap_err();
assert!(err.to_string().contains("not a uuid"), "got: {err}");
}