Cotton code
91 lines · 4.4 KB · RustRaw
//! Documents and Artifacts as a reader sees them: Artifacts is views over//! Documents' table, narrowed to the pages filed as `text/html`, and a paper's//! state is said by the database's `documents` lines.mod support;use rusqlite::types::Value as SqlValue;use serde_json::json;use support::{    mcp::{call_tool, mint_token},    Harness,};const AT: &str = "2026-08-23T03:00:00.000Z";fn file(h: &Harness, id: &str, name: &str, mime: &str, sha: char) {    file_as(h, id, name, mime, sha, "ready", None, AT);}#[allow(clippy::too_many_arguments)]fn file_as(h: &Harness, id: &str, name: &str, mime: &str, sha: char, status: &str, code: Option<&str>, at: &str) {    let text = |value: &str| SqlValue::Text(value.to_string());    h.run(        "INSERT INTO cotton_documents (id, workspace_id, title, original_name, mime, byte_size, sha256, storage_path, \         status, failed_stage, error_code, status_changed_at, created_at, updated_at) \         VALUES (?, ?, ?, ?, ?, 10, ?, ?, ?, ?, ?, ?, ?, ?)",        &[            text(id),            text(&h.workspace_id),            text(name),            text(name),            text(mime),            text(&sha.to_string().repeat(64)),            text(&format!("{}/{id}/original", h.workspace_id)),            text(status),            if status == "failed" { text("extract") } else { SqlValue::Null },            code.map_or(SqlValue::Null, text),            text(at),            text(at),            text(at),        ],    );}/// RULE: the view Artifacts reads (`where {mime: "text/html"}`, Made for you/// since Web pages' All became the shared links, 5 Nov 2026) names a column/// the document definition declares. Artifacts lost its own copy of the table/// (it reads Documents' since 21 Oct) and the copy it dropped was the only one/// that declared `mime`, so every read was refused ("Documents have no column/// \"mime\"") and Artifacts stood empty on the owner's phone.#[test]fn artifacts_lists_the_html_documents_and_nothing_else() {    let mut h = Harness::bare();    let token = mint_token(&mut h, &["read", "write"], "Synthetic reader");    call_tool(&h, &token, "add_tool", json!({"tool_id": "documents"}));    call_tool(&h, &token, "add_tool", json!({"tool_id": "artifacts"}));    file(&h, "00000000-0000-4000-8000-0000000000a1", "Otter tide chart.html", "text/html", 'a');    file(&h, "00000000-0000-4000-8000-0000000000b2", "Heron bank statement.pdf", "application/pdf", 'b');    let read = call_tool(&h, &token, "list_records", json!({"table": "document", "view": "artifacts/made"}));    let titles: Vec<&str> = read["records"]        .as_array()        .unwrap_or_else(|| panic!("a list: {read}"))        .iter()        .map(|record| record["data"]["title"].as_str().unwrap_or_default())        .collect();    assert_eq!(titles, vec!["Otter tide chart.html"], "{read}");}/// RULE (contract 3; owner's ask, 22 Oct 2026): a paper's state is a line of/// set `documents`, and one still `processing` an hour after filing says why/// it waits when the worker wrote the reason — `model_held` reads "Waiting for/// the assistant's credits", not "Opening it…" for ever.#[test]fn a_paper_waiting_on_the_model_says_why_after_an_hour() {    let mut h = Harness::bare();    let token = mint_token(&mut h, &["read", "write"], "Synthetic reader");    call_tool(&h, &token, "add_tool", json!({"tool_id": "documents"}));    let long_ago = "2020-01-01T00:00:00.000Z";    file_as(&h, "00000000-0000-4000-8000-0000000000c3", "Heron letter.pdf", "application/pdf", 'c', "processing", Some("model_held"), long_ago);    file_as(&h, "00000000-0000-4000-8000-0000000000d4", "Heron note.pdf", "application/pdf", 'd', "processing", None, long_ago);    file_as(&h, "00000000-0000-4000-8000-0000000000e5", "Heron scan.pdf", "application/pdf", 'e', "failed", Some("no_file"), long_ago);    let read = call_tool(&h, &token, "list_records", json!({"table": "document"}));    let said = |name: &str| {        read["records"].as_array().unwrap().iter()            .find(|record| record["data"]["original_name"] == name)            .and_then(|record| record["data"]["state_message"].as_str().map(str::to_string))    };    assert_eq!(said("Heron letter.pdf").as_deref(), Some("Waiting for the assistant's credits"), "{read}");    assert_eq!(said("Heron note.pdf").as_deref(), Some("Opening it…"));    assert_eq!(said("Heron scan.pdf").as_deref(), Some("The file for this document couldn't be found. Add it again."));}