Files
logbuch/tests/lib/media-upload.test.ts
Matthias Giesselmann b90ff252d1 Add Logbuch: project update blog with admin, media and API
Public archive with sidebar navigation, project pages, month archive,
search and entry pages with image blocks. Admin area for brands,
projects, post types, users, api clients, media and the entry editor
with drag and drop images, preview per audience, scheduling and
publish checks. Read and write API with bearer tokens, audience
scoping, idempotent creation, OpenAPI document and editorial guide.
Magic link login with configurable allowed domains, whole app behind
the session gate. 456 tests including design rule checks.
2026-07-31 21:33:42 +02:00

60 lines
1.9 KiB
TypeScript

import { mkdtemp, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'
import sharp from 'sharp'
import { db } from '~/data/db'
import { brands, media, projects } from '~/data/schema'
import { uploadMedia } from '~/lib/media-upload'
import { createLocalStorage, type Storage } from '~/lib/storage'
vi.mock('~/lib/media-images', async importOriginal => {
const original = await importOriginal<typeof import('~/lib/media-images')>()
return {
...original,
renderVariant: vi.fn(async () => {
throw new Error('libvips ist ausgefallen')
}),
}
})
let root: string
let storage: Storage
beforeAll(async () => {
root = await mkdtemp(join(tmpdir(), 'logbuch-upload-'))
storage = createLocalStorage(root)
})
afterAll(async () => {
await rm(root, { recursive: true, force: true })
})
async function project(): Promise<string> {
const [brand] = await db.insert(brands).values({ slug: 'nyo', name: 'NYO' }).returning()
const [row] = await db.insert(projects).values({ brandId: brand!.id, slug: 'pulsar', name: 'Pulsar' }).returning()
return row!.id
}
describe('uploadMedia mit gescheiterter Umwandlung', () => {
it('behält das Original und schreibt den Fehler an das Medium', async () => {
const projectId = await project()
const body = await sharp({ create: { width: 1200, height: 800, channels: 3, background: '#a33b6a' } }).png().toBuffer()
const result = await uploadMedia({ projectId, filename: 'ausfall.png', body, storage })
expect(result.ok).toBe(true)
const rows = await db.select().from(media)
expect(rows).toHaveLength(1)
expect(rows[0]!.variants).toEqual([])
expect(rows[0]!.variantError).toBe('libvips ist ausgefallen')
expect(rows[0]!.width).toBe(1200)
expect(rows[0]!.height).toBe(800)
expect(await storage.exists(rows[0]!.path)).toBe(true)
})
})