1use spate_core::checkpoint::AckRef;
6use spate_core::config::ComponentConfig;
7use spate_core::record::{PartitionId, RawPayload, Record, RecordMeta};
8use std::sync::atomic::{AtomicU64, Ordering};
9
10#[must_use]
15pub fn test_ack() -> AckRef {
16 AckRef::test_pair().0
17}
18
19#[must_use]
21pub fn record<T>(payload: T) -> Record<T> {
22 Record {
23 payload,
24 meta: RecordMeta {
25 partition: PartitionId(0),
26 offset: 0,
27 event_time_ms: 0,
28 key_hash: None,
29 },
30 ack: test_ack(),
31 }
32}
33
34#[must_use]
36pub fn raw_payload(bytes: &[u8]) -> RawPayload<'_> {
37 RawPayload {
38 bytes,
39 key: None,
40 partition: PartitionId(0),
41 offset: 0,
42 timestamp_ms: 0,
43 }
44}
45
46#[must_use]
54pub fn component_config(type_tag: &str, yaml: &str) -> ComponentConfig {
55 let raw = serde_yaml::from_str(yaml)
56 .unwrap_or_else(|e| panic!("component config for {type_tag:?} does not parse: {e}"));
57 ComponentConfig::new(type_tag, raw)
58}
59
60#[must_use]
65pub fn unique_name(prefix: &str) -> String {
66 static NEXT: AtomicU64 = AtomicU64::new(0);
67 format!("{prefix}-{}", NEXT.fetch_add(1, Ordering::Relaxed))
68}
69
70#[cfg(test)]
71mod tests {
72 use super::*;
73
74 #[test]
75 fn unique_name_never_repeats() {
76 let a = unique_name("p");
77 let b = unique_name("p");
78 assert_ne!(a, b);
79 assert!(a.starts_with("p-") && b.starts_with("p-"));
80 }
81
82 #[test]
85 fn component_config_takes_an_indented_body() {
86 assert_eq!(
87 component_config("t", " brokers: a\n topic: b\n"),
88 component_config("t", "brokers: a\ntopic: b\n"),
89 );
90 assert_eq!(component_config("t", " {}\n").type_tag(), "t");
91 }
92}