Files
supabase/apps/studio/lib/api/apiWrappers.test.ts
T
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

74 lines
2.5 KiB
TypeScript

import * as Sentry from '@sentry/react'
import type { JwtPayload } from '@supabase/supabase-js'
import type { NextApiRequest, NextApiResponse } from 'next'
import { describe, expect, it, vi } from 'vitest'
import { apiAuthenticate } from './apiAuthenticate'
import { apiWrapper } from './apiWrapper'
import { classifyResponseError } from '@/data/error-classification'
vi.mock('@/lib/constants', () => ({
IS_PLATFORM: true,
API_URL: 'https://api.example.com',
}))
vi.mock('./apiAuthenticate', () => ({
apiAuthenticate: vi.fn(),
}))
vi.mock('@sentry/react', () => ({
captureException: vi.fn(),
}))
describe('apiWrapper', () => {
const mockReq = {} as NextApiRequest
const mockRes = {
status: vi.fn().mockReturnThis(),
json: vi.fn().mockReturnThis(),
} as unknown as NextApiResponse
const mockHandler = vi.fn()
it('should call handler directly when withAuth is false', async () => {
await apiWrapper(mockReq, mockRes, mockHandler, { withAuth: false })
expect(mockHandler).toHaveBeenCalledWith(mockReq, mockRes, undefined)
expect(apiAuthenticate).not.toHaveBeenCalled()
})
it('should pass JWT claims to handler when withAuth is true', async () => {
const mockClaims: JwtPayload = {
iss: 'supabase',
sub: 'user-123',
aud: 'authenticated',
exp: 9999999999,
iat: 1000000000,
role: 'authenticated',
aal: 'aal1',
session_id: 'session-123',
}
vi.mocked(apiAuthenticate).mockResolvedValue(mockClaims)
await apiWrapper(mockReq, mockRes, mockHandler, { withAuth: true })
expect(apiAuthenticate).toHaveBeenCalledWith(mockReq, mockRes)
expect(mockHandler).toHaveBeenCalledWith(mockReq, mockRes, mockClaims)
})
it('should return 401 when authentication fails', async () => {
const mockError = { error: classifyResponseError({ message: 'Invalid token' }) }
vi.mocked(apiAuthenticate).mockResolvedValue(mockError)
await apiWrapper(mockReq, mockRes, mockHandler, { withAuth: true })
expect(mockRes.status).toHaveBeenCalledWith(401)
expect(mockHandler).not.toHaveBeenCalled()
})
it('should report to Sentry and return 500 when the handler throws', async () => {
const mockError = new Error('boom')
mockHandler.mockRejectedValue(mockError)
await apiWrapper(mockReq, mockRes, mockHandler, { withAuth: false })
expect(Sentry.captureException).toHaveBeenCalledWith(mockError)
expect(mockRes.status).toHaveBeenCalledWith(500)
expect(mockRes.json).toHaveBeenCalledWith({ error: mockError })
})
})