use super::*;

#[test]
fn test_create_remove_create_links() {
	let mut directory = Directory::from(vec![
		Descriptor::dir(b"a", 2),
		Descriptor::dir(b"b", 1),
		Descriptor::file(b"example"),
	]);

	let example1 = directory.as_ref()[2];
	directory.create_link(b"aa/bb/example", &example1);
	let example2 = directory.remove(b"a/b/example").unwrap();
	directory.create_link(b"a/b/example", &example2);

	dbg!(directory);
}

#[test]
fn test_remove_directory_promotes_children() {
	let mut directory = Directory::new();
	directory.create_dir(b"assets/images");
	let logo = Descriptor::file(b"logo.png");
	let icon = Descriptor::file(b"icon.png");
	directory.create_link(b"assets/images/logo.png", &logo);
	directory.create_link(b"assets/images/icon.png", &icon);

	let removed = directory.remove(b"assets/images").expect("directory should be removed");

	assert!(removed.is_dir());
	assert!(directory.find_desc(b"assets/images").is_none());
	assert!(directory.find_file(b"assets/logo.png").is_some());
	assert!(directory.find_file(b"assets/icon.png").is_some());
	assert!(directory.find_desc(b"assets/images/logo.png").is_none());
}

#[test]
fn test_move_file_creates_destination_dirs_and_preserves_descriptor() {
	let mut directory = Directory::new();
	let mut source = Descriptor::file(b"save.dat");
	source.content_type = 7;
	source.content_size = 123;
	source.section = Section {
		offset: 4,
		size: 2,
		nonce: [11, 13],
		mac: [17, 19],
	};
	directory.create_link(b"profiles/slot1/save.dat", &source);

	assert!(directory.move_file(b"profiles/slot1/save.dat", b"backup/slot1/save.dat"));
	assert!(directory.find_desc(b"profiles/slot1/save.dat").is_none());

	let moved = directory.find_file(b"backup/slot1/save.dat").expect("moved file should exist");
	assert_eq!(moved.content_type, source.content_type);
	assert_eq!(moved.content_size, source.content_size);
	assert_eq!(moved.section, source.section);

	assert!(!directory.move_file(b"backup", b"archive/backup"));
}
