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.6 KB
Raw Blame
// `?through=1`: a bucket recording passed on by the API (same origin), for the call page's waveform.
import { expect, test, vi } from 'vitest'

// The module loads db.ts, which needs a URL; this test never queries the database.
process.env.DATABASE_URL ||= 'postgres://unused@localhost:1/unused'
const { passThrough } = await import('./recording-route.ts')

const bucket = (status: number, headers: Record<string, string>, body = 'OggS') =>
  vi.fn(async () => new Response(body, { status, headers })) as unknown as typeof fetch

test('passes the bucket answer on with the headers a player needs, and the range asked for', async () => {
  const doFetch = bucket(206, {
    'content-type': 'audio/ogg',
    'content-range': 'bytes 0-3/10',
    'x-amz-id': 'secret-ish',
  })
  const answer = await passThrough('https://bucket.example/rec.ogg?sig=1', 'bytes=0-3', doFetch)
  expect(doFetch).toHaveBeenCalledWith(
    'https://bucket.example/rec.ogg?sig=1',
    expect.objectContaining({ headers: { range: 'bytes=0-3' }, redirect: 'error' }),
  )
  expect(answer.status).toBe(206)
  expect(answer.headers.get('content-type')).toBe('audio/ogg')
  expect(answer.headers.get('content-range')).toBe('bytes 0-3/10')
  expect(answer.headers.get('x-amz-id')).toBeNull()
  expect(answer.headers.get('cache-control')).toBe('private, no-store')
  expect(answer.headers.get('x-content-type-options')).toBe('nosniff')
  expect(await answer.text()).toBe('OggS')
})

test('a refusal of the bucket is a 502, never its body', async () => {
  const answer = await passThrough(
    'https://bucket.example/rec.ogg',
    undefined,
    bucket(403, {}, '<Error>AccessDenied</Error>'),
  )
  expect(answer.status).toBe(502)
  expect(await answer.text()).not.toContain('AccessDenied')
})

test('an object that is not audio is served as plain bytes, never as a page of the app', async () => {
  const answer = await passThrough(
    'https://bucket.example/rec.ogg',
    undefined,
    bucket(200, { 'content-type': 'text/html; charset=utf-8' }, '<script>'),
  )
  expect(answer.headers.get('content-type')).toBe('application/octet-stream')
  expect(answer.headers.get('x-content-type-options')).toBe('nosniff')
})

test('a link-local or metadata address is never fetched, and a failed fetch is a 502', async () => {
  const answer = await passThrough('http://169.254.169.254/latest/meta-data/', undefined)
  expect(answer.status).toBe(502)
  const failing = vi.fn(async () => {
    throw new TypeError('fetch failed')
  }) as unknown as typeof fetch
  expect((await passThrough('https://bucket.example/rec.ogg', undefined, failing)).status).toBe(502)
})