Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
import { invalidateCalendarOccurrences } from '@queries/calendar/occurrences';
import { storageServiceClient } from '@service-storage/client';
import type { CalendarOccurrenceItem } from '@service-storage/generated/schemas/calendarOccurrenceItem';
import { QueryClient, QueryClientProvider } from '@tanstack/solid-query';
import { ok } from 'neverthrow';
import { createComponent, createRoot, createSignal } from 'solid-js';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import {
type CalendarSource,
DEFAULT_GUEST_POLICY,
NO_VIEWER_CAPABILITIES,
} from '../../calendar/types';
import { useUpcomingCalendarEventsSource } from './upcoming-calendar-events';
vi.mock('@service-storage/client', () => ({
storageServiceClient: { listCalendarOccurrences: vi.fn() },
}));
vi.mock('@queries/calendar/graphql/flag', () => ({
useGraphqlCalendarHost: () => () => undefined,
markCalendarCacheUnsupported: vi.fn(),
}));
let client: QueryClient;
vi.mock('@queries/client', () => ({
get queryClient() {
return client;
},
}));
const today = new Date(2026, 8, 23, 12);
const source: CalendarSource = {
id: 'primary',
name: 'Work',
color: 'blue',
emailAddress: '[email protected]',
};
const sources = new Map([[source.id, source]]);
const list = vi.mocked(storageServiceClient.listCalendarOccurrences);
const disposers: (() => void)[] = [];
function occurrence(
day: number,
overrides: Partial<CalendarOccurrenceItem['event']> = {},
cancelled = false
): CalendarOccurrenceItem {
const startsAt = new Date(2026, 8, 23 + day, 13).toISOString();
const endsAt = new Date(2026, 8, 23 + day, 14).toISOString();
const time = { kind: 'timed' as const, startsAt, endsAt };
const id = `event-${day}`;
return {
event: {
id,
ownerId: 'macro|[email protected]',
icalUid: id,
calendarId: source.id,
title: id,
status: 'confirmed',
visibility: 'default',
transparency: 'opaque',
eventType: 'default',
conferenceUrl: 'https://meet.google.com/room',
time,
recurrenceLines: [],
sequence: 0,
isReadOnly: false,
guestPolicy: DEFAULT_GUEST_POLICY,
isSnapshot: false,
viewerCan: NO_VIEWER_CAPABILITIES,
attendees: [],
createdAt: today.toISOString(),
updatedAt: today.toISOString(),
...overrides,
},
occurrence: {
eventId: id,
occurrenceKey: startsAt,
time,
isCancelled: cancelled,
},
};
}
function respond(items: CalendarOccurrenceItem[]) {
list.mockResolvedValue(ok({ items, hasMore: false, syncStatus: 'ready' }));
}
function setup(metadataReady = true) {
const [now, setNow] = createSignal(today);
const [userId, setUserId] = createSignal<string | undefined>(
'macro|[email protected]'
);
const [hidden, setHidden] = createSignal<ReadonlySet<string>>(new Set());
const [sourcesReady, setSourcesReady] = createSignal(metadataReady);
let result!: ReturnType<typeof useUpcomingCalendarEventsSource>;
const Harness = () => {
result = useUpcomingCalendarEventsSource({
userId,
now,
sourceById: () => sources,
sourcesReady,
isSourceVisible: (id) => !hidden().has(id),
});
return null;
};
const dispose = createRoot((dispose) => {
createComponent(QueryClientProvider, {
client,
get children() {
return createComponent(Harness, {});
},
});
return dispose;
});
disposers.push(dispose);
return { result, setNow, setUserId, setHidden, setSourcesReady };
}
beforeEach(() => {
client = new QueryClient({ defaultOptions: { queries: { retry: false } } });
list.mockReset();
respond([]);
});
afterEach(() => {
for (const dispose of disposers.splice(0)) dispose();
client.clear();
});
describe('upcoming calendar events source', () => {
it('waits to display agenda colors without delaying occurrence fetches', async () => {
respond([1, 2, 3, 4, 5].map((day) => occurrence(day)));
const { result, setSourcesReady } = setup(false);
await vi.waitFor(() =>
expect(client.getQueryCache().getAll()[0]?.state.status).toBe('success')
);
expect(result.events()).toEqual([]);
expect(result.loading()).toBe(true);
expect(list).toHaveBeenCalledTimes(1);
setSourcesReady(true);
expect(result.events()).toHaveLength(5);
expect(result.events().every((event) => event.color === source.color)).toBe(
true
);
expect(result.loading()).toBe(false);
expect(list).toHaveBeenCalledTimes(1);
});
it('skips working locations and keeps looking for upcoming events', async () => {
const locations = [0, 1, 2, 3, 4].map((day) =>
occurrence(day, { title: 'Office', eventType: 'working_location' })
);
const allDay = {
kind: 'allDay' as const,
startDate: '2026-09-23',
endDate: '2026-09-24',
};
locations[0].event.time = allDay;
locations[0].occurrence.time = allDay;
list
.mockResolvedValueOnce(
ok({ items: locations, hasMore: false, syncStatus: 'ready' })
)
.mockResolvedValueOnce(
ok({
items: [16, 17, 18, 19, 20].map((day) => occurrence(day)),
hasMore: false,
syncStatus: 'ready',
})
);
const { result } = setup();
await vi.waitFor(() => expect(result.events()).toHaveLength(5));
expect(result.events().map((event) => event.title)).toEqual(
[16, 17, 18, 19, 20].map((day) => `event-${day}`)
);
expect(list).toHaveBeenCalledTimes(2);
});
it('stops after the first window when five events are available', async () => {
respond([5, 3, 2, 4, 1].map((day) => occurrence(day)));
const { result } = setup();
await vi.waitFor(() => expect(result.events()).toHaveLength(5));
expect(result.events().map((call) => call.title)).toEqual(
[1, 2, 3, 4, 5].map((day) => `event-${day}`)
);
expect(result.events()[0].color).toBe(source.color);
expect(result.events()[0].url).toBe(
'https://meet.google.com/room?authuser=calendar%40macro.com'
);
expect(list).toHaveBeenCalledTimes(1);
expect(result.loading()).toBe(false);
expect(result.findEvent(result.events()[0].id)).toEqual(
expect.objectContaining({
title: 'event-1',
attendees: [],
isReadOnly: false,
conferenceUrl: 'https://meet.google.com/room',
})
);
});
it('uses the visible copy type without filtering ordinary events named Office', async () => {
const location = occurrence(0, {
sources: [
{
calendarId: source.id,
title: 'Office',
isReadOnly: false,
eventType: 'working_location',
reminders: { useDefault: true, overrides: [] },
transparency: 'transparent',
visibility: 'default',
},
],
});
respond([
location,
...[1, 2, 3, 4, 5].map((day) => occurrence(day, { title: 'Office' })),
]);
const { result } = setup();
await vi.waitFor(() => expect(result.events()).toHaveLength(5));
expect(result.events().map((event) => event.eventId)).toEqual(
[1, 2, 3, 4, 5].map((day) => `event-${day}`)
);
expect(
result.findEvent(
JSON.stringify(['event-0', location.occurrence.occurrenceKey])
)
).toBeUndefined();
});
it('widens to later windows after filtering cancelled, declined and hidden events', async () => {
const hidden = occurrence(1, {
sources: [
{
calendarId: 'hidden',
title: 'Hidden',
isReadOnly: false,
eventType: 'default',
reminders: { useDefault: true, overrides: [] },
transparency: 'opaque',
visibility: 'default',
},
],
});
list
.mockResolvedValueOnce(
ok({
items: [
hidden,
occurrence(2, {}, true),
occurrence(3, {
attendees: [
{
email: '[email protected]',
isSelf: true,
isOrganizer: false,
isOptional: false,
responseStatus: 'declined',
},
],
}),
occurrence(4, { eventType: 'out_of_office' }),
occurrence(5, { conferenceUrl: 'javascript:alert(1)' }),
occurrence(6, { conferenceUrl: null }),
],
hasMore: false,
syncStatus: 'ready',
})
)
.mockResolvedValueOnce(
ok({
items: [16, 17, 18, 19, 20].map((day) => occurrence(day)),
hasMore: false,
syncStatus: 'ready',
})
);
const { result, setHidden } = setup();
setHidden(new Set(['hidden']));
await vi.waitFor(() => expect(result.events()).toHaveLength(8));
expect(
result
.events()
.map((event) => event.title)
.slice(0, 3)
).toEqual(['event-4', 'event-5', 'event-6']);
expect(result.events()[1].url).toBeUndefined();
expect(result.events()[2].url).toBeUndefined();
expect(
result
.events()
.slice(0, 4)
.map((event) => event.isMeeting)
).toEqual([false, false, false, true]);
expect(list).toHaveBeenCalledTimes(2);
expect(list.mock.calls[1][0].start).toBe(list.mock.calls[0][0].end);
});
it('stops sparse-calendar lookups at the supported 729-day horizon', async () => {
const { result } = setup();
await vi.waitFor(() => expect(result.loading()).toBe(false));
expect(list).toHaveBeenCalledTimes(6);
const end = new Date(today);
end.setHours(0, 0, 0, 0);
end.setDate(end.getDate() + 729);
expect(list.mock.calls[5][0].end).toBe(end.toISOString());
expect(result.events()).toEqual([]);
});
it('keeps recurring occurrences and recognizes embedded Macro links', async () => {
const macroUrl = 'https://macro.com/app/meet/valid-token-1234567890';
respond(
[1, 2, 3, 4, 5].map((day) =>
occurrence(day, {
id: 'series',
conferenceUrl: null,
description: `<p>Join <a href="${macroUrl}?join=true">the call</a></p>`,
})
)
);
const { result } = setup();
await vi.waitFor(() => expect(result.events()).toHaveLength(5));
expect(new Set(result.events().map((call) => call.id)).size).toBe(5);
expect(result.events().every((call) => call.url === macroUrl)).toBe(true);
});
it('widens when a displayed call ends and refreshes through shared calendar invalidation', async () => {
list
.mockResolvedValueOnce(
ok({
items: [0, 1, 2, 3, 4].map((day) => occurrence(day)),
hasMore: false,
syncStatus: 'ready',
})
)
.mockResolvedValueOnce(
ok({ items: [occurrence(16)], hasMore: false, syncStatus: 'ready' })
);
const { result, setNow } = setup();
await vi.waitFor(() => expect(result.events()).toHaveLength(5));
setNow(new Date(2026, 8, 23, 14));
await vi.waitFor(() =>
expect(result.events().at(-1)?.title).toBe('event-16')
);
expect(result.events().some((call) => call.title === 'event-0')).toBe(
false
);
respond([1, 2, 3, 4, 5].map((day) => occurrence(day)));
await invalidateCalendarOccurrences();
await vi.waitFor(() =>
expect(result.events().at(-1)?.title).toBe('event-5')
);
});
it("does not show another account's placeholder data after switching users", async () => {
respond([1, 2, 3, 4, 5].map((day) => occurrence(day)));
const { result, setUserId } = setup();
await vi.waitFor(() => expect(result.events()).toHaveLength(5));
list.mockImplementation(() => new Promise(() => {}));
setUserId('macro|[email protected]');
expect(result.events()).toEqual([]);
expect(result.loading()).toBe(true);
});
});