use treehouse::vfs::{entries, query, BufferedFile, Content, MemDir, ToDynDir, VPath, VPathBuf};

const HEWWO: &[u8] = b"hewwo :3";
const FWOOFEE: &[u8] = b"fwoofee -w-";
const BOOP: &[u8] = b"boop >w<";

fn vfs() -> MemDir {
    let file1 = BufferedFile::new(Content::new("text/plain", HEWWO.to_vec()));
    let file2 = BufferedFile::new(Content::new("text/plain", FWOOFEE.to_vec()));
    let file3 = BufferedFile::new(Content::new("text/plain", BOOP.to_vec()));

    let mut inner = MemDir::new();
    inner.add(VPath::new("file3.txt"), file3.to_dyn());

    let mut vfs = MemDir::new();
    vfs.add(VPath::new("file1.txt"), file1.to_dyn());
    vfs.add(VPath::new("file2.txt"), file2.to_dyn());
    vfs.add(VPath::new("inner"), inner.to_dyn());
    vfs
}

#[test]
fn dir() {
    let vfs = vfs();

    let mut dir = entries(&vfs, VPath::ROOT);
    dir.sort();
    assert_eq!(
        dir,
        vec![
            VPathBuf::new("file1.txt"),
            VPathBuf::new("file2.txt"),
            VPathBuf::new("inner"),
        ]
    );

    assert!(entries(&vfs, VPath::new("file1.txt")).is_empty());
    assert!(entries(&vfs, VPath::new("file2.txt")).is_empty());
    assert_eq!(
        entries(&vfs, VPath::new("inner")),
        vec![VPathBuf::new("inner/file3.txt")]
    );
}

#[test]
fn content() {
    let vfs = vfs();

    assert_eq!(
        query::<Content>(&vfs, VPath::new("file1.txt"))
            .map(Content::bytes)
            .as_deref(),
        Some(HEWWO)
    );
    assert_eq!(
        query::<Content>(&vfs, VPath::new("file2.txt"))
            .map(Content::bytes)
            .as_deref(),
        Some(FWOOFEE)
    );
    assert_eq!(
        query::<Content>(&vfs, VPath::new("inner/file3.txt"))
            .map(Content::bytes)
            .as_deref(),
        Some(BOOP)
    );
}
