Files
supabase/apps/studio/hooks/misc/useReportDateRange.test.tsx
Danny WhiteandJoshen Lim 8fcd5e7f65 feat(studio): add Warehouse report date controls (#53494)
## Problem

The Warehouse report only shows the last hour.

## Solution

Add date selection and refresh using the shared report controls. Respect
the organisation’s log retention limits and the metrics API’s 30-day
maximum.

| After |
| --- |
| <img width="1688" height="1050" alt="CleanShot 2026-11-18 at 11 35
24@2x"
src="https://github.com/user-attachments/assets/88267bf8-1054-480b-ac26-c0c2b58386bc"
/> |
| _The new controls._ |
| <img width="2420" height="1028" alt="CleanShot 2026-11-18 at 11 47
52@2x"
src="https://github.com/user-attachments/assets/383c06a0-5470-4748-b9a6-fe80647a1399"
/> |
| _The standard retention upgrade prompt follow tapping either of these
on a Pro plan._ |

## Review instructions

1. Open the [Studio staging
preview](https://studio-staging-git-dnywh-warehouse-metrics-range-supabase.vercel.app)
and select a staging project with Warehouse access.
2. Open **Observability > Warehouse**, or
`/project/<ref>/observability/warehouse`
([example](https://studio-staging-git-dnywh-warehouse-metrics-range-supabase.vercel.app/dashboard/project/yirnfrgkxmnqgexikhfc/observability/warehouse)).
Change Last 60 minutes to Last 24 hours and check that the chart or
no-data state reflects the selected period.
3. Refresh the report. Check that the selected period stays selected.
4. On a paid staging organisation, select a period beyond its log
retention (for example, Last 14 days with 7-day retention). Check that
the retention upgrade prompt appears and the current period stays
selected.

---------

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
2026-11-19 15:08:18 +11:00

87 lines
3.0 KiB
TypeScript

import { QueryClient } from '@tanstack/react-query'
import { act, waitFor } from '@testing-library/react'
import dayjs from 'dayjs'
import { describe, expect, test } from 'vitest'
import { useReportDateRange } from './useReportDateRange'
import { organizationKeys } from '@/data/organizations/keys'
import { projectKeys } from '@/data/projects/keys'
import { customRenderHook } from '@/tests/lib/custom-render'
const hoursAgo = (hours: number) => dayjs().subtract(hours, 'hour').toISOString()
const render = (searchParams?: Record<string, string>) => {
const queryClient = new QueryClient({
defaultOptions: { queries: { retry: false, staleTime: Infinity } },
})
queryClient.setQueryData(projectKeys.detail('default'), { organization_id: 1 })
queryClient.setQueryData(organizationKeys.list(), [
{ id: 1, slug: 'free-org', plan: { id: 'free' } },
])
queryClient.setQueryData(organizationKeys.entitlements('free-org'), {
entitlements: [
{
feature: { key: 'log.retention_days' },
type: 'numeric',
config: { enabled: true, unlimited: false, value: 1 },
},
],
})
return customRenderHook(() => useReportDateRange(), {
queryClient,
nuqs: { hasMemory: true, searchParams },
})
}
describe('useReportDateRange retention entitlement', () => {
test('handleDatePickerChange prompts for a start 36 hours ago with a one-day entitlement', async () => {
const { result } = render()
await waitFor(() => expect(result.current.isOrgPlanLoading).toBe(false))
let isLocked = false
act(() => {
isLocked = result.current.handleDatePickerChange({
from: hoursAgo(36),
to: dayjs().toISOString(),
isHelper: false,
})
})
expect(isLocked).toBe(true)
expect(result.current.showUpgradePrompt).toBe(true)
})
test('handleDatePickerChange accepts a start inside the entitlement', async () => {
const { result } = render()
await waitFor(() => expect(result.current.isOrgPlanLoading).toBe(false))
let isLocked = true
act(() => {
isLocked = result.current.handleDatePickerChange({
from: hoursAgo(12),
to: dayjs().toISOString(),
isHelper: false,
})
})
expect(isLocked).toBe(false)
expect(result.current.showUpgradePrompt).toBe(false)
})
test('ignores a stored start 36 hours ago and falls back to the default range', async () => {
const stored = hoursAgo(36)
const { result } = render({ its: stored, ite: dayjs().toISOString() })
await waitFor(() => expect(result.current.isOrgPlanLoading).toBe(false))
expect(result.current.selectedDateRange.period_start.date).not.toBe(stored)
})
test('keeps a stored start inside the entitlement', async () => {
const stored = hoursAgo(12)
const { result } = render({ its: stored, ite: dayjs().toISOString() })
await waitFor(() => expect(result.current.isOrgPlanLoading).toBe(false))
expect(result.current.selectedDateRange.period_start.date).toBe(stored)
})
})