Cotton code
173 lines · 6.6 KB · RustRaw
//! `project.conversation`, and the row a project page carries.//!//! Every fixture is synthetic — a workspace of zeroes and a project nobody has//! ([nothing-real-ever-goes-in-cotton-s-public-tree]). The controller comes//! from `shell::client::phone_tests::attached`, which is the one place a paired//! phone is assembled.use serde_json::{json, Value};use super::*;use crate::shell::client::phone_tests::attached;use crate::shell::client::ClientController;const PROJECT_ID: &str = "44444444-4444-4444-8444-444444444444";fn seed_project(client: &ClientController, slug: &str) {    client        .with_writer(|replica| {            replica                .driver                .run(                    "INSERT INTO cotton_memory_projects                       (id, workspace_id, slug, name, status, created_at, updated_at)                     VALUES (?,?,?,?, 'active', '2026-09-01T09:00:00.000Z',                             '2026-09-01T09:00:00.000Z')",                    &[                        rusqlite::types::Value::Text(PROJECT_ID.to_string()),                        rusqlite::types::Value::Text(replica.workspace_id.to_string()),                        rusqlite::types::Value::Text(slug.to_string()),                        rusqlite::types::Value::Text("Cotton".to_string()),                    ],                )                .map_err(|error| error.to_string())?;            Ok(())        })        .expect("the project lands");}fn open(client: &ClientController, slug: &str) -> Value {    client        .write_rows("project.conversation", &mut |ctx| {            write_conversation(ctx, slug)        })        .map(crate::authoring::own_answer)        .expect("the conversation opens")}#[test]fn the_first_call_opens_the_conversation_and_the_second_finds_it() {    let (_dir, client) = attached();    seed_project(&client, "cotton");    let first = open(&client, "cotton");    assert_eq!(first["created"], json!(true));    assert_eq!(first["kind"], json!("conversation"));    assert_eq!(first["state"], json!("queued"));    let id = first["id"].as_str().expect("an id").to_string();    // IDEMPOTENT, and that is what the project page leans on: the composer asks    // for the conversation every time it is first used.    let second = open(&client, "cotton");    assert_eq!(second["created"], json!(false));    assert_eq!(second["id"], json!(id));}#[test]fn the_row_it_opened_is_a_host_owned_conversation_on_the_project() {    let (_dir, client) = attached();    seed_project(&client, "cotton");    let id = open(&client, "cotton")["id"]        .as_str()        .expect("an id")        .to_string();    let row = client        .with_writer(|replica| {            replica                .driver                .get(                    "SELECT kind, project_id, host_owned, chosen_by, device_id, prompt, title \                       FROM cotton_sessions WHERE id = ?",                    &[rusqlite::types::Value::Text(id.clone())],                )                .map_err(|error| error.to_string())        })        .expect("the read")        .expect("the row");    assert_eq!(row.text("kind"), Some("conversation"));    assert_eq!(row.text("project_id"), Some(PROJECT_ID));    assert_eq!(row.int("host_owned"), Some(1));    // A CONVERSATION IS ALWAYS THE OWNER'S, whatever Mac it lands on — and no    // Mac is named, because it is not work placed on a machine somebody picked.    assert_eq!(row.text("chosen_by"), Some("owner"));    assert_eq!(row.text("device_id"), None);    // The standing prompt is written ONTO the row when it is born, and it names    // the row's own id — a tool call carries no session identity, so this is    // the only place a conversation can learn what to pass as    // `parentSessionId`.    let prompt = row.text("prompt").unwrap_or_default();    assert!(prompt.contains(&id), "the prompt names its own row — {prompt}");    assert!(prompt.contains("cotton"), "and the project's slug");    assert_eq!(row.text("title"), Some("Cotton — conversation"));}#[test]fn a_project_nobody_has_is_refused_by_name() {    let (_dir, client) = attached();    let refusal = client        .write_rows("project.conversation", &mut |ctx| {            write_conversation(ctx, "nowhere")        })        .expect_err("a refusal");    assert!(refusal.contains("nowhere"), "{refusal}");}#[test]fn a_read_of_a_project_with_no_conversation_answers_none_and_opens_nothing() {    let (_dir, client) = attached();    seed_project(&client, "cotton");    let found = client        .with_writer(|replica| conversation_of(replica.driver, PROJECT_ID))        .expect("the read");    assert!(        found.is_none(),        "a page being LOOKED at must not put a session on a Mac"    );    open(&client, "cotton");    let found = client        .with_writer(|replica| conversation_of(replica.driver, PROJECT_ID))        .expect("the read")        .expect("the conversation");    assert_eq!(found["state"], json!("queued"));    assert!(found["id"].as_str().is_some());    assert!(found["lastActiveAt"].as_str().is_some());    // Nothing has gone wrong, and the read says so with two nulls rather than    // two absent keys the screen would have to guess about.    assert_eq!(found["error"], Value::Null);    assert_eq!(found["machine"], Value::Null);}/// RULE: a conversation that failed to start is read with its sentence and its Mac.// Without them the project page draws the owner's first message as waiting// for ever, and nothing on the page says the conversation never started.#[test]fn a_failed_conversation_carries_why_and_where_for_the_page_to_say_so() {    let (_dir, client) = attached();    seed_project(&client, "cotton");    let opened = open(&client, "cotton");    let id = opened["id"].as_str().expect("the row").to_string();    client        .with_writer(|replica| {            replica                .driver                .run(                    "UPDATE cotton_sessions SET state = 'failed', machine = 'loop-mac',                            error = 'this session names no repository' WHERE id = ?",                    &[rusqlite::types::Value::Text(id.clone())],                )                .map_err(|error| error.to_string())?;            Ok(())        })        .expect("the failure lands");    let found = client        .with_writer(|replica| conversation_of(replica.driver, PROJECT_ID))        .expect("the read")        .expect("the conversation");    assert_eq!(found["state"], json!("failed"));    assert_eq!(found["error"], json!("this session names no repository"));    assert_eq!(found["machine"], json!("loop-mac"));}