openpencil/tests/engine/app/storage/unique-name.test.ts
Danila Poyarkov 56a3b77ac5 feat(app): add local-first storage substrate
- Recover the IndexedDB and in-memory document cache behind the storage domain

- Add persistent outbox supersession, cache eviction, import validation, and naming helpers

- Keep the substrate provider-neutral and cover its pure behavior

Co-authored-by: Rob Coenen <753704+rcoenen@users.noreply.github.com>
2026-07-26 14:30:39 +03:00

36 lines
1.4 KiB
TypeScript

import { describe, expect, test } from 'bun:test'
import { nextUniqueStorageName } from '@/app/storage/unique-name'
describe('nextUniqueStorageName', () => {
test('returns desired name when free', () => {
expect(nextUniqueStorageName('KonversioDesigns', [])).toBe('KonversioDesigns')
expect(nextUniqueStorageName('KonversioDesigns', ['Other'])).toBe('KonversioDesigns')
})
test('appends (1), (2), … when name is taken', () => {
expect(nextUniqueStorageName('File', ['File'])).toBe('File (1)')
expect(nextUniqueStorageName('File', ['File', 'File (1)'])).toBe('File (2)')
expect(nextUniqueStorageName('File', ['File', 'File (1)', 'File (2)'])).toBe('File (3)')
})
test('skips gaps and picks the first free number', () => {
expect(nextUniqueStorageName('File', ['File', 'File (2)'])).toBe('File (1)')
})
test('trims whitespace and falls back to Untitled', () => {
expect(nextUniqueStorageName(' Draft ', ['Draft'])).toBe('Draft (1)')
expect(nextUniqueStorageName(' ', ['Untitled'])).toBe('Untitled (1)')
expect(nextUniqueStorageName('', [])).toBe('Untitled')
})
test('ignores blank taken names', () => {
expect(nextUniqueStorageName('A', ['', ' ', 'A'])).toBe('A (1)')
})
test('treats Name (1) as a distinct base', () => {
// Importing a file already named "File (1)" when that display name exists.
expect(nextUniqueStorageName('File (1)', ['File (1)'])).toBe('File (1) (1)')
})
})