Files
makeCAD/crates/engine/src/schema/render_library_tests.rs
T
austin62jake bec762fb2b Agents and scripts: the render library browsing commands describe their exact parameters and reply (#11443) (#11444)
* claim: #11443

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Render library: input and output schemas for List, Describe, Targets and Preview (#11443)

Describe, Targets and Preview read their strict parameter types from
schema/render_library.rs, which become their contracts. List keeps its two
readers (the physical profiles' takes availability: null, the other
catalogs' doesn't); its contract takes what either takes. The handlers build
LibraryPage, LibraryResource, LibraryTargets and LibraryPreview instead of
json! literals; a catalog resource is read into ResourceSummary through a
small Listed reader. The physical profiles' replies keep their JSON, which the
schemas describe.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Render library schema: the list says which availability each catalog takes

render.library.list has two readers: the physical profiles' takes
availability all, usable or null; the other catalogs' (and a call
without kind, which lists surfaces) take all, usable, specification or
blocked, but not null. The schema now says so with an anyOf on kind.
The type still takes what either reader takes: the readers check the
query and the page before availability, so refusing it in the type
would change which message a call with two mistakes gets in strict.
A new test checks that the schema takes ten calls exactly when the
handler does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-04 14:50:44 +00:00

146 lines
7.0 KiB
Rust

use serde_json::{Value, json};
use super::{
LibraryDescribeParams, LibraryDescription, LibraryListParams, LibraryPage, LibraryPreview, LibraryPreviewParams, LibraryTargets,
LibraryTargetsParams,
};
use crate::Session;
use crate::schema::tests::{assert_keys, assert_strict_keeps_todays_errors, conforms};
use crate::schema::{ParamCheck, input_schema_for, output_schema_for};
#[test]
fn schemas_and_the_key_check_agree() {
assert_keys(input_schema_for::<LibraryListParams>(), &["availability", "kind", "limit", "offset", "query"]);
assert_keys(input_schema_for::<LibraryDescribeParams>(), &["id", "kind"]);
assert_keys(input_schema_for::<LibraryTargetsParams>(), &["limit", "offset", "query"]);
assert_keys(input_schema_for::<LibraryPreviewParams>(), &["id"]);
}
/// The list's schema takes an `availability` exactly when the reader for its `kind` does: the
/// physical profiles' takes `null` but not `specification` or `blocked`, the others' the reverse.
#[test]
fn the_list_schema_takes_each_readers_availability() {
let schema = input_schema_for::<LibraryListParams>();
let schema = schema.as_value();
let calls = [
json!({}),
json!({"availability": "blocked"}),
json!({"availability": null}),
json!({"kind": "hdr", "availability": "specification"}),
json!({"kind": "hdr", "availability": null}),
json!({"kind": "physical_profiles"}),
json!({"kind": "physical_profiles", "availability": "usable"}),
json!({"kind": "physical_profiles", "availability": null}),
json!({"kind": "physical_profiles", "availability": "blocked"}),
json!({"kind": "physical_profiles", "availability": "specification"}),
];
for p in calls {
let handled = Session::default().execute("render.library.list", &p).is_ok();
assert_eq!(conforms(schema, schema, &p), handled, "{p}");
}
}
#[test]
fn strict_keeps_todays_errors() {
let long = "x".repeat(257);
assert_strict_keeps_todays_errors(&[
("render.library.list", json!({"kind": "not-a-kind"})),
("render.library.list", json!({"query": long})),
("render.library.list", json!({"query": "a\u{7}b"})),
("render.library.list", json!({"limit": 0})),
("render.library.list", json!({"limit": 129})),
("render.library.list", json!({"offset": 4097})),
("render.library.list", json!({"availability": "sometimes"})),
("render.library.list", json!({"availability": null})),
("render.library.list", json!({"kind": "physical_profiles", "availability": "blocked"})),
("render.library.list", json!({"kind": "physical_profiles", "limit": 0})),
("render.library.describe", json!({"kind": "not-a-kind", "id": "MAT-001"})),
("render.library.describe", json!({"kind": "materials", "id": "MAT 001"})),
("render.library.describe", json!({"kind": "materials", "id": "MAT-999"})),
("render.library.describe", json!({"kind": "physical_profiles", "id": "no-such-profile"})),
("render.library.targets", json!({"limit": 0})),
("render.library.targets", json!({"limit": 129})),
("render.library.targets", json!({"query": long})),
("render.library.targets", json!({"offset": 262_145})),
("render.library.preview", json!({"id": "MAT-001"})),
("render.library.preview", json!({"id": ""})),
]);
}
/// #11443: the four commands reply as their output schemas say, in Warn without warnings. Every
/// resource of every catalog, physical profiles included, is listed through its pages, and the
/// first of each is described. Like the handlers, warn and strict refuse a misspelt key.
#[test]
fn render_library_commands_reply_with_their_schemas() {
let (page, described, targets, preview) = (
output_schema_for::<LibraryPage>(),
output_schema_for::<LibraryDescription>(),
output_schema_for::<LibraryTargets>(),
output_schema_for::<LibraryPreview>(),
);
let mut s = Session { param_check: ParamCheck::Warn, ..Session::default() };
let run = |s: &mut Session, id: &str, p: Value| s.execute(id, &p).unwrap_or_else(|e| panic!("{id} {p}: {e}"));
let matches = |schema: &schemars::Schema, r: &Value| {
let schema = schema.as_value();
assert!(conforms(schema, schema, r), "{r} doesn't match {schema}");
assert!(r.get("param_warnings").is_none(), "{r}");
};
let kinds = [
"capabilities",
"environments",
"hdr",
"lighting",
"materials",
"native_scenes",
"physical_profiles",
"quality",
"rigs",
"scenes",
"selene_appearances",
"selene_shots",
"surface_manifest",
"surfaces",
];
for kind in kinds {
let mut offset = Some(0);
let mut first = None;
let mut listed = 0;
while let Some(at) = offset {
let r = run(&mut s, "render.library.list", json!({"kind": kind, "offset": at, "limit": 128}));
matches(&page, &r);
let items = r["items"].as_array().cloned().unwrap_or_default();
first = first.or_else(|| items.first().map(|i| i["id"].clone()));
listed += items.len();
offset = r["next_offset"].as_u64();
}
assert!(listed > 0, "{kind} lists nothing");
let id = first.unwrap_or_else(|| panic!("no first {kind}"));
let r = run(&mut s, "render.library.describe", json!({"kind": kind, "id": id}));
matches(&described, &r);
assert_eq!(r["physical_references"].is_object(), matches!(kind, "materials" | "surfaces"), "{kind}: {r}");
}
matches(&page, &run(&mut s, "render.library.list", json!({"query": "brushed", "availability": "usable", "offset": 0, "limit": 5})));
matches(&page, &run(&mut s, "render.library.list", json!({"kind": "physical_profiles", "availability": null})));
let empty = run(&mut s, "render.library.targets", json!({}));
matches(&targets, &empty);
assert!(empty["bounds"].is_null(), "{empty}");
run(&mut s, "solid.box", json!({"length": 10, "width": 20, "height": 5, "body_name": "Target"}));
run(
&mut s,
"appearance.assign",
json!({"bodies": ["Target"], "color": [180, 120, 70], "pbr": {"metallic": 1.0, "roughness": 0.3, "dielectric_ior": 1.5}}),
);
let r = run(&mut s, "render.library.targets", json!({"query": "targ", "offset": 0, "limit": 1}));
matches(&targets, &r);
assert_eq!((r["total"].as_u64(), r["items"][0]["name"].as_str()), (Some(1), Some("Target")), "{r}");
assert!(r["items"][0]["pbr"].is_object() && r["bounds"].is_object(), "{r}");
if cfg!(feature = "bundled-render-library") {
matches(&preview, &run(&mut s, "render.library.preview", json!({"id": "SURF-001"})));
}
for level in [ParamCheck::Warn, ParamCheck::Strict] {
let mut s = Session { param_check: level, ..Session::default() };
let error = s.execute("render.library.list", &json!({"knd": "hdr"})).err().map(|e| e.to_string()).unwrap_or_default();
assert!(error.contains("knd"), "{level:?}: {error}");
}
}