Thing/crates/thing-contract/tests/common/mod.rs
Meta Vens 9ed4a72a53 thing-contract: TextInputNode's slot 2 is input_kind in every runtime; the nil vector
Rename kind to input_kind at key 2 (the wire is the slot key; no golden byte moves).
nil_positions.rs walks every array element of every value golden, splices nil over it and
records whether the Rust decode door accepts it, into goldens/nil-positions.tsv. The tsv is
blessed by the next commit; this one builds, and its nil test fails until then.
golden_bytes, blessing and goldens_dir move to tests/common so both test files share one path.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 02:14:35 +02:00

167 lines
5 KiB
Rust

//! Constructors for the values the goldens and the other tests share. Every value is built
//! through the crate's public types; no bytes are spelled here.
#![allow(dead_code)]
use std::path::PathBuf;
use thing_contract::*;
pub fn goldens_dir() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/goldens")
}
pub fn blessing() -> bool {
std::env::var("THING_BLESS").as_deref() == Ok("1")
}
/// Bless the file or compare against it. Returns the committed bytes.
pub fn golden_bytes(file: &str, produced: &[u8]) -> Vec<u8> {
let path = goldens_dir().join(file);
if blessing() {
std::fs::write(&path, produced).unwrap_or_else(|error| panic!("{file}: {error}"));
}
let committed = std::fs::read(&path).unwrap_or_else(|error| panic!("{file}: {error}"));
assert_eq!(committed, produced, "{file}: the committed golden differs from what the crate writes");
committed
}
pub fn seq(value: i32) -> Seq {
Seq::new(value).unwrap()
}
pub fn ver(value: i64) -> Version {
Version::new(value).unwrap()
}
pub fn count(value: i32) -> Count {
Count::new(value).unwrap()
}
pub fn seg(value: &str) -> Segment {
Segment::new(value).unwrap()
}
pub fn nid(id: &str) -> NodeId {
NodeId::new(id)
}
pub fn lit_text(value: &str) -> TextSource {
TextSource::Literal(TextLiteral { value: value.into() })
}
pub fn lit_bool(value: bool) -> BoolSource {
BoolSource::Literal(BoolLiteral { value })
}
pub fn lit_number(value: f64) -> NumberSource {
NumberSource::Literal(NumberLiteral { value })
}
pub fn param(name: &str) -> ParamName {
ParamName { name: name.into() }
}
pub fn path(names: &[&str]) -> MemberPath {
MemberPath { steps: names.iter().map(|name| MemberStep::Name(MemberName { name: (*name).into() })).collect() }
}
pub fn record_bind(member: MemberPath, format: Option<Format>) -> Bind {
Bind {
source: StateSource::Record(StateRef {
provider: Some(ProviderId::new("provider.a")),
schema: SchemaId::new("gamecult.thing.test.v1"),
key: RecordKey::new("row-1"),
}),
member,
format,
}
}
pub fn item_bind(member: MemberPath) -> Bind {
Bind { source: StateSource::Item, member, format: None }
}
pub fn command(id: &str) -> CommandRef {
CommandRef::Id(CommandIdRef { id: CommandId::new(id) })
}
pub fn common(id: &str) -> NodeCommon {
NodeCommon { id: nid(id), visible: lit_bool(true), a11y_label: None }
}
pub fn px(value: f64) -> Dimension {
Dimension::Px(Scalar { value })
}
pub fn size2(width: Dimension, height: Dimension) -> Size2 {
Size2 { width, height }
}
pub fn edges(all: Dimension) -> Edges {
Edges { top: all.clone(), right: all.clone(), bottom: all.clone(), left: all }
}
pub fn style() -> BoxStyle {
BoxStyle {
display: Display::Flex,
direction: Direction::Column,
wrap: Wrap::NoWrap,
gap: size2(px(4.0), px(8.0)),
padding: edges(px(2.0)),
margin: edges(Dimension::Auto),
size: size2(Dimension::Percent(Scalar { value: 100.0 }), Dimension::Content),
min_size: size2(px(0.0), px(0.0)),
max_size: size2(Dimension::Auto, Dimension::Auto),
position: Position::Relative,
inset: edges(Dimension::Auto),
overflow: Overflow::Visible,
align_items: ItemAlign::Stretch,
align_self: ItemAlign::Auto,
align_content: ContentAlign::Start,
justify_content: ContentAlign::SpaceBetween,
grow: 1.0,
shrink: 0.0,
basis: Dimension::Auto,
grid_columns: vec![],
grid_rows: vec![],
}
}
pub fn plain_paint() -> Paint {
Paint { background: None, border: None, radius: 0.0 }
}
pub fn boxed(id: &str, children: Vec<Node>) -> Node {
Node::Box(BoxNode { common: common(id), style: style(), paint: plain_paint(), role: Role::Group, children })
}
pub fn text(id: &str, content: &str) -> Node {
Node::Text(TextNode {
common: common(id),
content: lit_text(content),
style_token: "body".into(),
wrap: true,
max_lines: None,
selectable: false,
})
}
pub fn invocation() -> CommandInvocation {
CommandInvocation {
invocation_id: "3f2b8c1e-5d44-4a6b-9a07-1c2d3e4f5a6b".into(),
surface_key: "thing:surface:provider.a.main".into(),
surface_version: ver(7),
command: CommandId::new("go"),
payload: RecordPayload { schema: SchemaId::new("gamecult.thing.test.go.v1"), bytes: vec![0x91, 0x01, 0xff, 0x00] },
captured: vec![
CapturedInput { node: nid("name-input"), value: InputValue::Text(TextValue { value: "Ada".into() }) },
CapturedInput { node: nid("level"), value: InputValue::Number(NumberValue { value: 0.75 }) },
CapturedInput { node: nid("flag"), value: InputValue::Bool(BoolValue { value: true }) },
CapturedInput { node: nid("pick"), value: InputValue::Choice(ChoiceValue { value: "a".into() }) },
],
client: "renderer.web".into(),
issued_at: "2026-01-01T00:00:00Z".into(),
delegation: Some("delegate-1".into()),
}
}