use crate::{
world::sync::host_entity_channel::HostEntityChannel, BigMapKey, EntityCommand, GlobalEntity,
HostEntity, HostType, RemoteEntity,
};
#[test]
fn test_all_migration_commands_through_real_flow() {
let global_entity = GlobalEntity::from_u64(10001);
let old_remote_entity = RemoteEntity::new(99);
let new_host_entity = HostEntity::new(100);
let mut host_channel = HostEntityChannel::new(HostType::Server);
host_channel.send_command(EntityCommand::EnableDelegation(Some(1), global_entity));
host_channel.send_command(EntityCommand::MigrateResponse(
Some(2),
global_entity,
old_remote_entity,
new_host_entity,
));
let commands = host_channel.extract_outgoing_commands();
assert!(
commands.len() >= 2,
"Should have at least 2 commands buffered"
);
let has_migrate_response = commands
.iter()
.any(|cmd| matches!(cmd, EntityCommand::MigrateResponse(_, _, _, _)));
assert!(
has_migrate_response,
"MigrateResponse should be in outgoing commands"
);
}
#[test]
fn test_request_authority_command() {
let global_entity = GlobalEntity::from_u64(10002);
let mut host_channel = HostEntityChannel::new(HostType::Client);
host_channel.send_command(EntityCommand::Publish(Some(1), global_entity));
host_channel.send_command(EntityCommand::EnableDelegation(Some(2), global_entity));
host_channel.send_command(EntityCommand::RequestAuthority(Some(3), global_entity));
let commands = host_channel.extract_outgoing_commands();
let has_request = commands
.iter()
.any(|cmd| matches!(cmd, EntityCommand::RequestAuthority(_, _)));
assert!(
has_request,
"RequestAuthority should be in outgoing commands"
);
}
#[test]
fn test_enable_delegation_response_command() {
let global_entity = GlobalEntity::from_u64(10003);
let mut host_channel = HostEntityChannel::new(HostType::Client);
host_channel.send_command(EntityCommand::Publish(Some(1), global_entity));
host_channel.send_command(EntityCommand::EnableDelegation(Some(2), global_entity));
host_channel.send_command(EntityCommand::EnableDelegationResponse(
Some(3),
global_entity,
));
let commands = host_channel.extract_outgoing_commands();
let has_response = commands
.iter()
.any(|cmd| matches!(cmd, EntityCommand::EnableDelegationResponse(_, _)));
assert!(
has_response,
"EnableDelegationResponse should be in outgoing commands"
);
}
#[test]
fn test_complete_delegation_flow_with_all_commands() {
let global_entity = GlobalEntity::from_u64(10004);
let old_remote_entity = RemoteEntity::new(200);
let new_host_entity = HostEntity::new(201);
let mut server_channel = HostEntityChannel::new(HostType::Server);
server_channel.send_command(EntityCommand::EnableDelegation(Some(1), global_entity));
server_channel.send_command(EntityCommand::MigrateResponse(
Some(2),
global_entity,
old_remote_entity,
new_host_entity,
));
let commands = server_channel.extract_outgoing_commands();
assert_eq!(commands.len(), 2, "Should have all 2 commands");
assert!(matches!(commands[0], EntityCommand::EnableDelegation(_, _)));
assert!(matches!(
commands[1],
EntityCommand::MigrateResponse(_, _, _, _)
));
}
#[test]
#[should_panic(expected = "Cannot send MigrateResponse")]
fn test_migrate_response_requires_delegation() {
let global_entity = GlobalEntity::from_u64(10005);
let old_remote_entity = RemoteEntity::new(201);
let host_entity = HostEntity::new(202);
let mut host_channel = HostEntityChannel::new(HostType::Server);
host_channel.send_command(EntityCommand::MigrateResponse(
Some(1),
global_entity,
old_remote_entity,
host_entity,
));
}
#[test]
#[should_panic(expected = "Cannot request authority")]
fn test_request_authority_requires_delegation() {
let global_entity = GlobalEntity::from_u64(10006);
let mut host_channel = HostEntityChannel::new(HostType::Client);
host_channel.send_command(EntityCommand::Publish(Some(1), global_entity));
host_channel.send_command(EntityCommand::RequestAuthority(Some(2), global_entity));
}
#[test]
fn test_migrate_response_through_host_channel_send() {
let global_entity = GlobalEntity::from_u64(10007);
let old_remote_entity = RemoteEntity::new(202);
let new_host_entity = HostEntity::new(203);
let mut host_channel = HostEntityChannel::new(HostType::Server);
host_channel.send_command(EntityCommand::EnableDelegation(Some(1), global_entity));
host_channel.send_command(EntityCommand::MigrateResponse(
Some(2),
global_entity,
old_remote_entity,
new_host_entity,
));
let commands = host_channel.extract_outgoing_commands();
assert!(
commands
.iter()
.any(|cmd| matches!(cmd, EntityCommand::MigrateResponse(_, _, _, _))),
"MigrateResponse should have been sent successfully"
);
}
#[test]
fn test_server_delegation_command_sequence() {
let global_entity = GlobalEntity::from_u64(10008);
let old_remote_entity = RemoteEntity::new(203);
let new_host_entity = HostEntity::new(204);
let mut host_channel = HostEntityChannel::new(HostType::Server);
host_channel.send_command(EntityCommand::EnableDelegation(Some(1), global_entity));
host_channel.send_command(EntityCommand::MigrateResponse(
Some(2),
global_entity,
old_remote_entity,
new_host_entity,
));
let commands = host_channel.extract_outgoing_commands();
assert_eq!(
commands.len(),
2,
"Should have both EnableDelegation and MigrateResponse"
);
assert!(
matches!(commands[0], EntityCommand::EnableDelegation(_, _)),
"First command must be EnableDelegation"
);
assert!(
matches!(commands[1], EntityCommand::MigrateResponse(_, _, _, _)),
"Second command must be MigrateResponse"
);
}
#[test]
#[should_panic(expected = "Cannot send MigrateResponse")]
fn test_server_delegation_wrong_sequence_panics() {
let global_entity = GlobalEntity::from_u64(10009);
let old_remote_entity = RemoteEntity::new(204);
let new_host_entity = HostEntity::new(205);
let mut host_channel = HostEntityChannel::new(HostType::Server);
host_channel.send_command(EntityCommand::MigrateResponse(
Some(1),
global_entity,
old_remote_entity,
new_host_entity,
));
}