From 9e3a1a467548b43cef9084dbaca30886e0c39f68 Mon Sep 17 00:00:00 2001 From: Aditya Mukhopadhyay Date: Sat, 18 Apr 2026 18:37:31 +0530 Subject: [PATCH 1/4] test(compat): cross-platform .graph compatibility tests (issue #150) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add two native integration tests and one browser wasm-bindgen-test verifying that .graph bytes are readable across the native ↔ browser WASM boundary. Native side (tests/cross_platform_compat_test.rs): - native_raw_page_bytes_round_trip: transact facts, checkpoint, read raw bytes, write to a second file, reload — mirrors export_graph / import_graph page concatenation semantics - fixture_readable_by_native: loads tests/fixtures/compat.graph (the committed binary fixture) via Minigraf::open and asserts known facts Browser side (src/browser/mod.rs): - native_fixture_readable_by_browser_db: embeds the same fixture via include_bytes!, calls BrowserDb::import_graph, asserts same facts — the native → browser direction of the cross-boundary test Fixture (tests/fixtures/compat.graph): - Generated by examples/generate_compat_fixture.rs - v7 format, two facts: :alice :name "Alice", :alice :age 30 - Committed so wasm-pack tests can embed it without a build step Closes #150 Co-Authored-By: Claude Sonnet 4.6 --- examples/generate_compat_fixture.rs | 37 +++++++++ src/browser/mod.rs | 33 ++++++++ tests/cross_platform_compat_test.rs | 113 ++++++++++++++++++++++++++++ tests/fixtures/compat.graph | Bin 0 -> 28672 bytes 4 files changed, 183 insertions(+) create mode 100644 examples/generate_compat_fixture.rs create mode 100644 tests/cross_platform_compat_test.rs create mode 100644 tests/fixtures/compat.graph diff --git a/examples/generate_compat_fixture.rs b/examples/generate_compat_fixture.rs new file mode 100644 index 0000000000..d3746f08b7 --- /dev/null +++ b/examples/generate_compat_fixture.rs @@ -0,0 +1,37 @@ +//! Generates `tests/fixtures/compat.graph` — a minimal v7 `.graph` file containing +//! two known facts used by the cross-platform compatibility tests. +//! +//! Run once when the file format changes or the fixture needs regenerating: +//! cargo run --example generate_compat_fixture +//! +//! The fixture is committed to the repository. Do not regenerate it unless the +//! v7 file format itself has changed — regenerating changes the binary and +//! every cross-platform test that embeds it via `include_bytes!`. + +use std::path::PathBuf; + +fn main() -> anyhow::Result<()> { + let manifest_dir = env!("CARGO_MANIFEST_DIR"); + let fixture_path = PathBuf::from(manifest_dir).join("tests/fixtures/compat.graph"); + let tmp_path = fixture_path.with_extension("graph.tmp"); + + // Remove any leftover from a previous run. + let _ = std::fs::remove_file(&tmp_path); + let _ = std::fs::remove_file(tmp_path.with_extension("wal")); + + // Populate with known facts. + let db = minigraf::Minigraf::open(&tmp_path)?; + db.execute(r#"(transact [[:alice :name "Alice"]])"#)?; + db.execute("(transact [[:alice :age 30]])")?; + // Checkpoint flushes WAL → main file so the bytes are self-contained. + db.checkpoint()?; + drop(db); + + // Remove WAL sidecar before copying. + let wal_path = tmp_path.with_extension("graph.tmp.wal"); + let _ = std::fs::remove_file(&wal_path); + + std::fs::rename(&tmp_path, &fixture_path)?; + println!("Written: {}", fixture_path.display()); + Ok(()) +} diff --git a/src/browser/mod.rs b/src/browser/mod.rs index a57902b9ef..11d3aab34d 100644 --- a/src/browser/mod.rs +++ b/src/browser/mod.rs @@ -435,6 +435,39 @@ mod tests { assert_eq!(blob.byte_length() as usize % crate::storage::PAGE_SIZE, 0); } + /// Load the committed binary fixture (produced by `cargo run --example + /// generate_compat_fixture` from the native build) into a `BrowserDb` via + /// `import_graph` and verify the known facts are queryable. + /// + /// This is the **native → browser** direction of the cross-platform + /// compatibility test. The companion native side lives in + /// `tests/cross_platform_compat_test.rs`. + #[wasm_bindgen_test] + async fn native_fixture_readable_by_browser_db() { + let fixture: &[u8] = include_bytes!("../../tests/fixtures/compat.graph"); + let db = BrowserDb::open_in_memory().expect("open in-memory"); + let arr = js_sys::Uint8Array::from(fixture); + db.import_graph(arr).await.expect("import native fixture"); + + let r = db + .execute(r#"(query [:find ?name :where [?e :name ?name]])"#.to_string()) + .await + .expect("query name"); + let v: serde_json::Value = serde_json::from_str(&r).unwrap(); + let results = v["results"].as_array().unwrap(); + assert_eq!(results.len(), 1, "expected 1 name result from native fixture"); + assert_eq!(results[0][0], serde_json::Value::String("Alice".into())); + + let r2 = db + .execute("(query [:find ?age :where [?e :age ?age]])".to_string()) + .await + .expect("query age"); + let v2: serde_json::Value = serde_json::from_str(&r2).unwrap(); + let results2 = v2["results"].as_array().unwrap(); + assert_eq!(results2.len(), 1, "expected 1 age result from native fixture"); + assert_eq!(results2[0][0], serde_json::Value::Number(30.into())); + } + #[wasm_bindgen_test] async fn idb_persistence_round_trip() { let db_name = "minigraf-test-persistence"; diff --git a/tests/cross_platform_compat_test.rs b/tests/cross_platform_compat_test.rs new file mode 100644 index 0000000000..ebc9498fa7 --- /dev/null +++ b/tests/cross_platform_compat_test.rs @@ -0,0 +1,113 @@ +//! Cross-platform `.graph` file compatibility tests (native side). +//! +//! Verifies that raw page bytes produced by the native storage layer are +//! self-consistent (mimicking what `BrowserDb::export_graph` / `import_graph` +//! do) and that the committed binary fixture is readable by `Minigraf::open`. +//! +//! The companion browser side lives in `src/browser/mod.rs` under +//! `#[wasm_bindgen_test]` and loads the same fixture via `include_bytes!`. + +use minigraf::{Minigraf, QueryResult}; +use std::path::PathBuf; + +fn tmp(tag: &str) -> PathBuf { + std::env::temp_dir().join(format!("minigraf_compat_{tag}.graph")) +} + +fn cleanup(path: &PathBuf) { + let _ = std::fs::remove_file(path); + let _ = std::fs::remove_file(path.with_extension("graph.wal")); +} + +/// Produce a `.graph` file, read its raw bytes, write them to a second file, +/// open the second file — verifying that byte-for-byte copies are readable. +/// +/// This mirrors what `BrowserDb::export_graph()` + `BrowserDb::import_graph()` +/// do: the export concatenates raw 4 KB pages; the import splits them back and +/// loads via a fresh `PersistentFactStorage`. +#[test] +fn native_raw_page_bytes_round_trip() { + let src = tmp("src"); + let dst = tmp("dst"); + cleanup(&src); + cleanup(&dst); + + // Produce a populated, checkpointed .graph file. + { + let db = Minigraf::open(&src).expect("open src"); + db.execute(r#"(transact [[:alice :name "Alice"]])"#) + .expect("transact name"); + db.execute("(transact [[:alice :age 30]])") + .expect("transact age"); + db.checkpoint().expect("checkpoint"); + } + + // Copy raw bytes to a new path (simulates export → import across the boundary). + let bytes = std::fs::read(&src).expect("read src"); + std::fs::write(&dst, &bytes).expect("write dst"); + + // Open the byte-copy and verify both facts survive. + let db2 = Minigraf::open(&dst).expect("open dst"); + + let r = db2 + .execute("(query [:find ?name :where [?e :name ?name]])") + .expect("query name"); + match r { + QueryResult::QueryResults { results, .. } => { + assert_eq!(results.len(), 1, "expected 1 name result"); + } + _ => panic!("expected QueryResults for name query"), + } + + let r2 = db2 + .execute("(query [:find ?age :where [?e :age ?age]])") + .expect("query age"); + match r2 { + QueryResult::QueryResults { results, .. } => { + assert_eq!(results.len(), 1, "expected 1 age result"); + } + _ => panic!("expected QueryResults for age query"), + } + + cleanup(&src); + cleanup(&dst); +} + +/// Load the committed fixture (produced by `cargo run --example generate_compat_fixture`) +/// via `Minigraf::open` and assert that the known facts are present. +/// +/// This fixture is also loaded by the browser WASM tests via `include_bytes!` + +/// `BrowserDb::import_graph`, completing the cross-boundary coverage. +#[test] +fn fixture_readable_by_native() { + let fixture: &[u8] = include_bytes!("fixtures/compat.graph"); + + // Write to a temp path so Minigraf::open can use it. + let path = tmp("fixture"); + cleanup(&path); + std::fs::write(&path, fixture).expect("write fixture"); + + let db = Minigraf::open(&path).expect("open fixture"); + + let r = db + .execute("(query [:find ?name :where [?e :name ?name]])") + .expect("query name"); + match r { + QueryResult::QueryResults { results, .. } => { + assert_eq!(results.len(), 1, "expected 1 name result from fixture"); + } + _ => panic!("expected QueryResults for name query"), + } + + let r2 = db + .execute("(query [:find ?age :where [?e :age ?age]])") + .expect("query age"); + match r2 { + QueryResult::QueryResults { results, .. } => { + assert_eq!(results.len(), 1, "expected 1 age result from fixture"); + } + _ => panic!("expected QueryResults for age query"), + } + + cleanup(&path); +} diff --git a/tests/fixtures/compat.graph b/tests/fixtures/compat.graph new file mode 100644 index 0000000000000000000000000000000000000000..e49b5758cea613c10fb0f90fe7c81200e0e66299 GIT binary patch literal 28672 zcmeI(F-yZh7{>AUu1Uq|96JfR=^#qMp*q+_K@f_aI}`~;sP1kqLP5vkAdU_Wx;Y7c zfr73=7X?SZK?jvwu0e8&Q!$W#AiNE|BX_?}5AStnYj<->MB+QXbW+kxsCo5wa2q1s} z0tg_000IagfB*sr6hUC-ZR7p^wb5ETTWa>6D%(dvt=);#`uW4)WLVWVqyFh#=i}2| zN_peM+F|=3lCa+0-;4Tomb%W)8u#ssAj%#QKmY**5I_I{1Q0*~0R#{zs=(a0{{ORC zkXy4@R0{Tu00IagfB*srAbMPa*=*eRrr7@f Date: Sat, 18 Apr 2026 18:37:52 +0530 Subject: [PATCH 2/4] style: rustfmt browser compat test Co-Authored-By: Claude Sonnet 4.6 --- src/browser/mod.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/browser/mod.rs b/src/browser/mod.rs index 11d3aab34d..161453c3c1 100644 --- a/src/browser/mod.rs +++ b/src/browser/mod.rs @@ -455,7 +455,11 @@ mod tests { .expect("query name"); let v: serde_json::Value = serde_json::from_str(&r).unwrap(); let results = v["results"].as_array().unwrap(); - assert_eq!(results.len(), 1, "expected 1 name result from native fixture"); + assert_eq!( + results.len(), + 1, + "expected 1 name result from native fixture" + ); assert_eq!(results[0][0], serde_json::Value::String("Alice".into())); let r2 = db @@ -464,7 +468,11 @@ mod tests { .expect("query age"); let v2: serde_json::Value = serde_json::from_str(&r2).unwrap(); let results2 = v2["results"].as_array().unwrap(); - assert_eq!(results2.len(), 1, "expected 1 age result from native fixture"); + assert_eq!( + results2.len(), + 1, + "expected 1 age result from native fixture" + ); assert_eq!(results2[0][0], serde_json::Value::Number(30.into())); } From 9ee031bc36abe6d1882e433916490840aff77ce1 Mon Sep 17 00:00:00 2001 From: Aditya Mukhopadhyay Date: Sat, 18 Apr 2026 18:40:54 +0530 Subject: [PATCH 3/4] fix(test): gate cross_platform_compat_test.rs on not(wasm32) wasm-pack compiles integration tests for the browser target too. Minigraf::open() is not available on wasm32, causing compile errors. Add #![cfg(not(target_arch = "wasm32"))] to exclude the file entirely. Co-Authored-By: Claude Sonnet 4.6 --- tests/cross_platform_compat_test.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/cross_platform_compat_test.rs b/tests/cross_platform_compat_test.rs index ebc9498fa7..0d1c3a8d08 100644 --- a/tests/cross_platform_compat_test.rs +++ b/tests/cross_platform_compat_test.rs @@ -1,4 +1,10 @@ //! Cross-platform `.graph` file compatibility tests (native side). +// These tests use Minigraf::open() and the file system, which are not available +// on wasm32 targets. wasm-pack compiles integration tests too, so gate the +// entire file. +#![cfg(not(target_arch = "wasm32"))] + +//! //! //! Verifies that raw page bytes produced by the native storage layer are //! self-consistent (mimicking what `BrowserDb::export_graph` / `import_graph` From e45ee38fb0658bea988476754d17820662b24bba Mon Sep 17 00:00:00 2001 From: Aditya Mukhopadhyay Date: Sat, 18 Apr 2026 18:46:17 +0530 Subject: [PATCH 4/4] fix(example): gate generate_compat_fixture native code on not(wasm32) wasm-pack compiles examples when running browser tests. Add a no-op wasm32 main() so the example compiles cleanly under wasm-pack, while keeping the native implementation under cfg(not(target_arch = "wasm32")). Co-Authored-By: Claude Sonnet 4.6 --- examples/generate_compat_fixture.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/examples/generate_compat_fixture.rs b/examples/generate_compat_fixture.rs index d3746f08b7..b8b0b098cc 100644 --- a/examples/generate_compat_fixture.rs +++ b/examples/generate_compat_fixture.rs @@ -8,9 +8,16 @@ //! v7 file format itself has changed — regenerating changes the binary and //! every cross-platform test that embeds it via `include_bytes!`. -use std::path::PathBuf; +// wasm-pack compiles examples for the browser target; provide a no-op entry +// point so the example compiles cleanly. The actual generator only makes sense +// on native (it needs the file system and Minigraf::open). +#[cfg(target_arch = "wasm32")] +fn main() {} +#[cfg(not(target_arch = "wasm32"))] fn main() -> anyhow::Result<()> { + use std::path::PathBuf; + let manifest_dir = env!("CARGO_MANIFEST_DIR"); let fixture_path = PathBuf::from(manifest_dir).join("tests/fixtures/compat.graph"); let tmp_path = fixture_path.with_extension("graph.tmp");