123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506 |
- import {OrganizationFixture} from 'sentry-fixture/organization';
- import {
- InvoicedSubscriptionFixture,
- SubscriptionFixture,
- } from 'getsentry-test/fixtures/subscription';
- import {OnDemandBudgetMode, type OnDemandBudgets} from 'getsentry/types';
- import {
- exceedsInvoicedBudgetLimit,
- getTotalBudget,
- parseOnDemandBudgetsFromSubscription,
- } from 'getsentry/views/onDemandBudgets/utils';
- describe('parseOnDemandBudgetsFromSubscription', function () {
- it('returns per-category budget for non-AM plans - with on-demand budget', function () {
- const organization = OrganizationFixture();
- const subscription = SubscriptionFixture({
- organization,
- plan: 'mm2_f',
- onDemandMaxSpend: 123,
- });
- const ondemandBudgets = parseOnDemandBudgetsFromSubscription(subscription);
- expect(ondemandBudgets).toEqual({
- budgetMode: OnDemandBudgetMode.SHARED,
- sharedMaxBudget: 123,
- });
- });
- it('returns shared on-demand budget for non-AM plans - without on-demand budget', function () {
- const organization = OrganizationFixture();
- let subscription = SubscriptionFixture({
- organization,
- plan: 'mm2_f',
- onDemandMaxSpend: 0,
- });
- let ondemandBudgets = parseOnDemandBudgetsFromSubscription(subscription);
- expect(ondemandBudgets).toEqual({
- budgetMode: OnDemandBudgetMode.SHARED,
- sharedMaxBudget: 0,
- });
- // omitted onDemandMaxSpend
- subscription = SubscriptionFixture({organization, plan: 'mm2_f'});
- ondemandBudgets = parseOnDemandBudgetsFromSubscription(subscription);
- expect(ondemandBudgets).toEqual({
- budgetMode: OnDemandBudgetMode.SHARED,
- sharedMaxBudget: 0,
- });
- });
- it('returns shared on-demand budget for AM plans', function () {
- const organization = OrganizationFixture();
- const subscription = SubscriptionFixture({
- organization,
- plan: 'am1_business',
- planTier: 'am1',
- reservedEvents: 200000,
- reservedTransactions: 250000,
- reservedAttachments: 25,
- onDemandMaxSpend: 123,
- onDemandBudgets: {
- enabled: true,
- budgetMode: OnDemandBudgetMode.SHARED,
- sharedMaxBudget: 123,
- onDemandSpendUsed: 0,
- },
- });
- const ondemandBudgets = parseOnDemandBudgetsFromSubscription(subscription);
- expect(ondemandBudgets).toEqual({
- budgetMode: OnDemandBudgetMode.SHARED,
- sharedMaxBudget: 123,
- });
- });
- it('returns shared on-demand budget for AM plans - without on-demand budget', function () {
- const organization = OrganizationFixture();
- let subscription = SubscriptionFixture({
- organization,
- plan: 'am1_business',
- planTier: 'am1',
- reservedEvents: 200000,
- reservedTransactions: 250000,
- reservedAttachments: 25,
- onDemandMaxSpend: 0,
- onDemandBudgets: {
- enabled: false,
- budgetMode: OnDemandBudgetMode.SHARED,
- sharedMaxBudget: 0,
- onDemandSpendUsed: 0,
- },
- });
- let ondemandBudgets = parseOnDemandBudgetsFromSubscription(subscription);
- expect(ondemandBudgets).toEqual({
- budgetMode: OnDemandBudgetMode.SHARED,
- sharedMaxBudget: 0,
- });
- // missing onDemandBudgets
- subscription = SubscriptionFixture({
- organization,
- plan: 'am1_business',
- planTier: 'am1',
- reservedEvents: 200000,
- reservedTransactions: 250000,
- reservedAttachments: 25,
- onDemandMaxSpend: 0,
- });
- ondemandBudgets = parseOnDemandBudgetsFromSubscription(subscription);
- expect(ondemandBudgets).toEqual({
- budgetMode: OnDemandBudgetMode.SHARED,
- sharedMaxBudget: 0,
- });
- // missing onDemandBudgets and onDemandMaxSpend
- subscription = SubscriptionFixture({
- organization,
- plan: 'am1_business',
- planTier: 'am1',
- reservedEvents: 200000,
- reservedTransactions: 250000,
- reservedAttachments: 25,
- });
- ondemandBudgets = parseOnDemandBudgetsFromSubscription(subscription);
- expect(ondemandBudgets).toEqual({
- budgetMode: OnDemandBudgetMode.SHARED,
- sharedMaxBudget: 0,
- });
- });
- it('returns per-category on-demand budget for AM plans', function () {
- const organization = OrganizationFixture();
- const subscription = SubscriptionFixture({
- organization,
- plan: 'am1_business',
- planTier: 'am1',
- reservedEvents: 200000,
- reservedTransactions: 250000,
- reservedAttachments: 25,
- onDemandMaxSpend: 100 + 200 + 300,
- onDemandBudgets: {
- enabled: true,
- budgetMode: OnDemandBudgetMode.PER_CATEGORY,
- errorsBudget: 100,
- transactionsBudget: 200,
- attachmentsBudget: 300,
- monitorSeatsBudget: 400,
- replaysBudget: 0,
- budgets: {
- errors: 100,
- transactions: 200,
- attachments: 300,
- replays: 0,
- monitorSeats: 400,
- uptime: 500,
- },
- attachmentSpendUsed: 0,
- errorSpendUsed: 0,
- transactionSpendUsed: 0,
- usedSpends: {
- errors: 0,
- transactions: 0,
- attachments: 0,
- replays: 0,
- monitorSeats: 0,
- uptime: 0,
- },
- },
- });
- subscription.categories.errors!.reserved = 200000;
- subscription.categories.transactions!.reserved = 250000;
- subscription.categories.attachments!.reserved = 25;
- subscription.categories.monitorSeats!.reserved = 1;
- const ondemandBudgets = parseOnDemandBudgetsFromSubscription(subscription);
- expect(ondemandBudgets).toEqual({
- budgetMode: OnDemandBudgetMode.PER_CATEGORY,
- errorsBudget: 100,
- transactionsBudget: 200,
- attachmentsBudget: 300,
- monitorSeatsBudget: 400,
- uptimeBudget: 500,
- replaysBudget: 0,
- budgets: {
- errors: 100,
- transactions: 200,
- attachments: 300,
- replays: 0,
- monitorSeats: 400,
- uptime: 500,
- },
- });
- });
- it('reconstructs shared on-demand budget if onDemandBudgets is missing', function () {
- const organization = OrganizationFixture();
- const subscription = SubscriptionFixture({
- organization,
- plan: 'am1_business',
- planTier: 'am1',
- reservedEvents: 200000,
- reservedTransactions: 250000,
- reservedAttachments: 25,
- onDemandMaxSpend: 123,
- });
- subscription.categories.errors!.reserved = 200000;
- subscription.categories.transactions!.reserved = 250000;
- subscription.categories.attachments!.reserved = 25;
- const ondemandBudgets = parseOnDemandBudgetsFromSubscription(subscription);
- expect(ondemandBudgets).toEqual({
- budgetMode: OnDemandBudgetMode.SHARED,
- sharedMaxBudget: 123,
- });
- });
- });
- describe('getTotalBudget', function () {
- it('returns total on-demand budget for non-AM plans - with on-demand budget', function () {
- const organization = OrganizationFixture();
- const subscription = SubscriptionFixture({
- organization,
- plan: 'mm2_f',
- onDemandMaxSpend: 123,
- });
- const actualTotalBudget = getTotalBudget(
- parseOnDemandBudgetsFromSubscription(subscription)
- );
- expect(actualTotalBudget).toBe(123);
- });
- it('returns total on-demand budget for non-AM plans - without on-demand budget', function () {
- const organization = OrganizationFixture();
- let subscription = SubscriptionFixture({
- organization,
- plan: 'mm2_f',
- onDemandMaxSpend: 0,
- });
- let actualTotalBudget = getTotalBudget(
- parseOnDemandBudgetsFromSubscription(subscription)
- );
- expect(actualTotalBudget).toBe(0);
- // omitted onDemandMaxSpend
- subscription = SubscriptionFixture({organization, plan: 'mm2_f'});
- actualTotalBudget = getTotalBudget(
- parseOnDemandBudgetsFromSubscription(subscription)
- );
- expect(actualTotalBudget).toBe(0);
- });
- it('returns total budget of shared on-demand budget for AM plans', function () {
- const organization = OrganizationFixture();
- const subscription = SubscriptionFixture({
- organization,
- plan: 'am1_business',
- planTier: 'am1',
- reservedEvents: 200000,
- reservedTransactions: 250000,
- reservedAttachments: 25,
- onDemandMaxSpend: 100 + 200 + 300,
- onDemandBudgets: {
- enabled: true,
- budgetMode: OnDemandBudgetMode.SHARED,
- sharedMaxBudget: 123,
- onDemandSpendUsed: 0,
- },
- });
- const actualTotalBudget = getTotalBudget(
- parseOnDemandBudgetsFromSubscription(subscription)
- );
- expect(actualTotalBudget).toBe(123);
- });
- it('returns total budget of shared on-demand budget for AM plans - without on-demand budget', function () {
- const organization = OrganizationFixture();
- let subscription = SubscriptionFixture({
- organization,
- plan: 'am1_business',
- planTier: 'am1',
- reservedEvents: 200000,
- reservedTransactions: 250000,
- reservedAttachments: 25,
- onDemandMaxSpend: 0,
- onDemandBudgets: {
- enabled: false,
- budgetMode: OnDemandBudgetMode.SHARED,
- sharedMaxBudget: 0,
- onDemandSpendUsed: 0,
- },
- });
- let actualTotalBudget = getTotalBudget(
- parseOnDemandBudgetsFromSubscription(subscription)
- );
- expect(actualTotalBudget).toBe(0);
- // missing onDemandBudgets
- subscription = SubscriptionFixture({
- organization,
- plan: 'am1_business',
- planTier: 'am1',
- reservedEvents: 200000,
- reservedTransactions: 250000,
- reservedAttachments: 25,
- onDemandMaxSpend: 0,
- });
- actualTotalBudget = getTotalBudget(
- parseOnDemandBudgetsFromSubscription(subscription)
- );
- expect(actualTotalBudget).toBe(0);
- // missing onDemandBudgets and onDemandMaxSpend
- subscription = SubscriptionFixture({
- organization,
- plan: 'am1_business',
- planTier: 'am1',
- reservedEvents: 200000,
- reservedTransactions: 250000,
- reservedAttachments: 25,
- });
- actualTotalBudget = getTotalBudget(
- parseOnDemandBudgetsFromSubscription(subscription)
- );
- expect(actualTotalBudget).toBe(0);
- });
- it('returns total budget of per-category on-demand budget for AM plans', function () {
- const organization = OrganizationFixture();
- const subscription = SubscriptionFixture({
- organization,
- plan: 'am1_business',
- planTier: 'am1',
- reservedEvents: 200000,
- reservedTransactions: 250000,
- reservedAttachments: 25,
- onDemandMaxSpend: 100 + 200 + 300,
- onDemandBudgets: {
- enabled: true,
- budgetMode: OnDemandBudgetMode.PER_CATEGORY,
- errorsBudget: 100,
- transactionsBudget: 200,
- attachmentsBudget: 300,
- replaysBudget: 0,
- budgets: {errors: 100, transactions: 200, attachments: 300, uptime: 400},
- attachmentSpendUsed: 0,
- errorSpendUsed: 0,
- transactionSpendUsed: 0,
- usedSpends: {errors: 0, transactions: 0, attachments: 0, replays: 0},
- },
- });
- subscription.categories.errors!.reserved = 200000;
- subscription.categories.transactions!.reserved = 250000;
- subscription.categories.attachments!.reserved = 25;
- const actualTotalBudget = getTotalBudget(
- parseOnDemandBudgetsFromSubscription(subscription)
- );
- expect(actualTotalBudget).toEqual(100 + 200 + 300 + 400);
- });
- it('returns total on-demand budget if onDemandBudgets is missing', function () {
- const organization = OrganizationFixture();
- const subscription = SubscriptionFixture({
- organization,
- plan: 'am1_business',
- planTier: 'am1',
- reservedEvents: 200000,
- reservedTransactions: 250000,
- reservedAttachments: 25,
- onDemandMaxSpend: 123,
- });
- const actualTotalBudget = getTotalBudget(
- parseOnDemandBudgetsFromSubscription(subscription)
- );
- expect(actualTotalBudget).toBe(123);
- });
- });
- describe('exceedsInvoicedBudgetLimit', function () {
- it('returns false for non-invoiced subscription', function () {
- const organization = OrganizationFixture();
- const subscription = SubscriptionFixture({organization});
- const ondemandBudget: OnDemandBudgets = {
- budgetMode: OnDemandBudgetMode.SHARED,
- sharedMaxBudget: 3_000_000,
- };
- expect(exceedsInvoicedBudgetLimit(subscription, ondemandBudget)).toBe(false);
- });
- it('returns false for invoiced subscriptions without flag', function () {
- const organization = OrganizationFixture();
- const subscription = InvoicedSubscriptionFixture({organization});
- const ondemandBudget: OnDemandBudgets = {
- budgetMode: OnDemandBudgetMode.SHARED,
- sharedMaxBudget: 0,
- };
- expect(exceedsInvoicedBudgetLimit(subscription, ondemandBudget)).toBe(false);
- });
- it('returns false for invoiced subscriptions with budget and with onDemandInvoiced flag', function () {
- // no limit for CC-invoiced on-demand
- const organization = OrganizationFixture();
- const subscription = InvoicedSubscriptionFixture({
- organization,
- onDemandInvoiced: true,
- supportsOnDemand: true,
- });
- const ondemandBudget: OnDemandBudgets = {
- budgetMode: OnDemandBudgetMode.SHARED,
- sharedMaxBudget: 1000,
- };
- expect(exceedsInvoicedBudgetLimit(subscription, ondemandBudget)).toBe(false);
- });
- it('returns true for invoiced subscriptions with budget and without any flags', function () {
- // if an invoiced customer is somehow setting OD budget without either onDemandInvoicedManual or onDemandInvoiced, always stop them
- const organization = OrganizationFixture();
- const subscription = InvoicedSubscriptionFixture({organization});
- const ondemandBudget: OnDemandBudgets = {
- budgetMode: OnDemandBudgetMode.SHARED,
- sharedMaxBudget: 1000,
- };
- expect(exceedsInvoicedBudgetLimit(subscription, ondemandBudget)).toBe(true);
- });
- it('returns false for invoiced subscriptions with flag and budget lower than or equal to 5x custom price', function () {
- const organization = OrganizationFixture();
- const subscription = InvoicedSubscriptionFixture({
- organization,
- onDemandInvoicedManual: true,
- supportsOnDemand: true,
- customPrice: 12000,
- });
- const ondemandBudget: OnDemandBudgets = {
- budgetMode: OnDemandBudgetMode.SHARED,
- sharedMaxBudget: 5000,
- };
- expect(exceedsInvoicedBudgetLimit(subscription, ondemandBudget)).toBe(false);
- ondemandBudget.sharedMaxBudget = 800;
- expect(exceedsInvoicedBudgetLimit(subscription, ondemandBudget)).toBe(false);
- });
- it('returns false for invoiced subscriptions with flag and budget lower than or equal to 5x acv', function () {
- const organization = OrganizationFixture();
- const subscription = InvoicedSubscriptionFixture({
- organization,
- onDemandInvoicedManual: true,
- supportsOnDemand: true,
- acv: 12000,
- });
- const ondemandBudget: OnDemandBudgets = {
- budgetMode: OnDemandBudgetMode.SHARED,
- sharedMaxBudget: 5000,
- };
- expect(exceedsInvoicedBudgetLimit(subscription, ondemandBudget)).toBe(false);
- ondemandBudget.sharedMaxBudget = 800;
- expect(exceedsInvoicedBudgetLimit(subscription, ondemandBudget)).toBe(false);
- });
- it('returns true for invoiced subscriptions with flag and budget greater than 5x custom price', function () {
- const organization = OrganizationFixture();
- const subscription = InvoicedSubscriptionFixture({
- organization,
- onDemandInvoicedManual: true,
- supportsOnDemand: true,
- customPrice: 12000,
- });
- const ondemandBudget: OnDemandBudgets = {
- budgetMode: OnDemandBudgetMode.SHARED,
- sharedMaxBudget: 5001,
- };
- expect(exceedsInvoicedBudgetLimit(subscription, ondemandBudget)).toBe(true);
- });
- it('returns false for invoiced subscriptions with flag and budget greater than 5x acv', function () {
- const organization = OrganizationFixture();
- const subscription = InvoicedSubscriptionFixture({
- organization,
- onDemandInvoicedManual: true,
- supportsOnDemand: true,
- acv: 12000,
- });
- const ondemandBudget: OnDemandBudgets = {
- budgetMode: OnDemandBudgetMode.SHARED,
- sharedMaxBudget: 5001,
- };
- expect(exceedsInvoicedBudgetLimit(subscription, ondemandBudget)).toBe(true);
- });
- });
|