Skip to content

Commit

Permalink
Add missing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aidewoode committed Dec 20, 2023
1 parent a3a132b commit b09bd97
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/daemons/media_listener_service
Expand Up @@ -24,7 +24,8 @@ end.parse!
daemon_options = {
dir_mode: :normal,
dir: options[:dir],
multiple: false
multiple: false,
shush: ENV["RAILS_ENV"] == "test"
}

Daemons.run_proc(options[:name], daemon_options) do
Expand Down
34 changes: 34 additions & 0 deletions test/controllers/media_syncing_controller_test.rb
@@ -0,0 +1,34 @@
# frozen_string_literal: true

require "test_helper"

class MediaSyncingControllerTest < ActionDispatch::IntegrationTest
include ActiveJob::TestHelper

test "should sync media" do
login users(:admin)

Media.stub(:syncing?, false) do
assert_enqueued_with(job: MediaSyncJob) do
post media_syncing_url
end
end
end

test "should only admin can sync media" do
login

post media_syncing_url
assert_response :forbidden
end

test "should not sync media when the media is syncing" do
login users(:admin)

Media.stub(:syncing?, true) do
assert_no_enqueued_jobs do
post media_syncing_url
end
end
end
end
44 changes: 43 additions & 1 deletion test/jobs/media_sync_job_test.rb
Expand Up @@ -3,7 +3,7 @@
require "test_helper"

class MediaSyncJobTest < ActiveJob::TestCase
test "sync media" do
test "sync all media" do
mock = Minitest::Mock.new
mock.expect(:call, true, [])

Expand All @@ -14,4 +14,46 @@ class MediaSyncJobTest < ActiveJob::TestCase
mock.verify
end
end

test "sync added media" do
file_paths = [fixtures_file_path("artist1_album1.flac")]
mock = Minitest::Mock.new

mock.expect(:call, true, [:added, file_paths])

MediaSyncJob.perform_later(:added, file_paths)

Media.stub(:sync, mock) do
perform_enqueued_jobs
mock.verify
end
end

test "sync removed media" do
file_paths = [fixtures_file_path("artist1_album1.flac")]
mock = Minitest::Mock.new

mock.expect(:call, true, [:removed, file_paths])

MediaSyncJob.perform_later(:removed, file_paths)

Media.stub(:sync, mock) do
perform_enqueued_jobs
mock.verify
end
end

test "sync modified media" do
file_paths = [fixtures_file_path("artist1_album1.flac")]
mock = Minitest::Mock.new

mock.expect(:call, true, [:modified, file_paths])

MediaSyncJob.perform_later(:modified, file_paths)

Media.stub(:sync, mock) do
perform_enqueued_jobs
mock.verify
end
end
end
22 changes: 22 additions & 0 deletions test/models/media_listener_test.rb
@@ -0,0 +1,22 @@
# frozen_string_literal: true

require "test_helper"

class MediaListenerTest < ActiveSupport::TestCase
teardown do
MediaListener.stop
end

test "start listening" do
MediaListener.start

assert MediaListener.running?
end

test "stop listening" do
MediaListener.start
MediaListener.stop

assert_not MediaListener.running?
end
end
16 changes: 16 additions & 0 deletions test/models/setting_test.rb
Expand Up @@ -3,6 +3,8 @@
require "test_helper"

class SettingTest < ActiveSupport::TestCase
include ActiveJob::TestHelper

test "should have AVAILABLE_SETTINGS constant" do
assert_equal [:media_path, :discogs_token, :transcode_bitrate, :allow_transcode_lossless, :enable_media_listener], Setting::AVAILABLE_SETTINGS
end
Expand Down Expand Up @@ -68,4 +70,18 @@ class SettingTest < ActiveSupport::TestCase

assert_not setting.valid?
end

test "should sync media when media_path changed" do
assert_enqueued_with(job: MediaSyncJob) do
Setting.update(media_path: Rails.root.join("test/fixtures"))
end
end

test "should toggle media listener when enable_media_listener changed" do
Setting.update(enable_media_listener: true)
assert MediaListener.running?

Setting.update(enable_media_listener: false)
assert_not MediaListener.running?
end
end

0 comments on commit b09bd97

Please sign in to comment.