From 560b6f8268e2c5285c161be52dacfe877b08cf1b Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 15:02:41 +0000 Subject: [PATCH] Compare streaming native output with previous framing --- crates/vectorcraft-format/src/stream.rs | 4 ++ crates/vectorcraft-format/tests/stream.rs | 74 +++++++++++++++++++++++ 2 files changed, 78 insertions(+) diff --git a/crates/vectorcraft-format/src/stream.rs b/crates/vectorcraft-format/src/stream.rs index 16bc77ee8..2e8b0695e 100644 --- a/crates/vectorcraft-format/src/stream.rs +++ b/crates/vectorcraft-format/src/stream.rs @@ -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. diff --git a/crates/vectorcraft-format/tests/stream.rs b/crates/vectorcraft-format/tests/stream.rs index 35347ca2e..98f4685e2 100644 --- a/crates/vectorcraft-format/tests/stream.rs +++ b/crates/vectorcraft-format/tests/stream.rs @@ -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, 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); + } + } + } +}