//! Schema v40 over MCP: the utility filter and the analysed fields on a photo, //! the owner's answer about a face, and the near-duplicate proposals — driven //! through `dispatch::call_tool` against a fake `ToolServices`. //! //! Every name, number and line of text is synthetic. mod support; use cotton_store::protocol::derived_id::{media_duplicate_id, media_face_id}; use rusqlite::types::Value as SqlValue; use serde_json::{json, Map, Value}; use support::harness::{synthetic_uuid, Harness}; use support::mcp::{call_tool, call_tool_raw, mint_token}; const AT: &str = "2026-09-01T00:00:00.000Z"; fn sha(ch: &str) -> String { ch.repeat(64) } fn t(value: &str) -> SqlValue { SqlValue::Text(value.to_string()) } fn run(h: &Harness, sql: &str, params: Vec) { h.run(sql, ¶ms); } /// One face row, whole — the columns the WORKER writes, which no tool answers /// back and only the file can be asked for. fn face_row(h: &Harness, id: &str) -> Map { h.one("SELECT * FROM cotton_media_faces WHERE id = ?", &[t(id)]) .expect("the face row") } fn face_value(row: &Map, name: &str) -> Value { row.get(name).cloned().unwrap_or(Value::Null) } #[derive(Default)] struct PhotoExtra<'a> { ocr_text: Option<&'a str>, is_utility: Option, phash: Option<&'a str>, } fn insert_photo(h: &Harness, hash: &str, extra: PhotoExtra<'_>) -> String { let id = format!("photo:{hash}"); let ws = h.workspace_id.clone(); run( h, "INSERT INTO cotton_photos (id, workspace_id, key, sha256, content_type, size_bytes, width, height, taken_at, created_at, updated_at, ocr_text, is_utility, phash) VALUES (?, ?, ?, ?, 'image/jpeg', 1000, 100, 80, ?, ?, ?, ?, ?, ?)", vec![ t(&id), t(&ws), t(&format!("{ws}/photos/{hash}.jpg")), t(hash), t(AT), t(AT), t(AT), extra.ocr_text.map_or(SqlValue::Null, t), extra.is_utility.map_or(SqlValue::Null, SqlValue::Integer), extra.phash.map_or(SqlValue::Null, t), ], ); id } fn insert_person(h: &Harness, name: &str) -> String { let id = synthetic_uuid("person"); run( h, "INSERT INTO cotton_people (id, workspace_id, full_name, contact_source, created_at, updated_at) VALUES (?, ?, ?, 'manual', ?, ?)", vec![t(&id), t(&h.workspace_id), t(name), t(AT), t(AT)], ); id } fn insert_face( h: &Harness, photo_id: &str, source_id: &str, person_id: Option<&str>, suggested_person_id: Option<&str>, suggested_distance: Option, ) -> String { let id = media_face_id("photo", photo_id, "cotton", source_id); run( h, "INSERT INTO cotton_media_faces (id, workspace_id, record_kind, record_id, source, source_id, x, y, w, h, quality, cluster_id, person_id, suggested_person_id, suggested_distance, created_at, updated_at) VALUES (?, ?, 'photo', ?, 'cotton', ?, 0.3, 0.1, 0.2, 0.2, 0.7, NULL, ?, ?, ?, ?, ?)", vec![ t(&id), t(&h.workspace_id), t(photo_id), t(source_id), person_id.map_or(SqlValue::Null, t), suggested_person_id.map_or(SqlValue::Null, t), suggested_distance.map_or(SqlValue::Null, SqlValue::Real), t(AT), t(AT), ], ); id } fn insert_duplicate(h: &Harness, keep_id: &str, copy_id: &str, distance: i64) -> String { let id = media_duplicate_id(keep_id, copy_id); run( h, "INSERT INTO cotton_media_duplicates (id, workspace_id, record_kind, keep_id, copy_id, distance, method, state, decided_at, created_at, updated_at) VALUES (?, ?, 'photo', ?, ?, ?, 'phash', 'open', NULL, ?, ?)", vec![ t(&id), t(&h.workspace_id), t(keep_id), t(copy_id), SqlValue::Integer(distance), t(AT), t(AT), ], ); id } // ── what the Mac learned ──────────────────────────────────────────────────── #[test] fn filters_by_utility_and_answers_text_and_utility_only_once_analysed() { let mut h = Harness::new(); let secret = mint_token(&mut h, &["read", "write"], "Claude"); let receipt = insert_photo( &h, &sha("a"), PhotoExtra { ocr_text: Some("TOTAL 1,240.00\nTHANK YOU"), is_utility: Some(1), ..PhotoExtra::default() }, ); let beach = insert_photo( &h, &sha("b"), PhotoExtra { is_utility: Some(0), ..PhotoExtra::default() }, ); let fresh = insert_photo(&h, &sha("c"), PhotoExtra::default()); let utility = call_tool(&h, &secret, "list_photos", json!({ "utility": true })); assert_eq!( utility["photos"] .as_array() .expect("a list") .iter() .map(|row| row["id"].clone()) .collect::>(), vec![json!(receipt)] ); assert_eq!( utility["photos"][0]["text"], json!("TOTAL 1,240.00\nTHANK YOU") ); assert_eq!(utility["photos"][0]["utility"], json!(true)); let photographs = call_tool(&h, &secret, "list_photos", json!({ "utility": false })); assert_eq!( photographs["photos"] .as_array() .expect("a list") .iter() .map(|row| row["id"].clone()) .collect::>(), vec![json!(beach)] ); let one = call_tool(&h, &secret, "get_photo", json!({ "id": fresh })); assert!(one.get("text").is_none(), "{one}"); assert!(one.get("utility").is_none(), "{one}"); let bad = call_tool_raw(&h, &secret, "list_photos", json!({ "utility": "yes" })); assert!(bad.is_error); } // ── update_face ───────────────────────────────────────────────────────────── #[test] fn shows_the_macs_suggestion_and_naming_the_person_clears_it() { let mut h = Harness::new(); let secret = mint_token(&mut h, &["read", "write"], "Claude"); let photo = insert_photo(&h, &sha("a"), PhotoExtra::default()); let ravi = insert_person(&h, "Ravi"); let face = insert_face(&h, &photo, "vision:0", None, Some(&ravi), Some(0.31)); let shown = call_tool(&h, &secret, "get_photo", json!({ "id": photo })); assert_eq!(shown["faces"].as_array().expect("faces").len(), 1); assert_eq!(shown["faces"][0]["id"], json!(face)); assert_eq!(shown["faces"][0]["person"], Value::Null); assert_eq!( shown["faces"][0]["suggestion"], json!({ "person": { "id": ravi, "name": "Ravi" }, "distance": 0.31 }) ); let named = call_tool( &h, &secret, "update_face", json!({ "id": face, "personId": ravi }), ); assert_eq!(named["photoId"], json!(photo)); assert_eq!(named["face"]["person"]["id"], json!(ravi)); assert_eq!(named["face"]["suggestion"], Value::Null); let row = face_row(&h, &face); assert_eq!(face_value(&row, "person_id"), json!(ravi)); assert_eq!(face_value(&row, "suggested_person_id"), Value::Null); assert_eq!(face_value(&row, "suggested_distance"), Value::Null); let cleared = call_tool( &h, &secret, "update_face", json!({ "id": face, "personId": null }), ); assert_eq!(cleared["face"]["person"], Value::Null); } #[test] fn dismissing_a_suggestion_stamps_the_face_and_refuses_when_there_is_none() { let mut h = Harness::new(); let secret = mint_token(&mut h, &["read", "write"], "Claude"); let photo = insert_photo(&h, &sha("a"), PhotoExtra::default()); let ravi = insert_person(&h, "Ravi"); let face = insert_face(&h, &photo, "vision:0", None, Some(&ravi), Some(0.4)); let dismissed = call_tool( &h, &secret, "update_face", json!({ "id": face, "dismissSuggestion": true }), ); assert_eq!(dismissed["face"]["suggestion"], Value::Null); assert!( dismissed["face"]["suggestionDismissedAt"].is_string(), "{dismissed}" ); assert!(face_value(&face_row(&h, &face), "suggestion_dismissed_at").is_string()); let again = call_tool_raw( &h, &secret, "update_face", json!({ "id": face, "dismissSuggestion": true }), ); assert!(again.is_error); } #[test] fn refuses_a_call_that_decides_nothing_both_at_once_an_unknown_person_an_unknown_face() { let mut h = Harness::new(); let secret = mint_token(&mut h, &["read", "write"], "Claude"); let photo = insert_photo(&h, &sha("a"), PhotoExtra::default()); let face = insert_face(&h, &photo, "vision:0", None, None, None); for args in [ json!({ "id": face }), json!({ "id": face, "personId": "p", "dismissSuggestion": true }), json!({ "id": face, "personId": synthetic_uuid("nobody") }), json!({ "id": "nope", "personId": null }), ] { let refused = call_tool_raw(&h, &secret, "update_face", args.clone()); assert!(refused.is_error, "for {args}"); } } // ── list_duplicates / decide_duplicate ────────────────────────────────────── #[test] fn groups_proposals_by_keep_filters_by_state_and_records_the_decision() { let mut h = Harness::new(); let secret = mint_token(&mut h, &["read", "write"], "Claude"); let a = insert_photo( &h, &sha("a"), PhotoExtra { phash: Some("0000000000000000"), ..PhotoExtra::default() }, ); let b = insert_photo( &h, &sha("b"), PhotoExtra { phash: Some("0000000000000007"), ..PhotoExtra::default() }, ); let c = insert_photo( &h, &sha("c"), PhotoExtra { phash: Some("000000000000003f"), ..PhotoExtra::default() }, ); let x = insert_photo( &h, &sha("d"), PhotoExtra { phash: Some("ffffffffffffffff"), ..PhotoExtra::default() }, ); let y = insert_photo( &h, &sha("e"), PhotoExtra { phash: Some("fffffffffffffffe"), ..PhotoExtra::default() }, ); let ab = insert_duplicate(&h, &a, &b, 3); insert_duplicate(&h, &a, &c, 6); insert_duplicate(&h, &x, &y, 1); let all = call_tool(&h, &secret, "list_duplicates", json!({})); assert_eq!(all["total"], json!(3)); let mut expected = vec![a.clone(), x.clone()]; expected.sort(); assert_eq!( all["groups"] .as_array() .expect("groups") .iter() .map(|group| group["keepId"].as_str().unwrap_or_default().to_string()) .collect::>(), expected ); let group_a = all["groups"] .as_array() .expect("groups") .iter() .find(|group| group["keepId"] == json!(a)) .expect("the group keyed on a"); assert_eq!( group_a["copies"] .as_array() .expect("copies") .iter() .map(|copy| (copy["copyId"].clone(), copy["distance"].clone())) .collect::>(), vec![(json!(b), json!(3)), (json!(c), json!(6))] ); let decided = call_tool( &h, &secret, "decide_duplicate", json!({ "id": ab, "state": "confirmed" }), ); assert_eq!(decided["keepId"], json!(a)); assert_eq!(decided["state"], json!("confirmed")); assert!(decided["decidedAt"].is_string(), "{decided}"); // Nothing was deleted. let still_there = call_tool(&h, &secret, "get_photo", json!({ "id": b })); assert_eq!(still_there["id"], json!(b)); let open = call_tool( &h, &secret, "list_duplicates", json!({ "state": "open", "keep": a }), ); assert_eq!(open["total"], json!(1)); assert_eq!(open["groups"][0]["copies"][0]["copyId"], json!(c)); let reopened = call_tool( &h, &secret, "decide_duplicate", json!({ "id": ab, "state": "open" }), ); assert_eq!(reopened["state"], json!("open")); assert!(reopened.get("decidedAt").is_none(), "{reopened}"); for args in [ json!({ "id": ab, "state": "kept" }), json!({ "id": ab }), json!({ "id": "nope", "state": "dismissed" }), ] { let refused = call_tool_raw(&h, &secret, "decide_duplicate", args.clone()); assert!(refused.is_error, "for {args}"); } let bad_keep = call_tool_raw( &h, &secret, "list_duplicates", json!({ "keep": "photo:short" }), ); assert!(bad_keep.is_error); }