Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

feat(kb): add unit test CI, search API tests, and Playwright smoke tests (#52151)

Ali Waseem99fd573d1a51
diff --git a/.github/workflows/kb-e2e.yml b/.github/workflows/kb-e2e.yml
new file mode 100644
index 00000000000..8f777734863
--- /dev/null
+++ b/.github/workflows/kb-e2e.yml
@@ -0,0 +1,128 @@
+name: KB E2E Tests
+
+on:
+ pull_request:
+ types: [opened, synchronize, reopened, ready_for_review]
+ branches: ['master']
+ paths:
+ - 'apps/kb/**'
+ - 'e2e/kb/**'
+ - '.github/workflows/kb-e2e.yml'
+ # Daily smoke test against production
+ schedule:
+ - cron: '0 4 * * *'
+ workflow_dispatch:
+ inputs:
+ base_url:
+ description: 'Base URL to test against'
+ required: false
+ default: 'https://supabase.com'
+ type: string
+
+concurrency:
+ group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
+ cancel-in-progress: true
+
+permissions:
+ contents: read
+ statuses: read
+ pull-requests: read
+
+env:
+ CI: true
+
+jobs:
+ e2e:
+ name: KB E2E
+ if: github.event_name != 'pull_request' || github.event.pull_request.draft == false
+ timeout-minutes: 30
+ runs-on: blacksmith-4vcpu-ubuntu-2404
+
+ steps:
+ - name: Detect changed paths
+ id: changes
+ if: github.event_name == 'pull_request'
+ uses: dorny/paths-filter@de90cc6fb38fc0963ad72b210f1f284cd68cea36 # v3.0.2
+ with:
+ filters: |
+ kb_app:
+ - 'apps/kb/**'
+
+ - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
+ with:
+ persist-credentials: false
+ sparse-checkout: |
+ e2e/kb
+ scripts
+ patches
+
+ - uses: pnpm/action-setup@0ebf47130e4866e96fce0953f49152a61190b271 # v6.0.9
+ name: Install pnpm
+ with:
+ run_install: false
+
+ - name: Use Node.js
+ uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0
+ with:
+ node-version-file: '.nvmrc'
+ cache: 'pnpm'
+
+ # Vercel skips the kb preview when a PR only changes the harness, so wait
+ # for a preview only when apps/kb changed. See scripts/waitForVercelPreview.js.
+ - name: Wait for Vercel kb preview
+ if: github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository && steps.changes.outputs.kb_app == 'true'
+ id: deployment
+ continue-on-error: true
+ background: true
+ run: node scripts/waitForVercelPreview.js
+ env:
+ GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+ HEAD_SHA: ${{ github.event.pull_request.head.sha }}
+ PR_NUMBER: ${{ github.event.pull_request.number }}
+ VERCEL_STATUS_CONTEXT: 'Vercel – kb'
+ VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }}
+ VERCEL_TEAM_ID: ${{ secrets.VERCEL_TEAM_ID }}
+
+ - name: Install dependencies
+ run: pnpm install --frozen-lockfile --filter=e2e-kb...
+
+ - name: Install Playwright Chromium
+ run: pnpm -C e2e/kb exec playwright install chromium --with-deps --only-shell
+
+ - name: Wait for Vercel kb preview to finish
+ wait: [deployment]
+
+ # Harness-only PRs, scheduled and manual runs test production (or base_url). When
+ # apps/kb changed but no preview resolved, skip rather than test
+ # production, which doesn't have this PR's changes.
+ - name: Resolve base URL
+ id: base-url
+ env:
+ BASE_URL_INPUT: ${{ inputs.base_url }}
+ KB_APP_CHANGED: ${{ steps.changes.outputs.kb_app }}
+ DEPLOYMENT_URL: ${{ steps.deployment.outputs.deployment-url }}
+ run: |
+ if [ -n "$DEPLOYMENT_URL" ]; then
+ printf 'url=%s\n' "$DEPLOYMENT_URL" >> "$GITHUB_OUTPUT"
+ elif [ "$KB_APP_CHANGED" = "true" ]; then
+ echo "::warning::No Vercel kb preview URL for this pull request. Skipping Playwright."
+ else
+ printf 'url=%s\n' "${BASE_URL_INPUT:-https://supabase.com}" >> "$GITHUB_OUTPUT"
+ fi
+
+ - name: Run kb E2E
+ if: steps.base-url.outputs.url != ''
+ working-directory: e2e/kb
+ run: pnpm run e2e:kb
+ env:
+ PLAYWRIGHT_BASE_URL: ${{ steps.base-url.outputs.url }}
+
+ - name: Upload Playwright report
+ if: failure()
+ uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
+ with:
+ name: kb-playwright-report
+ path: |
+ e2e/kb/playwright-report/
+ e2e/kb/test-results/
+ retention-days: 7
diff --git a/.github/workflows/kb-tests.yml b/.github/workflows/kb-tests.yml
new file mode 100644
index 00000000000..c71ffb90f8a
--- /dev/null
+++ b/.github/workflows/kb-tests.yml
@@ -0,0 +1,52 @@
+name: KB tests
+
+on:
+ pull_request:
+ branches: ['master']
+ paths:
+ - '.github/workflows/kb-tests.yml'
+ - 'apps/kb/**'
+ - 'packages/ui/**'
+ - 'packages/ui-patterns/**'
+
+# Cancel old builds on new commit for same workflow + branch/PR
+concurrency:
+ group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
+ cancel-in-progress: true
+
+permissions:
+ contents: read
+
+env:
+ CI: true
+
+jobs:
+ build:
+ runs-on: blacksmith-4vcpu-ubuntu-2404
+
+ steps:
+ - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
+ with:
+ persist-credentials: false
+ sparse-checkout: |
+ apps/kb
+ packages
+ patches
+
+ - uses: pnpm/action-setup@0ebf47130e4866e96fce0953f49152a61190b271 # v6.0.9
+ name: Install pnpm
+ with:
+ run_install: false
+
+ - name: Use Node.js
+ uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0
+ with:
+ node-version-file: '.nvmrc'
+ cache: 'pnpm'
+
+ - name: Install deps
+ run: pnpm install --frozen-lockfile
+
+ - name: Run tests
+ run: pnpm run test
+ working-directory: ./apps/kb
diff --git a/apps/kb/README.md b/apps/kb/README.md
index 1927fb48bc0..0024c25880a 100644
--- a/apps/kb/README.md
+++ b/apps/kb/README.md
@@ -58,3 +58,30 @@ content, creates a GitHub Discussion for any guide that doesn't have one yet
(`sync:troubleshooting-entries`), and pushes title/content updates to discussions whose guide has since
changed (`sync:troubleshooting-updates`). This is separate from kb's own build — it has real side effects
(creating/updating Discussions, writing DB rows), so it never runs as part of `pnpm build`/`prebuild`.
+
+## Tests
+
+Unit tests (vitest) live next to the code they cover, including the search API (`api/search/_search.test.ts`;
+the `_` prefix keeps Vercel from deploying it as a function). Run them from `apps/kb`:
+
+```sh
+pnpm test
+```
+
+`.github/workflows/kb-tests.yml` runs them on pull requests that touch `apps/kb`.
+
+Functional smoke tests (Playwright) live in `e2e/kb` and cover the key `/kb` journeys: home → topic → guide,
+troubleshooting articles, the markdown export (`.md` and `Accept: text/markdown`), and the search API
+(production only, since previews don't have search configured). They run against
+production by default. Install the browser once, then run from the repository root:
+
+```sh
+pnpm -C e2e/kb exec playwright install chromium
+pnpm e2e:kb
+
+# Against a preview or local build
+PLAYWRIGHT_BASE_URL=https://<preview-url> pnpm e2e:kb
+```
+
+`.github/workflows/kb-e2e.yml` runs the smoke tests on pull requests against the PR's Vercel kb preview (or
+production, when only `e2e/kb` changed), and daily against production. Run it manually with a `base_url` to test any other deployment.
diff --git a/apps/kb/api/search/_search.test.ts b/apps/kb/api/search/_search.test.ts
new file mode 100644
index 00000000000..bf39675faa0
--- /dev/null
+++ b/apps/kb/api/search/_search.test.ts
@@ -0,0 +1,72 @@
+import { beforeEach, describe, expect, it, vi } from 'vitest'
+
+const rpc = vi.fn()
+vi.mock('@supabase/supabase-js', () => ({ createClient: () => ({ rpc }) }))
+
+const { GET } = await import('./index.js')
+
+const row = {
+ page_title: 'Migrate from Heroku',
+ slug: 'kb/guides/Migrate-From-Heroku',
+ heading: 'Overview',
+ excerpt: 'Move your database',
+ score: 1,
+}
+
+const search = (params: string) => GET(new Request(`https://supabase.com/kb/api/search?${params}`))
+
+describe('GET /api/search', () => {
+ beforeEach(() => {
+ rpc.mockReset()
+ vi.unstubAllGlobals()
+ })
+
+ it('rejects a missing or blank query', async () => {
+ expect((await search('')).status).toBe(400)
+ expect((await search('q=%20%20')).status).toBe(400)
+ expect(rpc).not.toHaveBeenCalled()
+ })
+
+ it('maps rows to results and passes the limit through', async () => {
+ rpc.mockResolvedValue({ data: [row], error: null })
+
+ const response = await search('q=heroku&limit=3')
+
+ expect(rpc).toHaveBeenCalledWith('search_kb', { query_text: 'heroku', match_limit: 3 })
+ expect(await response.json()).toEqual([
+ {
+ title: 'Migrate from Heroku',
+ href: 'https://supabase.com/kb/guides/migrate-from-heroku',
+ heading: 'Overview',
+ excerpt: 'Move your database',
+ score: 1,
+ },
+ ])
+ })
+
+ it('defaults the limit to 10 when invalid', async () => {
+ rpc.mockResolvedValue({ data: [], error: null })
+ await search('q=x&limit=abc')
+ expect(rpc).toHaveBeenCalledWith('search_kb', { query_text: 'x', match_limit: 10 })
+ })
+
+ it('returns 500 when the query fails', async () => {
+ vi.spyOn(console, 'error').mockImplementation(() => {})
+ rpc.mockResolvedValue({ data: null, error: new Error('boom') })
+ expect((await search('q=x')).status).toBe(500)
+ })
+
+ it('includes markdown content, or null when it cannot be fetched', async () => {
+ vi.spyOn(console, 'warn').mockImplementation(() => {})
+ rpc.mockResolvedValue({ data: [row, { ...row, slug: 'kb/guides/missing' }], error: null })
+ const fetch = vi.fn(async (url: string) =>
+ url.endsWith('missing.md') ? new Response('', { status: 404 }) : new Response('# Heroku')
+ )
+ vi.stubGlobal('fetch', fetch)
+
+ const results = await (await search('q=x&content=true')).json()
+
+ expect(fetch).toHaveBeenCalledWith('https://supabase.com/kb/guides/migrate-from-heroku.md')
+ expect(results.map((r: { content: string | null }) => r.content)).toEqual(['# Heroku', null])
+ })
+})
diff --git a/e2e/kb/.gitignore b/e2e/kb/.gitignore
new file mode 100644
index 00000000000..68c5d18f00d
--- /dev/null
+++ b/e2e/kb/.gitignore
@@ -0,0 +1,5 @@
+node_modules/
+/test-results/
+/playwright-report/
+/blob-report/
+/playwright/.cache/
diff --git a/e2e/kb/features/kb-smoke.spec.ts b/e2e/kb/features/kb-smoke.spec.ts
new file mode 100644
index 00000000000..ec89fd4be2a
--- /dev/null
+++ b/e2e/kb/features/kb-smoke.spec.ts
@@ -0,0 +1,50 @@
+import { expect, test } from '@playwright/test'
+
+const GUIDE = '/kb/guides/migrate-from-heroku'
+
+test('home page links to guides', async ({ page }) => {
+ const response = await page.goto('/kb')
+ expect(response?.ok()).toBeTruthy()
+ await expect(page.locator('a[href^="/kb/guides/"]').first()).toBeVisible()
+})
+
+test('topic page links to a guide that renders', async ({ page }) => {
+ await page.goto('/kb/topics/migration')
+ await page.locator('a[href^="/kb/guides/"]').first().click()
+ await expect(page).toHaveURL(/\/kb\/guides\//)
+ await expect(page.locator('main article h1')).toBeVisible()
+})
+
+test('troubleshooting article renders', async ({ page }) => {
+ await page.goto('/kb/topics/troubleshooting')
+ await page.locator('a[href^="/kb/troubleshooting/"]').first().click()
+ await expect(page).toHaveURL(/\/kb\/troubleshooting\//)
+ await expect(page.locator('main article h1')).toBeVisible()
+})
+
+test('guide is available as markdown via .md and Accept header', async ({ request }) => {
+ for (const response of [
+ await request.get(`${GUIDE}.md`),
+ await request.get(GUIDE, { headers: { Accept: 'text/markdown' } }),
+ ]) {
+ expect(response.ok()).toBeTruthy()
+ expect(response.headers()['content-type']).toContain('text/markdown')
+ expect(await response.text()).toMatch(/^# /)
+ }
+})
+
+test('markdown sitemap lists guides', async ({ request }) => {
+ const response = await request.get('/kb/sitemap.md')
+ expect(response.ok()).toBeTruthy()
+ expect(await response.text()).toContain('/kb/guides/')
+})
+
+test('search API returns results and rejects a missing query', async ({ request, baseURL }) => {
+ test.skip(baseURL !== 'https://supabase.com', 'Search is only configured in production')
+
+ const results = await request.get('/kb/api/search?q=auth&limit=1')
+ expect(results.ok()).toBeTruthy()
+ expect(await results.json()).toEqual([expect.objectContaining({ title: expect.any(String) })])
+
+ expect((await request.get('/kb/api/search')).status()).toBe(400)
+})
diff --git a/e2e/kb/package.json b/e2e/kb/package.json
new file mode 100644
index 00000000000..8216df6d190
--- /dev/null
+++ b/e2e/kb/package.json
@@ -0,0 +1,15 @@
+{
+ "name": "e2e-kb",
+ "version": "1.0.0",
+ "private": true,
+ "type": "module",
+ "scripts": {
+ "e2e:kb": "playwright test"
+ },
+ "dependencies": {
+ "@playwright/test": "^1.59.1"
+ },
+ "devDependencies": {
+ "@types/node": "catalog:"
+ }
+}
diff --git a/e2e/kb/playwright.config.ts b/e2e/kb/playwright.config.ts
new file mode 100644
index 00000000000..b2c4872feec
--- /dev/null
+++ b/e2e/kb/playwright.config.ts
@@ -0,0 +1,20 @@
+import { defineConfig } from '@playwright/test'
+
+const IS_CI = !!process.env.CI
+
+export default defineConfig({
+ testDir: './features',
+ testMatch: /.*\.spec\.ts/,
+ timeout: 30_000,
+ forbidOnly: IS_CI,
+ retries: IS_CI ? 2 : 0,
+ use: {
+ baseURL: process.env.PLAYWRIGHT_BASE_URL || 'https://supabase.com',
+ browserName: 'chromium',
+ headless: true,
+ screenshot: 'only-on-failure',
+ trace: 'retain-on-failure',
+ },
+ reporter: [['list'], ['html', { open: 'never', outputFolder: './playwright-report' }]],
+ outputDir: './test-results',
+})
diff --git a/e2e/kb/tsconfig.json b/e2e/kb/tsconfig.json
new file mode 100644
index 00000000000..cecfba3008e
--- /dev/null
+++ b/e2e/kb/tsconfig.json
@@ -0,0 +1,12 @@
+{
+ "compilerOptions": {
+ "target": "ES2022",
+ "module": "nodenext",
+ "skipLibCheck": true,
+ "esModuleInterop": true,
+ "strict": true,
+ "noEmit": true,
+ "allowImportingTsExtensions": true,
+ "types": ["node"]
+ }
+}
diff --git a/package.json b/package.json
index 7d806865f65..53c5e91aa9d 100644
--- a/package.json
+++ b/package.json
@@ -46,6 +46,7 @@
"e2e:docs:local-smoke": "pnpm --prefix e2e/docs run e2e:docs:local-smoke",
"e2e:www": "pnpm --prefix e2e/www run e2e:www",
"e2e:www:all": "pnpm --prefix e2e/www run e2e:www:all",
+ "e2e:kb": "pnpm --prefix e2e/kb run e2e:kb",
"perf:kong": "ab -t 5 -c 20 -T application/json http://localhost:8000/",
"perf:meta": "ab -t 5 -c 20 -T application/json http://localhost:5555/tables",
"setup:cli": "supabase start -x studio && supabase status --output json > keys.json && node scripts/generateLocalEnv.js",
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 9e9fb6aa8a9..603e618d7aa 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -2223,6 +2223,16 @@ importers:
specifier: ^4.12.1
version: 4.12.1
+ e2e/kb:
+ dependencies:
+ '@playwright/test':
+ specifier: ^1.59.1
+ version: 1.59.1
+ devDependencies:
+ '@types/node':
+ specifier: 'catalog:'
+ version: 22.13.14
+
e2e/shared:
dependencies:
'@axe-core/playwright':