import { describe, expect, it } from 'vitest';
import { sameOriginGroup, truncateToProcess } from './origin-name';

describe('truncateToProcess', () => {
  it('keeps first 3 dash segments (drops warehouse)', () => {
    expect(truncateToProcess('美国（包邮）180g纯棉T恤成人款-DG001-单面印花-美西洛杉矶一仓'))
      .toBe('美国（包邮）180g纯棉T恤成人款-DG001-单面印花');
  });
  it('is identity for 3-segment names', () => {
    expect(truncateToProcess('美国（不包邮）230g水洗炒雪花T恤-KRHM003-直喷双面'))
      .toBe('美国（不包邮）230g水洗炒雪花T恤-KRHM003-直喷双面');
  });
  it('handles malformed names without country prefix', () => {
    expect(truncateToProcess('（不包邮）230g水洗炒雪花T恤-FRTM001-双面印花'))
      .toBe('（不包邮）230g水洗炒雪花T恤-FRTM001-双面印花');
  });
  it('returns empty for null/empty', () => {
    expect(truncateToProcess(null)).toBe('');
    expect(truncateToProcess('')).toBe('');
  });
});

describe('sameOriginGroup', () => {
  it('matches same product across warehouses', () => {
    expect(sameOriginGroup(
      '美国（包邮）180g纯棉T恤成人款-DG001-单面印花-美西洛杉矶一仓',
      '美国（包邮）180g纯棉T恤成人款-DG001-单面印花-美中亚特兰大仓',
    )).toBe(true);
  });
  it('does not match different SKU', () => {
    expect(sameOriginGroup(
      '美国（包邮）180g纯棉T恤成人款-DG001-单面印花',
      '美国（包邮）180g纯棉T恤成人款-DG002-单面印花',
    )).toBe(false);
  });
  it('does not match different country', () => {
    expect(sameOriginGroup(
      '美国（包邮）T恤-DG001-单面印花',
      '德国（不包邮）T恤-DG001-单面印花',
    )).toBe(false);
  });
  it('does not match empty names', () => {
    expect(sameOriginGroup(null, 'x-y-z')).toBe(false);
    expect(sameOriginGroup('', '')).toBe(false);
  });
});
