// @vitest-environment happy-dom

import { afterEach, describe, expect, it } from 'vitest';
import {
  DEFAULT_LEARNITIVE_GIT_CORS_PROXY,
  LEARNITIVE_GIT_AUTHOR_EMAIL_STORAGE_KEY,
  LEARNITIVE_GIT_AUTHOR_NAME_STORAGE_KEY,
  LEARNITIVE_GIT_CORS_PROXY_STORAGE_KEY,
  isDefaultPublicGitCorsProxy,
  readBrowserGitSettings,
  writeBrowserGitCorsProxySetting,
} from '../browser-settings';

afterEach(() => window.localStorage.clear());

describe('R31 browser Git settings', () => {
  it('defaults browser Git to the standard isomorphic-git CORS proxy without inventing identity', () => {
    expect(readBrowserGitSettings()).toEqual({
      authorName: '',
      authorEmail: '',
      corsProxy: DEFAULT_LEARNITIVE_GIT_CORS_PROXY,
    });
    expect(isDefaultPublicGitCorsProxy(`${DEFAULT_LEARNITIVE_GIT_CORS_PROXY}/`)).toBe(true);
  });

  it('migrates the legacy empty proxy default while preserving an explicit R31 clear', () => {
    window.localStorage.setItem(LEARNITIVE_GIT_AUTHOR_NAME_STORAGE_KEY, 'Ada Lovelace');
    window.localStorage.setItem(LEARNITIVE_GIT_AUTHOR_EMAIL_STORAGE_KEY, 'ada@example.org');
    window.localStorage.setItem(LEARNITIVE_GIT_CORS_PROXY_STORAGE_KEY, '');
    expect(readBrowserGitSettings().corsProxy).toBe(DEFAULT_LEARNITIVE_GIT_CORS_PROXY);

    writeBrowserGitCorsProxySetting('');
    expect(readBrowserGitSettings()).toEqual({
      authorName: 'Ada Lovelace',
      authorEmail: 'ada@example.org',
      corsProxy: '',
    });
  });
});
