//! `today_agenda` over `dispatch::call_tool`. //! //! Every case that exercises the tool is here; a case for another group's tool //! belongs in that group's file. //! //! `list_meetings`, `get_meeting` and `get_transcript` have NO test in the //! TypeScript suite — nothing under `packages/server/__tests__/` names any of //! the three. The four cases at the bottom of this file are therefore NOT //! ports; they are written here, and marked as such, because a handler nothing //! ever calls is indistinguishable from the stub it replaced. They assert only //! what `records.ts` plainly says. //! //! **The clock is the harness's, and it is FIXED.** The TypeScript reads the //! wall clock in most of these and fakes it in one; here `h.clock.set(…)` is //! the only clock, which is stronger — an agenda case cannot pass by accident //! on the day it is run. mod support; use std::sync::atomic::{AtomicU64, Ordering}; use chrono::{DateTime, Utc}; use cotton_store::schema::ddl::SCHEMA_VERSION; use rusqlite::types::Value as SqlValue; use serde_json::{json, Value}; use support::harness::Harness; use support::mcp::{call_tool, call_tool_raw, mint_token}; /// When every seeded row says it was written. const AT: &str = "2026-08-22T10:00:00.000Z"; const KOLKATA: &str = "Asia/Kolkata"; /// Where the calendar fixture's database lives — never the host's zone. const ZONE: &str = "Europe/London"; /// 4 Sep 2026 in Kolkata: midnight and 09:00, as instants. const SEP4_MIDNIGHT: &str = "2026-09-03T18:30:00.000Z"; const SEP4_0900: &str = "2026-09-04T03:30:00.000Z"; const VICKY_GCAL: &str = "7pfkedoofe98utr5qbffl34oab"; const BELLA_GCAL: &str = "m4rbfl24nhk6kqpdu284dcsmu0"; // ── the fixture ───────────────────────────────────────────────────────────── /// An op the way a device would mint one. The ids are DERIVED from a /// per-process counter, never random. fn op(table: &str, row_id: &str, patch: Value) -> Value { static NEXT: AtomicU64 = AtomicU64::new(0); let n = NEXT.fetch_add(1, Ordering::Relaxed); json!({ "opId": format!("01J{:023}", n), "deviceId": "device:seed", "kind": "upsert", "table": table, "rowId": row_id, "patch": patch, "hlc": format!("1700000000{:03}:0000:device:seed", n), "schemaVersion": SCHEMA_VERSION, }) } /// Fold ops into the file the way a device's push folds them. fn seed(h: &Harness, ops: Vec) { h.seed(&ops); } /// `cotton_news_settings.timezone` is the one workspace-scoped zone in the /// file, written straight in: nothing on the MCP surface sets it. fn seed_zone(h: &Harness, timezone: &str) { h.with(|db| { db.run( "INSERT INTO cotton_news_settings (workspace_id, timezone, created_at, updated_at)\ \n VALUES (?, ?, ?, ?)", &[ SqlValue::Text(h.workspace_id.clone()), SqlValue::Text(timezone.to_string()), SqlValue::Text(AT.to_string()), SqlValue::Text(AT.to_string()), ], ) }) .expect("the zone lands"); } /// A uuid-shaped id a fixture can name. DERIVED, never random. fn fixture_id(label: &str) -> String { support::harness::synthetic_uuid(label) } /// The harness clock, moved to a named instant. /// /// Everything is seeded first, at the default start, so the rows a case reads /// carry the stamps it expects and only the READING sees the moved clock. fn at(h: &Harness, instant: &str) { h.clock.set( DateTime::parse_from_rfc3339(instant) .expect("a literal instant") .with_timezone(&Utc), ); } // ── mcp.test.ts, the `read tools` block ───────────────────────────────────── // // `search_records` and `get_record` share that block and belong to the search // half of this group; only its `today_agenda` cases are here. struct Seeded { h: Harness, secret: String, } /// `seededHarness` — one todo and one note, as `mcp.test.ts` builds it. fn seeded_harness() -> Seeded { let mut h = Harness::new(); let secret = mint_token(&mut h, &["read", "write"], "Claude"); seed( &h, vec![ op( "cotton_todos", &fixture_id("todo-cardamom"), json!({ "title": "buy cardamom", "notes": "the green ones", "status": "open", "priority": 0, "workspace_id": h.workspace_id, "created_at": AT, "updated_at": AT, }), ), op( "cotton_notes", &fixture_id("note-kitchen"), json!({ "title": "kitchen rebuild", "body": "cardamom shelf goes above the hob", "kind": "note", "workspace_id": h.workspace_id, "created_at": AT, "updated_at": AT, }), ), ], ); Seeded { h, secret } } #[test] fn returns_todays_agenda() { let s = seeded_harness(); let answer = call_tool_raw( &s.h, &s.secret, "today_agenda", json!({ "timezone": KOLKATA }), ); assert!(!answer.is_error, "{}", answer.text); assert!(answer.json().get("todos").is_some()); } #[test] fn falls_back_to_the_workspaces_own_timezone_when_the_caller_names_none() { // The assistant rarely knows where the user is. Defaulting to UTC put // "today" five and a half hours behind the owner's day: between 00:00 and // 05:30 IST, `today_agenda` answered with YESTERDAY. let s = seeded_harness(); seed_zone(&s.h, KOLKATA); let answer = call_tool_raw(&s.h, &s.secret, "today_agenda", json!({})); assert!(!answer.is_error, "{}", answer.text); assert_eq!(answer.json()["timezone"], KOLKATA); } #[test] fn still_honours_an_explicit_timezone_over_the_workspaces() { let s = seeded_harness(); seed_zone(&s.h, KOLKATA); let result = call_tool( &s.h, &s.secret, "today_agenda", json!({ "timezone": "Europe/London" }), ); assert_eq!(result["timezone"], "Europe/London"); } #[test] fn uses_utc_when_the_workspace_has_no_timezone_recorded() { let s = seeded_harness(); let answer = call_tool_raw(&s.h, &s.secret, "today_agenda", json!({})); assert!(!answer.is_error, "{}", answer.text); assert_eq!(answer.json()["timezone"], "UTC"); } #[test] fn dates_the_agenda_by_the_workspace_zone_not_utc_just_after_ist_midnight() { // The instant is FIXED. Comparing the result against the wall clock would // agree with the code under test 18.5 hours a day no matter which zone it // used — it can only fail inside the 5.5 h window where the two calendars // disagree, which is to say it would pass a UTC regression on most runs. // // 19:00Z on the 23rd is 00:30 IST on the 24th: a UTC agenda says the 23rd, // an IST one says the 24th, and the assertion below names both. let s = seeded_harness(); seed_zone(&s.h, KOLKATA); at(&s.h, "2026-08-23T19:00:00Z"); // No `timezone` argument: the zone has to come from the workspace. Passing // it explicitly would test the caller. let result = call_tool(&s.h, &s.secret, "today_agenda", json!({})); assert_eq!(result["timezone"], KOLKATA); assert_eq!(result["date"], "2026-08-24"); assert_ne!(result["date"], "2026-08-23"); } // ── mcpTodosDue.test.ts — the day/clock pair beside each todo ─────────────── struct SeededTodos { h: Harness, secret: String, timed: String, date_only: String, } fn seeded_todos() -> SeededTodos { let mut h = Harness::new(); let secret = mint_token(&mut h, &["read", "write"], "Claude"); seed_zone(&h, KOLKATA); let timed = fixture_id("agenda-todo-timed"); let date_only = fixture_id("agenda-todo-date-only"); let undated = fixture_id("agenda-todo-undated"); let timed_too = fixture_id("agenda-todo-timed-too"); let base = |title: &str, due: Option<&str>| -> Value { json!({ "title": title, "status": "open", "priority": 0, "workspace_id": h.workspace_id, "created_at": AT, "updated_at": AT, "due_at": due, }) }; seed( &h, vec![ op( "cotton_todos", &timed, base("call the bank", Some(SEP4_0900)), ), op( "cotton_todos", &date_only, base("water the plants", Some(SEP4_MIDNIGHT)), ), op("cotton_todos", &undated, base("read the sqlite book", None)), // 10 Sep, 18:00 Kolkata — after today's window, so off the agenda. op( "cotton_todos", &timed_too, base("file the return", Some("2026-09-10T12:30:00.000Z")), ), ], ); SeededTodos { h, secret, timed, date_only, } } #[test] fn today_agenda_carries_the_pair_beside_each_todo() { let s = seeded_todos(); let agenda = call_tool(&s.h, &s.secret, "today_agenda", json!({})); let todos = agenda["todos"].as_array().expect("todos").clone(); let of = |id: &str| -> Value { todos .iter() .find(|todo| todo["id"] == id) .cloned() .unwrap_or(Value::Null) }; // Both are overdue by the harness clock, so both are on the agenda. let timed = of(&s.timed); assert_eq!(timed["dueDate"], "2026-09-04"); assert_eq!(timed["dueTime"], "09:00"); let date_only = of(&s.date_only); assert_eq!(date_only["dueDate"], "2026-09-04"); assert_eq!(date_only["dueTime"], Value::Null); } // ── mcpEvents.test.ts — the calendar, expanded ────────────────────────────── // // The owner's own calendar, exactly as `mcpEvents.test.ts` has it: a weekly // "Vicky" whose 4 Sep and 11 Sep occurrences were moved, and a weekly "Call // with Bella" with 23 Sep cancelled. struct Calendar { h: Harness, secret: String, vicky: String, vicky_moved_to_the_8th: String, } /// The calendar above, with the harness clock standing at `now`. fn calendar_at(now: &str) -> Calendar { let mut h = Harness::new(); let secret = mint_token(&mut h, &["read", "write"], "Claude"); let calendar = fixture_id("agenda-calendar"); let vicky = fixture_id("agenda-vicky"); let vicky_moved_to_the_8th = fixture_id("agenda-vicky-8th"); let vicky_moved_to_the_12th = fixture_id("agenda-vicky-12th"); let bella = fixture_id("agenda-bella"); let dinner = fixture_id("agenda-dinner"); seed( &h, vec![ op( "cotton_collections", &calendar, json!({ "kind": "calendar", "name": "Personal", "is_default": true, "workspace_id": h.workspace_id, "created_at": AT, }), ), op( "cotton_news_settings", &h.workspace_id, json!({ "workspace_id": h.workspace_id, "timezone": ZONE, "created_at": AT, "updated_at": AT, }), ), op( "cotton_events", &vicky, json!({ "collection_id": calendar, "title": "Vicky", "starts_at": "2026-09-04T06:30:00.000Z", "ends_at": "2026-09-04T07:30:00.000Z", "all_day": false, "rrule": "FREQ=WEEKLY", "gcal_id": VICKY_GCAL, "sync_state": "synced", "created_at": AT, "updated_at": AT, }), ), op( "cotton_events", &vicky_moved_to_the_8th, json!({ "collection_id": calendar, "title": "Vicky", "starts_at": "2026-09-08T06:30:00.000Z", "ends_at": "2026-09-08T07:30:00.000Z", "all_day": false, "series_id": vicky, "original_starts_at": "2026-09-04T06:30:00.000Z", "gcal_id": format!("{VICKY_GCAL}_20260904T063000Z"), "sync_state": "synced", "created_at": AT, "updated_at": AT, }), ), op( "cotton_events", &vicky_moved_to_the_12th, json!({ "collection_id": calendar, "title": "Vicky", "starts_at": "2026-09-12T06:30:00.000Z", "ends_at": "2026-09-12T07:30:00.000Z", "all_day": false, "series_id": vicky, "original_starts_at": "2026-09-11T06:30:00.000Z", "gcal_id": format!("{VICKY_GCAL}_20260911T063000Z"), "sync_state": "synced", "created_at": AT, "updated_at": AT, }), ), op( "cotton_events", &bella, json!({ "collection_id": calendar, "title": "Call with Bella", "starts_at": "2026-09-16T09:00:00.000Z", "ends_at": "2026-09-16T09:30:00.000Z", "all_day": false, "rrule": "FREQ=WEEKLY", "exdates": ["2026-09-23T09:00:00.000Z"], "gcal_id": BELLA_GCAL, "sync_state": "synced", "created_at": AT, "updated_at": AT, }), ), op( "cotton_events", &dinner, json!({ "collection_id": calendar, "title": "dinner with amma", "starts_at": "2026-09-05T14:00:00.000Z", "ends_at": "2026-09-05T16:00:00.000Z", "all_day": false, "created_at": AT, "updated_at": AT, }), ), ], ); at(&h, now); Calendar { h, secret, vicky, vicky_moved_to_the_8th, } } fn event_ids(agenda: &Value) -> Vec { agenda["events"] .as_array() .expect("events") .iter() .map(|event| event["id"].as_str().unwrap_or_default().to_string()) .collect() } #[test] fn draws_a_weekly_event_on_a_day_that_is_not_the_day_the_series_starts() { // A Friday, a fortnight after Vicky's first occurrence. let cal = calendar_at("2026-09-18T09:00:00Z"); let agenda = call_tool(&cal.h, &cal.secret, "today_agenda", json!({})); assert_eq!(agenda["date"], "2026-09-18"); assert_eq!(agenda["timezone"], ZONE); let events = agenda["events"].as_array().expect("events"); assert_eq!(events.len(), 1, "{agenda}"); assert_eq!(events[0]["id"], cal.vicky); assert_eq!(events[0]["title"], "Vicky"); assert_eq!(events[0]["startsAt"], "2026-09-18T06:30:00.000Z"); assert_eq!(events[0]["seriesId"], cal.vicky); assert_eq!(events[0]["occurrenceAt"], "2026-09-18T06:30:00.000Z"); } #[test] fn shows_an_occurrence_that_was_moved_into_today_and_not_the_day_it_left() { let moved = calendar_at("2026-09-08T09:00:00Z"); let on_the_eighth = call_tool(&moved.h, &moved.secret, "today_agenda", json!({})); assert_eq!( event_ids(&on_the_eighth), vec![moved.vicky_moved_to_the_8th.clone()] ); // The 4th is the day it left: the series draws nothing there any more. let left = calendar_at("2026-09-04T09:00:00Z"); let on_the_fourth = call_tool(&left.h, &left.secret, "today_agenda", json!({})); assert_eq!(on_the_fourth["events"], json!([])); } #[test] fn does_not_draw_a_cancelled_occurrence() { let cal = calendar_at("2026-09-23T08:00:00Z"); let agenda = call_tool(&cal.h, &cal.secret, "today_agenda", json!({})); assert_eq!(agenda["events"], json!([])); // …while the week before is there. let before = calendar_at("2026-09-16T08:00:00Z"); let earlier = call_tool(&before.h, &before.secret, "today_agenda", json!({})); assert_eq!(earlier["events"].as_array().map(Vec::len), Some(1)); }