Files
supabase/apps/docs/app/api/search_v2/route.test.ts
Jeremias Menichelli f920456c3f feat: Improve kb search to be on pair with docs search v2 (#53454)
## Problem
though they use the same package and were conceived to be 1-1, search v2
in Docs and Kb search have some differences:
- Docs new search function is more complex to prevent no results,
improvements kb search hasn't seen. For example, when you look for
"passwor", incomplete term, Docs return results around the password
term, but kb doesn't **(see screenshot below)**.
- On kb side, we have a `content=true` parameter to fetch markdown
content from results to serve some agentic tools, but Docs search never
received that upgrade. Fair to say, in docs we are not covering all
routes in markdown at the moment so it's not 100% reliable, but whenever
we reach that, we can switch to use this search endpoint for MCP
tooling.
 
<img width="1512" height="867" alt="Screenshot 2026-11-16 at 13 06 08"
src="https://github.com/user-attachments/assets/99baa772-d4b1-4d73-bfa7-7110d5635f0a"
/>

## Solution

- Add a migration file on kb search table to match docs search SQL
function.
- Add `content=true` parameter to docs search

<!--
## Preview links

If relevant, include links to changed pages for easy review access.

Copy the preview base URL from the Vercel bot comment on this PR. Use
the following table as an example template.

| Site | Live | Preview | Search for |
| -------------- |
-------------------------------------------------------------------------
|
------------------------------------------------------------------------------------------------------------
| ----------------------------- |
| WWW | [/blog/your-post](https://supabase.com/blog/your-post) |
[/blog/your-post](https://zone-www-dot-com-git-branch-name-supabase.vercel.app/blog/your-post)
| unique phrase from the change |
| Docs |
[/docs/guides/your-page](https://supabase.com/docs/guides/your-page) |
[/docs/guides/your-page](https://docs-git-branch-name-supabase.vercel.app/docs/guides/your-page)
| unique phrase from the change |
| Studio | [/dashboard](https://supabase.com/dashboard) |
[/dashboard](https://studio-git-branch-name-supabase.vercel.app/dashboard)
| unique phrase from the change |
| Design system | [/design-system](https://supabase.com/design-system) |
[/design-system](https://design-system-git-branch-name-supabase.vercel.app/design-system)
| unique phrase from the change |
| UI library | [/library](https://supabase.com/library) |
[/library](https://ui-library-git-branch-name-supabase.vercel.app/library)
| unique phrase from the change |
| Knowledge base |
[/kb/guides/your-page](https://supabase.com/kb/guides/your-page) |
[/kb/guides/your-page](https://kb-git-branch-name-supabase.vercel.app/kb/guides/your-page)
| unique phrase from the change |
-->

<!-- ## Additional context

Optionally add any other context or screenshots.

-->

## Review instructions

- To test kb new search function, you would have checkout this branch,
to spin locally kb search migration and search, other way we need to
wait for merging and testing. It's important Supabase integration
returns _green_ here to make sure migrations run safely.
- For docs search new parameter. Open the preview for docs in this
branch:
1. Open the search modal, and open your browser's dev tools, go to the
network tab
   1. Do a search, grab the URL and open it in a new tab
   1. Add `content=true` and `limit=3` as query prameters
1. You should get back three results with a content property containing
the markdown content of each route (see example below)
   
<img width="1273" height="793" alt="Screenshot 2026-11-16 at 13 12 32"
src="https://github.com/user-attachments/assets/d1af4663-e17e-438e-99ac-4e63f51460a9"
/>


## Checklist

Check all before review:

- [x] I have read
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
- [x] If I wrote a new docs topic or edited an existing topic, I used
the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs
[style
guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide)
2026-11-17 16:55:36 +01:00

78 lines
2.8 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from 'vitest'
const rpc = vi.fn()
vi.mock('~/scripts/search_v2/supabase', () => ({
SEARCH_FUNCTION: 'docs_search_v2',
createSupabaseClient: () => ({ rpc }),
}))
vi.mock('@sentry/nextjs', () => ({ captureException: vi.fn() }))
vi.mock('~/lib/constants', () => ({
BASE_PATH: '/docs',
IS_DEV: false,
IS_PRODUCTION: true,
PROD_URL: 'https://supabase.com/docs',
}))
const { GET } = await import('./route')
const guideRow = { slug: 'guides/auth/users', page_title: 'Users', heading: 'Users', score: 3500 }
const referenceRow = { slug: 'reference/javascript/auth-signup', page_title: 'signUp', score: 3400 }
const search = (params: string) =>
GET(new Request(`https://supabase.com/docs/api/search_v2?${params}`) as never)
describe('GET /api/search_v2', () => {
beforeEach(() => {
rpc.mockReset()
vi.unstubAllGlobals()
})
it('rejects a missing query', async () => {
expect((await search('')).status).toBe(400)
expect(rpc).not.toHaveBeenCalled()
})
it('returns the rows as is, without fetching content, by default', async () => {
rpc.mockResolvedValue({ data: [guideRow], error: null })
const fetch = vi.fn()
vi.stubGlobal('fetch', fetch)
const response = await search('q=users&limit=3')
expect(rpc).toHaveBeenCalledWith('docs_search_v2', { query_text: 'users', match_limit: 3 })
expect(await response.json()).toEqual([guideRow])
expect(fetch).not.toHaveBeenCalled()
})
it('adds the markdown of guides when content=true', async () => {
rpc.mockResolvedValue({ data: [guideRow], error: null })
const fetch = vi.fn(async () => new Response('# Users'))
vi.stubGlobal('fetch', fetch)
const results = await (await search('q=users&content=true')).json()
expect(fetch).toHaveBeenCalledWith('https://supabase.com/docs/guides/auth/users.md')
expect(results).toEqual([{ ...guideRow, content: '# Users' }])
})
it('sets content to null for pages with no markdown or that fail to load', async () => {
vi.spyOn(console, 'warn').mockImplementation(() => {})
const missing = { ...guideRow, slug: 'guides/missing' }
rpc.mockResolvedValue({ data: [referenceRow, missing], error: null })
const fetch = vi.fn(async () => new Response('', { status: 404 }))
vi.stubGlobal('fetch', fetch)
const results = await (await search('q=x&content=true')).json()
// reference pages have no markdown export, so they are not fetched at all
expect(fetch).toHaveBeenCalledTimes(1)
expect(results.map((r: { content: string | null }) => r.content)).toEqual([null, null])
})
it('returns 500 when the query fails', async () => {
vi.spyOn(console, 'error').mockImplementation(() => {})
rpc.mockResolvedValue({ data: null, error: { message: 'boom' } })
expect((await search('q=x')).status).toBe(500)
})
})