//! What `parse_op` does with an op off the wire: `{opId, receipt}`, every//! other field read out of the receipt by `cotton_store::wire::op`, and this//! build's one rule on top — the receipt's schema is this build's.usesuper::*;use cotton_store::canonical::{encode, Kind, Receipt};use cotton_store::protocol::types::OpKind;use serde_json::json;use std::collections::BTreeMap;fn receipt(kind: Kind, schema: u32) -> String {letmut fields = BTreeMap::new();if kind != Kind::Retract { fields.insert("title".to_string(), json!("buy cardamom")); }let bytes = encode(&Receipt { table: "cotton_todos".to_string(), row_id: "todo-1".to_string(), kind, stamp: "1700000000000:0000:device:test".to_string(), author: "device:test".to_string(), actor: "device-job:test".to_string(), schema, restamps: None, erase: None, undo: None, fields, }) .expect("a receipt encodes");String::from_utf8(bytes).expect("canonical bytes are UTF-8")}fn wire(kind: Kind, schema: u32) -> Value { json!({ "opId": "01J0000000000000000000000A", "receipt": receipt(kind, schema) })}#[test]fn parses_a_well_formed_op_out_of_its_receipt() {let parsed = parse_op(&wire(Kind::Create, SCHEMA_VERSION)).expect("a well-formed op parses"); assert_eq!(parsed.op_id, "01J0000000000000000000000A"); assert_eq!(parsed.kind, OpKind::Upsert); assert_eq!(parsed.table, "cotton_todos"); assert_eq!(parsed.row_id, "todo-1"); assert_eq!(parsed.device_id, "device:test"); assert_eq!(parsed.actor, "device-job:test"); assert_eq!(parsed.schema_version, SCHEMA_VERSION); assert_eq!(parsed.receipt, receipt(Kind::Create, SCHEMA_VERSION));}#[test]fn answers_a_reason_rather_than_throwing() { assert!(parse_op(&json!("nope")).is_err()); assert!(parse_op(&json!({ "opId": "", "receipt": receipt(Kind::Create, SCHEMA_VERSION) })).is_err()); assert!(parse_op(&json!({ "opId": "01J" })).is_err());// Bytes that are a receipt but not in its canonical form are not a write.let loose = receipt(Kind::Create, SCHEMA_VERSION).replacen('{', "{ ", 1); assert!(parse_op(&json!({ "opId": "01J", "receipt": loose })).is_err());}#[test]fn a_retract_carries_no_patch() {let parsed = parse_op(&wire(Kind::Retract, SCHEMA_VERSION)).expect("a retract parses"); assert_eq!(parsed.kind, OpKind::Delete); assert!(parsed.patch.is_none());}// RULE: A synced op is taken on the exchange's floor rule — a receipt minted at or above this build's floor (`move_intents::floor_of`) reaches the fold, because every rise between only added and a receipt above the file is the fold's to park as newer-shape or hold; one below the floor never enters the fold.#[test]fn takes_receipts_from_the_floor_up_and_refuses_below_it() {let floor = cotton_store::schema::move_intents::floor_of(SCHEMA_VERSION);for version in floor..=SCHEMA_VERSION + 1 { assert!(parse_op(&wire(Kind::Create, version)).is_ok(), "{version}"); }if floor > 0 { assert!(parse_op(&wire(Kind::Create, floor - 1)).is_err()); assert!(parse_op(&wire(Kind::Create, 0)).is_err()); }}