Files
supabase/apps/studio/lib/ai/test-fixtures.ts
CharisandJoshen Lim 40d9a2e929 fix(studio): invalidate notebook cache after assistant delete (#49496)
Stacked on supabase/supabase#49415.

## Summary

- collect completed assistant delete_notebook tool calls as deleted
cache effects
- evict deleted notebooks from the React Query cache and notebook state
- cover collector, cache eviction, and the open-tab Notebook not found
state

## Verification

- pnpm test:studio -- notebook-cache-invalidation
ExplorerNotebookTab.assistant-cache-invalidation
- pnpm --filter studio typecheck
- touched-file ESLint and Prettier checks

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

## Summary by CodeRabbit

* **Bug Fixes**
* Improved notebook deletion handling so removed notebooks no longer
remain visible after deletion.
* Navigation and cached notebook data now update promptly when a
notebook is deleted.
* Attempting to access a deleted notebook now displays a clear “Notebook
not found” message.

* **Tests**
* Added coverage for notebook deletion, cache invalidation, navigation
updates, and the resulting not-found state.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
2026-08-25 14:41:56 +08:00

182 lines
4.4 KiB
TypeScript

import type { ToolUIPart, UIMessage } from 'ai'
export function createUserMessage(content: string, id = 'user-msg-1'): UIMessage {
return {
id,
role: 'user',
parts: [
{
type: 'text',
text: content,
},
],
}
}
export function createAssistantTextMessage(content: string, id = 'assistant-msg-1'): UIMessage {
return {
id,
role: 'assistant',
parts: [
{
type: 'text',
text: content,
},
],
}
}
export function createAssistantMessageWithExecuteSqlTool(
query: string,
results: Array<Record<string, any>> = [{ id: 1, name: 'test' }],
id = 'assistant-tool-msg-1'
): UIMessage {
return {
id,
role: 'assistant',
parts: [
{
type: 'text',
text: "I'll run that SQL query for you.",
},
{
type: 'tool-execute_sql',
state: 'output-available',
toolCallId: 'call-123',
input: { sql: query },
output: results,
} satisfies ToolUIPart,
],
}
}
export function createAssistantMessageWithUpdateNotebookTool(
output: Record<string, unknown> = {
id: 'notebook-1',
name: 'Signup funnel',
previous_content: { schema_version: 1, cells: [] },
},
id = 'assistant-notebook-msg-1'
): UIMessage {
return {
id,
role: 'assistant',
parts: [
{
type: 'tool-update_notebook',
state: 'output-available',
toolCallId: 'call-notebook-1',
input: { id: 'notebook-1', expected_updated_at: '2026-01-01T00:00:00.000Z' },
output,
} satisfies ToolUIPart,
],
}
}
export function createAssistantMessageWithCreateNotebookTool(
output: Record<string, unknown> = {
id: 'notebook-1',
name: 'Signup funnel',
},
id = 'assistant-notebook-msg-1'
): UIMessage {
return {
id,
role: 'assistant',
parts: [
{
type: 'tool-create_notebook',
state: 'output-available',
toolCallId: 'call-notebook-1',
input: { name: 'Signup funnel', content: { schema_version: 1, cells: [] } },
output,
} satisfies ToolUIPart,
],
}
}
export function createAssistantMessageWithDeleteNotebookTool(
output: Record<string, unknown> = {
id: 'notebook-1',
name: 'Signup funnel',
},
id = 'assistant-notebook-msg-1'
): UIMessage {
return {
id,
role: 'assistant',
parts: [
{
type: 'tool-delete_notebook',
state: 'output-available',
toolCallId: 'call-notebook-1',
input: { id: 'notebook-1' },
output,
} satisfies ToolUIPart,
],
}
}
export function createAssistantMessageWithMultipleTools(
id = 'assistant-multi-tool-msg-1'
): UIMessage {
return {
id,
role: 'assistant',
parts: [
{
type: 'text',
text: 'Let me check the database structure and run some queries.',
},
{
type: 'tool-execute_sql',
state: 'output-available',
toolCallId: 'call-456',
input: { sql: 'SELECT * FROM users LIMIT 5' },
output: [
{ id: 1, email: 'user1@example.com' },
{ id: 2, email: 'user2@example.com' },
],
} satisfies ToolUIPart,
{
type: 'tool-execute_sql',
state: 'output-available',
toolCallId: 'call-789',
toolName: 'execute_sql',
input: { sql: 'DESCRIBE users' },
output: [
{ column: 'id', type: 'integer', nullable: false },
{ column: 'email', type: 'varchar', nullable: false },
],
} as ToolUIPart,
],
}
}
export function createLongConversation(): Array<UIMessage> {
return [
createUserMessage('Show me all users', 'msg-1'),
createAssistantMessageWithExecuteSqlTool('SELECT * FROM users', [{ id: 1 }], 'msg-2'),
createUserMessage('How many users are there?', 'msg-3'),
createAssistantMessageWithExecuteSqlTool(
'SELECT COUNT(*) FROM users',
[{ count: 100 }],
'msg-4'
),
createUserMessage('Show me the schema', 'msg-5'),
createAssistantTextMessage("Here's the database schema...", 'msg-6'),
createUserMessage('Create a new table', 'msg-7'),
createAssistantMessageWithExecuteSqlTool(
'CREATE TABLE posts (id SERIAL PRIMARY KEY)',
[],
'msg-8'
),
createUserMessage('Add some data', 'msg-9'),
createAssistantMessageWithExecuteSqlTool(
"INSERT INTO posts (title) VALUES ('Test')",
[],
'msg-10'
),
]
}