# typed: true
# frozen_string_literal: true

require "bundle"
require "bundle/skipper"
require "bundle/dsl"

RSpec.describe Homebrew::Bundle::Skipper do
  subject(:skipper) { described_class }

  before do
    allow(ENV).to receive(:[]).and_return(nil)
    allow(ENV).to receive(:[]).with("HOMEBREW_BUNDLE_BREW_SKIP").and_return("mysql")
    allow(ENV).to receive(:[]).with("HOMEBREW_BUNDLE_TAP_SKIP").and_return("org/repo")
    allow(Formatter).to receive(:warning)
    skipper.skipped_entries = nil
    skipper.failed_taps = nil
  end

  describe ".skip?" do
    context "with a listed formula" do
      let(:entry) { Homebrew::Bundle::Dsl::Entry.new(:brew, "mysql") }

      it "returns true" do
        expect(skipper.skip?(entry)).to be true
      end
    end

    context "with an unbottled formula on ARM" do
      let(:entry) { Homebrew::Bundle::Dsl::Entry.new(:brew, "mysql") }

      it "returns true" do
        allow(Hardware::CPU).to receive(:arm?).and_return(true)
        allow(Homebrew).to receive(:default_prefix?).and_return(true)
        stub_formula_loader formula("mysql") {
          T.bind(self, T.class_of(Formula))
          url "mysql-1.0"
        }

        expect(skipper.skip?(entry)).to be true
      end
    end

    context "with an unlisted cask", :needs_macos do
      let(:entry) { Homebrew::Bundle::Dsl::Entry.new(:cask, "java") }

      it "returns false" do
        expect(skipper.skip?(entry)).to be false
      end
    end

    context "with a flatpak entry", :needs_macos do
      let(:entry) { Homebrew::Bundle::Dsl::Entry.new(:flatpak, "org.gnome.Calculator") }

      it "skips on macOS with warning" do
        expect($stdout).to receive(:puts).with(
          Formatter.warning("Skipping flatpak org.gnome.Calculator (unsupported on macOS)"),
        )
        expect(skipper.skip?(entry)).to be true
      end

      it "skips silently when silent flag is set" do
        expect($stdout).not_to receive(:puts)
        expect(skipper.skip?(entry, silent: true)).to be true
      end
    end

    context "with a WinGet entry", :needs_macos do
      let(:entry) { Homebrew::Bundle::Dsl::Entry.new(:winget, "Valve.Steam") }

      it "skips on macOS with warning" do
        expect($stdout).to receive(:puts).with(
          Formatter.warning("Skipping winget Valve.Steam (requires WSL)"),
        )
        expect(skipper.skip?(entry)).to be true
      end
    end

    context "with a flatpak entry on Linux", :needs_linux do
      let(:entry) { Homebrew::Bundle::Dsl::Entry.new(:flatpak, "org.gnome.Calculator") }

      it "does not skip" do
        expect(skipper.skip?(entry)).to be false
      end
    end

    context "with a WinGet entry on Linux outside WSL", :needs_linux do
      let(:entry) { Homebrew::Bundle::Dsl::Entry.new(:winget, "App Installer") }

      it "skips with warning" do
        allow(OS).to receive(:wsl?).and_return(false)
        expect($stdout).to receive(:puts).with(
          Formatter.warning("Skipping winget App Installer (requires WSL)"),
        )
        expect(skipper.skip?(entry)).to be true
      end
    end

    context "with a WinGet entry on WSL", :needs_linux do
      let(:entry) { Homebrew::Bundle::Dsl::Entry.new(:winget, "App Installer") }

      it "does not skip" do
        allow(OS).to receive(:wsl?).and_return(true)
        expect(skipper.skip?(entry)).to be false
      end
    end

    context "with a cask that requires macOS", :needs_linux do
      let(:entry) { Homebrew::Bundle::Dsl::Entry.new(:cask, "testball") }

      it "skips on Linux with warning" do
        allow(Cask::CaskLoader).to receive(:load).with("testball").and_return(
          instance_double(Cask::Cask, supports_linux?: false),
        )
        expect($stdout).to receive(:puts).with(
          Formatter.warning("Skipping cask testball (requires macOS)"),
        )
        expect(skipper.skip?(entry)).to be true
      end
    end

    context "with a platform-agnostic cask on Linux", :needs_linux do
      let(:entry) { Homebrew::Bundle::Dsl::Entry.new(:cask, "testball") }

      it "does not skip" do
        allow(Cask::CaskLoader).to receive(:load).with("testball").and_return(
          instance_double(Cask::Cask, supports_linux?: true),
        )
        expect(skipper.skip?(entry)).to be false
      end
    end

    context "with a cask from an untapped tap on Linux", :needs_linux do
      let(:entry) do
        Homebrew::Bundle::Dsl::Entry.new(:cask, "vscodium-linux", full_name: "ublue-os/tap/vscodium-linux")
      end

      it "does not skip when cask can't be loaded" do
        allow(Cask::CaskLoader).to receive(:load).with("vscodium-linux").and_raise(Cask::CaskUnavailableError.new("vscodium-linux"))
        expect(skipper.skip?(entry)).to be false
      end
    end

    context "with a listed formula in a failed tap" do
      let(:entry) { Homebrew::Bundle::Dsl::Entry.new(:brew, "org/repo/formula") }

      it "returns true" do
        skipper.tap_failed!("org/repo")

        expect(skipper.skip?(entry)).to be true
      end
    end
  end

  describe ".failed_tap!" do
    context "with a tap" do
      let(:tap) { Homebrew::Bundle::Dsl::Entry.new(:tap, "org/repo-b") }
      let(:entry) { Homebrew::Bundle::Dsl::Entry.new(:brew, "org/repo-b/formula") }

      it "returns false" do
        expect(skipper.skip?(entry)).to be false

        skipper.tap_failed! tap.name

        expect(skipper.skip?(entry)).to be true
      end
    end
  end
end
