diff --git a/app/assistant/date-format.ts b/app/assistant/date-format.ts new file mode 100644 index 00000000..e7a810fb --- /dev/null +++ b/app/assistant/date-format.ts @@ -0,0 +1,62 @@ +/** + * Date and calendar-event text for the assistant's tool results. Pure (no + * native imports) so it can be unit-tested. + */ + +const WEEKDAYS = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]; +const MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; + +export type FormattableEvent = { + title: string; + startMs: number; + endMs: number; + allDay: boolean; + location?: string; +}; + +export function formatEvent(event: FormattableEvent): string { + const location = event.location ? ` @ ${event.location}` : ""; + return `- ${event.title || "(untitled)"} — ${formatEventSpan(event)}${location}`; +} + +/** + * When an event happens, end included: a multi-day all-day event names its last + * day (all-day ends are exclusive, at the midnight after it), a timed event its + * end time. Without the end, an event that started yesterday and runs through + * tomorrow reads as yesterday's. + */ +export function formatEventSpan(event: Pick): string { + const start = new Date(event.startMs); + if (event.allDay) { + const lastDay = new Date(Math.max(event.startMs, event.endMs - 1)); + return sameDay(start, lastDay) + ? `${formatDate(start)} (all day)` + : `${formatDate(start)} – ${formatDate(lastDay)} (all day)`; + } + if (!(event.endMs > event.startMs)) return formatDateTime(start); + const end = new Date(event.endMs); + return sameDay(start, end) + ? `${formatDateTime(start)} – ${formatTime(end)}` + : `${formatDateTime(start)} – ${formatDateTime(end)}`; +} + +export function formatDateTime(date: Date): string { + return `${formatDate(date)}, ${formatTime(date)}`; +} + +export function formatDate(date: Date): string { + return `${WEEKDAYS[date.getDay()]} ${MONTHS[date.getMonth()]} ${date.getDate()}`; +} + +export function formatTime(date: Date): string { + let hours = date.getHours(); + const minutes = date.getMinutes().toString().padStart(2, "0"); + const meridiem = hours >= 12 ? "PM" : "AM"; + hours = hours % 12; + if (hours === 0) hours = 12; + return `${hours}:${minutes} ${meridiem}`; +} + +function sameDay(a: Date, b: Date): boolean { + return a.getFullYear() === b.getFullYear() && a.getMonth() === b.getMonth() && a.getDate() === b.getDate(); +} diff --git a/app/assistant/system-tools.ts b/app/assistant/system-tools.ts index ccff0c25..4d41bc8a 100644 --- a/app/assistant/system-tools.ts +++ b/app/assistant/system-tools.ts @@ -1,10 +1,11 @@ -import { readUpcomingEventsAsync, type CalendarEvent } from "../native/calendar"; +import { readUpcomingEventsAsync } from "../native/calendar"; import { hasCalendarPermission } from "../native/calendar-permissions"; import { mediaControllerBridge } from "../native/media-controller"; import { spotifyService } from "../native/spotify"; import { dismissNotification, readActiveNotifications } from "../native/notification-icons"; import { shell } from "../ui/shell/shell"; import { toolRegistry, type ToolRegistry, type ToolResult } from "./tool-registry"; +import { formatDateTime, formatEvent } from "./date-format"; /** * Registers the always-available system tools into the registry. Called once at @@ -12,9 +13,6 @@ import { toolRegistry, type ToolRegistry, type ToolResult } from "./tool-registr * media, notifications); nothing here needs an app window to be open. */ -const WEEKDAYS = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]; -const MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; - let registered = false; export function registerSystemTools(registry: ToolRegistry = toolRegistry): void { @@ -244,35 +242,12 @@ function describeNowPlaying(): string { return "Nothing is currently playing."; } -function formatEvent(event: CalendarEvent): string { - const when = event.allDay ? `${formatDate(new Date(event.startMs))} (all day)` : formatDateTime(new Date(event.startMs)); - const location = event.location ? ` @ ${event.location}` : ""; - return `- ${event.title || "(untitled)"} — ${when}${location}`; -} - function formatNotification(notification: { key: string; appName: string; title: string; text: string; customText?: string[] }): string { const title = notification.title || notification.customText?.join(" ") || "(no title)"; const body = notification.text ? `: ${notification.text}` : ""; return `- [${notification.appName}] ${title}${body} (key: ${notification.key})`; } -function formatDateTime(date: Date): string { - return `${formatDate(date)}, ${formatTime(date)}`; -} - -function formatDate(date: Date): string { - return `${WEEKDAYS[date.getDay()]} ${MONTHS[date.getMonth()]} ${date.getDate()}`; -} - -function formatTime(date: Date): string { - let hours = date.getHours(); - const minutes = date.getMinutes().toString().padStart(2, "0"); - const meridiem = hours >= 12 ? "PM" : "AM"; - hours = hours % 12; - if (hours === 0) hours = 12; - return `${hours}:${minutes} ${meridiem}`; -} - function clampNumber(value: unknown, min: number, max: number, fallback: number): number { const num = typeof value === "number" ? value : Number(value); if (!Number.isFinite(num)) return fallback; diff --git a/tests/date-format.test.cjs b/tests/date-format.test.cjs new file mode 100644 index 00000000..5b847e22 --- /dev/null +++ b/tests/date-format.test.cjs @@ -0,0 +1,45 @@ +const test = require('node:test'); +const assert = require('node:assert/strict'); +const fs = require('node:fs'); +const path = require('node:path'); +const vm = require('node:vm'); +const ts = require('typescript'); + +// Local-time formatting: pin the zone so the expectations hold on any machine. +process.env.TZ = 'Europe/Brussels'; + +function load(file) { + const sandbox = { exports: {}, require: id => { throw new Error(`Unstubbed import: ${id}`); } }; + vm.runInNewContext(ts.transpileModule(fs.readFileSync(path.join(__dirname, '..', file), 'utf8'), { + compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020 }, + }).outputText, sandbox); + return sandbox.exports; +} + +const { formatEvent, formatEventSpan } = load('app/assistant/date-format.ts'); +const local = (y, m, d, h = 0, min = 0) => new Date(y, m - 1, d, h, min).getTime(); + +test('a multi-day all-day event names its last day (all-day ends are exclusive)', () => { + // Mon Nov 16 – Wed Nov 18, as the calendar provider stores it: end = midnight after the last day. + const span = formatEventSpan({ startMs: local(2026, 11, 16), endMs: local(2026, 11, 19), allDay: true }); + assert.equal(span, 'Mon Nov 16 – Wed Nov 18 (all day)'); +}); + +test('a one-day all-day event stays a single date', () => { + assert.equal(formatEventSpan({ startMs: local(2026, 11, 16), endMs: local(2026, 11, 17), allDay: true }), 'Mon Nov 16 (all day)'); +}); + +test('a timed event shows its end time, and the end date when it crosses midnight', () => { + assert.equal(formatEventSpan({ startMs: local(2026, 11, 19, 9), endMs: local(2026, 11, 19, 12), allDay: false }), 'Thu Nov 19, 9:00 AM – 12:00 PM'); + assert.equal(formatEventSpan({ startMs: local(2026, 11, 19, 22), endMs: local(2026, 11, 20, 2), allDay: false }), 'Thu Nov 19, 10:00 PM – Fri Nov 20, 2:00 AM'); +}); + +test('a timed event without a usable end keeps the old start-only form', () => { + assert.equal(formatEventSpan({ startMs: local(2026, 11, 19, 9), endMs: local(2026, 11, 19, 9), allDay: false }), 'Thu Nov 19, 9:00 AM'); +}); + +test('formatEvent keeps the title and location around the span', () => { + const line = formatEvent({ title: 'Office', startMs: local(2026, 11, 16), endMs: local(2026, 11, 19), allDay: true, location: 'HQ' }); + assert.equal(line, '- Office — Mon Nov 16 – Wed Nov 18 (all day) @ HQ'); + assert.equal(formatEvent({ title: '', startMs: local(2026, 11, 16), endMs: local(2026, 11, 17), allDay: true, location: '' }), '- (untitled) — Mon Nov 16 (all day)'); +}); diff --git a/tests/ios-calendar.test.cjs b/tests/ios-calendar.test.cjs index 88acbd03..484eb53f 100644 --- a/tests/ios-calendar.test.cjs +++ b/tests/ios-calendar.test.cjs @@ -243,6 +243,7 @@ test('iOS assistant calendar tool awaits events, reports denied access and failu '../native/media-controller': { mediaControllerBridge: { start: async () => {} } }, '../native/spotify': { spotifyService: () => null }, '../native/notification-icons': {}, '../ui/shell/shell': {}, './tool-registry': {}, + './date-format': load('app/assistant/date-format.ts'), }, { global: { isIOS: true } }); registerSystemTools({ registerSystemTool: (definition, handler) => registered.set(definition.name, handler) }); assert.deepEqual([...registered.keys()], ['glasses.get_state', 'glasses.show_alert', 'calendar.list_events', diff --git a/tests/ios-spotify.test.cjs b/tests/ios-spotify.test.cjs index 4a7f941c..0a7428a0 100644 --- a/tests/ios-spotify.test.cjs +++ b/tests/ios-spotify.test.cjs @@ -511,6 +511,7 @@ test('the assistant offers Spotify search-and-play once signed in and refreshes snapshot: () => ({ accessEnabled: true, playbackState: 'playing', title: 'Song', artist: 'Band', appName: 'Spotify' }) } }, '../native/spotify': { spotifyService: () => spotify }, '../native/notification-icons': {}, '../ui/shell/shell': {}, './tool-registry': {}, + './date-format': load('app/assistant/date-format.ts'), }, { global: { isIOS: true } }); registerSystemTools({ registerSystemTool: (definition, handler) => systemTools.set(definition.name, handler),