Cotton code
48 lines · 2.6 KB · RustRaw
//! `export_package` and `import_package`: the Cotton package over MCP.mod support;use serde_json::{json, Value};use support::fakes::FakeServices;use support::harness::Harness;use support::mcp::{call_tool, call_tool_raw, mint_token};fn todo(h: &Harness, secret: &str, title: &str) -> String {    let made = call_tool(h, secret, "create_record", json!({"table": "todo", "data": {"title": title}}));    made["record"]["id"].as_str().or_else(|| made["id"].as_str()).unwrap_or_else(|| panic!("an id in {made}")).to_string()}/// RULE: `export_package` files the package as a ready document of this database and answers its id with what each table held; `import_package` of that document into the same database finds every record already there and changes nothing.#[test]fn an_exported_package_is_a_document_and_imports_back_unchanged() {    let mut h = Harness::new();    let secret = mint_token(&mut h, &["read", "write"], "Packager");    todo(&h, &secret, "Buy milk");    todo(&h, &secret, "Call the bank");    let exported = call_tool(&h, &secret, "export_package", json!({"tables": ["todo"]}));    let document = exported["document"].as_str().unwrap_or_else(|| panic!("a document in {exported}")).to_string();    assert_eq!(exported["tables"][0]["table"], "cotton_todos", "{exported}");    assert!(exported["tables"][0]["rows"].as_u64().unwrap_or(0) >= 2, "{exported}");    let row = h.one("SELECT status, mime, storage_path FROM cotton_documents WHERE id = ?", &[rusqlite::types::Value::Text(document.clone())]).expect("the document row");    assert_eq!(row["status"], "ready");    assert_eq!(row["mime"], "application/zip");    assert!(row["storage_path"].as_str().unwrap_or_default().ends_with(".cottonpkg"));    let imported = call_tool(&h, &secret, "import_package", json!({"document": document}));    assert_eq!(imported["created"], json!({}), "{imported}");    assert_eq!(imported["refused"], json!([]), "{imported}");    assert!(imported["unchanged"]["cotton_todos"].as_u64().unwrap_or(0) >= 2, "{imported}");}/// RULE: Both package tools refuse by name on a server with no object store, rather than half-working.#[test]fn without_an_object_store_both_tools_refuse_by_name() {    let mut h = Harness::with_services(FakeServices::none());    let secret = mint_token(&mut h, &["read", "write"], "Packager");    for (tool, args) in [("export_package", json!({"tables": ["todo"]})), ("import_package", json!({"document": "nope"}))] {        let answer = call_tool_raw(&h, &secret, tool, args);        let text: Value = answer.result.clone();        assert!(text.to_string().contains("no object store"), "{tool}: {text}");    }}