import { describe, expect, it } from 'vitest';
import { determineBaseBranch } from '../squash-base.js';

describe('determineBaseBranch — 4.x GOLDEN RULE (reference branch by type)', () => {
  it('feature/* squashes onto develop', () => {
    const d = determineBaseBranch('feature/impersonation');
    expect(d.branchType).toBe('feature');
    expect(d.baseBranch).toBe('develop');
    expect(d.blocked).toBe(false);
  });

  it('release/* squashes onto main (NOT develop) — release → prod', () => {
    const d = determineBaseBranch('release/3.53.0');
    expect(d.branchType).toBe('release');
    expect(d.baseBranch).toBe('main');
    expect(d.blocked).toBe(false);
  });

  it('hotfix/* squashes onto main — hotfix → prod', () => {
    const d = determineBaseBranch('hotfix/3.53.1');
    expect(d.branchType).toBe('hotfix');
    expect(d.baseBranch).toBe('main');
    expect(d.blocked).toBe(false);
  });

  it('develop squashes onto main — develop → prod', () => {
    const d = determineBaseBranch('develop');
    expect(d.branchType).toBe('develop');
    expect(d.baseBranch).toBe('main');
    expect(d.blocked).toBe(false);
  });

  it('main and master are BLOCKED (never squash on production)', () => {
    for (const b of ['main', 'master']) {
      const d = determineBaseBranch(b);
      expect(d.branchType).toBe('main');
      expect(d.blocked).toBe(true);
      expect(d.baseBranch).toBeNull();
    }
  });

  it('unknown branch types default the reference to develop (4.x default)', () => {
    const d = determineBaseBranch('experiment/x');
    expect(d.branchType).toBe('unknown');
    expect(d.baseBranch).toBe('develop');
    expect(d.blocked).toBe(false);
  });

  it('honors custom main/develop branch names', () => {
    const names = { main: 'production', develop: 'integration' };
    expect(determineBaseBranch('release/1.0.0', names).baseBranch).toBe('production');
    expect(determineBaseBranch('feature/x', names).baseBranch).toBe('integration');
    // The configured production branch is blocked even under a custom name.
    expect(determineBaseBranch('production', names).blocked).toBe(true);
    // 'master' stays a protected name regardless of config.
    expect(determineBaseBranch('master', names).blocked).toBe(true);
  });

  it('is case-insensitive and trims whitespace', () => {
    expect(determineBaseBranch('  Release/2.0.0 ').baseBranch).toBe('main');
    expect(determineBaseBranch('MAIN').blocked).toBe(true);
  });
});
