import type { BasePayload } from 'payload' import { describe, expect, test, vi } from 'vitest' import { migrateCookieSettingsSaveButton } from '../src/globals/CookieSettings/migrateSaveButton.js' import { getConsentTexts } from '../src/modules/consent/getConsentTexts.js' function payloadWith(db: Record, cookieSettings: Record = {}) { return { db, findGlobal: vi.fn(async () => cookieSettings), logger: { error: vi.fn(), info: vi.fn(), warn: vi.fn() }, } as unknown as BasePayload } describe('Cookie Settings buttons.save → savePreferences (1.12.2)', () => { test('MongoDB: copies the old value for every locale, only when the new field is missing', async () => { const updateOne = vi.fn(async () => ({ modifiedCount: 1 })) const payload = payloadWith({ name: 'mongoose', globals: { collection: { updateOne } } }) await migrateCookieSettingsSaveButton(payload) expect(updateOne).toHaveBeenCalledWith( { 'buttons.save': { $exists: true }, 'buttons.savePreferences': { $exists: false }, globalType: 'cookie-settings' }, [{ $set: { 'buttons.savePreferences': '$buttons.save' } }], ) expect(payload.logger.info).toHaveBeenCalled() }) test('other adapters: nothing (the SQL migration renames the column)', async () => { const updateOne = vi.fn() await migrateCookieSettingsSaveButton(payloadWith({ name: 'sqlite', globals: { collection: { updateOne } } })) expect(updateOne).not.toHaveBeenCalled() }) test('a failing update is logged, start continues', async () => { const payload = payloadWith({ name: 'mongoose', globals: { collection: { updateOne: vi.fn(async () => { throw new Error('no rights') }) } }, }) await expect(migrateCookieSettingsSaveButton(payload)).resolves.toBeUndefined() expect(payload.logger.error).toHaveBeenCalled() }) test('getConsentTexts: old field as the raw locale map (Mongo, not in the schema) → this locale', async () => { const payload = payloadWith({}, { buttons: { save: { de: 'Einstellungen speichern', pl: 'Zapisz ustawienia' } } }) const config = { defaultLocale: 'pl', locales: [{ code: 'pl', label: 'PL' }, { code: 'de', label: 'DE' }] } as const const texts = await getConsentTexts({ config, locale: 'de', payload, privacyPolicy: false }) expect(texts.buttons.save).toBe('Einstellungen speichern') }) })