Add an operator or generator
Both are short lists defined in Rust. The UI keeps a copy of each so it can suggest values as you type, and a test fails if the two copies differ. Always change both together.
| List | Rust | UI copy |
|---|---|---|
| Check operators | core/src/http/assertions.rs → ASSERTION_OPERATORS |
ui/src/domain/http.ts |
| Generators | core/src/tools/random.rs → GENERATORS |
ui/src/domain/generators.ts |
The test that compares them is core/tests/ui_contract.rs.
Add an operator
Section titled “Add an operator”This adds isUuid, which passes when a value is a UUID:
runtime: assertions: - expression: res.body.id operator: isUuid-
Add it to the list
Section titled “Add it to the list”In
core/src/http/assertions.rs, add the name toASSERTION_OPERATORSand increase the array length by one. The editor suggests operators in this order.core/src/http/assertions.rs pub const ASSERTION_OPERATORS: [&str; 29] = ["eq",// …"isFalsy","isUuid",]; -
Implement it
Section titled “Implement it”In the same file,
holds(operator, actual, expected)is amatchon the operator name. Add an arm:core/src/http/assertions.rs Ok(match operator {"eq" => actual.is_some_and(|a| loosely_equal(a, &value())),// …"isUuid" => actual.is_some_and(|a| a.as_str().is_some_and(is_uuid)),_ => return Err(format!("unsupported operator {operator:?}")),})The arguments:
actualOption<&Value>: what the expression selected, orNoneif it found nothingexpectedThe check’s value, as raw textReturn
Ok(true)orOk(false). ReturnErr(…)only whenexpecteditself is invalid, such as a bad regex.To compare values the same way the other operators do, use the helpers already in the file:
parse_expected(text → JSON value),as_number,plain_string,loosely_equal,length,pattern.Then write
is_uuid:core/src/http/assertions.rs fn is_uuid(text: &str) -> bool {let parts: Vec<&str> = text.split('-').collect();parts.iter().map(|p| p.len()).eq([8, 4, 4, 4, 12])&& parts.iter().all(|p| p.chars().all(|c| c.is_ascii_hexdigit()))} -
Add test cases
Section titled “Add test cases”Add cases to the
operatorstest at the bottom ofassertions.rs, then run:Terminal window cargo test -p cactus-core http::assertionsevery_listed_operator_is_checkedfails if a name is in the list but has nomatcharm. -
Copy it to the UI
Section titled “Copy it to the UI”Add
"isUuid"toASSERTION_OPERATORSinui/src/domain/http.ts, at the same position as in Rust.Operators whose names start with
isare treated as taking no value: the editor hides the value field and suggests no value. Name a value-less operatoris…; name an operator that takes a value anything else.Check:
Terminal window cargo test -p cactus-core --test ui_contractthe_editor_offers_every_operator_core_checkscompares the two lists. -
Document it
Section titled “Document it”Add a row to the operators table in Expressions and operators.
Add a generator
Section titled “Add a generator”Generators are values the set tool fills in when a value is exactly the generator’s name. This adds $randomHex:
- tool: set values: nonce: $randomHex-
Add it in Rust
Section titled “Add it in Rust”In
core/src/tools/random.rs, add the name toGENERATORSand an arm to thematchingenerate:core/src/tools/random.rs pub const GENERATORS: &[&str] = &["$uuid", "$timestamp", "$isoTime", "$randomInt", "$randomEmail", "$randomHex"];pub fn generate(name: &str) -> Option<String> {Some(match name {"$uuid" => uuid_v4(),"$timestamp" => now_unix_millis().to_string(),// …"$randomEmail" => format!("user-{}@example.com", &hex(&random_bytes::<8>())[..10]),"$randomHex" => hex(&random_bytes::<8>()),_ => return None,})}random_bytesreads the OS random source andhexformats bytes. Use them instead of adding a dependency. -
Test it
Section titled “Test it”Add a case to the
generatorstest in the same file:Terminal window cargo test -p cactus-core tools::random -
Copy it to the UI
Section titled “Copy it to the UI”Add
"$randomHex"toGENERATORSinui/src/domain/generators.ts, at the same position. Theseteditor suggests it once a value starts with$.Check:
Terminal window cargo test -p cactus-core --test ui_contractthe_set_tool_offers_every_generator_core_hascompares the two lists. -
Document it
Section titled “Document it”Add a row to the generators table in Flow tools → set.