tests/calendar-assistant.test.ts
import { describe, it, expect, vi } from "vitest";
// @ts-ignore -- plain-JS agent module without type declarations
import agentModule, { handleMcpMessage, makeContext } from "../src/index.js";
const agent: any = agentModule;
const fixedNow = () => new Date("2026-01-15T12:00:00Z");
const cal = (...lines: string[]) => ["BEGIN:VCALENDAR", "VERSION:2.0", "PRODID:-//Test//Fixture//EN", ...lines, "END:VCALENDAR"].join("\r\n");
const WEEKLY_DST = cal(
"BEGIN:VEVENT",
"UID:standup-1",
"SUMMARY:Standup",
"DTSTART;TZID=America/New_York:20260302T090000",
"DTEND;TZID=America/New_York:20260302T093000",
"RRULE:FREQ=WEEKLY;BYDAY=MO,WE;COUNT=6",
"EXDATE;TZID=America/New_York:20260304T090000",
"END:VEVENT",
);
describe("calendar-assistant: parse_ics", () => {
it("parses UTC, TZID and all-day events (happy path)", async () => {
const ics = cal(
"BEGIN:VEVENT",
"UID:utc-1",
"SUMMARY:Call\\, with comma",
"DTSTART:20260310T150000Z",
"DTEND:20260310T160000Z",
"LOCATION:Room 1",
"END:VEVENT",
"BEGIN:VEVENT",
"UID:tz-1",
"SUMMARY:Berlin sync",
"DTSTART;TZID=Europe/Berlin:20260310T090000",
"DTEND;TZID=Europe/Berlin:20260310T100000",
"END:VEVENT",
"BEGIN:VEVENT",
"UID:allday-1",
"SUMMARY:Offsite",
"DTSTART;VALUE=DATE:20260312",
"DTEND;VALUE=DATE:20260314",
"END:VEVENT",
);
const out = await agent.call("parse_ics", { ics }, { now: fixedNow });
expect(out.vevent_count).toBe(3);
const byUid = Object.fromEntries(out.events.map((e: any) => [e.uid, e]));
expect(byUid["utc-1"]).toMatchObject({ start: "2026-03-10T15:00:00Z", end: "2026-03-10T16:00:00Z", summary: "Call, with comma", location: "Room 1", all_day: false });
expect(byUid["tz-1"]).toMatchObject({ start: "2026-03-10T08:00:00Z", end: "2026-03-10T09:00:00Z", timezone: "Europe/Berlin" });
expect(byUid["allday-1"]).toMatchObject({ start: "2026-03-12", end: "2026-03-14", all_day: true });
});
it("expands a weekly RRULE with COUNT and EXDATE, keeping local time across the US DST change", async () => {
const out = await agent.call("parse_ics", { ics: WEEKLY_DST, expand_from: "2026-03-01T00:00:00Z", expand_until: "2026-04-01T00:00:00Z" });
const starts = out.events.map((e: any) => e.start);
// COUNT=6 counts Mar 2, 4 (excluded), 9, 11, 16, 18. 09:00 EST = 14:00Z before Mar 8; 09:00 EDT = 13:00Z after.
expect(starts).toEqual(["2026-03-02T14:00:00Z", "2026-03-09T13:00:00Z", "2026-03-11T13:00:00Z", "2026-03-16T13:00:00Z", "2026-03-18T13:00:00Z"]);
expect(out.events.every((e: any) => e.recurring === true && e.end > e.start)).toBe(true);
});
it("supports DAILY with INTERVAL and UNTIL, and BYDAY-limited DAILY", async () => {
const ics = cal(
"BEGIN:VEVENT",
"UID:daily-1",
"SUMMARY:Every other day",
"DTSTART:20260501T080000Z",
"DURATION:PT15M",
"RRULE:FREQ=DAILY;INTERVAL=2;UNTIL=20260507T080000Z",
"END:VEVENT",
"BEGIN:VEVENT",
"UID:weekdays",
"SUMMARY:Weekdays",
"DTSTART:20260501T120000Z",
"DTEND:20260501T123000Z",
"RRULE:FREQ=DAILY;BYDAY=MO,TU,WE,TH,FR;COUNT=3",
"END:VEVENT",
);
const out = await agent.call("parse_ics", { ics, expand_from: "2026-04-01T00:00:00Z", expand_until: "2026-06-01T00:00:00Z" });
const daily = out.events.filter((e: any) => e.uid === "daily-1").map((e: any) => e.start);
expect(daily).toEqual(["2026-05-01T08:00:00Z", "2026-05-03T08:00:00Z", "2026-05-05T08:00:00Z", "2026-05-07T08:00:00Z"]);
// 2026-05-01 is a Friday; the next weekdays are Mon 4 and Tue 5.
const wk = out.events.filter((e: any) => e.uid === "weekdays").map((e: any) => e.start.slice(0, 10));
expect(wk).toEqual(["2026-05-01", "2026-05-04", "2026-05-05"]);
});
it("supports MONTHLY BYDAY ordinals and all-day weekly recurrences", async () => {
const ics = cal(
"BEGIN:VEVENT",
"UID:second-tuesday",
"SUMMARY:Board",
"DTSTART;TZID=America/Chicago:20260113T100000",
"DTEND;TZID=America/Chicago:20260113T110000",
"RRULE:FREQ=MONTHLY;BYDAY=2TU;COUNT=3",
"END:VEVENT",
"BEGIN:VEVENT",
"UID:allday-weekly",
"SUMMARY:Gym",
"DTSTART;VALUE=DATE:20260105",
"RRULE:FREQ=WEEKLY;UNTIL=20260119",
"END:VEVENT",
);
const out = await agent.call("parse_ics", { ics, expand_from: "2026-01-01T00:00:00Z", expand_until: "2026-06-01T00:00:00Z" });
expect(out.events.filter((e: any) => e.uid === "second-tuesday").map((e: any) => e.start)).toEqual([
"2026-01-13T16:00:00Z",
"2026-02-10T16:00:00Z",
"2026-03-10T15:00:00Z", // CDT after Mar 8
]);
expect(out.events.filter((e: any) => e.uid === "allday-weekly").map((e: any) => e.start)).toEqual(["2026-01-05", "2026-01-12", "2026-01-19"]);
});
it("applies RECURRENCE-ID overrides and ignores cancelled occurrences in free/busy", async () => {
const ics = cal(
"BEGIN:VEVENT",
"UID:series-9",
"SUMMARY:1:1",
"DTSTART:20260601T140000Z",
"DTEND:20260601T143000Z",
"RRULE:FREQ=DAILY;COUNT=3",
"END:VEVENT",
"BEGIN:VEVENT",
"UID:series-9",
"RECURRENCE-ID:20260602T140000Z",
"SUMMARY:1:1 (moved)",
"DTSTART:20260602T170000Z",
"DTEND:20260602T173000Z",
"END:VEVENT",
);
const out = await agent.call("parse_ics", { ics, expand_from: "2026-05-01T00:00:00Z", expand_until: "2026-07-01T00:00:00Z" });
expect(out.events.map((e: any) => [e.start, e.summary])).toEqual([
["2026-06-01T14:00:00Z", "1:1"],
["2026-06-02T17:00:00Z", "1:1 (moved)"],
["2026-06-03T14:00:00Z", "1:1"],
]);
expect(out.events[1].recurrence_id).toBe("2026-06-02T14:00:00Z");
});
it("resolves DST gaps and ambiguous local times per RFC 5545", async () => {
const ics = cal(
"BEGIN:VEVENT",
"UID:gap",
"DTSTART;TZID=America/New_York:20260308T023000",
"DURATION:PT30M",
"END:VEVENT",
"BEGIN:VEVENT",
"UID:ambiguous",
"DTSTART;TZID=America/New_York:20261101T013000",
"DURATION:PT30M",
"END:VEVENT",
);
const out = await agent.call("parse_ics", { ics });
const byUid = Object.fromEntries(out.events.map((e: any) => [e.uid, e]));
expect(byUid.gap.start).toBe("2026-03-08T07:30:00Z"); // 02:30 does not exist; becomes 03:30 EDT
expect(byUid.ambiguous.start).toBe("2026-11-01T05:30:00Z"); // first 01:30 (EDT)
});
it("handles floating times, Windows TZIDs, folded lines and unknown TZIDs with warnings", async () => {
const ics = cal(
"BEGIN:VEVENT",
"UID:floating",
"SUMMARY:Lunch with a very long title that is folded",
" across two lines",
"DTSTART:20260415T120000",
"DTEND:20260415T130000",
"END:VEVENT",
"BEGIN:VEVENT",
"UID:windows",
"DTSTART;TZID=Pacific Standard Time:20260415T090000",
"DTEND;TZID=Pacific Standard Time:20260415T100000",
"END:VEVENT",
"BEGIN:VEVENT",
"UID:unknown",
"DTSTART;TZID=Custom/Nowhere:20260415T090000",
"DTEND;TZID=Custom/Nowhere:20260415T100000",
"END:VEVENT",
);
const out = await agent.call("parse_ics", { ics, default_timezone: "Asia/Tokyo" });
const byUid = Object.fromEntries(out.events.map((e: any) => [e.uid, e]));
expect(byUid.floating).toMatchObject({ start: "2026-04-15T03:00:00Z", floating: true, summary: "Lunch with a very long title that is folded across two lines" });
expect(byUid.windows.start).toBe("2026-04-15T16:00:00Z");
expect(byUid.unknown.start).toBe("2026-04-15T00:00:00Z");
expect(out.warnings.some((w: string) => w.includes("Custom/Nowhere"))).toBe(true);
});
it("warns instead of guessing on unsupported recurrence parts", async () => {
const ics = cal("BEGIN:VEVENT", "UID:setpos", "DTSTART:20260105T100000Z", "DTEND:20260105T110000Z", "RRULE:FREQ=MONTHLY;BYDAY=MO,TU;BYSETPOS=-1", "END:VEVENT");
const out = await agent.call("parse_ics", { ics, expand_from: "2026-01-01T00:00:00Z", expand_until: "2026-12-31T00:00:00Z" });
expect(out.events).toHaveLength(1);
expect(out.warnings.join(" ")).toContain("BYSETPOS");
});
it("rejects invalid input", async () => {
await expect(agent.call("parse_ics", {})).rejects.toMatchObject({ code: "invalid_input" });
await expect(agent.call("parse_ics", { ics: "BEGIN:VCALENDAR", extra: 1 })).rejects.toMatchObject({ code: "invalid_input" });
await expect(agent.call("parse_ics", { ics: "x", default_timezone: "Mars/Olympus" })).rejects.toMatchObject({ code: "invalid_input" });
});
});
describe("calendar-assistant: find_free_slots", () => {
const wh = { timezone: "America/New_York", start: "09:00", end: "17:00" };
it("returns DST-correct working windows minus busy time", async () => {
const out = await agent.call(
"find_free_slots",
{ calendars: [{ name: "work", ics: WEEKLY_DST }], working_hours: wh, duration_minutes: 60, date_from: "2026-03-06", date_to: "2026-03-09" },
{ now: fixedNow },
);
expect(out.timezone).toBe("America/New_York");
// Fri Mar 6 (EST, UTC-5) is fully free; weekend skipped; Mon Mar 9 (EDT, UTC-4) after the 09:00-09:30 standup.
expect(out.slots).toEqual([
{ start: "2026-03-06T14:00:00Z", end: "2026-03-06T22:00:00Z", local_start: "2026-03-06T09:00-05:00", local_end: "2026-03-06T17:00-05:00" },
{ start: "2026-03-09T13:30:00Z", end: "2026-03-09T21:00:00Z", local_start: "2026-03-09T09:30-04:00", local_end: "2026-03-09T17:00-04:00" },
]);
});
it("is DST-correct for Europe/London at the October change and merges several calendars and busy blocks", async () => {
const a = cal("BEGIN:VEVENT", "UID:a", "DTSTART:20261023T090000Z", "DTEND:20261023T100000Z", "END:VEVENT");
const out = await agent.call(
"find_free_slots",
{
calendars: [{ ics: a }, { events: [{ start: "2026-10-26T12:00:00Z", end: "2026-10-26T13:00:00Z" }] }],
busy: [{ start: "2026-10-23T14:00:00Z", end: "2026-10-23T16:30:00Z" }],
working_hours: { timezone: "Europe/London", start: "09:00", end: "17:00" },
duration_minutes: 30,
date_from: "2026-10-23",
date_to: "2026-10-26",
},
{ now: fixedNow },
);
// Fri Oct 23 is BST (UTC+1): 08:00Z-16:00Z. Mon Oct 26 is GMT: 09:00Z-17:00Z.
expect(out.slots.map((s: any) => [s.start, s.end])).toEqual([
["2026-10-23T08:00:00Z", "2026-10-23T09:00:00Z"],
["2026-10-23T10:00:00Z", "2026-10-23T14:00:00Z"],
["2026-10-26T09:00:00Z", "2026-10-26T12:00:00Z"],
["2026-10-26T13:00:00Z", "2026-10-26T17:00:00Z"],
]);
expect(out.busy_intervals_considered).toBe(3);
});
it("supports granularity, buffers, all-day blocking and excludes past time using the injected clock", async () => {
const ics = cal(
"BEGIN:VEVENT",
"UID:holiday",
"DTSTART;VALUE=DATE:20260317",
"DTEND;VALUE=DATE:20260318",
"END:VEVENT",
"BEGIN:VEVENT",
"UID:free-time",
"TRANSP:TRANSPARENT",
"DTSTART:20260316T150000Z",
"DTEND:20260316T200000Z",
"END:VEVENT",
"BEGIN:VEVENT",
"UID:cancelled",
"STATUS:CANCELLED",
"DTSTART:20260316T170000Z",
"DTEND:20260316T180000Z",
"END:VEVENT",
"BEGIN:VEVENT",
"UID:busy",
"DTSTART:20260316T150000Z",
"DTEND:20260316T153000Z",
"END:VEVENT",
);
const out = await agent.call(
"find_free_slots",
{ calendars: [{ ics }], working_hours: { ...wh, start: "10:00", end: "13:00" }, duration_minutes: 60, granularity_minutes: 30, buffer_minutes: 15, date_from: "2026-03-16", date_to: "2026-03-17" },
{ now: () => new Date("2026-03-16T14:10:00Z") }, // 10:10 EDT on Mar 16
);
// Busy 11:00-11:30 local, padded 10:45-11:45. Before: 10:15-10:45 (too short). After: 11:45-13:00 -> 12:00 start only.
// Mar 17 blocked by the all-day holiday. Transparent and cancelled events do not block.
expect(out.slots.map((s: any) => [s.local_start, s.local_end])).toEqual([["2026-03-16T12:00-04:00", "2026-03-16T13:00-04:00"]]);
const allowAllDay = await agent.call(
"find_free_slots",
{ calendars: [{ ics }], working_hours: { ...wh, start: "10:00", end: "12:00" }, duration_minutes: 120, date_from: "2026-03-17", date_to: "2026-03-17", all_day_events_block: false },
{ now: fixedNow },
);
expect(allowAllDay.slots).toHaveLength(1);
});
it("rejects invalid input and unknown time zones", async () => {
await expect(agent.call("find_free_slots", { working_hours: wh, date_from: "2026-03-01", date_to: "2026-03-02" })).rejects.toMatchObject({ code: "invalid_input" });
await expect(
agent.call("find_free_slots", { working_hours: { ...wh, timezone: "Not/AZone" }, duration_minutes: 30, date_from: "2026-03-01", date_to: "2026-03-02" }),
).rejects.toMatchObject({ code: "invalid_input" });
await expect(
agent.call("find_free_slots", { working_hours: { ...wh, start: "9am" }, duration_minutes: 30, date_from: "2026-03-01", date_to: "2026-03-02" }),
).rejects.toMatchObject({ code: "invalid_input" });
await expect(agent.call("find_free_slots", { working_hours: wh, duration_minutes: 30, date_from: "2026-03-05", date_to: "2026-03-01" })).rejects.toMatchObject({
code: "invalid_input",
});
});
});
describe("calendar-assistant: propose_meeting", () => {
it("drafts a valid, escaped, folded VEVENT that round-trips through parse_ics", async () => {
const out = await agent.call(
"propose_meeting",
{
title: "Plan; review, and \\ decide",
start: "2026-03-09T10:00",
timezone: "America/New_York",
duration_minutes: 45,
description: "Line one\nLine two " + "x".repeat(120),
attendees: ["ana@example.com"],
organizer: "me@example.org",
},
{ now: fixedNow },
);
expect(out.ics).toContain("DTSTART:20260309T140000Z");
expect(out.ics).toContain("DTEND:20260309T144500Z");
expect(out.ics).toContain("SUMMARY:Plan\\; review\\, and \\\\ decide");
expect(out.ics).toContain("DTSTAMP:20260115T120000Z");
for (const line of out.ics.split("\r\n")) expect(Buffer.byteLength(line, "utf8")).toBeLessThanOrEqual(75);
const parsed = await agent.call("parse_ics", { ics: out.ics });
expect(parsed.events[0]).toMatchObject({ uid: out.event.uid, summary: "Plan; review, and \\ decide", start: "2026-03-09T14:00:00Z", end: "2026-03-09T14:45:00Z" });
const again = await agent.call("propose_meeting", { title: "Plan; review, and \\ decide", start: "2026-03-09T14:00:00Z", duration_minutes: 45, attendees: ["ana@example.com"], organizer: "me@example.org" }, { now: fixedNow });
expect(again.event.uid).toBe(out.event.uid); // deterministic UID from the same details
});
it("rejects bad emails, missing end/duration and local times without a zone", async () => {
await expect(agent.call("propose_meeting", { title: "x", start: "2026-03-09T14:00:00Z", duration_minutes: 30, attendees: ["not-an-email\r\nATTENDEE:x"] })).rejects.toMatchObject({
code: "invalid_input",
});
await expect(agent.call("propose_meeting", { title: "x", start: "2026-03-09T14:00:00Z" })).rejects.toMatchObject({ code: "invalid_input" });
await expect(agent.call("propose_meeting", { title: "x", start: "2026-03-09T14:00", duration_minutes: 30 })).rejects.toMatchObject({ code: "invalid_input" });
});
});
describe("calendar-assistant: create_google_event (external_write)", () => {
const event = { summary: "Planning", start: "2026-03-09T14:00:00Z", end: "2026-03-09T14:45:00Z", time_zone: "America/New_York", attendees: ["ana@example.com"] };
const env = { GOOGLE_CALENDAR_ACCESS_TOKEN: "test-token-not-real" };
it("requires confirmation and never touches the network without it", async () => {
const fetch = vi.fn();
await expect(agent.call("create_google_event", event, { fetch, env })).rejects.toMatchObject({ code: "confirmation_required" });
await expect(agent.call("create_google_event", { ...event, confirm: false }, { fetch, env })).rejects.toMatchObject({ code: "confirmation_required" });
expect(fetch).not.toHaveBeenCalled();
});
it("requires the credential", async () => {
const fetch = vi.fn();
await expect(agent.call("create_google_event", { ...event, confirm: true }, { fetch, env: { GOOGLE_CALENDAR_ACCESS_TOKEN: "" } })).rejects.toMatchObject({
code: "missing_credential",
});
expect(fetch).not.toHaveBeenCalled();
});
it("posts to the Google Calendar API with an injected fake fetch", async () => {
const calls: any[] = [];
const fetch = async (url: string, init: any) => {
calls.push({ url, init });
return new Response(JSON.stringify({ id: "abc123", htmlLink: "https://www.google.com/calendar/event?eid=abc", status: "confirmed" }), { status: 200 });
};
const out = await agent.call("create_google_event", { ...event, confirm: true }, { fetch, env });
expect(out).toMatchObject({ created: true, already_exists: false, id: "abc123", status: "confirmed", calendar_id: "primary" });
expect(calls).toHaveLength(1);
const u = new URL(calls[0].url);
expect(u.hostname).toBe("www.googleapis.com");
expect(u.pathname).toBe("/calendar/v3/calendars/primary/events");
expect(u.searchParams.get("sendUpdates")).toBe("none");
expect(calls[0].init.method).toBe("POST");
expect(calls[0].init.headers.authorization).toBe("Bearer test-token-not-real");
const body = JSON.parse(calls[0].init.body);
expect(body).toMatchObject({ summary: "Planning", start: { dateTime: "2026-03-09T14:00:00Z", timeZone: "America/New_York" }, attendees: [{ email: "ana@example.com" }] });
expect(body.id).toMatch(/^[a-v0-9]{5,1024}$/);
expect(JSON.stringify(out)).not.toContain("test-token-not-real");
});
it("treats 409 as an existing event (idempotent retry) and maps auth errors", async () => {
const conflict = async () => new Response(JSON.stringify({ error: { message: "duplicate" } }), { status: 409 });
const out = await agent.call("create_google_event", { ...event, confirm: true }, { fetch: conflict, env });
expect(out).toMatchObject({ created: false, already_exists: true });
const denied = async () => new Response("{}", { status: 401 });
await expect(agent.call("create_google_event", { ...event, confirm: true }, { fetch: denied, env })).rejects.toMatchObject({ code: "google_auth_error" });
});
});
describe("calendar-assistant: permissions", () => {
it("declares least-privilege permissions", () => {
expect(agent.manifest.id).toBe("agentrove.calendar-assistant");
expect(agent.manifest.permissions).toMatchObject({ filesystem: "none", network: ["www.googleapis.com"], credentials: ["GOOGLE_CALENDAR_ACCESS_TOKEN"] });
const writes = agent.manifest.tools.filter((t: any) => t.side_effects !== "none").map((t: any) => t.name);
expect(writes).toEqual(["create_google_event"]);
});
it("denies hosts outside the network allowlist (and plain http)", async () => {
const fetch = vi.fn();
const ctx = makeContext(agent.manifest, { fetch });
await expect(ctx.fetch("https://evil.example.com/steal")).rejects.toMatchObject({ code: "network_denied" });
await expect(ctx.fetch("http://www.googleapis.com/calendar/v3")).rejects.toMatchObject({ code: "network_denied" });
expect(fetch).not.toHaveBeenCalled();
});
});
describe("calendar-assistant: MCP", () => {
it("lists tools and calls one over JSON-RPC", async () => {
const list = await handleMcpMessage(agent, { jsonrpc: "2.0", id: 1, method: "tools/list" });
expect(list.result.tools.map((t: any) => t.name)).toEqual(["parse_ics", "find_free_slots", "propose_meeting", "create_google_event"]);
expect(list.result.tools[0].inputSchema.required).toEqual(["ics"]);
const call = await handleMcpMessage(agent, {
jsonrpc: "2.0",
id: 2,
method: "tools/call",
params: { name: "parse_ics", arguments: { ics: cal("BEGIN:VEVENT", "UID:m", "DTSTART:20260310T150000Z", "DTEND:20260310T160000Z", "END:VEVENT") } },
});
expect(call.result.isError).toBe(false);
expect(call.result.structuredContent.events[0].uid).toBe("m");
const blocked = await handleMcpMessage(agent, {
jsonrpc: "2.0",
id: 3,
method: "tools/call",
params: { name: "create_google_event", arguments: { summary: "x", start: "2026-03-09T14:00:00Z", end: "2026-03-09T15:00:00Z" } },
});
expect(blocked.result.isError).toBe(true);
expect(blocked.result.content[0].text).toContain("confirmation_required");
});
});