import { DOCUMENT } from '@angular/common';
import { TestBed } from '@angular/core/testing';
import { TenantResolverService } from './tenant-resolver.service';

describe('TenantResolverService', () => {
  function resolveBaseDomain(hostname: string): string {
    TestBed.resetTestingModule();
    TestBed.configureTestingModule({
      providers: [
        TenantResolverService,
        {
          provide: DOCUMENT,
          useValue: { location: { hostname, protocol: 'https:' } }
        }
      ]
    });

    return TestBed.inject(TenantResolverService).getBaseDomain();
  }

  it('keeps a root storefront domain', () => {
    expect(resolveBaseDomain('example.com')).toBe('example.com');
  });

  it('removes tenant and www subdomains from the API domain', () => {
    expect(resolveBaseDomain('store1.example.com')).toBe('example.com');
    expect(resolveBaseDomain('www.example.com')).toBe('example.com');
  });

  it('keeps a country-code second-level domain', () => {
    expect(resolveBaseDomain('store1.example.co.uk')).toBe('example.co.uk');
  });
});
