import {
  resolveManagedFileUrl,
  resolveManagedFileUrlRecord,
  resolveManagedFileUrlRecords,
  resolveManagedFileUrls,
} from './public-url.util';

describe('public url util', () => {
  const originalPublicBaseUrl = process.env.PUBLIC_BASE_URL;
  const originalStorageBasePath = process.env.STORAGE_BASE_PATH;

  beforeEach(() => {
    process.env.PUBLIC_BASE_URL = 'http://192.168.1.12:4000';
    process.env.STORAGE_BASE_PATH = 'uploads';
  });

  afterEach(() => {
    process.env.PUBLIC_BASE_URL = originalPublicBaseUrl;
    process.env.STORAGE_BASE_PATH = originalStorageBasePath;
  });

  it('resolves a local managed file url', () => {
    expect(resolveManagedFileUrl('/uploads/posts/images/file.png')).toBe(
      'http://192.168.1.12:4000/uploads/posts/images/file.png',
    );
  });

  it('resolves arrays of local managed file urls', () => {
    expect(resolveManagedFileUrls(['/uploads/a.png', '/uploads/b.png'])).toEqual([
      'http://192.168.1.12:4000/uploads/a.png',
      'http://192.168.1.12:4000/uploads/b.png',
    ]);
  });

  it('resolves url objects used for media variants', () => {
    expect(
      resolveManagedFileUrlRecord({
        lowUrl: '/uploads/posts/images/image-1/low.webp',
        mediumUrl: '/uploads/posts/images/image-1/medium.webp',
        externalUrl: 'https://cdn.example.com/image.webp',
      }),
    ).toEqual({
      lowUrl: 'http://192.168.1.12:4000/uploads/posts/images/image-1/low.webp',
      mediumUrl: 'http://192.168.1.12:4000/uploads/posts/images/image-1/medium.webp',
      externalUrl: 'https://cdn.example.com/image.webp',
    });
  });

  it('resolves arrays of media variant objects', () => {
    expect(
      resolveManagedFileUrlRecords([
        {
          lowUrl: '/uploads/posts/images/image-1/low.webp',
          mediumUrl: '/uploads/posts/images/image-1/medium.webp',
        },
      ]),
    ).toEqual([
      {
        lowUrl: 'http://192.168.1.12:4000/uploads/posts/images/image-1/low.webp',
        mediumUrl: 'http://192.168.1.12:4000/uploads/posts/images/image-1/medium.webp',
      },
    ]);
  });
});
