Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -249,3 +249,223 @@ impl TryFrom<&InstantAssetLockProof> for RawInstantLockProof {
})
}
}

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

// ---------------------------------------------------------------
// Default
// ---------------------------------------------------------------

#[test]
fn test_default_instant_asset_lock_proof() {
let proof = InstantAssetLockProof::default();
assert_eq!(proof.output_index(), 0);
assert_eq!(proof.transaction().version, 0);
assert_eq!(proof.transaction().lock_time, 0);
assert_eq!(proof.transaction().input.len(), 1);
assert_eq!(proof.transaction().output.len(), 1);
assert!(proof.transaction().special_transaction_payload.is_none());
}

// ---------------------------------------------------------------
// Constructor and accessors
// ---------------------------------------------------------------

#[test]
fn test_new_stores_fields_correctly() {
let proof = raw_instant_asset_lock_proof_fixture(None, None);
assert_eq!(proof.output_index(), 0);
// Verify the instant lock and transaction are accessible
let _il = proof.instant_lock();
let _tx = proof.transaction();
}

#[test]
fn test_instant_lock_accessor() {
let proof = raw_instant_asset_lock_proof_fixture(None, None);
let il = proof.instant_lock();
assert_eq!(il.version, 1);
assert_eq!(il.inputs.len(), 1);
}

#[test]
fn test_transaction_accessor() {
let proof = raw_instant_asset_lock_proof_fixture(None, None);
let tx = proof.transaction();
assert_eq!(tx.version, 0);
assert_eq!(tx.lock_time, 0);
assert_eq!(tx.input.len(), 1);
}

#[test]
fn test_output_index_accessor() {
let proof = raw_instant_asset_lock_proof_fixture(None, None);
assert_eq!(proof.output_index(), 0);
}

#[test]
fn test_output_index_with_custom_value() {
let proof = raw_instant_asset_lock_proof_fixture(None, None);
// Create a new proof with a different output_index
let custom_proof =
InstantAssetLockProof::new(proof.instant_lock.clone(), proof.transaction.clone(), 5);
assert_eq!(custom_proof.output_index(), 5);
}

// ---------------------------------------------------------------
// output()
// ---------------------------------------------------------------

#[test]
fn test_output_returns_some_for_valid_asset_lock_transaction() {
let proof = raw_instant_asset_lock_proof_fixture(None, None);
// The fixture creates a transaction with AssetLockPayloadType containing one credit output
let output = proof.output();
assert!(output.is_some());
}

#[test]
fn test_output_returns_none_for_default_transaction() {
let proof = InstantAssetLockProof::default();
// Default transaction has no special_transaction_payload
assert!(proof.output().is_none());
}

#[test]
fn test_output_returns_none_for_out_of_range_index() {
let proof = raw_instant_asset_lock_proof_fixture(None, None);
// Fixture has output_index 0 and only 1 credit output, so index 99 should be out of range
let modified_proof =
InstantAssetLockProof::new(proof.instant_lock.clone(), proof.transaction.clone(), 99);
assert!(modified_proof.output().is_none());
}

// ---------------------------------------------------------------
// out_point()
// ---------------------------------------------------------------

#[test]
fn test_out_point_returns_some_for_valid_proof() {
let proof = raw_instant_asset_lock_proof_fixture(None, None);
let outpoint = proof.out_point();
assert!(outpoint.is_some());
let outpoint = outpoint.unwrap();
assert_eq!(outpoint.txid, proof.transaction.txid());
assert_eq!(outpoint.vout, 0);
}

#[test]
fn test_out_point_returns_none_for_default() {
let proof = InstantAssetLockProof::default();
assert!(proof.out_point().is_none());
}

// ---------------------------------------------------------------
// create_identifier()
// ---------------------------------------------------------------

#[test]
fn test_create_identifier_succeeds_for_valid_proof() {
let proof = raw_instant_asset_lock_proof_fixture(None, None);
let result = proof.create_identifier();
assert!(result.is_ok());
let identifier = result.unwrap();
// Identifier should be 32 bytes
assert_eq!(identifier.as_slice().len(), 32);
}

#[test]
fn test_create_identifier_deterministic() {
let proof = raw_instant_asset_lock_proof_fixture(None, None);
let id1 = proof.create_identifier().unwrap();
let id2 = proof.create_identifier().unwrap();
assert_eq!(id1, id2);
}

#[test]
fn test_create_identifier_fails_for_default() {
let proof = InstantAssetLockProof::default();
let result = proof.create_identifier();
assert!(result.is_err());
}

// ---------------------------------------------------------------
// to_object()
// ---------------------------------------------------------------

#[test]
fn test_to_object_succeeds() {
let proof = raw_instant_asset_lock_proof_fixture(None, None);
let result = proof.to_object();
assert!(result.is_ok());
}

#[test]
fn test_to_cleaned_object_succeeds() {
let proof = raw_instant_asset_lock_proof_fixture(None, None);
let result = proof.to_cleaned_object();
assert!(result.is_ok());
}

// ---------------------------------------------------------------
// RawInstantLockProof round-trip
// ---------------------------------------------------------------

#[test]
fn test_raw_instant_lock_proof_round_trip() {
let proof = raw_instant_asset_lock_proof_fixture(None, None);
let raw = RawInstantLockProof::try_from(&proof).unwrap();
let recovered = InstantAssetLockProof::try_from(raw).unwrap();

assert_eq!(recovered.output_index, proof.output_index);
assert_eq!(recovered.instant_lock, proof.instant_lock);
assert_eq!(recovered.transaction.txid(), proof.transaction.txid());
}

#[test]
fn test_raw_instant_lock_proof_preserves_output_index() {
let base = raw_instant_asset_lock_proof_fixture(None, None);
let proof =
InstantAssetLockProof::new(base.instant_lock.clone(), base.transaction.clone(), 7);
let raw = RawInstantLockProof::try_from(&proof).unwrap();
assert_eq!(raw.output_index, 7);
let recovered = InstantAssetLockProof::try_from(raw).unwrap();
assert_eq!(recovered.output_index(), 7);
}

// ---------------------------------------------------------------
// Eq / Clone
// ---------------------------------------------------------------

#[test]
fn test_clone_equals_original() {
let proof = raw_instant_asset_lock_proof_fixture(None, None);
let cloned = proof.clone();
assert_eq!(proof, cloned);
}

#[test]
fn test_different_output_index_not_equal() {
let proof = raw_instant_asset_lock_proof_fixture(None, None);
let mut modified = proof.clone();
modified.output_index = 1;
assert_ne!(proof, modified);
}

// ---------------------------------------------------------------
// TryFrom<Value>
// ---------------------------------------------------------------

#[test]
fn test_try_from_value_round_trip() {
let proof = raw_instant_asset_lock_proof_fixture(None, None);
let value = proof.to_object().unwrap();
let recovered = InstantAssetLockProof::try_from(value).unwrap();
assert_eq!(proof.output_index, recovered.output_index);
assert_eq!(proof.instant_lock, recovered.instant_lock);
assert_eq!(proof.transaction.txid(), recovered.transaction.txid());
}
}
Loading
Loading