mirror of
https://github.com/supabase/supabase.git
synced 2026-09-22 13:37:53 +08:00
c37e756983
## Context Currently in the SQL Editor, toggling "favourite" for a snippet doesn't persist unless you manually save the snippet (which expects a change in the snippet's content before allowing so) - which is a bit of an odd UX This used to work before we introduced manual saving which is currently the default behaviour for the SQL Editor - `addFavorite` and `removeFavorite` would add to the `needsSaving` queue which the editor's save scheduler will subscribe and trigger the save. However the save scheduler doesn't subscribe to the queue for manual saving mode ([ref](https://github.com/supabase/supabase/blob/master/apps/studio/state/sql-editor/sql-editor-save-scheduler.ts#L90)) - hence toggling favourite on a snippet never triggers a PATCH request. Am opting to immediately trigger a PATCH request when toggling favourites which is a bit more of an expected UX imo One thing to note is that favoriting a snippet essentially does a save on the snippet - which means the contents will be persisted as well, although i think this is alright ## To test - [ ] Verify that toggling favourite for a SQL snippet persists immediately - Can verify by checking the context menu CTA to see if it's changed from "Add to favourites" to "Remove from favourites" or vice versa <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **Improvements** - Favoriting or unfavoriting SQL snippets now saves immediately. - Favorite changes are handled consistently across the SQL Editor, including the utility panel and snippet navigation. - Pending content saves are coordinated to prevent favorite changes from being overwritten. - If saving a favorite fails, the previous favorite state is restored and an error notification is shown. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
350 lines
12 KiB
TypeScript
350 lines
12 KiB
TypeScript
import { untrustedSql } from '@supabase/pg-meta'
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
import { createSaveMechanism, type SaveMechanismStore } from './sql-editor-save'
|
|
import type { StateSnippet } from './types'
|
|
import type { SnippetFolder } from '@/data/content/sql-folders-query'
|
|
import type { SqlSnippets } from '@/types'
|
|
|
|
function makeStateSnippet(
|
|
overrides: Omit<Partial<StateSnippet['snippet']>, 'type' | 'content'> & {
|
|
content?: SqlSnippets.Content
|
|
} = {}
|
|
): StateSnippet {
|
|
return {
|
|
projectRef: 'ref',
|
|
splitSizes: [50, 50],
|
|
snippet: {
|
|
id: 'snippet-1',
|
|
type: 'sql',
|
|
name: 'Query',
|
|
description: '',
|
|
visibility: 'user',
|
|
project_id: 1,
|
|
owner_id: 1,
|
|
folder_id: null,
|
|
favorite: false,
|
|
inserted_at: '2024-01-01T00:00:00.000Z',
|
|
updated_at: '2024-01-01T00:00:00.000Z',
|
|
status: 'new',
|
|
content: {
|
|
content_id: 'snippet-1',
|
|
schema_version: '1',
|
|
unchecked_sql: untrustedSql('select 1'),
|
|
},
|
|
...overrides,
|
|
},
|
|
}
|
|
}
|
|
|
|
function makeFolder(id: string, name: string): SnippetFolder {
|
|
return { id, name, owner_id: 1, project_id: 1, parent_id: null }
|
|
}
|
|
|
|
function setup(initial?: Partial<SaveMechanismStore>) {
|
|
const state: SaveMechanismStore = {
|
|
snippets: {},
|
|
folders: {},
|
|
removeFolder: vi.fn((id: string) => {
|
|
delete state.folders[id]
|
|
}),
|
|
...initial,
|
|
}
|
|
const upsertContent = vi.fn(async () => null)
|
|
const createSQLSnippetFolder = vi.fn(async () => makeFolder('real-id', 'Folder'))
|
|
const updateSQLSnippetFolder = vi.fn(async () => null)
|
|
const invalidate = vi.fn(async () => {})
|
|
const notify = { success: vi.fn(), error: vi.fn() }
|
|
|
|
const mechanism = createSaveMechanism({
|
|
state,
|
|
upsertContent,
|
|
createSQLSnippetFolder,
|
|
updateSQLSnippetFolder,
|
|
invalidate,
|
|
notify,
|
|
debounceMs: 1000,
|
|
})
|
|
|
|
return {
|
|
state,
|
|
upsertContent,
|
|
createSQLSnippetFolder,
|
|
updateSQLSnippetFolder,
|
|
invalidate,
|
|
notify,
|
|
mechanism,
|
|
}
|
|
}
|
|
|
|
describe('save mechanism — saveSnippet', () => {
|
|
beforeEach(() => vi.useFakeTimers())
|
|
afterEach(() => vi.useRealTimers())
|
|
|
|
it('debounces and persists a loaded snippet, transitioning new → saving → saved', async () => {
|
|
const t = setup({ snippets: { 'snippet-1': makeStateSnippet() } })
|
|
|
|
t.mechanism.saveSnippet({ id: 'snippet-1', projectRef: 'ref', shouldInvalidate: false })
|
|
expect(t.upsertContent).not.toHaveBeenCalled() // debounced, not yet fired
|
|
|
|
await vi.runAllTimersAsync()
|
|
|
|
expect(t.upsertContent).toHaveBeenCalledTimes(1)
|
|
expect(t.upsertContent).toHaveBeenCalledWith({
|
|
projectRef: 'ref',
|
|
payload: expect.objectContaining({ id: 'snippet-1', type: 'sql' }),
|
|
})
|
|
expect(t.state.snippets['snippet-1']!.snippet.status).toBe('saved')
|
|
})
|
|
|
|
it('does NOT call upsertContent for a snippet whose content is not loaded', async () => {
|
|
const snippet = makeStateSnippet({ content: undefined })
|
|
const t = setup({ snippets: { 'snippet-1': snippet } })
|
|
|
|
t.mechanism.saveSnippet({ id: 'snippet-1', projectRef: 'ref', shouldInvalidate: false })
|
|
await vi.runAllTimersAsync()
|
|
|
|
expect(t.upsertContent).not.toHaveBeenCalled()
|
|
// status is left untouched — nothing was persisted
|
|
expect(t.state.snippets['snippet-1']!.snippet.status).toBe('new')
|
|
})
|
|
|
|
it('invalidates the lists only when shouldInvalidate is true', async () => {
|
|
const t = setup({ snippets: { 'snippet-1': makeStateSnippet() } })
|
|
|
|
t.mechanism.saveSnippet({ id: 'snippet-1', projectRef: 'ref', shouldInvalidate: false })
|
|
await vi.runAllTimersAsync()
|
|
expect(t.invalidate).not.toHaveBeenCalled()
|
|
|
|
t.mechanism.saveSnippet({ id: 'snippet-1', projectRef: 'ref', shouldInvalidate: true })
|
|
await vi.runAllTimersAsync()
|
|
expect(t.invalidate).toHaveBeenCalledWith('ref')
|
|
})
|
|
|
|
it('transitions to save_failed when the upsert rejects', async () => {
|
|
const t = setup({ snippets: { 'snippet-1': makeStateSnippet({ status: 'saved' }) } })
|
|
t.upsertContent.mockRejectedValueOnce(new Error('network'))
|
|
|
|
t.mechanism.saveSnippet({ id: 'snippet-1', projectRef: 'ref', shouldInvalidate: false })
|
|
await vi.runAllTimersAsync()
|
|
|
|
expect(t.state.snippets['snippet-1']!.snippet.status).toBe('save_failed')
|
|
})
|
|
|
|
it('debounces per snippet id — distinct snippets save independently', async () => {
|
|
const t = setup({
|
|
snippets: {
|
|
'snippet-1': makeStateSnippet({ id: 'snippet-1' }),
|
|
'snippet-2': makeStateSnippet({ id: 'snippet-2' }),
|
|
},
|
|
})
|
|
|
|
t.mechanism.saveSnippet({ id: 'snippet-1', projectRef: 'ref', shouldInvalidate: false })
|
|
t.mechanism.saveSnippet({ id: 'snippet-2', projectRef: 'ref', shouldInvalidate: false })
|
|
await vi.runAllTimersAsync()
|
|
|
|
expect(t.upsertContent).toHaveBeenCalledTimes(2)
|
|
})
|
|
|
|
it('coalesces rapid saves of the same snippet into one persist', async () => {
|
|
const t = setup({ snippets: { 'snippet-1': makeStateSnippet() } })
|
|
|
|
t.mechanism.saveSnippet({ id: 'snippet-1', projectRef: 'ref', shouldInvalidate: false })
|
|
t.mechanism.saveSnippet({ id: 'snippet-1', projectRef: 'ref', shouldInvalidate: false })
|
|
t.mechanism.saveSnippet({ id: 'snippet-1', projectRef: 'ref', shouldInvalidate: false })
|
|
await vi.runAllTimersAsync()
|
|
|
|
expect(t.upsertContent).toHaveBeenCalledTimes(1)
|
|
})
|
|
})
|
|
|
|
describe('save mechanism — saveFavorite', () => {
|
|
it('persists the snippet immediately, without debouncing', async () => {
|
|
const t = setup({ snippets: { 'snippet-1': makeStateSnippet({ favorite: true }) } })
|
|
|
|
await t.mechanism.saveFavorite({ id: 'snippet-1', projectRef: 'ref', previousFavorite: false })
|
|
|
|
expect(t.upsertContent).toHaveBeenCalledTimes(1)
|
|
expect(t.upsertContent).toHaveBeenCalledWith({
|
|
projectRef: 'ref',
|
|
payload: expect.objectContaining({ id: 'snippet-1', favorite: true }),
|
|
})
|
|
expect(t.state.snippets['snippet-1']!.snippet.status).toBe('saved')
|
|
})
|
|
|
|
it('cancels a pending debounced content save for the same id', async () => {
|
|
vi.useFakeTimers()
|
|
const t = setup({ snippets: { 'snippet-1': makeStateSnippet({ favorite: true }) } })
|
|
|
|
t.mechanism.saveSnippet({ id: 'snippet-1', projectRef: 'ref', shouldInvalidate: false })
|
|
await t.mechanism.saveFavorite({ id: 'snippet-1', projectRef: 'ref', previousFavorite: false })
|
|
await vi.runAllTimersAsync()
|
|
|
|
// Only the immediate favorite save fired — the debounced one never ran.
|
|
expect(t.upsertContent).toHaveBeenCalledTimes(1)
|
|
vi.useRealTimers()
|
|
})
|
|
|
|
it('invalidates the lists on a successful save, so the Favorites nav section refetches', async () => {
|
|
const t = setup({ snippets: { 'snippet-1': makeStateSnippet({ favorite: true }) } })
|
|
|
|
await t.mechanism.saveFavorite({ id: 'snippet-1', projectRef: 'ref', previousFavorite: false })
|
|
|
|
expect(t.invalidate).toHaveBeenCalledWith('ref')
|
|
})
|
|
|
|
it('rolls back to previousFavorite, notifies, and does NOT invalidate when the save fails', async () => {
|
|
const t = setup({
|
|
snippets: { 'snippet-1': makeStateSnippet({ favorite: true, status: 'saved' }) },
|
|
})
|
|
t.upsertContent.mockRejectedValueOnce(new Error('network'))
|
|
|
|
await t.mechanism.saveFavorite({ id: 'snippet-1', projectRef: 'ref', previousFavorite: false })
|
|
|
|
expect(t.state.snippets['snippet-1']!.snippet.favorite).toBe(false)
|
|
expect(t.state.snippets['snippet-1']!.snippet.status).toBe('save_failed')
|
|
expect(t.notify.error).toHaveBeenCalled()
|
|
expect(t.invalidate).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('does NOT call upsertContent for a snippet whose content is not loaded', async () => {
|
|
const snippet = makeStateSnippet({ content: undefined })
|
|
const t = setup({ snippets: { 'snippet-1': snippet } })
|
|
|
|
await t.mechanism.saveFavorite({ id: 'snippet-1', projectRef: 'ref', previousFavorite: false })
|
|
|
|
expect(t.upsertContent).not.toHaveBeenCalled()
|
|
})
|
|
})
|
|
|
|
describe('save mechanism — createFolder', () => {
|
|
it('persists a new folder and swaps the local placeholder for it', async () => {
|
|
const t = setup({
|
|
folders: {
|
|
'local-1': {
|
|
projectRef: 'ref',
|
|
status: 'new_editing',
|
|
folder: makeFolder('local-1', ''),
|
|
},
|
|
},
|
|
})
|
|
|
|
await t.mechanism.createFolder({
|
|
projectRef: 'ref',
|
|
name: 'My Folder',
|
|
placeholderId: 'local-1',
|
|
})
|
|
|
|
expect(t.createSQLSnippetFolder).toHaveBeenCalledWith({ projectRef: 'ref', name: 'My Folder' })
|
|
expect(t.state.removeFolder).toHaveBeenCalledWith('local-1')
|
|
expect(t.state.folders['real-id']).toMatchObject({ status: 'idle', folder: { id: 'real-id' } })
|
|
expect(t.notify.success).toHaveBeenCalled()
|
|
})
|
|
|
|
it('rolls back the placeholder when creation fails', async () => {
|
|
const t = setup({
|
|
folders: {
|
|
'local-1': {
|
|
projectRef: 'ref',
|
|
status: 'new_saving',
|
|
folder: makeFolder('local-1', ''),
|
|
},
|
|
},
|
|
})
|
|
t.createSQLSnippetFolder.mockRejectedValueOnce(new Error('boom'))
|
|
|
|
await t.mechanism.createFolder({
|
|
projectRef: 'ref',
|
|
name: 'My Folder',
|
|
placeholderId: 'local-1',
|
|
})
|
|
|
|
expect(t.state.removeFolder).toHaveBeenCalledWith('local-1')
|
|
expect(t.notify.error).toHaveBeenCalled()
|
|
})
|
|
})
|
|
|
|
describe('save mechanism — updateFolder', () => {
|
|
it('renames an existing folder and resets its status to idle', async () => {
|
|
const t = setup({
|
|
folders: { f1: { projectRef: 'ref', status: 'saving', folder: makeFolder('f1', 'Old') } },
|
|
})
|
|
|
|
await t.mechanism.updateFolder({ id: 'f1', projectRef: 'ref', name: 'New' })
|
|
|
|
expect(t.updateSQLSnippetFolder).toHaveBeenCalledWith({
|
|
projectRef: 'ref',
|
|
id: 'f1',
|
|
name: 'New',
|
|
})
|
|
expect(t.state.folders['f1']!.status).toBe('idle')
|
|
expect(t.notify.success).toHaveBeenCalled()
|
|
})
|
|
|
|
it("rolls the name back to the folder's own previousName when the rename fails", async () => {
|
|
const t = setup({
|
|
folders: {
|
|
f1: {
|
|
projectRef: 'ref',
|
|
status: 'saving',
|
|
folder: makeFolder('f1', 'New'),
|
|
previousName: 'Old',
|
|
},
|
|
},
|
|
})
|
|
t.updateSQLSnippetFolder.mockRejectedValueOnce(new Error('boom'))
|
|
|
|
await t.mechanism.updateFolder({ id: 'f1', projectRef: 'ref', name: 'New' })
|
|
|
|
expect(t.state.folders['f1']!.folder.name).toBe('Old')
|
|
expect(t.state.folders['f1']!.status).toBe('idle')
|
|
expect(t.state.folders['f1']!.previousName).toBeUndefined()
|
|
})
|
|
|
|
it('clears previousName on a successful rename', async () => {
|
|
const t = setup({
|
|
folders: {
|
|
f1: {
|
|
projectRef: 'ref',
|
|
status: 'saving',
|
|
folder: makeFolder('f1', 'New'),
|
|
previousName: 'Old',
|
|
},
|
|
},
|
|
})
|
|
|
|
await t.mechanism.updateFolder({ id: 'f1', projectRef: 'ref', name: 'New' })
|
|
|
|
expect(t.state.folders['f1']!.previousName).toBeUndefined()
|
|
})
|
|
|
|
it('rolls back each folder to its own previousName when concurrent renames fail', async () => {
|
|
const t = setup({
|
|
folders: {
|
|
a: {
|
|
projectRef: 'ref',
|
|
status: 'saving',
|
|
folder: makeFolder('a', 'A-new'),
|
|
previousName: 'A-old',
|
|
},
|
|
b: {
|
|
projectRef: 'ref',
|
|
status: 'saving',
|
|
folder: makeFolder('b', 'B-new'),
|
|
previousName: 'B-old',
|
|
},
|
|
},
|
|
})
|
|
t.updateSQLSnippetFolder.mockRejectedValue(new Error('boom'))
|
|
|
|
// Both renames in flight at once; each must restore its own previous name.
|
|
await Promise.all([
|
|
t.mechanism.updateFolder({ id: 'a', projectRef: 'ref', name: 'A-new' }),
|
|
t.mechanism.updateFolder({ id: 'b', projectRef: 'ref', name: 'B-new' }),
|
|
])
|
|
|
|
expect(t.state.folders['a']!.folder.name).toBe('A-old')
|
|
expect(t.state.folders['b']!.folder.name).toBe('B-old')
|
|
})
|
|
})
|