Include merged source-loading and integer-coercion fixes

This commit is contained in:
Fredrik Hallin committed 2026-11-28 08:54:10 +01:00
commit 52d0fa85e3
4 files changed
+47 -12

No files matched your search

+22
View File
@@ -3,6 +3,28 @@ mod end_merge_tests {
use crate::headless::{Filesystem, Headless, RenderOptions};
use serde_json::json;
#[test]
fn linked_material_source_opens_as_sbs_with_a_mcraft_name() {
use materialcraft_engine::{Session, core::Package, file_cmds};
let dir = tempfile::tempdir().unwrap();
let mut source = Headless::new(Session::new(Package::new("Linked source")), Filesystem::TrustedLocal);
source.command_run("file.new", json!({"template": "empty"})).unwrap();
source.command_run("graph.node.add", json!({"def": "uniform"})).unwrap();
let xml = file_cmds::save_bytes(&source.session).unwrap();
let archive = file_cmds::save_files_at(&source.session, "archive.mcraft").unwrap().zip().unwrap();
for (name, bytes) in [("source.sbs", &xml), ("source.mcraft", &xml), ("archive.mcraft", &archive)] {
let path = dir.path().join(name);
std::fs::write(&path, bytes).unwrap();
let mut opened = Headless::new(Headless::default_session(), Filesystem::TrustedLocal);
opened.load(path.to_str().unwrap()).unwrap_or_else(|error| panic!("{name}: {error}"));
let graph = opened.session.graph().unwrap();
assert_eq!(graph.nodes.len(), 1, "{name}");
assert_eq!(graph.nodes[0].def, "uniform", "{name}");
}
}
#[cfg(unix)]
#[test]
fn trusted_reads_are_bounded_and_regular_files_only() {
+15 -11
View File
@@ -133,16 +133,7 @@ impl Value {
Value::Float4([a[0], a[1], a[2], a[3]])
}
Value::Color(_) => Value::Color(parse_color(j)?),
Value::Int(_) => Value::Int(match j.as_i64() {
Some(value) => i32::try_from(value).ok()?,
None => {
let value = j.as_f64()?.round();
if !value.is_finite() || value < i32::MIN as f64 || value > i32::MAX as f64 {
return None;
}
value as i32
}
}),
Value::Int(_) => Value::Int(parse_integer(j)?),
Value::Int2(_) => Value::Int2(parse_integers(j)?),
Value::Int3(_) => Value::Int3(parse_integers(j)?),
Value::Int4(_) => Value::Int4(parse_integers(j)?),
@@ -175,8 +166,21 @@ fn parse_integers<const COUNT: usize>(value: &serde_json::Value) -> Option<[i32;
let mut result = [0; COUNT];
for (target, component) in result.iter_mut().zip(components) {
*target = i32::try_from(component.as_i64()?).ok()?;
*target = parse_integer(component)?;
}
Some(result)
}
fn parse_integer(value: &serde_json::Value) -> Option<i32> {
match value.as_i64() {
Some(value) => i32::try_from(value).ok(),
None => {
let value = value.as_f64()?.round();
if !value.is_finite() || value < i32::MIN as f64 || value > i32::MAX as f64 {
return None;
}
Some(value as i32)
}
}
}
+8
View File
@@ -35,6 +35,14 @@ fn decimal_parameter_text_preserves_a_float_midpoint() {
assert_eq!(Value::Float2([0.0; 2]).coerce_json(&input), Some(Value::Float2([rounded, -rounded])));
}
#[test]
fn coerce_json_int_vectors_accept_floats() {
assert_eq!(Value::Int2([0; 2]).coerce_json(&serde_json::json!([3.0, 4.0])), Some(Value::Int2([3, 4])));
assert_eq!(Value::Int3([0; 3]).coerce_json(&serde_json::json!([1.0, 2, 8.6])), Some(Value::Int3([1, 2, 9])));
assert_eq!(Value::Int4([0; 4]).coerce_json(&serde_json::json!([1.0, 2.0, 3.0, 4.0])), Some(Value::Int4([1, 2, 3, 4])));
assert_eq!(Value::Int2([0; 2]).coerce_json(&serde_json::json!([1e20, 1.0])), None);
}
#[test]
fn gradient_interpolates() {
let g = Gradient::default();
+2 -1
View File
@@ -227,7 +227,8 @@ fn open_resolved(
}
pub(crate) fn read_package_bytes(bytes: &[u8], name: &str, linked: &dyn Fn(&str) -> Option<Vec<u8>>) -> Result<materialcraft_formats::sbs::SbsRead> {
if materialcraft_formats::project::is_archive(bytes) || is_mcraft(name) {
// Linked-material cooks can pass SBS XML under a .mcraft name.
if materialcraft_formats::project::is_archive(bytes) {
return Ok(materialcraft_formats::project::read(bytes, name)?);
}