mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 07:29:16 +00:00
## Context
_TLDR: Changes here speeds up unit tests on CI from ~14 minutes to ~3
minutes_ 😂
_TLDR 2: The file changes are solely only test files +
`studio-unit-tests.yml`_
We've currently got a lot of unit tests in the repository which while
great for coverage, hasn't been ideally written. There's a lot of
redundancy + overly specific tests that break easily, and our current
unit test takes about 15 minutes to run in the GHA per PR. Hence am
doing a audit clean up of all existing unit tests in the studio repo.
## Changes involved
- `.test.ts` files now run in `node`, `.test.tsx` in `jsdom`
- jsdom costs about 1s of setup per file. A `.ts` test that needs the
DOM opts in with `// @vitest-environment jsdom` (46 files do).
- Removed redundant and over-specific tests
- Duplicates are merged into `test.each`. Tests that only covered
libraries, existed for coverage, or asserted class names and snapshots
are deleted. 19 files are merged into a sibling or deleted.
- Fixed about 15 tests that couldn't fail
- Examples: unawaited assertions, fixtures equal to the default, and a
`semverGte` test calling `semverLte`.
- Sped up the slowest files
- Real sleeps are replaced with fake timers, `user.type` with paste, and
Radix option queries are scoped to the open listbox. For example,
`RestoringState` went from 18s to 0.3s and
`useTableEditorFiltersSort.navigation` from 20s to 3s.
- These ^ already shave down the unit tests time from 14 mins to 9 mins,
but am also sharding unit tests across 4 jobs to increase speed up
further which then shaves down time to 3 mins now
222 lines
8.0 KiB
TypeScript
222 lines
8.0 KiB
TypeScript
import type { Trace } from 'braintrust'
|
|
import { describe, expect, it, vi } from 'vitest'
|
|
|
|
import {
|
|
toolUsageScorer,
|
|
urlValidityScorer,
|
|
type AssistantEvalOutput,
|
|
type Expected,
|
|
} from './scorer'
|
|
import type { Transcript } from './transcript'
|
|
|
|
const DOCS_URL = 'https://supabase.com/docs/guides/auth'
|
|
|
|
/**
|
|
* Minimal stand-in for a live Trace. Only getThread is exercised by the online
|
|
* fallback path (see resolveTranscript in scorer.ts).
|
|
*/
|
|
const mockTrace = (thread: unknown[]) => {
|
|
const getThread = vi.fn().mockResolvedValue(thread)
|
|
return { trace: { getThread } as unknown as Trace, getThread }
|
|
}
|
|
|
|
const THREAD_WITH_DOCS_URL = [
|
|
{ role: 'user', content: 'Where are the auth docs?' },
|
|
{ role: 'assistant', content: [{ type: 'text', text: `See ${DOCS_URL} for details.` }] },
|
|
]
|
|
|
|
const transcript = (lastAssistantTurn: string): Transcript => ({
|
|
currentUserInput: 'Where are the auth docs?',
|
|
priorConversation: null,
|
|
lastAssistantTurn,
|
|
lastAssistantTurnWithToolInputs: lastAssistantTurn,
|
|
})
|
|
|
|
const runUrlValidityScorer = (output: AssistantEvalOutput | null, trace?: Trace) =>
|
|
urlValidityScorer({
|
|
input: { prompt: 'Where are the auth docs?' },
|
|
expected: {},
|
|
output,
|
|
trace,
|
|
})
|
|
|
|
/**
|
|
* Minimal stand-in for a live Trace exposing only the tool spans
|
|
* toolUsageScorer reads via getToolSpans. `input`, when provided as a plain
|
|
* object, passes straight through getToolSpanInput's AI-SDK-tuple unwrap
|
|
* (which only fires for array-shaped span.input), so it lands as-is.
|
|
*/
|
|
const mockToolTrace = (calls: Array<{ name: string; input?: unknown }>) => {
|
|
const getSpans = vi
|
|
.fn()
|
|
.mockResolvedValue(calls.map(({ name, input }) => ({ span_attributes: { name }, input })))
|
|
return { trace: { getSpans } as unknown as Trace }
|
|
}
|
|
|
|
const runToolUsageScorer = (
|
|
expected: Expected,
|
|
trace?: Trace,
|
|
output: AssistantEvalOutput | null = null
|
|
) => toolUsageScorer({ input: { prompt: 'irrelevant' }, expected, output, trace })
|
|
|
|
describe('scorers with online (null) output', () => {
|
|
// Online scorers run against live production logs, which have no eval task
|
|
// and therefore no output at all — Braintrust passes null.
|
|
it('returns null instead of throwing when there is neither output nor trace', async () => {
|
|
await expect(runUrlValidityScorer(null)).resolves.toBeNull()
|
|
})
|
|
|
|
it('derives the transcript from the trace when output is null', async () => {
|
|
const fetchMock = vi.fn().mockResolvedValue({ ok: true, status: 200 })
|
|
vi.stubGlobal('fetch', fetchMock)
|
|
|
|
const { trace, getThread } = mockTrace(THREAD_WITH_DOCS_URL)
|
|
const result = await runUrlValidityScorer(null, trace)
|
|
|
|
expect(getThread).toHaveBeenCalledWith({ preprocessor: 'thread' })
|
|
expect(result).toMatchObject({ name: 'URL Validity', score: 1, metadata: { urls: [DOCS_URL] } })
|
|
|
|
vi.unstubAllGlobals()
|
|
})
|
|
|
|
it('prefers the offline task transcript over the trace when both are present', async () => {
|
|
const { trace, getThread } = mockTrace(THREAD_WITH_DOCS_URL)
|
|
const output = {
|
|
finishReason: 'stop' as const,
|
|
transcript: transcript('No links here, just prose.'),
|
|
}
|
|
|
|
// No supabase URLs in the offline transcript, so the scorer opts out.
|
|
await expect(runUrlValidityScorer(output, trace)).resolves.toBeNull()
|
|
expect(getThread).not.toHaveBeenCalled()
|
|
})
|
|
})
|
|
|
|
describe('toolUsageScorer', () => {
|
|
it.each<[string, Expected]>([
|
|
['neither requiredTools nor forbiddenTools is specified', {}],
|
|
['requiredTools is empty and forbiddenTools is unset', { requiredTools: [] }],
|
|
['forbiddenTools is empty and requiredTools is unset', { forbiddenTools: [] }],
|
|
])('returns null when %s', async (_, expected) => {
|
|
const { trace } = mockToolTrace([{ name: 'execute_sql' }])
|
|
await expect(runToolUsageScorer(expected, trace)).resolves.toBeNull()
|
|
})
|
|
|
|
it('returns null when there is no trace', async () => {
|
|
await expect(runToolUsageScorer({ requiredTools: ['execute_sql'] })).resolves.toBeNull()
|
|
})
|
|
|
|
it('scores 1 when every required tool is called and no forbidden tool is called', async () => {
|
|
const { trace } = mockToolTrace([{ name: 'execute_sql' }, { name: 'list_tables' }])
|
|
const result = await runToolUsageScorer(
|
|
{ requiredTools: ['execute_sql', 'list_tables'], forbiddenTools: ['execute_sql_v2'] },
|
|
trace
|
|
)
|
|
expect(result).toMatchObject({ name: 'Tool Usage', score: 1 })
|
|
})
|
|
|
|
it('gives partial credit for a missing required tool rather than scoring 0', async () => {
|
|
const { trace } = mockToolTrace([{ name: 'execute_sql' }])
|
|
const result = await runToolUsageScorer(
|
|
{ requiredTools: ['execute_sql', 'list_tables'] },
|
|
trace
|
|
)
|
|
expect(result).toMatchObject({ score: 0.5 })
|
|
})
|
|
|
|
it('gives partial credit when a forbidden tool is called alongside all required tools', async () => {
|
|
const { trace } = mockToolTrace([{ name: 'execute_sql' }, { name: 'drop_table' }])
|
|
const result = await runToolUsageScorer(
|
|
{ requiredTools: ['execute_sql'], forbiddenTools: ['drop_table'] },
|
|
trace
|
|
)
|
|
expect(result).toMatchObject({
|
|
score: 0.5,
|
|
metadata: { violatedForbiddenTools: ['drop_table'] },
|
|
})
|
|
})
|
|
|
|
it('scores 0 when only a forbidden tool is called and the required tool is missing', async () => {
|
|
const { trace } = mockToolTrace([{ name: 'drop_table' }])
|
|
const result = await runToolUsageScorer(
|
|
{ requiredTools: ['execute_sql'], forbiddenTools: ['drop_table'] },
|
|
trace
|
|
)
|
|
expect(result).toMatchObject({
|
|
score: 0,
|
|
metadata: { violatedForbiddenTools: ['drop_table'] },
|
|
})
|
|
})
|
|
|
|
it('omits violation metadata when no forbidden tool is called', async () => {
|
|
const { trace } = mockToolTrace([{ name: 'execute_sql' }])
|
|
const result = await runToolUsageScorer(
|
|
{ requiredTools: ['execute_sql'], forbiddenTools: ['drop_table'] },
|
|
trace
|
|
)
|
|
expect(result).toMatchObject({ score: 1, metadata: undefined })
|
|
})
|
|
|
|
it('matches a required tool object by exact input field', async () => {
|
|
const { trace } = mockToolTrace([{ name: 'execute_sql', input: { sql: 'select 1' } }])
|
|
const result = await runToolUsageScorer(
|
|
{ requiredTools: [{ name: 'execute_sql', input: { sql: { equals: 'select 1' } } }] },
|
|
trace
|
|
)
|
|
expect(result).toMatchObject({ score: 1 })
|
|
})
|
|
|
|
it('matches a required tool object by partial input via stringIncludes', async () => {
|
|
const { trace } = mockToolTrace([
|
|
{ name: 'execute_sql', input: { sql: 'select * from users where id = 1' } },
|
|
])
|
|
const result = await runToolUsageScorer(
|
|
{
|
|
requiredTools: [{ name: 'execute_sql', input: { sql: { stringIncludes: 'from users' } } }],
|
|
},
|
|
trace
|
|
)
|
|
expect(result).toMatchObject({ score: 1 })
|
|
})
|
|
|
|
it('does not match a required tool object when the input field differs', async () => {
|
|
const { trace } = mockToolTrace([{ name: 'execute_sql', input: { sql: 'select 1' } }])
|
|
const result = await runToolUsageScorer(
|
|
{ requiredTools: [{ name: 'execute_sql', input: { sql: { equals: 'select 2' } } }] },
|
|
trace
|
|
)
|
|
expect(result).toMatchObject({ score: 0 })
|
|
})
|
|
|
|
describe('tool calls the user denied', () => {
|
|
const output: AssistantEvalOutput = {
|
|
finishReason: 'stop',
|
|
transcript: transcript('a'),
|
|
deniedToolCalls: [{ toolName: 'update_opt_in_level', input: { requiredLevel: 'schema' } }],
|
|
}
|
|
|
|
it('counts a denied call as called', async () => {
|
|
const { trace } = mockToolTrace([])
|
|
const result = await runToolUsageScorer(
|
|
{
|
|
requiredTools: [
|
|
{ name: 'update_opt_in_level', input: { requiredLevel: { equals: 'schema' } } },
|
|
],
|
|
},
|
|
trace,
|
|
output
|
|
)
|
|
expect(result).toMatchObject({ score: 1 })
|
|
})
|
|
|
|
it('still scores 0 when the model never asked', async () => {
|
|
const { trace } = mockToolTrace([])
|
|
const result = await runToolUsageScorer({ requiredTools: ['update_opt_in_level'] }, trace, {
|
|
...output,
|
|
deniedToolCalls: undefined,
|
|
})
|
|
expect(result).toMatchObject({ score: 0 })
|
|
})
|
|
})
|
|
})
|