Skip to content

Private AI meeting notes that stay on your computer. No meeting bot. Free and open source for Mac, Windows and Linux. An alternative to Granola, Otter and Fireflies.

15.4 KB
Raw Blame
import { readFileSync } from 'node:fs'
import { afterEach, beforeEach, expect, it, vi } from 'vitest'
import { isValidElement, type ReactElement, type ReactNode } from 'react'
import { renderToStaticMarkup } from 'react-dom/server'
import { Menu } from '@base-ui/react/menu'
import { StartRecording } from '../src/renderer/src/components/StartRecording'
import { recordingChoices } from '../src/renderer/src/agenda'
import { toasts } from '../src/renderer/src/components/TrashToast'
import type { AgendaEvent, WidgetView } from '../src/shared/types'

// Exercise the component's handlers and subscriptions without a browser or Electron.
const hooks = vi.hoisted(() => ({ values: [] as unknown[], refs: [] as { current: unknown }[], state: 0, ref: 0,
  effects: [] as (() => (() => void) | void)[], layout: [] as (() => (() => void) | void)[] }))
vi.mock('react', async original => ({ ...await original<typeof import('react')>(),
  useState: (initial: unknown) => {
    const index = hooks.state++
    if (!(index in hooks.values)) hooks.values[index] = typeof initial === 'function' ? initial() : initial
    return [hooks.values[index], (value: unknown) => { hooks.values[index] = typeof value === 'function' ? value(hooks.values[index]) : value }]
  },
  useRef: (initial: unknown) => hooks.refs[hooks.ref++] ?? (hooks.refs[hooks.ref - 1] = { current: initial }),
  useCallback: (callback: unknown) => callback,
  useEffect: (effect: () => void) => hooks.effects.push(effect),
  useLayoutEffect: (effect: () => void) => hooks.layout.push(effect),
}))
vi.mock('react-dom', () => ({ createPortal: (children: unknown) => children }))
vi.mock('../src/renderer/src/dates', () => ({ useDay: () => new Date().toDateString() }))
vi.mock('../src/renderer/src/components/TrashToast', () => ({ toasts: { add: vi.fn(), close: vi.fn() } }))
const now = new Date(2026, 10, 13, 12).getTime()
const event = (id: string, start = now - 60_000, end = now + 30 * 60_000): AgendaEvent => ({ id, title: `Planning ${id}`, start, end, url: null, attendees: [{ name: 'Robin Vale', email: null }] })
const start = vi.fn()
const day = vi.fn()
const uploaded = vi.fn()
const gone = vi.fn()
let calendarChanged: () => void
let meetingsChanged: () => void
let widgetChanged: (view: WidgetView | null) => void
let cleanups: (() => void)[]
const flush = async () => { await Promise.resolve(); await Promise.resolve() }
const render = (emptyActions?: HTMLElement, folderId?: string) => {
  hooks.state = 0; hooks.ref = 0; hooks.effects = []; hooks.layout = []
  return StartRecording({ folderId, onUploaded: uploaded, onGone: gone, emptyActions }) as ReactElement<Record<string, unknown>> | null
}
const run = (effect: () => (() => void) | void) => { const cleanup = effect(); if (cleanup) cleanups.push(cleanup) }
type Element = ReactElement<Record<string, unknown>>
function elements(node: ReactNode): Element[] {
  if (Array.isArray(node)) return node.flatMap(elements)
  return isValidElement<Record<string, unknown>>(node) ? [node, ...elements(node.props.children as ReactNode)] : []
}
const find = (tree: ReactNode, id: string) => elements(tree).find(el => el.props['data-testid'] === id)!
const label = (tree: ReactNode) => renderToStaticMarkup(find(tree, 'start-recording').props.children as ReactNode)
const click = async (element: Element) => { (element.props.onClick as () => void)(); await flush() }
async function calendar(events: AgendaEvent[]) {
  day.mockResolvedValue({ status: 'granted', events })
  render(); run(hooks.effects[0]); await flush()
  return render()
}
beforeEach(async () => {
  vi.useFakeTimers(); vi.setSystemTime(now); vi.clearAllMocks()
  hooks.values = []; hooks.refs = []; cleanups = []
  start.mockResolvedValue({ id: 'note' }); day.mockResolvedValue({ status: 'granted', events: [] })
  vi.stubGlobal('document', { getElementById: () => ({ nodeType: 1 }) })
  vi.stubGlobal('window', { addEventListener: vi.fn(), removeEventListener: vi.fn(), yogurt: {
    capabilities: { calendar: true },
    widget: { state: async () => null, onState: (cb: typeof widgetChanged) => { widgetChanged = cb; return vi.fn() } },
    calendar: { day, onChange: (cb: () => void) => { calendarChanged = cb; return vi.fn() } },
    meetings: { start, onChange: (cb: () => void) => { meetingsChanged = cb; return vi.fn() } },
  } })
  render(); run(hooks.layout[0]); await flush()
})
afterEach(() => { cleanups.reverse().forEach(cleanup => cleanup()); vi.useRealTimers(); vi.unstubAllGlobals() })

it('keeps upload and the unattached action available outside the live/15-minute window', async () => {
  for (const events of [[], [event('past', now - 60_000, now)], [event('later', now + 15 * 60_000 + 1)]]) {
    const tree = await calendar(events)
    expect(label(tree)).toContain('Record')
    expect(find(tree, 'recording-choices').type).toBe(Menu.Trigger)
    expect(find(tree, 'recording-upload')).toBeDefined()
    await click(find(tree, 'start-recording'))
    expect(start).toHaveBeenLastCalledWith(null, undefined)
  }
})
it('starts an unattached recording from the empty library even during a calendar event', async () => {
  await calendar([event('live')])
  const tree = render(document.getElementById('empty')!)
  const cta = find(tree, 'empty-start-recording')
  expect(renderToStaticMarkup(cta.props.children as ReactNode)).toContain('Record')
  await click(cta)
  expect(start).toHaveBeenCalledExactlyOnceWith(null, undefined)
})

it('labels the primary action from the meeting state and names an uploaded note in its tooltip', async () => {
  for (const [events, text, tooltip] of [
    [[], 'Record', 'Record'],
    [[event('live')], 'Take notes · Planning live', 'Take notes · Planning live'],
    [[{ ...event('live'), note: { id: 'note', state: 'stopped' } }], 'Resume recording', 'Resume recording'],
    [[{ ...event('live'), note: { id: 'note', state: 'done', source: 'audio-file' } }], 'Open note', 'Open Planning live'],
  ] satisfies [AgendaEvent[], string, string][]) {
    const tree = await calendar(events)
    expect(label(tree)).toContain(`<span class="recording-label">${text}</span>`)
    expect(find(tree, 'start-recording').props.title).toBe(tooltip)
  }
})

it('carries the folder through the toolbar, calendar, unattached and empty-state record actions', async () => {
  await calendar([event('live')])
  const tree = render(document.getElementById('empty')!, 'reviews')
  for (const [action, eventId] of [['start-recording', 'live'], ['recording-meeting', 'live'], ['recording-unattached', null], ['empty-start-recording', null]] as const) {
    await click(find(tree, action))
    expect(start).toHaveBeenLastCalledWith(eventId, 'reviews')
  }
})

it('attaches the one live meeting and preserves its full title while CSS truncates the label', async () => {
  const live = { ...event('live'), title: 'Planning the next release and reviewing every open question '.repeat(4) }
  const tree = await calendar([event('soon', now + 8 * 60_000), live])
  expect(label(tree)).toContain(`Take notes · ${live.title}`)
  expect(find(tree, 'start-recording').props.title).toBe(`Take notes · ${live.title}`)
  expect(find(tree, 'recording-choices').type).toBe(Menu.Trigger)
  await click(find(tree, 'start-recording'))
  expect(start).toHaveBeenCalledExactlyOnceWith(live.id, undefined)
  expect(uploaded).not.toHaveBeenCalled()
  const css = readFileSync(new URL('../src/renderer/src/workspace.css', import.meta.url), 'utf8')
  expect(css).toMatch(/\.recording-label \{[^}]*max-width:200px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap/)
  expect(css).toMatch(/\.recording-split \{[^}]*max-width:min\(100%,276px\)/)
})
it('uses the menu trigger for two live meetings, with live choices first, New recording, and Upload', async () => {
  const tree = await calendar([event('soon', now + 8 * 60_000), event('second', now - 30_000), event('first')])
  expect(label(tree)).toContain('Take notes · 2 now')
  // The same Base UI trigger used by the arrow owns click, Enter, arrows and Escape.
  expect(find(tree, 'start-recording').type).toBe(Menu.Trigger)
  expect(find(tree, 'start-recording').props.onClick).toBeUndefined()
  ;(tree!.props.onOpenChange as (open: boolean) => void)(true)
  expect(render()!.props.open).toBe(true)
  expect(start).not.toHaveBeenCalled()
  const items = elements(tree).filter(el => el.type === Menu.Item)
  expect(items.map(item => item.props.label)).toEqual(['Planning first', 'Planning second', 'Planning soon', undefined, undefined])
  expect(renderToStaticMarkup(items[0].props.children as ReactNode)).toContain('Now')
  const soon = renderToStaticMarkup(items[2].props.children as ReactNode)
  expect(soon).toContain('in 8 min'); expect(soon).toContain('Robin Vale')
  await click(items[2]); expect(start).toHaveBeenLastCalledWith('soon', undefined)
  expect(render()!.props.open).toBe(false)
  await click(find(render(), 'recording-unattached')); expect(start).toHaveBeenLastCalledWith(null, undefined)
})
it('suggests an upcoming meeting but keeps the main action unattached', async () => {
  const tree = await calendar([event('soon', now + 8 * 60_000)])
  expect(label(tree)).toContain('Record')
  expect(find(tree, 'recording-choices').type).toBe(Menu.Trigger)
  await click(find(tree, 'start-recording'))
  expect(start).toHaveBeenCalledExactlyOnceWith(null, undefined)
})
it.each(['stopped', 'done'] as const)('labels a %s note for resume in the title bar and menu without suggesting a new folder', async state => {
  const live = { ...event('live'), note: { id: 'existing', state } }
  const tree = await calendar([live])
  expect(label(tree)).toContain('Resume recording')
  expect(find(tree, 'start-recording').props.title).toBe('Resume recording')
  const row = find(tree, 'recording-meeting')
  const html = renderToStaticMarkup(row.props.children as ReactNode)
  expect(html).toContain('Resume notes · Planning live')
  // resume keeps the mic: a counterclockwise arrow reads as undo
  expect(html).not.toContain('data-icon="RotateCcw"')
  expect(html).toContain('<svg')
  expect(html).toContain('Now')
  start.mockResolvedValue({ id: 'existing', resumeRequested: true })
  await click(find(tree, 'start-recording'))
  await click(row)
  expect(start).toHaveBeenCalledTimes(2)
  expect(start).toHaveBeenLastCalledWith(live.id, undefined)
  expect(uploaded).not.toHaveBeenCalled()
})
it('keeps the take-notes label and start path for a prepared note', async () => {
  const tree = await calendar([{ ...event('prepared'), note: { id: 'prepared-note', state: 'prepared' } }])
  expect(label(tree)).toContain('Take notes · Planning prepared')
  const html = renderToStaticMarkup(find(tree, 'recording-meeting').props.children as ReactNode)
  expect(html).not.toContain('Resume notes')
  expect(html).not.toContain('data-icon="RotateCcw"')
  expect(html).toContain('<svg')
  await click(find(tree, 'start-recording'))
  expect(start).toHaveBeenCalledExactlyOnceWith('prepared', undefined)
})
it('refreshes the resume label when the linked note changes or is trashed', async () => {
  const live = event('live')
  await calendar([live])
  day.mockResolvedValue({ status: 'granted', events: [{ ...live, note: { id: 'existing', state: 'done' } }] })
  meetingsChanged(); await flush()
  expect(label(render())).toContain('Resume recording')
  day.mockResolvedValue({ status: 'granted', events: [live] })
  meetingsChanged(); await flush()
  expect(label(render())).toContain('Take notes · Planning live')
})
it('changes at the 15-minute, start and end boundaries and never offers a past event', async () => {
  const upcoming = event('boundary', now + 16 * 60_000, now + 17 * 60_000)
  await calendar([upcoming])
  for (const [minutes, expected, arrow] of [[0, 'Record', true], [1, 'Record', true], [16, 'Take notes · Planning boundary', true], [17, 'Record', true]] as const) {
    vi.setSystemTime(now + minutes * 60_000 - 1)
    render(); run(hooks.effects[1]); await vi.advanceTimersByTimeAsync(1)
    const tree = render()
    expect(label(tree)).toContain(expected)
    expect(!!find(tree, 'recording-choices')).toBe(arrow)
  }
  expect(recordingChoices([upcoming], upcoming.end)).toEqual([])
})
it('hides the entire control during recording and closes a previously open menu', async () => {
  const tree = await calendar([event('first'), event('second')])
  ;(tree!.props.onOpenChange as (open: boolean) => void)(true)
  widgetChanged({ kind: 'recording' } as WidgetView)
  expect(render()).toBeNull(); run(hooks.effects[2])
  widgetChanged(null)
  expect(render()!.props.open).toBe(false)
})
it('clears revoked calendars immediately and ignores an older in-flight response', async () => {
  await calendar([event('live')])
  let reply!: (value: unknown) => void
  day.mockImplementationOnce(() => new Promise(resolve => { reply = resolve }))
  calendarChanged()
  expect(find(render(), 'recording-meeting')).toBeUndefined()
  day.mockResolvedValue({ status: 'denied', events: [event('private')] })
  calendarChanged(); await flush()
  reply({ status: 'granted', events: [event('stale')] }); await flush()
  expect(find(render(), 'recording-meeting')).toBeUndefined()
})
it('refreshes on focus and polls today without reading the agenda-selected date', async () => {
  await calendar([])
  day.mockClear()
  const focus = vi.mocked(window.addEventListener).mock.calls.find(([name]) => name === 'focus')![1] as () => void
  focus(); await flush()
  expect(day).toHaveBeenCalledExactlyOnceWith(now)
  await vi.advanceTimersByTimeAsync(60_000)
  expect(day).toHaveBeenLastCalledWith(now + 60_000)
})
it('reports a removed event, refreshes choices, and never reports a failed or raced start as new', async () => {
  const tree = await calendar([event('live')])
  start.mockResolvedValueOnce({ error: 'gone' })
  day.mockResolvedValue({ status: 'granted', events: [] })
  await click(find(tree, 'start-recording'))
  expect(toasts.add).toHaveBeenLastCalledWith(expect.objectContaining({ title: 'That meeting is no longer on your calendar.' }))
  expect(gone).toHaveBeenCalledOnce()
  expect(find(render(), 'recording-meeting')).toBeUndefined()
  expect(uploaded).not.toHaveBeenCalled()
  start.mockResolvedValueOnce({ id: 'existing', alreadyRecording: true })
  await click(find(render(), 'start-recording'))
  expect(uploaded).not.toHaveBeenCalled()
  start.mockRejectedValueOnce(new Error('fixture start failure'))
  await click(find(render(), 'start-recording'))
  expect(toasts.add).toHaveBeenLastCalledWith(expect.objectContaining({ title: "Yogurt couldn't start the notes. Try again in a moment." }))
  expect(find(render(), 'start-recording').props.disabled).toBe(false)
  expect(gone).toHaveBeenCalledOnce()
})

it('opens an uploaded calendar-linked note without offering to append a recording', async () => {
  const tree = await calendar([{ ...event('imported'), note: { id: 'imported', state: 'done', source: 'audio-file' } }])
  expect(label(tree)).toContain('Open note')
  expect(find(tree, 'start-recording').props.title).toBe('Open Planning imported')
  expect(label(tree)).not.toContain('Resume')
})

it('opens the upload flow with the current destination callback without starting a recording', async () => {
  const dispatchEvent = vi.fn()
  const file = { token: 'sample', name: 'Planning.mp3', title: 'Planning', modifiedAt: now, size: 1000 }
  const pick = vi.fn(async () => file)
  Object.assign(window.yogurt, { audioUpload: { pick } })
  Object.assign(document, { dispatchEvent })
  const tree = await calendar([])
  await click(find(tree, 'recording-upload'))
  expect(start).not.toHaveBeenCalled()
  expect(pick).toHaveBeenCalledOnce()
  expect(dispatchEvent).toHaveBeenCalledOnce()
  expect(dispatchEvent.mock.calls[0][0]).toMatchObject({ type: 'audio-upload-open', detail: { file, onCreated: uploaded } })
})