mirror of
https://github.com/supabase/supabase.git
synced 2026-12-06 08:29:01 +00:00
Enables Braintrust tracing and feedback for Assistant chats on EU-region projects. This was the final holdout that makes it so that tracing is applied consistently across all projects. Adds `projectRegion` metadata to root traces so the project’s region is recorded with each chat (consistent with the existing [isHighComplianceProject metadata](https://github.com/supabase/supabase/blob/917dcb388939b8c54ea13c1e31b3c7285fb2ca65/apps/studio/lib/ai/generate-assistant-response.ts#L188)). Verified in staging with a [successful Assistant trace recording `projectRegion: eu-central-1`](https://www.braintrust.dev/app/supabase.io/p/Assistant/logs?r=1b4a05d7a6c1414e62f532094aa39dc1&sid=4fdc76b08a4c5a43). <img width="2656" height="1560" alt="CleanShot 2026-11-12 at 1 12 30 PM@2x" src="https://github.com/user-attachments/assets/4e81033a-c754-46db-845d-586d1d71e035" /> Closes AI-1197
237 lines
7.3 KiB
TypeScript
237 lines
7.3 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
import { getAIDetails } from './ai-details'
|
|
|
|
vi.mock('@/data/organizations/organizations-query', () => ({
|
|
getOrganizations: vi.fn(),
|
|
}))
|
|
|
|
vi.mock('@/data/projects/project-detail-query', () => ({
|
|
getProjectDetail: vi.fn(),
|
|
}))
|
|
|
|
vi.mock('@/data/config/project-settings-v2-query', () => ({
|
|
getProjectSettings: vi.fn(),
|
|
}))
|
|
|
|
vi.mock('@/hooks/misc/useOrgOptedIntoAi', () => ({
|
|
getAiOptInLevel: vi.fn(),
|
|
}))
|
|
|
|
vi.mock('@/data/entitlements/entitlements-query', () => ({
|
|
checkEntitlement: vi.fn(),
|
|
}))
|
|
|
|
const AUTH = 'Bearer token'
|
|
const HEADERS = { 'Content-Type': 'application/json', Authorization: AUTH }
|
|
const ORG_SLUG = 'test-org'
|
|
const PROJECT_REF = 'test-project'
|
|
|
|
describe('getAIDetails', () => {
|
|
let mockGetOrganizations: ReturnType<typeof vi.fn>
|
|
let mockGetProjectDetail: ReturnType<typeof vi.fn>
|
|
let mockGetProjectSettings: ReturnType<typeof vi.fn>
|
|
let mockGetAiOptInLevel: ReturnType<typeof vi.fn>
|
|
let mockCheckEntitlement: ReturnType<typeof vi.fn>
|
|
|
|
beforeEach(async () => {
|
|
const orgsQuery = await import('@/data/organizations/organizations-query')
|
|
const projectQuery = await import('@/data/projects/project-detail-query')
|
|
const settingsQuery = await import('@/data/config/project-settings-v2-query')
|
|
const aiHook = await import('@/hooks/misc/useOrgOptedIntoAi')
|
|
const entitlementsQuery = await import('@/data/entitlements/entitlements-query')
|
|
|
|
mockGetOrganizations = vi.mocked(orgsQuery.getOrganizations)
|
|
mockGetProjectDetail = vi.mocked(projectQuery.getProjectDetail)
|
|
mockGetProjectSettings = vi.mocked(settingsQuery.getProjectSettings)
|
|
mockGetAiOptInLevel = vi.mocked(aiHook.getAiOptInLevel)
|
|
mockCheckEntitlement = vi.mocked(entitlementsQuery.checkEntitlement)
|
|
|
|
mockGetOrganizations.mockResolvedValue([
|
|
{ id: 1, slug: ORG_SLUG, plan: { id: 'pro' }, opt_in_tags: [] },
|
|
])
|
|
mockGetProjectDetail.mockResolvedValue({
|
|
ref: PROJECT_REF,
|
|
region: 'us-east-1',
|
|
organization_id: 1,
|
|
})
|
|
mockGetProjectSettings.mockResolvedValue({ is_sensitive: false })
|
|
mockCheckEntitlement.mockResolvedValue({ hasAccess: false })
|
|
mockGetAiOptInLevel.mockReturnValue('schema')
|
|
})
|
|
|
|
it('returns the resolved posture when the project belongs to the org', async () => {
|
|
const result = await getAIDetails({
|
|
orgSlug: ORG_SLUG,
|
|
projectRef: PROJECT_REF,
|
|
authorization: AUTH,
|
|
})
|
|
|
|
expect(result).toEqual({
|
|
aiOptInLevel: 'schema',
|
|
hasAccessToAdvanceModel: false,
|
|
orgId: 1,
|
|
orgSlug: ORG_SLUG,
|
|
planId: 'pro',
|
|
projectRegion: 'us-east-1',
|
|
isHighComplianceProject: false,
|
|
})
|
|
})
|
|
|
|
it.each(['eu-west-1', undefined])(
|
|
'validates the project/org pair with region %s',
|
|
async (region) => {
|
|
mockGetProjectDetail.mockResolvedValue({
|
|
ref: PROJECT_REF,
|
|
region,
|
|
organization_id: 1,
|
|
})
|
|
|
|
const result = await getAIDetails({
|
|
orgSlug: ORG_SLUG,
|
|
projectRef: PROJECT_REF,
|
|
authorization: AUTH,
|
|
})
|
|
|
|
expect(result.orgId).toBe(1)
|
|
expect(result.projectRegion).toBe(region)
|
|
}
|
|
)
|
|
|
|
it('flags a High Compliance project', async () => {
|
|
mockGetProjectSettings.mockResolvedValue({ is_sensitive: true })
|
|
|
|
const result = await getAIDetails({
|
|
orgSlug: ORG_SLUG,
|
|
projectRef: PROJECT_REF,
|
|
authorization: AUTH,
|
|
})
|
|
|
|
expect(result.isHighComplianceProject).toBe(true)
|
|
})
|
|
|
|
it('calls getAiOptInLevel with the matched org opt_in_tags', async () => {
|
|
const opt_in_tags = ['AI_SQL_GENERATOR_OPT_IN']
|
|
mockGetOrganizations.mockResolvedValue([
|
|
{ id: 1, slug: ORG_SLUG, plan: { id: 'pro' }, opt_in_tags },
|
|
])
|
|
|
|
await getAIDetails({ orgSlug: ORG_SLUG, projectRef: PROJECT_REF, authorization: AUTH })
|
|
|
|
expect(mockGetAiOptInLevel).toHaveBeenCalledWith(opt_in_tags)
|
|
})
|
|
|
|
it('returns hasAccessToAdvanceModel true when the entitlement grants access', async () => {
|
|
mockCheckEntitlement.mockResolvedValue({ hasAccess: true })
|
|
|
|
const result = await getAIDetails({
|
|
orgSlug: ORG_SLUG,
|
|
projectRef: PROJECT_REF,
|
|
authorization: AUTH,
|
|
})
|
|
|
|
expect(result.hasAccessToAdvanceModel).toBe(true)
|
|
})
|
|
|
|
it('finds the correct org when multiple orgs are returned', async () => {
|
|
mockGetOrganizations.mockResolvedValue([
|
|
{ id: 1, slug: 'org-1', plan: { id: 'free' }, opt_in_tags: [] },
|
|
{ id: 2, slug: ORG_SLUG, plan: { id: 'pro' }, opt_in_tags: [] },
|
|
])
|
|
mockGetProjectDetail.mockResolvedValue({
|
|
ref: PROJECT_REF,
|
|
region: 'us-east-1',
|
|
organization_id: 2,
|
|
})
|
|
|
|
const result = await getAIDetails({
|
|
orgSlug: ORG_SLUG,
|
|
projectRef: PROJECT_REF,
|
|
authorization: AUTH,
|
|
})
|
|
|
|
expect(result.orgId).toBe(2)
|
|
expect(result.planId).toBe('pro')
|
|
})
|
|
|
|
it('forwards authorization headers to all fetches', async () => {
|
|
await getAIDetails({ orgSlug: ORG_SLUG, projectRef: PROJECT_REF, authorization: AUTH })
|
|
|
|
expect(mockGetOrganizations).toHaveBeenCalledWith({ headers: HEADERS })
|
|
expect(mockCheckEntitlement).toHaveBeenCalledWith(
|
|
ORG_SLUG,
|
|
'assistant.advance_model',
|
|
undefined,
|
|
HEADERS
|
|
)
|
|
expect(mockGetProjectDetail).toHaveBeenCalledWith(
|
|
{ ref: PROJECT_REF, skipWake: true },
|
|
undefined,
|
|
HEADERS
|
|
)
|
|
expect(mockGetProjectSettings).toHaveBeenCalledWith(
|
|
{ projectRef: PROJECT_REF },
|
|
undefined,
|
|
HEADERS
|
|
)
|
|
})
|
|
|
|
describe('when the project does not belong to the org', () => {
|
|
beforeEach(() => {
|
|
mockGetOrganizations.mockResolvedValue([
|
|
{ id: 1, slug: ORG_SLUG, plan: { id: 'pro' }, opt_in_tags: ['AI_SQL_GENERATOR_OPT_IN'] },
|
|
{ id: 2, slug: 'other-org', plan: { id: 'free' }, opt_in_tags: [] },
|
|
])
|
|
mockGetProjectDetail.mockResolvedValue({
|
|
ref: PROJECT_REF,
|
|
region: 'us-east-1',
|
|
organization_id: 2,
|
|
})
|
|
mockGetAiOptInLevel.mockReturnValue('schema_and_log_and_data')
|
|
mockCheckEntitlement.mockResolvedValue({ hasAccess: true })
|
|
})
|
|
|
|
it('falls back to the most restrictive posture', async () => {
|
|
const result = await getAIDetails({
|
|
orgSlug: ORG_SLUG,
|
|
projectRef: PROJECT_REF,
|
|
authorization: AUTH,
|
|
})
|
|
|
|
expect(result.orgId).toBeUndefined()
|
|
expect(result.projectRegion).toBeUndefined()
|
|
expect(result.aiOptInLevel).toBe('disabled')
|
|
expect(result.hasAccessToAdvanceModel).toBe(false)
|
|
expect(result.orgSlug).toBeUndefined()
|
|
expect(result.planId).toBeUndefined()
|
|
})
|
|
})
|
|
|
|
it('falls back to the most restrictive posture when the org slug matches no org', async () => {
|
|
mockGetOrganizations.mockResolvedValue([])
|
|
|
|
const result = await getAIDetails({
|
|
orgSlug: ORG_SLUG,
|
|
projectRef: PROJECT_REF,
|
|
authorization: AUTH,
|
|
})
|
|
|
|
expect(result.aiOptInLevel).toBe('disabled')
|
|
expect(result.orgId).toBeUndefined()
|
|
expect(result.orgSlug).toBeUndefined()
|
|
})
|
|
|
|
it('falls back to the most restrictive posture when project detail is unavailable', async () => {
|
|
mockGetProjectDetail.mockResolvedValue(undefined)
|
|
|
|
const result = await getAIDetails({
|
|
orgSlug: ORG_SLUG,
|
|
projectRef: PROJECT_REF,
|
|
authorization: AUTH,
|
|
})
|
|
|
|
expect(result.aiOptInLevel).toBe('disabled')
|
|
expect(result.orgId).toBeUndefined()
|
|
})
|
|
})
|