Files
supabase/apps/studio/lib/api/rate.test.ts
Ali Waseem b04d14856a Resolve AI opt-in and tracing settings server-side (#48855)
The AI endpoints resolved organization and project settings
independently and applied them together without confirming they belonged
to the same pairing. Consolidates both into a single `getAIDetails` that
reconciles them and falls back to the most restrictive posture when
unconfirmed, and applies the HIPAA sensitivity gate to the opt-in level,
which previously only existed on the client.

Fixes FE-4110

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

- **New Features**
- Consolidated AI access details across organization and project
settings.
- AI functionality now validates project ownership and disables access
for mismatched or HIPAA-sensitive projects.
- AI responses include plan, region, opt-in status, sensitivity,
authorization, and advanced model access information.

- **Bug Fixes**
- Improved fail-closed behavior when project or organization data is
missing or inconsistent.
- Updated AI generation, feedback, rate, and policy flows to
consistently apply consolidated access settings.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-08-10 10:34:31 -06:00

79 lines
1.8 KiB
TypeScript

import { expect, test, vi } from 'vitest'
// End of third-party imports
import rate from '../../pages/api/ai/feedback/rate'
import { sanitizeMessagePart } from '../ai/tools/tool-sanitizer'
vi.mock('../ai/tools/tool-sanitizer', () => ({
sanitizeMessagePart: vi.fn((part) => part),
}))
test('rate calls the tool sanitizer', async () => {
const mockReq = {
method: 'POST',
headers: {
authorization: 'Bearer test-token',
},
body: {
rating: 'negative',
messages: [
{
role: 'assistant',
parts: [
{
type: 'tool-execute_sql',
state: 'output-available',
output: 'test output',
},
],
},
],
messageId: 'test-message-id',
projectRef: 'test-project',
orgSlug: 'test-org',
reason: 'The response was not helpful',
},
on: vi.fn(),
}
const mockRes = {
status: vi.fn(() => mockRes),
json: vi.fn(() => mockRes),
setHeader: vi.fn(() => mockRes),
}
vi.mock('@/lib/ai/ai-details', () => ({
getAIDetails: vi.fn().mockResolvedValue({
aiOptInLevel: 'schema_and_log_and_data',
hasAccessToAdvanceModel: true,
hasHipaaAddon: false,
region: 'us-east-1',
isSensitive: false,
}),
}))
vi.mock('@/lib/ai/model', () => ({
getModel: vi.fn().mockResolvedValue({
modelParams: { model: {} },
}),
}))
vi.mock('ai', () => ({
generateText: vi.fn().mockResolvedValue({
output: {
category: 'sql_generation',
},
}),
Output: { object: vi.fn() },
}))
vi.mock('@/components/ui/AIAssistantPanel/Message.utils', () => ({
rateMessageResponseSchema: {},
}))
await rate(mockReq as any, mockRes as any)
expect(sanitizeMessagePart).toHaveBeenCalled()
})