# typed: true
# frozen_string_literal: true

require "language/node"
require "utils/shebang"

RSpec.describe Language::Node::Shebang do
  let(:file) { Tempfile.new("node-shebang") }
  let(:broken_file) { Tempfile.new("node-shebang") }
  let(:f) do
    f = {}

    f[:node18] = formula "node@18" do
      T.bind(self, T.class_of(Formula))
      url "https://brew.sh/node-18.0.0.tgz"
    end

    f[:versioned_node_dep] = formula "foo" do
      T.bind(self, T.class_of(Formula))
      url "https://brew.sh/foo-1.0.tgz"

      depends_on "node@18"
    end

    f[:no_deps] = formula "foo" do
      T.bind(self, T.class_of(Formula))
      url "https://brew.sh/foo-1.0.tgz"
    end

    f[:multiple_deps] = formula "foo" do
      T.bind(self, T.class_of(Formula))
      url "https://brew.sh/foo-1.0.tgz"

      depends_on "node"
      depends_on "node@18"
    end

    f
  end

  before do
    file.write <<~EOS
      #!/usr/bin/env node
      a
      b
      c
    EOS
    file.flush
    broken_file.write <<~EOS
      #!node
      a
      b
      c
    EOS
    broken_file.flush
  end

  after { [file, broken_file].each(&:unlink) }

  describe "#detected_node_shebang" do
    it "can be used to replace Node shebangs" do
      allow(Formulary).to receive(:factory).with(f[:node18].name).and_return(f[:node18])
      Utils::Shebang.rewrite_shebang described_class.detected_node_shebang(f[:versioned_node_dep]), file.path

      expect(File.read(file)).to eq <<~EOS
        #!#{HOMEBREW_PREFIX/"opt/node@18/bin/node"}
        a
        b
        c
      EOS
    end

    it "can fix broken shebang like `#!node`" do
      allow(Formulary).to receive(:factory).with(f[:node18].name).and_return(f[:node18])
      Utils::Shebang.rewrite_shebang described_class.detected_node_shebang(f[:versioned_node_dep]),
                                     broken_file.path

      expect(File.read(broken_file)).to eq <<~EOS
        #!#{HOMEBREW_PREFIX/"opt/node@18/bin/node"}
        a
        b
        c
      EOS
    end

    it "errors if formula doesn't depend on node" do
      expect { Utils::Shebang.rewrite_shebang described_class.detected_node_shebang(f[:no_deps]), file.path }
        .to raise_error(ShebangDetectionError, "Cannot detect Node shebang: formula does not depend on Node.")
    end

    it "errors if formula depends on more than one node" do
      expect { Utils::Shebang.rewrite_shebang described_class.detected_node_shebang(f[:multiple_deps]), file.path }
        .to raise_error(ShebangDetectionError, "Cannot detect Node shebang: formula has multiple Node dependencies.")
    end
  end
end
