use hakodb::document::hako_doc::{DocView, HakoDoc};
use hakodb::document::value::Value;
use std::sync::Arc;
fn check(doc: &HakoDoc) {
let expected = serde_json::to_vec(&doc.to_json()).unwrap();
let mut got = Vec::new();
doc.write_json(&mut got);
assert_eq!(got, expected, "mismatch for doc ts={}", doc._time);
assert_eq!(doc.to_json_bytes(), expected);
let view = DocView::new(Arc::new(doc.encode())).expect("encoded doc must frame");
let mut view_json = Vec::new();
view.write_json(&mut view_json).expect("view writer");
assert_eq!(view_json, expected, "view mismatch for doc ts={}", doc._time);
assert_eq!(view.to_json_bytes().as_deref(), Some(expected.as_slice()));
}
fn doc_with_time(fields: Vec<(&str, Value)>, ts: i64) -> HakoDoc {
let mut d = HakoDoc::default();
for (k, v) in fields {
d.insert(k, v);
}
d._time = ts;
d
}
#[test]
fn empty_doc_is_just_time() {
check(&doc_with_time(vec![], 0));
check(&doc_with_time(vec![], -5));
check(&doc_with_time(vec![], i64::MAX));
}
#[test]
fn time_merges_at_sorted_position() {
check(&doc_with_time(
vec![
("0abc", Value::Int(1)),
("Zebra", Value::Int(2)),
("_time", Value::String("field-wins".into())),
("apple", Value::Int(3)),
],
99,
));
check(&doc_with_time(vec![("a", Value::Int(1))], 7));
check(&doc_with_time(vec![("zzz", Value::Int(1))], 7));
}
#[test]
fn string_escapes_match_serde() {
let tricky = "a\"b\\c\nd\re\tf\x08g\x0Ch\x00i\x1Fj\x7f😀é";
check(&doc_with_time(vec![("s", Value::String(tricky.into()))], 1));
let mut d = HakoDoc::default();
d.insert("k\"ey\\", Value::Int(1));
d._time = 2;
check(&d);
}
#[test]
fn numerics_match_serde() {
check(&doc_with_time(
vec![
("zero", Value::Int(0)),
("neg", Value::Int(-1)),
("min", Value::Int(i64::MIN)),
("max", Value::Int(i64::MAX)),
("ratio", Value::Float(3.5)),
("big", Value::Float(1e100)),
("negzero", Value::Float(-0.0)),
("nan", Value::Float(f64::NAN)),
("inf", Value::Float(f64::INFINITY)),
("ninf", Value::Float(f64::NEG_INFINITY)),
("t", Value::Timestamp(1790635278743865)),
],
3,
));
}
#[test]
fn shapes_match_serde() {
let inner = vec![
(Arc::from("z"), Value::Int(1)),
(Arc::from("a"), Value::Int(2)),
];
check(&doc_with_time(
vec![
("nul", Value::Null),
("st", Value::ServerTimestamp),
("b", Value::Bool(true)),
("bf", Value::Bool(false)),
("arr", Value::Array(vec![Value::Int(1), Value::String("x".into()), Value::Null])),
("nested", Value::Array(vec![Value::Map(inner.clone())])),
("map", Value::Map(inner)),
("bin", Value::Binary(vec![0, 1, 255, 254])),
("binempty", Value::Binary(vec![])),
("r", Value::Reference { collection: "posts".into(), doc_id: "a\"b".into() }),
("bl", Value::BlobLink { offset: 123456789012345, len: 999 }),
("emptystr", Value::String("".into())),
("emptyarr", Value::Array(vec![])),
],
42,
));
}
#[test]
fn wide_maps_use_vec_fallback() {
let wide: Vec<(Arc<str>, Value)> = (0..40)
.rev()
.map(|i| (Arc::from(format!("k{i:02}")), Value::Int(i)))
.collect();
check(&doc_with_time(vec![("wide", Value::Map(wide))], 5));
}
#[test]
fn long_runs_match_serde() {
let mut s = String::new();
for i in 0..5000 {
s.push('a');
if i % 97 == 0 {
s.push('"');
}
if i % 149 == 0 {
s.push('\n');
}
if i % 211 == 0 {
s.push('\u{1}');
}
if i % 307 == 0 {
s.push('😀');
}
if i % 401 == 0 {
s.push('\\');
}
}
assert!(s.len() > 4096);
check(&doc_with_time(
vec![
("body", Value::String(s)),
("bin", Value::Binary((0..=255).collect())),
],
11,
));
check(&doc_with_time(
vec![(
"edge",
Value::String(format!("\"\\\n{}{}", "x".repeat(5000), "\t\x00\"")),
)],
12,
));
}
#[test]
fn escapes_at_every_lane_offset() {
let specials = ['"', '\\', '\n', '\r', '\t', '\u{8}', '\u{c}', '\u{0}', '\u{1f}'];
for &c in &specials {
for off in 0..=16usize {
let mut s = "a".repeat(off);
s.push(c);
s.push_str(&"b".repeat(20));
check(&doc_with_time(vec![("k", Value::String(s))], 1));
}
}
}
#[test]
fn writer_appends_to_existing_buffer() {
let d = doc_with_time(vec![("a", Value::Int(1))], 5);
let mut out = b"prefix:".to_vec();
d.write_json(&mut out);
let expected = serde_json::to_vec(&d.to_json()).unwrap();
assert_eq!(&out[7..], &expected[..]);
assert_eq!(&out[..7], b"prefix:");
}