5. Test your TEE contract - Terminal 3 Documentation

Documentation Index

Fetch the complete documentation index at: /llms.txt

Use this file to discover all available pages before exploring further.

**Unit tests (Rust):**Test the business-logic guards on the native target with cargo test. Each function takes the raw input bytes and returns Result<Vec<u8>, String>, so you call it directly and assert on the error string — there is no ContractError enum or test harness to set up. Functions that call the host (http, kv-store) only run on wasm32; natively they return an error, so unit tests focus on input parsing and validation.

#[cfg(test)]
mod tests {
    use super::*;

#[test]
    fn book_offer_rejects_inline_pii() {
        // book-offer deserialises into a struct with only opaque ids + amount,
        // so a payload that tries to smuggle passenger PII fails at parse time.
        let input = serde_json::to_vec(&serde_json::json!({
            "offer_id": "off_1",
            "passengers": [{ "given_name": "Jane" }],   // not a valid field
            "total_amount": "199.00", "total_currency": "GBP",
        }))
        .unwrap();
        let err = book_offer(&input).unwrap_err();
        assert!(err.contains("bad input"));
    }

#[test]
    fn book_offer_rejects_non_json() {
        assert!(book_offer(b"not json").unwrap_err().contains("bad input"));
    }
}

Test checklist: