<commit_before>require 'spec_helper'
require 'uri'
require 'socket'
require 'securerandom'

describe Uploadcare::Api::File do
  before :each do
    @api = API
    @file = @api.upload IMAGE_URL
  end

  it 'file should be an instance of File' do
    @file.should be_an_instance_of Uploadcare::Api::File
  end

  it 'should not be initialized without correct UUID given' do
    expect {Uploadcare::Api::File.new(@api, "not-uuid")}.to raise_error
  end

  it 'should have valid url' do
    @file.uuid.should match UUID_REGEX
  end

  it 'should return public url' do
    @file.should respond_to :cdn_url
    @file.should respond_to :public_url
  end

  it 'public url should be valid url' do
    url = @file.cdn_url
    uri = URI.parse(url)
    uri.should be_kind_of(URI::HTTP)
  end

  it 'should be able to load image data' do
    expect {@file.load_data}.to_not raise_error
  end

  it 'should store itself' do
    expect { @file.store }.to_not raise_error
  end

  it 'file should respond with nil for :stored? and :deleted? methods unless loaded' do
    @file.is_loaded?.should == false
    @file.is_stored?.should == nil
    @file.is_deleted?.should == nil
  end

  it 'should be able to tell thenever file was stored' do
    @file.load
    expect(@file.stored?).to be(true)
    wait_until_ready(@file)
    @file.delete
    expect(@file.stored?).to be(false)
  end

  it 'should delete itself' do
    expect { @file.delete }.to_not raise_error
  end

  it 'should be able to tell thenever file was deleted' do
    @file.load
    @file.is_deleted?.should == false
    wait_until_ready(@file)
    @file.delete
    @file.is_deleted?.should == true
  end

  it 'should construct file from uuid' do
    file = @api.file @file.uuid
    file.should be_kind_of(Uploadcare::Api::File)
  end

  it 'should construct file from cdn url' do
    url = @file.cdn_url + "-/crop/150x150/center/-/format/png/"
    file = @api.file url
    file.should be_kind_of(Uploadcare::Api::File)
  end

  it 'shoul respond to datetime_ methods' do
    @file.load
    @file.should respond_to(:datetime_original)
    @file.should respond_to(:datetime_uploaded)
    @file.should respond_to(:datetime_stored)
    @file.should respond_to(:datetime_removed)
  end

  it 'should respond to datetime_uploaded' do
    @file.load
    @file.datetime_uploaded.should be_kind_of(DateTime)
  end

  it 'should respond to datetime_stored' do
    @file.load
    @file.store
    @file.datetime_stored.should be_kind_of(DateTime)
  end

  it 'should respond to datetime_removed' do
    @file.load
    wait_until_ready(@file)
    @file.delete
    @file.datetime_removed.should be_kind_of(DateTime)
    @file.datetime_deleted.should be_kind_of(DateTime)
    @file.datetime_removed.should == @file.datetime_deleted
  end


  it 'should copy itself' do
    # This can cause "File is not ready yet" error if ran too early
    # In this case we retry it 3 times before giving up
    result = retry_if(Uploadcare::Error::RequestError::BadRequest){@file.copy}
    result.should be_kind_of(Hash)
    result["type"].should == "file"
  end


  describe '#internal_copy' do
    describe 'integration' do
      it 'creates an internal copy of the file' do
        response = retry_if(Uploadcare::Error::RequestError::BadRequest){@file.internal_copy}

        expect( response['type'] ).to eq 'file'
        expect( response['result']['uuid'] ).not_to eq @file.uuid
      end
    end

    describe 'params' do
      let(:url_without_ops){ @api.file(SecureRandom.uuid).cdn_url }
      let(:url_with_ops){ url_without_ops + "-/crop/5x5/center/" }
      let(:file){ @api.file(url_with_ops) }

      context 'if no params given' do
        it 'requests server to create an unstored copy with operataions applied' do
          expect(@api).to receive(:post)
            .with('/files/', source: url_with_ops)

          file.internal_copy
        end
      end<commit_msg>Remove old `should` syntax from file spec<commit_after>require 'spec_helper'
require 'uri'
require 'socket'
require 'securerandom'

describe Uploadcare::Api::File do
  before :each do
    @api = API
    @file = @api.upload IMAGE_URL
  end

  it 'is an instance of File' do
    expect(@file).to be_an_instance_of Uploadcare::Api::File
  end

  it 'is not initialized without correct UUID given' do
    expect {Uploadcare::Api::File.new(@api, "not-uuid")}.to raise_error
  end

  it 'has a valid url' do
    expect(@file.uuid).to match UUID_REGEX
  end

  it 'returns a public url' do
    expect(@file).to respond_to :cdn_url
    expect(@file).to respond_to :public_url
  end

  it 'has a public url that is valid' do
    url = @file.cdn_url
    uri = URI.parse(url)
    expect(uri).to be_kind_of(URI::HTTP)
  end

  it 'is able to load image data' do
    expect {@file.load_data}.to_not raise_error
  end

  it 'stores itself' do
    expect { @file.store }.to_not raise_error
  end

  it 'responds with nil for :stored? and :deleted? methods unless loaded' do
    expect(@file.is_loaded?).to be false
    expect(@file.is_stored?).to be_nil
    expect(@file.is_deleted?).to be_nil
  end

  it 'is able to tell thenever file was stored' do
    @file.load
    expect(@file.stored?).to be(true)
    wait_until_ready(@file)
    @file.delete
    expect(@file.stored?).to be(false)
  end

  it 'deletes itself' do
    expect { @file.delete }.to_not raise_error
  end

  it 'is able to tell thenever file was deleted' do
    @file.load
    @file.is_deleted?.should == false
    wait_until_ready(@file)
    @file.delete
    expect(@file.is_deleted?).to be true
  end

  it 'constructs file from uuid' do
    file = @api.file @file.uuid
    expect(file).to be_kind_of(Uploadcare::Api::File)
  end

  it 'constructs file from cdn url' do
    url = @file.cdn_url + "-/crop/150x150/center/-/format/png/"
    file = @api.file url
    expect(file).to be_kind_of(Uploadcare::Api::File)
  end

  it 'responds to datetime_ methods' do
    @file.load
    expect(@file).to respond_to(:datetime_original)
    expect(@file).to respond_to(:datetime_uploaded)
    expect(@file).to respond_to(:datetime_stored)
    expect(@file).to respond_to(:datetime_removed)
  end

  it 'responds to datetime_uploaded' do
    @file.load
    expect(@file.datetime_uploaded).to be_kind_of(DateTime)
  end

  it 'responds to datetime_stored' do
    @file.load
    @file.store
    expect(@file.datetime_stored).to be_kind_of(DateTime)
  end

  it 'responds to datetime_removed' do
    @file.load
    wait_until_ready(@file)
    @file.delete
    expect(@file.datetime_removed).to be_kind_of(DateTime)
    expect(@file.datetime_deleted).to be_kind_of(DateTime)
    expect(@file.datetime_removed).to eq @file.datetime_deleted
  end


  it 'copies itself' do
    # This can cause "File is not ready yet" error if ran too early
    # In this case we retry it 3 times before giving up
    result = retry_if(Uploadcare::Error::RequestError::BadRequest){@file.copy}
    expect(result).to be_kind_of(Hash)
    expect(result["type"]).to eq "file"
  end


  describe '#internal_copy' do
    describe 'integration' do
      it 'creates an internal copy of the file' do
        response = retry_if(Uploadcare::Error::RequestError::BadRequest){@file.internal_copy}

        expect( response['type'] ).to eq 'file'
        expect( response['result']['uuid'] ).not_to eq @file.uuid
      end
    end

    describe 'params' do
      let(:url_without_ops){ @api.file(SecureRandom.uuid).cdn_url }
      let(:url_with_ops){ url_without_ops + "-/crop/5x5/center/" }
      let(:file){ @api.file(url_with_ops) }

      context 'if no params given' do
        it 'requests server to create an unstored copy with operataions applied' do
          expect(@api).to receive(:post)
            .with('/files/', source: url_with_ops)

          file.internal_copy
        end
      end
{'fraction_non_alphanumeric': 0.10791458892724716, 'fraction_numerical': 0.0023014959723820483, 'mean_word_length': 3.286027397260274, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3598371', 'n_tokens_mistral': 2631, 'n_tokens_neox': 2565, 'n_words': 719}