<commit_before>module EyServicesFake
  class MockBackend

    def self.tresfiestas_fake
      require 'ey_services_fake/tresfiestas_fake'
      TresfiestasFake
    end

    def self.setup!(actors = {})
      unless actors[:awsm]
        require 'ey_services_fake/reacharound_awsm'
        actors[:awsm] = ReacharoundAwsm.new
      end
      unless actors[:service_provider]
        require 'ey_services_fake/mocking_bird_service'
        actors[:service_provider] = MockingBirdService.new
      end
      unless actors[:tresfiestas]
        actors[:tresfiestas] = tresfiestas_fake.new
      end
      new(actors)
    end

    def app_for(actor_name)
      this = self
      @apps ||= {}
      @apps[actor_name] ||= Rack::Builder.new do
        this.actors.each do |k, actor|
          if actor.respond_to?(:extra_middlewares)
            self.instance_eval(&actor.extra_middlewares)
          end
        end
        if ENV["REQUEST_DEBUG"]
          require 'request_visualizer'
          use RequestVisualizer do |str|
            found = str
            this.actors.each do |k, actor|
              if str.match(actor.base_url)
                found = k.to_s
              end
            end
            found
          end
        end
        run this.actors[actor_name].app
      end
    end

    def app
      this = self
      @app ||= Rack::Builder.new do
        this.actors.each do |k, actor|
          map "#{actor.base_url}/" do
            run this.app_for(k)
          end
        end
      end
    end

    attr_reader :actors
    def initialize(actors)
      @actors = actors
    end

    def actor(role)
      @actors[role] or raise "No actor registered as #{role}, I have #{@actors.keys.inspect}"
    end

    def reset!
      @actors.values.each do |v|
        v.reset!
      end
    end

    def awsm
      awsm_hash = actor(:tresfiestas).find_awsm
      unless awsm_hash
        awsm_hash = actor(:tresfiestas).create_awsm(actor(:awsm).base_url, app_for(:awsm))
        actor(:awsm).setup(awsm_hash[:auth_id], awsm_hash[:auth_key], actor(:tresfiestas).base_url, app_for(:tresfiestas))
      end
      awsm_hash
    end

    def partner
      partner_hash = actor(:tresfiestas).find_partner(sso_user)
      unless partner_hash
        partner_hash = actor(:tresfiestas).create_partner(sso_user, actor(:service_provider).base_url, app_for(:service_provider))
        @actors.values.each do |actor|
          if actor.respond_to?(:service_provider_setup)
            actor.service_provider_setup(partner_hash[:auth_id], partner_hash[:auth_key], actor(:service_provider).base_url, app_for(:service_provider))
          end
        end
        actor(:service_provider).setup(partner_hash[:auth_id], partner_hash[:auth_key], actor(:tresfiestas).base_url, app_for(:tresfiestas))
      end
      partner_hash
    end

    def service
      partner_hash = self.partner
      service_hash = actor(:tresfiestas).find_service(partner_hash[:id])
      unless service_hash
        actor(:service_provider).register_service(partner_hash[:registration_url])
        service_hash = actor(:tresfiestas).find_service(partner_hash[:id])
      end
      if actor(:tresfiestas).respond_to?(:document_service)
        service_hash.merge!(:service_doc => actor(:tresfiestas).document_service(service_hash[:id]))
      end
      service_hash.merge(:partner => partner_hash)
    end

    def sso_user
      actor(:awsm).sso_user #allows for nils (some implementations of AWSM may decide this is ok)
    end

    def sso_account
      awsm #need to have setup awsm before you can create accounts!
      sso_user_something = sso_user #the sso_user is a somehting, not necessarily a hash
      sso_account_hash = actor(:awsm).find_sso_account(sso_user_something)
      unless sso_account_hash
        sso_account_hash = actor(:awsm).create_sso_account(sso_user_something)
      end
      sso_account_hash
    end
<commit_msg>tweak the fake for certain scenarios<commit_after>module EyServicesFake
  class MockBackend

    def self.tresfiestas_fake
      require 'ey_services_fake/tresfiestas_fake'
      TresfiestasFake
    end

    def self.setup!(actors = {})
      unless actors[:awsm]
        require 'ey_services_fake/reacharound_awsm'
        actors[:awsm] = ReacharoundAwsm.new
      end
      unless actors[:service_provider]
        require 'ey_services_fake/mocking_bird_service'
        actors[:service_provider] = MockingBirdService.new
      end
      unless actors[:tresfiestas]
        actors[:tresfiestas] = tresfiestas_fake.new
      end
      new(actors)
    end

    def app_for(actor_name)
      this = self
      @apps ||= {}
      @apps[actor_name] ||= Rack::Builder.new do
        this.actors.each do |k, actor|
          if actor.respond_to?(:extra_middlewares)
            self.instance_eval(&actor.extra_middlewares)
          end
        end
        if ENV["REQUEST_DEBUG"]
          require 'request_visualizer'
          use RequestVisualizer do |str|
            found = str
            this.actors.each do |k, actor|
              if str.match(actor.base_url)
                found = k.to_s
              end
            end
            found
          end
        end
        run this.actors[actor_name].app
      end
    end

    def app
      this = self
      @app ||= Rack::Builder.new do
        this.actors.each do |k, actor|
          map "#{actor.base_url}/" do
            run this.app_for(k)
          end
        end
      end
    end

    attr_reader :actors
    def initialize(actors)
      @actors = actors
    end

    def actor(role)
      @actors[role] or raise "No actor registered as #{role}, I have #{@actors.keys.inspect}"
    end

    def reset!
      @actors.values.each do |v|
        v.reset!
      end
    end

    def awsm
      awsm_hash = actor(:tresfiestas).find_awsm
      unless awsm_hash
        awsm_hash = actor(:tresfiestas).create_awsm(actor(:awsm).base_url, app_for(:awsm))
        actor(:awsm).setup(awsm_hash[:auth_id], awsm_hash[:auth_key], actor(:tresfiestas).base_url, app_for(:tresfiestas))
      end
      awsm_hash
    end

    def partner
      partner_hash = actor(:tresfiestas).find_partner(sso_user)
      unless partner_hash
        partner_hash = actor(:tresfiestas).create_partner(sso_user, actor(:service_provider).base_url, app_for(:service_provider))
        @actors.values.each do |actor|
          if actor.respond_to?(:service_provider_setup)
            actor.service_provider_setup(partner_hash[:auth_id], partner_hash[:auth_key], actor(:service_provider).base_url, app_for(:service_provider))
          end
        end
        actor(:service_provider).setup(partner_hash[:auth_id], partner_hash[:auth_key], actor(:tresfiestas).base_url, app_for(:tresfiestas))
      end
      partner_hash
    end

    def service
      partner_hash = self.partner
      service_hash = actor(:tresfiestas).find_service(partner_hash[:id])
      unless service_hash
        actor(:service_provider).register_service(partner_hash[:registration_url])
        service_hash = actor(:tresfiestas).find_service(partner_hash[:id])
      end
      if actor(:tresfiestas).respond_to?(:document_service)
        service_hash.merge!(:service_doc => actor(:tresfiestas).document_service(service_hash[:id]))
      end
      service_hash.merge(:partner => partner_hash)
    end

    def sso_user
      awsm
      actor(:awsm).sso_user #allows for nils (some implementations of AWSM may decide this is ok)
    end

    def sso_account
      awsm #need to have setup awsm before you can create accounts!
      sso_user_something = sso_user #the sso_user is a somehting, not necessarily a hash
      sso_account_hash = actor(:awsm).find_sso_account(sso_user_something)
      unless sso_account_hash
        sso_account_hash = actor(:awsm).create_sso_account(sso_user_something)
      end
      sso_account_hash
    end

{'fraction_non_alphanumeric': 0.09123252858958068, 'fraction_numerical': 0.0, 'mean_word_length': 2.585876993166287, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3618326', 'n_tokens_mistral': 2507, 'n_tokens_neox': 2486, 'n_words': 484}