import { describe, expect, it } from 'vitest';
import { CLOUD_PREFS_KEY, decodeClientPrefs, encodeClientPrefs } from './cloudprefs';

describe('cloudprefs', () => {
  it('round-trips the synced client prefs', () => {
    const p = { theme: 'dark', reduceMotion: true, boardLabels: 'classic' } as const;
    expect(decodeClientPrefs(encodeClientPrefs(p))).toEqual(p);
  });

  it('never encodes the locale (it syncs via the durable account instead)', () => {
    const raw = encodeClientPrefs({ theme: 'light', reduceMotion: false, boardLabels: 'none' });
    expect(raw).not.toContain('locale');
  });

  it('returns an empty partial for missing or malformed input', () => {
    expect(decodeClientPrefs(null)).toEqual({});
    expect(decodeClientPrefs(undefined)).toEqual({});
    expect(decodeClientPrefs('')).toEqual({});
    expect(decodeClientPrefs('not json')).toEqual({});
    expect(decodeClientPrefs('[1,2,3]')).toEqual({});
  });

  it('keeps only valid fields and drops unknown or mistyped ones', () => {
    const raw = JSON.stringify({ theme: 'neon', reduceMotion: 'yes', boardLabels: 'classic', locale: 'ru' });
    expect(decodeClientPrefs(raw)).toEqual({ boardLabels: 'classic' });
  });

  it('exposes the CloudStorage key', () => {
    expect(CLOUD_PREFS_KEY).toBe('prefs');
  });
});
