<commit_before>require 'pty'

module Snapshot
  class Runner
    TRACE_DIR = '/tmp/snapshot_traces'

    def initialize
      Snapshot::DependencyChecker.check_dependencies
    end

    def work
      SnapshotConfig.shared_instance.js_file # to verify the file can be found

      Builder.new.build_app

      counter = 0
      SnapshotConfig.shared_instance.devices.each do |device|
        SnapshotConfig.shared_instance.languages.each do |language|

          begin
            run_tests(device, language)
            counter += copy_screenshots(language)
          rescue Exception => ex
            Helper.log.error(ex)
          end

        end
      end

      Helper.log.info "Successfully finished generating #{counter} screenshots.".green
      Helper.log.info "Check it out here: #{SnapshotConfig.shared_instance.screenshots_path}".green
      ReportsGenerator.new.generate
    end

    def clean_old_traces
      FileUtils.rm_rf(TRACE_DIR)
      FileUtils.mkdir_p(TRACE_DIR)
    end


    def run_tests(device, language)
      Helper.log.info "Running tests on #{device} in language #{language}".green
      app_path = Dir.glob("/tmp/snapshot/build/*.app").first

      clean_old_traces

      ENV['SNAPSHOT_LANGUAGE'] = language
      command = generate_test_command(device, language, app_path)
      
      retry_run = false

      lines = []
      PTY.spawn(command) do |stdin, stdout, pid|
        stdin.each do |line|
          lines << line
          result = parse_test_line(line)

          case result
            when :retry
              retry_run = true
            when :screenshot
              Helper.log.info "Successfully took screenshot 📱"
            end<commit_msg>Added better output if UI Automation tests fail<commit_after>require 'pty'

module Snapshot
  class Runner
    TRACE_DIR = '/tmp/snapshot_traces'

    def initialize
      Snapshot::DependencyChecker.check_dependencies
    end

    def work
      SnapshotConfig.shared_instance.js_file # to verify the file can be found

      Builder.new.build_app

      counter = 0
      SnapshotConfig.shared_instance.devices.each do |device|
        SnapshotConfig.shared_instance.languages.each do |language|

          begin
            run_tests(device, language)
            counter += copy_screenshots(language)
          rescue Exception => ex
            Helper.log.error(ex)
          end

        end
      end

      Helper.log.info "Successfully finished generating #{counter} screenshots.".green
      Helper.log.info "Check it out here: #{SnapshotConfig.shared_instance.screenshots_path}".green
      ReportsGenerator.new.generate
    end

    def clean_old_traces
      FileUtils.rm_rf(TRACE_DIR)
      FileUtils.mkdir_p(TRACE_DIR)
    end


    def run_tests(device, language)
      Helper.log.info "Running tests on #{device} in language #{language}".green
      app_path = Dir.glob("/tmp/snapshot/build/*.app").first

      clean_old_traces

      ENV['SNAPSHOT_LANGUAGE'] = language
      command = generate_test_command(device, language, app_path)
      
      retry_run = false

      lines = []
      PTY.spawn(command) do |stdin, stdout, pid|
        stdin.each do |line|
          lines << line
          begin
            result = parse_test_line(line)

            case result
              when :retry
                retry_run = true
              when :screenshot
                Helper.log.info "Successfully took screenshot 📱"
              end
            rescue Exception => ex
              Helper.log.error lines.join('\n')
              Helper.log.error ex.to_s.red
            end
{'fraction_non_alphanumeric': 0.07828983897834536, 'fraction_numerical': 0.000555247084952804, 'mean_word_length': 2.1549912434325744, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 6, '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': '1965221', 'n_tokens_mistral': 1058, 'n_tokens_neox': 1005, 'n_words': 237}