2019-04-29 19:27:42 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 21:27:38 -05:00
|
|
|
RSpec.describe StylesheetCache do
|
2021-07-16 09:58:01 -05:00
|
|
|
describe ".add" do
|
2015-05-05 00:50:13 -05:00
|
|
|
it "correctly cycles once MAX_TO_KEEP is hit" do
|
2017-05-03 10:31:16 -05:00
|
|
|
StylesheetCache.destroy_all
|
|
|
|
|
2015-05-05 00:50:13 -05:00
|
|
|
(StylesheetCache::MAX_TO_KEEP + 1).times do |i|
|
2017-04-12 09:52:52 -05:00
|
|
|
StylesheetCache.add("a", "d" + i.to_s, "c" + i.to_s, "map")
|
2015-05-05 00:50:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
expect(StylesheetCache.count).to eq StylesheetCache::MAX_TO_KEEP
|
|
|
|
expect(StylesheetCache.order(:id).first.content).to eq "c1"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does nothing if digest is set and already exists" do
|
2017-08-16 09:59:38 -05:00
|
|
|
StylesheetCache.delete_all
|
2017-05-03 10:31:16 -05:00
|
|
|
|
2017-08-16 09:59:38 -05:00
|
|
|
expect(StylesheetCache.add("a", "b", "c", "map")).to be_present
|
|
|
|
expect(StylesheetCache.add("a", "b", "cc", "map")).to eq(false)
|
2015-05-05 00:50:13 -05:00
|
|
|
|
|
|
|
expect(StylesheetCache.count).to eq 1
|
|
|
|
expect(StylesheetCache.first.content).to eq "c"
|
|
|
|
end
|
|
|
|
|
2018-09-13 21:54:11 -05:00
|
|
|
it "it retains stylesheets for competing targets" do
|
|
|
|
StylesheetCache.destroy_all
|
|
|
|
|
|
|
|
StylesheetCache.add("desktop", SecureRandom.hex, "body { }", "map", max_to_keep: 2)
|
|
|
|
StylesheetCache.add("desktop", SecureRandom.hex, "body { }", "map", max_to_keep: 2)
|
|
|
|
StylesheetCache.add("mobile", SecureRandom.hex, "body { }", "map", max_to_keep: 2)
|
|
|
|
StylesheetCache.add("mobile", SecureRandom.hex, "body { }", "map", max_to_keep: 2)
|
|
|
|
StylesheetCache.add("mobile", SecureRandom.hex, "body { }", "map", max_to_keep: 2)
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
expect(StylesheetCache.order(:id).pluck(:target)).to eq(%w[desktop desktop mobile mobile])
|
2018-09-13 21:54:11 -05:00
|
|
|
end
|
2021-07-16 09:58:01 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe ".clean_up" do
|
|
|
|
it "removes items older than threshold" do
|
|
|
|
StylesheetCache.destroy_all
|
|
|
|
|
|
|
|
StylesheetCache.add("a", "b", "c", "map")
|
|
|
|
StylesheetCache.add("d", "e", "f", "map")
|
2018-09-13 21:54:11 -05:00
|
|
|
|
2021-07-16 09:58:01 -05:00
|
|
|
above_threshold = StylesheetCache::CLEANUP_AFTER_DAYS - 1
|
|
|
|
StylesheetCache.first.update!(created_at: above_threshold.days.ago)
|
|
|
|
|
|
|
|
StylesheetCache.clean_up
|
|
|
|
expect(StylesheetCache.all.size).to eq(2)
|
|
|
|
|
|
|
|
below_threshold = StylesheetCache::CLEANUP_AFTER_DAYS + 1
|
|
|
|
StylesheetCache.first.update!(created_at: below_threshold.days.ago)
|
|
|
|
|
|
|
|
StylesheetCache.clean_up
|
|
|
|
expect(StylesheetCache.all.size).to eq(1)
|
|
|
|
end
|
2015-05-05 00:50:13 -05:00
|
|
|
end
|
|
|
|
end
|