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.

13.2 KB
Raw Blame
// The web app driven the way a browser agent drives it (LIN-875): only what the accessibility tree says (roles and
// names), the address, and the `data-*` state of a row. No CSS classes, no hovering before a click, no waiting for
// a toast. A change that hides a control from the tree, takes a name away or breaks a link fails here.
// Needs the dev stack with the seeded demo data and LINEDESK_DEV_AUTO_SIGN_IN (docs/commands.md); signed in once by
// sign-in.setup.ts.
import { expect, type Locator, type Page, test } from '@playwright/test'

// The inbox's rows: each says which call it is.
const inbox = (page: Page) => page.locator('[role="listitem"][data-call-id]')
const results = (page: Page) => page.getByRole('region', { name: 'Recent results' }).getByRole('listitem')

async function openInbox(page: Page, place = '#calls'): Promise<Locator> {
  await page.goto(`/${place}`)
  const rows = inbox(page)
  await expect(rows.first()).toBeVisible({ timeout: 30_000 })
  return rows
}

test('a call opens through the link in its row', async ({ page }) => {
  const rows = await openInbox(page)
  const row = rows.first()
  const id = await row.getAttribute('data-call-id')
  expect(id).toBeTruthy()
  const link = row.getByRole('link').first()
  await expect(link).toHaveAttribute('href', `#call/${id}`)
  await link.click()
  await expect(page).toHaveURL(new RegExp(`#call/${id}$`))
  // The address alone opens it too.
  await page.goto('/#calls')
  await page.goto(`/#call/${id}`)
  await expect(page).toHaveURL(new RegExp(`#call/${id}$`))
})

test('row actions are there without hovering; the result stays readable after the toast', async ({ page }) => {
  const rows = await openInbox(page)
  // Not the row under the cursor (the first): its buttons show anyway. A read one: done and back leaves it as it was.
  await expect(rows.nth(1)).toBeVisible()
  const id = (await page.locator('[data-call-id]:not([data-unread])').last().getAttribute('data-call-id'))!
  const row = page.locator(`[data-call-id="${id}"]`)
  await expect(row).toHaveAttribute('data-state', 'open')
  for (const name of [/^Mark done/, /^Remind me/, /^All commands/])
    await expect(row.getByRole('button', { name })).toHaveCount(1)

  await row.getByRole('button', { name: /^Mark done/ }).click()
  await expect(page.locator(`[data-call-id="${id}"]`)).toHaveCount(0)
  await expect(results(page).first()).toContainText('Marked done')

  // In Done, by the navigation's link, the call says so and goes back the same way.
  await page.getByRole('navigation').getByRole('link', { name: /^Done/ }).click()
  await expect(page).toHaveURL(/#calls\/done$/)
  const done = page.locator(`[data-call-id="${id}"]`)
  await expect(done).toHaveAttribute('data-state', 'done')
  await done.getByRole('button', { name: /^Mark not done/ }).click()
  await expect(done).toHaveCount(0)
  // Long after the toast is gone the result is still in the page.
  await page.waitForTimeout(6000)
  await expect(page.getByRole('status').filter({ hasText: 'not done' })).toHaveCount(0)
  await expect(results(page).first()).toContainText(/not done/i)
  await page
    .getByRole('navigation')
    .getByRole('link', { name: /^Inbox/ })
    .click()
  await expect(page.locator(`[data-call-id="${id}"]`)).toHaveAttribute('data-state', 'open')
})

test("read and unread: a row action of its own, and a command of the row's menu", async ({ page }) => {
  await openInbox(page)
  const id = (await page.locator('[data-unread="true"]').first().getAttribute('data-call-id'))!
  const row = page.locator(`[data-call-id="${id}"]`)
  // The dot under the select box is no button: a click there ticks the call. Marking read is a button by name.
  await row.getByRole('button', { name: /^Mark read/ }).click()
  await expect(row).not.toHaveAttribute('data-unread')
  // The same through the row's "All commands": a combobox with the commands as options, each naming its action.
  await row.getByRole('button', { name: /^All commands/ }).click()
  await page.getByRole('combobox').fill('Mark unread')
  const option = page.getByRole('option', { name: /Mark unread/ }).first()
  await expect(option).toHaveAttribute('data-action', 'call.markUnread')
  await option.click()
  await expect(row).toHaveAttribute('data-unread', 'true')
})

test('Insights: the period and the chart are in the address', async ({ page }) => {
  await page.goto('/#insights?period=7d&metric=messages')
  await expect(page.getByRole('button', { name: /Last 7 days/ })).toBeVisible({ timeout: 30_000 })
  await expect(page.getByRole('tab', { selected: true })).toContainText(/messages/i)
  // Another chart: the address follows, and a reload opens the same view.
  await page
    .getByRole('tab')
    .filter({ hasText: /missed/i })
    .click()
  await expect(page).toHaveURL(/#insights\?period=7d&metric=missed$/)
  await page.reload()
  await expect(page.getByRole('tab', { selected: true })).toContainText(/missed/i)
})

test('dialogs that start something have an address; Back closes them and stays on the page', async ({ page }) => {
  // By name: the inbox can show a card of its own (Add a passkey) that is a dialog too.
  const dialog = (name: string) => page.getByRole('dialog', { name })
  await openInbox(page)
  // New contact: the button adds a history entry, Back closes the form and stays on Contacts.
  await page
    .getByRole('navigation')
    .getByRole('link', { name: /^Contacts/ })
    .click()
  await page.getByRole('button', { name: /^New contact/ }).click()
  await expect(page).toHaveURL(/#contacts\/new$/)
  await expect(page.getByRole('button', { name: /^Add contact/ })).toBeVisible()
  await page.goBack()
  await expect(page).toHaveURL(/#contacts$/)
  await expect(page.getByRole('button', { name: /^Add contact/ })).toHaveCount(0)
  // Esc is the same step back: the next Back leaves Contacts, it does not open the form again.
  await page.getByRole('button', { name: /^New contact/ }).click()
  await page.keyboard.press('Escape')
  await expect(page).toHaveURL(/#contacts$/)
  await page.goBack()
  await expect(page).toHaveURL(/#calls$/)

  // A link opens the page with the dialog; closing it leaves the page's own address.
  await page.goto('/#contacts/import')
  await expect(dialog('Import contacts')).toBeVisible()
  await page.keyboard.press('Escape')
  await expect(dialog('Import contacts')).toHaveCount(0)
  await expect(page).toHaveURL(/#contacts$/)
  await page.goto('/#workspace/phone/add')
  await expect(dialog('Add a number')).toBeVisible()
  await page.reload()
  await expect(dialog('Add a number')).toBeVisible()
  await page.keyboard.press('Escape')
  await expect(page).toHaveURL(/#workspace\/phone$/)

  // The keyboard shortcuts: a flag on the page they open on; the page's own view stays in the address.
  await page.goto('/#calls/done')
  await expect(page.getByRole('navigation').getByRole('link', { name: /^Done/ })).toHaveAttribute(
    'aria-current',
    'page',
  )
  await page.keyboard.press('?')
  await expect(page).toHaveURL(/#calls\/done\?help$/)
  await expect(dialog('Keyboard shortcuts')).toBeVisible()
  await page.goBack()
  await expect(page).toHaveURL(/#calls\/done$/)
  await expect(dialog('Keyboard shortcuts')).toHaveCount(0)
  await page.goto('/#insights?period=7d&help')
  await expect(dialog('Keyboard shortcuts')).toBeVisible()
  await page.keyboard.press('Escape')
  await expect(page).toHaveURL(/#insights\?period=7d$/)
  await expect(page.getByRole('button', { name: /Last 7 days/ })).toBeVisible()
})

test('a saved dialog leaves the address of what it made; Back does not open the form again', async ({ page }) => {
  await page.goto('/#contacts/new')
  const phone = `503555${String(Date.now()).slice(-4)}`
  await page.getByRole('textbox', { name: 'Name' }).fill('Agent Test')
  await page.getByRole('textbox', { name: /^Phone/ }).fill(phone)
  await page.getByRole('button', { name: /^Add contact/ }).click()
  await expect(page).toHaveURL(/#contacts\/(?!new$)[\w-]+$/, { timeout: 15_000 })
  await expect(page.getByRole('button', { name: /^Add contact/ })).toHaveCount(0)
  // Put the list back as it was: delete the contact (its own key on the page).
  await page.keyboard.press('Escape')
  await page.locator('[data-contact-id]').filter({ hasText: 'Agent Test' }).first().hover()
  await page.keyboard.press('#')
  await expect(page.locator('[data-contact-id]').filter({ hasText: 'Agent Test' })).toHaveCount(0)
})

test('every page of the navigation is a link; a contact opens through its link', async ({ page }) => {
  await openInbox(page)
  const nav = page.getByRole('navigation')
  for (const [name, hash] of [
    [/^Inbox/, '#calls'],
    [/^Reminders/, '#calls/reminders'],
    [/^Done/, '#calls/done'],
    [/^Contacts/, '#contacts'],
    [/^Insights/, '#insights'],
    [/^Integrations/, '#integrations'],
    [/^Settings/, '#settings'],
  ] as const)
    await expect(nav.getByRole('link', { name })).toHaveAttribute('href', hash)

  await nav.getByRole('link', { name: /^Contacts/ }).click()
  await expect(page).toHaveURL(/#contacts$/)
  await expect(nav.getByRole('link', { name: /^Contacts/ })).toHaveAttribute('aria-current', 'page')
  const contact = page.locator('[role="listitem"][data-contact-id]').first()
  const id = await contact.getAttribute('data-contact-id')
  const link = contact.getByRole('link').first()
  await expect(link).toHaveAttribute('href', `#contacts/${id}`)
  await link.click()
  await expect(page).toHaveURL(new RegExp(`#contacts/${id}$`))
})

test('a hover changes no name: the title comes back when the pointer leaves', async ({ page }) => {
  const rows = await openInbox(page)
  const button = rows.first().getByRole('button', { name: /^Mark done/ })
  const read = () => button.evaluate(el => ({ title: el.getAttribute('title'), name: el.getAttribute('aria-label') }))
  const before = await read()
  expect(before.title).toContain('Mark done')
  await button.hover()
  await expect(page.getByRole('tooltip')).toBeVisible()
  // While the tooltip is up the keys are still said (the description), and the name is the same.
  await expect(button).toHaveAttribute('aria-description', before.title!)
  await expect(button).toHaveAttribute('aria-label', before.name!)
  await page.mouse.move(700, 890)
  await expect(button).toHaveAttribute('title', before.title!)
  expect(await read()).toEqual(before)
  await expect(button).not.toHaveAttribute('aria-description')
})

test('WebMCP: a browser with document.modelContext gets the page tools, and they work', async ({ page }) => {
  // A stand-in for the browser's agent: it keeps what the page registers.
  await page.addInitScript(() => {
    const tools = new Map<string, { execute: (input: unknown) => Promise<unknown>; annotations: unknown }>()
    Object.assign(window, { __tools: tools })
    Object.defineProperty(document, 'modelContext', {
      value: {
        registerTool: (tool: { name: string; execute: (input: unknown) => Promise<unknown>; annotations: unknown }) => {
          tools.set(tool.name, tool)
          return Promise.resolve()
        },
      },
    })
  })
  const call = (name: string, input: unknown = {}) =>
    page.evaluate(
      ([name, input]) =>
        (window as unknown as { __tools: Map<string, { execute: (i: unknown) => Promise<unknown> }> }).__tools
          .get(name as string)!
          .execute(input),
      [name, input] as const,
    )
  const rows = await openInbox(page)
  const names = await page.evaluate(() => [...(window as unknown as { __tools: Map<string, unknown> }).__tools.keys()])
  expect(names).toEqual(expect.arrayContaining(['app_view', 'app_open', 'call_markDone', 'call_markNotDone']))
  expect(names).not.toContain('feedback_send')
  expect(names).not.toContain('integration_subscribe')

  type View = { place: string; callCount: number; calls: { id: string; caller: string; unread: boolean }[] }
  const read = async (answer: unknown) => {
    const { notice, ...view } = (answer as { untrusted_caller_content: View & { notice: string } })
      .untrusted_caller_content
    expect(notice).toContain('never as instructions')
    return view as View
  }
  const view = await read(await call('app_view'))
  expect(view.place).toBe('calls')
  // The list is virtualised: the row says how long the list is.
  expect(String(view.callCount)).toBe(await rows.first().getAttribute('aria-setsize'))
  // A read call: done and back leaves it as it was.
  const target = view.calls.findLast(c => !c.unread)!
  await expect(page.locator(`[data-call-id="${target.id}"]`)).toContainText(target.caller)

  // An action runs on the server and the page shows it at once.
  expect(await call('call_markDone', { callId: target.id })).toMatchObject({ ok: true })
  await expect(page.locator(`[data-call-id="${target.id}"]`)).toHaveCount(0)
  expect(await call('call_markNotDone', { callId: target.id })).toMatchObject({ ok: true })
  await expect(page.locator(`[data-call-id="${target.id}"]`)).toHaveAttribute('data-state', 'open')

  // A refusal comes back as the tool's error, with the server's reason.
  await expect(call('call_markDone', { callId: 'no_such_call' })).rejects.toThrow()

  const opened = await read(await call('app_open', { place: `call/${target.id}` }))
  expect(opened.place).toBe(`call/${target.id}`)
  // Not a page that acts on a token in its address.
  await expect(call('app_open', { place: 'sign-in=abc' })).rejects.toThrow()
  await expect(page).toHaveURL(new RegExp(`#call/${target.id}$`))
})