Files
supabase/apps/studio/components/interfaces/Organization/Usage/Usage.utils.test.ts
Jordi Enric 090e5bceff feat(studio): filter organization usage by branch FE-4153 (#49257)
## Summary

Customers could see a branch-driven egress overage in their organization
total but could not select the branch on the Usage page. This adds a
branch filter next to the parent project selector and uses the selected
branch's project ref for usage requests.

- Validate bookmarked branch refs before displaying usage. Show loading
and API errors explicitly, and disclose when an unavailable branch falls
back to its parent.
- Keep the filter, notice, and usage requests on the same project ref.
Allow returning to the parent even when the branches response omits its
main row.
- Find the selected branch's disk usage beyond the first page of
organization projects and explain that deleted branches still contribute
to organization totals.
- Correct the branching and egress documentation to explain separate
branch usage.

## Scope

This is the branch-selector improvement proposed in FE-4153's
discussion. It does not provide the original ticket's reconciled
per-project egress table or historical rows for deleted branches. The
current daily-usage API contract supports a single project filter, but
does not return project-grouped usage. Keep the full reconciliation work
open.

## Validation

All 47 focused Vitest tests pass across six files, including nine MSW
page tests covering usage request refs, direct branch links,
pending/failed lookups, deleted-branch fallback, invalid-project
recovery, and denied subscription permissions. Disk tests cover
selection beyond the first 96 projects and pagination failures.
Deterministic shuffled runs with seeds `4153` and `49257` pass with
retries disabled (`--retry=0`). Four focused production mutants were
killed by behavioral assertions; original source bytes were restored and
verified after every probe.

Studio TypeScript, Prettier on all PR paths, and focused lint checks
pass; inherited `any` warnings remain.

Browser QA on the matching `be17d77` Vercel preview passed parent usage
(0.213 GB), searching for `dev-test`, branch selection, return to main,
invalid-project recovery, stale-branch fallback, and returning to
organization totals. The selected branch showed 0 GB, so nonzero branch
overage was not verified. Browser network traffic was not captured;
request-ref coverage comes from MSW.

Three existing auth/router/platform module-mock scaffolds remain as a
test-harness limitation; strict no-module-mock compliance is not
claimed.

All Studio unit-test and E2E shards passed on be17d77801; CI report jobs
are finishing.

Related: FE-4153


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **New Features**
* Filter organization usage by project branch with searchable branch
selection. Usage details reflect the selected branch where available,
including branches found on later project-list pages.
* See whether usage is scoped to a branch, the main branch, or a
project, and switch to organization-wide totals. Billing and quota
totals include all branches, including deleted branches that are no
longer selectable.
* **Documentation**
* Clarified how non-compute usage is recorded per branch, included in
organization totals, and viewed; it isn’t itemized by branch on
invoices.
* Clarified that Preview branch Egress counts toward organization
totals, not the parent project’s usage, and how to view it.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-10-19 11:30:37 +00:00

335 lines
9.2 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import {
dailyUsageToDataPoints,
getUsageBranchOptions,
resolveUsageProjectRef,
} from './Usage.utils'
import { PricingMetric } from '@/data/analytics/org-daily-stats-query'
import type { OrgDailyUsageResponse } from '@/data/analytics/org-daily-stats-query'
import { createTestBranch } from '@/tests/lib/branch-test-utils'
describe('dailyUsageToDataPoints', () => {
it('returns empty array when dailyUsage is undefined', () => {
const result = dailyUsageToDataPoints(undefined, () => true)
expect(result).toEqual([])
})
it('returns empty array when usages array is empty', () => {
const dailyUsage: OrgDailyUsageResponse = {
usages: [],
}
const result = dailyUsageToDataPoints(dailyUsage, () => true)
expect(result).toEqual([])
})
it('transforms single usage entry correctly', () => {
const dailyUsage: OrgDailyUsageResponse = {
usages: [
{
date: '2025-10-01',
metric: PricingMetric.EGRESS,
usage: 15,
usage_original: 15 * 1e9,
breakdown: null,
},
],
}
const result = dailyUsageToDataPoints(dailyUsage, () => true)
expect(result).toHaveLength(1)
expect(result[0]).toEqual({
period_start: '2025-10-01',
periodStartFormatted: '01 Oct',
egress: 15 * 1e9,
})
})
it('groups multiple metrics by date correctly', () => {
const dailyUsage: OrgDailyUsageResponse = {
usages: [
{
date: '2025-10-01',
metric: PricingMetric.EGRESS,
usage: 1000000,
usage_original: 1000000,
breakdown: null,
},
{
date: '2025-10-01',
metric: PricingMetric.DATABASE_SIZE,
usage: 5000000,
usage_original: 5000000,
breakdown: null,
},
],
}
const result = dailyUsageToDataPoints(dailyUsage, () => true)
expect(result).toHaveLength(1)
expect(result[0]).toEqual({
period_start: '2025-10-01',
periodStartFormatted: '01 Oct',
egress: 1000000,
database_size: 5000000,
})
})
it('creates separate data points for different dates', () => {
const dailyUsage: OrgDailyUsageResponse = {
usages: [
{
date: '2025-10-01',
metric: PricingMetric.EGRESS,
usage: 1000000,
usage_original: 1000000,
breakdown: null,
},
{
date: '2025-10-02',
metric: PricingMetric.EGRESS,
usage: 2000000,
usage_original: 2000000,
breakdown: null,
},
],
}
const result = dailyUsageToDataPoints(dailyUsage, () => true)
expect(result).toHaveLength(2)
expect(result[0]).toEqual({
period_start: '2025-10-01',
periodStartFormatted: '01 Oct',
egress: 1000000,
})
expect(result[1]).toEqual({
period_start: '2025-10-02',
periodStartFormatted: '02 Oct',
egress: 2000000,
})
})
it('includes breakdown data when present', () => {
const dailyUsage: OrgDailyUsageResponse = {
usages: [
{
date: '2025-10-01',
metric: PricingMetric.EGRESS,
usage: 1000000,
usage_original: 1000000,
breakdown: {
egress_rest: 400000,
egress_realtime: 300000,
egress_storage: 200000,
egress_supavisor: 100000,
egress_function: 0,
egress_graphql: 0,
egress_logdrain: 0,
},
},
],
}
const result = dailyUsageToDataPoints(dailyUsage, () => true)
expect(result).toHaveLength(1)
expect(result[0]).toEqual({
period_start: '2025-10-01',
periodStartFormatted: '01 Oct',
egress: 1000000,
egress_rest: 400000,
egress_realtime: 300000,
egress_storage: 200000,
egress_supavisor: 100000,
egress_function: 0,
egress_graphql: 0,
egress_logdrain: 0,
})
})
it('handles complex scenario with multiple dates, metrics, and breakdowns', () => {
const dailyUsage: OrgDailyUsageResponse = {
usages: [
{
date: '2025-10-01',
metric: PricingMetric.EGRESS,
usage: 1000000,
usage_original: 1000000,
breakdown: {
egress_rest: 600000,
egress_realtime: 400000,
egress_storage: 0,
egress_supavisor: 0,
egress_function: 0,
egress_graphql: 0,
egress_logdrain: 0,
},
},
{
date: '2025-10-01',
metric: PricingMetric.DATABASE_SIZE,
usage: 5000000,
usage_original: 5000000,
breakdown: null,
},
{
date: '2025-10-02',
metric: PricingMetric.EGRESS,
usage: 1500000,
usage_original: 1500000,
breakdown: {
egress_rest: 900000,
egress_realtime: 600000,
egress_storage: 0,
egress_supavisor: 0,
egress_function: 0,
egress_graphql: 0,
egress_logdrain: 0,
},
},
{
date: '2025-10-02',
metric: PricingMetric.STORAGE_SIZE,
usage: 2000000,
usage_original: 2000000,
breakdown: null,
},
],
}
const result = dailyUsageToDataPoints(dailyUsage, () => true)
expect(result).toHaveLength(2)
expect(result[0]).toEqual({
period_start: '2025-10-01',
periodStartFormatted: '01 Oct',
egress: 1000000,
egress_rest: 600000,
egress_realtime: 400000,
egress_storage: 0,
egress_supavisor: 0,
egress_function: 0,
egress_graphql: 0,
egress_logdrain: 0,
database_size: 5000000,
})
expect(result[1]).toEqual({
period_start: '2025-10-02',
periodStartFormatted: '02 Oct',
egress: 1500000,
egress_rest: 900000,
egress_realtime: 600000,
egress_storage: 0,
egress_supavisor: 0,
egress_function: 0,
egress_graphql: 0,
egress_logdrain: 0,
storage_size: 2000000,
})
})
it('handles zero usage values', () => {
const dailyUsage: OrgDailyUsageResponse = {
usages: [
{
date: '2025-10-01',
metric: PricingMetric.EGRESS,
usage: 0,
usage_original: 0,
breakdown: null,
},
],
}
const result = dailyUsageToDataPoints(dailyUsage, () => true)
expect(result).toHaveLength(1)
expect(result[0]).toEqual({
period_start: '2025-10-01',
periodStartFormatted: '01 Oct',
egress: 0,
})
})
})
describe('getUsageBranchOptions', () => {
it('returns no options when there are no branches', () => {
expect(getUsageBranchOptions(undefined)).toEqual([])
expect(getUsageBranchOptions([])).toEqual([])
})
it('returns no options when the project only has a main branch', () => {
const branches = [createTestBranch({ name: 'main', project_ref: 'parent', is_default: true })]
expect(getUsageBranchOptions(branches)).toEqual([])
})
it('puts the main branch first, then the newest branches', () => {
const branches = [
createTestBranch({
name: 'older',
project_ref: 'older-ref',
created_at: '2026-01-01T00:00:00Z',
}),
createTestBranch({ name: 'main', project_ref: 'parent', is_default: true }),
createTestBranch({
name: 'newer',
project_ref: 'newer-ref',
created_at: '2026-02-01T00:00:00Z',
}),
]
expect(getUsageBranchOptions(branches).map((branch) => branch.name)).toEqual([
'main',
'newer',
'older',
])
})
it('returns branches without a main branch', () => {
const branches = [
createTestBranch({ name: 'one', project_ref: 'one-ref' }),
createTestBranch({ name: 'two', project_ref: 'two-ref' }),
]
expect(getUsageBranchOptions(branches).map((branch) => branch.name)).toEqual(['one', 'two'])
})
it('returns a lone branch that is not the main branch', () => {
const branches = [createTestBranch({ name: 'only', project_ref: 'only-ref' })]
expect(getUsageBranchOptions(branches).map((branch) => branch.name)).toEqual(['only'])
})
})
describe('resolveUsageProjectRef', () => {
const branchOptions = [
createTestBranch({ name: 'main', project_ref: 'parent-ref', is_default: true }),
createTestBranch({ name: 'preview', project_ref: 'branch-ref' }),
]
it('returns null when no project is selected', () => {
expect(resolveUsageProjectRef(null, branchOptions, 'branch-ref')).toBe(null)
})
it('returns the selected branch ref', () => {
expect(resolveUsageProjectRef('parent-ref', branchOptions, 'branch-ref')).toBe('branch-ref')
})
it('falls back to the project when the branch ref is not one of its branches', () => {
expect(resolveUsageProjectRef('parent-ref', branchOptions, 'other-org-branch-ref')).toBe(
'parent-ref'
)
})
it('falls back to the project while its branches are still unknown', () => {
expect(resolveUsageProjectRef('parent-ref', [], 'branch-ref')).toBe('parent-ref')
})
it('rejects a matching branch ref belonging to another parent', () => {
expect(resolveUsageProjectRef('other-parent', branchOptions, 'branch-ref')).toBe('other-parent')
})
})