test: vitest fürs frontend
- Clip-Store: Versionsschutz, Sortierung, Reset, 409 als ClipConflictError - Sync-Store: Verbindungsaufbau, Ereignisse, Backoff, 4401, Watchdog, stop - Formatierung: Geräteerkennung aus dem User-Agent, Datumsformat Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4c5d910f54
commit
998e86c84f
6 files changed
+1099
-1
No files matched your search
@@ -0,0 +1,110 @@
|
||||
import { createPinia, setActivePinia } from 'pinia'
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
|
||||
import type { Clip } from '@/api/types'
|
||||
|
||||
import { ClipConflictError, useClipsStore } from './clips'
|
||||
|
||||
function clip(overrides: Partial<Clip> = {}): Clip {
|
||||
return {
|
||||
id: 1,
|
||||
kind: 'text',
|
||||
title: null,
|
||||
content: 'hello',
|
||||
language: null,
|
||||
pinned: false,
|
||||
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 })
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,168 @@
|
||||
import { createPinia, setActivePinia } from 'pinia'
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
|
||||
import { setUnauthorizedHandler } from '@/api/client'
|
||||
import type { Clip } from '@/api/types'
|
||||
|
||||
import { useClipsStore } from './clips'
|
||||
import { useSyncStore } from './sync'
|
||||
|
||||
/** Minimal stand-in for the browser WebSocket, driven by the test. */
|
||||
class FakeSocket {
|
||||
static instances: FakeSocket[] = []
|
||||
static readonly OPEN = 1
|
||||
readyState = 0
|
||||
onmessage: ((e: { data: string }) => void) | null = null
|
||||
onclose: ((e: { code: number }) => void) | null = null
|
||||
onerror: (() => void) | null = null
|
||||
closed = false
|
||||
|
||||
constructor(public url: string) {
|
||||
FakeSocket.instances.push(this)
|
||||
}
|
||||
|
||||
receive(message: unknown): void {
|
||||
this.readyState = FakeSocket.OPEN
|
||||
this.onmessage?.({ data: JSON.stringify(message) })
|
||||
}
|
||||
|
||||
serverClose(code: number): void {
|
||||
this.readyState = 3
|
||||
this.onclose?.({ code })
|
||||
}
|
||||
|
||||
close(): void {
|
||||
this.closed = true
|
||||
this.readyState = 3
|
||||
}
|
||||
}
|
||||
|
||||
const latest = () => FakeSocket.instances.at(-1)!
|
||||
|
||||
const clip: Clip = {
|
||||
id: 7,
|
||||
kind: 'text',
|
||||
title: null,
|
||||
content: 'live',
|
||||
language: null,
|
||||
pinned: false,
|
||||
version: 1,
|
||||
created_at: '2026-10-25T10:00:00Z',
|
||||
updated_at: '2026-10-25T10:00:00Z',
|
||||
}
|
||||
|
||||
describe('sync store', () => {
|
||||
beforeEach(() => {
|
||||
setActivePinia(createPinia())
|
||||
FakeSocket.instances = []
|
||||
vi.stubGlobal('WebSocket', FakeSocket)
|
||||
vi.useFakeTimers()
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
useSyncStore().stop()
|
||||
vi.useRealTimers()
|
||||
vi.unstubAllGlobals()
|
||||
})
|
||||
|
||||
it('connects to the same host and goes live on hello', () => {
|
||||
const sync = useSyncStore()
|
||||
sync.start()
|
||||
|
||||
expect(latest().url).toBe(`ws://${location.host}/api/ws`)
|
||||
expect(sync.status).toBe('connecting')
|
||||
|
||||
latest().receive({ type: 'hello' })
|
||||
expect(sync.status).toBe('live')
|
||||
})
|
||||
|
||||
it('applies clip events to the clip store', () => {
|
||||
const sync = useSyncStore()
|
||||
const clips = useClipsStore()
|
||||
sync.start()
|
||||
latest().receive({ type: 'hello' })
|
||||
|
||||
latest().receive({ type: 'clip.created', data: clip })
|
||||
expect(clips.items.map((c) => c.id)).toEqual([7])
|
||||
|
||||
latest().receive({ type: 'clip.updated', data: { ...clip, version: 2, content: 'edited' } })
|
||||
expect(clips.items[0]!.content).toBe('edited')
|
||||
|
||||
latest().receive({ type: 'clip.deleted', data: { id: 7 } })
|
||||
expect(clips.items).toEqual([])
|
||||
})
|
||||
|
||||
it('reloads the list after reconnecting if it was loaded before', () => {
|
||||
const sync = useSyncStore()
|
||||
const clips = useClipsStore()
|
||||
clips.loaded = true
|
||||
const load = vi.spyOn(clips, 'load').mockResolvedValue()
|
||||
sync.start()
|
||||
|
||||
latest().receive({ type: 'hello' })
|
||||
|
||||
expect(load).toHaveBeenCalledOnce()
|
||||
})
|
||||
|
||||
it('reconnects with backoff after an unexpected close', () => {
|
||||
const sync = useSyncStore()
|
||||
sync.start()
|
||||
latest().serverClose(1006)
|
||||
|
||||
expect(sync.status).toBe('connecting')
|
||||
expect(FakeSocket.instances).toHaveLength(1)
|
||||
vi.advanceTimersByTime(1_600) // first delay is 1 s ± 50 %
|
||||
expect(FakeSocket.instances).toHaveLength(2)
|
||||
})
|
||||
|
||||
it('stops and reports an expired session on close code 4401', () => {
|
||||
const onUnauthorized = vi.fn()
|
||||
setUnauthorizedHandler(onUnauthorized)
|
||||
const sync = useSyncStore()
|
||||
sync.start()
|
||||
|
||||
latest().serverClose(4401)
|
||||
vi.advanceTimersByTime(60_000)
|
||||
|
||||
expect(onUnauthorized).toHaveBeenCalledOnce()
|
||||
expect(sync.status).toBe('offline')
|
||||
expect(FakeSocket.instances).toHaveLength(1)
|
||||
})
|
||||
|
||||
it('drops a silent connection after the watchdog timeout', () => {
|
||||
const sync = useSyncStore()
|
||||
sync.start()
|
||||
latest().receive({ type: 'hello' })
|
||||
const first = latest()
|
||||
|
||||
vi.advanceTimersByTime(75_000)
|
||||
expect(first.closed).toBe(true)
|
||||
vi.advanceTimersByTime(1_600)
|
||||
expect(FakeSocket.instances).toHaveLength(2)
|
||||
})
|
||||
|
||||
it('pings keep the connection alive', () => {
|
||||
const sync = useSyncStore()
|
||||
sync.start()
|
||||
latest().receive({ type: 'hello' })
|
||||
for (let i = 0; i < 5; i++) {
|
||||
vi.advanceTimersByTime(30_000)
|
||||
latest().receive({ type: 'ping' })
|
||||
}
|
||||
|
||||
expect(FakeSocket.instances).toHaveLength(1)
|
||||
expect(sync.status).toBe('live')
|
||||
})
|
||||
|
||||
it('stop closes the socket and does not reconnect', () => {
|
||||
const sync = useSyncStore()
|
||||
sync.start()
|
||||
const socket = latest()
|
||||
sync.stop()
|
||||
vi.advanceTimersByTime(60_000)
|
||||
|
||||
expect(socket.closed).toBe(true)
|
||||
expect(sync.status).toBe('offline')
|
||||
expect(FakeSocket.instances).toHaveLength(1)
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user