import { ConfigService } from '@nestjs/config';
import { buildAiMusicShareUrl, buildPostShareUrl, buildProfileShareUrl, normalizeShareBaseUrl, resolveShareBaseUrl } from './share-url.util';

describe('share URL utilities', () => {
  const originalShare = process.env.SHARE_BASE_URL;
  const originalPublicWeb = process.env.PUBLIC_WEB_URL;

  afterEach(() => {
    if (originalShare === undefined) delete process.env.SHARE_BASE_URL;
    else process.env.SHARE_BASE_URL = originalShare;
    if (originalPublicWeb === undefined) delete process.env.PUBLIC_WEB_URL;
    else process.env.PUBLIC_WEB_URL = originalPublicWeb;
  });

  it('normalizes defaults and trailing slashes', () => {
    expect(normalizeShareBaseUrl(undefined)).toBe('https://oudelaa.com');
    expect(normalizeShareBaseUrl(' https://web.test/// ')).toBe('https://web.test');
  });

  it('resolves environment before configuration', () => {
    process.env.SHARE_BASE_URL = 'https://env.test/';
    const config = { get: jest.fn(() => 'https://config.test') } as unknown as ConfigService;
    expect(resolveShareBaseUrl(config)).toBe('https://env.test');
    expect(config.get).not.toHaveBeenCalled();
  });

  it('builds encoded profile, post, and AI music links', () => {
    expect(buildProfileShareUrl('https://web', { username: 'a/b' })).toBe('https://web/u/a%2Fb');
    expect(buildProfileShareUrl('https://web', { _id: { toString: () => 'id one' } })).toBe('https://web/profile/id%20one');
    expect(buildPostShareUrl('https://web', 'post/1')).toBe('https://web/posts/post%2F1');
    expect(buildAiMusicShareUrl('https://web', 'track 1')).toBe('https://web/ai/music/track%201');
  });
});
