Browse Source

Remove "previously changed" state

webui
aeris 4 years ago
parent
commit
39bef45c92
  1. 2
      Gemfile
  2. 2
      Gemfile.lock
  3. 2
      app/models/check.rb
  4. 3
      app/models/site.rb
  5. 1
      bin/cli.rb
  6. 61
      spec/models/site_spec.rb

2
Gemfile

@ -17,7 +17,7 @@ gem 'diffy'
group :development, :test do
gem 'pry-byebug'
gem 'timecop'
gem 'rspec-rails'
end

2
Gemfile.lock

@ -154,6 +154,7 @@ GEM
sqlite3 (1.3.13)
thor (0.20.0)
thread_safe (0.3.6)
timecop (0.9.1)
tzinfo (1.2.5)
thread_safe (~> 0.1)
websocket-driver (0.6.5)
@ -183,6 +184,7 @@ DEPENDENCIES
spring-watcher-listen (~> 2.0.0)
sqlite3
thor
timecop
tzinfo-data
BUNDLED WITH

2
app/models/check.rb

@ -9,8 +9,6 @@ class Check < ApplicationRecord
end
def diff!(content, debug: false)
return :previously_changed if self.changed_at
self.checked_at = Time.now
state = :unchanged

3
app/models/site.rb

@ -50,7 +50,7 @@ class Site < ApplicationRecord
self.checks.each { |c| c.reference! content }
end
STATES = %i[unchanged previously_changed changed error].freeze
STATES = %i[unchanged changed error].freeze
def update_state(current, state)
current_index = STATES.index current
@ -93,7 +93,6 @@ class Site < ApplicationRecord
end
def check(debug: false)
return :previously_changed if self.changed_at
reference = self.reference
response = self.class.grab self.url
content = response.body

1
bin/cli.rb

@ -36,7 +36,6 @@ class App < Thor
COLORS = {
reference: :blue,
unchanged: :green,
previously_changed: :light_red,
changed: :red,
error: { background: :red }
}.freeze

61
spec/models/site_spec.rb

@ -1,10 +1,9 @@
RSpec.describe Site, type: :model do
REFERENCE_TARGET = '<div id="content">bar</div>'
REFERENCE = "<html><body>foo #{REFERENCE_TARGET}</body></html>"
REFERENCE_TARGET = '<div id="content">bar</div>'
REFERENCE = "<html><body>foo #{REFERENCE_TARGET}</body></html>"
CHANGE_OUTSIDE_TARGET = '<html><body>baz <div id="content">bar</div></body></html>'
CHANGE_TARGET = '<div id="content">baz</div>'
CHANGE_INSIDE_TARGET = "<html><body>foo #{CHANGE_TARGET}</body></html>"
CHANGE_TARGET = '<div id="content">baz</div>'
CHANGE_INSIDE_TARGET = "<html><body>foo #{CHANGE_TARGET}</body></html>"
let :site do
Site.create! url: 'http://localhost/'
@ -24,13 +23,17 @@ RSpec.describe Site, type: :model do
end
def check!(content)
site.reference! REFERENCE
stub_page content
site.check
end
def reference_and_check!(content)
site.reference! REFERENCE
self.check! content
end
it 'must not change if no change with no check' do
status = check! REFERENCE
status = reference_and_check! REFERENCE
expect(status).to be :unchanged
expect(site.changed_at).to be_nil
@ -40,7 +43,7 @@ RSpec.describe Site, type: :model do
it 'must not change if no change with checks' do
check = add_check css: '#content'
status = check! REFERENCE
status = reference_and_check! REFERENCE
expect(status).to be :unchanged
expect(site.changed_at).to be_nil
@ -51,16 +54,16 @@ RSpec.describe Site, type: :model do
end
it 'must change if change with no check' do
status = check! CHANGE_OUTSIDE_TARGET
status = reference_and_check! CHANGE_OUTSIDE_TARGET
expect(status).to be :changed
expect(site.changed_at).not_to be_nil
expect(site.content).not_to eq REFERENCE
expect(site.content).to eq CHANGE_OUTSIDE_TARGET
end
it 'must not change if change but no check changed' do
check = add_check css: '#content'
status = check! CHANGE_OUTSIDE_TARGET
check = add_check css: '#content'
status = reference_and_check! CHANGE_OUTSIDE_TARGET
expect(status).to be :unchanged
expect(site.changed_at).to be_nil
@ -71,8 +74,8 @@ RSpec.describe Site, type: :model do
end
it 'must change if check changed' do
check = add_check css: '#content'
status = check! CHANGE_INSIDE_TARGET
check = add_check css: '#content'
status = reference_and_check! CHANGE_INSIDE_TARGET
expect(status).to be :changed
expect(site.changed_at).not_to be_nil
@ -81,4 +84,34 @@ RSpec.describe Site, type: :model do
expect(check.changed_at).not_to be_nil
expect(check.content).to eq CHANGE_TARGET
end
it 'must stay changed if no change after a change' do
first_date = Date.parse '2018-01-01'
second_date = first_date + 1
third_date = second_date + 1
Timecop.freeze first_date do
status = reference_and_check! CHANGE_OUTSIDE_TARGET
expect(status).to be :changed
expect(site.changed_at).to eq first_date
expect(site.content).to eq CHANGE_OUTSIDE_TARGET
end
Timecop.freeze second_date do
status = check! CHANGE_OUTSIDE_TARGET
expect(status).to be :unchanged
expect(site.changed_at).to eq first_date
expect(site.content).to eq CHANGE_OUTSIDE_TARGET
end
Timecop.freeze third_date do
status = check! CHANGE_INSIDE_TARGET
expect(status).to be :changed
expect(site.changed_at).to eq third_date
expect(site.content).to eq CHANGE_INSIDE_TARGET
end
end
end

Loading…
Cancel
Save