import { beforeEach, describe, expect, it } from 'vitest' import { listBrands } from '~/data/repositories/projects' import { performSaveBrand } from '~/lib/admin-brands' import { adminViewer, auditEntries, form, makeBrand, moderatorViewer, resetAccounts } from '../support/admin' beforeEach(resetAccounts) describe('performSaveBrand', () => { it('legt eine Marke an und protokolliert das', async () => { const state = await performSaveBrand( adminViewer(), form({ name: 'Pocket Rocket', slug: '', color: '#C43A22', sort: '2' }), ) expect(state.status).toBe('ok') expect(state.created).toBe(true) const brands = await listBrands() expect(brands).toHaveLength(1) expect(brands[0]).toMatchObject({ slug: 'pocket-rocket', name: 'Pocket Rocket', color: '#c43a22', sort: 2 }) const entries = await auditEntries() expect(entries[0]).toMatchObject({ action: 'brand.create', entity: 'brand', entityId: brands[0]!.id }) }) it('lehnt einen vergebenen Slug ab', async () => { await makeBrand('nyo', 'NYO') const state = await performSaveBrand(adminViewer(), form({ name: 'NYO zwei', slug: 'nyo', color: '#191c20' })) expect(state.message).toBe('slugTaken') expect(state.fields?.slug).toBe('slugTaken') expect(await listBrands()).toHaveLength(1) }) it('speichert Änderungen an einer bestehenden Marke', async () => { const brand = await makeBrand('nyo', 'NYO') const state = await performSaveBrand( adminViewer(), form({ id: brand.id, name: 'NYO GmbH', slug: 'nyo', color: '#2b4a9b', sort: '1' }), ) expect(state.status).toBe('ok') expect(state.created).toBe(false) const brands = await listBrands() expect(brands[0]).toMatchObject({ name: 'NYO GmbH', color: '#2b4a9b' }) expect((await auditEntries())[0]?.action).toBe('brand.update') }) it('lässt einen Moderator keine Marke anlegen', async () => { const state = await performSaveBrand(moderatorViewer(), form({ name: 'Fremd', color: '#191c20' })) expect(state.message).toBe('forbidden') expect(await listBrands()).toHaveLength(0) expect(await auditEntries()).toHaveLength(0) }) it('verlangt einen Namen', async () => { const state = await performSaveBrand(adminViewer(), form({ name: '', color: '#191c20' })) expect(state.fields?.name).toBe('nameRequired') }) })