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.

2.8 KB
Raw Blame
import { afterEach, beforeEach, expect, test, vi } from 'vitest'
import {
  OPENING_MAX_MS,
  OPENING_PAUSE_MS,
  OPENING_SILENCE_MS,
  type OpeningReason,
  openingGate,
} from './outbound-opening.ts'

beforeEach(() => vi.useFakeTimers())
afterEach(() => vi.useRealTimers())

/** The gate and what it resolved with so far (null while it waits). */
function gate() {
  const g = openingGate()
  const state: { reason: OpeningReason | null } = { reason: null }
  void g.ready.then(reason => (state.reason = reason))
  return { g, state }
}
const advance = async (ms: number) => vi.advanceTimersByTimeAsync(ms)

test('nobody speaks after the pickup: the first sentence starts after the silence wait', async () => {
  const { state } = gate()
  await advance(OPENING_SILENCE_MS - 1)
  expect(state.reason).toBeNull()
  await advance(1)
  expect(state.reason).toBe('silence')
})

test('the person greets: the first sentence waits for the end of their turn, which gets no reply of its own', async () => {
  const { g, state } = gate()
  await advance(300)
  g.speaking(true)
  g.heard()
  await advance(OPENING_SILENCE_MS) // still talking past the silence wait
  expect(state.reason).toBeNull()
  expect(g.takeTurn()).toBe(true)
  await advance(0)
  expect(state.reason).toBe('turn')
  // Later turns are answered as usual.
  expect(g.takeTurn()).toBe(false)
})

test('the person greets and goes quiet without an end of turn: the first sentence starts after the pause', async () => {
  const { g, state } = gate()
  g.speaking(true)
  await advance(1500)
  g.speaking(false)
  await advance(OPENING_PAUSE_MS - 1)
  expect(state.reason).toBeNull()
  // A late transcript of the same words starts the pause again.
  g.heard()
  await advance(OPENING_PAUSE_MS - 1)
  expect(state.reason).toBeNull()
  await advance(1)
  expect(state.reason).toBe('pause')
  expect(g.takeTurn()).toBe(false)
})

test('speaking again within the pause keeps the first sentence waiting', async () => {
  const { g, state } = gate()
  g.speaking(true)
  g.speaking(false)
  await advance(OPENING_PAUSE_MS - 100)
  g.speaking(true)
  await advance(OPENING_PAUSE_MS)
  expect(state.reason).toBeNull()
})

test('a transcript without a speaking state counts as speech too', async () => {
  const { g, state } = gate()
  await advance(500)
  g.heard()
  await advance(OPENING_SILENCE_MS)
  expect(state.reason).toBe('pause')
})

test('the person keeps talking: the first sentence starts at the longest wait', async () => {
  const { g, state } = gate()
  g.speaking(true)
  await advance(OPENING_MAX_MS - 1)
  expect(state.reason).toBeNull()
  await advance(1)
  expect(state.reason).toBe('longest')
})

test('closed (the call ended): nothing starts, and no turn is held', async () => {
  const { g, state } = gate()
  g.close()
  await advance(OPENING_MAX_MS)
  expect(state.reason).toBeNull()
  expect(g.takeTurn()).toBe(false)
})