Hochladen per Dateiauswahl, Drag & Drop und Einfügen mit Fortschritt und Abbrechen, Aufbewahrung wählbar. Datei-Karten mit Bildvorschau, Größe, Ablauf und Download; Speicheranzeige im Konto. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
209 lines
7.0 KiB
TypeScript
209 lines
7.0 KiB
TypeScript
import { createPinia, setActivePinia } from 'pinia'
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
import type { Clip } from '@/api/types'
|
|
|
|
import { ClipConflictError, matchesFilter, useClipsStore } from './clips'
|
|
|
|
function clip(overrides: Partial<Clip> = {}): Clip {
|
|
return {
|
|
id: 1,
|
|
kind: 'text',
|
|
title: null,
|
|
content: 'hello',
|
|
language: null,
|
|
pinned: false,
|
|
project_id: null,
|
|
file: null,
|
|
version: 1,
|
|
created_at: '2026-10-25T10:00:00Z',
|
|
updated_at: '2026-10-25T10:00:00Z',
|
|
...overrides,
|
|
}
|
|
}
|
|
|
|
function jsonResponse(status: number, body: unknown): Response {
|
|
return new Response(JSON.stringify(body), {
|
|
status,
|
|
headers: { 'Content-Type': 'application/json' },
|
|
})
|
|
}
|
|
|
|
describe('clips store', () => {
|
|
beforeEach(() => setActivePinia(createPinia()))
|
|
afterEach(() => vi.unstubAllGlobals())
|
|
|
|
it('ignores older versions arriving late', () => {
|
|
const store = useClipsStore()
|
|
store.upsert(clip({ version: 3, content: 'new' }))
|
|
store.upsert(clip({ version: 2, content: 'old' }))
|
|
|
|
expect(store.items).toHaveLength(1)
|
|
expect(store.items[0]!.content).toBe('new')
|
|
})
|
|
|
|
it('replaces equal or newer versions', () => {
|
|
const store = useClipsStore()
|
|
store.upsert(clip({ version: 1 }))
|
|
store.upsert(clip({ version: 2, content: 'edited' }))
|
|
|
|
expect(store.items[0]!.content).toBe('edited')
|
|
expect(store.total).toBe(1)
|
|
})
|
|
|
|
it('sorts pinned first, then most recently updated', () => {
|
|
const store = useClipsStore()
|
|
store.upsert(clip({ id: 1, updated_at: '2026-10-25T10:00:00Z' }))
|
|
store.upsert(clip({ id: 2, updated_at: '2026-10-25T12:00:00Z' }))
|
|
store.upsert(clip({ id: 3, updated_at: '2026-10-25T08:00:00Z', pinned: true }))
|
|
|
|
expect(store.items.map((c) => c.id)).toEqual([3, 2, 1])
|
|
})
|
|
|
|
it('removes clips and keeps the total in step', () => {
|
|
const store = useClipsStore()
|
|
store.upsert(clip({ id: 1 }))
|
|
store.upsert(clip({ id: 2 }))
|
|
store.removeLocal(1)
|
|
store.removeLocal(99)
|
|
|
|
expect(store.items.map((c) => c.id)).toEqual([2])
|
|
expect(store.total).toBe(1)
|
|
})
|
|
|
|
it('reset forgets everything', () => {
|
|
const store = useClipsStore()
|
|
store.upsert(clip())
|
|
store.loaded = true
|
|
store.reset()
|
|
|
|
expect(store.items).toEqual([])
|
|
expect(store.total).toBe(0)
|
|
expect(store.loaded).toBe(false)
|
|
})
|
|
|
|
it('turns a 409 into ClipConflictError and adopts the server version', async () => {
|
|
const store = useClipsStore()
|
|
store.upsert(clip({ version: 1 }))
|
|
const current = clip({ version: 2, content: 'from phone' })
|
|
vi.stubGlobal(
|
|
'fetch',
|
|
vi.fn(async () => jsonResponse(409, { detail: 'Konflikt', current })),
|
|
)
|
|
|
|
const error = await store.update(1, { content: 'from laptop' }, 1).catch((e: unknown) => e)
|
|
|
|
expect(error).toBeInstanceOf(ClipConflictError)
|
|
expect((error as ClipConflictError).current.content).toBe('from phone')
|
|
expect(store.items[0]!.content).toBe('from phone')
|
|
})
|
|
|
|
it('sends the expected version with updates', async () => {
|
|
const store = useClipsStore()
|
|
const fetchMock = vi.fn<typeof fetch>(async () => jsonResponse(200, clip({ version: 2 })))
|
|
vi.stubGlobal('fetch', fetchMock)
|
|
|
|
await store.update(1, { pinned: true }, 1)
|
|
|
|
const init = fetchMock.mock.calls[0]![1]!
|
|
expect(init.method).toBe('PATCH')
|
|
expect(JSON.parse(init.body as string)).toEqual({ pinned: true, version: 1 })
|
|
})
|
|
|
|
it('requests the filtered list', async () => {
|
|
const store = useClipsStore()
|
|
const fetchMock = vi.fn<typeof fetch>(async () => jsonResponse(200, { items: [], total: 0 }))
|
|
vi.stubGlobal('fetch', fetchMock)
|
|
|
|
await store.setFilter({ project: 3, query: ' a&b ' })
|
|
|
|
const url = new URL(String(fetchMock.mock.calls[0]![0]), 'https://x')
|
|
expect(url.searchParams.get('project')).toBe('3')
|
|
expect(url.searchParams.get('q')).toBe('a&b')
|
|
|
|
await store.setFilter({ project: 3, query: 'a&b' })
|
|
expect(fetchMock).toHaveBeenCalledOnce()
|
|
})
|
|
|
|
it('drops responses to superseded requests', async () => {
|
|
const store = useClipsStore()
|
|
const resolvers: ((r: Response) => void)[] = []
|
|
vi.stubGlobal(
|
|
'fetch',
|
|
vi.fn(() => new Promise<Response>((resolve) => resolvers.push(resolve))),
|
|
)
|
|
|
|
const first = store.setFilter({ project: null, query: 'a' })
|
|
const second = store.setFilter({ project: null, query: 'ab' })
|
|
resolvers[1]!(jsonResponse(200, { items: [clip({ id: 2, content: 'ab' })], total: 1 }))
|
|
await second
|
|
resolvers[0]!(jsonResponse(200, { items: [clip({ id: 1, content: 'a' })], total: 1 }))
|
|
await first
|
|
|
|
expect(store.items.map((c) => c.id)).toEqual([2])
|
|
expect(store.loading).toBe(false)
|
|
})
|
|
|
|
it('keeps live updates that do not match the filter out of the list', () => {
|
|
const store = useClipsStore()
|
|
store.filter = { project: 5, query: '' }
|
|
store.upsert(clip({ id: 1, project_id: 5 }))
|
|
store.upsert(clip({ id: 2, project_id: 6 }))
|
|
expect(store.items.map((c) => c.id)).toEqual([1])
|
|
|
|
// Moved to another project on a different device
|
|
store.upsert(clip({ id: 1, project_id: 6, version: 2 }))
|
|
expect(store.items).toEqual([])
|
|
expect(store.total).toBe(0)
|
|
})
|
|
|
|
it('tells subscribers about changes to filtered-out clips', () => {
|
|
const store = useClipsStore()
|
|
store.filter = { project: 'none', query: '' }
|
|
const listener = vi.fn()
|
|
const unsubscribe = store.subscribe(listener)
|
|
|
|
store.upsert(clip({ id: 1, project_id: 9 }))
|
|
store.removeLocal(1)
|
|
unsubscribe()
|
|
store.upsert(clip({ id: 2 }))
|
|
|
|
expect(listener.mock.calls).toEqual([
|
|
[{ clip: clip({ id: 1, project_id: 9 }) }],
|
|
[{ deleted: 1 }],
|
|
])
|
|
})
|
|
|
|
it('detaches clips from a deleted project like the backend', () => {
|
|
const store = useClipsStore()
|
|
store.upsert(clip({ id: 1, project_id: 4, version: 2 }))
|
|
store.upsert(clip({ id: 2, project_id: 5 }))
|
|
|
|
store.detachProject(4)
|
|
|
|
const byId = Object.fromEntries(store.items.map((c) => [c.id, c]))
|
|
expect(byId[1]).toMatchObject({ project_id: null, version: 3 })
|
|
expect(byId[2]).toMatchObject({ project_id: 5, version: 1 })
|
|
})
|
|
})
|
|
|
|
describe('matchesFilter', () => {
|
|
const all = { project: null, query: '' }
|
|
|
|
it('filters by project', () => {
|
|
expect(matchesFilter(clip({ project_id: 1 }), all)).toBe(true)
|
|
expect(matchesFilter(clip({ project_id: 1 }), { ...all, project: 1 })).toBe(true)
|
|
expect(matchesFilter(clip({ project_id: 2 }), { ...all, project: 1 })).toBe(false)
|
|
expect(matchesFilter(clip(), { ...all, project: 'none' })).toBe(true)
|
|
expect(matchesFilter(clip({ project_id: 1 }), { ...all, project: 'none' })).toBe(false)
|
|
})
|
|
|
|
it('searches title and content case-insensitively', () => {
|
|
const c = clip({ title: 'Server', content: 'cfg = getConfig()' })
|
|
expect(matchesFilter(c, { ...all, query: 'CONFIG' })).toBe(true)
|
|
expect(matchesFilter(c, { ...all, query: ' serv ' })).toBe(true)
|
|
expect(matchesFilter(c, { ...all, query: 'missing' })).toBe(false)
|
|
expect(matchesFilter(clip({ content: 'Straße' }), { ...all, query: 'STRASSE' })).toBe(true)
|
|
})
|
|
})
|