Cotton code
51 lines · 2.0 KB · RustRaw
//! The Camera tool's shot: one photo row per shot. Synthetic bytes only.use std::path::Path;use serde_json::{json, Value};use crate::shell::client::phone_tests::attached;use crate::shell::client::ClientController;/// RULE: A camera shot is saved the moment it is taken as exactly one photo row, so nothing the person shot ever waits on understanding.#[test]fn a_shot_is_saved_at_once_as_one_photo_row() {    let (dir, client) = attached();    let before = count(&client, "cotton_photos");    let answer = shot_on_phone(&client, dir.path(), 1);    assert!(answer["recordId"].as_str().unwrap().starts_with("photo:"));    assert_eq!(count(&client, "cotton_photos"), before + 1);}/// RULE: A camera shot is a photo or a video; anything else is refused before a byte moves.#[test]fn a_shot_is_a_photo_or_a_video() {    assert!(super::camera::validate_shot(&json!({"path": "/tmp/x.pdf", "mime": "application/pdf", "kind": "document"})).is_err());}fn shot_on_phone(client: &ClientController, dir: &Path, n: usize) -> Value {    let path = dir.join(format!("shot-{n}.jpg"));    std::fs::write(&path, format!("synthetic shot {n}")).expect("a synthetic shot");    let args = super::camera::validate_shot(&json!({        "path": path.to_string_lossy(), "mime": "image/jpeg", "filename": format!("shot-{n}.jpg"), "kind": "photo",    }))    .map_err(|e| e.message)    .expect("a camera's arguments");    let layout = client.layout().clone();    let answer = client        .write_rows("camera.shot", &mut |ctx| super::camera::write_shot(ctx, &layout, &args, client.self_device_id().as_deref()))        .expect("the shot is saved");    crate::authoring::own_answer(answer)}fn count(client: &ClientController, table: &str) -> i64 {    client        .with_writer(|f| {            f.driver                .get(&format!("SELECT COUNT(*) AS n FROM {table}"), &[])                .map_err(|e| e.to_string())?                .and_then(|r| r.int("n"))                .ok_or_else(|| "no count".to_string())        })        .unwrap()}