Skip to main content

tightbeam/testing/
assertions.rs

1//! Assertion primitives for testing framework
2//!
3//! This module provides core assertion types and contracts used by the
4//! testing framework. All runtime recording now uses `TraceCollector`
5//! for async-safe, explicit trace collection.
6
7#[cfg(not(feature = "std"))]
8extern crate alloc;
9#[cfg(not(feature = "std"))]
10use alloc::{borrow::Cow, string::String, vec::Vec};
11
12#[cfg(feature = "std")]
13use std::borrow::Cow;
14
15use crate::asn1::{MessagePriority, Version};
16use crate::testing::macros::Cardinality;
17
18#[cfg(feature = "policy")]
19use crate::policy::TransitStatus;
20
21#[derive(Clone, Debug, PartialEq, Eq, Hash)]
22pub enum AssertionLabel {
23	Custom(Cow<'static, str>),
24}
25
26impl AssertionLabel {
27	/// Check if this label matches another, supporting tightbeam URN shorthand.
28	///
29	/// Returns true if:
30	/// - Labels are exactly equal, OR
31	/// - `self` (recorded) ends with `/{other}` (shorthand)
32	///
33	/// This allows specs to use shorthand like `"create_frame_start"` to match
34	/// recorded URNs like `"urn:tightbeam:instrumentation:event/create_frame_start"`.
35	pub fn matches(&self, other: &AssertionLabel) -> bool {
36		if self == other {
37			return true;
38		}
39
40		// Try shorthand matching: recorded ends with /{expected}
41		let (Self::Custom(recorded), Self::Custom(expected)) = (self, other);
42		let pattern = ["/", expected.as_ref()].concat();
43		recorded.ends_with(&pattern)
44	}
45}
46
47/// Type-safe wrapper for assertion values supporting PartialEq comparison
48#[derive(Debug, Clone)]
49pub enum AssertionValue {
50	String(String),
51	Bool(bool),
52	U8(u8),
53	U32(u32),
54	U64(u64),
55	I32(i32),
56	I64(i64),
57	F64(f64), // f64 implements PartialOrd and PartialEq — not Ord or Eq
58	MessagePriority(MessagePriority),
59	Version(Version),
60	Some(Box<AssertionValue>),
61	IsNone,
62	IsSome,
63	RatioActual(u64, u64),
64	RatioLimit(u64, u64),
65	#[cfg(feature = "policy")]
66	TransitStatus(TransitStatus),
67}
68
69impl PartialEq for AssertionValue {
70	fn eq(&self, other: &Self) -> bool {
71		match (self, other) {
72			// IsSome matches any Some(_) value
73			(Self::IsSome, Self::Some(_)) => true,
74			(Self::Some(_), Self::IsSome) => true,
75			// IsSome matches IsSome (both represent "some value exists")
76			(Self::IsSome, Self::IsSome) => true,
77			// IsSome does not match None
78			(Self::IsSome, Self::IsNone) => false,
79			(Self::IsNone, Self::IsSome) => false,
80			// Standard comparisons for other variants
81			(Self::String(a), Self::String(b)) => a == b,
82			(Self::Bool(a), Self::Bool(b)) => a == b,
83			(Self::U8(a), Self::U8(b)) => a == b,
84			(Self::U32(a), Self::U32(b)) => a == b,
85			(Self::U64(a), Self::U64(b)) => a == b,
86			(Self::I32(a), Self::I32(b)) => a == b,
87			(Self::I64(a), Self::I64(b)) => a == b,
88			(Self::F64(a), Self::F64(b)) => a == b,
89			(Self::MessagePriority(a), Self::MessagePriority(b)) => a == b,
90			(Self::Version(a), Self::Version(b)) => a == b,
91			(Self::Some(a), Self::Some(b)) => a == b,
92			(Self::IsNone, Self::IsNone) => true,
93			(Self::RatioActual(an, ad), Self::RatioActual(bn, bd)) => ratio_equal(*an, *ad, *bn, *bd),
94			(Self::RatioLimit(an, ad), Self::RatioLimit(bn, bd)) => ratio_equal(*an, *ad, *bn, *bd),
95			(Self::RatioActual(an, ad), Self::RatioLimit(bn, bd)) => ratio_less_equal(*an, *ad, *bn, *bd),
96			(Self::RatioLimit(an, ad), Self::RatioActual(bn, bd)) => ratio_less_equal(*bn, *bd, *an, *ad),
97			#[cfg(feature = "policy")]
98			(Self::TransitStatus(a), Self::TransitStatus(b)) => a == b,
99			_ => false,
100		}
101	}
102}
103
104fn ratio_equal(an: u64, ad: u64, bn: u64, bd: u64) -> bool {
105	if ad == 0 || bd == 0 {
106		return false;
107	}
108	an.saturating_mul(bd) == bn.saturating_mul(ad)
109}
110
111fn ratio_less_equal(an: u64, ad: u64, bn: u64, bd: u64) -> bool {
112	if ad == 0 || bd == 0 {
113		return false;
114	}
115	an.saturating_mul(bd) <= bn.saturating_mul(ad)
116}
117
118// From implementations for ergonomic conversion
119impl From<String> for AssertionValue {
120	fn from(s: String) -> Self {
121		Self::String(s)
122	}
123}
124
125impl From<&str> for AssertionValue {
126	fn from(s: &str) -> Self {
127		Self::String(s.to_string())
128	}
129}
130
131impl From<bool> for AssertionValue {
132	fn from(b: bool) -> Self {
133		Self::Bool(b)
134	}
135}
136
137impl From<u8> for AssertionValue {
138	fn from(n: u8) -> Self {
139		Self::U8(n)
140	}
141}
142
143impl From<u32> for AssertionValue {
144	fn from(n: u32) -> Self {
145		Self::U32(n)
146	}
147}
148
149impl From<u64> for AssertionValue {
150	fn from(n: u64) -> Self {
151		Self::U64(n)
152	}
153}
154
155impl From<i32> for AssertionValue {
156	fn from(n: i32) -> Self {
157		Self::I32(n)
158	}
159}
160
161impl From<i64> for AssertionValue {
162	fn from(n: i64) -> Self {
163		Self::I64(n)
164	}
165}
166
167impl From<MessagePriority> for AssertionValue {
168	fn from(p: MessagePriority) -> Self {
169		Self::MessagePriority(p)
170	}
171}
172
173impl From<Version> for AssertionValue {
174	fn from(v: Version) -> Self {
175		Self::Version(v)
176	}
177}
178
179#[cfg(feature = "policy")]
180impl From<TransitStatus> for AssertionValue {
181	fn from(status: TransitStatus) -> Self {
182		Self::TransitStatus(status)
183	}
184}
185
186// Option support - convert Some(x) to Some(Box<AssertionValue>) and None to None
187impl<T> From<Option<T>> for AssertionValue
188where
189	T: Into<AssertionValue>,
190{
191	fn from(opt: Option<T>) -> Self {
192		match opt {
193			Some(val) => Self::Some(Box::new(val.into())),
194			None => Self::IsNone,
195		}
196	}
197}
198
199/// Marker type for asserting that an Option is Some(_) without checking the inner value
200/// Use with `equals!(IsSome)` in assertion specs.
201#[derive(Debug, Clone, Copy, PartialEq, Eq)]
202pub struct IsSome;
203
204impl From<IsSome> for AssertionValue {
205	fn from(_: IsSome) -> Self {
206		Self::IsSome
207	}
208}
209
210/// Marker type for asserting that an Option is None
211/// Use with `equals!(IsNone)` in assertion specs.
212#[derive(Debug, Clone, Copy, PartialEq, Eq)]
213pub struct IsNone;
214
215impl From<IsNone> for AssertionValue {
216	fn from(_: IsNone) -> Self {
217		Self::IsNone
218	}
219}
220
221#[derive(Debug, Clone, Copy, PartialEq, Eq)]
222pub enum Presence {
223	Present,
224	Absent,
225}
226
227impl Presence {
228	pub fn of_option<T>(opt: &Option<T>) -> Self {
229		if opt.is_some() {
230			Self::Present
231		} else {
232			Self::Absent
233		}
234	}
235}
236
237impl From<Presence> for AssertionValue {
238	fn from(presence: Presence) -> Self {
239		match presence {
240			Presence::Present => AssertionValue::IsSome,
241			Presence::Absent => AssertionValue::IsNone,
242		}
243	}
244}
245
246#[derive(Debug, Clone, Copy, PartialEq, Eq)]
247pub struct RatioLimit(pub u64, pub u64);
248
249impl From<(u64, u64)> for AssertionValue {
250	fn from(pair: (u64, u64)) -> Self {
251		Self::RatioActual(pair.0, pair.1)
252	}
253}
254
255impl From<RatioLimit> for AssertionValue {
256	fn from(limit: RatioLimit) -> Self {
257		Self::RatioLimit(limit.0, limit.1)
258	}
259}
260
261#[derive(Clone, Debug)]
262pub struct Assertion {
263	pub seq: usize,
264	pub label: AssertionLabel,
265	pub tags: Vec<&'static str>,
266	pub payload_hash: Option<[u8; 32]>,
267	pub value: Option<AssertionValue>,
268}
269
270impl Assertion {
271	pub fn new(seq: usize, label: AssertionLabel, tags: Vec<&'static str>, payload_hash: Option<[u8; 32]>) -> Self {
272		Self { seq, label, tags, payload_hash, value: None }
273	}
274
275	pub fn with_value(
276		seq: usize,
277		label: AssertionLabel,
278		tags: Vec<&'static str>,
279		payload_hash: Option<[u8; 32]>,
280		value: AssertionValue,
281	) -> Self {
282		Self { seq, label, tags, payload_hash, value: Some(value) }
283	}
284}
285
286#[derive(Clone, Debug)]
287pub struct AssertionContract {
288	pub label: AssertionLabel,
289	pub tag_filter: Option<Vec<&'static str>>,
290	pub cardinality: Cardinality,
291	pub expected_value: Option<AssertionValue>,
292}
293
294impl AssertionContract {
295	pub fn new(label: AssertionLabel, cardinality: Cardinality) -> Self {
296		Self { label, tag_filter: None, cardinality, expected_value: None }
297	}
298
299	pub fn with_tag_filter(mut self, tags: Vec<&'static str>) -> Self {
300		self.tag_filter = Some(tags);
301		self
302	}
303
304	pub fn with_value(mut self, expected_value: AssertionValue) -> Self {
305		self.expected_value = Some(expected_value);
306		self
307	}
308
309	pub fn is_satisfied_by(&self, assertions: &[Assertion]) -> bool {
310		let matching: Vec<_> = assertions
311			.iter()
312			.filter(|a| {
313				// Match label (supports tightbeam URN shorthand)
314				if !a.label.matches(&self.label) {
315					return false;
316				}
317
318				// Tag matching: if spec has tag_filter, assertion must have all those tags
319				if let Some(ref filter_tags) = self.tag_filter {
320					for filter_tag in filter_tags {
321						if !a.tags.contains(filter_tag) {
322							return false;
323						}
324					}
325				}
326
327				true
328			})
329			.collect();
330
331		// Check cardinality
332		if !self.cardinality.is_satisfied_by(matching.len()) {
333			return false;
334		}
335
336		// Check value constraint if present
337		if let Some(ref expected) = self.expected_value {
338			// All matching assertions must have the expected value
339			matching.iter().all(|a| a.value.as_ref() == Some(expected))
340		} else {
341			true
342		}
343	}
344
345	pub fn describe(&self) -> String {
346		let cardinality_desc = self.cardinality.describe();
347		let tag_desc = if let Some(ref tags) = self.tag_filter {
348			format!(" with tags {tags:?}")
349		} else {
350			String::new()
351		};
352		if let Some(ref expected) = self.expected_value {
353			format!("{cardinality_desc} with value {expected:?}{tag_desc}")
354		} else {
355			format!("{cardinality_desc}{tag_desc}")
356		}
357	}
358}
359
360// Re-export cardinality functions via nested module for legacy calls
361pub mod cardinality {
362	pub use crate::testing::macros::{absent, at_least, at_most, between, exactly, present};
363}