Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

test(sync): e2e coverage for encrypted-store sync gating (#4469 follow-up) (#4472)

Louis Beaumontbd45d2d5153c
diff --git a/crates/screenpipe-engine/src/cli/store_file.rs b/crates/screenpipe-engine/src/cli/store_file.rs
index 4effb3ad1..290eeff8e 100644
--- a/crates/screenpipe-engine/src/cli/store_file.rs
+++ b/crates/screenpipe-engine/src/cli/store_file.rs
@@ -40,6 +40,32 @@ fn read_store_from(path: &Path) -> Result<Value> {
read_store_from_with_key(path, store_encryption_key)
}
+/// Test-only: read `<data_dir>/store.bin` with an injected decryption key.
+///
+/// Lets other modules' tests exercise the SPSTORE1-encrypted read path (the
+/// real on-disk shape when the user enabled store encryption) without the
+/// system keychain, which is unavailable in CI. Mirrors [`read_store_for`].
+#[cfg(test)]
+pub(crate) fn read_store_for_with_key(
+ data_dir: &Path,
+ key_provider: impl FnOnce() -> Result<[u8; 32]>,
+) -> Result<Value> {
+ read_store_from_with_key(&store_path_for(data_dir), key_provider)
+}
+
+/// Test-only: write `<data_dir>/store.bin` with an injected encryption key.
+/// Pair with [`read_store_for_with_key`] to round-trip an encrypted store in
+/// tests. `encrypt = true` produces the SPSTORE1 layout.
+#[cfg(test)]
+pub(crate) fn write_store_for_with_key(
+ data_dir: &Path,
+ store: &Value,
+ encrypt: bool,
+ key_provider: impl FnOnce() -> Result<[u8; 32]>,
+) -> Result<()> {
+ write_store_to_with_key(&store_path_for(data_dir), store, encrypt, key_provider)
+}
+
fn read_store_from_with_key(
path: &Path,
key_provider: impl FnOnce() -> Result<[u8; 32]>,
diff --git a/crates/screenpipe-engine/src/sync_api.rs b/crates/screenpipe-engine/src/sync_api.rs
index 39a9edfe3..ec411423c 100644
--- a/crates/screenpipe-engine/src/sync_api.rs
+++ b/crates/screenpipe-engine/src/sync_api.rs
@@ -1328,6 +1328,15 @@ fn is_settings_bool_enabled(screenpipe_dir: &std::path::Path, key: &str) -> bool
Ok(v) => v,
Err(_) => return false,
};
+ settings_bool(&store, key)
+}
+
+/// Pure predicate: read `settings.<key>` as a bool from a decoded store, false
+/// if the key is missing or not a bool. Split out from the (possibly
+/// encrypted) store read so tests can exercise it against a store decoded with
+/// an injected key — see `is_settings_bool_enabled` for why the read path
+/// matters (B1: encrypted stores silently disabled background sync).
+fn settings_bool(store: &serde_json::Value, key: &str) -> bool {
store
.get("settings")
.and_then(|s| s.get(key))
@@ -1846,6 +1855,64 @@ mod tests {
assert!(!is_settings_bool_enabled(dir.path(), "memoriesSyncEnabled"));
}
+ /// Pure-predicate edge cases for `settings.<key>`: missing `settings`,
+ /// missing key, and a non-bool value all read as `false`.
+ #[test]
+ fn test_settings_bool_predicate_edges() {
+ // No `settings` object at all.
+ assert!(!settings_bool(
+ &serde_json::json!({}),
+ "connectionsSyncEnabled"
+ ));
+ // `settings` present, key absent.
+ assert!(!settings_bool(
+ &serde_json::json!({ "settings": { "other": true } }),
+ "connectionsSyncEnabled"
+ ));
+ // Non-bool value (e.g. a string "true") is not treated as enabled.
+ assert!(!settings_bool(
+ &serde_json::json!({ "settings": { "connectionsSyncEnabled": "true" } }),
+ "connectionsSyncEnabled"
+ ));
+ assert!(settings_bool(
+ &serde_json::json!({ "settings": { "connectionsSyncEnabled": true } }),
+ "connectionsSyncEnabled"
+ ));
+ }
+
+ /// End-to-end of the B1 fix: when store encryption is ON, `store.bin` is
+ /// SPSTORE1-encrypted and the OLD plaintext `read_to_string + serde` reader
+ /// silently failed — returning `false` for EVERY sync toggle and disabling
+ /// background connection/pipe/memory sync. This writes the encrypted form
+ /// and asserts every toggle is read correctly through the decrypting path.
+ /// Injected key → no system keychain needed (CI-safe).
+ #[test]
+ fn test_settings_toggles_read_through_encrypted_store() {
+ use crate::cli::store_file::{read_store_for_with_key, write_store_for_with_key};
+ let key = [9u8; 32];
+ let dir = tempfile::tempdir().unwrap();
+
+ let store = serde_json::json!({
+ "settings": {
+ "connectionsSyncEnabled": true,
+ "pipeSyncEnabled": true,
+ "memoriesSyncEnabled": false,
+ }
+ });
+ write_store_for_with_key(dir.path(), &store, true, || Ok(key)).unwrap();
+
+ // The encrypted layout must NOT be readable as plaintext JSON — this is
+ // exactly the condition the old reader choked on.
+ let raw = std::fs::read(dir.path().join("store.bin")).unwrap();
+ assert_eq!(&raw[..8], b"SPSTORE1");
+ assert!(serde_json::from_slice::<serde_json::Value>(&raw).is_err());
+
+ let decoded = read_store_for_with_key(dir.path(), || Ok(key)).unwrap();
+ assert!(settings_bool(&decoded, "connectionsSyncEnabled"));
+ assert!(settings_bool(&decoded, "pipeSyncEnabled"));
+ assert!(!settings_bool(&decoded, "memoriesSyncEnabled"));
+ }
+
#[test]
fn test_sync_init_request_serialization() {
let request = SyncInitRequest {
diff --git a/crates/screenpipe-engine/src/sync_provider.rs b/crates/screenpipe-engine/src/sync_provider.rs
index d7fe3a3c0..9bf09500a 100644
--- a/crates/screenpipe-engine/src/sync_provider.rs
+++ b/crates/screenpipe-engine/src/sync_provider.rs
@@ -157,6 +157,19 @@ fn sync_data_type_enabled(gate_dir: Option<&std::path::Path>, blob_type: BlobTyp
Some(d) => d,
None => return true,
};
+ let store = match crate::cli::store_file::read_store_for(dir) {
+ Ok(v) => v,
+ Err(_) => return true,
+ };
+ sync_data_type_enabled_in(&store, blob_type)
+}
+
+/// Pure predicate: whether `blob_type` is enabled per a decoded store's
+/// `sync_config`. Split out from the (possibly encrypted) store read so tests
+/// can exercise the SPSTORE1-encrypted path with an injected key. Fail-OPEN:
+/// types without a toggle, an absent key, or a missing `sync_config` default to
+/// enabled so existing data-sync users are never silently regressed.
+fn sync_data_type_enabled_in(store: &serde_json::Value, blob_type: BlobType) -> bool {
// Map upload blob types to the UI's sync_config toggles. The accessibility
// tree is screen text, so it follows the OCR toggle.
let key = match blob_type {
@@ -164,10 +177,6 @@ fn sync_data_type_enabled(gate_dir: Option<&std::path::Path>, blob_type: BlobTyp
BlobType::Transcripts => "syncTranscripts",
_ => return true,
};
- let store = match crate::cli::store_file::read_store_for(dir) {
- Ok(v) => v,
- Err(_) => return true,
- };
store
.get("sync_config")
.and_then(|c| c.get(key))
@@ -959,6 +968,56 @@ mod tests {
assert!(sync_data_type_enabled(Some(dir.path()), BlobType::Input));
}
+ /// Fail-open coverage for the predicate: a `sync_config` that exists but is
+ /// missing the relevant key, or a missing `sync_config` entirely, must
+ /// default to enabled so existing data-sync users are never regressed.
+ #[test]
+ fn test_sync_data_type_predicate_fail_open() {
+ // Empty store object → enabled.
+ assert!(sync_data_type_enabled_in(
+ &serde_json::json!({}),
+ BlobType::Ocr
+ ));
+ // sync_config present but the toggle key is absent → enabled.
+ let partial = serde_json::json!({ "sync_config": { "syncTranscripts": false } });
+ assert!(sync_data_type_enabled_in(&partial, BlobType::Ocr));
+ assert!(!sync_data_type_enabled_in(&partial, BlobType::Transcripts));
+ // Non-bool toggle value → fall back to enabled, not panic.
+ let weird = serde_json::json!({ "sync_config": { "syncOcr": "nope" } });
+ assert!(sync_data_type_enabled_in(&weird, BlobType::Ocr));
+ }
+
+ /// End-to-end of the real on-disk shape when store encryption is ON: the
+ /// SPSTORE1-encrypted `store.bin` must decrypt and the per-type toggles must
+ /// be honored. Guards the B1 regression class (a plaintext-only reader
+ /// silently returns the fail-open default for encrypted stores). Uses an
+ /// injected key so the system keychain is never touched (CI-safe).
+ #[test]
+ fn test_sync_data_type_gating_encrypted_store() {
+ use crate::cli::store_file::{read_store_for_with_key, write_store_for_with_key};
+ let key = [7u8; 32];
+ let dir = tempfile::tempdir().unwrap();
+
+ let store = serde_json::json!({
+ "sync_config": { "syncOcr": false, "syncTranscripts": true }
+ });
+ // Write the encrypted (SPSTORE1) form, then read it back via the
+ // keychain-decrypting path with the same injected key.
+ write_store_for_with_key(dir.path(), &store, true, || Ok(key)).unwrap();
+
+ // Sanity: the on-disk bytes are actually encrypted, not plaintext JSON.
+ let raw = std::fs::read(dir.path().join("store.bin")).unwrap();
+ assert_eq!(&raw[..8], b"SPSTORE1");
+
+ let decoded = read_store_for_with_key(dir.path(), || Ok(key)).unwrap();
+ assert!(!sync_data_type_enabled_in(&decoded, BlobType::Ocr));
+ assert!(!sync_data_type_enabled_in(
+ &decoded,
+ BlobType::Accessibility
+ ));
+ assert!(sync_data_type_enabled_in(&decoded, BlobType::Transcripts));
+ }
+
#[test]
fn test_sync_chunk_serialization() {
let chunk = SyncChunk {