import { RankPosition } from '../../../src/domain/value-objects/rank-position.vo';

describe('RankPosition', () => {
  describe('create', () => {
    it('应该创建有效的排名位置', () => {
      const position = RankPosition.create(1);
      expect(position.value).toBe(1);
    });

    it('排名为0时应该抛出错误', () => {
      expect(() => RankPosition.create(0)).toThrow('排名必须大于0');
    });

    it('排名为负数时应该抛出错误', () => {
      expect(() => RankPosition.create(-1)).toThrow('排名必须大于0');
    });
  });

  describe('isTop', () => {
    it('第1名应该在前10', () => {
      const position = RankPosition.create(1);
      expect(position.isTop(10)).toBe(true);
    });

    it('第10名应该在前10', () => {
      const position = RankPosition.create(10);
      expect(position.isTop(10)).toBe(true);
    });

    it('第11名不应该在前10', () => {
      const position = RankPosition.create(11);
      expect(position.isTop(10)).toBe(false);
    });
  });

  describe('isFirst', () => {
    it('第1名应该是第一名', () => {
      const position = RankPosition.create(1);
      expect(position.isFirst()).toBe(true);
    });

    it('第2名不应该是第一名', () => {
      const position = RankPosition.create(2);
      expect(position.isFirst()).toBe(false);
    });
  });

  describe('isTopThree', () => {
    it('第1名应该在前三', () => {
      const position = RankPosition.create(1);
      expect(position.isTopThree()).toBe(true);
    });

    it('第3名应该在前三', () => {
      const position = RankPosition.create(3);
      expect(position.isTopThree()).toBe(true);
    });

    it('第4名不应该在前三', () => {
      const position = RankPosition.create(4);
      expect(position.isTopThree()).toBe(false);
    });
  });

  describe('calculateChange', () => {
    it('排名上升应该返回正数', () => {
      const current = RankPosition.create(5);
      const previous = RankPosition.create(10);

      expect(current.calculateChange(previous)).toBe(5);
    });

    it('排名下降应该返回负数', () => {
      const current = RankPosition.create(10);
      const previous = RankPosition.create(5);

      expect(current.calculateChange(previous)).toBe(-5);
    });

    it('排名不变应该返回0', () => {
      const current = RankPosition.create(5);
      const previous = RankPosition.create(5);

      expect(current.calculateChange(previous)).toBe(0);
    });

    it('没有上次排名应该返回0', () => {
      const current = RankPosition.create(5);

      expect(current.calculateChange(null)).toBe(0);
    });
  });

  describe('getChangeDescription', () => {
    it('上升应该显示上升符号', () => {
      const current = RankPosition.create(5);
      const previous = RankPosition.create(10);

      expect(current.getChangeDescription(previous)).toBe('↑5');
    });

    it('下降应该显示下降符号', () => {
      const current = RankPosition.create(10);
      const previous = RankPosition.create(5);

      expect(current.getChangeDescription(previous)).toBe('↓5');
    });

    it('不变应该显示-', () => {
      const current = RankPosition.create(5);
      const previous = RankPosition.create(5);

      expect(current.getChangeDescription(previous)).toBe('-');
    });
  });

  describe('isBetterThan', () => {
    it('排名靠前应该更好', () => {
      const first = RankPosition.create(1);
      const second = RankPosition.create(2);

      expect(first.isBetterThan(second)).toBe(true);
      expect(second.isBetterThan(first)).toBe(false);
    });
  });

  describe('equals', () => {
    it('相同排名应该相等', () => {
      const pos1 = RankPosition.create(5);
      const pos2 = RankPosition.create(5);

      expect(pos1.equals(pos2)).toBe(true);
    });

    it('不同排名应该不相等', () => {
      const pos1 = RankPosition.create(5);
      const pos2 = RankPosition.create(10);

      expect(pos1.equals(pos2)).toBe(false);
    });
  });
});
