commerce.rs (27206B)
1 use super::agreement::{buyer, group, line, money, seller, terms, window}; 2 use radroots_domain::*; 3 4 fn record(id: u8) -> RecordId { 5 RecordId::from_bytes([id; 32]) 6 } 7 fn source(id: u8) -> SourceId { 8 SourceId::from_bytes([id; 32]) 9 } 10 fn time(value: u64) -> UnixSeconds { 11 UnixSeconds::new(value).unwrap() 12 } 13 fn seq(value: u64) -> Sequence { 14 Sequence::new(value).unwrap() 15 } 16 fn authority() -> AuthorityRef { 17 AuthorityRef::new( 18 ExecutorId::from_bytes([20; 32]), 19 Generation::new(2).unwrap(), 20 ) 21 } 22 fn digest() -> TermsDigest { 23 TermsDigest::from_bytes([21; 32]) 24 } 25 fn private_terms() -> Terms { 26 terms(vec![line(7, 9)], vec![group(9)], money("50"), money("700")).unwrap() 27 } 28 fn quantities() -> Vec<LineQuantity> { 29 vec![LineQuantity::new( 30 AgreementLineId::from_bytes([7; 32]), 31 "2".parse().unwrap(), 32 )] 33 } 34 fn measurement() -> FinalMeasurement { 35 FinalMeasurement::new( 36 AgreementLineId::from_bytes([7; 32]), 37 Quantity::new("1".parse().unwrap(), listing::MeasurementUnit::Kilogram).unwrap(), 38 money("325"), 39 ) 40 } 41 fn parents() -> Vec<SourceId> { 42 [3, 10, 11, 12, 13, 14, 15, 16].map(source).to_vec() 43 } 44 fn header(author: PublicKey, order: Option<OrderId>, parents: Vec<SourceId>) -> CommerceHeader { 45 CommerceHeader::new( 46 ThreadId::from_bytes([1; 32]), 47 order, 48 author, 49 buyer(), 50 seller(), 51 time(100), 52 seq(1), 53 None, 54 parents, 55 ) 56 .unwrap() 57 } 58 fn order() -> Option<OrderId> { 59 Some(OrderId::from_bytes([2; 32])) 60 } 61 fn issue() -> IssueReport { 62 IssueReport::new( 63 IssueCode::Shortage, 64 "Private issue", 65 quantities(), 66 vec![record(10)], 67 ) 68 .unwrap() 69 } 70 fn resolution() -> Resolution { 71 Resolution::new( 72 vec![record(11)], 73 vec![record(12)], 74 quantities(), 75 "Private resolution", 76 ) 77 .unwrap() 78 } 79 80 // Expected roles come from the operation contract, independently of the 81 // production role matcher: bit1 seller, bit2 buyer, both requires later 82 // authenticated parent-relative counterparty checks. 83 fn cases() -> Vec<(CommercePayload, &'static str, u8)> { 84 let quote = TermsQuote::new(private_terms(), time(200), Some(source(10))); 85 assert_eq!(quote.terms(), &private_terms()); 86 assert_eq!(quote.expires_at(), time(200)); 87 assert_eq!(quote.in_reply_to(), Some(source(10))); 88 let request = TermsRequest::new(private_terms(), time(201), Some(record(10))); 89 assert_eq!(request.terms(), &private_terms()); 90 assert_eq!(request.expires_at(), time(201)); 91 assert_eq!(request.quote(), Some(record(10))); 92 let reason = RecordReason::new(record(10), "Private reason 🥕").unwrap(); 93 assert_eq!(reason.target(), record(10)); 94 assert_eq!(reason.reason(), "Private reason 🥕"); 95 let proposal = TermsProposal::new( 96 Some(record(11)), 97 private_terms(), 98 time(202), 99 "Private proposal", 100 ) 101 .unwrap(); 102 assert_eq!(proposal.base_agreement(), Some(record(11))); 103 assert_eq!(proposal.terms(), &private_terms()); 104 assert_eq!(proposal.expires_at(), time(202)); 105 assert_eq!(proposal.reason(), "Private proposal"); 106 let authorized = TermsAuthorization::new(record(10), digest(), time(203)); 107 assert_eq!(authorized.proposal(), record(10)); 108 assert_eq!(authorized.terms_digest(), digest()); 109 assert_eq!(authorized.expires_at(), time(203)); 110 let committed = 111 AgreementCommitment::new(Some(record(11)), record(10), digest(), authority(), seq(7)) 112 .unwrap(); 113 assert_eq!(committed.base_agreement(), Some(record(11))); 114 assert_eq!(committed.authorization(), record(10)); 115 assert_eq!(committed.terms_digest(), digest()); 116 assert_eq!(committed.authority(), authority()); 117 assert_eq!(committed.seller_sequence(), seq(7)); 118 let acknowledged = WithdrawalAcknowledgement::new(record(10), seq(8)).unwrap(); 119 assert_eq!(acknowledged.withdrawal(), record(10)); 120 assert_eq!(acknowledged.seller_sequence(), seq(8)); 121 let cancellation = 122 CancellationProposal::new(record(11), quantities(), "Private cancellation", time(204)) 123 .unwrap(); 124 assert_eq!(cancellation.agreement(), record(11)); 125 assert_eq!(cancellation.residual(), quantities()); 126 assert_eq!(cancellation.reason(), "Private cancellation"); 127 assert_eq!(cancellation.expires_at(), time(204)); 128 let authorization = ProposalAuthorization::new(record(10), time(205)); 129 assert_eq!(authorization.proposal(), record(10)); 130 assert_eq!(authorization.expires_at(), time(205)); 131 let released = ResidualRelease::new( 132 record(10), 133 record(11), 134 record(12), 135 quantities(), 136 authority(), 137 seq(9), 138 ) 139 .unwrap(); 140 assert_eq!(released.proposal(), record(10)); 141 assert_eq!(released.authorization(), record(11)); 142 assert_eq!(released.agreement(), record(12)); 143 assert_eq!(released.residual(), quantities()); 144 assert_eq!(released.authority(), authority()); 145 assert_eq!(released.seller_sequence(), seq(9)); 146 let ready = ReadinessClaim::new( 147 record(10), 148 FulfillmentGroupId::from_bytes([9; 32]), 149 quantities(), 150 window(), 151 ) 152 .unwrap(); 153 assert_eq!(ready.agreement(), record(10)); 154 assert_eq!( 155 ready.fulfillment_group(), 156 FulfillmentGroupId::from_bytes([9; 32]) 157 ); 158 assert_eq!(ready.lines(), quantities()); 159 assert_eq!(ready.window(), window()); 160 let handover = HandoverClaim::new( 161 record(10), 162 FulfillmentGroupId::from_bytes([9; 32]), 163 quantities(), 164 vec![measurement()], 165 time(90), 166 authority(), 167 seq(10), 168 ) 169 .unwrap(); 170 assert_eq!(handover.agreement(), record(10)); 171 assert_eq!( 172 handover.fulfillment_group(), 173 FulfillmentGroupId::from_bytes([9; 32]) 174 ); 175 assert_eq!(handover.lines(), quantities()); 176 assert_eq!(handover.measurements(), [measurement()]); 177 assert_eq!(handover.occurred_at(), time(90)); 178 assert_eq!(handover.authority(), authority()); 179 assert_eq!(handover.seller_sequence(), seq(10)); 180 let receipt = ReceiptObservation::new( 181 record(10), 182 quantities(), 183 Some(record(11)), 184 time(91), 185 "Private receipt", 186 ) 187 .unwrap(); 188 assert_eq!(receipt.handover(), record(10)); 189 assert_eq!(receipt.lines(), quantities()); 190 assert_eq!(receipt.supersedes(), Some(record(11))); 191 assert_eq!(receipt.occurred_at(), time(91)); 192 assert_eq!(receipt.note(), "Private receipt"); 193 let issue = issue(); 194 assert_eq!(issue.code(), IssueCode::Shortage); 195 assert_eq!(issue.description(), "Private issue"); 196 assert_eq!(issue.lines(), quantities()); 197 assert_eq!(issue.source_records(), [record(10)]); 198 let resolution = resolution(); 199 assert_eq!(resolution.issues(), [record(11)]); 200 assert_eq!(resolution.accepted_receipts(), [record(12)]); 201 assert_eq!(resolution.release_residual(), quantities()); 202 assert_eq!(resolution.summary(), "Private resolution"); 203 let resolution_proposal = ResolutionProposal::new(record(10), resolution.clone(), time(206)); 204 assert_eq!(resolution_proposal.agreement(), record(10)); 205 assert_eq!(resolution_proposal.resolution(), &resolution); 206 assert_eq!(resolution_proposal.expires_at(), time(206)); 207 let resolved = ResolutionCommitment::new(record(10), record(11), authority(), seq(11)).unwrap(); 208 assert_eq!(resolved.proposal(), record(10)); 209 assert_eq!(resolved.authorization(), record(11)); 210 assert_eq!(resolved.authority(), authority()); 211 assert_eq!(resolved.seller_sequence(), seq(11)); 212 vec![ 213 (CommercePayload::Quote(quote), "quote", 1), 214 (CommercePayload::Request(request), "request", 2), 215 (CommercePayload::Decline(reason.clone()), "decline", 1), 216 ( 217 CommercePayload::TermsProposed(proposal), 218 "terms_proposed", 219 3, 220 ), 221 ( 222 CommercePayload::TermsAuthorized(authorized), 223 "terms_authorized", 224 3, 225 ), 226 ( 227 CommercePayload::TermsRejected(reason.clone()), 228 "terms_rejected", 229 3, 230 ), 231 ( 232 CommercePayload::AgreementCommitted(committed), 233 "agreement_committed", 234 1, 235 ), 236 ( 237 CommercePayload::WithdrawalRequested(reason), 238 "withdrawal_requested", 239 2, 240 ), 241 ( 242 CommercePayload::WithdrawalAcknowledged(acknowledged), 243 "withdrawal_acknowledged", 244 1, 245 ), 246 ( 247 CommercePayload::CancellationProposed(cancellation), 248 "cancellation_proposed", 249 3, 250 ), 251 ( 252 CommercePayload::CancellationAuthorized(authorization), 253 "cancellation_authorized", 254 3, 255 ), 256 ( 257 CommercePayload::ResidualReleased(released), 258 "residual_released", 259 1, 260 ), 261 (CommercePayload::Ready(ready), "ready", 1), 262 (CommercePayload::Handover(handover), "handover", 1), 263 (CommercePayload::Receipt(receipt), "receipt", 2), 264 (CommercePayload::Issue(issue), "issue", 3), 265 ( 266 CommercePayload::ResolutionProposed(resolution_proposal), 267 "resolution_proposed", 268 3, 269 ), 270 ( 271 CommercePayload::ResolutionAuthorized(authorization), 272 "resolution_authorized", 273 3, 274 ), 275 ( 276 CommercePayload::ResolutionCommitted(resolved), 277 "resolution_committed", 278 1, 279 ), 280 ] 281 } 282 283 #[test] 284 fn all_nineteen_complete_families_preserve_exact_data_and_declared_role_order_reference_rules() { 285 let cases = cases(); 286 assert_eq!(cases.len(), 19); 287 for (payload, token, roles) in cases { 288 assert_eq!(payload.record_type().as_str(), token); 289 assert_eq!(format!("{payload:?}"), "CommercePayload(<redacted>)"); 290 for (author, bit) in [(seller(), 1), (buyer(), 2)] { 291 let original = header(author, order(), parents()); 292 let result = CommerceContent::new(original.clone(), payload.clone()); 293 if roles & bit == 0 { 294 assert_eq!(result.unwrap_err(), CommerceError::InvalidAuthor, "{token}"); 295 continue; 296 } 297 let value = result.unwrap(); 298 assert_eq!(value.profile(), "radroots.commerce"); 299 assert_eq!(value.version(), 1); 300 assert_eq!(value.record_type().as_str(), token); 301 assert_eq!(value.header(), &original); 302 assert_eq!(value.payload(), &payload); 303 assert_eq!(value, value.clone()); 304 assert_eq!(format!("{value:?}"), "CommerceContent(<redacted>)"); 305 let no_order = CommerceContent::new(header(author, None, parents()), payload.clone()); 306 if token == "quote" { 307 assert!(no_order.is_ok()); 308 } else { 309 assert_eq!( 310 no_order.unwrap_err(), 311 CommerceError::InvalidOrder, 312 "{token}" 313 ); 314 } 315 assert_eq!( 316 CommerceContent::new(header(author, order(), vec![]), payload.clone()).unwrap_err(), 317 CommerceError::MissingParent, 318 "{token}" 319 ); 320 let required: &[u8] = match token { 321 "quote" | "request" => &[3, 10], 322 "terms_proposed" => &[3, 11], 323 "agreement_committed" | "receipt" | "resolution_committed" => &[10, 11], 324 "residual_released" | "resolution_proposed" => &[10, 11, 12], 325 "cancellation_proposed" => &[11], 326 "decline" 327 | "terms_authorized" 328 | "terms_rejected" 329 | "withdrawal_requested" 330 | "withdrawal_acknowledged" 331 | "cancellation_authorized" 332 | "ready" 333 | "handover" 334 | "issue" 335 | "resolution_authorized" => &[10], 336 _ => panic!("unknown fixture family"), 337 }; 338 for missing in required { 339 let mut remaining = parents(); 340 remaining.retain(|parent| *parent != source(*missing)); 341 assert_eq!( 342 CommerceContent::new(header(author, order(), remaining), payload.clone()) 343 .unwrap_err(), 344 CommerceError::MissingParent, 345 "{token}: missing source{missing}" 346 ); 347 } 348 } 349 } 350 } 351 352 #[test] 353 fn common_scope_claims_are_exact_bounded_and_have_no_implicit_genesis_or_source_coverage() { 354 let thread = ThreadId::from_bytes([1; 32]); 355 let make = |author, buyer, seller, sequence, previous, parents| { 356 CommerceHeader::new( 357 thread, 358 order(), 359 author, 360 buyer, 361 seller, 362 time(100), 363 sequence, 364 previous, 365 parents, 366 ) 367 }; 368 let outsider = 369 PublicKey::from_hex("f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9") 370 .unwrap(); 371 assert_eq!( 372 make(seller(), seller(), seller(), seq(1), None, vec![]).unwrap_err(), 373 CommerceError::PartiesCoincide 374 ); 375 assert_eq!( 376 make(outsider, buyer(), seller(), seq(1), None, vec![]).unwrap_err(), 377 CommerceError::InvalidAuthor 378 ); 379 assert_eq!( 380 make(seller(), buyer(), seller(), seq(0), None, vec![]).unwrap_err(), 381 CommerceError::InvalidSequence 382 ); 383 assert_eq!( 384 make( 385 seller(), 386 buyer(), 387 seller(), 388 seq(1), 389 Some(record(16)), 390 parents() 391 ) 392 .unwrap_err(), 393 CommerceError::InvalidPrevious 394 ); 395 assert_eq!( 396 make(seller(), buyer(), seller(), seq(2), None, parents()).unwrap_err(), 397 CommerceError::InvalidPrevious 398 ); 399 assert_eq!( 400 make( 401 seller(), 402 buyer(), 403 seller(), 404 seq(2), 405 Some(record(16)), 406 vec![] 407 ) 408 .unwrap_err(), 409 CommerceError::MissingParent 410 ); 411 for invalid in [ 412 vec![source(2), source(1)], 413 vec![source(1); 2], 414 (0..9).map(source).collect(), 415 ] { 416 assert_eq!( 417 make(seller(), buyer(), seller(), seq(1), None, invalid).unwrap_err(), 418 CommerceError::InvalidParents 419 ); 420 } 421 let mut spacious = Vec::with_capacity(4096); 422 spacious.extend(parents()); 423 let value = make( 424 seller(), 425 buyer(), 426 seller(), 427 seq(i64::MAX as u64), 428 Some(record(16)), 429 spacious, 430 ) 431 .unwrap(); 432 assert_eq!(value.thread_id(), thread); 433 assert_eq!(value.order_id(), order()); 434 assert_eq!(value.author(), seller()); 435 assert_eq!(value.buyer(), buyer()); 436 assert_eq!(value.seller(), seller()); 437 assert_eq!(value.created_at(), time(100)); 438 assert_eq!(value.author_sequence(), seq(i64::MAX as u64)); 439 assert_eq!(value.author_previous(), Some(record(16))); 440 assert_eq!(value.parents(), parents()); 441 assert_eq!(value, value.clone()); 442 assert_eq!(format!("{value:?}"), "CommerceHeader(<redacted>)"); 443 } 444 445 #[test] 446 fn complete_terms_sources_and_participants_are_required_without_conflating_references_with_proof() { 447 let outsider = 448 PublicKey::from_hex("f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9") 449 .unwrap(); 450 for payload in [ 451 CommercePayload::Quote(TermsQuote::new(private_terms(), time(200), None)), 452 CommercePayload::Request(TermsRequest::new(private_terms(), time(200), None)), 453 CommercePayload::TermsProposed( 454 TermsProposal::new(None, private_terms(), time(200), "").unwrap(), 455 ), 456 ] { 457 let author = if payload.record_type() == CommerceType::Request { 458 outsider 459 } else { 460 seller() 461 }; 462 let different = CommerceHeader::new( 463 ThreadId::from_bytes([1; 32]), 464 order(), 465 author, 466 outsider, 467 seller(), 468 time(100), 469 seq(1), 470 None, 471 parents(), 472 ) 473 .unwrap(); 474 assert_eq!( 475 CommerceContent::new(different, payload.clone()).unwrap_err(), 476 CommerceError::TermsPartiesMismatch 477 ); 478 let author = if payload.record_type() == CommerceType::Request { 479 buyer() 480 } else { 481 outsider 482 }; 483 let different_seller = CommerceHeader::new( 484 ThreadId::from_bytes([1; 32]), 485 order(), 486 author, 487 buyer(), 488 outsider, 489 time(100), 490 seq(1), 491 None, 492 parents(), 493 ) 494 .unwrap(); 495 assert_eq!( 496 CommerceContent::new(different_seller, payload.clone()).unwrap_err(), 497 CommerceError::TermsPartiesMismatch 498 ); 499 let author = if payload.record_type() == CommerceType::Request { 500 buyer() 501 } else { 502 seller() 503 }; 504 assert!( 505 CommerceContent::new(header(author, order(), vec![source(3)]), payload.clone()).is_ok() 506 ); 507 assert_eq!( 508 CommerceContent::new(header(author, order(), vec![source(10)]), payload).unwrap_err(), 509 CommerceError::MissingParent 510 ); 511 } 512 let committed = 513 AgreementCommitment::new(None, record(10), digest(), authority(), seq(1)).unwrap(); 514 assert!( 515 CommerceContent::new( 516 header(seller(), order(), vec![source(10)]), 517 CommercePayload::AgreementCommitted(committed) 518 ) 519 .is_ok() 520 ); 521 let receipt = ReceiptObservation::new(record(10), quantities(), None, time(90), "").unwrap(); 522 assert!( 523 CommerceContent::new( 524 header(buyer(), order(), vec![source(10)]), 525 CommercePayload::Receipt(receipt) 526 ) 527 .is_ok() 528 ); 529 // Payload reference maxima and the eight-parent envelope apply jointly. 530 // References are refused rather than silently omitted or claimed covered. 531 let issue = 532 IssueReport::new(IssueCode::Other, "", vec![], (0..9).map(record).collect()).unwrap(); 533 assert_eq!( 534 CommerceContent::new( 535 header(seller(), order(), (0..8).map(source).collect()), 536 CommercePayload::Issue(issue) 537 ) 538 .unwrap_err(), 539 CommerceError::MissingParent 540 ); 541 } 542 543 #[test] 544 fn bounded_private_text_preserves_unicode_and_optional_empty_claims_without_diagnostic_disclosure() 545 { 546 let maximum = "🥕".repeat(1024); 547 assert_eq!( 548 RecordReason::new(record(10), &maximum).unwrap().reason(), 549 maximum 550 ); 551 assert_eq!(RecordReason::new(record(10), "").unwrap().reason(), ""); 552 for invalid in ["x".repeat(1025), "🥕".repeat(1025), "x".repeat(4097)] { 553 assert_eq!( 554 RecordReason::new(record(10), &invalid).unwrap_err(), 555 CommerceError::InvalidText 556 ); 557 assert_eq!( 558 TermsProposal::new(None, private_terms(), time(200), &invalid).unwrap_err(), 559 CommerceError::InvalidText 560 ); 561 assert_eq!( 562 CancellationProposal::new(record(10), quantities(), &invalid, time(200)).unwrap_err(), 563 CommerceError::InvalidText 564 ); 565 assert_eq!( 566 ReceiptObservation::new(record(10), quantities(), None, time(90), &invalid) 567 .unwrap_err(), 568 CommerceError::InvalidText 569 ); 570 } 571 let maximum = "🥕".repeat(2048); 572 assert_eq!( 573 IssueReport::new(IssueCode::Other, &maximum, vec![], vec![]) 574 .unwrap() 575 .description(), 576 maximum 577 ); 578 assert_eq!( 579 Resolution::new(vec![record(10)], vec![], vec![], &maximum) 580 .unwrap() 581 .summary(), 582 maximum 583 ); 584 for invalid in ["x".repeat(2049), "🥕".repeat(2049), "x".repeat(8193)] { 585 assert_eq!( 586 IssueReport::new(IssueCode::Other, &invalid, vec![], vec![]).unwrap_err(), 587 CommerceError::InvalidText 588 ); 589 assert_eq!( 590 Resolution::new(vec![record(10)], vec![], vec![], &invalid).unwrap_err(), 591 CommerceError::InvalidText 592 ); 593 } 594 for (code, token) in [ 595 (IssueCode::Shortage, "shortage"), 596 (IssueCode::Quality, "quality"), 597 (IssueCode::Substitution, "substitution"), 598 (IssueCode::Collection, "collection"), 599 (IssueCode::Excess, "excess"), 600 (IssueCode::Other, "other"), 601 ] { 602 assert_eq!(code.as_str(), token); 603 } 604 assert_eq!( 605 format!( 606 "{:?}", 607 RecordReason::new(record(10), "Private reason").unwrap() 608 ), 609 "RecordReason(<redacted>)" 610 ); 611 assert_eq!(format!("{:?}", issue()), "IssueReport(<redacted>)"); 612 assert_eq!(format!("{:?}", resolution()), "Resolution(<redacted>)"); 613 } 614 615 #[test] 616 fn quantity_reference_and_measurement_collections_reject_ambiguity_and_preserve_zero_observations() 617 { 618 let maximum_lines: Vec<_> = (0..32) 619 .map(|id| LineQuantity::new(AgreementLineId::from_bytes([id; 32]), "1".parse().unwrap())) 620 .collect(); 621 let value = IssueReport::new( 622 IssueCode::Other, 623 "", 624 maximum_lines.clone(), 625 (0..32).map(record).collect(), 626 ) 627 .unwrap(); 628 assert_eq!(value.lines(), maximum_lines); 629 assert_eq!(value.source_records().len(), 32); 630 assert!( 631 Resolution::new( 632 (0..32).map(record).collect(), 633 (32..64).map(record).collect(), 634 maximum_lines.clone(), 635 "" 636 ) 637 .is_ok() 638 ); 639 for invalid in [vec![maximum_lines[0]; 2], vec![maximum_lines[0]; 33]] { 640 assert_eq!( 641 IssueReport::new(IssueCode::Other, "", invalid.clone(), vec![]).unwrap_err(), 642 CommerceError::InvalidLines 643 ); 644 assert_eq!( 645 ReadinessClaim::new( 646 record(10), 647 FulfillmentGroupId::from_bytes([9; 32]), 648 invalid, 649 window() 650 ) 651 .unwrap_err(), 652 CommerceError::InvalidLines 653 ); 654 } 655 for invalid in [vec![record(1); 2], vec![record(1); 33]] { 656 assert_eq!( 657 IssueReport::new(IssueCode::Other, "", vec![], invalid.clone()).unwrap_err(), 658 CommerceError::InvalidReferences 659 ); 660 assert_eq!( 661 Resolution::new(invalid, vec![], vec![], "").unwrap_err(), 662 CommerceError::InvalidReferences 663 ); 664 } 665 assert_eq!( 666 Resolution::new(vec![], vec![], vec![], "").unwrap_err(), 667 CommerceError::InvalidReferences 668 ); 669 assert_eq!( 670 ReadinessClaim::new( 671 record(10), 672 FulfillmentGroupId::from_bytes([9; 32]), 673 vec![], 674 window() 675 ) 676 .unwrap_err(), 677 CommerceError::InvalidLines 678 ); 679 assert_eq!( 680 CancellationProposal::new(record(10), vec![], "", time(200)).unwrap_err(), 681 CommerceError::InvalidLines 682 ); 683 assert_eq!( 684 ReceiptObservation::new(record(10), vec![], None, time(90), "").unwrap_err(), 685 CommerceError::InvalidLines 686 ); 687 let zero = LineQuantity::new(AgreementLineId::from_bytes([7; 32]), Decimal::ZERO); 688 assert_eq!(zero.line_id(), AgreementLineId::from_bytes([7; 32])); 689 assert_eq!(zero.amount(), Decimal::ZERO); 690 assert_eq!(format!("{zero:?}"), "LineQuantity(<redacted>)"); 691 let observed = 692 ReceiptObservation::new(record(10), vec![zero], None, time(90), "Nothing observed") 693 .unwrap(); 694 assert_eq!(observed.lines(), [zero]); 695 assert!( 696 CommerceContent::new( 697 header(buyer(), order(), vec![source(10)]), 698 CommercePayload::Receipt(observed) 699 ) 700 .is_ok() 701 ); 702 let measured = measurement(); 703 assert_eq!(measured.line_id(), AgreementLineId::from_bytes([7; 32])); 704 assert_eq!( 705 measured.quantity(), 706 Quantity::new("1".parse().unwrap(), listing::MeasurementUnit::Kilogram).unwrap() 707 ); 708 assert_eq!(measured.line_total(), money("325")); 709 assert_eq!(format!("{measured:?}"), "FinalMeasurement(<redacted>)"); 710 let unrelated = FinalMeasurement::new( 711 AgreementLineId::from_bytes([8; 32]), 712 measured.quantity(), 713 money("325"), 714 ); 715 for invalid in [vec![measured; 2], vec![measured; 33], vec![unrelated]] { 716 assert_eq!( 717 HandoverClaim::new( 718 record(10), 719 FulfillmentGroupId::from_bytes([9; 32]), 720 quantities(), 721 invalid, 722 time(90), 723 authority(), 724 seq(1) 725 ) 726 .unwrap_err(), 727 CommerceError::InvalidMeasurements 728 ); 729 } 730 let no_measurements = HandoverClaim::new( 731 record(10), 732 FulfillmentGroupId::from_bytes([9; 32]), 733 quantities(), 734 vec![], 735 time(90), 736 authority(), 737 seq(1), 738 ) 739 .unwrap(); 740 assert!(no_measurements.measurements().is_empty()); 741 } 742 743 #[test] 744 fn every_seller_decision_requires_positive_sequence_without_minting_authority() { 745 assert_eq!( 746 AgreementCommitment::new(None, record(10), digest(), authority(), seq(0)).unwrap_err(), 747 CommerceError::InvalidSequence 748 ); 749 assert_eq!( 750 WithdrawalAcknowledgement::new(record(10), seq(0)).unwrap_err(), 751 CommerceError::InvalidSequence 752 ); 753 assert_eq!( 754 ResidualRelease::new( 755 record(10), 756 record(11), 757 record(12), 758 quantities(), 759 authority(), 760 seq(0) 761 ) 762 .unwrap_err(), 763 CommerceError::InvalidSequence 764 ); 765 assert_eq!( 766 HandoverClaim::new( 767 record(10), 768 FulfillmentGroupId::from_bytes([9; 32]), 769 quantities(), 770 vec![], 771 time(90), 772 authority(), 773 seq(0) 774 ) 775 .unwrap_err(), 776 CommerceError::InvalidSequence 777 ); 778 assert_eq!( 779 ResolutionCommitment::new(record(10), record(11), authority(), seq(0)).unwrap_err(), 780 CommerceError::InvalidSequence 781 ); 782 } 783 784 #[test] 785 fn record_errors_are_input_free_and_bounded() { 786 for error in [ 787 CommerceError::PartiesCoincide, 788 CommerceError::InvalidAuthor, 789 CommerceError::InvalidOrder, 790 CommerceError::InvalidSequence, 791 CommerceError::InvalidPrevious, 792 CommerceError::InvalidParents, 793 CommerceError::MissingParent, 794 CommerceError::TermsPartiesMismatch, 795 CommerceError::InvalidText, 796 CommerceError::InvalidLines, 797 CommerceError::InvalidMeasurements, 798 CommerceError::InvalidReferences, 799 ] { 800 assert!(error.to_string().len() < 96); 801 assert!(core::error::Error::source(&error).is_none()); 802 assert!(!format!("{error:?}").contains("Private reason")); 803 } 804 }