99 lines · 5.7 KB · RustRaw
//! The Settings supporting tables through the generic records surface.mod support;use cotton_store::protocol::derived_id::project_link_id;use rusqlite::types::Value as SqlValue;use serde_json::json;use support::harness::{synthetic_uuid, Harness};use support::mcp::{call_tool, call_tool_raw, mint_token};const AT: &str = "2026-10-13T09:00:00.000Z";/// RULE: Everything's count reads need the filtered total, never the calendar's section aggregates; counting must not group every live row for headings it does not return.#[test]fn count_only_leaves_out_calendar_section_aggregates() { let mut h = Harness::new(); let token = mint_token(&mut h, &["read"], "Synthetic count reader"); for table in ["todo", "event"] { let whole = call_tool(&h, &token, "list_records", json!({"table":table,"today":"2026-10-15"})); h.db.watch_statements(); let count = call_tool(&h, &token, "list_records", json!({"table":table,"count_only":true,"today":"2026-10-15"})); assert_eq!(count["total"], whole["total"]); assert!(!h.db.statements_seen().iter().any(|sql| sql.to_ascii_uppercase().contains("GROUP BY")), "count read grouped its rows: {:?}", h.db.statements_seen()); }}fn text(value: impl Into<String>) -> SqlValue { SqlValue::Text(value.into())}fn seed(h: &Harness) -> (String, String) { let project = "settings-project"; let memory = synthetic_uuid("settings-memory"); let feedback = synthetic_uuid("settings-feedback"); h.run("INSERT INTO cotton_memory_projects (id, workspace_id, slug, name, repos, status, created_at, updated_at) VALUES (?, ?, 'settings', 'Settings project', '[]', 'active', ?, ?)", &[text(project), text(&h.workspace_id), text(AT), text(AT)]); h.run("INSERT INTO cotton_memories (id, workspace_id, slug, title, status, origin_kind, created_at, updated_at) VALUES (?, ?, 'settings-entry', 'The first settings sentence', 'active', 'user', ?, ?)", &[text(&memory), text(&h.workspace_id), text(AT), text(AT)]); h.run("INSERT INTO cotton_project_links (id, workspace_id, project_id, record_kind, record_id, applied_by, position, created_at, updated_at) VALUES (?, ?, ?, 'memory', ?, 'user', 1000, ?, ?)", &[text(project_link_id(project, "memory", &memory)), text(&h.workspace_id), text(project), text(&memory), text(AT), text(AT)]); h.run("INSERT INTO cotton_feedback (id, workspace_id, app, body, author_kind, author_label, status, created_at, updated_at) VALUES (?, ?, 'cotton-ios', 'The settings report', 'guest', 'A tester', 'open', ?, ?)", &[text(&feedback), text(&h.workspace_id), text(AT), text(AT)]); h.run("INSERT INTO cotton_project_links (id, workspace_id, project_id, record_kind, record_id, applied_by, created_at, updated_at) VALUES (?, ?, ?, 'feedback', ?, 'user', ?, ?)", &[text(project_link_id(project, "feedback", &feedback)), text(&h.workspace_id), text(project), text(&feedback), text(AT), text(AT)]); h.run("INSERT INTO cotton_repos (id, workspace_id, key, host, owner, name, kind, web_url, created_at, updated_at) VALUES ('repo', ?, 'cotton/settings', 'cotton.test', 'cotton', 'settings', 'cotton', 'https://cotton.test/cotton/settings', ?, ?)", &[text(&h.workspace_id), text(AT), text(AT)]); h.run("INSERT INTO cotton_repo_labels (id, workspace_id, repo_id, name, color, description, created_at, updated_at) VALUES ('settings-label', ?, 'repo', 'Bug', 'ff0000', 'A synthetic label', ?, ?)", &[text(&h.workspace_id), text(AT), text(AT)]); h.run("INSERT INTO cotton_repo_rules (id, workspace_id, repo_id, branch, required_contexts_json, required_approvals, block_outdated, allow_push, created_at, updated_at) VALUES ('settings-rule', ?, 'repo', 'main', '[\"test\"]', 1, 1, 0, ?, ?)", &[text(&h.workspace_id), text(AT), text(AT)]); (memory, feedback)}/// RULE: A completed import stored in SQLite is history, not the phone's live/// import picker. It must not make a transient import record appear through/// generic Settings reads after the picker has gone away.#[test]fn durable_import_history_is_not_a_message_import_projection() { let mut h = Harness::new(); let token = mint_token(&mut h, &["read"], "Settings test"); h.run( "INSERT INTO cotton_imports \ (id, workspace_id, source, title, added, skipped, finished_at, created_at) \ VALUES ('durable-import', ?, 'chat_export', 'Old import', 2, 1, ?, ?)", &[text(&h.workspace_id), text(AT), text(AT)], ); assert!( call_tool_raw( &h, &token, "list_records", json!({"table":"message_import","limit":50}), ) .is_error, "durable import history escaped as a transient message-import record" );}/// RULE: Repository labels and branch rules are host-owned views. A generic/// records write must not create a local edit or delete that bypasses their/// named repository engine commands.#[test]fn repository_settings_views_refuse_generic_writes() { let mut h = Harness::new(); let token = mint_token(&mut h, &["read", "write"], "Settings test"); seed(&h); for (table, id, patch) in [ ("repo_label", "settings-label", json!({"name":"Changed"})), ("repo_rule", "settings-rule", json!({"allow_push":true})), ] { assert!( call_tool_raw( &h, &token, "update_record", json!({"table":table,"id":id,"patch":patch}), ) .is_error, "{table} accepted a generic update instead of its host command" ); assert!( call_tool_raw(&h, &token, "delete_record", json!({"table":table,"id":id})).is_error, "{table} accepted a generic delete instead of its host command" ); }}