Files
supabase/apps/studio/data/storage/versioning/archived-object-names-query.test.ts
Francesco Sansalvadore 25708ed67a chore(studio): storage versioning override conflict (#51593)
## Problem

When uploading a file with same name of already existing file, the ui
automatically creates a new file with incremental number and doesn't
consider the scenario of giving the possibility of storing the new
upload as a version of the already existing file.

## Solution

Provide modal to chose the behaviour when name conflict happens when
uploading a file:
- Replace existing file (same file with version history)
- Keep both (new file with incremental numbering)


https://github.com/user-attachments/assets/ee192be9-4087-4e14-89f1-1c82acf550cd

Manage edge case of clashes with files created when bucket had
versioning enabled (so with `is_versioned` flag to true) and clashing
upload happens when bucket is in SUSPENDED state.


https://github.com/user-attachments/assets/00694c77-07c1-405e-bd54-bdf0bf99d274

Unrelated fix:
- add "Delete permanently" action when selecting multiple files with
versioning enabled

<img width="510" height="203" alt="Screenshot 2026-10-13 at 15 59 27"
src="https://github.com/user-attachments/assets/5692fc23-2fd3-4259-9de2-3932aecbccd0"
/>
2026-10-13 17:14:30 +02:00

96 lines
3.1 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import { toArchivedFileNames } from './archived-object-names-query'
import type { components } from '@/data/api'
type StorageObjectV2 = components['schemas']['StorageListResponseV2_Output']['objects'][number]
const makeRow = (
name: string,
{
version,
createdAt,
archivedAt,
isDeleteMarker = false,
}: {
version: string
createdAt: string
archivedAt?: string
isDeleteMarker?: boolean
}
): StorageObjectV2 => ({
id: `id-${name}-${version}`,
name,
version,
created_at: createdAt,
updated_at: createdAt,
last_accessed_at: createdAt,
archived_at: archivedAt ?? null,
is_delete_marker: isDeleteMarker,
metadata: isDeleteMarker ? null : { size: 10, mimetype: 'image/svg+xml' },
})
/** A delete marker on top of the version it hid: what archiving a file leaves behind. */
const archivedRows = (name: string) => [
makeRow(name, { version: 'v-marker', createdAt: '2024-02-01T00:00:00Z', isDeleteMarker: true }),
makeRow(name, {
version: 'v1',
createdAt: '2024-01-01T00:00:00Z',
archivedAt: '2024-02-01T00:00:00Z',
}),
]
describe('toArchivedFileNames', () => {
it('takes the leaf, since v2 returns the full key from the bucket root', () => {
expect(toArchivedFileNames(archivedRows('folder/nested/icon-1.svg'))).toEqual(['icon-1.svg'])
})
it('leaves a root-level name alone', () => {
expect(toArchivedFileNames(archivedRows('icon-1.svg'))).toEqual(['icon-1.svg'])
})
it('ignores a live file, which has no delete marker on top', () => {
expect(
toArchivedFileNames([
makeRow('photo.png', { version: 'v1', createdAt: '2024-01-01T00:00:00Z' }),
])
).toEqual([])
})
it('ignores a delete marker left behind by a permanent delete', () => {
// Nothing is retained underneath, so there is nothing to restore and the explorer does
// not list it either. Prompting here would name a file the user cannot find anywhere.
expect(
toArchivedFileNames([
makeRow('app icon-1.svg', {
version: 'v-marker',
createdAt: '2024-02-01T00:00:00Z',
isDeleteMarker: true,
}),
])
).toEqual([])
})
it('drops the empty folder placeholder, which is not a file anyone uploads onto', () => {
expect(toArchivedFileNames(archivedRows('folder/.emptyFolderPlaceholder'))).toEqual([])
})
it('reports a leaf once even when two folders both hold it archived', () => {
expect(
toArchivedFileNames([...archivedRows('a/icon.svg'), ...archivedRows('b/icon.svg')])
).toEqual(['icon.svg'])
})
it('discards the trailing path when the page was cut short', () => {
// Its rows are a fragment, and a fragment classifies wrongly either way.
const rows = [...archivedRows('a/first.svg'), ...archivedRows('b/second.svg')]
expect(toArchivedFileNames(rows, true)).toEqual(['first.svg'])
expect(toArchivedFileNames(rows, false)).toEqual(['first.svg', 'second.svg'])
})
it('handles an absent list', () => {
expect(toArchivedFileNames()).toEqual([])
expect(toArchivedFileNames([])).toEqual([])
})
})