import { describe, expect, it } from 'vitest';
import {
  describeEngineMode,
  selectEngineMode,
  supportsMultithread,
} from '../../src/ffmpeg/engine-mode';
import type { EngineEnvironment } from '../../src/ffmpeg/ffmpeg.types';

const isolated: EngineEnvironment = {
  secureContext: true,
  crossOriginIsolated: true,
  sharedArrayBuffer: true,
  hardwareConcurrency: 8,
};

describe('engine mode selection', () => {
  it('selects multithread only when every required capability is present', () => {
    expect(supportsMultithread(isolated)).toBe(true);
    expect(selectEngineMode(isolated, 'automatic')).toBe('multithread');

    for (const missing of [
      'secureContext',
      'crossOriginIsolated',
      'sharedArrayBuffer',
    ] as const) {
      expect(
        selectEngineMode({ ...isolated, [missing]: false }, 'automatic')
      ).toBe('single-thread');
    }
  });

  it('honours the safe single-thread preference', () => {
    expect(selectEngineMode(isolated, 'force-single-thread')).toBe(
      'single-thread'
    );
    expect(describeEngineMode('single-thread')).toMatch(/compatibility/);
  });
});
