Cotton code
182 lines · 7.1 KB · RustRaw
//! A repeating todo is one slip that says "missed n", and one tick clears the//! run: the ticked occurrence is DONE, the earlier ones still open are MISSED//! — never done — so the history stays true, and one Undo opens the run again.//!//! Driven over `dispatch::call_tool` against a real store, through the generic//! verbs (`update_record`, `undo`) the phone's tick runs on and through the//! todo tools' own `complete_todo` and `update_todo`.mod support;use rusqlite::types::Value as SqlValue;use serde_json::{json, Map, Value};use support::harness::Harness;use support::mcp::call_tool_as_owner;use support::synthetic_uuid;/// The device id the owner's app writes under. Synthetic, like every fixture.const DEVICE: &str = "device:00000000000000000000000000";/// The harness clock is fixed at 2026-09-08T10:00:00Z, a Tuesday.const AT: &str = "2026-09-08T10:00:00.000Z";/// Three Wednesdays gone, and the one to come.const FIRST: &str = "2026-08-19T09:00:00.000Z";const SECOND: &str = "2026-08-26T09:00:00.000Z";const THIRD: &str = "2026-09-02T09:00:00.000Z";const NEXT: &str = "2026-09-09T09:00:00.000Z";const TITLE: &str = "Water the plants";fn ok(h: &Harness, tool: &str, args: Value) -> Value {    let answer = call_tool_as_owner(h, DEVICE, tool, args);    assert!(!answer.is_error, "{tool}: {}", answer.text);    answer.json()}fn zoned() -> Harness {    let h = Harness::new();    h.with(|db| {        db.run(            "INSERT INTO cotton_news_settings (workspace_id, timezone, created_at, updated_at) \             VALUES (?, 'UTC', ?, ?)",            &[                SqlValue::Text(h.workspace_id.clone()),                SqlValue::Text(AT.to_string()),                SqlValue::Text(AT.to_string()),            ],        )    })    .expect("the zone lands");    h}fn seed_todo(h: &Harness, label: &str, due_at: &str) -> String {    let id = synthetic_uuid(label);    h.with(|db| {        db.run(            "INSERT INTO cotton_todos (id, workspace_id, title, status, due_at, priority, \             created_at, updated_at) VALUES (?, ?, ?, 'open', ?, 0, ?, ?)",            &[                SqlValue::Text(id.clone()),                SqlValue::Text(h.workspace_id.clone()),                SqlValue::Text(TITLE.to_string()),                SqlValue::Text(due_at.to_string()),                SqlValue::Text(AT.to_string()),                SqlValue::Text(AT.to_string()),            ],        )    })    .expect("the todo lands");    id}fn todo_row(h: &Harness, id: &str) -> Map<String, Value> {    h.one(        "SELECT status, completed_at FROM cotton_todos WHERE id = ?",        &[SqlValue::Text(id.to_string())],    )    .expect("the todo is there")}fn status(h: &Harness, id: &str) -> String {    todo_row(h, id)["status"].as_str().unwrap_or_default().to_string()}/// A weekly series anchored three Wednesdays back: three late open/// occurrences and the one to come, by occurrence. A pass beside an open copy/// makes nothing (one live copy per series, 882e949a7), so the two middle/// weeks and the one to come are seeded as the older passes left them.fn late_series(h: &Harness) -> [String; 4] {    let todo = seed_todo(h, "plants", FIRST);    let template = ok(        h,        "make_todo_recurring",        json!({ "todoId": todo, "rrule": "FREQ=WEEKLY;BYDAY=WE", "dtstart": FIRST }),    );    let template = template["id"].as_str().expect("an id").to_string();    for (label, at) in [("plants-second", SECOND), ("plants-third", THIRD), ("plants-next", NEXT)] {        let id = seed_todo(h, label, at);        h.with(|db| {            db.run(                "UPDATE cotton_todos SET recurrence_id = ?, occurrence_at = ? WHERE id = ?",                &[SqlValue::Text(template.clone()), SqlValue::Text(at.to_string()), SqlValue::Text(id.clone())],            )        })        .expect("the week's occurrence lands");    }    [FIRST, SECOND, THIRD, NEXT].map(|at| {        h.one(            "SELECT id FROM cotton_todos WHERE recurrence_id = ? AND occurrence_at = ?",            &[SqlValue::Text(template.clone()), SqlValue::Text(at.to_string())],        )        .and_then(|row| row.get("id").and_then(Value::as_str).map(str::to_string))        .unwrap_or_else(|| panic!("the occurrence at {at} is there"))    })}fn tick(h: &Harness, id: &str) -> Value {    ok(        h,        "update_record",        json!({ "table": "todo", "id": id, "patch": { "status": "done" } }),    )}/// RULE: One tick on a repeating todo clears its missed run: the ticked one is done, the earlier open ones are missed and never done, the receipt counts them, and one Undo opens all of them again.#[test]fn one_tick_clears_the_missed_run_and_one_undo_opens_it_again() {    let h = zoned();    let [first, second, third, next] = late_series(&h);    let done = tick(&h, &third);    assert_eq!(status(&h, &third), "done");    for earlier in [&first, &second] {        let row = todo_row(&h, earlier);        assert_eq!(row["status"], "missed", "an earlier occurrence is missed, never done");        assert!(row["completed_at"].is_null(), "a missed occurrence was never completed");    }    assert_eq!(status(&h, &next), "open", "a later occurrence is untouched");    assert_eq!(done["change"]["receipt"]["said"], format!("Done {TITLE} · 2 earlier missed cleared"));    assert_eq!(done["change"]["undoable"], true);    let undone = ok(&h, "undo", json!({ "change_id": done["change"]["id"] }));    assert_eq!(undone["change"]["receipt"]["said"], "Undone", "the run's Undo says Undone");    for id in [&first, &second, &third, &next] {        assert_eq!(status(&h, id), "open", "one Undo restores the whole run");    }}/// RULE: complete_todo is the same tick: completing a repeating todo's occurrence marks the earlier open ones of its series missed.#[test]fn complete_todo_marks_the_earlier_run_missed() {    let h = zoned();    let [first, second, third, next] = late_series(&h);    ok(&h, "complete_todo", json!({ "id": third }));    assert_eq!(status(&h, &third), "done");    assert_eq!(status(&h, &first), "missed");    assert_eq!(status(&h, &second), "missed");    assert_eq!(status(&h, &next), "open");}/// RULE: Ticking a todo that does not repeat says "Done <title>" and changes nothing but that todo.#[test]fn a_todo_that_does_not_repeat_is_just_done() {    let h = zoned();    let once = seed_todo(&h, "once", SECOND);    let other = seed_todo(&h, "other", FIRST);    let done = tick(&h, &once);    assert_eq!(done["change"]["receipt"]["said"], format!("Done {TITLE}"));    assert!(done["change"]["after"].get("@missed").is_none(), "a lone todo has no run");    assert_eq!(status(&h, &other), "open");}/// RULE: Ticking the earliest late occurrence clears nothing: the later ones stay open, and the receipt says only Done.#[test]fn ticking_the_earliest_late_one_leaves_the_later_ones_open() {    let h = zoned();    let [first, second, third, next] = late_series(&h);    let done = tick(&h, &first);    assert_eq!(done["change"]["receipt"]["said"], format!("Done {TITLE}"));    for later in [&second, &third, &next] {        assert_eq!(status(&h, later), "open");    }}