Skip to content

The micro-VM for AI agents — light enough to embed on your laptop, elastic enough to power an agentic cloud.

5.0 KB
Raw Blame
import { describe, expect, it } from 'vitest'
import { OrganizationPlan } from '@/billing-api'
import { cycleFacts } from './ThisCycleCard'

// Noon-UTC instants so the local calendar day matches UTC in any test zone.
const plan: OrganizationPlan = {
  planId: 'pro',
  planName: 'Pro',
  status: 'active',
  cycleFrom: new Date('2026-08-05T12:00:00.000Z'),
  cycleTo: new Date('2026-09-05T12:00:00.000Z'),
  includedQuotaCents: 25000,
  quotaConsumedCents: 6250,
  quotaRemainingCents: 18750,
}
const now = new Date('2026-08-14T12:00:00.000Z')
const catalog = [
  {
    id: 'starter',
    name: 'Starter',
    priceMonthlyCents: 1_900,
    includedQuotaCents: 3_000,
    concurrencyLimit: 20,
    selfServe: true,
  },
]

describe('cycleFacts', () => {
  it('counts the days to the roll and states the window', () => {
    const facts = cycleFacts(plan, now)
    expect(facts).toMatchObject({ unlimited: false, daysLeft: 22, note: null })
    expect(facts.window).toBe('Aug 5 – Sep 5')
  })

  it('trial countdown uses the trial window while conversion has not been attempted', () => {
    const facts = cycleFacts(
      {
        ...plan,
        trial: {
          startedAt: new Date('2026-08-05T12:00:00.000Z'),
          endsAt: new Date('2026-08-17T12:00:00.000Z'),
          conversionAmountCents: 14900,
          currency: 'USD',
          conversionState: 'not_attempted',
        },
      },
      now,
    )
    expect(facts).toMatchObject({
      daysLeft: 3,
      rollDay: 'Aug 17',
      window: 'Aug 5 – Aug 17',
      trialActive: true,
    })
  })

  it('trial countdown keeps the trial window while conversion is pending', () => {
    const facts = cycleFacts(
      {
        ...plan,
        cycleTo: new Date('2026-08-17T12:00:00.000Z'),
        trial: {
          startedAt: new Date('2026-08-05T12:00:00.000Z'),
          endsAt: new Date('2026-08-17T12:00:00.000Z'),
          conversionAmountCents: 14900,
          currency: 'USD',
          conversionState: 'pending',
        },
      },
      new Date('2026-08-17T12:00:00.000Z'),
    )
    expect(facts).toMatchObject({
      daysLeft: 0,
      rollDay: 'Aug 17',
      window: 'Aug 5 – Aug 17',
      trialActive: false,
    })
  })

  it('trial countdown uses the reanchored paid cycle after conversion succeeds', () => {
    const facts = cycleFacts(
      {
        ...plan,
        cycleFrom: new Date('2026-08-17T12:00:00.000Z'),
        cycleTo: new Date('2026-09-17T12:00:00.000Z'),
        trial: {
          startedAt: new Date('2026-08-05T12:00:00.000Z'),
          endsAt: new Date('2026-08-17T12:00:00.000Z'),
          conversionAmountCents: 14900,
          currency: 'USD',
          conversionState: 'succeeded',
        },
      },
      new Date('2026-08-20T12:00:00.000Z'),
    )
    expect(facts).toMatchObject({
      daysLeft: 28,
      rollDay: 'Sep 17',
      window: 'Aug 17 – Sep 17',
      trialActive: false,
    })
  })

  it('never counts below zero after the cycle lapsed', () => {
    expect(cycleFacts(plan, new Date('2026-09-20T12:00:00.000Z')).daysLeft).toBe(0)
  })

  it('reads a null grant as unlimited — a negotiated deal, not zero quota', () => {
    const custom = { ...plan, includedQuotaCents: null, quotaRemainingCents: null }
    expect(cycleFacts(custom, now).unlimited).toBe(true)
  })

  it('notes a cancellation with the date the quota survives to', () => {
    const facts = cycleFacts({ ...plan, status: 'canceled' }, now)
    expect(facts.note).toBe('Canceled — quota stays usable until Sep 5, then pay-as-you-go from the wallet')
  })

  it('notes a queued downgrade with the roll date, cancellation taking precedence', () => {
    expect(cycleFacts({ ...plan, pendingPlanId: 'starter' }, now, catalog).note).toBe(
      'Downgrades to Starter when the cycle rolls on Sep 5',
    )
    expect(cycleFacts({ ...plan, status: 'canceled', pendingPlanId: 'starter' }, now, catalog).note).toMatch(
      /^Canceled/,
    )
  })

  // The Overview panel states the subscription is ending; this tab must not
  // simultaneously promise the queued downgrade happens at the same roll.
  it('lets a cancellation outrank the downgrade queued behind it', () => {
    expect(cycleFacts({ ...plan, pendingPlanId: 'starter', cancelAtPeriodEnd: true }, now, catalog).note).toBe(
      'Ends Sep 5 — quota stays usable until the roll, then pay-as-you-go from the wallet',
    )
  })

  // New copy: this state used to produce no note at all.
  it('states a scheduled cancellation even with no downgrade queued behind it', () => {
    expect(cycleFacts({ ...plan, cancelAtPeriodEnd: true }, now, catalog).note).toBe(
      'Ends Sep 5 — quota stays usable until the roll, then pay-as-you-go from the wallet',
    )
  })

  // A negotiated plan never appears in the public catalog, so there is no name
  // to resolve — but the note still has to say something.
  it('falls back to the id only when the catalog cannot name the queued plan', () => {
    expect(cycleFacts({ ...plan, pendingPlanId: 'negotiated' }, now, catalog).note).toBe(
      'Downgrades to negotiated when the cycle rolls on Sep 5',
    )
  })
})