//! `file_thought` into ANY table — a built-in or one a tool brought — and its//! take-back, through the owner's own dispatch.mod support;use base64::{engine::general_purpose::STANDARD, Engine};use cotton_core::definitions::{package::ToolPackage, TableDefinition, ToolDefinition};use rusqlite::types::ValueasSqlValue;use serde_json::{json, Value};use support::harness::{synthetic_uuid, Harness};use support::mcp::{call_tool, call_tool_as_owner, mint_token};/// The device id the owner's app writes under. Synthetic, like every fixture.const DEVICE: &str = "device:00000000000000000000000000";const AT: &str = "2026-09-01T09:00:00.000Z";/// A household with the classroom tool imported: `lesson` is a table the/// owner made, its title column `title`.fn classroom_household() -> Harness {letmut h = Harness::new();let token = mint_token(&mut h, &["read", "write"], "Teacher");let tool: ToolDefinition = serde_json::from_str(include_str!("fixtures/classroom/manifest.json")).unwrap();let tables: Vec<TableDefinition> = [ include_str!("fixtures/classroom/tables/lesson.json"), include_str!("fixtures/classroom/tables/class.json"), include_str!("fixtures/classroom/tables/student.json"), include_str!("fixtures/classroom/tables/grade.json"), ] .into_iter() .map(|s| serde_json::from_str(s).unwrap()) .collect();let package = ToolPackage { board: None, tool, tables, pictures: Default::default(), samples: Default::default() };let bytes = cotton_core::definitions::package::encode_unsigned_package(&package).unwrap(); call_tool(&h, &token, "import_tool", json!({ "bytes": STANDARD.encode(bytes) })); h}/// A capture still waiting to be filed, holding `words`.fn waiting_thought(h: &Harness, words: &str) -> String {let id = synthetic_uuid("thought"); h.with(|db| { db.run("INSERT INTO cotton_inbox_items (id, workspace_id, raw_text, captured_at, route_status, \ routed_records, created_at, updated_at) VALUES (?,?,?,?,'pending','[]',?,?)", &[SqlValue::Text(id.clone()),SqlValue::Text(h.workspace_id.clone()),SqlValue::Text(words.to_string()),SqlValue::Text(AT.to_string()),SqlValue::Text(AT.to_string()),SqlValue::Text(AT.to_string()), ], ) }) .expect("the thought is written"); id}fn thought(h: &Harness, id: &str) -> Value { h.one("SELECT route_status, routed_kind, routed_id, routed_records FROM cotton_inbox_items WHERE id = ?", &[SqlValue::Text(id.to_string())], ) .map(Value::Object) .expect("the thought is there")}fn owner(h: &Harness, name: &str, args: Value) -> Value {let answer = call_tool_as_owner(h, DEVICE, name, args); assert!(!answer.is_error, "{name} was refused: {}", answer.text); answer.json()}fn refused(h: &Harness, name: &str, args: Value) -> String {let answer = call_tool_as_owner(h, DEVICE, name, args); assert!(answer.is_error, "{name} ran where it should have been refused: {}", answer.text); answer.text}/// RULE: A waiting thought is filed into any table the database has — one a tool brought included — its words in that table's title column, the thought stamped as filed into that record, and the receipt naming it.#[test]fn a_thought_is_filed_into_a_table_the_owner_made() {let h = classroom_household();let id = waiting_thought(&h, "Fractions with pizza slices");let filed = owner(&h, "file_thought", json!({ "id": id, "as": "lesson" }));let lesson = filed["became"][0]["id"].as_str().expect("the record it became").to_string(); assert_eq!(filed["became"][0]["kind"], "lesson");let receipt = filed["receipt"].as_str().unwrap_or_default(); assert!(receipt.contains("Fractions with pizza slices"), "the receipt names the record: {receipt}");let record = owner(&h, "get_record_by_table", json!({ "table": "lesson", "id": lesson })); assert_eq!(record["data"]["title"], "Fractions with pizza slices", "{record}");let row = thought(&h, &id); assert_eq!(row["route_status"], "corrected"); assert_eq!(row["routed_kind"], "lesson"); assert_eq!(row["routed_id"], json!(lesson));}/// RULE: Taking back a thought filed into a table no router files into is that record's own Undo — the untouched record goes and the thought waits again — and a record changed since stays, with the filing.#[test]fn taking_back_a_filing_into_any_table_is_the_add_s_own_undo() {let h = classroom_household();let id = waiting_thought(&h, "Leaf rubbings in the yard");let filed = owner(&h, "file_thought", json!({ "id": id, "as": "lesson" }));let lesson = filed["became"][0]["id"].as_str().unwrap().to_string();let back = owner(&h, "file_thought", json!({ "id": id, "as": null })); assert_eq!(back["routeStatus"], "pending"); assert_eq!(back["removed"], 1); assert_eq!(thought(&h, &id)["route_status"], "pending");// The add's Undo bins the lesson, as it does for any add.let gone = owner(&h, "get_record_by_table", json!({ "table": "lesson", "id": lesson })); assert!(gone["deleted_at"].is_string(), "the lesson is in the bin: {gone}");// Filed again, then the lesson is edited: the take-back is refused whole.let filed = owner(&h, "file_thought", json!({ "id": id, "as": "lesson" }));let lesson = filed["became"][0]["id"].as_str().unwrap().to_string(); owner(&h, "update_record", json!({ "table": "lesson", "id": lesson, "patch": { "title": "Leaf rubbings, bring crayons" } }));let kept = refused(&h, "file_thought", json!({ "id": id, "as": null })); assert!(kept.contains("changed since"), "{kept}"); assert_eq!(thought(&h, &id)["routed_kind"], "lesson", "the filing stays with the record");let record = owner(&h, "get_record_by_table", json!({ "table": "lesson", "id": lesson })); assert_eq!(record["data"]["title"], "Leaf rubbings, bring crayons");}/// RULE: A thought is filed into a table, never into Thoughts itself or a table this database does not have, and a table that needs more than the words is refused with the thought still waiting.#[test]fn filing_into_no_such_table_or_thoughts_is_refused_and_the_thought_waits() {let h = classroom_household();let id = waiting_thought(&h, "Dentist on Thursday");// Thoughts is no kit tool's table, so it is refused as no tool's. refused(&h, "file_thought", json!({ "id": id, "as": "thought" })); refused(&h, "file_thought", json!({ "id": id, "as": "no_such_table" }));let bad = refused(&h, "file_thought", json!({ "id": id, "as": "Lesson!" })); assert!(bad.contains("table"), "{bad}");// An event needs when it starts; a person's handle needs its person.let event = refused(&h, "file_thought", json!({ "id": id, "as": "event" })); assert!(event.contains("Starts is required"), "{event}");let handle = refused(&h, "file_thought", json!({ "id": id, "as": "person_handle" })); assert!(handle.contains("Person is required"), "{handle}"); assert_eq!(h.count("SELECT COUNT(*) FROM cotton_person_handles", &[]), 0, "nothing half-made"); assert_eq!(thought(&h, &id)["route_status"], "pending"); assert_eq!(thought(&h, &id)["routed_kind"], Value::Null);}/// RULE: A person — whose avatar is a read-only link to a photo, never a parent — is filed into by the same create every list runs, and taken back by its Undo.#[test]fn a_thought_is_filed_into_a_person() {let h = classroom_household();let id = waiting_thought(&h, "Rosalind Okafor");let filed = owner(&h, "file_thought", json!({ "id": id, "as": "person" }));let person = filed["became"][0]["id"].as_str().expect("the person it became").to_string();let record = owner(&h, "get_record_by_table", json!({ "table": "person", "id": person })); assert_eq!(record["data"]["full_name"], "Rosalind Okafor", "{record}"); assert_eq!(thought(&h, &id)["routed_kind"], "person"); owner(&h, "file_thought", json!({ "id": id, "as": null })); assert_eq!(thought(&h, &id)["route_status"], "pending");}/// RULE: `file_thought` refuses, before anything is made, every table the one fileable rule refuses — one whose title is not typed, one that needs more than the words, one nothing is added to by hand — each with a sentence saying why, and the thought keeps waiting.#[test]fn filing_into_a_table_the_rule_refuses_is_refused_before_anything_is_made() {let h = classroom_household();let id = waiting_thought(&h, "Savings at the co-op bank");let sender = refused(&h, "file_thought", json!({ "id": id, "as": "email_sender" })); assert!(sender.contains("no title a thought's words can go in"), "{sender}");let language = refused(&h, "file_thought", json!({ "id": id, "as": "person_language" })); assert!(language.contains("needs more than the thought's words"), "{language}");let album = refused(&h, "file_thought", json!({ "id": id, "as": "album" })); assert!(album.contains("by hand"), "{album}");for table in ["email_sender", "person_language", "album"] {let store = cotton_core::definitions::builtin_store_table(table).expect("a kit table's store"); assert_eq!(h.count(&format!("SELECT COUNT(*) FROM {store}"), &[]), 0, "nothing made in {table}"); } assert_eq!(thought(&h, &id)["route_status"], "pending"); assert_eq!(thought(&h, &id)["routed_kind"], Value::Null);}