Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

6.2 KB
Raw Blame
import type { AiUsageSnapshot } from '@service-auth/ai-billing-types';
import { describe, expect, it } from 'vitest';
import { DEFAULT_AUTO_RELOAD } from '../core/usage';
import { toUsageSummary } from './usage-summary';

const snapshot: AiUsageSnapshot = {
  tier: 'free',
  unlimited: false,
  payer: 'macro|[email protected]',
  can_manage_billing: true,
  seats: 1,
  period_start: '2026-10-01T00:00:00Z',
  period_end: '2026-11-01T00:00:00Z',
  included_cents: 500,
  used_cents: 250,
  credits_consumed_cents: 0,
  credit_balance_cents: 2_500,
  overage_enabled: false,
  overage_limit_cents: 0,
  overage_charged_cents: 0,
  overage_suspended: false,
  uncovered_cents: 0,
  remaining_cents: 250,
};

describe('usage billing access', () => {
  it('uses authoritative credit scope for a single-seat team and preserves the older-backend fallback', () => {
    expect(
      toUsageSummary({
        ...snapshot,
        tier: 'max',
        credits_shared_with_team: true,
      })
    ).toMatchObject({ billingAccess: 'payer', creditScope: 'team' });
    expect(
      toUsageSummary({ ...snapshot, seats: 4, credits_shared_with_team: false })
        .creditScope
    ).toBe('personal');
    expect(toUsageSummary({ ...snapshot, seats: 4 }).creditScope).toBe('team');
    expect(toUsageSummary(snapshot).creditScope).toBe('personal');
  });

  it('marks a Free payer as ineligible for credit purchases', () => {
    expect(toUsageSummary(snapshot)).toMatchObject({
      billingAccess: 'free',
      monthlyPercent: 50,
      creditBalanceCents: 2_500,
    });
  });

  it.each(['premium', 'max'] as const)(
    'does not grant billing access to a %s non-payer',
    (tier) => {
      expect(
        toUsageSummary({ ...snapshot, tier, can_manage_billing: false })
          .billingAccess
      ).toBe('team-member');
    }
  );

  it('still displays a Free monthly allowance supplied by the backend', () => {
    expect(
      toUsageSummary({ ...snapshot, included_cents: 1_000, used_cents: 250 })
    ).toMatchObject({
      billingAccess: 'free',
      monthlyPercent: 25,
    });
  });

  it('shows an exhausted Free allowance without granting access to legacy credits', () => {
    expect(
      toUsageSummary({
        ...snapshot,
        used_cents: 500,
        blocked_reason: 'free_allowance_exhausted',
      })
    ).toMatchObject({ billingAccess: 'free', monthlyPercent: 100 });
  });

  it.each(['premium', 'max'] as const)(
    'lets a %s payer purchase credits',
    (tier) => {
      expect(toUsageSummary({ ...snapshot, tier })).toMatchObject({
        billingAccess: 'payer',
        creditScope: 'personal',
      });
      expect(toUsageSummary({ ...snapshot, tier, seats: 4 })).toMatchObject({
        billingAccess: 'payer',
        creditScope: 'team',
      });
    }
  );
});

describe('automatic reload', () => {
  it('decodes authoritative calendar-month budget facts and keeps legacy responses unknown', () => {
    const auto_reload = {
      minimum_balance_cents: 1_000,
      target_balance_cents: 10_000,
      monthly_spend_limit_cents: 5_000,
      suspended: false,
      active: true,
    };
    expect(
      toUsageSummary({ ...snapshot, tier: 'max', auto_reload }).autoReload
        .budget
    ).toBeUndefined();
    expect(
      toUsageSummary({
        ...snapshot,
        tier: 'max',
        auto_reload: {
          ...auto_reload,
          monthly_budget: {
            committed_cents: 4_951,
            resets_at: '2026-11-01T00:00:00Z',
            limit_reached: true,
          },
        },
      }).autoReload.budget
    ).toEqual({
      spentCents: 4_951,
      limitCents: 5_000,
      resetsAt: '2026-11-01T00:00:00Z',
      limitReached: true,
    });
  });

  it('treats usage billing as the opt-in and reads thresholds from the backend', () => {
    expect(
      toUsageSummary({
        ...snapshot,
        tier: 'premium',
        overage_enabled: true,
        overage_limit_cents: 7_500,
        auto_reload: {
          minimum_balance_cents: 500,
          target_balance_cents: 2_000,
          monthly_spend_limit_cents: 7_500,
          suspended: true,
          active: false,
        },
      }).autoReload
    ).toEqual({
      settings: {
        enabled: true,
        minimumBalanceCents: 500,
        targetBalanceCents: 2_000,
        monthlySpendLimitCents: 7_500,
      },
      suspended: true,
    });
  });

  it('starts an off Auto-Reload from the catalog defaults and the plan limit, but keeps an opted-in payer’s settings', () => {
    const catalog = {
      plans: [
        {
          tier: 'premium' as const,
          monthly_price_cents: 4_000,
          included_ai_cents_per_seat: 2_000,
          purchasable: true,
          auto_reload_default_monthly_limit_cents: 20_000,
        },
        {
          tier: 'max' as const,
          monthly_price_cents: 20_000,
          included_ai_cents_per_seat: 10_000,
          purchasable: true,
          auto_reload_default_monthly_limit_cents: 100_000,
        },
      ],
      credit_packs_cents: [1_000],
      overage_limit_min_cents: 500,
      overage_limit_max_cents: 500_000,
      auto_reload_defaults: {
        minimum_balance_cents: 500,
        target_balance_cents: 2_500,
      },
    };
    const stored = {
      minimum_balance_cents: 1_000,
      target_balance_cents: 10_000,
      monthly_spend_limit_cents: null,
      suspended: false,
      active: false,
    };
    expect(
      toUsageSummary({ ...snapshot, tier: 'max', auto_reload: stored }, catalog)
        .autoReload.settings
    ).toEqual({
      enabled: false,
      minimumBalanceCents: 500,
      targetBalanceCents: 2_500,
      monthlySpendLimitCents: 100_000,
    });
    expect(
      toUsageSummary(
        {
          ...snapshot,
          tier: 'premium',
          overage_enabled: true,
          auto_reload: stored,
        },
        catalog
      ).autoReload.settings
    ).toEqual({
      enabled: true,
      minimumBalanceCents: 1_000,
      targetBalanceCents: 10_000,
      monthlySpendLimitCents: null,
    });
  });

  it('falls back to the default thresholds when the snapshot has none', () => {
    expect(toUsageSummary(snapshot).autoReload).toEqual({
      settings: DEFAULT_AUTO_RELOAD,
      suspended: false,
    });
  });
});