2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-28 10:27:38 +08:00
|
|
|
RSpec.describe DiscourseUpdates do
|
2013-07-03 23:06:07 +08:00
|
|
|
def stub_data(latest, missing, critical, updated_at)
|
2021-11-09 14:20:09 +08:00
|
|
|
DiscourseUpdates.latest_version = latest
|
|
|
|
DiscourseUpdates.missing_versions_count = missing
|
|
|
|
DiscourseUpdates.critical_updates_available = critical
|
|
|
|
DiscourseUpdates.updated_at = updated_at
|
2013-07-03 23:06:07 +08:00
|
|
|
end
|
|
|
|
|
2023-06-21 22:00:19 +08:00
|
|
|
subject(:version) { DiscourseUpdates.check_version }
|
2013-07-03 23:06:07 +08:00
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "when version check was done at the current installed version" do
|
2021-11-09 14:20:09 +08:00
|
|
|
before { DiscourseUpdates.last_installed_version = Discourse::VERSION::STRING }
|
2013-07-03 23:06:07 +08:00
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "when a good version check request happened recently" do
|
|
|
|
context "when server is up-to-date" do
|
2020-03-11 05:13:17 +08:00
|
|
|
let(:time) { 12.hours.ago }
|
|
|
|
before { stub_data(Discourse::VERSION::STRING, 0, false, time) }
|
2013-11-05 01:51:01 +08:00
|
|
|
|
|
|
|
it "returns all the version fields" do
|
2023-06-21 22:00:19 +08:00
|
|
|
expect(version.latest_version).to eq(Discourse::VERSION::STRING)
|
|
|
|
expect(version.missing_versions_count).to eq(0)
|
|
|
|
expect(version.critical_updates).to eq(false)
|
|
|
|
expect(version.installed_version).to eq(Discourse::VERSION::STRING)
|
|
|
|
expect(version.stale_data).to eq(false)
|
2013-11-05 01:51:01 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns the timestamp of the last version check" do
|
2023-06-21 22:00:19 +08:00
|
|
|
expect(version.updated_at).to be_within_one_second_of(time)
|
2013-11-05 01:51:01 +08:00
|
|
|
end
|
2013-07-03 23:06:07 +08:00
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "when server is not up-to-date" do
|
2020-03-11 05:13:17 +08:00
|
|
|
let(:time) { 12.hours.ago }
|
|
|
|
before { stub_data("0.9.0", 2, false, time) }
|
2013-11-05 01:51:01 +08:00
|
|
|
|
|
|
|
it "returns all the version fields" do
|
2023-06-21 22:00:19 +08:00
|
|
|
expect(version.latest_version).to eq("0.9.0")
|
|
|
|
expect(version.missing_versions_count).to eq(2)
|
|
|
|
expect(version.critical_updates).to eq(false)
|
|
|
|
expect(version.installed_version).to eq(Discourse::VERSION::STRING)
|
2013-11-05 01:51:01 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns the timestamp of the last version check" do
|
2023-06-21 22:00:19 +08:00
|
|
|
expect(version.updated_at).to be_within_one_second_of(time)
|
2013-11-05 01:51:01 +08:00
|
|
|
end
|
2013-07-03 23:06:07 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "when a version check has never been performed" do
|
2013-11-05 01:51:01 +08:00
|
|
|
before { stub_data(nil, nil, false, nil) }
|
2013-07-03 23:06:07 +08:00
|
|
|
|
2013-11-05 01:51:01 +08:00
|
|
|
it "returns the installed version" do
|
2023-06-21 22:00:19 +08:00
|
|
|
expect(version.installed_version).to eq(Discourse::VERSION::STRING)
|
2013-07-03 23:06:07 +08:00
|
|
|
end
|
|
|
|
|
2013-11-05 01:51:01 +08:00
|
|
|
it "indicates that version check has not been performed" do
|
2023-06-21 22:00:19 +08:00
|
|
|
expect(version.updated_at).to eq(nil)
|
|
|
|
expect(version.stale_data).to eq(true)
|
2013-07-03 23:06:07 +08:00
|
|
|
end
|
|
|
|
|
2013-11-05 01:51:01 +08:00
|
|
|
it "does not return latest version info" do
|
2023-06-21 22:00:19 +08:00
|
|
|
expect(version.latest_version).to eq(nil)
|
|
|
|
expect(version.missing_versions_count).to eq(nil)
|
|
|
|
expect(version.critical_updates).to eq(nil)
|
2013-11-05 01:51:01 +08:00
|
|
|
end
|
2013-07-03 23:06:07 +08:00
|
|
|
|
2013-11-05 01:51:01 +08:00
|
|
|
it "queues a version check" do
|
2024-02-23 14:12:28 +08:00
|
|
|
expect_enqueued_with(job: :call_discourse_hub) { version }
|
2013-11-05 01:51:01 +08:00
|
|
|
end
|
2013-07-03 23:06:07 +08:00
|
|
|
end
|
|
|
|
|
2013-11-05 01:51:01 +08:00
|
|
|
# These cases should never happen anymore, but keep the specs to be sure
|
|
|
|
# they're handled in a sane way.
|
2022-07-28 00:14:14 +08:00
|
|
|
context "with old version check data" do
|
2013-11-05 01:51:01 +08:00
|
|
|
shared_examples "queue version check and report that version is ok" do
|
|
|
|
it "queues a version check" do
|
2024-02-23 14:12:28 +08:00
|
|
|
expect_enqueued_with(job: :call_discourse_hub) { version }
|
2013-11-05 01:51:01 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "reports 0 missing versions" do
|
2023-06-21 22:00:19 +08:00
|
|
|
expect(version.missing_versions_count).to eq(0)
|
2013-11-05 01:51:01 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "reports that a version check will be run soon" do
|
2023-06-21 22:00:19 +08:00
|
|
|
expect(version.version_check_pending).to eq(true)
|
2013-11-05 01:51:01 +08:00
|
|
|
end
|
|
|
|
end
|
2013-07-03 23:06:07 +08:00
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "when installed is latest" do
|
2013-11-05 01:51:01 +08:00
|
|
|
before { stub_data(Discourse::VERSION::STRING, 1, false, 8.hours.ago) }
|
|
|
|
include_examples "queue version check and report that version is ok"
|
|
|
|
end
|
2013-07-03 23:06:07 +08:00
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "when installed does not match latest version, but missing_versions_count is 0" do
|
2013-11-05 01:51:01 +08:00
|
|
|
before { stub_data("0.10.10.123", 0, false, 8.hours.ago) }
|
|
|
|
include_examples "queue version check and report that version is ok"
|
|
|
|
end
|
2013-07-03 23:06:07 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "when version check was done at a different installed version" do
|
2021-11-09 14:20:09 +08:00
|
|
|
before { DiscourseUpdates.last_installed_version = "0.9.1" }
|
2013-07-31 00:11:51 +08:00
|
|
|
|
2013-11-05 01:51:01 +08:00
|
|
|
shared_examples "when last_installed_version is old" do
|
2013-07-31 00:11:51 +08:00
|
|
|
it "queues a version check" do
|
2024-02-23 14:12:28 +08:00
|
|
|
expect_enqueued_with(job: :call_discourse_hub) { version }
|
2013-07-31 00:11:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "reports 0 missing versions" do
|
2023-06-21 22:00:19 +08:00
|
|
|
expect(version.missing_versions_count).to eq(0)
|
2013-07-31 00:11:51 +08:00
|
|
|
end
|
2013-11-05 01:51:01 +08:00
|
|
|
|
|
|
|
it "reports that a version check will be run soon" do
|
2023-06-21 22:00:19 +08:00
|
|
|
expect(version.version_check_pending).to eq(true)
|
2013-11-05 01:51:01 +08:00
|
|
|
end
|
2013-07-31 00:11:51 +08:00
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "when missing_versions_count is 0" do
|
2013-11-05 01:51:01 +08:00
|
|
|
before { stub_data("0.9.7", 0, false, 8.hours.ago) }
|
|
|
|
include_examples "when last_installed_version is old"
|
|
|
|
end
|
2013-07-31 00:11:51 +08:00
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "when missing_versions_count is not 0" do
|
2013-11-05 01:51:01 +08:00
|
|
|
before { stub_data("0.9.7", 1, false, 8.hours.ago) }
|
|
|
|
include_examples "when last_installed_version is old"
|
2013-07-31 00:11:51 +08:00
|
|
|
end
|
|
|
|
end
|
2021-01-22 23:09:02 +08:00
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
describe "new features" do
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:admin)
|
2021-01-22 23:09:02 +08:00
|
|
|
fab!(:admin2) { Fabricate(:admin) }
|
|
|
|
let!(:last_item_date) { 5.minutes.ago }
|
|
|
|
let!(:sample_features) do
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"emoji" => "🤾",
|
|
|
|
"title" => "Super Fruits",
|
|
|
|
"description" => "Taste explosion!",
|
|
|
|
"created_at" => 40.minutes.ago,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"emoji" => "🙈",
|
|
|
|
"title" => "Fancy Legumes",
|
|
|
|
"description" => "Magic legumes!",
|
|
|
|
"created_at" => 15.minutes.ago,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"emoji" => "🤾",
|
|
|
|
"title" => "Quality Veggies",
|
|
|
|
"description" => "Green goodness!",
|
|
|
|
"created_at" => last_item_date,
|
|
|
|
},
|
|
|
|
]
|
2023-01-09 19:18:21 +08:00
|
|
|
end
|
2021-01-22 23:09:02 +08:00
|
|
|
|
|
|
|
before(:each) do
|
|
|
|
Discourse.redis.del "new_features_last_seen_user_#{admin.id}"
|
|
|
|
Discourse.redis.del "new_features_last_seen_user_#{admin2.id}"
|
|
|
|
Discourse.redis.set("new_features", MultiJson.dump(sample_features))
|
|
|
|
end
|
|
|
|
|
2022-12-23 20:41:30 +08:00
|
|
|
after { DiscourseUpdates.clean_state }
|
2022-12-23 11:17:42 +08:00
|
|
|
|
2021-01-22 23:09:02 +08:00
|
|
|
it "returns all items on the first run" do
|
2021-02-11 02:12:04 +08:00
|
|
|
result = DiscourseUpdates.new_features
|
2021-01-22 23:09:02 +08:00
|
|
|
|
|
|
|
expect(result.length).to eq(3)
|
|
|
|
expect(result[2]["title"]).to eq("Super Fruits")
|
|
|
|
end
|
|
|
|
|
2021-02-11 02:12:04 +08:00
|
|
|
it "correctly marks unseen items by user" do
|
2021-01-22 23:09:02 +08:00
|
|
|
DiscourseUpdates.stubs(:new_features_last_seen).with(admin.id).returns(10.minutes.ago)
|
|
|
|
DiscourseUpdates.stubs(:new_features_last_seen).with(admin2.id).returns(30.minutes.ago)
|
|
|
|
|
2021-02-11 02:12:04 +08:00
|
|
|
expect(DiscourseUpdates.has_unseen_features?(admin.id)).to eq(true)
|
|
|
|
expect(DiscourseUpdates.has_unseen_features?(admin2.id)).to eq(true)
|
2021-01-22 23:09:02 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "can mark features as seen for a given user" do
|
2021-02-11 02:12:04 +08:00
|
|
|
expect(DiscourseUpdates.has_unseen_features?(admin.id)).to be_truthy
|
2021-01-22 23:09:02 +08:00
|
|
|
|
|
|
|
DiscourseUpdates.mark_new_features_as_seen(admin.id)
|
2021-02-11 02:12:04 +08:00
|
|
|
expect(DiscourseUpdates.has_unseen_features?(admin.id)).to eq(false)
|
2021-01-22 23:09:02 +08:00
|
|
|
|
|
|
|
# doesn't affect another user
|
2021-02-11 02:12:04 +08:00
|
|
|
expect(DiscourseUpdates.has_unseen_features?(admin2.id)).to eq(true)
|
2021-01-22 23:09:02 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "correctly sees newly added features as unseen" do
|
|
|
|
DiscourseUpdates.mark_new_features_as_seen(admin.id)
|
2021-02-11 02:12:04 +08:00
|
|
|
expect(DiscourseUpdates.has_unseen_features?(admin.id)).to eq(false)
|
2023-12-07 06:25:00 +08:00
|
|
|
expect(DiscourseUpdates.new_features_last_seen(admin.id)).to be_within(1.second).of(
|
|
|
|
last_item_date,
|
|
|
|
)
|
2021-01-22 23:09:02 +08:00
|
|
|
|
|
|
|
updated_features = [
|
|
|
|
{ "emoji" => "🤾", "title" => "Brand New Item", "created_at" => 2.minutes.ago },
|
|
|
|
]
|
|
|
|
updated_features += sample_features
|
|
|
|
|
|
|
|
Discourse.redis.set("new_features", MultiJson.dump(updated_features))
|
2021-02-11 02:12:04 +08:00
|
|
|
expect(DiscourseUpdates.has_unseen_features?(admin.id)).to eq(true)
|
2021-01-22 23:09:02 +08:00
|
|
|
end
|
2021-01-26 05:30:43 +08:00
|
|
|
|
|
|
|
it "correctly shows features by Discourse version" do
|
|
|
|
features_with_versions = [
|
2021-02-20 00:03:36 +08:00
|
|
|
{ "emoji" => "🤾", "title" => "Bells", "created_at" => 2.days.ago },
|
|
|
|
{
|
|
|
|
"emoji" => "🙈",
|
|
|
|
"title" => "Whistles",
|
|
|
|
"created_at" => 120.minutes.ago,
|
|
|
|
:discourse_version => "2.6.0.beta1",
|
|
|
|
},
|
2021-01-26 05:30:43 +08:00
|
|
|
{
|
|
|
|
"emoji" => "🙈",
|
|
|
|
"title" => "Confetti",
|
|
|
|
"created_at" => 15.minutes.ago,
|
|
|
|
:discourse_version => "2.7.0.beta2",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"emoji" => "🤾",
|
|
|
|
"title" => "Not shown yet",
|
|
|
|
"created_at" => 10.minutes.ago,
|
|
|
|
:discourse_version => "2.7.0.beta5",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"emoji" => "🤾",
|
|
|
|
"title" => "Not shown yet (beta < stable)",
|
|
|
|
"created_at" => 10.minutes.ago,
|
|
|
|
:discourse_version => "2.7.0",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"emoji" => "🤾",
|
|
|
|
"title" => "Ignore invalid version",
|
|
|
|
"created_at" => 10.minutes.ago,
|
|
|
|
:discourse_version => "invalid-version",
|
2023-01-09 19:18:21 +08:00
|
|
|
},
|
2021-01-26 05:30:43 +08:00
|
|
|
]
|
|
|
|
|
|
|
|
Discourse.redis.set("new_features", MultiJson.dump(features_with_versions))
|
2021-11-09 14:20:09 +08:00
|
|
|
DiscourseUpdates.last_installed_version = "2.7.0.beta2"
|
2021-02-11 02:12:04 +08:00
|
|
|
result = DiscourseUpdates.new_features
|
2021-01-26 05:30:43 +08:00
|
|
|
|
|
|
|
expect(result.length).to eq(3)
|
|
|
|
expect(result[0]["title"]).to eq("Confetti")
|
|
|
|
expect(result[1]["title"]).to eq("Whistles")
|
|
|
|
expect(result[2]["title"]).to eq("Bells")
|
|
|
|
end
|
2024-10-22 07:56:58 +08:00
|
|
|
|
|
|
|
it "correctly shows features with correct boolean experimental site settings" do
|
|
|
|
features_with_versions = [
|
|
|
|
{
|
|
|
|
"emoji" => "🤾",
|
|
|
|
"title" => "Bells",
|
|
|
|
"created_at" => 2.days.ago,
|
|
|
|
"experiment_setting" => "enable_mobile_theme",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"emoji" => "🙈",
|
|
|
|
"title" => "Whistles",
|
|
|
|
"created_at" => 3.days.ago,
|
|
|
|
"experiment_setting" => "default_theme_id",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"emoji" => "🙈",
|
|
|
|
"title" => "Confetti",
|
|
|
|
"created_at" => 4.days.ago,
|
|
|
|
"experiment_setting" => "wrong value",
|
|
|
|
},
|
|
|
|
]
|
|
|
|
|
|
|
|
Discourse.redis.set("new_features", MultiJson.dump(features_with_versions))
|
|
|
|
DiscourseUpdates.last_installed_version = "2.7.0.beta2"
|
|
|
|
result = DiscourseUpdates.new_features
|
|
|
|
|
|
|
|
expect(result.length).to eq(3)
|
|
|
|
expect(result[0]["experiment_setting"]).to eq("enable_mobile_theme")
|
|
|
|
expect(result[1]["experiment_setting"]).to be_nil
|
|
|
|
expect(result[2]["experiment_setting"]).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it "correctly shows features when related plugins are installed" do
|
|
|
|
Discourse.stubs(:plugins_by_name).returns({ "discourse-ai" => true })
|
|
|
|
|
|
|
|
features_with_versions = [
|
|
|
|
{
|
|
|
|
"emoji" => "🤾",
|
|
|
|
"title" => "Bells",
|
|
|
|
"created_at" => 2.days.ago,
|
|
|
|
"plugin_name" => "discourse-ai",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"emoji" => "🙈",
|
|
|
|
"title" => "Confetti",
|
|
|
|
"created_at" => 4.days.ago,
|
|
|
|
"plugin_name" => "uninstalled-plugin",
|
|
|
|
},
|
|
|
|
]
|
|
|
|
|
|
|
|
Discourse.redis.set("new_features", MultiJson.dump(features_with_versions))
|
|
|
|
DiscourseUpdates.last_installed_version = "2.7.0.beta2"
|
|
|
|
result = DiscourseUpdates.new_features
|
|
|
|
|
|
|
|
expect(result.length).to eq(1)
|
|
|
|
expect(result[0]["title"]).to eq("Bells")
|
|
|
|
end
|
2021-01-22 23:09:02 +08:00
|
|
|
end
|
2022-12-16 01:12:53 +08:00
|
|
|
|
|
|
|
describe "#get_last_viewed_feature_date" do
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:user)
|
2022-12-16 01:12:53 +08:00
|
|
|
|
|
|
|
it "returns an ActiveSupport::TimeWithZone object" do
|
|
|
|
time = Time.zone.parse("2022-12-13T21:33:59Z")
|
|
|
|
DiscourseUpdates.bump_last_viewed_feature_date(user.id, time)
|
|
|
|
expect(DiscourseUpdates.get_last_viewed_feature_date(user.id)).to eq(time)
|
|
|
|
end
|
|
|
|
end
|
2013-07-03 23:06:07 +08:00
|
|
|
end
|