mod common;
use common::*;
use node_app_replica::wire::{value_within_depth, MAX_VALUE_DEPTH};
use node_app_replica::{LedgerStore, MemoryLedger, PullRequest, ReplicaError};
use serde::Deserialize;
use serde_json::{json, Value};
#[derive(Deserialize)]
struct Vector {
limit: usize,
accepted_up_to: usize,
cases: Vec<Case>,
}
#[derive(Deserialize)]
struct Case {
shape: String,
containers: usize,
accepted: bool,
}
fn vector() -> Vector {
let path = concat!(
env!("CARGO_MANIFEST_DIR"),
"/tests/fixtures/value-depth.json"
);
let text = std::fs::read_to_string(path).unwrap();
serde_json::from_str(&text).unwrap()
}
fn shaped(shape: &str, containers: usize) -> Value {
match shape {
"array" | "object" | "alternating" => {
let mut value = json!(1);
for i in 0..containers {
let array = shape == "array" || (shape == "alternating" && i % 2 == 0);
value = if array {
json!([value])
} else {
json!({ "a": value })
};
}
value
}
"empty" => {
let mut value = json!([]);
for _ in 1..containers {
value = json!([value]);
}
value
}
"wide" => {
let mut value = json!(1);
for _ in 1..containers {
value = json!([value]);
}
json!([1, "x", value])
}
other => panic!("unknown shape {other}"),
}
}
fn on_both_stores(body: impl Fn(&dyn LedgerStore)) {
body(&sqlite());
body(&MemoryLedger::new());
}
#[test]
fn the_limit_is_the_fixtures_and_serde_jsons() {
let vector = vector();
assert_eq!(MAX_VALUE_DEPTH, vector.limit);
for containers in [vector.accepted_up_to, vector.accepted_up_to + 1] {
let text = shaped("array", containers).to_string();
let parsed = serde_json::from_str::<Value>(&text);
assert_eq!(
parsed.is_ok(),
containers <= vector.accepted_up_to,
"{containers}"
);
assert_eq!(
value_within_depth(&shaped("array", containers)),
containers <= vector.accepted_up_to
);
}
}
#[test]
fn touch_reconcile_and_the_pull_refuse_past_the_limit() {
on_both_stores(|store| {
for case in vector().cases {
let name = format!("{} {}", case.shape, case.containers);
let value = shaped(&case.shape, case.containers);
assert_eq!(value_within_depth(&value), case.accepted, "{name}");
let c = collection(TestEnv::leak(T0, &[]));
c.ensure(store).unwrap();
let deep = row(&format!("deep-{name}"), value);
let other = row("b", json!(1));
if case.accepted {
let before = c.head(store).unwrap().version;
assert_eq!(c.touch(store, &deep).unwrap(), before + 1, "{name}");
c.remove(store, &deep.key).unwrap();
continue;
}
let head = c.head(store).unwrap();
for error in [
c.touch(store, &deep).unwrap_err(),
c.reconcile(store, &[other.clone(), deep.clone()])
.unwrap_err(),
] {
assert!(
matches!(&error, ReplicaError::InvalidRow { key, .. } if *key == deep.key),
"{name}: {error}"
);
assert!(
error.to_string().contains("at most 127 are allowed"),
"{name}: {error}"
);
}
assert_eq!(c.head(store).unwrap(), head, "{name}");
c.mark_dirty(store).unwrap();
let request = PullRequest {
epoch: String::new(),
version: 0,
schema: 1,
limit: 500,
resume: None,
};
let loaded = vec![deep.clone()];
let error = c
.answer(store, &request, |_store, _keys| Ok(loaded.clone()))
.unwrap_err();
assert!(
error.to_string().starts_with("replica_row_invalid:"),
"{name}: {error}"
);
}
});
}
#[test]
fn the_same_refusal_text_as_the_typescript_helper() {
let c = collection(TestEnv::leak(T0, &[]));
let store = MemoryLedger::new();
c.ensure(&store).unwrap();
let error = c
.touch(&store, &row("k", shaped("array", 128)))
.unwrap_err();
assert_eq!(
error.to_string(),
"replica_row_invalid: row 'k': value nests 128 or more containers; at most 127 are allowed (the node's JSON limit)"
);
}