import { describe, expect, it } from 'vitest';
import { CURRENT_THEME_VERSION } from '@core/custom-theme';
import { migrateCustomTheme } from './theme-migrations';

describe('migrateCustomTheme', () => {
  it('passes a current record through unchanged (plus version stamp)', () => {
    const record = {
      id: 3,
      version: CURRENT_THEME_VERSION,
      name: 'Brand',
      config: { font: 'Georgia' },
      created: '2026-06-12T10:00:00.000Z',
      modified: '2026-06-12T11:00:00.000Z',
    };
    expect(migrateCustomTheme(record)).toEqual(record);
  });

  it('fills missing or invalid fields with safe defaults', () => {
    const migrated = migrateCustomTheme({ id: '7', config: ['not', 'an', 'object'] });
    expect(migrated.id).toBe(7);
    expect(migrated.version).toBe(CURRENT_THEME_VERSION);
    expect(migrated.name).toBe('Untitled theme');
    expect(migrated.config).toEqual({});
    expect(typeof migrated.created).toBe('string');
    expect(typeof migrated.modified).toBe('string');
  });

  it('keeps unknown fields written by a newer build', () => {
    const migrated = migrateCustomTheme({
      id: 1,
      name: 'Next',
      config: {},
      futureField: 'kept',
    });
    expect((migrated as unknown as Record<string, unknown>).futureField).toBe('kept');
  });
});
