import { describe, expect, it } from 'vitest';

import {
  isVersionCompatible,
  pickPrimaryFile,
  resolveInstallDirectory,
  resolveModrinthLoader,
  resolveProjectType,
} from './compatibility';
import type { ModrinthVersion } from './modrinth';

describe('catalog compatibility', () => {
  it('maps paper to plugin installs', () => {
    expect(resolveModrinthLoader('PAPER')).toBe('paper');
    expect(resolveProjectType('PAPER')).toBe('plugin');
    expect(resolveInstallDirectory('plugin')).toBe('plugins');
  });

  it('maps fabric to mod installs', () => {
    expect(resolveModrinthLoader('FABRIC')).toBe('fabric');
    expect(resolveProjectType('FABRIC')).toBe('mod');
    expect(resolveInstallDirectory('mod')).toBe('mods');
  });

  it('checks version compatibility', () => {
    const version: ModrinthVersion = {
      id: 'v1',
      project_id: 'p1',
      name: '1.0.0',
      version_number: '1.0.0',
      game_versions: ['1.21.1'],
      loaders: ['paper'],
      files: [],
      date_published: '2026-01-01T00:00:00Z',
    };

    expect(isVersionCompatible(version, '1.21.1', 'paper')).toBe(true);
    expect(isVersionCompatible(version, '1.20.1', 'paper')).toBe(false);
  });

  it('picks primary jar file', () => {
    const version: ModrinthVersion = {
      id: 'v1',
      project_id: 'p1',
      name: '1.0.0',
      version_number: '1.0.0',
      game_versions: ['1.21.1'],
      loaders: ['fabric'],
      files: [
        {
          filename: 'readme.txt',
          url: 'https://example.com/readme.txt',
          hashes: {},
          primary: false,
          size: 10,
        },
        {
          filename: 'mod.jar',
          url: 'https://example.com/mod.jar',
          hashes: { sha512: 'abc' },
          primary: true,
          size: 100,
        },
      ],
      date_published: '2026-01-01T00:00:00Z',
    };

    expect(pickPrimaryFile(version)?.filename).toBe('mod.jar');
  });
});
