import { describe, it } from 'node:test';
import assert from 'node:assert/strict';
import { matchWizardOption } from './wizard-detect-match.js';

describe('matchWizardOption', () => {
  const JAVA_OPTIONS = ['Java 11 LTS', 'Java 17 LTS', 'Java 21 LTS', 'Java 22'];

  // ── happy path ────────────────────────────────────────────────────────────

  it('returns exact match (case-insensitive)', () => {
    assert.equal(matchWizardOption('Java 17 LTS', JAVA_OPTIONS), 'Java 17 LTS');
    assert.equal(matchWizardOption('java 17 lts', JAVA_OPTIONS), 'Java 17 LTS');
    assert.equal(matchWizardOption('JAVA 17 LTS', JAVA_OPTIONS), 'Java 17 LTS');
  });

  it('returns contains match when no exact match', () => {
    const options = ['Spring Boot 3.x', 'Spring Boot 4.x', 'Quarkus'];
    // detected is a substring of an option
    assert.equal(matchWizardOption('Spring Boot', options), 'Spring Boot 3.x');
  });

  it('returns prefix match when contains fails', () => {
    const options = ['Maven', 'Gradle', 'Gradle KTS'];
    assert.equal(matchWizardOption('mav', options), 'Maven');
    assert.equal(matchWizardOption('gra', options), 'Gradle');
  });

  it('is case-insensitive for prefix matching', () => {
    const options = ['Maven', 'Gradle'];
    assert.equal(matchWizardOption('MAV', options), 'Maven');
    assert.equal(matchWizardOption('GRA', options), 'Gradle');
  });

  // ── null / empty input ────────────────────────────────────────────────────

  it('returns empty string for null detected', () => {
    assert.equal(matchWizardOption(null, JAVA_OPTIONS), '');
  });

  it('returns empty string for undefined detected', () => {
    assert.equal(matchWizardOption(undefined, JAVA_OPTIONS), '');
  });

  it('returns empty string for blank detected', () => {
    assert.equal(matchWizardOption('   ', JAVA_OPTIONS), '');
    assert.equal(matchWizardOption('', JAVA_OPTIONS), '');
  });

  it('returns empty string for empty options array', () => {
    assert.equal(matchWizardOption('Java 17', []), '');
  });

  // ── no match ─────────────────────────────────────────────────────────────

  it('returns empty string when nothing matches', () => {
    assert.equal(matchWizardOption('Haskell', JAVA_OPTIONS), '');
  });

  // ── multi-word detected ───────────────────────────────────────────────────

  it('uses first word of detected for prefix matching when longer match fails', () => {
    const options = ['TypeScript 5.x', 'JavaScript ES2022'];
    // "TypeScript" → first word is "typescript" → prefix matches "TypeScript 5.x"
    assert.equal(matchWizardOption('TypeScript latest', options), 'TypeScript 5.x');
  });

  // ── whitespace trim ───────────────────────────────────────────────────────

  it('trims whitespace from detected before matching', () => {
    assert.equal(matchWizardOption('  Java 21 LTS  ', JAVA_OPTIONS), 'Java 21 LTS');
  });
});
