# typed: false
# frozen_string_literal: true
require "livecheck/strategy/page_match"
describe Homebrew::Livecheck::Strategy::PageMatch do
subject(:page_match) { described_class }
let(:url) { "https://brew.sh/blog/" }
let(:regex) { %r{href=.*?/homebrew[._-]v?(\d+(?:\.\d+)+)/?["' >]}i }
let(:page_content) {
<<~EOS
Homebrew — Homebrew
EOS
}
let(:page_content_matches) { ["2.6.0", "2.5.0", "2.4.0", "2.3.0", "2.2.0", "2.1.0", "2.0.0", "1.9.0"] }
let(:find_versions_return_hash) {
{
matches: {
"2.6.0" => Version.new("2.6.0"),
"2.5.0" => Version.new("2.5.0"),
"2.4.0" => Version.new("2.4.0"),
"2.3.0" => Version.new("2.3.0"),
"2.2.0" => Version.new("2.2.0"),
"2.1.0" => Version.new("2.1.0"),
"2.0.0" => Version.new("2.0.0"),
"1.9.0" => Version.new("1.9.0"),
},
regex: regex,
url: url,
}
}
let(:find_versions_cached_return_hash) {
return_hash = find_versions_return_hash
return_hash[:cached] = true
return_hash
}
describe "::match?" do
it "returns true for any URL" do
expect(page_match.match?(url)).to be true
end
end
describe "::page_matches" do
it "finds matching text in page content using a regex" do
expect(page_match.page_matches(page_content, regex)).to eq(page_content_matches)
end
it "finds matching text in page content using a strategy block" do
expect(page_match.page_matches(page_content, regex) { |content| content.scan(regex).map(&:first).uniq })
.to eq(page_content_matches)
end
end
describe "::find_versions?" do
it "finds versions in provided_content" do
expect(page_match.find_versions(url, regex, provided_content: page_content))
.to eq(find_versions_cached_return_hash)
end
end
end