Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
456 changes: 456 additions & 0 deletions apps/app/test/account-stories.test.ts

Large diffs are not rendered by default.

167 changes: 167 additions & 0 deletions apps/app/test/calendar-stories.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
import { describe, expect, it } from "vitest";
import * as Calendar from "../gen/app__calendar.ts";
import * as AgentCalendar from "../gen/app__calendar__agent.ts";
import * as Editor from "../gen/app__calendar__editor.ts";
import * as ReminderReopen from "../gen/app__calendar__reminderReopen.ts";
import * as Sharing from "../gen/app__calendar__sharing.ts";
import * as Writing from "../gen/app__calendar__writing.ts";

describe("Calendar story specifications", () => {
it('allows the agent to read events and free time without approval cards while rejecting non-agent reads (spec/app/calendar/agent.md, "Story: Let the agent read my calendar")', () => {
expect(AgentCalendar.canRead(AgentCalendar.initial, "Owner")).toBe(false);
expect(AgentCalendar.canRead(AgentCalendar.initial, "World")).toBe(false);
expect(AgentCalendar.canRead(AgentCalendar.initial, "Agent")).toBe(true);

const readEvents = AgentCalendar.read(AgentCalendar.initial, "Events", "Agent");
expect(readEvents.state.stage).toBe("Idle");
expect(readEvents.state.query).toBe("Events");
expect(readEvents.state.feed.status).toBe("Loading");
expect(readEvents.out).toEqual([{ tag: "Ask", ticket: readEvents.state.feed.ticket, query: "Events" }]);
const staleTicket = readEvents.state.feed.ticket;
expect(AgentCalendar.canRead(readEvents.state, "Agent")).toBe(true);

const readFreeTime = AgentCalendar.read(readEvents.state, "FreeTime", "Agent");
expect(readFreeTime.state.query).toBe("FreeTime");
expect(readFreeTime.out).toEqual([{ tag: "Ask", ticket: readFreeTime.state.feed.ticket, query: "FreeTime" }]);
expect(AgentCalendar.canFeedAnswered(readFreeTime.state, staleTicket, "World")).toBe(false);
expect(AgentCalendar.canFeedAnswered(readFreeTime.state, readFreeTime.state.feed.ticket, "World")).toBe(true);

const freeDone = AgentCalendar.feedAnswered(readFreeTime.state, readFreeTime.state.feed.ticket, true, "World");
expect(freeDone.feed.status).toBe("Loaded");
expect(freeDone.card.showing).toBe(false);
expect(AgentCalendar.inv(freeDone)).toBe(true);
});

it('keeps unreadable draft slots intact, avoids writing over them, and permits reset only in development builds (spec/app/calendar/editor.md, "Story: Recover a draft the phone cannot read")', () => {
expect(Editor.canSlotUnreadable(Editor.initial)).toBe(true);
const unreadable = Editor.slotUnreadable(Editor.initial);
expect(unreadable.unreadable).toBe(true);
expect(Editor.canResetSlot(unreadable, false)).toBe(false);
expect(Editor.canResetSlot(unreadable, true)).toBe(true);

const blankFormOverCorrupt = Editor.newEvent(unreadable, 2).state;
const edited = Editor.editTitle(blankFormOverCorrupt, false);
expect(edited.kept).toBe(false);
expect(Editor.inv(edited)).toBe(true);

const closed = Editor.close(edited);
const resetRequested = Editor.resetSlot(closed, true);
expect(resetRequested.out).toEqual([{ tag: "ResetSlot" }]);

const failedReset = Editor.slotReset(resetRequested.state, false);
expect(failedReset.unreadable).toBe(true);
const succeededReset = Editor.slotReset(failedReset, true);
expect(succeededReset.unreadable).toBe(false);
expect(Editor.inv(succeededReset)).toBe(true);
});

// spec/app/calendar/editor.md, "Story: Keep each account's draft with that account"
it("closes the open draft editor when another owner signs in and isolates owner slots", () => {
const ownerOne = Editor.ownerChanged(Editor.initial, 1);
const opened = Editor.newEvent(ownerOne, 3).state;
const titled = Editor.editTitle(opened, false);
expect(titled.owner).toBe(1);
expect(titled.form).toBe("NewEvent");

expect(Editor.canOwnerChanged(titled, 1)).toBe(false);
expect(Editor.canOwnerChanged(titled, 2)).toBe(true);
const switched = Editor.ownerChanged(titled, 2);
expect(switched.owner).toBe(2);
expect(switched.form).toBe("NoForm");
expect(switched.shown).toBe(false);
expect(switched.unreadable).toBe(false);
expect(Editor.inv(switched)).toBe(true);

const saved = Editor.save(titled).state;
expect(Editor.inFlight(saved)).toBe(true);
expect(Editor.canOwnerChanged(saved, 2)).toBe(false);
});

it('opens Calendar on a tapped day in Timeline layout with overlays and open items cleared (spec/app/calendar/index.md, "Story: Open Calendar on the day I tapped")', () => {
const onAgenda = Calendar.chooseLayout(Calendar.initial, "Agenda").state;
const withOverlay = Calendar.openMonth(onAgenda);
expect(withOverlay.layout).toBe("Agenda");
expect(withOverlay.overlay).toBe("MonthDialog");
expect(Calendar.canOpenOn(withOverlay, 4)).toBe(true);
expect(Calendar.canOpenOn(withOverlay, -1)).toBe(false);

const jumped = Calendar.openOn(withOverlay, 4);
expect(jumped.state.anchor).toBe(4);
expect(jumped.state.layout).toBe("Timeline");
expect(jumped.state.overlay).toBe("NoOverlay");
expect(jumped.state.open).toBe(0);
expect(jumped.state.reminder).toBe(0);
expect(Calendar.inv(jumped.state)).toBe(true);
});

it('holds one selected reminder in review, blocks expired confirmations, and submits only once (spec/app/calendar/reminderReopen.md, "Story: Review reopening the selected reminder without an alert")', () => {
expect(ReminderReopen.canOffer(ReminderReopen.initial, 1, "reminder-a", 1, 100, 100)).toBe(false);
expect(ReminderReopen.canOffer(ReminderReopen.initial, 1, "reminder-a", 1, 200, 50)).toBe(true);

const offered = ReminderReopen.offer(ReminderReopen.initial, 1, "reminder-a", 1, 200, 50);
expect(offered.showing).toBe(true);
expect(ReminderReopen.canOffer(offered, 2, "reminder-b", 1, 200, 50)).toBe(false);
expect(ReminderReopen.canConfirm(offered, 200)).toBe(false);
expect(ReminderReopen.canConfirm(offered, 150)).toBe(true);

const confirmed = ReminderReopen.confirm(offered, 150);
expect(confirmed.state.sending).toBe(true);
expect(confirmed.out).toEqual([{
tag: "ApplyReopen",
id: 1,
reminderKey: "reminder-a",
revision: 1,
dueAt: 200,
attempt: 1,
}]);
expect(ReminderReopen.canConfirm(confirmed.state, 160)).toBe(false);

const done = ReminderReopen.reopenAnswered(confirmed.state, 1);
expect(done.showing).toBe(false);
expect(done.sending).toBe(false);
expect(done.nextAttempt).toBe(2);
expect(ReminderReopen.inv(done)).toBe(true);

const secondOffer = ReminderReopen.offer(done, 2, "reminder-b", 2, 250, 180);
const cancelled = ReminderReopen.cancel(secondOffer);
expect(cancelled.showing).toBe(false);
expect(ReminderReopen.inv(cancelled)).toBe(true);
});

it('permits agent-requested deletion only on the owner first matching Yes or exact Title reply (spec/app/calendar/sharing.md, "Story: Delete only after I said yes")', () => {
expect(Sharing.lawANoNeverDeletes(true, true)).toBe(true);
expect(Sharing.lawANoNeverDeletes(false, true)).toBe(true);

expect(Sharing.deleteConfirmed(true, "Yes", true)).toBe(true);
expect(Sharing.deleteConfirmed(true, "Title", true)).toBe(true);
expect(Sharing.deleteConfirmed(true, "No", true)).toBe(false);
expect(Sharing.deleteConfirmed(true, "Question", true)).toBe(false);
expect(Sharing.deleteConfirmed(true, "Other", true)).toBe(false);
expect(Sharing.deleteConfirmed(false, "Yes", true)).toBe(false);
expect(Sharing.deleteConfirmed(true, "Yes", false)).toBe(false);
});

// spec/app/calendar/writing.md, "Story: Set an event's time with controls that never produce an impossible range"
it("clamps event start times inside the day, enforces positive durations, and sets safe default start hours", () => {
for (const minute of [0, 10, 600, 1430, 1439]) {
expect(Writing.lawStartStaysInTheDay(minute)).toBe(true);
}
expect(Writing.earlierStart(5)).toBe(0);
expect(Writing.laterStart(1430)).toBe(1439);

expect(Writing.lawShorterEndStaysPositive(10)).toBe(true);
expect(Writing.shorterEnd(10)).toBe(1);
expect(Writing.longerEnd(30)).toBe(45);

expect(Writing.lawDurationStaysPositive(540, 60, 500)).toBe(true);
expect(Writing.durationFor(540, 60, 500)).toBe(60);
expect(Writing.durationFor(540, 60, 540)).toBe(60);
expect(Writing.durationFor(540, 60, 630)).toBe(90);

expect(Writing.lawDefaultStartIsAnHourOfADay(0, 14)).toBe(true);
expect(Writing.lawDefaultStartIsAnHourOfADay(0, 23)).toBe(true);
expect(Writing.lawDefaultStartIsAnHourOfADay(1, 14)).toBe(true);
expect(Writing.defaultStart(0, 14)).toEqual({ offset: 0, hour: 15 });
expect(Writing.defaultStart(1, 14)).toEqual({ offset: 1, hour: 10 });
});
});
Loading
Loading