Cotton code
120 lines · 4.3 KB · RustRaw
//! The store half of `apps/cotton/__tests__/client/media.test.ts`.use super::*;use crate::test_support::{temp_layout, WORKSPACE};fn write(path: &Path, bytes: &[u8]) {    if let Some(parent) = path.parent() {        std::fs::create_dir_all(parent).expect("the folder");    }    std::fs::write(path, bytes).expect("the file");}#[test]fn it_lands_a_file_at_its_own_digest_and_answers_where() {    let (dir, layout) = temp_layout();    let source = dir.path().join("an-attachment.jpg");    write(&source, b"synthetic bytes");    let stored = put_media_file(&layout, WORKSPACE, &source, "jpg").expect("a stored file");    assert_eq!(stored.size_bytes, 15);    assert_eq!(stored.sha256.len(), 64);    assert_eq!(        stored.path,        layout            .client_media_dir()            .join(WORKSPACE)            .join(&stored.sha256[..2])            .join(format!("{}.jpg", stored.sha256))    );    assert_eq!(        std::fs::read(&stored.path).expect("the bytes"),        b"synthetic bytes"    );}#[test]fn the_same_bytes_converge_on_one_file_whatever_they_were_called() {    let (dir, layout) = temp_layout();    let one = dir.path().join("one.jpg");    let two = dir.path().join("two.jpg");    write(&one, b"the same bytes");    write(&two, b"the same bytes");    let first = put_media_file(&layout, WORKSPACE, &one, "jpg").expect("a stored file");    let second = put_media_file(&layout, WORKSPACE, &two, "jpg").expect("a stored file");    assert_eq!(first.path, second.path);    assert_eq!(first.sha256, second.sha256);}#[test]fn it_leaves_no_scratch_file_behind_when_the_copy_cannot_be_made() {    let (dir, layout) = temp_layout();    let source = dir.path().join("gone.jpg");    write(&source, b"synthetic bytes");    let (sha256, _) = sha256_file(&source).expect("a digest");    // The bytes are hashed first, then copied; deleting between the two is what    // a person removing a file mid-import does.    std::fs::remove_file(&source).expect("the file goes");    assert!(put_media_file(&layout, WORKSPACE, &source, "jpg").is_err());    let folder = layout.client_media_dir().join(WORKSPACE).join(&sha256[..2]);    let leftovers: Vec<String> = std::fs::read_dir(&folder)        .map(|entries| {            entries                .filter_map(Result::ok)                .map(|entry| entry.file_name().to_string_lossy().to_string())                .collect()        })        .unwrap_or_default();    assert!(leftovers.is_empty(), "{leftovers:?}");}#[test]fn a_digest_is_taken_through_a_stream_so_a_large_file_is_never_held_whole() {    let (dir, layout) = temp_layout();    let source = dir.path().join("big.bin");    // Past the 1 MB high-water mark, so more than one read happens.    write(&source, &vec![7u8; 3 * 1024 * 1024 + 11]);    let (sha256, size_bytes) = sha256_file(&source).expect("a digest");    assert_eq!(size_bytes, 3 * 1024 * 1024 + 11);    let stored = put_media_file(&layout, WORKSPACE, &source, "bin").expect("a stored file");    assert_eq!(stored.sha256, sha256);    assert_eq!(stored.size_bytes, size_bytes);}#[test]fn a_missing_file_has_no_size_rather_than_an_error() {    let (dir, _layout) = temp_layout();    assert_eq!(file_size_of(&dir.path().join("nothing.jpg")), None);    // A DIRECTORY is not a file either — a path that happens to exist is not    // an attachment.    assert_eq!(file_size_of(dir.path()), None);    let there = dir.path().join("there.jpg");    write(&there, b"1234");    assert_eq!(file_size_of(&there), Some(4));}#[test]fn a_file_with_no_extension_is_named_by_its_digest_alone() {    let (_dir, layout) = temp_layout();    let path = media_file_path(&layout, WORKSPACE, &"a".repeat(64), "");    assert!(path.file_name().expect("a name").to_string_lossy() == "a".repeat(64));}/// RULE: Client imports cannot publish media while the backup pins its file inventory.#[test]fn local_backup_refuses_a_media_import_until_the_snapshot_finishes() {    let (dir, layout) = temp_layout();    let source = dir.path().join("synthetic.txt");    write(&source, b"synthetic import");    let backup = cotton_store::media_backup_lock::snapshot(&layout.client_media_dir()).unwrap();    assert!(put_media_file(&layout, WORKSPACE, &source, "txt").is_err());    drop(backup);    assert!(put_media_file(&layout, WORKSPACE, &source, "txt").is_ok());}