import { describe, expect, it } from 'vitest';
import {
  VK_ONBOARDING_KEY,
  VK_PREFS_KEY,
  decodeVKOnboarding,
  decodeVKPrefs,
  encodeVKOnboarding,
  encodeVKPrefs,
} from './vkprefs';

describe('vkprefs', () => {
  it('round-trips the synced display prefs', () => {
    const p = {
      theme: 'dark',
      locale: 'ru',
      reduceMotion: true,
      boardLabels: 'classic',
      zoomBoard: false,
    } as const;
    expect(decodeVKPrefs(encodeVKPrefs(p))).toEqual(p);
  });

  it('carries the locale (unlike the Telegram bundle)', () => {
    const raw = encodeVKPrefs({
      theme: 'light',
      locale: 'en',
      reduceMotion: false,
      boardLabels: 'none',
      zoomBoard: true,
    });
    expect(raw).toContain('locale');
    expect(decodeVKPrefs(raw).locale).toBe('en');
  });

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

  it('keeps only valid prefs fields and drops unknown or mistyped ones', () => {
    const raw = JSON.stringify({
      theme: 'neon',
      locale: 'de',
      reduceMotion: 'yes',
      boardLabels: 'classic',
      zoomBoard: 1,
      extra: true,
    });
    expect(decodeVKPrefs(raw)).toEqual({ boardLabels: 'classic' });
  });

  it('round-trips the onboarding flags', () => {
    const s = { lobbyDone: true, gameDone: false } as const;
    expect(decodeVKOnboarding(encodeVKOnboarding(s))).toEqual(s);
  });

  it('keeps only boolean onboarding flags', () => {
    expect(decodeVKOnboarding(null)).toEqual({});
    expect(decodeVKOnboarding(JSON.stringify({ lobbyDone: 1, gameDone: true }))).toEqual({ gameDone: true });
  });

  it('exposes the VK Bridge storage keys', () => {
    expect(VK_PREFS_KEY).toBe('prefs');
    expect(VK_ONBOARDING_KEY).toBe('onboarding');
  });
});
