Files
supabase/apps/studio/lib/telemetry.test.tsx
Alaister YoungandAlaister Young bbb0af65cb [FE-3106] (part 14) – refactor(studio): drop the dual-runtime shims and aliases (#53223)
Drops the remaining Next-era shims and aliases that only existed for the
dual runtime (FE-3106). Part 14 of the stack.

**Depends on:** #53222

**Changed:**
- ~45 files – import Sentry from `@sentry/react` directly instead of the
aliased `@sentry/nextjs`
- `hooks/ui/usePreventNavigationOnUnsavedChanges.ts` – is now the
TanStack `useBlocker` implementation (previously swapped in by a Vite
alias)
- `components/interfaces/App/RouteValidationWrapper.tsx` – `next/router`
→ `useNavigate` + the leaf route's `fullPath` (`/project/$ref/sql/$id`)
for the exempt-route and last-visited checks
- `hooks/misc/withAuth.tsx` – no `getLayout` forwarding
- `TemplateEditor.test.tsx` – renders inside a router for the
unsaved-changes blocker

**Removed:**
- `compat/sentry-nextjs.ts` and its Vite alias, the
`usePreventNavigationOnUnsavedChanges` alias and Next hook,
`types/next.ts` (`NextPageWithLayout`), the `@sentry/nextjs` dependency

The `compat/next/*` shims for `link`, `router`, `head`, `image`,
`navigation`, `dynamic` and `script` stay: about 300 components outside
`routes/**` still import them.

Addresses
[FE-3106](https://linear.app/supabase/issue/FE-3106/move-page-bodies-into-route-files-and-delete-pages).

## To test

- Trigger an error (e.g. a failing query) – it reaches Sentry as before
- Edit an email template or edge function and navigate away – the
unsaved-changes prompt appears
- Open an org you don't belong to – toast and redirect home; open a SQL
snippet, leave and come back – last snippet is restored

---------

Co-authored-by: Alaister Young <10985857+alaister@users.noreply.github.com>
2026-11-17 17:49:20 +09:00

183 lines
6.0 KiB
TypeScript

import { render, waitFor } from '@testing-library/react'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { Telemetry } from './telemetry'
const mocks = vi.hoisted(() => ({
identify: vi.fn(),
useUser: vi.fn(),
useOrganizationsQuery: vi.fn(),
setUser: vi.fn(),
setTag: vi.fn(),
}))
vi.mock('common', async (importOriginal) => {
const actual = await importOriginal<typeof import('common')>()
return {
...actual,
posthogClient: {
identify: mocks.identify,
},
useUser: () => mocks.useUser(),
PageTelemetry: () => null,
}
})
vi.mock('ui-patterns/consent', () => ({
useConsentToast: () => ({ hasAcceptedConsent: true }),
}))
vi.mock('@/data/organizations/organizations-query', () => ({
useOrganizationsQuery: () => mocks.useOrganizationsQuery(),
}))
vi.mock('@/hooks/misc/useSelectedOrganization', () => ({
useSelectedOrganizationQuery: () => ({ data: undefined }),
}))
vi.mock('@sentry/react', () => ({
setUser: (...args: unknown[]) => mocks.setUser(...args),
setTag: (...args: unknown[]) => mocks.setTag(...args),
}))
const USER_ID = 'user-abc-123'
const CREATED_AT = '2026-05-14T22:30:00.000Z'
const orgs = (count: number) =>
Array.from({ length: count }, (_, i) => ({ id: i, slug: `org-${i}` }))
describe('Telemetry — posthog identify mirroring', () => {
beforeEach(() => {
mocks.identify.mockReset()
mocks.useUser.mockReset()
mocks.useOrganizationsQuery.mockReset()
mocks.setUser.mockReset()
mocks.setTag.mockReset()
})
it('fires identify with both org_count and signup_timestamp when user and orgs are loaded', async () => {
mocks.useUser.mockReturnValue({ id: USER_ID, created_at: CREATED_AT })
mocks.useOrganizationsQuery.mockReturnValue({ data: orgs(1) })
render(<Telemetry />)
await waitFor(() => {
expect(mocks.identify).toHaveBeenCalledWith(USER_ID, {
org_count: 1,
signup_timestamp: CREATED_AT,
})
})
})
it('omits signup_timestamp when created_at is missing', async () => {
mocks.useUser.mockReturnValue({ id: USER_ID, created_at: undefined })
mocks.useOrganizationsQuery.mockReturnValue({ data: orgs(1) })
render(<Telemetry />)
await waitFor(() => {
expect(mocks.identify).toHaveBeenCalledWith(USER_ID, { org_count: 1 })
})
})
it('dedupes when nothing has changed', async () => {
mocks.useUser.mockReturnValue({ id: USER_ID, created_at: CREATED_AT })
mocks.useOrganizationsQuery.mockReturnValue({ data: orgs(1) })
const { rerender } = render(<Telemetry />)
await waitFor(() => expect(mocks.identify).toHaveBeenCalledTimes(1))
rerender(<Telemetry />)
rerender(<Telemetry />)
// Still only one identify — same user, same orgCount, same signupTimestamp.
expect(mocks.identify).toHaveBeenCalledTimes(1)
})
it('re-fires identify when created_at arrives after the first effect run (CodeRabbit regression)', async () => {
// Initial render: user.id is set, but created_at is still undefined (e.g. partial session parse).
mocks.useUser.mockReturnValue({ id: USER_ID, created_at: undefined })
mocks.useOrganizationsQuery.mockReturnValue({ data: orgs(1) })
const { rerender } = render(<Telemetry />)
await waitFor(() => expect(mocks.identify).toHaveBeenCalledTimes(1))
expect(mocks.identify).toHaveBeenLastCalledWith(USER_ID, { org_count: 1 })
// created_at lands later. The dedup ref must track signupTimestamp,
// and the effect deps must include user.created_at, or the second
// identify gets silently skipped — which is exactly the race we shipped
// the original fix to close.
mocks.useUser.mockReturnValue({ id: USER_ID, created_at: CREATED_AT })
rerender(<Telemetry />)
await waitFor(() => expect(mocks.identify).toHaveBeenCalledTimes(2))
expect(mocks.identify).toHaveBeenLastCalledWith(USER_ID, {
org_count: 1,
signup_timestamp: CREATED_AT,
})
})
it('re-fires identify when org_count changes', async () => {
mocks.useUser.mockReturnValue({ id: USER_ID, created_at: CREATED_AT })
mocks.useOrganizationsQuery.mockReturnValue({ data: orgs(1) })
const { rerender } = render(<Telemetry />)
await waitFor(() => expect(mocks.identify).toHaveBeenCalledTimes(1))
mocks.useOrganizationsQuery.mockReturnValue({ data: orgs(2) })
rerender(<Telemetry />)
await waitFor(() => expect(mocks.identify).toHaveBeenCalledTimes(2))
expect(mocks.identify).toHaveBeenLastCalledWith(USER_ID, {
org_count: 2,
signup_timestamp: CREATED_AT,
})
})
it('does not fire identify when user or orgs are missing', async () => {
// user missing
mocks.useUser.mockReturnValue(null)
mocks.useOrganizationsQuery.mockReturnValue({ data: orgs(1) })
const first = render(<Telemetry />)
await new Promise((r) => setTimeout(r, 10))
expect(mocks.identify).not.toHaveBeenCalled()
first.unmount()
// user present but orgs not loaded
mocks.useUser.mockReturnValue({ id: USER_ID, created_at: CREATED_AT })
mocks.useOrganizationsQuery.mockReturnValue({ data: undefined })
render(<Telemetry />)
await new Promise((r) => setTimeout(r, 10))
expect(mocks.identify).not.toHaveBeenCalled()
})
})
describe('Telemetry — Sentry user identification', () => {
beforeEach(() => {
mocks.identify.mockReset()
mocks.useUser.mockReset()
mocks.useOrganizationsQuery.mockReset()
mocks.setUser.mockReset()
})
it('sets the raw (un-hashed) user id on Sentry', async () => {
mocks.useUser.mockReturnValue({ id: USER_ID, created_at: CREATED_AT })
mocks.useOrganizationsQuery.mockReturnValue({ data: orgs(1) })
render(<Telemetry />)
await waitFor(() => {
expect(mocks.setUser).toHaveBeenCalledWith({ id: USER_ID })
})
})
it('does not set a Sentry user when the user is not logged in', async () => {
mocks.useUser.mockReturnValue(null)
mocks.useOrganizationsQuery.mockReturnValue({ data: orgs(1) })
render(<Telemetry />)
await new Promise((r) => setTimeout(r, 10))
expect(mocks.setUser).not.toHaveBeenCalled()
})
})