import { describe, it, expect } from 'vitest';

import { languageNeedsServerSync } from './language';
import type { Profile } from './model';

// The reconciler only reads isGuest + preferredLanguage; a partial cast keeps the fixture small.
const profile = (over: Partial<Profile>): Profile => ({ isGuest: false, preferredLanguage: 'en', ...over }) as Profile;

describe('languageNeedsServerSync', () => {
  it('is false without a profile', () => {
    expect(languageNeedsServerSync(null, 'ru')).toBe(false);
    expect(languageNeedsServerSync(undefined, 'ru')).toBe(false);
  });

  it('is false for a guest — guests keep only the client preference', () => {
    expect(languageNeedsServerSync(profile({ isGuest: true, preferredLanguage: 'en' }), 'ru')).toBe(false);
  });

  it('is false when the account already matches the locale', () => {
    expect(languageNeedsServerSync(profile({ preferredLanguage: 'ru' }), 'ru')).toBe(false);
  });

  it('is true for a real account whose stored language differs (banner + push follow it)', () => {
    expect(languageNeedsServerSync(profile({ preferredLanguage: 'en' }), 'ru')).toBe(true);
    expect(languageNeedsServerSync(profile({ preferredLanguage: 'ru' }), 'en')).toBe(true);
  });
});
