import { fireEvent, screen, waitFor, within } from '@testing-library/react' import userEvent from '@testing-library/user-event' import { platformComponents as components } from 'api-types' import { HttpResponse } from 'msw' import { beforeEach, describe, expect, test, vi } from 'vitest' import { getCatalogEntry } from '../AccessToken.permissions' import { NewScopedTokenSheet } from './NewScopedTokenSheet' import type { ProfileContextType } from '@/lib/profile' import { createMockOrganizationResponse, createMockProject } from '@/tests/helpers' import { customRender } from '@/tests/lib/custom-render' import { addAPIMock } from '@/tests/lib/msw' type OrganizationResponse = components['schemas']['OrganizationResponse_Output'] type ProjectsResponse = components['schemas']['ListProjectsPaginatedResponse_Output'] type OrganizationProjectsResponse = components['schemas']['OrganizationProjectsResponse_Output'] type CreateTokenResponse = components['schemas']['CreateScopedAccessTokenResponse_Output'] type CreateClassicTokenResponse = components['schemas']['CreateAccessTokenResponse_Output'] type ProjectDetailResponse = components['schemas']['ProjectDetailResponse_Output'] const mockUseReducedMotion = vi.fn(() => false) vi.mock('common', async (importOriginal) => { const actual = (await importOriginal()) as typeof import('common') return { ...actual, useReducedMotion: () => mockUseReducedMotion() } }) const { mockTrack } = vi.hoisted(() => ({ mockTrack: vi.fn() })) vi.mock('@/lib/telemetry/track', () => ({ useTrack: () => mockTrack })) // Each test mounts the full sheet and walks several async steps. That takes ~1s on an idle // machine but approaches the default 5s limit when CI workers are saturated. vi.setConfig({ testTimeout: 15_000 }) const user = userEvent.setup({ writeToClipboard: true, }) const PROFILE_CONTEXT: ProfileContextType = { profile: { id: 1, auth0_id: 'auth0|test', gotrue_id: 'gotrue-test', username: 'testuser', primary_email: 'test@example.com', first_name: null, last_name: null, mobile: null, is_alpha_user: false, is_sso_user: false, disabled_features: [], free_project_limit: null, }, error: null, isLoading: false, isError: false, isSuccess: true, } const mockOrganizations = () => addAPIMock({ method: 'get', path: '/platform/organizations', response: () => HttpResponse.json([ createMockOrganizationResponse({ slug: 'acme-prod', name: 'Acme Production' }), ]), }) const mockProjects = () => addAPIMock({ method: 'get', path: '/platform/projects', response: () => HttpResponse.json({ pagination: { count: 1, limit: 100, offset: 0 }, projects: [ { ...createMockProject({ id: 1, ref: 'project-1', name: 'Project 1', organization_id: 1, }), organization_slug: 'acme-prod', preview_branch_refs: [], }, ], }), }) const mockOrgProjects = () => addAPIMock({ method: 'get', path: '/platform/organizations/:slug/projects', response: () => HttpResponse.json({ pagination: { count: 1, limit: 100, offset: 0 }, projects: [ { cloud_provider: 'AWS', databases: [], inserted_at: new Date().toISOString(), integration_source: null, is_branch: false, name: 'Project 1', ref: 'project-1', region: 'us-east-1', status: 'ACTIVE_HEALTHY', }, ], }), }) // The review screen's project pill looks up the selected project const mockProjectDetail = () => addAPIMock({ method: 'get', path: '/platform/projects/:ref', response: () => HttpResponse.json({ ...createMockProject({ id: 1, ref: 'project-1', name: 'Project 1', organization_id: 1 }), connectionString: null, db_host: 'db.project-1.supabase.co', high_availability: false, integration_source: null, inserted_at: '2026-01-01T00:00:00Z', is_physical_backups_enabled: false, restUrl: 'https://project-1.supabase.co', status: 'ACTIVE_HEALTHY', subscription_id: 'subscription-1', updated_at: '2026-01-01T00:00:00Z', }), }) const mockPermissionsMap = () => addAPIMock({ method: 'get', // @ts-expect-error Studio API is missing from types path: '/scoped-access-token-permissions', response: () => HttpResponse.json({ scopes: {}, endpoints: {}, mcp_tools: {}, }), }) const mockCreateToken = () => addAPIMock({ method: 'post', path: '/platform/profile/scoped-access-tokens', response: () => HttpResponse.json({ created_at: '', expires_at: null, id: 'plop', last_used_at: null, name: 'test', token: 'a_token_value', token_alias: '', permissions: [], }), }) const mockCreateClassicToken = () => addAPIMock({ method: 'post', path: '/platform/profile/access-tokens', response: () => HttpResponse.json({ created_at: '', expires_at: null, id: 1, last_used_at: null, name: 'test', token: 'a_classic_token_value', token_alias: '', }), }) /** * Permission categories render collapsed, so a category has to be expanded before its * rows are in the DOM. */ const expandPermissionCategory = async (name: string) => fireEvent.click(await screen.findByRole('button', { name: new RegExp(`^${name}`) })) /** * Each permission row renders a hidden native