use std::{collections::HashMap, iter, net::IpAddr};
use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
use insta::{assert_debug_snapshot, assert_snapshot};
use pnet::datalink::DataLinkReceiver;
use rstest::rstest;
use crate::{
cli::RenderOpts,
start,
tests::{
cases::test_utils::{
build_tcp_packet, opts_ui, os_input_output, os_input_output_factory,
sample_frames_short, sample_frames_sustained_long,
sample_frames_sustained_multiple_processes, sample_frames_sustained_one_process,
sleep_and_quit_events, sleep_resize_and_quit_events, test_backend_factory,
},
fakes::{
create_fake_dns_client, get_interfaces, get_open_sockets, NetworkFrames, TerminalEvents,
},
},
Opt, OsInputOutput,
};
#[test]
fn basic_startup() {
let network_frames = vec![NetworkFrames::new(vec![
None, // sleep
]) as Box<dyn DataLinkReceiver>];
let (terminal_events, terminal_draw_events, backend) = test_backend_factory(190, 50);
let os_input = os_input_output(network_frames, 1);
let opts = opts_ui();
start(backend, os_input, opts);
let terminal_draw_events_mirror = terminal_draw_events.lock().unwrap();
assert_eq!(terminal_draw_events_mirror.len(), 1);
assert_snapshot!(&terminal_draw_events_mirror[0]);
assert_debug_snapshot!(terminal_events.lock().unwrap().as_slice());
}
#[test]
fn pause_by_space() {
let network_frames = vec![NetworkFrames::new(vec![
Some(build_tcp_packet(
"1.1.1.1",
"10.0.0.2",
12345,
443,
b"I have come from 1.1.1.1",
)),
None, // sleep
None, // sleep
None, // sleep
Some(build_tcp_packet(
"1.1.1.1",
"10.0.0.2",
12345,
443,
b"Same here, but one second later",
)),
]) as Box<dyn DataLinkReceiver>];
// sleep for 1s, then press space, sleep for 2s, then quit
let mut events: Vec<Option<Event>> = iter::repeat(None).take(1).collect();
events.push(Some(Event::Key(KeyEvent::new(
KeyCode::Char(' '),
KeyModifiers::NONE,
))));
events.push(None);
events.push(None);
events.push(Some(Event::Key(KeyEvent::new(
KeyCode::Char(' '),
KeyModifiers::NONE,
))));
events.push(Some(Event::Key(KeyEvent::new(
KeyCode::Char('c'),
KeyModifiers::CONTROL,
))));
let events = Box::new(TerminalEvents::new(events));
let os_input = os_input_output_factory(network_frames, None, None, events);
let (terminal_events, terminal_draw_events, backend) = test_backend_factory(190, 50);
let opts = opts_ui();
start(backend, os_input, opts);
let terminal_draw_events_mirror = terminal_draw_events.lock().unwrap();
assert_eq!(terminal_draw_events_mirror.len(), 3);
assert_snapshot!(&terminal_draw_events_mirror[0]);
assert_snapshot!(&terminal_draw_events_mirror[1]);
assert_snapshot!(&terminal_draw_events_mirror[2]);
assert_debug_snapshot!(terminal_events.lock().unwrap().as_slice());
}
#[test]
fn rearranged_by_tab() {
let network_frames = vec![NetworkFrames::new(vec![
Some(build_tcp_packet(
"1.1.1.1",
"10.0.0.2",
12345,
443,
b"I have come from 1.1.1.1",
)),
None, // sleep
None, // sleep
None, // sleep
Some(build_tcp_packet(
"1.1.1.1",
"10.0.0.2",
12345,
443,
b"Same here, but one second later",
)),
]) as Box<dyn DataLinkReceiver>];
// sleep for 1s, then press tab, sleep for 2s, then quit
let mut events: Vec<Option<Event>> = iter::repeat(None).take(1).collect();
events.push(None);
events.push(Some(Event::Key(KeyEvent::new(
KeyCode::Tab,
KeyModifiers::NONE,
))));
events.push(None);
events.push(None);
events.push(Some(Event::Key(KeyEvent::new(
KeyCode::Char('c'),
KeyModifiers::CONTROL,
))));
let events = Box::new(TerminalEvents::new(events));
let os_input = os_input_output_factory(network_frames, None, None, events);
let (terminal_events, terminal_draw_events, backend) = test_backend_factory(190, 50);
let opts = opts_ui();
start(backend, os_input, opts);
let terminal_draw_events_mirror = terminal_draw_events.lock().unwrap();
assert_eq!(terminal_draw_events_mirror.len(), 5);
assert_snapshot!(&terminal_draw_events_mirror[0]);
assert_snapshot!(&terminal_draw_events_mirror[1]);
assert_snapshot!(&terminal_draw_events_mirror[2]);
assert_snapshot!(&terminal_draw_events_mirror[3]);
assert_snapshot!(&terminal_draw_events_mirror[4]);
assert_d