Files
supabase/apps/studio/components/interfaces/Explorer/ExplorerQuerySourceMenu.test.tsx
T
Charis c0e109f662 refactor(studio): borrow the wire schema's time range in the query-source registry (#49070)
Second of a stack. **Stacked on #49069** — review that one first; this
PR's diff only makes sense on top of it. Base will retarget to `master`
automatically when #49069 merges.

Net `-72` lines. No behavior change beyond the one noted at the bottom.

## The problem

The query-source registry carried its own `LogTimeRange` type and
`logTimeRangeSchema`, which had drifted from the notebook wire schema's
copy in four ways:

| | wire schema | registry |
|---|---|---|
| discriminant | `_tag: 'relative_time_range'` | `type: 'relative'` |
| absolute bounds | `start` / `end` | `from` / `to` |
| relative units | minute…year | minute, hour, day |
| validation | none | positive int, end-after-start |

Two definitions of one concept, neither convertible to the other without
a lossy mapping — and the notebook query cell was papering over it by
discarding a log cell's persisted range and substituting a default.

## What changed

#49069 moved the validations onto the wire schema's `timeRangeSchema`
and exported it. This PR deletes the registry's copy and points every
consumer at `TimeRange`. The registry keeps what is genuinely runtime:
endpoints, labels, availability, defaults.

The field renames ripple mechanically through the logs date-picker
helpers, the time-range submenu, `useLogsCustomRange`, the SQL editor's
session state, and their tests. Coverage for the absolute-range and unit
rules moved to `notebook-schema.test.ts` in #49069, alongside the schema
that now owns them.

`ExplorerQuerySourceMenu` also drops its hand-rolled custom-range
construction in favor of `customDateRangeToLogTimeRange`, which already
existed and does the same clamping.

## One behavior change

`logTimeRangeToDatePickerValue` now renders a range whose unit has no
picker preset (week, month, year — allowed by the wire schema, not
offered in the UI) as a resolved absolute range, instead of trying and
failing to build a helper for it. Previously unreachable, since the
registry's narrower unit set made those ranges unrepresentable.

## Verification

Typecheck, Prettier, and the lint ratchet clean. 401 tests pass across
the notebook schema, query sources, the logs source components, the SQL
editor, and the Explorer surfaces.

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

## Summary by CodeRabbit

* **Bug Fixes**
  * Improved log time-range handling across Explorer and SQL Editor.
* Custom date ranges now display and resolve correctly, including
clamping invalid ranges.
* Unsupported relative time units are converted to compatible absolute
date-picker values.

* **Refactor**
* Standardized log queries on a shared time-range format for more
consistent validation and behavior.


<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
2026-08-14 13:45:41 +07:00

127 lines
3.9 KiB
TypeScript

import { screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { FeatureFlagContext } from 'common'
import { mockAnimationsApi } from 'jsdom-testing-mocks'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { ExplorerQuerySourceMenu } from './ExplorerQuerySourceMenu'
import { customRender } from '@/tests/lib/custom-render'
import { addAPIMock } from '@/tests/lib/msw'
mockAnimationsApi()
beforeEach(() => {
addAPIMock({
method: 'get',
path: '/platform/projects/:ref',
response: {
id: 1,
ref: 'default',
organization_id: 1,
name: 'Test Project',
status: 'ACTIVE_HEALTHY',
cloud_provider: 'AWS',
region: 'us-east-1',
db_host: 'db.default.supabase.co',
restUrl: 'https://default.supabase.co/rest/v1/',
inserted_at: '2024-01-01T00:00:00Z',
updated_at: '2024-01-01T00:00:00Z',
subscription_id: 'sub_123',
is_branch_enabled: false,
is_physical_backups_enabled: false,
high_availability: false,
integration_source: null,
connectionString: 'postgresql://postgres@localhost:5432/postgres',
is_hibernating: false,
},
})
})
describe('ExplorerQuerySourceMenu', () => {
const renderWithFlags = (
source: Parameters<typeof ExplorerQuerySourceMenu>[0]['source'],
flags: Record<string, boolean>
) =>
customRender(
<FeatureFlagContext.Provider value={{ configcat: flags, posthog: {}, hasLoaded: true }}>
<ExplorerQuerySourceMenu source={source} onSourceChange={vi.fn()} />
</FeatureFlagContext.Provider>
)
it('emits a complete default binding when the query changes source', async () => {
const onSourceChange = vi.fn()
customRender(
<ExplorerQuerySourceMenu
source={{
id: 'logs',
type: 'logs',
parameters: { time_range: { _tag: 'relative_time_range', amount: 1, unit: 'hour' } },
}}
onSourceChange={onSourceChange}
/>
)
await userEvent.click(screen.getByRole('button', { name: 'Query source: Logs' }))
await userEvent.click(screen.getByText('Database'))
expect(onSourceChange).toHaveBeenCalledWith({
id: 'database',
type: 'database',
parameters: {},
})
})
it('emits the selected log time range as source parameters', async () => {
const onSourceChange = vi.fn()
customRender(
<ExplorerQuerySourceMenu
source={{
id: 'logs',
type: 'logs',
parameters: { time_range: { _tag: 'relative_time_range', amount: 1, unit: 'hour' } },
}}
onSourceChange={onSourceChange}
/>
)
await userEvent.click(screen.getByRole('button', { name: 'Query source: Logs' }))
await userEvent.hover(screen.getByText('Time range'))
await userEvent.click(await screen.findByText('Last 3 hours'))
expect(onSourceChange).toHaveBeenCalledWith({
id: 'logs',
type: 'logs',
parameters: { time_range: { _tag: 'relative_time_range', amount: 3, unit: 'hour' } },
})
})
it('does not offer logs when source flags are disabled for a database query', async () => {
renderWithFlags(
{ id: 'database', type: 'database', parameters: {} },
{ sqlEditorLogsSource: false, otelLegacyLogs: false }
)
await userEvent.click(screen.getByRole('button', { name: 'Query source: Database' }))
expect(screen.queryByText('Logs')).not.toBeInTheDocument()
})
it('keeps logs available when an existing query already uses it', async () => {
renderWithFlags(
{
id: 'logs',
type: 'logs',
parameters: { time_range: { _tag: 'relative_time_range', amount: 1, unit: 'hour' } },
},
{ sqlEditorLogsSource: false, otelLegacyLogs: false }
)
await userEvent.click(screen.getByRole('button', { name: 'Query source: Logs' }))
expect(screen.getAllByText('Logs')).toHaveLength(2)
expect(screen.getByText('Database')).toBeInTheDocument()
})
})