import { describe, expect, test } from 'vitest';

import { maintenanceDedupKey } from '../../convex/maintenanceDedup';

describe('maintenanceDedupKey', () => {
  test('combines mergeBase and head into a stable key', () => {
    expect(maintenanceDedupKey({ mergeBaseSha: 'a', headSha: 'b' })).toBe('a:b');
  });

  test('falls back to nobase when mergeBase is missing', () => {
    expect(maintenanceDedupKey({ headSha: 'b' })).toBe('nobase:b');
  });

  test('returns null when head is missing', () => {
    expect(maintenanceDedupKey({ mergeBaseSha: 'a' })).toBeNull();
  });

  test('returns null when head is empty', () => {
    expect(maintenanceDedupKey({ mergeBaseSha: 'a', headSha: '' })).toBeNull();
  });

  test('returns null when head is only whitespace', () => {
    expect(maintenanceDedupKey({ mergeBaseSha: 'a', headSha: '   ' })).toBeNull();
  });

  test('is deterministic across repeated calls (no Date.now/random)', () => {
    const input = { mergeBaseSha: 'base123', headSha: 'head456' };
    const first = maintenanceDedupKey(input);
    const second = maintenanceDedupKey(input);
    expect(first).toBe(second);
    expect(first).toBe('base123:head456');
  });

  test('different heads produce different keys', () => {
    const a = maintenanceDedupKey({ mergeBaseSha: 'base', headSha: 'h1' });
    const b = maintenanceDedupKey({ mergeBaseSha: 'base', headSha: 'h2' });
    expect(a).not.toBe(b);
  });

  test('trims surrounding whitespace on inputs', () => {
    expect(
      maintenanceDedupKey({ mergeBaseSha: '  a  ', headSha: '  b  ' }),
    ).toBe('a:b');
  });
});
