# frozen_string_literal: true

require_relative 'test_helper'

class TestFileVerifier < Minitest::Test
  include TestHelpers

  def setup
    setup_temp_dirs
    @json_path = File.join(@tmpdir, 'test.json')
  end

  def teardown
    teardown_temp_dirs
  end

  # ─── verify_key ───────────────────────────────────────────────────────

  def test_verify_key_with_correct_value_returns_ok
    File.write(@json_path, JSON.pretty_generate('enabledPlugins' => { 'foo@bar' => true }))

    v = PluginConfig::FileVerifier.new(@json_path)
    result = v.verify_key('enabledPlugins', 'foo@bar', true)

    assert result[:verified]
  end

  def test_verify_key_with_wrong_value_returns_not_ok
    File.write(@json_path, JSON.pretty_generate('enabledPlugins' => { 'foo@bar' => false }))

    v = PluginConfig::FileVerifier.new(@json_path)
    result = v.verify_key('enabledPlugins', 'foo@bar', true)

    refute result[:verified]
    assert_match(/false/, result[:details])
  end

  def test_verify_key_missing_file_returns_file_missing
    v = PluginConfig::FileVerifier.new('/nonexistent/path.json')
    result = v.verify_key('enabledPlugins', 'foo@bar', true)

    refute result[:verified]
    assert_match(/File not found/, result[:details])
  end

  def test_verify_key_invalid_json_returns_parse_error
    File.write(@json_path, 'not valid json {{{')

    v = PluginConfig::FileVerifier.new(@json_path)
    result = v.verify_key('enabledPlugins', 'foo@bar', true)

    refute result[:verified]
    assert_match(/JSON parse error/, result[:details])
  end

  def test_verify_key_missing_section_returns_not_found
    File.write(@json_path, JSON.pretty_generate('other' => {}))

    v = PluginConfig::FileVerifier.new(@json_path)
    result = v.verify_key('enabledPlugins', 'foo@bar', true)

    refute result[:verified]
    assert_match(/Section.*not found/, result[:details])
  end

  # ─── verify_key_absent ────────────────────────────────────────────────

  def test_verify_key_absent_when_key_is_absent_returns_ok
    File.write(@json_path, JSON.pretty_generate('enabledPlugins' => {}))

    v = PluginConfig::FileVerifier.new(@json_path)
    result = v.verify_key_absent('enabledPlugins', 'foo@bar')

    assert result[:verified]
  end

  def test_verify_key_absent_when_key_is_present_returns_not_ok
    File.write(@json_path, JSON.pretty_generate('enabledPlugins' => { 'foo@bar' => true }))

    v = PluginConfig::FileVerifier.new(@json_path)
    result = v.verify_key_absent('enabledPlugins', 'foo@bar')

    refute result[:verified]
    assert_match(/still present/, result[:details])
  end

  # ─── verify_written ───────────────────────────────────────────────────

  def test_verify_written_matching_content_returns_ok
    content = '{"hello": "world"}'
    File.write(@json_path, content)

    v = PluginConfig::FileVerifier.new(@json_path)
    result = v.verify_written(content)

    assert result[:verified]
  end

  def test_verify_written_mismatched_content_returns_not_ok
    File.write(@json_path, '{"hello": "world"}')

    v = PluginConfig::FileVerifier.new(@json_path)
    result = v.verify_written('{"hello": "different"}')

    refute result[:verified]
    assert_match(/mismatch/, result[:details])
  end
end
