mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-16 20:25:02 +00:00
Compare streaming native output with previous framing
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
//! Streaming helpers for the native JSON envelope.
|
||||
//!
|
||||
//! Field values are emitted in bounded chunks. The decoded JSON and tail framing stay the same
|
||||
//! as VectorCraft's previous writer, but compressed bytes can differ when a deflate backend's
|
||||
//! output depends on input chunking. Gzip readers still validate the CRC and size trailer.
|
||||
//!
|
||||
//! Copyright (c) 2026 ArtCraft Team and the VectorCraft contributors.
|
||||
//! Extracted from VectorCraft, licensed under MIT OR Apache-2.0.
|
||||
|
||||
|
||||
@@ -106,3 +106,77 @@ fn native_schema_order_and_versions() {
|
||||
let header = FileHeader { version: 0, generator: "test", pdf_compatible: None, preview: None, profiles: &profiles, pdf: None };
|
||||
assert!(write_file_json(Vec::new(), &header, false, |_| Ok(Default::default())).is_err());
|
||||
}
|
||||
|
||||
// Oracle for the previous app writer: each member was first serialized to a Vec, then
|
||||
// indented and appended. Compare decoded file bytes, not backend-dependent gzip bytes.
|
||||
#[test]
|
||||
fn streamed_native_file_matches_previous_framing() {
|
||||
use craft_vectorcraft_format::{
|
||||
Tail,
|
||||
stream::{FileHeader, write_file_json},
|
||||
};
|
||||
use flate2::{Compression, read::GzDecoder, write::GzEncoder};
|
||||
use std::collections::BTreeMap;
|
||||
use std::io::Read;
|
||||
|
||||
fn old_field(out: &mut Vec<u8>, first: &mut bool, pretty: bool, key: &str, value: &impl serde::Serialize) {
|
||||
out.extend_from_slice(if std::mem::take(first) { b"{" } else { b"," });
|
||||
if pretty {
|
||||
out.extend_from_slice(b"\n ");
|
||||
}
|
||||
out.extend_from_slice(format!("\"{key}\"{}", if pretty { ": " } else { ":" }).as_bytes());
|
||||
let json = if pretty { serde_json::to_vec_pretty(value) } else { serde_json::to_vec(value) }.unwrap();
|
||||
let mut indent = Indent { inner: out, on: pretty };
|
||||
indent.write_all(&json).unwrap();
|
||||
}
|
||||
|
||||
let payload = vec![42; craft_vectorcraft_format::INLINE_MAX];
|
||||
for version in [1, 3, 4] {
|
||||
for pretty in [false, true] {
|
||||
let mut tail = Tail::new(version);
|
||||
let image = tail.image("image/png", &payload, false);
|
||||
let profile = tail.image("application/vnd.iccprofile", &payload, false);
|
||||
let pdf = tail.image("application/pdf", &payload, false);
|
||||
let profiles = BTreeMap::from([("profile", profile)]);
|
||||
let images = BTreeMap::from([("image", image)]);
|
||||
let document = serde_json::json!({"layers": [{"key": "image", "name": "A"}]});
|
||||
let header = FileHeader {
|
||||
version,
|
||||
generator: "VectorCraft test",
|
||||
pdf_compatible: Some(true),
|
||||
preview: None,
|
||||
profiles: &profiles,
|
||||
pdf: Some(&pdf),
|
||||
};
|
||||
let mut actual = Vec::new();
|
||||
write_file_json(&mut actual, &header, pretty, |w| {
|
||||
if pretty { serde_json::to_writer_pretty(w, &document) } else { serde_json::to_writer(w, &document) }.map_err(io::Error::other)?;
|
||||
Ok(images.clone())
|
||||
})
|
||||
.unwrap();
|
||||
tail.write_to(&mut actual).unwrap();
|
||||
|
||||
let mut old = Vec::new();
|
||||
let mut first = true;
|
||||
let format = if version < 3 { "drawcraft" } else { "vectorcraft" };
|
||||
old_field(&mut old, &mut first, pretty, "format", &format);
|
||||
old_field(&mut old, &mut first, pretty, "version", &version);
|
||||
old_field(&mut old, &mut first, pretty, "generator", &"VectorCraft test");
|
||||
old_field(&mut old, &mut first, pretty, "pdfCompatible", &true);
|
||||
old_field(&mut old, &mut first, pretty, "profiles", &profiles);
|
||||
old_field(&mut old, &mut first, pretty, "pdf", &pdf);
|
||||
old_field(&mut old, &mut first, pretty, "document", &document);
|
||||
old_field(&mut old, &mut first, pretty, "images", &images);
|
||||
old.extend_from_slice(if pretty { b"\n}" } else { b"}" });
|
||||
tail.write_to(&mut old).unwrap();
|
||||
assert_eq!(actual, old, "version {version}, pretty {pretty}");
|
||||
for bytes in [&old, &actual] {
|
||||
let mut gzip = GzEncoder::new(Vec::new(), Compression::default());
|
||||
gzip.write_all(bytes).unwrap();
|
||||
let mut decoded = Vec::new();
|
||||
GzDecoder::new(gzip.finish().unwrap().as_slice()).read_to_end(&mut decoded).unwrap();
|
||||
assert_eq!(decoded, old);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user