require 'rails_helper'

RSpec.describe Dunlop::FileZip do

  let(:zipped_filename) { fixture_path.join("file_zip/sip24_kvd_1.zip") }
  let(:gzipped_filename) { fixture_path.join("file_zip/sip24_kvd_1.gz") }
  let(:unzipped_filename) { fixture_path.join("file_zip/sip24_kvd_1.txt") }
  let(:destination_filename) { "/tmp/sip24_kvd_1.txt" }

  before do
    FileUtils.rm_f(destination_filename)
  end

  after do
    FileUtils.rm_f(destination_filename)
  end

  context ".unzip" do

    context "mime-type: application/zip" do

      it "unzips the file to the provided filename" do
        expect(File.exists?(destination_filename)).to be_falsey
        subject.unzip(zipped_filename, destination_filename)
        expect(File.exists?(destination_filename)).to be_truthy
        expect(File.exists?(zipped_filename)).to be_truthy
        expect(File.read(destination_filename)).to eq File.read(unzipped_filename)
      end



    end

    context "mime-type: application/x-gzip" do

      it "unzips the file to the provided filename" do
        expect(File.exists?(destination_filename)).to be_falsey
        subject.unzip(gzipped_filename, destination_filename)
        expect(File.exists?(destination_filename)).to be_truthy
        expect(File.exists?(gzipped_filename)).to be_truthy
        expect(File.read(destination_filename)).to eq File.read(unzipped_filename)
      end

    end

    context "mime-type: text/plain" do

      it "copies the file to the provided filename" do
        expect(File.exists?(destination_filename)).to be_falsey
        subject.unzip(unzipped_filename, destination_filename)
        expect(File.exists?(destination_filename)).to be_truthy
        expect(File.exists?(unzipped_filename)).to be_truthy
        expect(File.read(destination_filename)).to eq File.read(unzipped_filename)
      end

    end

  end

  context ".gzip" do
    let(:gzipped_filename) { "/tmp/sip24_kvd_1.txt.gz" }

    before do
      FileUtils.rm_f(gzipped_filename)
    end

    after do
      FileUtils.rm_f(gzipped_filename)
    end

    it "gzips the file to the provided filename" do
      expect(File.exists?(gzipped_filename)).to be_falsey
      subject.gzip(unzipped_filename, gzipped_filename)
      expect(File.exists?(gzipped_filename)).to be_truthy
      expect(File.exists?(unzipped_filename)).to be_truthy

      expect(File.size(unzipped_filename)).to eq 305
      expect(File.size(gzipped_filename)).to eq 153
      expect(subject.mime_type(gzipped_filename)).to eq 'application/x-gzip'
    end

  end

end
