import { describe, it, expect } from 'vitest';
import { bumpSemver, compareSemver, isValidSemver } from '../../src/utils/semver.js';

describe('bumpSemver', () => {
  it('bumps patch', () => {
    expect(bumpSemver('0.1.0', 'patch')).toBe('0.1.1');
    expect(bumpSemver('1.2.3', 'patch')).toBe('1.2.4');
  });

  it('bumps minor and resets patch', () => {
    expect(bumpSemver('0.1.5', 'minor')).toBe('0.2.0');
    expect(bumpSemver('1.2.3', 'minor')).toBe('1.3.0');
  });

  it('bumps major and resets minor + patch', () => {
    expect(bumpSemver('0.1.5', 'major')).toBe('1.0.0');
    expect(bumpSemver('1.2.3', 'major')).toBe('2.0.0');
  });

  it('falls back to 0.1.0 on invalid input', () => {
    expect(bumpSemver('not-a-semver', 'patch')).toBe('0.1.0');
    expect(bumpSemver('1.0', 'patch')).toBe('0.1.0');
    expect(bumpSemver('1.0.0-beta', 'patch')).toBe('0.1.0');
    expect(bumpSemver('', 'patch')).toBe('0.1.0');
  });
});

describe('compareSemver', () => {
  it('returns 0 for equal', () => {
    expect(compareSemver('1.2.3', '1.2.3')).toBe(0);
  });

  it('returns -1 when a < b at any field', () => {
    expect(compareSemver('1.2.3', '1.2.4')).toBe(-1);
    expect(compareSemver('1.2.3', '1.3.0')).toBe(-1);
    expect(compareSemver('1.2.3', '2.0.0')).toBe(-1);
  });

  it('returns +1 when a > b at any field', () => {
    expect(compareSemver('1.2.4', '1.2.3')).toBe(1);
    expect(compareSemver('1.3.0', '1.2.3')).toBe(1);
    expect(compareSemver('2.0.0', '1.2.3')).toBe(1);
  });

  it('compares numerically (10 > 9, not lex)', () => {
    expect(compareSemver('0.10.0', '0.9.0')).toBe(1);
    expect(compareSemver('0.9.0', '0.10.0')).toBe(-1);
  });

  it('returns 0 for invalid input rather than throwing', () => {
    expect(compareSemver('bad', '1.0.0')).toBe(0);
    expect(compareSemver('1.0.0', 'bad')).toBe(0);
  });
});

describe('isValidSemver', () => {
  it('accepts MAJOR.MINOR.PATCH digits', () => {
    expect(isValidSemver('0.0.0')).toBe(true);
    expect(isValidSemver('1.2.3')).toBe(true);
    expect(isValidSemver('999.999.999')).toBe(true);
  });

  it('rejects everything else', () => {
    expect(isValidSemver('1.2')).toBe(false);
    expect(isValidSemver('1.2.3.4')).toBe(false);
    expect(isValidSemver('v1.2.3')).toBe(false);
    expect(isValidSemver('1.2.3-beta')).toBe(false);
    expect(isValidSemver('')).toBe(false);
  });
});
