import { describe, it, expect } from 'vitest';
import { render, screen } from '@testing-library/react';
import PolicyVersionDiff from './PolicyVersionDiff';

const version = (n: number, snapshot: Record<string, unknown>) => ({
  version: n,
  created_at: 1_800_000_000 + n,
  snapshot,
});

describe('PolicyVersionDiff', () => {
  /** ⭐ سؤال واقعی «چه چیزی عوض شد؟» است، نه «هر نسخه چه بود». */
  it('shows only the fields that changed between versions', () => {
    render(
      <PolicyVersionDiff
        versions={[
          version(1, { name: 'تخفیف پاییز', priority: 0, effects: [{ type: 'discount_percent', value: 10 }] }),
          version(2, { name: 'تخفیف پاییز', priority: 5, effects: [{ type: 'discount_percent', value: 20 }] }),
        ]}
      />,
    );

    expect(screen.getByText('اولویت')).toBeInTheDocument();
    expect(screen.getByText('اثرها')).toBeInTheDocument();
    // نام عوض نشده، پس نباید ردیف بگیرد.
    expect(screen.queryByText('نام')).toBeNull();
  });

  it('marks the first version rather than diffing it against nothing', () => {
    render(<PolicyVersionDiff versions={[version(1, { name: 'قانون' })]} />);

    expect(screen.getByText('نسخهٔ نخست')).toBeInTheDocument();
  });

  it('says so when a version changed nothing meaningful', () => {
    render(
      <PolicyVersionDiff
        versions={[version(1, { name: 'قانون', priority: 0 }), version(2, { name: 'قانون', priority: 0 })]}
      />,
    );

    expect(screen.getByText('بدون تغییرِ معنادار')).toBeInTheDocument();
  });
});
