# This file is auto-generated by alef — DO NOT EDIT.
# alef:hash:4e15143f4af1ae8bafbdb1506ef057da924484c66a19483966333558ad437e75
# To regenerate: alef generate
# To verify freshness: alef verify --exit-code
# Issues & docs: https://github.com/kreuzberg-dev/alef
# frozen_string_literal: true

# RSpec hooks to isolate plugin registry state between tests.
# Unregisters any test-prefixed backends (test-*, test_*) after each test
# to prevent pollution from one test affecting subsequent tests.
begin
  require 'kreuzberg'
  RSpec.configure do |config|
    # Track initial registry state before each test
    config.before(:each) do
      begin
        @_initial_ocr_backends = Kreuzberg.list_ocr_backends.to_set rescue Set.new
        @_initial_embedding_backends = Kreuzberg.list_embedding_backends.to_set rescue Set.new
        @_initial_document_extractors = Kreuzberg.list_document_extractors.to_set rescue Set.new
        @_initial_renderers = Kreuzberg.list_renderers.to_set rescue Set.new
        @_initial_validators = Kreuzberg.list_validators.to_set rescue Set.new
        @_initial_post_processors = Kreuzberg.list_post_processors.to_set rescue Set.new
      rescue
        # If registry functions aren't available, skip cleanup
      end
    end

    # Clean up test-prefixed backends after each test
    config.after(:each) do
      begin
        current_ocr = Kreuzberg.list_ocr_backends.to_set rescue Set.new
        (current_ocr - @_initial_ocr_backends).each do |name|
          Kreuzberg.unregister_ocr_backend(name) if name.to_s.start_with?('test-', 'test_')
        end

        current_embedding = Kreuzberg.list_embedding_backends.to_set rescue Set.new
        (current_embedding - @_initial_embedding_backends).each do |name|
          Kreuzberg.unregister_embedding_backend(name) if name.to_s.start_with?('test-', 'test_')
        end

        current_extractors = Kreuzberg.list_document_extractors.to_set rescue Set.new
        (current_extractors - @_initial_document_extractors).each do |name|
          Kreuzberg.unregister_document_extractor(name) if name.to_s.start_with?('test-', 'test_')
        end

        current_renderers = Kreuzberg.list_renderers.to_set rescue Set.new
        (current_renderers - @_initial_renderers).each do |name|
          Kreuzberg.unregister_renderer(name) if name.to_s.start_with?('test-', 'test_')
        end

        current_validators = Kreuzberg.list_validators.to_set rescue Set.new
        (current_validators - @_initial_validators).each do |name|
          Kreuzberg.unregister_validator(name) if name.to_s.start_with?('test-', 'test_')
        end

        current_processors = Kreuzberg.list_post_processors.to_set rescue Set.new
        (current_processors - @_initial_post_processors).each do |name|
          Kreuzberg.unregister_post_processor(name) if name.to_s.start_with?('test-', 'test_')
        end
      rescue
        # Cleanup failures are non-fatal; continue silently
      end
    end
  end
rescue LoadError
  # Kreuzberg not available; skip registry cleanup
end

# Change to the configured test-documents directory so that fixture file paths like
# "pdf/fake_memo.pdf" resolve correctly when running rspec from e2e/ruby/.
# spec_helper.rb lives in e2e/ruby/spec/; the fixtures dir resolves three directories up.
_test_documents = File.expand_path('../../../test_documents', __dir__)
Dir.chdir(_test_documents) if Dir.exist?(_test_documents)
