Skip to content

Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.

4.5 KB
Raw Blame
import type {
  CalendarParticipant,
  GoogleCalendarAccount,
  GoogleCalendarEvent,
} from '@/data/calendarTypes';
import {
  buildCalendarMeetingContext,
  getCalendarParticipantEmails,
  getCalendarSpeakerLabelSuggestions,
  getSpeakerCountSuggestion,
  parseCalendarParticipants,
} from '../meetingContext';

const participant = (overrides: Partial<CalendarParticipant>): CalendarParticipant => ({
  email: null,
  displayName: null,
  responseStatus: 'accepted',
  optional: false,
  organizer: false,
  resource: false,
  self: false,
  ...overrides,
});

const event = (overrides: Partial<GoogleCalendarEvent>): GoogleCalendarEvent =>
  ({
    id: 30,
    accountId: 1,
    calendarLocalId: 10,
    googleCalendarId: 'primary',
    googleEventId: 'event-1',
    iCalUID: 'ical-1',
    summary: 'Weekly Planning',
    description: null,
    location: null,
    startAt: '2026-06-24T10:00:00.000Z',
    endAt: '2026-06-24T11:00:00.000Z',
    allDay: 0,
    status: 'confirmed',
    organizerEmail: '[email protected]',
    creatorEmail: null,
    attendeesJson: null,
    updated: null,
    htmlLink: null,
    createdAt: null,
    updatedAt: null,
    ...overrides,
  }) as GoogleCalendarEvent;

const account = (overrides: Partial<GoogleCalendarAccount> = {}): GoogleCalendarAccount =>
  ({
    id: 1,
    googleSubject: 'google-sub-1',
    email: '[email protected]',
    displayName: 'Owner',
    grantedScopes: 'openid email calendar',
    status: 'connected',
    lastSyncAt: null,
    lastError: null,
    createdAt: null,
    updatedAt: null,
    ...overrides,
  }) as GoogleCalendarAccount;

describe('calendar meeting context', () => {
  it('parses normalized attendee JSON defensively', () => {
    expect(parseCalendarParticipants(null)).toBeNull();
    expect(parseCalendarParticipants('not-json')).toBeNull();
    expect(
      parseCalendarParticipants(JSON.stringify([{ email: ' [email protected] ' }, {}])),
    ).toEqual([expect.objectContaining({ email: '[email protected]', displayName: null })]);
  });

  it('suggests speaker count from non-declined human attendees only', () => {
    expect(
      getSpeakerCountSuggestion([
        participant({ email: '[email protected]' }),
        participant({ email: '[email protected]', displayName: 'Duplicate Alice' }),
        participant({ email: '[email protected]', responseStatus: 'declined' }),
        participant({ displayName: 'Conference Room', resource: true }),
        participant({ displayName: 'Huddle Room' }),
        participant({ displayName: 'Casey' }),
      ]),
    ).toBe(2);
  });

  it('returns human attendee emails and quick-label suggestions', () => {
    const participants = [
      participant({ email: '[email protected]', displayName: 'Alice' }),
      participant({ email: '[email protected]', displayName: 'Duplicate Alice' }),
      participant({ email: '[email protected]', responseStatus: 'declined' }),
      participant({ displayName: 'Conference Room', resource: true }),
      participant({ email: '[email protected]' }),
    ];

    expect(getCalendarParticipantEmails(participants)).toEqual([
      '[email protected]',
      '[email protected]',
    ]);
    expect(getCalendarSpeakerLabelSuggestions(participants)).toEqual([
      { label: 'Alice', email: '[email protected]' },
      { label: '[email protected]', email: '[email protected]' },
    ]);
  });

  it('builds selected event context with organizer fallback and opaque event id', () => {
    const context = buildCalendarMeetingContext(
      event({
        attendeesJson: JSON.stringify([participant({ email: '[email protected]' })]),
      }),
      account(),
    );

    expect(context.title).toBe('Weekly Planning');
    expect(context.participants).toEqual([
      expect.objectContaining({ email: '[email protected]' }),
      expect.objectContaining({ email: '[email protected]', organizer: true }),
    ]);
    expect(context.suggestedSpeakerCount).toBe(2);
    expect(JSON.parse(context.calendarEventId)).toEqual({
      provider: 'google_calendar',
      accountSubject: 'google-sub-1',
      calendarId: 'primary',
      eventId: 'event-1',
      iCalUID: 'ical-1',
    });
  });

  it('uses the default meeting title when an event summary is unavailable', () => {
    const context = buildCalendarMeetingContext(
      event({ summary: '   ', organizerEmail: null }),
      null,
    );

    expect(context.title).toBe('Untitled meeting');
    expect(context.participants).toEqual([]);
    expect(context.suggestedSpeakerCount).toBeUndefined();
  });
});