Files
supabase/apps/studio/lib/ai/message-utils.test.ts
Joshen Lim 60b51d74d5 Audit existing unit tests + Remove redundant ones (#52215)
## 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
2026-10-23 18:51:27 +08:00

192 lines
5.8 KiB
TypeScript

import type { DynamicToolUIPart, UIMessage } from 'ai'
import { describe, expect, it } from 'vitest'
import {
getParallelApprovalIdsToReject,
isManualApprovalRequested,
prepareMessagesForAPI,
} from './message-utils'
const makeApprovalPart = (id: string, isAutomatic = false): DynamicToolUIPart =>
({
type: 'dynamic-tool',
toolName: 'test_tool',
toolCallId: id,
state: 'approval-requested',
input: {},
approval: { id, ...(isAutomatic ? { isAutomatic: true } : {}) },
}) as DynamicToolUIPart
const makeResultPart = (id: string): DynamicToolUIPart => ({
type: 'dynamic-tool',
toolName: 'test_tool',
toolCallId: id,
state: 'output-available',
input: {},
output: {},
})
describe('isManualApprovalRequested', () => {
it('returns true for a human approval-requested tool part', () => {
expect(isManualApprovalRequested(makeApprovalPart('a1'))).toBe(true)
})
it('returns false for an automatic approval', () => {
expect(isManualApprovalRequested(makeApprovalPart('a1', true))).toBe(false)
})
it('returns false for a tool result part', () => {
expect(isManualApprovalRequested(makeResultPart('r1'))).toBe(false)
})
it('returns false for a content part with no state or approval', () => {
expect(isManualApprovalRequested({ type: 'text', text: 'hello' })).toBe(false)
})
})
describe('getParallelApprovalIdsToReject', () => {
it('returns [] for empty messages', () => {
expect(getParallelApprovalIdsToReject([])).toEqual([])
})
it('returns [] when there are no assistant messages', () => {
const messages: UIMessage[] = [{ id: '1', role: 'user', parts: [] }]
expect(getParallelApprovalIdsToReject(messages)).toEqual([])
})
it('returns [] when last assistant message has no pending approvals', () => {
const messages: UIMessage[] = [{ id: '1', role: 'assistant', parts: [makeResultPart('r1')] }]
expect(getParallelApprovalIdsToReject(messages)).toEqual([])
})
it('returns [] when there is only one pending approval', () => {
const messages: UIMessage[] = [{ id: '1', role: 'assistant', parts: [makeApprovalPart('a1')] }]
expect(getParallelApprovalIdsToReject(messages)).toEqual([])
})
it('returns all but the first id when there are multiple pending approvals', () => {
const messages: UIMessage[] = [
{
id: '1',
role: 'assistant',
parts: [makeApprovalPart('a1'), makeApprovalPart('a2'), makeApprovalPart('a3')],
},
]
expect(getParallelApprovalIdsToReject(messages)).toEqual(['a2', 'a3'])
})
it('only inspects the last assistant message', () => {
const messages: UIMessage[] = [
{
id: '1',
role: 'assistant',
parts: [makeApprovalPart('old1'), makeApprovalPart('old2')],
},
{ id: '2', role: 'user', parts: [] },
{ id: '3', role: 'assistant', parts: [makeApprovalPart('new1')] },
]
expect(getParallelApprovalIdsToReject(messages)).toEqual([])
})
it('ignores non-approval tool parts', () => {
const messages: UIMessage[] = [
{
id: '1',
role: 'assistant',
parts: [makeResultPart('r1'), makeApprovalPart('a1'), makeApprovalPart('a2')],
},
]
expect(getParallelApprovalIdsToReject(messages)).toEqual(['a2'])
})
it('ignores automatic approvals when picking extras to reject', () => {
const messages: UIMessage[] = [
{
id: '1',
role: 'assistant',
parts: [
makeApprovalPart('auto', true),
makeApprovalPart('manual-1'),
makeApprovalPart('manual-2'),
],
},
]
expect(getParallelApprovalIdsToReject(messages)).toEqual(['manual-2'])
})
})
describe('prepareMessagesForAPI', () => {
it('should limit messages to last 7 entries', () => {
const messages: UIMessage[] = Array.from({ length: 10 }, (_, i) => ({
id: `msg-${i}`,
role: 'user',
parts: [{ type: 'text', text: `Message ${i}` }],
}))
const result = prepareMessagesForAPI(messages)
expect(result).toHaveLength(7)
expect(result[0].parts[0]).toEqual({ type: 'text', text: 'Message 3' })
expect(result[6].parts[0]).toEqual({ type: 'text', text: 'Message 9' })
})
it('should preserve messages without results', () => {
const messages: UIMessage[] = [
{
id: 'msg-1',
role: 'user',
parts: [{ type: 'text', text: 'Question' }],
},
{
id: 'msg-2',
role: 'assistant',
parts: [{ type: 'text', text: 'Answer' }],
},
]
const result = prepareMessagesForAPI(messages)
expect(result).toHaveLength(2)
expect(result[0]).toEqual(messages[0])
expect(result[1]).toEqual(messages[1])
})
it('should handle array with exactly 7 messages', () => {
const messages: UIMessage[] = Array.from({ length: 7 }, (_, i) => ({
id: `msg-${i}`,
role: i % 2 === 0 ? 'user' : 'assistant',
parts: [{ type: 'text', text: `Message ${i}` }],
}))
const result = prepareMessagesForAPI(messages)
expect(result).toHaveLength(7)
expect(result).toEqual(messages)
})
it('should only remove results from assistant messages, not user messages', () => {
const messages = [
{
id: 'msg-1',
role: 'user',
parts: [{ type: 'text', text: 'Question' }],
results: { data: 'user data' },
},
{
id: 'msg-2',
role: 'assistant',
parts: [{ type: 'text', text: 'Answer' }],
results: { data: 'assistant data' },
},
] as Array<UIMessage & { results?: unknown }>
const result = prepareMessagesForAPI(messages)
expect(result).toHaveLength(2)
// User message keeps results (not removed by the function)
expect((result[0] as any).results).toEqual({ data: 'user data' })
// Assistant message has results removed
expect(result[1]).not.toHaveProperty('results')
})
})