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.4 KB
Raw Blame
// Server admin › Partners: what waits (partners to approve or with a brand to review, one row each) and when a partner row
// offers its AI keys switch.
import { expect, test } from 'vitest'
import {
  offersKeysSwitch,
  PARTNER_STATUS_ORDER,
  waitingBrands,
  waitingCount,
  waitingPartners,
} from './partnersModel.ts'

const partner = (id: string, status: 'pending' | 'approved' | 'suspended') => ({ id, status })
const brand = (partnerId: string, state: 'none' | 'pending' | 'approved') => ({ partnerId, state })

test('a partner waits while it is to approve or its brand is to review, one row even when both wait', () => {
  const partners = [
    partner('a', 'pending'),
    partner('b', 'approved'),
    partner('c', 'suspended'),
    partner('d', 'approved'),
  ]
  // Never approved and changed since approval both wait; an approved brand does not.
  expect(waitingBrands([brand('a', 'none'), brand('b', 'approved'), brand('d', 'pending')])).toEqual([
    brand('a', 'none'),
    brand('d', 'pending'),
  ])
  const brands = [brand('a', 'pending'), brand('b', 'approved'), brand('d', 'pending')]
  expect(waitingPartners(partners, brands).map(p => p.id)).toEqual(['a', 'd'])
  expect(waitingCount(partners, brands)).toBe(2)
  // A brand alone puts an approved partner on the list (B2).
  expect(waitingCount([partner('b', 'approved')], [brand('b', 'none')])).toBe(1)
  expect(waitingCount([], [])).toBe(0)
})

test('Turn off AI keys only for a partner with a bill and keys of its own, Turn on after it was turned off', () => {
  const at = new Date().toISOString()
  expect(offersKeysSwitch({ billingAccountId: 'bill', ownKeys: 0, ownKeysDisabled: null })).toBe(false)
  expect(offersKeysSwitch({ billingAccountId: 'bill', ownKeys: 2, ownKeysDisabled: null })).toBe(true)
  // Turned off, then the partner removed its keys: the way back stays.
  expect(offersKeysSwitch({ billingAccountId: 'bill', ownKeys: 0, ownKeysDisabled: { reason: 'x', at } })).toBe(true)
  // No bill: no own keys at all.
  expect(offersKeysSwitch({ billingAccountId: null, ownKeys: 3, ownKeysDisabled: null })).toBe(false)
})

test('the Status column sorts the partners to approve first, then the suspended, then the approved', () => {
  const statuses = (['approved', 'pending', 'suspended'] as const).slice()
  expect(statuses.sort((a, b) => PARTNER_STATUS_ORDER[a] - PARTNER_STATUS_ORDER[b])).toEqual([
    'pending',
    'suspended',
    'approved',
  ])
})