# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: BUSL-1.1

require_relative "../../../../base"

describe "VagrantPlugins::GuestWindows::Cap::FileSystem" do
  let(:caps) do
    VagrantPlugins::GuestWindows::Plugin
      .components
      .guest_capabilities[:windows]
  end

  let(:machine) { double("machine", communicate: comm) }
  let(:comm) { double("comm") }

  before { allow(comm).to receive(:execute) }

  describe ".create_tmp_path" do
    let(:cap) { caps.get(:create_tmp_path) }
    let(:opts) { {} }

    it "should generate path on guest" do
      expect(comm).to receive(:execute).with(/GetRandomFileName/, any_args)
      cap.create_tmp_path(machine, opts)
    end

    it "should capture path generated on guest" do
      expect(comm).to receive(:execute).with(/Write-Output/, any_args).and_yield(:stdout, "TMP_PATH")
      expect(cap.create_tmp_path(machine, opts)).to eq("TMP_PATH")
    end

    it "should strip newlines on path" do
      expect(comm).to receive(:execute).with(/Write-Output/, any_args).and_yield(:stdout, "TMP_PATH\r\n")
      expect(cap.create_tmp_path(machine, opts)).to eq("TMP_PATH")
    end

    context "when type is a directory" do
      before { opts[:type] = :directory }

      it "should create guest path as a directory" do
        expect(comm).to receive(:execute).with(/CreateDirectory/, any_args)
        cap.create_tmp_path(machine, opts)
      end
    end
  end

  describe ".decompress_zip" do
    let(:cap) { caps.get(:decompress_zip) }
    let(:comp) { "compressed_file" }
    let(:dest) { "path/to/destination" }
    let(:opts) { {} }

    before { allow(cap).to receive(:create_tmp_path).and_return("TMP_DIR") }
    after{ cap.decompress_zip(machine, comp, dest, opts) }

    it "should create temporary directory for extraction" do
      expect(cap).to receive(:create_tmp_path)
    end

    it "should extract file with zip" do
      expect(comm).to receive(:execute).with(/copyhere/, any_args)
    end

    it "should extract file to temporary directory" do
      expect(comm).to receive(:execute).with(/TMP_DIR/, any_args)
    end

    it "should remove compressed file from guest" do
      expect(comm).to receive(:execute).with(/Remove-Item .*#{comp}/, any_args)
    end

    it "should remove extraction directory from guest" do
      expect(comm).to receive(:execute).with(/Remove-Item .*TMP_DIR/, any_args)
    end

    it "should create parent directories for destination" do
      expect(comm).to receive(:execute).with(/New-Item .*Directory .*to\\"/, any_args)
    end

    context "when type is directory" do
      before { opts[:type] = :directory }

      it "should create destination directory" do
        expect(comm).to receive(:execute).with(/New-Item .*Directory .*destination"/, any_args)
      end
    end
  end
end
