Files
supabase/apps/studio/lib/server/configcat.test.ts
Charis 7798e42435 feat(studio): notebook read tools (#48908)
## Summary
- Adds `list_notebooks` (cursor-paginated) and `get_notebook` AI tools
in `lib/ai/tools/notebook-tools.ts`, modeled directly on
`report-tools.ts`: server-side `getContent`/`getNotebook` with the
`authorization` header forwarded, zod-validated input.
- `get_notebook` resolves every cell and exposes `unchecked_sql` as a
plain `sql` field for the agent to read — display only, per the
`safe-sql-execution` skill; nothing here executes SQL.
- Registers both tools in `lib/ai/tools/index.ts` (same platform branch
as reports) and in `lib/ai/tool-filter.ts`'s `toolSetValidationSchema` +
`TOOL_CATEGORY_MAP` (`SCHEMA` tier).
- Adds an optional `headers` param to `content-infinite-query.ts`'s
`getContent`, mirroring the sibling `content-query.ts`, so the
cursor-paginated fetch can carry the `Authorization` header from a
server context.
- New tools are behind the Explorer feature flag.

Stacked on #48907 (1.4 — notebook query and mutation hooks), per the
Notebooks implementation plan (stack 2.1).

Resolves FE-4081
Resolves FE-4080

## Test plan
- [x] `pnpm exec tsc --noEmit` — no new errors
- [x] `pnpm exec vitest run lib/ai/tools/notebook-tools.test.ts
lib/ai/tools/index.test.ts lib/ai/tools/report-tools.test.ts
data/content/notebooks` — 36/36 passing
- [x] `pnpm --filter studio run lint` — no new warnings
- [x] `pnpm exec prettier --check` on changed files — clean

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

* **New Features**
  * Added AI tools to list project notebooks with pagination.
* Added AI support for retrieving notebook markdown and resolved SQL
cell content.
  * Notebook tools now respect project and authorization context.
* Notebook features are available only when Explorer access is enabled.
  * Content requests can forward custom request headers.

* **Tests**
* Added coverage for notebook tools, Explorer access, feature flags,
authorization, pagination, and error handling.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-08-11 08:40:51 -04:00

128 lines
4.5 KiB
TypeScript

import * as configcat from '@configcat/sdk/node'
import type { IConfigCatClient } from '@configcat/sdk/node'
import { beforeEach, describe, expect, it, vi } from 'vitest'
vi.mock('@configcat/sdk/node', () => ({
getClient: vi.fn(),
PollingMode: {
LazyLoad: 'LazyLoad',
},
User: vi.fn(),
}))
describe('lib/server/configcat getServerFlags', () => {
const mockClient = {
getAllValuesAsync: vi.fn(),
}
beforeEach(() => {
vi.clearAllMocks()
vi.resetModules()
vi.unstubAllEnvs()
vi.mocked(configcat.getClient).mockReturnValue(mockClient as unknown as IConfigCatClient)
})
it('should return empty array and skip getClient when no env vars are present', async () => {
const { getServerFlags, trustedUserEmail } = await import('./configcat')
const result = await getServerFlags(trustedUserEmail('test@example.com'))
expect(result).toEqual([])
expect(configcat.getClient).not.toHaveBeenCalled()
})
it('should prefer the proxy over the direct SDK key when both are configured', async () => {
vi.stubEnv('NEXT_PUBLIC_CONFIGCAT_SDK_KEY', 'test-sdk-key')
vi.stubEnv('NEXT_PUBLIC_CONFIGCAT_PROXY_URL', 'https://proxy.example.com')
mockClient.getAllValuesAsync.mockResolvedValue([])
const { getServerFlags, trustedUserEmail } = await import('./configcat')
await getServerFlags(trustedUserEmail('test@example.com'))
expect(configcat.getClient).toHaveBeenCalledTimes(1)
expect(configcat.getClient).toHaveBeenCalledWith('configcat-proxy/frontend-v2', 'LazyLoad', {
baseUrl: 'https://proxy.example.com',
})
})
it('should fall back to the direct SDK key when no proxy URL is configured', async () => {
vi.stubEnv('NEXT_PUBLIC_CONFIGCAT_SDK_KEY', 'test-sdk-key')
mockClient.getAllValuesAsync.mockResolvedValue([])
const { getServerFlags, trustedUserEmail } = await import('./configcat')
await getServerFlags(trustedUserEmail('test@example.com'))
expect(configcat.getClient).toHaveBeenCalledWith('test-sdk-key', 'LazyLoad')
})
it('should call getAllValuesAsync with a user built from the given email', async () => {
vi.stubEnv('NEXT_PUBLIC_CONFIGCAT_SDK_KEY', 'test-sdk-key')
const mockValues = [{ settingKey: 'explorer', settingValue: true }]
mockClient.getAllValuesAsync.mockResolvedValue(mockValues)
const { getServerFlags, trustedUserEmail } = await import('./configcat')
const result = await getServerFlags(trustedUserEmail('test@example.com'))
expect(configcat.User).toHaveBeenCalledWith(
'test@example.com',
undefined,
undefined,
expect.any(Object)
)
expect(result).toEqual(mockValues)
})
it('reuses the same client across calls instead of creating a new one each time', async () => {
vi.stubEnv('NEXT_PUBLIC_CONFIGCAT_SDK_KEY', 'test-sdk-key')
mockClient.getAllValuesAsync.mockResolvedValue([])
const { getServerFlags, trustedUserEmail } = await import('./configcat')
await getServerFlags(trustedUserEmail('a@example.com'))
await getServerFlags(trustedUserEmail('b@example.com'))
expect(configcat.getClient).toHaveBeenCalledTimes(1)
})
describe('is_staff targeting attribute', () => {
beforeEach(() => {
vi.stubEnv('NEXT_PUBLIC_CONFIGCAT_SDK_KEY', 'test-sdk-key')
mockClient.getAllValuesAsync.mockResolvedValue([])
})
it('is true for a real @supabase.com/@supabase.io email', async () => {
const { getServerFlags, trustedUserEmail } = await import('./configcat')
await getServerFlags(trustedUserEmail('person@supabase.io'))
expect(configcat.User).toHaveBeenCalledWith(
'person@supabase.io',
undefined,
undefined,
expect.objectContaining({ is_staff: 'true' })
)
})
it('is false for a domain that merely contains "@supabase." as a substring', async () => {
const { getServerFlags, trustedUserEmail } = await import('./configcat')
await getServerFlags(trustedUserEmail('attacker@supabase.evil.com'))
expect(configcat.User).toHaveBeenCalledWith(
'attacker@supabase.evil.com',
undefined,
undefined,
expect.objectContaining({ is_staff: 'false' })
)
})
it('is false when no email is given', async () => {
const { getServerFlags } = await import('./configcat')
await getServerFlags(undefined)
expect(configcat.User).toHaveBeenCalledWith(
'anonymous',
undefined,
undefined,
expect.objectContaining({ is_staff: 'false' })
)
})
})
})