import { describe, expect, it } from 'vitest';
import {
  getCoreAssetUrls,
  resolveFromAppBase,
} from '../../src/ffmpeg/asset-urls';
import { LogBuffer } from '../../src/ffmpeg/log-buffer';
import { normalizeProgress } from '../../src/ffmpeg/progress-parser';

describe('FFmpeg runtime utilities', () => {
  it('resolves assets under a nested application base', () => {
    expect(
      resolveFromAppBase(
        'vendor/core.wasm',
        'https://tools.example/apps/av/index.html',
        './'
      )
    ).toBe('https://tools.example/apps/av/vendor/core.wasm');

    const mt = getCoreAssetUrls(
      'multithread',
      (path) => `https://tools.example/deep/${path}`
    );
    expect(mt.coreURL).toContain(
      '/0.12.10-reviewed-stack5m.1/mt/ffmpeg-core.js'
    );
    expect(mt.workerURL).toContain(
      '/0.12.10-reviewed-stack5m.1/mt/ffmpeg-core.worker.js'
    );
  });

  it('clamps experimental progress and removes invalid numbers', () => {
    expect(normalizeProgress({ progress: 3, time: -10 })).toEqual({
      progress: 1,
      time: 0,
    });
    expect(
      normalizeProgress({ progress: Number.NaN, time: Number.NaN })
    ).toEqual({ progress: 0, time: 0 });
  });

  it('bounds diagnostic log memory', () => {
    const logs = new LogBuffer(2, 100);
    logs.append({ type: 'stdout', message: 'one' });
    logs.append({ type: 'stderr', message: 'two\u0000' });
    logs.append({ type: 'stderr', message: 'three' });
    expect(logs.snapshot()).toEqual([
      { type: 'stderr', message: 'two' },
      { type: 'stderr', message: 'three' },
    ]);
  });
});
