//! The `cite` example's retrieval is illustrative — no document is fetched — //! so the cost it is decided and sealed at must be an estimate, never a //! hardware reading. Checked on the cost the example builds (the example //! file is compiled in here as a module) and on the `jcp` receipt sealed //! over it, decoded from the signed core. #[allow(dead_code)] #[path = "../examples/cite.rs"] mod cite; use ed25519_dalek::SigningKey; use jcp_cite::{Citation, Source}; use jcp_grant::issue; use jcp_runtime::Runtime; use jcp_schema::{Capability, ContentId, Did, Grant, Provenance}; #[test] fn the_cite_examples_retrieval_is_sealed_as_an_estimate() { let issuer = SigningKey::from_bytes(&[1u8; 32]); let agent = Did::from_verifying_key(&SigningKey::from_bytes(&[2u8; 32]).verifying_key()); let corpus = ContentId::of_bytes(b"acme-workspace"); let citation = Citation::new( agent, ContentId::of_bytes(b"answer"), vec![Source::new(ContentId::of_bytes(b"doc"), ContentId::of_bytes(b"grant:finance"))], 4_200, [7; 16], ); let cost = cite::retrieval_cost(&citation); eprintln!("retrieval cost: {} µJ [{}] measured {:?}", cost.uj(), cost.provenance.jcr1_label(), cost.measured_uj); assert_eq!(cost.provenance, Provenance::Estimator, "an illustrative retrieval graded {:?}", cost.provenance); assert_eq!(cost.measured_uj, None, "a measured figure nothing read"); assert_eq!(cost.uj(), citation.energy_uj, "the receipt's cost is the citation's figure"); let grant = Grant { issuer: Did::from_verifying_key(&issuer.verifying_key()), subject: agent, capability: Capability::new(corpus, ["retrieve"]), budget_uj: 1_000_000, budget_funds: None, caveats: Vec::new(), expiry: 0, parent: None, delegate_depth: None, nonce: [0; 16], }; let sg = issue(&issuer, grant.clone()).expect("issue"); let frame = citation.frame(grant.id(), corpus).expect("frame"); let decision = Runtime::new().decide(&frame, &sg, agent, cost, 1); assert!(decision.is_allow(), "{:?}", decision.verdict); let steward = SigningKey::from_bytes(&[9u8; 32]); let env = jcp_receipt::seal(&frame, &grant, &decision, &steward, "did:example:steward").expect("seal"); let core = jcp_receipt::jcr1::verify_cose_sign1(&env, "did:example:steward", &steward.verifying_key().to_bytes()).expect("verifies"); let sealed: serde_json::Value = jcp_schema::from_canonical_cbor(&core).expect("decodes"); eprintln!("sealed: cost_uj {} [{}]", sealed["cost_uj"], sealed["energy_provenance"]); assert_eq!(sealed["energy_provenance"].as_str(), Some("Estimator"), "{sealed}"); }