Skip to content

Commit

Permalink
Add Gemfile(.lock), add rspec tests for bangs jsons, add rspec to CI
Browse files Browse the repository at this point in the history
  • Loading branch information
nobodywasishere committed Mar 15, 2024
1 parent a64bf8d commit e739d93
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 1 deletion.
1 change: 1 addition & 0 deletions .bundle/config
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
BUNDLE_PATH: ./vendor/bundle
16 changes: 15 additions & 1 deletion .github/workflows/validate_json.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- name: Validate JSON
uses: walbo/[email protected]
with:
files: ./data/bangs.json
files: data/*.json

# optional, defaults to `$schema` in your JSON file
schema: ./data/bangs.schema.json
Expand All @@ -37,3 +37,17 @@ jobs:

# optional, default: true
allow-union-types: true
run-rspec-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
# Not needed with a .ruby-version file
ruby-version: 3.0
# runs 'bundle install' and caches installed gems automatically
bundler-cache: true
- name: Run tests
run: |
bundle exec rspec
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.DS_Store
/vendor
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
source 'https://rubygems.org'

gem "json"
gem "rspec", "~> 3.13"
gem 'addressable', '~> 2.8', '>= 2.8.6'
33 changes: 33 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
GEM
remote: https://rubygems.org/
specs:
addressable (2.8.6)
public_suffix (>= 2.0.2, < 6.0)
diff-lcs (1.5.1)
json (2.7.1)
public_suffix (5.0.4)
rspec (3.13.0)
rspec-core (~> 3.13.0)
rspec-expectations (~> 3.13.0)
rspec-mocks (~> 3.13.0)
rspec-core (3.13.0)
rspec-support (~> 3.13.0)
rspec-expectations (3.13.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-mocks (3.13.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-support (3.13.1)

PLATFORMS
ruby
x86_64-linux

DEPENDENCIES
addressable (~> 2.8, >= 2.8.6)
json
rspec (~> 3.13)

BUNDLED WITH
2.5.4
82 changes: 82 additions & 0 deletions spec/bangs_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
require "json"
require "rspec"
require "addressable"

bangs_json = JSON.parse(File.read("data/bangs.json"))
bang_triggers = bangs_json.map{|b| b["t"]}

kagi_bangs_json = JSON.parse(File.read("data/kagi_bangs.json"))
kagi_triggers = kagi_bangs_json.map{|b| b["t"]}

assist_bangs_json = JSON.parse(File.read("data/assistant_bangs.json"))
assistant_triggers = assist_bangs_json.map{|b| b["t"]}

def find_dups(*arr)
arr.map{ |a| a.uniq }
.flatten
.group_by { |element| element }
.select { |k, v| v.size > 1 }
.keys
end

def match_domains(bangs)
bangs.each do |bang|
next if bang["u"].start_with?("/")

it "domains match template urls (#{bang["s"]})" do
template = bang["u"].gsub("{{{s}}}", "example").gsub("www.", "")
uri = Addressable::URI.parse(template)
domain = bang["d"].gsub("{{{s}}}", "example").gsub("www.", "")

expect(domain).to eq(uri.host)
end
end
end

describe "bangs.json" do
it "doesn't have duplicate bang triggers" do
dups = find_dups(bang_triggers)

expect(dups).to be_empty, "Duplicate(s) found: #{dups.join(", ")}"
end

it "and kagi_bangs.json don't have duplicate bang triggers" do
dups = find_dups(bang_triggers, kagi_triggers)

expect(dups).to be_empty, "Duplicate(s) found: #{dups.join(", ")}"
end

it "and assist_bangs.json don't have duplicate bang triggers" do
dups = find_dups(bang_triggers, assistant_triggers)

expect(dups).to be_empty, "Duplicate(s) found: #{dups.join(", ")}"
end

match_domains(bangs_json)
end

describe "kagi_bangs.json" do
it "doesn't have duplicate bang triggers" do
dups = find_dups(kagi_triggers)

expect(dups).to be_empty, "Duplicate(s) found: #{dups.join(", ")}"
end

it "and assistant_bangs.json don't have duplicate bang triggers" do
dups = find_dups(kagi_triggers, assistant_triggers)

expect(dups).to be_empty, "Duplicate trigger(s) found: #{dups.join(", ")}"
end

match_domains(kagi_bangs_json)
end

describe "assistant_bangs.json" do
it "doesn't have duplicate bang triggers" do
dups = find_dups(assistant_triggers)

expect(dups).to be_empty, "Duplicate(s) found: #{dups.join(", ")}"
end

match_domains(assist_bangs_json)
end

0 comments on commit e739d93

Please sign in to comment.