Files
supabase/apps/studio/components/interfaces/Support/SupportAssistant.utils.test.ts
Monica Khoury d5ee11bea0 fix: hand off AI assistant to the project page in org view (#49477)
Fixes FE-4200, FE-4206.

## What is the current behavior?

Submitting a support ticket from an org-level page (with no project in
the URL) shows a "While you wait" AI assistant card. However, the
assistant is built around project-scoped context from the URL, so making
it work here required adding project-context fallbacks across several
features.

Two previous PRs addressed individual issues, but testing continued to
surface the same underlying problem in other areas, including chat
persistence, message rating, table browsing, and SQL editor actions.

- **#49244**  
- **#49430**  

Rather than keep adding fallbacks, this PR removes the underlying
context mismatch.

## What is the new behavior?

When a support ticket is submitted from an org-level page, the "While
you wait" card now links to the relevant project instead of trying to
run the AI Assistant without real project context.

The link opens the project with the AI Assistant sidebar and hands off
the support ticket. The chat is created there, so project-scoped
features like schema browsing, SQL actions, message rating, and chat
persistence work natively without special-casing.

If there’s no relevant project, the card isn’t shown.

The ticket form also restores the "No specific project" option for cases
where the auto-selected project isn't relevant.

## Additional context

Also fixes ?sidebar= deep links not opening the sidebar after
client-side navigation. LayoutSidebarProvider now reacts to URL param
changes instead of only checking on initial load.

## How to test

1. Submit a project-related support ticket from an org-level page.
2. Confirm the "While you wait" card shows "Open Assistant in project".
3. Click it and confirm the correct project opens with the AI Assistant
sidebar and support chat active.
4. Verify project-scoped features work, such as schema questions and
Edit query / Run.
5. Refresh and confirm the chat persists.
6. Select "No specific project" and confirm no assistant card is shown.
7. Submit a ticket from a project support page and confirm the existing
inline assistant behavior is unchanged.
8. Verify a project ?sidebar=ai-assistant deep link still opens the
sidebar normally.


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

* **New Features**
* Added support handoff links when a submitted ticket belongs to another
project.
* Handoff links securely preserve support request details without
exposing them in the URL.
* Opening a valid handoff link creates and selects a support chat with
the submitted request context.

* **Bug Fixes**
  * Improved sidebar behavior when URL state changes.
  * Project selector validation messages now remain visible.
* Invalid, expired, or mismatched handoffs now fall back to a new chat
and display an error message.
* Handoff details are securely handled only once and cleared after use.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-24 21:37:25 +03:00

131 lines
4.4 KiB
TypeScript

import { SupportCategories } from '@supabase/shared-types/out/constants'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import {
buildSupportAssistantPrompt,
consumeAssistantHandoff,
parseSupportAssistantPrompt,
storeAssistantHandoff,
} from './SupportAssistant.utils'
import type { SubmittedSupportRequest } from './SupportForm.state'
const supportRequest: SubmittedSupportRequest = {
organizationSlug: 'org-1',
projectRef: 'project-1',
category: SupportCategories.PROBLEM,
severity: 'Normal',
subject: 'API requests fail',
message: 'Requests fail with <500> & timeouts',
affectedServices: 'api;database',
library: 'javascript',
allowSupportAccess: true,
dashboardLogs: 'https://example.com/logs',
}
describe('SupportAssistant utils', () => {
beforeEach(() => {
sessionStorage.clear()
})
it('formats support requests as tagged assistant prompts', () => {
const prompt = buildSupportAssistantPrompt(supportRequest)
expect(prompt).toContain('<support>')
expect(prompt).toContain('<assistant_context>')
expect(prompt).toContain('a human member of the Supabase Support team is already looking at it')
expect(prompt).toContain('<subject>API requests fail</subject>')
expect(prompt).not.toContain('<organization_slug>')
expect(prompt).not.toContain('<project_ref>')
})
it('parses and unescapes tagged assistant prompts', () => {
const parsed = parseSupportAssistantPrompt(buildSupportAssistantPrompt(supportRequest))
expect(parsed).toMatchObject({
category: 'Problem',
severity: 'Normal',
subject: 'API requests fail',
message: 'Requests fail with <500> & timeouts',
support_access: 'Granted',
dashboard_logs: 'Attached',
})
})
it('falls back when optional support request fields are missing', () => {
const parsed = parseSupportAssistantPrompt(
buildSupportAssistantPrompt({
...supportRequest,
organizationSlug: undefined,
projectRef: undefined,
library: undefined,
dashboardLogs: undefined,
allowSupportAccess: false,
})
)
expect(parsed).toMatchObject({
library: 'Not provided',
support_access: 'Not granted',
dashboard_logs: 'Not attached',
})
})
it('returns null for text without a valid support payload', () => {
expect(parseSupportAssistantPrompt('Help me debug this issue')).toBeNull()
expect(parseSupportAssistantPrompt('<support></support>')).toBeNull()
})
it('round-trips a request through store/consume', () => {
storeAssistantHandoff('token-1', supportRequest)
expect(consumeAssistantHandoff('token-1')).toEqual(supportRequest)
})
it('preserves literal percent characters in the message (no URL encoding involved)', () => {
const request = { ...supportRequest, message: '100% CPU, literal %20 text' }
storeAssistantHandoff('token-1', request)
expect(consumeAssistantHandoff('token-1')?.message).toBe('100% CPU, literal %20 text')
})
it('removes the entry after consuming it, so it cannot be replayed', () => {
storeAssistantHandoff('token-1', supportRequest)
expect(consumeAssistantHandoff('token-1')).toEqual(supportRequest)
expect(consumeAssistantHandoff('token-1')).toBeNull()
})
it('returns null for a token that was never stored', () => {
expect(consumeAssistantHandoff('unknown-token')).toBeNull()
})
it('returns null for malformed JSON', () => {
sessionStorage.setItem('assistant-handoff:token-1', '{not valid json')
expect(consumeAssistantHandoff('token-1')).toBeNull()
})
it('returns null for validly-shaped JSON that does not match the expected schema', () => {
sessionStorage.setItem('assistant-handoff:token-1', JSON.stringify({ foo: 'bar' }))
expect(consumeAssistantHandoff('token-1')).toBeNull()
sessionStorage.setItem(
'assistant-handoff:token-2',
JSON.stringify({ ...supportRequest, allowSupportAccess: 'yes' })
)
expect(consumeAssistantHandoff('token-2')).toBeNull()
})
it('still returns the parsed value when cleanup (removeItem) throws', () => {
storeAssistantHandoff('token-1', supportRequest)
const removeItemSpy = vi.spyOn(Storage.prototype, 'removeItem').mockImplementation(() => {
throw new Error('storage unavailable')
})
expect(consumeAssistantHandoff('token-1')).toEqual(supportRequest)
removeItemSpy.mockRestore()
})
})