Skip to content

The open-source AI receptionist for businesses. Answers every call, books the appointment and hands over to a person when needed. You own the code and the data.

5.7 KB
Raw Blame
// Settings › Assistant › Transfers: each contact's "Announce the caller first" switch. On (the default), the consent
// hint about the AI-voice briefing shows; off, no hint, only what changes, and the row says the call goes straight through.
import { renderToStaticMarkup } from 'react-dom/server'
import { expect, test, vi } from 'vitest'
import type { TransferDraft } from './assistantDraft.ts'

vi.mock('./BusinessHoursDialog.tsx', () => ({ EditBusinessHours: () => null }))
vi.mock('../ui/PhoneInput.tsx', () => ({ PhoneInput: () => null }))

const { ContactRow } = await import('./TransferContacts.tsx')

const contact = (over: Partial<TransferDraft> = {}): TransferDraft => ({
  id: 'tc_1',
  name: 'Mike',
  number: '+1 503 555 0199',
  condition: 'The caller asks for a person',
  hours_only: false,
  ring_seconds: 25,
  if_no_answer: 'message',
  enabled: true,
  briefing: true,
  ...over,
})

const noop = () => {}
const render = (c: TransferDraft, open = true, isNew = false, more: { added?: boolean; numberLeft?: boolean } = {}) =>
  renderToStaticMarkup(
    <ContactRow
      {...more}
      contact={c}
      isNew={isNew}
      index={0}
      count={1}
      open={open}
      problem={null}
      hoursOn={false}
      numberTip=""
      defaultCountry="US"
      onToggle={noop}
      onClose={noop}
      onLeave={noop}
      onChange={noop}
      onMove={noop}
      onRemove={noop}
    />,
  )

const CONSENT = 'Only add someone who agreed to get transferred calls. Each one starts with a short AI-voice briefing.'

test('briefing on: the switch is checked and the consent hint shows', () => {
  const html = render(contact())
  expect(html).toContain('Announce the caller first')
  expect(html).toMatch(/<input[^>]*type="checkbox"[^>]*checked=""[^>]*>[\s\S]*Announce the caller first/)
  expect(html).toContain(CONSENT)
  expect(html).not.toContain('put straight through')
})

test('briefing off: no consent hint, the row says what changes and that the call goes straight through', () => {
  const html = render(contact({ briefing: false }))
  expect(html).not.toContain(CONSENT)
  expect(html).toContain('Calls are put straight through when they pick up.')
  expect(html).toContain(', put straight through')
  // Closed, the row's summary still says it.
  expect(render(contact({ briefing: false }), false)).toContain('Rings 25s, then a message, put straight through')
})

test('a contact just added: Close on its row, Remove under its fields, no status yet, an example placeholder', () => {
  const blank = contact({ name: '', number: '', condition: '' })
  const html = render(blank, true, true)
  expect(html).toContain('<span>Close</span>')
  expect(html).toContain('>Remove</span>')
  expect(html).not.toContain('Move up')
  expect(html).not.toContain(NO_NUMBER)
  expect(html).toContain('placeholder="e.g. Office"')
  // A contact already in the list says why it is not used.
  expect(render(blank, true, false)).toContain(NO_NUMBER)
  // Closed: Change opens it, like every line of settings.
  expect(render(contact(), false)).toContain('<span>Change</span>')
})

const NO_NUMBER = 'Add a number, or turn this contact off.'

test('switched on without a number: the closed row offers to add it in place, the open one a quiet note', () => {
  const missing = contact({ number: '' })
  // Open: under the number field, in the field-note look (not the red field message).
  const open = render(missing)
  expect(open).toContain(`<p class="k-field-desc tc-note">${NO_NUMBER}</p>`)
  expect(open).not.toContain('k-field-msg')
  expect(open.split(NO_NUMBER)).toHaveLength(2)
  // Closed: "Add a number" in the title line instead of "No number" and a note; no status chip, no move arrows alone.
  const closed = render(missing, false)
  expect(closed).toContain('<button type="button" class="k-link">Add a number</button>')
  expect(closed).not.toContain(NO_NUMBER)
  expect(closed).not.toContain('No number')
  expect(closed).not.toContain('Move up')
  // The owner's contact for the owner themself: their name, "You" and "Add your mobile number".
  const mine = renderToStaticMarkup(
    <ContactRow
      contact={missing}
      who={{ name: 'Mike Brennan', you: true }}
      index={0}
      count={1}
      open={false}
      problem={null}
      hoursOn={false}
      numberTip=""
      defaultCountry="US"
      onToggle={noop}
      onClose={noop}
      onLeave={noop}
      onChange={noop}
      onMove={noop}
      onRemove={noop}
    />,
  )
  expect(mine).toContain('Mike Brennan<span class="k-setrow-aside">You</span>')
  expect(mine).toContain('Add your mobile number')
  // Switched off: the row greyed out (its switch says it), no note.
  const off = render(contact({ number: '', enabled: false }), false)
  expect(off).toContain('is-dimmed')
  expect(off).not.toContain(NO_NUMBER)
  // Added during this visit: only once the number field was left.
  expect(render(missing, true, false, { added: true })).not.toContain(NO_NUMBER)
  expect(render(missing, true, false, { added: true, numberLeft: true })).toContain(NO_NUMBER)
})

test("the owner's contact: its Name field says the owner's name, as its row does", () => {
  const owner = contact({ id: 'owner', name: 'Owner' })
  const html = renderToStaticMarkup(
    <ContactRow
      contact={owner}
      who={{ name: 'Mike Brennan', you: false }}
      index={0}
      count={1}
      open
      problem={null}
      hoursOn={false}
      numberTip=""
      defaultCountry="US"
      onToggle={noop}
      onClose={noop}
      onLeave={noop}
      onChange={noop}
      onMove={noop}
      onRemove={noop}
    />,
  )
  expect(html).toContain('<b class="k-setrow-title">Mike Brennan')
  expect(html).toMatch(/id="tc-0-name"[^>]*value="Mike Brennan"/)
  expect(html).not.toContain('value="Owner"')
})