import { PostType } from '../enums/post-type.enum';
import { buildPostMediaResponse } from './post-media-response.util';

describe('post media response util', () => {
  it('returns display-safe waveformPeaks for audio media', () => {
    const media = buildPostMediaResponse({
      postType: PostType.AUDIO,
      audioUrl: '/uploads/posts/audios/audio.mp3',
      durationSeconds: 42,
      waveformPeaks: [1, 12, 30, 45, 20, 6],
    });

    expect(media.mediaType).toBe(PostType.AUDIO);
    expect(media.durationSeconds).toBe(42);
    expect(media.waveformPeaks).toHaveLength(160);
    expect(media.waveformPeaksPreview).toHaveLength(160);
    expect(media.waveformPeaksDetailed.length).toBeGreaterThanOrEqual(500);
    expect(media.waveformPeaksDetailed.length).toBeLessThanOrEqual(1000);
    expect(media.waveformPeaks).toEqual(media.waveformPeaksPreview);
    expect(media.waveformPeaks.every((value) => Number.isInteger(value))).toBe(true);
    expect(media.waveformPeaks.every((value) => value >= 0 && value <= 100)).toBe(true);
    expect(media.waveformPeaksDetailed.every((value) => Number.isInteger(value))).toBe(true);
    expect(media.waveformPeaksDetailed.every((value) => value >= 0 && value <= 100)).toBe(true);
  });

  it('does not attach waveformPeaks to non-audio media', () => {
    const media = buildPostMediaResponse({
      postType: PostType.IMAGE,
      imageUrls: ['/uploads/posts/images/image.jpg'],
      waveformPeaks: [10, 20, 30],
    });

    expect(media.mediaType).toBe(PostType.IMAGE);
    expect(media.waveformPeaks).toEqual([]);
    expect(media.waveformPeaksPreview).toEqual([]);
    expect(media.waveformPeaksDetailed).toEqual([]);
  });

  it('prefers rewritten HLS playback and keeps MP4 as fallback', () => {
    const media = buildPostMediaResponse({
      postType: PostType.VIDEO,
      hlsUrl: 'https://api.oudelaa.test/api/v1/media/hls/uploads/posts/hls/stream/master.m3u8',
      videoUrl: 'https://s3.cumin.dev/oudelaa/uploads/posts/video/video.mp4?X-Amz-Signature=signed',
    });

    expect(media.preferredPlaybackUrl).toBe(media.hlsUrl);
    expect(media.videoUrl).toContain('X-Amz-Signature=signed');
  });
});
