[Housekeeping] Pass the current action when calling the yt-dlp runner (#514)

* Updated yt-dlp runner to take an action type

* Added actions to all callers of the yt-dlp runner

* [SQUASH] updated test files to use new mocking strategy

* Removed unneeded alias
This commit is contained in:
Kieran
2024-12-13 12:29:05 -08:00
committed by GitHub
parent e9d365ee9e
commit 023f449dbe
19 changed files with 276 additions and 250 deletions
+4 -1
View File
@@ -3,6 +3,8 @@ defmodule Pinchflat.YtDlp.CommandRunner do
Runs yt-dlp commands using the `System.cmd/3` function Runs yt-dlp commands using the `System.cmd/3` function
""" """
require Logger
alias Pinchflat.Utils.CliUtils alias Pinchflat.Utils.CliUtils
alias Pinchflat.YtDlp.YtDlpCommandRunner alias Pinchflat.YtDlp.YtDlpCommandRunner
alias Pinchflat.Utils.FilesystemUtils, as: FSUtils alias Pinchflat.Utils.FilesystemUtils, as: FSUtils
@@ -24,7 +26,8 @@ defmodule Pinchflat.YtDlp.CommandRunner do
Returns {:ok, binary()} | {:error, output, status}. Returns {:ok, binary()} | {:error, output, status}.
""" """
@impl YtDlpCommandRunner @impl YtDlpCommandRunner
def run(url, command_opts, output_template, addl_opts \\ []) do def run(url, action_name, command_opts, output_template, addl_opts \\ []) do
Logger.debug("Running yt-dlp command for action: #{action_name}")
# This approach lets us mock the command for testing # This approach lets us mock the command for testing
command = backend_executable() command = backend_executable()
+4 -4
View File
@@ -41,7 +41,7 @@ defmodule Pinchflat.YtDlp.Media do
def download(url, command_opts \\ [], addl_opts \\ []) do def download(url, command_opts \\ [], addl_opts \\ []) do
all_command_opts = [:no_simulate] ++ command_opts all_command_opts = [:no_simulate] ++ command_opts
with {:ok, output} <- backend_runner().run(url, all_command_opts, "after_move:%()j", addl_opts), with {:ok, output} <- backend_runner().run(url, :download, all_command_opts, "after_move:%()j", addl_opts),
{:ok, parsed_json} <- Phoenix.json_library().decode(output) do {:ok, parsed_json} <- Phoenix.json_library().decode(output) do
{:ok, parsed_json} {:ok, parsed_json}
else else
@@ -56,7 +56,7 @@ defmodule Pinchflat.YtDlp.Media do
Returns {:ok, :downloadable | :ignorable} | {:error, any} Returns {:ok, :downloadable | :ignorable} | {:error, any}
""" """
def get_downloadable_status(url) do def get_downloadable_status(url) do
case backend_runner().run(url, [:simulate, :skip_download], "%(.{live_status})j") do case backend_runner().run(url, :get_downloadable_status, [:simulate, :skip_download], "%(.{live_status})j") do
{:ok, output} -> {:ok, output} ->
output output
|> Phoenix.json_library().decode!() |> Phoenix.json_library().decode!()
@@ -78,7 +78,7 @@ defmodule Pinchflat.YtDlp.Media do
# NOTE: it doesn't seem like this command actually returns anything in `after_move` since # NOTE: it doesn't seem like this command actually returns anything in `after_move` since
# we aren't downloading the main media file # we aren't downloading the main media file
backend_runner().run(url, all_command_opts, "after_move:%()j", addl_opts) backend_runner().run(url, :download_thumbnail, all_command_opts, "after_move:%()j", addl_opts)
end end
@doc """ @doc """
@@ -92,7 +92,7 @@ defmodule Pinchflat.YtDlp.Media do
all_command_opts = [:simulate, :skip_download] ++ command_opts all_command_opts = [:simulate, :skip_download] ++ command_opts
output_template = indexing_output_template() output_template = indexing_output_template()
case backend_runner().run(url, all_command_opts, output_template, addl_opts) do case backend_runner().run(url, :get_media_attributes, all_command_opts, output_template, addl_opts) do
{:ok, output} -> {:ok, output} ->
output output
|> Phoenix.json_library().decode!() |> Phoenix.json_library().decode!()
+6 -3
View File
@@ -33,12 +33,13 @@ defmodule Pinchflat.YtDlp.MediaCollection do
output_filepath = FilesystemUtils.generate_metadata_tmpfile(:json) output_filepath = FilesystemUtils.generate_metadata_tmpfile(:json)
file_listener_handler = Keyword.get(addl_opts, :file_listener_handler, false) file_listener_handler = Keyword.get(addl_opts, :file_listener_handler, false)
runner_opts = [output_filepath: output_filepath, use_cookies: use_cookies] runner_opts = [output_filepath: output_filepath, use_cookies: use_cookies]
action = :get_media_attributes_for_collection
if file_listener_handler do if file_listener_handler do
file_listener_handler.(output_filepath) file_listener_handler.(output_filepath)
end end
case runner.run(url, all_command_opts, output_template, runner_opts) do case runner.run(url, action, all_command_opts, output_template, runner_opts) do
{:ok, output} -> {:ok, output} ->
parsed_lines = parsed_lines =
output output
@@ -82,8 +83,9 @@ defmodule Pinchflat.YtDlp.MediaCollection do
all_command_opts = default_opts ++ command_opts all_command_opts = default_opts ++ command_opts
output_template = "%(.{channel,channel_id,playlist_id,playlist_title,filename})j" output_template = "%(.{channel,channel_id,playlist_id,playlist_title,filename})j"
action = :get_source_details
with {:ok, output} <- backend_runner().run(source_url, all_command_opts, output_template, addl_opts), with {:ok, output} <- backend_runner().run(source_url, action, all_command_opts, output_template, addl_opts),
{:ok, parsed_json} <- Phoenix.json_library().decode(output) do {:ok, parsed_json} <- Phoenix.json_library().decode(output) do
{:ok, format_source_details(parsed_json)} {:ok, format_source_details(parsed_json)}
else else
@@ -121,8 +123,9 @@ defmodule Pinchflat.YtDlp.MediaCollection do
all_command_opts = [:skip_download] ++ command_opts all_command_opts = [:skip_download] ++ command_opts
output_template = "playlist:%()j" output_template = "playlist:%()j"
action = :get_source_metadata
with {:ok, output} <- backend_runner().run(source_url, all_command_opts, output_template, addl_opts), with {:ok, output} <- backend_runner().run(source_url, action, all_command_opts, output_template, addl_opts),
{:ok, parsed_json} <- Phoenix.json_library().decode(output) do {:ok, parsed_json} <- Phoenix.json_library().decode(output) do
{:ok, parsed_json} {:ok, parsed_json}
else else
@@ -6,7 +6,7 @@ defmodule Pinchflat.YtDlp.YtDlpCommandRunner do
yt-dlp command. yt-dlp command.
""" """
@callback run(binary(), keyword(), binary()) :: {:ok, binary()} | {:error, binary(), integer()} @callback run(binary(), atom(), keyword(), binary()) :: {:ok, binary()} | {:error, binary(), integer()}
@callback run(binary(), keyword(), binary(), keyword()) :: {:ok, binary()} | {:error, binary(), integer()} @callback run(binary(), atom(), keyword(), binary(), keyword()) :: {:ok, binary()} | {:error, binary(), integer()}
@callback version() :: {:ok, binary()} | {:error, binary()} @callback version() :: {:ok, binary()} | {:error, binary()}
end end
@@ -9,8 +9,12 @@ defmodule Pinchflat.Downloading.MediaDownloadWorkerTest do
alias Pinchflat.Downloading.MediaDownloadWorker alias Pinchflat.Downloading.MediaDownloadWorker
setup do setup do
stub(YtDlpRunnerMock, :run, fn _url, _opts, _ot -> {:ok, "{}"} end) stub(YtDlpRunnerMock, :run, fn
stub(YtDlpRunnerMock, :run, fn _url, _opts, _ot, _addl -> {:ok, ""} end) _url, :download_thumbnail, _opts, _ot, _addl -> {:ok, ""}
_url, :download, _opts, _ot, _addl -> {:ok, ""}
end)
stub(YtDlpRunnerMock, :run, fn _url, :get_downloadable_status, _opts, _ot -> {:ok, "{}"} end)
stub(UserScriptRunnerMock, :run, fn _event_type, _data -> {:ok, "", 0} end) stub(UserScriptRunnerMock, :run, fn _event_type, _data -> {:ok, "", 0} end)
stub(HTTPClientMock, :get, fn _url, _headers, _opts -> {:ok, ""} end) stub(HTTPClientMock, :get, fn _url, _headers, _opts -> {:ok, ""} end)
@@ -54,11 +58,11 @@ defmodule Pinchflat.Downloading.MediaDownloadWorkerTest do
describe "perform/1" do describe "perform/1" do
test "it saves attributes to the media_item", %{media_item: media_item} do test "it saves attributes to the media_item", %{media_item: media_item} do
expect(YtDlpRunnerMock, :run, 1, fn _url, _opts, _ot, _addl -> expect(YtDlpRunnerMock, :run, 1, fn _url, :download, _opts, _ot, _addl ->
{:ok, render_metadata(:media_metadata)} {:ok, render_metadata(:media_metadata)}
end) end)
expect(YtDlpRunnerMock, :run, 1, fn _url, _opts, _ot, _addl -> {:ok, ""} end) expect(YtDlpRunnerMock, :run, 1, fn _url, :download_thumbnail, _opts, _ot, _addl -> {:ok, ""} end)
assert media_item.media_filepath == nil assert media_item.media_filepath == nil
perform_job(MediaDownloadWorker, %{id: media_item.id}) perform_job(MediaDownloadWorker, %{id: media_item.id})
@@ -68,11 +72,11 @@ defmodule Pinchflat.Downloading.MediaDownloadWorkerTest do
end end
test "it saves the metadata to the media_item", %{media_item: media_item} do test "it saves the metadata to the media_item", %{media_item: media_item} do
expect(YtDlpRunnerMock, :run, 1, fn _url, _opts, _ot, _addl -> expect(YtDlpRunnerMock, :run, 1, fn _url, :download, _opts, _ot, _addl ->
{:ok, render_metadata(:media_metadata)} {:ok, render_metadata(:media_metadata)}
end) end)
expect(YtDlpRunnerMock, :run, 1, fn _url, _opts, _ot, _addl -> {:ok, ""} end) expect(YtDlpRunnerMock, :run, 1, fn _url, :download_thumbnail, _opts, _ot, _addl -> {:ok, ""} end)
assert media_item.metadata == nil assert media_item.metadata == nil
perform_job(MediaDownloadWorker, %{id: media_item.id}) perform_job(MediaDownloadWorker, %{id: media_item.id})
@@ -87,7 +91,7 @@ defmodule Pinchflat.Downloading.MediaDownloadWorkerTest do
end end
test "it sets the job to retryable if the download fails", %{media_item: media_item} do test "it sets the job to retryable if the download fails", %{media_item: media_item} do
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot, _addl -> {:error, "error"} end) expect(YtDlpRunnerMock, :run, fn _url, :download, _opts, _ot, _addl -> {:error, "error"} end)
Oban.Testing.with_testing_mode(:inline, fn -> Oban.Testing.with_testing_mode(:inline, fn ->
{:ok, job} = Oban.insert(MediaDownloadWorker.new(%{id: media_item.id})) {:ok, job} = Oban.insert(MediaDownloadWorker.new(%{id: media_item.id}))
@@ -97,7 +101,7 @@ defmodule Pinchflat.Downloading.MediaDownloadWorkerTest do
end end
test "sets the job to retryable if the download failed and was retried", %{media_item: media_item} do test "sets the job to retryable if the download failed and was retried", %{media_item: media_item} do
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot, _addl -> expect(YtDlpRunnerMock, :run, fn _url, :download, _opts, _ot, _addl ->
{:error, "Unable to communicate with SponsorBlock", 1} {:error, "Unable to communicate with SponsorBlock", 1}
end) end)
@@ -109,7 +113,7 @@ defmodule Pinchflat.Downloading.MediaDownloadWorkerTest do
end end
test "does not set the job to retryable if retrying wouldn't fix the issue", %{media_item: media_item} do test "does not set the job to retryable if retrying wouldn't fix the issue", %{media_item: media_item} do
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot, _addl -> expect(YtDlpRunnerMock, :run, fn _url, :download, _opts, _ot, _addl ->
{:error, "Something something Video unavailable something something", 1} {:error, "Something something Video unavailable something something", 1}
end) end)
@@ -121,13 +125,13 @@ defmodule Pinchflat.Downloading.MediaDownloadWorkerTest do
end end
test "it ensures error are returned in a 2-item tuple", %{media_item: media_item} do test "it ensures error are returned in a 2-item tuple", %{media_item: media_item} do
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot, _addl -> {:error, "error", 1} end) expect(YtDlpRunnerMock, :run, fn _url, :download, _opts, _ot, _addl -> {:error, "error", 1} end)
assert {:error, :download_failed} = perform_job(MediaDownloadWorker, %{id: media_item.id}) assert {:error, :download_failed} = perform_job(MediaDownloadWorker, %{id: media_item.id})
end end
test "it does not download if the source is set to not download", %{media_item: media_item} do test "it does not download if the source is set to not download", %{media_item: media_item} do
expect(YtDlpRunnerMock, :run, 0, fn _url, _opts, _ot, _addl -> :ok end) expect(YtDlpRunnerMock, :run, 0, fn _url, :download, _opts, _ot, _addl -> :ok end)
Sources.update_source(media_item.source, %{download_media: false}) Sources.update_source(media_item.source, %{download_media: false})
@@ -135,7 +139,7 @@ defmodule Pinchflat.Downloading.MediaDownloadWorkerTest do
end end
test "does not download if the media item is set to not download", %{media_item: media_item} do test "does not download if the media item is set to not download", %{media_item: media_item} do
expect(YtDlpRunnerMock, :run, 0, fn _url, _opts, _ot, _addl -> :ok end) expect(YtDlpRunnerMock, :run, 0, fn _url, :download, _opts, _ot, _addl -> :ok end)
Media.update_media_item(media_item, %{prevent_download: true}) Media.update_media_item(media_item, %{prevent_download: true})
@@ -143,14 +147,14 @@ defmodule Pinchflat.Downloading.MediaDownloadWorkerTest do
end end
test "it saves the file's size to the database", %{media_item: media_item} do test "it saves the file's size to the database", %{media_item: media_item} do
expect(YtDlpRunnerMock, :run, 1, fn _url, _opts, _ot, _addl -> expect(YtDlpRunnerMock, :run, 1, fn _url, :download, _opts, _ot, _addl ->
metadata = render_parsed_metadata(:media_metadata) metadata = render_parsed_metadata(:media_metadata)
FilesystemUtils.write_p!(metadata["filepath"], "test") FilesystemUtils.write_p!(metadata["filepath"], "test")
{:ok, Phoenix.json_library().encode!(metadata)} {:ok, Phoenix.json_library().encode!(metadata)}
end) end)
expect(YtDlpRunnerMock, :run, 1, fn _url, _opts, _ot, _addl -> {:ok, ""} end) expect(YtDlpRunnerMock, :run, 1, fn _url, :download_thumbnail, _opts, _ot, _addl -> {:ok, ""} end)
perform_job(MediaDownloadWorker, %{id: media_item.id}) perform_job(MediaDownloadWorker, %{id: media_item.id})
media_item = Repo.reload(media_item) media_item = Repo.reload(media_item)
@@ -159,10 +163,12 @@ defmodule Pinchflat.Downloading.MediaDownloadWorkerTest do
end end
test "does not set redownloaded_at by default", %{media_item: media_item} do test "does not set redownloaded_at by default", %{media_item: media_item} do
expect(YtDlpRunnerMock, :run, 2, fn _url, _opts, _ot, _addl -> expect(YtDlpRunnerMock, :run, 1, fn _url, :download, _opts, _ot, _addl ->
{:ok, render_metadata(:media_metadata)} {:ok, render_metadata(:media_metadata)}
end) end)
expect(YtDlpRunnerMock, :run, 1, fn _url, :download_thumbnail, _opts, _ot, _addl -> {:ok, ""} end)
perform_job(MediaDownloadWorker, %{id: media_item.id}) perform_job(MediaDownloadWorker, %{id: media_item.id})
media_item = Repo.reload(media_item) media_item = Repo.reload(media_item)
@@ -174,14 +180,14 @@ defmodule Pinchflat.Downloading.MediaDownloadWorkerTest do
end end
test "sets the no_force_overwrites runner option", %{media_item: media_item} do test "sets the no_force_overwrites runner option", %{media_item: media_item} do
expect(YtDlpRunnerMock, :run, 1, fn _url, opts, _ot, _addl -> expect(YtDlpRunnerMock, :run, 1, fn _url, :download, opts, _ot, _addl ->
assert :no_force_overwrites in opts assert :no_force_overwrites in opts
refute :force_overwrites in opts refute :force_overwrites in opts
{:ok, render_metadata(:media_metadata)} {:ok, render_metadata(:media_metadata)}
end) end)
expect(YtDlpRunnerMock, :run, 1, fn _url, _opts, _ot, _addl -> {:ok, ""} end) expect(YtDlpRunnerMock, :run, 1, fn _url, :download_thumbnail, _opts, _ot, _addl -> {:ok, ""} end)
perform_job(MediaDownloadWorker, %{id: media_item.id}) perform_job(MediaDownloadWorker, %{id: media_item.id})
end end
@@ -189,7 +195,7 @@ defmodule Pinchflat.Downloading.MediaDownloadWorkerTest do
describe "perform/1 when testing non-downloadable media" do describe "perform/1 when testing non-downloadable media" do
test "does not retry the job if the media is currently not downloadable", %{media_item: media_item} do test "does not retry the job if the media is currently not downloadable", %{media_item: media_item} do
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot -> expect(YtDlpRunnerMock, :run, fn _url, :get_downloadable_status, _opts, _ot ->
{:ok, Phoenix.json_library().encode!(%{"live_status" => "is_live"})} {:ok, Phoenix.json_library().encode!(%{"live_status" => "is_live"})}
end) end)
@@ -203,7 +209,7 @@ defmodule Pinchflat.Downloading.MediaDownloadWorkerTest do
describe "perform/1 when testing forced downloads" do describe "perform/1 when testing forced downloads" do
test "ignores 'prevent_download' if forced", %{media_item: media_item} do test "ignores 'prevent_download' if forced", %{media_item: media_item} do
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot, _addl -> :ok end) expect(YtDlpRunnerMock, :run, fn _url, :download, _opts, _ot, _addl -> :ok end)
Sources.update_source(media_item.source, %{download_media: false}) Sources.update_source(media_item.source, %{download_media: false})
Media.update_media_item(media_item, %{prevent_download: true}) Media.update_media_item(media_item, %{prevent_download: true})
@@ -212,14 +218,14 @@ defmodule Pinchflat.Downloading.MediaDownloadWorkerTest do
end end
test "sets force_overwrites runner option", %{media_item: media_item} do test "sets force_overwrites runner option", %{media_item: media_item} do
expect(YtDlpRunnerMock, :run, 1, fn _url, opts, _ot, _addl -> expect(YtDlpRunnerMock, :run, 1, fn _url, :download, opts, _ot, _addl ->
assert :force_overwrites in opts assert :force_overwrites in opts
refute :no_force_overwrites in opts refute :no_force_overwrites in opts
{:ok, render_metadata(:media_metadata)} {:ok, render_metadata(:media_metadata)}
end) end)
expect(YtDlpRunnerMock, :run, 1, fn _url, _opts, _ot, _addl -> {:ok, ""} end) expect(YtDlpRunnerMock, :run, 1, fn _url, :download_thumbnail, _opts, _ot, _addl -> {:ok, ""} end)
perform_job(MediaDownloadWorker, %{id: media_item.id, force: true}) perform_job(MediaDownloadWorker, %{id: media_item.id, force: true})
end end
@@ -227,11 +233,11 @@ defmodule Pinchflat.Downloading.MediaDownloadWorkerTest do
describe "perform/1 when testing re-downloads" do describe "perform/1 when testing re-downloads" do
test "sets redownloaded_at on the media_item", %{media_item: media_item} do test "sets redownloaded_at on the media_item", %{media_item: media_item} do
expect(YtDlpRunnerMock, :run, 1, fn _url, _opts, _ot, _addl -> expect(YtDlpRunnerMock, :run, 1, fn _url, :download, _opts, _ot, _addl ->
{:ok, render_metadata(:media_metadata)} {:ok, render_metadata(:media_metadata)}
end) end)
expect(YtDlpRunnerMock, :run, 1, fn _url, _opts, _ot, _addl -> {:ok, ""} end) expect(YtDlpRunnerMock, :run, 1, fn _url, :download_thumbnail, _opts, _ot, _addl -> {:ok, ""} end)
perform_job(MediaDownloadWorker, %{id: media_item.id, quality_upgrade?: true}) perform_job(MediaDownloadWorker, %{id: media_item.id, quality_upgrade?: true})
media_item = Repo.reload(media_item) media_item = Repo.reload(media_item)
@@ -240,20 +246,20 @@ defmodule Pinchflat.Downloading.MediaDownloadWorkerTest do
end end
test "sets force_overwrites runner option", %{media_item: media_item} do test "sets force_overwrites runner option", %{media_item: media_item} do
expect(YtDlpRunnerMock, :run, 1, fn _url, opts, _ot, _addl -> expect(YtDlpRunnerMock, :run, 1, fn _url, :download, opts, _ot, _addl ->
assert :force_overwrites in opts assert :force_overwrites in opts
refute :no_force_overwrites in opts refute :no_force_overwrites in opts
{:ok, render_metadata(:media_metadata)} {:ok, render_metadata(:media_metadata)}
end) end)
expect(YtDlpRunnerMock, :run, 1, fn _url, _opts, _ot, _addl -> {:ok, ""} end) expect(YtDlpRunnerMock, :run, 1, fn _url, :download_thumbnail, _opts, _ot, _addl -> {:ok, ""} end)
perform_job(MediaDownloadWorker, %{id: media_item.id, force: true}) perform_job(MediaDownloadWorker, %{id: media_item.id, force: true})
end end
test "deletes old files if the media item has been updated" do test "deletes old files if the media item has been updated" do
expect(YtDlpRunnerMock, :run, 1, fn _url, _opts, _ot, _addl -> expect(YtDlpRunnerMock, :run, 1, fn _url, :download, _opts, _ot, _addl ->
tmp_media_item = media_item_with_attachments() tmp_media_item = media_item_with_attachments()
metadata = render_parsed_metadata(:media_metadata) metadata = render_parsed_metadata(:media_metadata)
metadata = Map.put(metadata, "filepath", tmp_media_item.media_filepath) metadata = Map.put(metadata, "filepath", tmp_media_item.media_filepath)
@@ -261,7 +267,7 @@ defmodule Pinchflat.Downloading.MediaDownloadWorkerTest do
{:ok, Phoenix.json_library().encode!(metadata)} {:ok, Phoenix.json_library().encode!(metadata)}
end) end)
expect(YtDlpRunnerMock, :run, 1, fn _url, _opts, _ot, _addl -> {:ok, ""} end) expect(YtDlpRunnerMock, :run, 1, fn _url, :download_thumbnail, _opts, _ot, _addl -> {:ok, ""} end)
old_media_item = media_item_with_attachments() old_media_item = media_item_with_attachments()
perform_job(MediaDownloadWorker, %{id: old_media_item.id, force: true}) perform_job(MediaDownloadWorker, %{id: old_media_item.id, force: true})
@@ -275,8 +281,9 @@ defmodule Pinchflat.Downloading.MediaDownloadWorkerTest do
describe "perform/1 when testing user script callbacks" do describe "perform/1 when testing user script callbacks" do
setup do setup do
stub(YtDlpRunnerMock, :run, fn _url, _opts, _ot, _addl -> stub(YtDlpRunnerMock, :run, fn
{:ok, render_metadata(:media_metadata)} _url, :download, _opts, _ot, _addl -> {:ok, render_metadata(:media_metadata)}
_url, :download_thumbnail, _opts, _ot, _addl -> {:ok, ""}
end) end)
:ok :ok
@@ -16,15 +16,14 @@ defmodule Pinchflat.Downloading.MediaDownloaderTest do
) )
stub(HTTPClientMock, :get, fn _url, _headers, _opts -> {:ok, ""} end) stub(HTTPClientMock, :get, fn _url, _headers, _opts -> {:ok, ""} end)
stub(YtDlpRunnerMock, :run, fn _url, _opts, _ot -> {:ok, "{}"} end) stub(YtDlpRunnerMock, :run, fn _url, :get_downloadable_status, _opts, _ot -> {:ok, "{}"} end)
stub(YtDlpRunnerMock, :run, fn _url, _opts, _ot, _addl_args -> {:ok, ""} end)
{:ok, %{media_item: media_item}} {:ok, %{media_item: media_item}}
end end
describe "download_for_media_item/3" do describe "download_for_media_item/3" do
test "calls the backend runner", %{media_item: media_item} do test "calls the backend runner", %{media_item: media_item} do
expect(YtDlpRunnerMock, :run, 1, fn url, _opts, ot, addl -> expect(YtDlpRunnerMock, :run, 1, fn url, :download, _opts, ot, addl ->
assert url == media_item.original_url assert url == media_item.original_url
assert ot == "after_move:%()j" assert ot == "after_move:%()j"
assert [{:output_filepath, filepath} | _] = addl assert [{:output_filepath, filepath} | _] = addl
@@ -33,16 +32,18 @@ defmodule Pinchflat.Downloading.MediaDownloaderTest do
{:ok, render_metadata(:media_metadata)} {:ok, render_metadata(:media_metadata)}
end) end)
expect(YtDlpRunnerMock, :run, 1, fn _url, _opts, _ot, _addl_args -> {:ok, ""} end) expect(YtDlpRunnerMock, :run, 1, fn _url, :download_thumbnail, _opts, _ot, _addl_args -> {:ok, ""} end)
assert {:ok, _} = MediaDownloader.download_for_media_item(media_item) assert {:ok, _} = MediaDownloader.download_for_media_item(media_item)
end end
test "saves the metadata filepath to the database", %{media_item: media_item} do test "saves the metadata filepath to the database", %{media_item: media_item} do
expect(YtDlpRunnerMock, :run, 2, fn _url, _opts, _ot, _addl -> expect(YtDlpRunnerMock, :run, 1, fn _url, :download, _opts, _ot, _addl ->
{:ok, render_metadata(:media_metadata)} {:ok, render_metadata(:media_metadata)}
end) end)
expect(YtDlpRunnerMock, :run, 1, fn _url, :download_thumbnail, _opts, _ot, _addl_args -> {:ok, ""} end)
assert is_nil(media_item.metadata) assert is_nil(media_item.metadata)
assert {:ok, updated_media_item} = MediaDownloader.download_for_media_item(media_item) assert {:ok, updated_media_item} = MediaDownloader.download_for_media_item(media_item)
@@ -51,7 +52,7 @@ defmodule Pinchflat.Downloading.MediaDownloaderTest do
end end
test "errors for non-downloadable media are passed through", %{media_item: media_item} do test "errors for non-downloadable media are passed through", %{media_item: media_item} do
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot -> expect(YtDlpRunnerMock, :run, fn _url, :get_downloadable_status, _opts, _ot ->
{:ok, Phoenix.json_library().encode!(%{"live_status" => "is_live"})} {:ok, Phoenix.json_library().encode!(%{"live_status" => "is_live"})}
end) end)
@@ -59,7 +60,7 @@ defmodule Pinchflat.Downloading.MediaDownloaderTest do
end end
test "non-recoverable errors are passed through", %{media_item: media_item} do test "non-recoverable errors are passed through", %{media_item: media_item} do
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot, _addl -> expect(YtDlpRunnerMock, :run, fn _url, :download, _opts, _ot, _addl ->
{:error, :some_error, 1} {:error, :some_error, 1}
end) end)
@@ -67,7 +68,7 @@ defmodule Pinchflat.Downloading.MediaDownloaderTest do
end end
test "unknown errors are passed through", %{media_item: media_item} do test "unknown errors are passed through", %{media_item: media_item} do
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot, _addl -> expect(YtDlpRunnerMock, :run, fn _url, :download, _opts, _ot, _addl ->
{:error, :some_error} {:error, :some_error}
end) end)
@@ -78,29 +79,31 @@ defmodule Pinchflat.Downloading.MediaDownloaderTest do
describe "download_for_media_item/3 when testing non-downloadable media" do describe "download_for_media_item/3 when testing non-downloadable media" do
test "calls the download runner if the media is currently downloadable", %{media_item: media_item} do test "calls the download runner if the media is currently downloadable", %{media_item: media_item} do
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot -> expect(YtDlpRunnerMock, :run, fn _url, :get_downloadable_status, _opts, _ot ->
{:ok, Phoenix.json_library().encode!(%{"live_status" => "was_live"})} {:ok, Phoenix.json_library().encode!(%{"live_status" => "was_live"})}
end) end)
expect(YtDlpRunnerMock, :run, 2, fn _url, _opts, _ot, _addl -> expect(YtDlpRunnerMock, :run, 1, fn _url, :download, _opts, _ot, _addl ->
{:ok, render_metadata(:media_metadata)} {:ok, render_metadata(:media_metadata)}
end) end)
expect(YtDlpRunnerMock, :run, 1, fn _url, :download_thumbnail, _opts, _ot, _addl_args -> {:ok, ""} end)
assert {:ok, _} = MediaDownloader.download_for_media_item(media_item) assert {:ok, _} = MediaDownloader.download_for_media_item(media_item)
end end
test "does not call the download runner if the media is not downloadable", %{media_item: media_item} do test "does not call the download runner if the media is not downloadable", %{media_item: media_item} do
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot -> expect(YtDlpRunnerMock, :run, fn _url, :get_downloadable_status, _opts, _ot ->
{:ok, Phoenix.json_library().encode!(%{"live_status" => "is_live"})} {:ok, Phoenix.json_library().encode!(%{"live_status" => "is_live"})}
end) end)
expect(YtDlpRunnerMock, :run, 0, fn _url, _opts, _ot, _addl -> {:ok, ""} end) expect(YtDlpRunnerMock, :run, 0, fn _url, :download, _opts, _ot, _addl -> {:ok, ""} end)
assert {:error, :unsuitable_for_download} = MediaDownloader.download_for_media_item(media_item) assert {:error, :unsuitable_for_download} = MediaDownloader.download_for_media_item(media_item)
end end
test "returns unexpected errors from the download status determination method", %{media_item: media_item} do test "returns unexpected errors from the download status determination method", %{media_item: media_item} do
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot -> {:error, :what_tha} end) expect(YtDlpRunnerMock, :run, fn _url, :get_downloadable_status, _opts, _ot -> {:error, :what_tha} end)
assert {:error, "Unknown error: {:error, :what_tha}"} = MediaDownloader.download_for_media_item(media_item) assert {:error, "Unknown error: {:error, :what_tha}"} = MediaDownloader.download_for_media_item(media_item)
end end
@@ -108,14 +111,14 @@ defmodule Pinchflat.Downloading.MediaDownloaderTest do
describe "download_for_media_item/3 when testing override options" do describe "download_for_media_item/3 when testing override options" do
test "includes override opts if specified", %{media_item: media_item} do test "includes override opts if specified", %{media_item: media_item} do
expect(YtDlpRunnerMock, :run, 1, fn _url, opts, _ot, _addl -> expect(YtDlpRunnerMock, :run, 1, fn _url, :download, opts, _ot, _addl ->
refute :force_overwrites in opts refute :force_overwrites in opts
assert :no_force_overwrites in opts assert :no_force_overwrites in opts
{:ok, render_metadata(:media_metadata)} {:ok, render_metadata(:media_metadata)}
end) end)
expect(YtDlpRunnerMock, :run, 1, fn _url, _opts, _ot, _addl -> {:ok, ""} end) expect(YtDlpRunnerMock, :run, 1, fn _url, :download_thumbnail, _opts, _ot, _addl -> {:ok, ""} end)
override_opts = [overwrite_behaviour: :no_force_overwrites] override_opts = [overwrite_behaviour: :no_force_overwrites]
@@ -125,12 +128,12 @@ defmodule Pinchflat.Downloading.MediaDownloaderTest do
describe "download_for_media_item/3 when testing cookie usage" do describe "download_for_media_item/3 when testing cookie usage" do
test "sets use_cookies if the source uses cookies" do test "sets use_cookies if the source uses cookies" do
expect(YtDlpRunnerMock, :run, 1, fn _url, _opts, _ot, addl -> expect(YtDlpRunnerMock, :run, 1, fn _url, :download, _opts, _ot, addl ->
assert {:use_cookies, true} in addl assert {:use_cookies, true} in addl
{:ok, render_metadata(:media_metadata)} {:ok, render_metadata(:media_metadata)}
end) end)
expect(YtDlpRunnerMock, :run, 1, fn _url, _opts, _ot, _addl_args -> {:ok, ""} end) expect(YtDlpRunnerMock, :run, 1, fn _url, :download_thumbnail, _opts, _ot, _addl_args -> {:ok, ""} end)
source = source_fixture(%{use_cookies: true}) source = source_fixture(%{use_cookies: true})
media_item = media_item_fixture(%{source_id: source.id}) media_item = media_item_fixture(%{source_id: source.id})
@@ -139,12 +142,12 @@ defmodule Pinchflat.Downloading.MediaDownloaderTest do
end end
test "does not set use_cookies if the source does not use cookies" do test "does not set use_cookies if the source does not use cookies" do
expect(YtDlpRunnerMock, :run, 1, fn _url, _opts, _ot, addl -> expect(YtDlpRunnerMock, :run, 1, fn _url, :download, _opts, _ot, addl ->
assert {:use_cookies, false} in addl assert {:use_cookies, false} in addl
{:ok, render_metadata(:media_metadata)} {:ok, render_metadata(:media_metadata)}
end) end)
expect(YtDlpRunnerMock, :run, 1, fn _url, _opts, _ot, _addl_args -> {:ok, ""} end) expect(YtDlpRunnerMock, :run, 1, fn _url, :download_thumbnail, _opts, _ot, _addl_args -> {:ok, ""} end)
source = source_fixture(%{use_cookies: false}) source = source_fixture(%{use_cookies: false})
media_item = media_item_fixture(%{source_id: source.id}) media_item = media_item_fixture(%{source_id: source.id})
@@ -157,7 +160,7 @@ defmodule Pinchflat.Downloading.MediaDownloaderTest do
test "returns a recovered tuple on recoverable errors", %{media_item: media_item} do test "returns a recovered tuple on recoverable errors", %{media_item: media_item} do
message = "Unable to communicate with SponsorBlock" message = "Unable to communicate with SponsorBlock"
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot, _addl -> expect(YtDlpRunnerMock, :run, fn _url, :download, _opts, _ot, _addl ->
{:error, message, 1} {:error, message, 1}
end) end)
@@ -167,14 +170,14 @@ defmodule Pinchflat.Downloading.MediaDownloaderTest do
test "attempts to update the media item on recoverable errors", %{media_item: media_item} do test "attempts to update the media item on recoverable errors", %{media_item: media_item} do
message = "Unable to communicate with SponsorBlock" message = "Unable to communicate with SponsorBlock"
expect(YtDlpRunnerMock, :run, 1, fn _url, _opts, _ot, addl -> expect(YtDlpRunnerMock, :run, 1, fn _url, :download, _opts, _ot, addl ->
[{:output_filepath, filepath} | _] = addl [{:output_filepath, filepath} | _] = addl
File.write(filepath, render_metadata(:media_metadata)) File.write(filepath, render_metadata(:media_metadata))
{:error, message, 1} {:error, message, 1}
end) end)
expect(YtDlpRunnerMock, :run, 1, fn _url, _opts, _ot, _addl_args -> {:ok, ""} end) expect(YtDlpRunnerMock, :run, 1, fn _url, :download_thumbnail, _opts, _ot, _addl_args -> {:ok, ""} end)
assert {:recovered, ^message} = MediaDownloader.download_for_media_item(media_item) assert {:recovered, ^message} = MediaDownloader.download_for_media_item(media_item)
media_item = Repo.reload(media_item) media_item = Repo.reload(media_item)
@@ -186,8 +189,9 @@ defmodule Pinchflat.Downloading.MediaDownloaderTest do
describe "download_for_media_item/3 when testing media_item attributes" do describe "download_for_media_item/3 when testing media_item attributes" do
setup do setup do
stub(YtDlpRunnerMock, :run, fn _url, _opts, _ot, _addl -> stub(YtDlpRunnerMock, :run, fn
{:ok, render_metadata(:media_metadata)} _url, :download, _opts, _ot, _addl -> {:ok, render_metadata(:media_metadata)}
_url, :download_thumbnail, _opts, _ot, _addl -> {:ok, ""}
end) end)
:ok :ok
@@ -234,7 +238,7 @@ defmodule Pinchflat.Downloading.MediaDownloaderTest do
end end
test "extracts the thumbnail_filepath", %{media_item: media_item} do test "extracts the thumbnail_filepath", %{media_item: media_item} do
expect(YtDlpRunnerMock, :run, 2, fn _url, _opts, _ot, _addl -> expect(YtDlpRunnerMock, :run, 1, fn _url, :download, _opts, _ot, _addl ->
metadata = render_parsed_metadata(:media_metadata) metadata = render_parsed_metadata(:media_metadata)
thumbnail_filepath = thumbnail_filepath =
@@ -250,6 +254,8 @@ defmodule Pinchflat.Downloading.MediaDownloaderTest do
{:ok, Phoenix.json_library().encode!(metadata)} {:ok, Phoenix.json_library().encode!(metadata)}
end) end)
expect(YtDlpRunnerMock, :run, 1, fn _url, :download_thumbnail, _opts, _ot, _addl_args -> {:ok, ""} end)
assert media_item.thumbnail_filepath == nil assert media_item.thumbnail_filepath == nil
assert {:ok, updated_media_item} = MediaDownloader.download_for_media_item(media_item) assert {:ok, updated_media_item} = MediaDownloader.download_for_media_item(media_item)
assert String.ends_with?(updated_media_item.thumbnail_filepath, ".webp") assert String.ends_with?(updated_media_item.thumbnail_filepath, ".webp")
@@ -258,7 +264,7 @@ defmodule Pinchflat.Downloading.MediaDownloaderTest do
end end
test "extracts the metadata_filepath", %{media_item: media_item} do test "extracts the metadata_filepath", %{media_item: media_item} do
expect(YtDlpRunnerMock, :run, 2, fn _url, _opts, _ot, _addl -> expect(YtDlpRunnerMock, :run, 1, fn _url, :download, _opts, _ot, _addl ->
metadata = render_parsed_metadata(:media_metadata) metadata = render_parsed_metadata(:media_metadata)
infojson_filepath = metadata["infojson_filename"] infojson_filepath = metadata["infojson_filename"]
@@ -267,6 +273,8 @@ defmodule Pinchflat.Downloading.MediaDownloaderTest do
{:ok, Phoenix.json_library().encode!(metadata)} {:ok, Phoenix.json_library().encode!(metadata)}
end) end)
expect(YtDlpRunnerMock, :run, 1, fn _url, :download_thumbnail, _opts, _ot, _addl_args -> {:ok, ""} end)
assert media_item.metadata_filepath == nil assert media_item.metadata_filepath == nil
assert {:ok, updated_media_item} = MediaDownloader.download_for_media_item(media_item) assert {:ok, updated_media_item} = MediaDownloader.download_for_media_item(media_item)
assert String.ends_with?(updated_media_item.metadata_filepath, ".info.json") assert String.ends_with?(updated_media_item.metadata_filepath, ".info.json")
@@ -277,8 +285,9 @@ defmodule Pinchflat.Downloading.MediaDownloaderTest do
describe "download_for_media_item/3 when testing NFO generation" do describe "download_for_media_item/3 when testing NFO generation" do
setup do setup do
stub(YtDlpRunnerMock, :run, fn _url, _opts, _ot, _addl -> stub(YtDlpRunnerMock, :run, fn
{:ok, render_metadata(:media_metadata)} _url, :download, _opts, _ot, _addl -> {:ok, render_metadata(:media_metadata)}
_url, :download_thumbnail, _opts, _ot, _addl -> {:ok, ""}
end) end)
:ok :ok
@@ -14,7 +14,7 @@ defmodule Pinchflat.FastIndexing.FastIndexingHelpersTest do
alias Pinchflat.FastIndexing.FastIndexingHelpers alias Pinchflat.FastIndexing.FastIndexingHelpers
setup do setup do
stub(YtDlpRunnerMock, :run, fn _url, _opts, _ot, _addl -> stub(YtDlpRunnerMock, :run, fn _url, :get_media_attributes, _opts, _ot, _addl ->
{:ok, media_attributes_return_fixture()} {:ok, media_attributes_return_fixture()}
end) end)
@@ -83,7 +83,7 @@ defmodule Pinchflat.FastIndexing.FastIndexingHelpersTest do
test "passes the source's download options to the yt-dlp runner", %{source: source} do test "passes the source's download options to the yt-dlp runner", %{source: source} do
expect(HTTPClientMock, :get, fn _url -> {:ok, "<yt:videoId>test_1</yt:videoId>"} end) expect(HTTPClientMock, :get, fn _url -> {:ok, "<yt:videoId>test_1</yt:videoId>"} end)
expect(YtDlpRunnerMock, :run, fn _url, opts, _ot, _addl_opts -> expect(YtDlpRunnerMock, :run, fn _url, :get_media_attributes, opts, _ot, _addl_opts ->
assert {:output, "/tmp/test/media/%(title)S.%(ext)S"} in opts assert {:output, "/tmp/test/media/%(title)S.%(ext)S"} in opts
assert {:remux_video, "mp4"} in opts assert {:remux_video, "mp4"} in opts
{:ok, media_attributes_return_fixture()} {:ok, media_attributes_return_fixture()}
@@ -95,7 +95,7 @@ defmodule Pinchflat.FastIndexing.FastIndexingHelpersTest do
test "sets use_cookies if the source uses cookies" do test "sets use_cookies if the source uses cookies" do
expect(HTTPClientMock, :get, fn _url -> {:ok, "<yt:videoId>test_1</yt:videoId>"} end) expect(HTTPClientMock, :get, fn _url -> {:ok, "<yt:videoId>test_1</yt:videoId>"} end)
stub(YtDlpRunnerMock, :run, fn _url, _opts, _ot, addl -> stub(YtDlpRunnerMock, :run, fn _url, :get_media_attributes, _opts, _ot, addl ->
assert {:use_cookies, true} in addl assert {:use_cookies, true} in addl
{:ok, media_attributes_return_fixture()} {:ok, media_attributes_return_fixture()}
@@ -109,7 +109,7 @@ defmodule Pinchflat.FastIndexing.FastIndexingHelpersTest do
test "does not set use_cookies if the source does not use cookies" do test "does not set use_cookies if the source does not use cookies" do
expect(HTTPClientMock, :get, fn _url -> {:ok, "<yt:videoId>test_1</yt:videoId>"} end) expect(HTTPClientMock, :get, fn _url -> {:ok, "<yt:videoId>test_1</yt:videoId>"} end)
stub(YtDlpRunnerMock, :run, fn _url, _opts, _ot, addl -> stub(YtDlpRunnerMock, :run, fn _url, :get_media_attributes, _opts, _ot, addl ->
assert {:use_cookies, false} in addl assert {:use_cookies, false} in addl
{:ok, media_attributes_return_fixture()} {:ok, media_attributes_return_fixture()}
@@ -126,7 +126,7 @@ defmodule Pinchflat.FastIndexing.FastIndexingHelpersTest do
profile = media_profile_fixture(%{shorts_behaviour: :exclude}) profile = media_profile_fixture(%{shorts_behaviour: :exclude})
source = source_fixture(%{media_profile_id: profile.id}) source = source_fixture(%{media_profile_id: profile.id})
stub(YtDlpRunnerMock, :run, fn _url, _opts, _ot, _addl -> stub(YtDlpRunnerMock, :run, fn _url, :get_media_attributes, _opts, _ot, _addl ->
output = output =
Phoenix.json_library().encode!(%{ Phoenix.json_library().encode!(%{
id: "video2", id: "video2",
@@ -150,7 +150,7 @@ defmodule Pinchflat.FastIndexing.FastIndexingHelpersTest do
test "does not blow up if a media item cannot be created", %{source: source} do test "does not blow up if a media item cannot be created", %{source: source} do
expect(HTTPClientMock, :get, fn _url -> {:ok, "<yt:videoId>test_1</yt:videoId>"} end) expect(HTTPClientMock, :get, fn _url -> {:ok, "<yt:videoId>test_1</yt:videoId>"} end)
stub(YtDlpRunnerMock, :run, fn _url, _opts, _ot, _addl -> stub(YtDlpRunnerMock, :run, fn _url, :get_media_attributes, _opts, _ot, _addl ->
{:ok, "{}"} {:ok, "{}"}
end) end)
@@ -160,7 +160,7 @@ defmodule Pinchflat.FastIndexing.FastIndexingHelpersTest do
test "does not blow up if a media item causes a yt-dlp error", %{source: source} do test "does not blow up if a media item causes a yt-dlp error", %{source: source} do
expect(HTTPClientMock, :get, fn _url -> {:ok, "<yt:videoId>test_1</yt:videoId>"} end) expect(HTTPClientMock, :get, fn _url -> {:ok, "<yt:videoId>test_1</yt:videoId>"} end)
stub(YtDlpRunnerMock, :run, fn _url, _opts, _ot, _addl -> stub(YtDlpRunnerMock, :run, fn _url, :get_media_attributes, _opts, _ot, _addl ->
{:error, "message", 1} {:error, "message", 1}
end) end)
@@ -84,7 +84,10 @@ defmodule Pinchflat.FastIndexing.FastIndexingWorkerTest do
source = source_fixture(fast_index: true) source = source_fixture(fast_index: true)
expect(HTTPClientMock, :get, fn _url -> {:ok, "<yt:videoId>test_1</yt:videoId>"} end) expect(HTTPClientMock, :get, fn _url -> {:ok, "<yt:videoId>test_1</yt:videoId>"} end)
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot, _addl -> {:ok, render_metadata(:media_metadata)} end)
expect(YtDlpRunnerMock, :run, fn _url, :get_media_attributes, _opts, _ot, _addl ->
{:ok, render_metadata(:media_metadata)}
end)
expect(AppriseRunnerMock, :run, fn servers, opts -> expect(AppriseRunnerMock, :run, fn servers, opts ->
assert "server_1" = servers assert "server_1" = servers
@@ -110,7 +113,11 @@ defmodule Pinchflat.FastIndexing.FastIndexingWorkerTest do
source = source_fixture(fast_index: true, download_media: false) source = source_fixture(fast_index: true, download_media: false)
expect(HTTPClientMock, :get, fn _url -> {:ok, "<yt:videoId>test_1</yt:videoId>"} end) expect(HTTPClientMock, :get, fn _url -> {:ok, "<yt:videoId>test_1</yt:videoId>"} end)
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot, _addl -> {:ok, render_metadata(:media_metadata)} end)
expect(YtDlpRunnerMock, :run, fn _url, :get_media_attributes, _opts, _ot, _addl ->
{:ok, render_metadata(:media_metadata)}
end)
expect(AppriseRunnerMock, :run, 0, fn _servers, _opts -> {:ok, ""} end) expect(AppriseRunnerMock, :run, 0, fn _servers, _opts -> {:ok, ""} end)
perform_job(FastIndexingWorker, %{id: source.id}) perform_job(FastIndexingWorker, %{id: source.id})
@@ -120,7 +127,11 @@ defmodule Pinchflat.FastIndexing.FastIndexingWorkerTest do
source = source_fixture(fast_index: true, title_filter_regex: "foobar") source = source_fixture(fast_index: true, title_filter_regex: "foobar")
expect(HTTPClientMock, :get, fn _url -> {:ok, "<yt:videoId>test_1</yt:videoId>"} end) expect(HTTPClientMock, :get, fn _url -> {:ok, "<yt:videoId>test_1</yt:videoId>"} end)
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot, _addl -> {:ok, render_metadata(:media_metadata)} end)
expect(YtDlpRunnerMock, :run, fn _url, :get_media_attributes, _opts, _ot, _addl ->
{:ok, render_metadata(:media_metadata)}
end)
expect(AppriseRunnerMock, :run, 0, fn _servers, _opts -> {:ok, ""} end) expect(AppriseRunnerMock, :run, 0, fn _servers, _opts -> {:ok, ""} end)
perform_job(FastIndexingWorker, %{id: source.id}) perform_job(FastIndexingWorker, %{id: source.id})
+3 -3
View File
@@ -757,7 +757,7 @@ defmodule Pinchflat.MediaTest do
end end
test "does delete the media item's metadata files" do test "does delete the media item's metadata files" do
stub(YtDlpRunnerMock, :run, fn _url, _opts, _ot, _addl -> {:ok, ""} end) stub(YtDlpRunnerMock, :run, fn _url, :download_thumbnail, _opts, _ot, _addl -> {:ok, ""} end)
media_item = Repo.preload(media_item_with_attachments(), [:metadata, :source]) media_item = Repo.preload(media_item_with_attachments(), [:metadata, :source])
update_attrs = %{ update_attrs = %{
@@ -789,7 +789,7 @@ defmodule Pinchflat.MediaTest do
end end
test "deletes the media item's metadata files" do test "deletes the media item's metadata files" do
stub(YtDlpRunnerMock, :run, fn _url, _opts, _ot, _addl -> {:ok, ""} end) stub(YtDlpRunnerMock, :run, fn _url, :download_thumbnail, _opts, _ot, _addl -> {:ok, ""} end)
media_item = Repo.preload(media_item_with_attachments(), [:metadata, :source]) media_item = Repo.preload(media_item_with_attachments(), [:metadata, :source])
update_attrs = %{ update_attrs = %{
@@ -875,7 +875,7 @@ defmodule Pinchflat.MediaTest do
end end
test "does not delete the media item's metadata files" do test "does not delete the media item's metadata files" do
stub(YtDlpRunnerMock, :run, fn _url, _opts, _ot, _addl -> {:ok, ""} end) stub(YtDlpRunnerMock, :run, fn _url, :download_thumbnail, _opts, _ot, _addl -> {:ok, ""} end)
media_item = Repo.preload(media_item_with_attachments(), [:metadata, :source]) media_item = Repo.preload(media_item_with_attachments(), [:metadata, :source])
update_attrs = %{ update_attrs = %{
@@ -62,7 +62,7 @@ defmodule Pinchflat.Metadata.MetadataFileHelpersTest do
describe "download_and_store_thumbnail_for/2" do describe "download_and_store_thumbnail_for/2" do
test "returns the filepath", %{media_item: media_item} do test "returns the filepath", %{media_item: media_item} do
stub(YtDlpRunnerMock, :run, fn _url, _opts, _ot, _addl -> {:ok, ""} end) stub(YtDlpRunnerMock, :run, fn _url, :download_thumbnail, _opts, _ot, _addl -> {:ok, ""} end)
filepath = Helpers.download_and_store_thumbnail_for(media_item) filepath = Helpers.download_and_store_thumbnail_for(media_item)
@@ -70,7 +70,7 @@ defmodule Pinchflat.Metadata.MetadataFileHelpersTest do
end end
test "calls yt-dlp with the expected options", %{media_item: media_item} do test "calls yt-dlp with the expected options", %{media_item: media_item} do
expect(YtDlpRunnerMock, :run, fn url, opts, ot, _addl -> expect(YtDlpRunnerMock, :run, fn url, :download_thumbnail, opts, ot, _addl ->
assert url == media_item.original_url assert url == media_item.original_url
assert ot == "after_move:%()j" assert ot == "after_move:%()j"
@@ -89,7 +89,7 @@ defmodule Pinchflat.Metadata.MetadataFileHelpersTest do
end end
test "sets use_cookies if the source uses cookies" do test "sets use_cookies if the source uses cookies" do
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot, addl -> expect(YtDlpRunnerMock, :run, fn _url, :download_thumbnail, _opts, _ot, addl ->
assert {:use_cookies, true} in addl assert {:use_cookies, true} in addl
{:ok, ""} {:ok, ""}
end) end)
@@ -101,7 +101,7 @@ defmodule Pinchflat.Metadata.MetadataFileHelpersTest do
end end
test "does not set use_cookies if the source does not use cookies" do test "does not set use_cookies if the source does not use cookies" do
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot, addl -> expect(YtDlpRunnerMock, :run, fn _url, :download_thumbnail, _opts, _ot, addl ->
assert {:use_cookies, false} in addl assert {:use_cookies, false} in addl
{:ok, ""} {:ok, ""}
end) end)
@@ -113,7 +113,7 @@ defmodule Pinchflat.Metadata.MetadataFileHelpersTest do
end end
test "returns nil if yt-dlp fails", %{media_item: media_item} do test "returns nil if yt-dlp fails", %{media_item: media_item} do
stub(YtDlpRunnerMock, :run, fn _url, _opts, _ot, _addl -> {:error, "error"} end) stub(YtDlpRunnerMock, :run, fn _url, :download_thumbnail, _opts, _ot, _addl -> {:error, "error"} end)
filepath = Helpers.download_and_store_thumbnail_for(media_item) filepath = Helpers.download_and_store_thumbnail_for(media_item)
@@ -8,9 +8,6 @@ defmodule Pinchflat.Metadata.SourceMetadataStorageWorkerTest do
alias Pinchflat.Metadata.MetadataFileHelpers alias Pinchflat.Metadata.MetadataFileHelpers
alias Pinchflat.Metadata.SourceMetadataStorageWorker alias Pinchflat.Metadata.SourceMetadataStorageWorker
@source_details_ot "%(.{channel,channel_id,playlist_id,playlist_title,filename})j"
@metadata_ot "playlist:%()j"
describe "kickoff_with_task/1" do describe "kickoff_with_task/1" do
test "enqueues a new worker for the source" do test "enqueues a new worker for the source" do
source = source_fixture() source = source_fixture()
@@ -32,8 +29,8 @@ defmodule Pinchflat.Metadata.SourceMetadataStorageWorkerTest do
describe "perform/1" do describe "perform/1" do
test "won't call itself in an infinite loop" do test "won't call itself in an infinite loop" do
stub(YtDlpRunnerMock, :run, fn stub(YtDlpRunnerMock, :run, fn
_url, _opts, ot, _addl when ot == @source_details_ot -> {:ok, source_details_return_fixture()} _url, :get_source_details, _opts, _ot, _addl -> {:ok, source_details_return_fixture()}
_url, _opts, ot, _addl when ot == @metadata_ot -> {:ok, "{}"} _url, :get_source_metadata, _opts, _ot, _addl -> {:ok, "{}"}
end) end)
source = source_fixture() source = source_fixture()
@@ -51,8 +48,8 @@ defmodule Pinchflat.Metadata.SourceMetadataStorageWorkerTest do
describe "perform/1 when testing attribute updates" do describe "perform/1 when testing attribute updates" do
test "the source description is saved" do test "the source description is saved" do
stub(YtDlpRunnerMock, :run, fn stub(YtDlpRunnerMock, :run, fn
_url, _opts, ot, _addl when ot == @source_details_ot -> {:ok, source_details_return_fixture()} _url, :get_source_details, _opts, _ot, _addl -> {:ok, source_details_return_fixture()}
_url, _opts, ot, _addl when ot == @metadata_ot -> {:ok, render_metadata(:channel_source_metadata)} _url, :get_source_metadata, _opts, _ot, _addl -> {:ok, render_metadata(:channel_source_metadata)}
end) end)
source = source_fixture(%{description: nil}) source = source_fixture(%{description: nil})
@@ -68,8 +65,8 @@ defmodule Pinchflat.Metadata.SourceMetadataStorageWorkerTest do
describe "perform/1 when testing metadata storage" do describe "perform/1 when testing metadata storage" do
test "sets metadata location for source" do test "sets metadata location for source" do
stub(YtDlpRunnerMock, :run, fn stub(YtDlpRunnerMock, :run, fn
_url, _opts, ot, _addl when ot == @source_details_ot -> {:ok, source_details_return_fixture()} _url, :get_source_details, _opts, _ot, _addl -> {:ok, source_details_return_fixture()}
_url, _opts, ot, _addl when ot == @metadata_ot -> {:ok, "{}"} _url, :get_source_metadata, _opts, _ot, _addl -> {:ok, "{}"}
end) end)
source = Repo.preload(source_fixture(), :metadata) source = Repo.preload(source_fixture(), :metadata)
@@ -87,8 +84,8 @@ defmodule Pinchflat.Metadata.SourceMetadataStorageWorkerTest do
file_contents = Phoenix.json_library().encode!(%{"title" => "test"}) file_contents = Phoenix.json_library().encode!(%{"title" => "test"})
stub(YtDlpRunnerMock, :run, fn stub(YtDlpRunnerMock, :run, fn
_url, _opts, ot, _addl when ot == @source_details_ot -> {:ok, source_details_return_fixture()} _url, :get_source_details, _opts, _ot, _addl -> {:ok, source_details_return_fixture()}
_url, _opts, ot, _addl when ot == @metadata_ot -> {:ok, file_contents} _url, :get_source_metadata, _opts, _ot, _addl -> {:ok, file_contents}
end) end)
perform_job(SourceMetadataStorageWorker, %{id: source.id}) perform_job(SourceMetadataStorageWorker, %{id: source.id})
@@ -100,8 +97,8 @@ defmodule Pinchflat.Metadata.SourceMetadataStorageWorkerTest do
test "sets metadata image location for source" do test "sets metadata image location for source" do
stub(YtDlpRunnerMock, :run, fn stub(YtDlpRunnerMock, :run, fn
_url, _opts, ot, _addl when ot == @source_details_ot -> {:ok, source_details_return_fixture()} _url, :get_source_details, _opts, _ot, _addl -> {:ok, source_details_return_fixture()}
_url, _opts, ot, _addl when ot == @metadata_ot -> {:ok, render_metadata(:channel_source_metadata)} _url, :get_source_metadata, _opts, _ot, _addl -> {:ok, render_metadata(:channel_source_metadata)}
end) end)
source = source_fixture() source = source_fixture()
@@ -118,8 +115,8 @@ defmodule Pinchflat.Metadata.SourceMetadataStorageWorkerTest do
test "stores metadata images for source" do test "stores metadata images for source" do
stub(YtDlpRunnerMock, :run, fn stub(YtDlpRunnerMock, :run, fn
_url, _opts, ot, _addl when ot == @source_details_ot -> {:ok, source_details_return_fixture()} _url, :get_source_details, _opts, _ot, _addl -> {:ok, source_details_return_fixture()}
_url, _opts, ot, _addl when ot == @metadata_ot -> {:ok, render_metadata(:channel_source_metadata)} _url, :get_source_metadata, _opts, _ot, _addl -> {:ok, render_metadata(:channel_source_metadata)}
end) end)
source = source_fixture() source = source_fixture()
@@ -138,12 +135,12 @@ defmodule Pinchflat.Metadata.SourceMetadataStorageWorkerTest do
describe "perform/1 when testing source image downloading" do describe "perform/1 when testing source image downloading" do
test "downloads and stores source images" do test "downloads and stores source images" do
stub(YtDlpRunnerMock, :run, fn stub(YtDlpRunnerMock, :run, fn
_url, _opts, ot, _addl when ot == @source_details_ot -> _url, :get_source_details, _opts, _ot, _addl ->
filename = Path.join([Application.get_env(:pinchflat, :media_directory), "Season 1", "bar.mp4"]) filename = Path.join([Application.get_env(:pinchflat, :media_directory), "Season 1", "bar.mp4"])
{:ok, source_details_return_fixture(%{filename: filename})} {:ok, source_details_return_fixture(%{filename: filename})}
_url, opts, ot, _addl when ot == @metadata_ot -> _url, :get_source_metadata, opts, _ot, _addl ->
assert {:convert_thumbnails, "jpg"} in opts assert {:convert_thumbnails, "jpg"} in opts
{:ok, render_metadata(:channel_source_metadata)} {:ok, render_metadata(:channel_source_metadata)}
@@ -168,10 +165,10 @@ defmodule Pinchflat.Metadata.SourceMetadataStorageWorkerTest do
test "calls one set of yt-dlp metadata opts for channels" do test "calls one set of yt-dlp metadata opts for channels" do
stub(YtDlpRunnerMock, :run, fn stub(YtDlpRunnerMock, :run, fn
_url, _opts, ot, _addl when ot == @source_details_ot -> _url, :get_source_details, _opts, _ot, _addl ->
{:ok, source_details_return_fixture()} {:ok, source_details_return_fixture()}
_url, opts, ot, _addl when ot == @metadata_ot -> _url, :get_source_metadata, opts, _ot, _addl ->
assert {:playlist_items, 0} in opts assert {:playlist_items, 0} in opts
assert :write_all_thumbnails in opts assert :write_all_thumbnails in opts
@@ -186,10 +183,10 @@ defmodule Pinchflat.Metadata.SourceMetadataStorageWorkerTest do
test "calls another set of yt-dlp metadata opts for playlists" do test "calls another set of yt-dlp metadata opts for playlists" do
stub(YtDlpRunnerMock, :run, fn stub(YtDlpRunnerMock, :run, fn
_url, _opts, ot, _addl when ot == @source_details_ot -> _url, :get_source_details, _opts, _ot, _addl ->
{:ok, source_details_return_fixture()} {:ok, source_details_return_fixture()}
_url, opts, ot, _addl when ot == @metadata_ot -> _url, :get_source_metadata, opts, _ot, _addl ->
assert {:playlist_items, 1} in opts assert {:playlist_items, 1} in opts
assert :write_thumbnail in opts assert :write_thumbnail in opts
@@ -204,12 +201,12 @@ defmodule Pinchflat.Metadata.SourceMetadataStorageWorkerTest do
test "does not store source images if the profile is not set to" do test "does not store source images if the profile is not set to" do
stub(YtDlpRunnerMock, :run, fn stub(YtDlpRunnerMock, :run, fn
_url, _opts, ot, _addl when ot == @source_details_ot -> _url, :get_source_details, _opts, _ot, _addl ->
filename = Path.join([Application.get_env(:pinchflat, :media_directory), "Season 1", "bar.mp4"]) filename = Path.join([Application.get_env(:pinchflat, :media_directory), "Season 1", "bar.mp4"])
{:ok, source_details_return_fixture(%{filename: filename})} {:ok, source_details_return_fixture(%{filename: filename})}
_url, _opts, ot, _addl when ot == @metadata_ot -> _url, :get_source_metadata, _opts, _ot, _addl ->
{:ok, render_metadata(:channel_source_metadata)} {:ok, render_metadata(:channel_source_metadata)}
end) end)
@@ -226,12 +223,12 @@ defmodule Pinchflat.Metadata.SourceMetadataStorageWorkerTest do
test "does not store source images if the series directory cannot be determined" do test "does not store source images if the series directory cannot be determined" do
stub(YtDlpRunnerMock, :run, fn stub(YtDlpRunnerMock, :run, fn
_url, _opts, ot, _addl when ot == @source_details_ot -> _url, :get_source_details, _opts, _ot, _addl ->
filename = Path.join([Application.get_env(:pinchflat, :media_directory), "foo", "bar.mp4"]) filename = Path.join([Application.get_env(:pinchflat, :media_directory), "foo", "bar.mp4"])
{:ok, source_details_return_fixture(%{filename: filename})} {:ok, source_details_return_fixture(%{filename: filename})}
_url, _opts, ot, _addl when ot == @metadata_ot -> _url, :get_source_metadata, _opts, _ot, _addl ->
{:ok, render_metadata(:channel_source_metadata)} {:ok, render_metadata(:channel_source_metadata)}
end) end)
@@ -248,10 +245,10 @@ defmodule Pinchflat.Metadata.SourceMetadataStorageWorkerTest do
test "sets use_cookies if the source uses cookies" do test "sets use_cookies if the source uses cookies" do
expect(YtDlpRunnerMock, :run, 2, fn expect(YtDlpRunnerMock, :run, 2, fn
_url, _opts, ot, _addl when ot == @source_details_ot -> _url, :get_source_details, _opts, _ot, _addl ->
{:ok, source_details_return_fixture()} {:ok, source_details_return_fixture()}
_url, _opts, ot, addl when ot == @metadata_ot -> _url, :get_source_metadata, _opts, _ot, addl ->
assert {:use_cookies, true} in addl assert {:use_cookies, true} in addl
{:ok, render_metadata(:channel_source_metadata)} {:ok, render_metadata(:channel_source_metadata)}
end) end)
@@ -264,10 +261,10 @@ defmodule Pinchflat.Metadata.SourceMetadataStorageWorkerTest do
test "does not set use_cookies if the source does not use cookies" do test "does not set use_cookies if the source does not use cookies" do
expect(YtDlpRunnerMock, :run, 2, fn expect(YtDlpRunnerMock, :run, 2, fn
_url, _opts, ot, _addl when ot == @source_details_ot -> _url, :get_source_details, _opts, _ot, _addl ->
{:ok, source_details_return_fixture()} {:ok, source_details_return_fixture()}
_url, _opts, ot, addl when ot == @metadata_ot -> _url, :get_source_metadata, _opts, _ot, addl ->
assert {:use_cookies, false} in addl assert {:use_cookies, false} in addl
{:ok, render_metadata(:channel_source_metadata)} {:ok, render_metadata(:channel_source_metadata)}
end) end)
@@ -282,12 +279,12 @@ defmodule Pinchflat.Metadata.SourceMetadataStorageWorkerTest do
describe "perform/1 when determining the series_directory" do describe "perform/1 when determining the series_directory" do
test "sets the series directory based on the returned media filepath" do test "sets the series directory based on the returned media filepath" do
stub(YtDlpRunnerMock, :run, fn stub(YtDlpRunnerMock, :run, fn
_url, _opts, ot, _addl when ot == @source_details_ot -> _url, :get_source_details, _opts, _ot, _addl ->
filename = Path.join([Application.get_env(:pinchflat, :media_directory), "Season 1", "bar.mp4"]) filename = Path.join([Application.get_env(:pinchflat, :media_directory), "Season 1", "bar.mp4"])
{:ok, source_details_return_fixture(%{filename: filename})} {:ok, source_details_return_fixture(%{filename: filename})}
_url, _opts, ot, _addl when ot == @metadata_ot -> _url, :get_source_metadata, _opts, _ot, _addl ->
{:ok, "{}"} {:ok, "{}"}
end) end)
@@ -300,12 +297,12 @@ defmodule Pinchflat.Metadata.SourceMetadataStorageWorkerTest do
test "does not set the series directory if it cannot be determined" do test "does not set the series directory if it cannot be determined" do
stub(YtDlpRunnerMock, :run, fn stub(YtDlpRunnerMock, :run, fn
_url, _opts, ot, _addl when ot == @source_details_ot -> _url, :get_source_details, _opts, _ot, _addl ->
filename = Path.join([Application.get_env(:pinchflat, :media_directory), "foo", "bar.mp4"]) filename = Path.join([Application.get_env(:pinchflat, :media_directory), "foo", "bar.mp4"])
{:ok, source_details_return_fixture(%{filename: filename})} {:ok, source_details_return_fixture(%{filename: filename})}
_url, _opts, ot, _addl when ot == @metadata_ot -> _url, :get_source_metadata, _opts, _ot, _addl ->
{:ok, "{}"} {:ok, "{}"}
end) end)
@@ -318,11 +315,11 @@ defmodule Pinchflat.Metadata.SourceMetadataStorageWorkerTest do
test "sets use_cookies if the source is set to use cookies" do test "sets use_cookies if the source is set to use cookies" do
expect(YtDlpRunnerMock, :run, 2, fn expect(YtDlpRunnerMock, :run, 2, fn
_url, _opts, ot, addl when ot == @source_details_ot -> _url, :get_source_details, _opts, _ot, addl ->
assert {:use_cookies, true} in addl assert {:use_cookies, true} in addl
{:ok, source_details_return_fixture()} {:ok, source_details_return_fixture()}
_url, _opts, ot, _addl when ot == @metadata_ot -> _url, :get_source_metadata, _opts, _ot, _addl ->
{:ok, "{}"} {:ok, "{}"}
end) end)
@@ -332,11 +329,11 @@ defmodule Pinchflat.Metadata.SourceMetadataStorageWorkerTest do
test "does not set use_cookies if the source is not set to use cookies" do test "does not set use_cookies if the source is not set to use cookies" do
expect(YtDlpRunnerMock, :run, 2, fn expect(YtDlpRunnerMock, :run, 2, fn
_url, _opts, ot, addl when ot == @source_details_ot -> _url, :get_source_details, _opts, _ot, addl ->
assert {:use_cookies, false} in addl assert {:use_cookies, false} in addl
{:ok, source_details_return_fixture()} {:ok, source_details_return_fixture()}
_url, _opts, ot, _addl when ot == @metadata_ot -> _url, :get_source_metadata, _opts, _ot, _addl ->
{:ok, "{}"} {:ok, "{}"}
end) end)
@@ -348,12 +345,12 @@ defmodule Pinchflat.Metadata.SourceMetadataStorageWorkerTest do
describe "perform/1 when storing the series NFO" do describe "perform/1 when storing the series NFO" do
test "stores the NFO if specified" do test "stores the NFO if specified" do
stub(YtDlpRunnerMock, :run, fn stub(YtDlpRunnerMock, :run, fn
_url, _opts, ot, _addl when ot == @source_details_ot -> _url, :get_source_details, _opts, _ot, _addl ->
filename = Path.join([Application.get_env(:pinchflat, :media_directory), "Season 1", "bar.mp4"]) filename = Path.join([Application.get_env(:pinchflat, :media_directory), "Season 1", "bar.mp4"])
{:ok, source_details_return_fixture(%{filename: filename})} {:ok, source_details_return_fixture(%{filename: filename})}
_url, _opts, ot, _addl when ot == @metadata_ot -> _url, :get_source_metadata, _opts, _ot, _addl ->
{:ok, "{}"} {:ok, "{}"}
end) end)
@@ -371,12 +368,12 @@ defmodule Pinchflat.Metadata.SourceMetadataStorageWorkerTest do
test "does not store the NFO if not specified" do test "does not store the NFO if not specified" do
stub(YtDlpRunnerMock, :run, fn stub(YtDlpRunnerMock, :run, fn
_url, _opts, ot, _addl when ot == @source_details_ot -> _url, :get_source_details, _opts, _ot, _addl ->
filename = Path.join([Application.get_env(:pinchflat, :media_directory), "Season 1", "bar.mp4"]) filename = Path.join([Application.get_env(:pinchflat, :media_directory), "Season 1", "bar.mp4"])
{:ok, source_details_return_fixture(%{filename: filename})} {:ok, source_details_return_fixture(%{filename: filename})}
_url, _opts, ot, _addl when ot == @metadata_ot -> _url, :get_source_metadata, _opts, _ot, _addl ->
{:ok, "{}"} {:ok, "{}"}
end) end)
@@ -390,12 +387,12 @@ defmodule Pinchflat.Metadata.SourceMetadataStorageWorkerTest do
test "does not store the NFO if the series directory cannot be determined" do test "does not store the NFO if the series directory cannot be determined" do
stub(YtDlpRunnerMock, :run, fn stub(YtDlpRunnerMock, :run, fn
_url, _opts, ot, _addl when ot == @source_details_ot -> _url, :get_source_details, _opts, _ot, _addl ->
filename = Path.join([Application.get_env(:pinchflat, :media_directory), "foo", "bar.mp4"]) filename = Path.join([Application.get_env(:pinchflat, :media_directory), "foo", "bar.mp4"])
{:ok, source_details_return_fixture(%{filename: filename})} {:ok, source_details_return_fixture(%{filename: filename})}
_url, _opts, ot, _addl when ot == @metadata_ot -> _url, :get_source_metadata, _opts, _ot, _addl ->
{:ok, "{}"} {:ok, "{}"}
end) end)
@@ -50,37 +50,35 @@ defmodule Pinchflat.SlowIndexing.MediaCollectionIndexingWorkerTest do
describe "perform/1" do describe "perform/1" do
setup do setup do
stub(YtDlpRunnerMock, :run, fn _url, :get_media_attributes_for_collection, _opts, _ot, _addl_opts -> {:ok, ""} end)
stub(AppriseRunnerMock, :run, fn _, _ -> {:ok, ""} end) stub(AppriseRunnerMock, :run, fn _, _ -> {:ok, ""} end)
:ok :ok
end end
test "it indexes the source if it should be indexed" do test "it indexes the source if it should be indexed" do
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot, _addl_opts -> {:ok, ""} end)
source = source_fixture(index_frequency_minutes: 10) source = source_fixture(index_frequency_minutes: 10)
perform_job(MediaCollectionIndexingWorker, %{id: source.id}) perform_job(MediaCollectionIndexingWorker, %{id: source.id})
end end
test "it indexes the source no matter what if the source has never been indexed before" do test "it indexes the source no matter what if the source has never been indexed before" do
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot, _addl_opts -> {:ok, ""} end)
source = source_fixture(index_frequency_minutes: 0, last_indexed_at: nil) source = source_fixture(index_frequency_minutes: 0, last_indexed_at: nil)
perform_job(MediaCollectionIndexingWorker, %{id: source.id}) perform_job(MediaCollectionIndexingWorker, %{id: source.id})
end end
test "it indexes the source no matter what if the 'force' arg is passed" do test "it indexes the source no matter what if the 'force' arg is passed" do
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot, _addl_opts -> {:ok, ""} end)
source = source_fixture(index_frequency_minutes: 0, last_indexed_at: DateTime.utc_now()) source = source_fixture(index_frequency_minutes: 0, last_indexed_at: DateTime.utc_now())
perform_job(MediaCollectionIndexingWorker, %{id: source.id, force: true}) perform_job(MediaCollectionIndexingWorker, %{id: source.id, force: true})
end end
test "it does not do any indexing if the source has been indexed and shouldn't be rescheduled" do test "it does not do any indexing if the source has been indexed and shouldn't be rescheduled" do
expect(YtDlpRunnerMock, :run, 0, fn _url, _opts, _ot, _addl_opts -> {:ok, ""} end) expect(YtDlpRunnerMock, :run, 0, fn _url, :get_media_attributes_for_collection, _opts, _ot, _addl_opts ->
{:ok, ""}
end)
source = source_fixture(index_frequency_minutes: -1, last_indexed_at: DateTime.utc_now()) source = source_fixture(index_frequency_minutes: -1, last_indexed_at: DateTime.utc_now())
@@ -88,7 +86,7 @@ defmodule Pinchflat.SlowIndexing.MediaCollectionIndexingWorkerTest do
end end
test "it does not reschedule if the source shouldn't be indexed" do test "it does not reschedule if the source shouldn't be indexed" do
stub(YtDlpRunnerMock, :run, fn _url, _opts, _ot, _addl_opts -> {:ok, ""} end) stub(YtDlpRunnerMock, :run, fn _url, :get_media_attributes_for_collection, _opts, _ot, _addl_opts -> {:ok, ""} end)
source = source_fixture(index_frequency_minutes: -1) source = source_fixture(index_frequency_minutes: -1)
perform_job(MediaCollectionIndexingWorker, %{id: source.id}) perform_job(MediaCollectionIndexingWorker, %{id: source.id})
@@ -97,7 +95,7 @@ defmodule Pinchflat.SlowIndexing.MediaCollectionIndexingWorkerTest do
end end
test "it kicks off a download job for each pending media item" do test "it kicks off a download job for each pending media item" do
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot, _addl_opts -> expect(YtDlpRunnerMock, :run, fn _url, :get_media_attributes_for_collection, _opts, _ot, _addl_opts ->
{:ok, source_attributes_return_fixture()} {:ok, source_attributes_return_fixture()}
end) end)
@@ -108,7 +106,7 @@ defmodule Pinchflat.SlowIndexing.MediaCollectionIndexingWorkerTest do
end end
test "it starts a job for any pending media item even if it's from another run" do test "it starts a job for any pending media item even if it's from another run" do
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot, _addl_opts -> expect(YtDlpRunnerMock, :run, fn _url, :get_media_attributes_for_collection, _opts, _ot, _addl_opts ->
{:ok, source_attributes_return_fixture()} {:ok, source_attributes_return_fixture()}
end) end)
@@ -120,7 +118,7 @@ defmodule Pinchflat.SlowIndexing.MediaCollectionIndexingWorkerTest do
end end
test "it does not kick off a job for media items that could not be saved" do test "it does not kick off a job for media items that could not be saved" do
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot, _addl_opts -> expect(YtDlpRunnerMock, :run, fn _url, :get_media_attributes_for_collection, _opts, _ot, _addl_opts ->
{:ok, source_attributes_return_fixture()} {:ok, source_attributes_return_fixture()}
end) end)
@@ -133,8 +131,6 @@ defmodule Pinchflat.SlowIndexing.MediaCollectionIndexingWorkerTest do
end end
test "it reschedules the job based on the index frequency" do test "it reschedules the job based on the index frequency" do
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot, _addl_opts -> {:ok, ""} end)
source = source_fixture(index_frequency_minutes: 10) source = source_fixture(index_frequency_minutes: 10)
perform_job(MediaCollectionIndexingWorker, %{id: source.id}) perform_job(MediaCollectionIndexingWorker, %{id: source.id})
@@ -146,8 +142,6 @@ defmodule Pinchflat.SlowIndexing.MediaCollectionIndexingWorkerTest do
end end
test "it creates a task for the rescheduled job" do test "it creates a task for the rescheduled job" do
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot, _addl_opts -> {:ok, ""} end)
source = source_fixture(index_frequency_minutes: 10) source = source_fixture(index_frequency_minutes: 10)
task_count_fetcher = fn -> task_count_fetcher = fn ->
@@ -160,8 +154,6 @@ defmodule Pinchflat.SlowIndexing.MediaCollectionIndexingWorkerTest do
end end
test "it creates a future task for fast indexing if appropriate" do test "it creates a future task for fast indexing if appropriate" do
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot, _addl_opts -> {:ok, ""} end)
source = source_fixture(index_frequency_minutes: 10, fast_index: true) source = source_fixture(index_frequency_minutes: 10, fast_index: true)
perform_job(MediaCollectionIndexingWorker, %{id: source.id}) perform_job(MediaCollectionIndexingWorker, %{id: source.id})
@@ -173,8 +165,6 @@ defmodule Pinchflat.SlowIndexing.MediaCollectionIndexingWorkerTest do
end end
test "it deletes existing fast indexing tasks if a new one is created" do test "it deletes existing fast indexing tasks if a new one is created" do
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot, _addl_opts -> {:ok, ""} end)
source = source_fixture(index_frequency_minutes: 10, fast_index: true) source = source_fixture(index_frequency_minutes: 10, fast_index: true)
{:ok, job} = Oban.insert(FastIndexingWorker.new(%{"id" => source.id})) {:ok, job} = Oban.insert(FastIndexingWorker.new(%{"id" => source.id}))
task = task_fixture(source_id: source.id, job_id: job.id) task = task_fixture(source_id: source.id, job_id: job.id)
@@ -185,8 +175,6 @@ defmodule Pinchflat.SlowIndexing.MediaCollectionIndexingWorkerTest do
end end
test "it does not create a task for fast indexing otherwise" do test "it does not create a task for fast indexing otherwise" do
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot, _addl_opts -> {:ok, ""} end)
source = source_fixture(index_frequency_minutes: 10, fast_index: false) source = source_fixture(index_frequency_minutes: 10, fast_index: false)
perform_job(MediaCollectionIndexingWorker, %{id: source.id}) perform_job(MediaCollectionIndexingWorker, %{id: source.id})
@@ -194,7 +182,9 @@ defmodule Pinchflat.SlowIndexing.MediaCollectionIndexingWorkerTest do
end end
test "it creates the basic media_item records" do test "it creates the basic media_item records" do
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot, _addl_opts -> {:ok, source_attributes_return_fixture()} end) expect(YtDlpRunnerMock, :run, fn _url, :get_media_attributes_for_collection, _opts, _ot, _addl_opts ->
{:ok, source_attributes_return_fixture()}
end)
source = source_fixture(index_frequency_minutes: 10) source = source_fixture(index_frequency_minutes: 10)
@@ -225,7 +215,7 @@ defmodule Pinchflat.SlowIndexing.MediaCollectionIndexingWorkerTest do
test "sends a notification if new media was found" do test "sends a notification if new media was found" do
source = source_fixture() source = source_fixture()
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot, _addl_opts -> expect(YtDlpRunnerMock, :run, fn _url, :get_media_attributes_for_collection, _opts, _ot, _addl_opts ->
{:ok, source_attributes_return_fixture()} {:ok, source_attributes_return_fixture()}
end) end)
@@ -174,7 +174,7 @@ defmodule Pinchflat.SlowIndexing.SlowIndexingHelpersTest do
describe "index_and_enqueue_download_for_media_items/1" do describe "index_and_enqueue_download_for_media_items/1" do
setup do setup do
stub(YtDlpRunnerMock, :run, fn _url, _opts, _ot, _addl_opts -> stub(YtDlpRunnerMock, :run, fn _url, :get_media_attributes_for_collection, _opts, _ot, _addl_opts ->
{:ok, source_attributes_return_fixture()} {:ok, source_attributes_return_fixture()}
end) end)
@@ -259,7 +259,7 @@ defmodule Pinchflat.SlowIndexing.SlowIndexingHelpersTest do
end end
test "doesn't blow up if a media item cannot be coerced into a struct", %{source: source} do test "doesn't blow up if a media item cannot be coerced into a struct", %{source: source} do
stub(YtDlpRunnerMock, :run, fn _url, _opts, _ot, _addl_opts -> stub(YtDlpRunnerMock, :run, fn _url, :get_media_attributes_for_collection, _opts, _ot, _addl_opts ->
response = response =
Phoenix.json_library().encode!(%{ Phoenix.json_library().encode!(%{
id: "video3", id: "video3",
@@ -283,7 +283,7 @@ defmodule Pinchflat.SlowIndexing.SlowIndexingHelpersTest do
end end
test "passes the source's download options to the yt-dlp runner", %{source: source} do test "passes the source's download options to the yt-dlp runner", %{source: source} do
expect(YtDlpRunnerMock, :run, fn _url, opts, _ot, _addl_opts -> expect(YtDlpRunnerMock, :run, fn _url, :get_media_attributes_for_collection, opts, _ot, _addl_opts ->
assert {:output, "/tmp/test/media/%(title)S.%(ext)S"} in opts assert {:output, "/tmp/test/media/%(title)S.%(ext)S"} in opts
assert {:remux_video, "mp4"} in opts assert {:remux_video, "mp4"} in opts
{:ok, source_attributes_return_fixture()} {:ok, source_attributes_return_fixture()}
@@ -293,7 +293,7 @@ defmodule Pinchflat.SlowIndexing.SlowIndexingHelpersTest do
end end
test "sets use_cookies if the source uses cookies" do test "sets use_cookies if the source uses cookies" do
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot, addl_opts -> expect(YtDlpRunnerMock, :run, fn _url, :get_media_attributes_for_collection, _opts, _ot, addl_opts ->
assert {:use_cookies, true} in addl_opts assert {:use_cookies, true} in addl_opts
{:ok, source_attributes_return_fixture()} {:ok, source_attributes_return_fixture()}
end) end)
@@ -304,7 +304,7 @@ defmodule Pinchflat.SlowIndexing.SlowIndexingHelpersTest do
end end
test "doesn't set use_cookies if the source doesn't use cookies" do test "doesn't set use_cookies if the source doesn't use cookies" do
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot, addl_opts -> expect(YtDlpRunnerMock, :run, fn _url, :get_media_attributes_for_collection, _opts, _ot, addl_opts ->
assert {:use_cookies, false} in addl_opts assert {:use_cookies, false} in addl_opts
{:ok, source_attributes_return_fixture()} {:ok, source_attributes_return_fixture()}
end) end)
@@ -323,7 +323,7 @@ defmodule Pinchflat.SlowIndexing.SlowIndexingHelpersTest do
test "creates a new media item for everything already in the file", %{source: source} do test "creates a new media item for everything already in the file", %{source: source} do
watcher_poll_interval = Application.get_env(:pinchflat, :file_watcher_poll_interval) watcher_poll_interval = Application.get_env(:pinchflat, :file_watcher_poll_interval)
stub(YtDlpRunnerMock, :run, fn _url, _opts, _ot, addl_opts -> stub(YtDlpRunnerMock, :run, fn _url, :get_media_attributes_for_collection, _opts, _ot, addl_opts ->
filepath = Keyword.get(addl_opts, :output_filepath) filepath = Keyword.get(addl_opts, :output_filepath)
File.write(filepath, source_attributes_return_fixture()) File.write(filepath, source_attributes_return_fixture())
@@ -342,7 +342,7 @@ defmodule Pinchflat.SlowIndexing.SlowIndexingHelpersTest do
test "enqueues a download for everything already in the file", %{source: source} do test "enqueues a download for everything already in the file", %{source: source} do
watcher_poll_interval = Application.get_env(:pinchflat, :file_watcher_poll_interval) watcher_poll_interval = Application.get_env(:pinchflat, :file_watcher_poll_interval)
stub(YtDlpRunnerMock, :run, fn _url, _opts, _ot, addl_opts -> stub(YtDlpRunnerMock, :run, fn _url, :get_media_attributes_for_collection, _opts, _ot, addl_opts ->
filepath = Keyword.get(addl_opts, :output_filepath) filepath = Keyword.get(addl_opts, :output_filepath)
File.write(filepath, source_attributes_return_fixture()) File.write(filepath, source_attributes_return_fixture())
@@ -362,7 +362,7 @@ defmodule Pinchflat.SlowIndexing.SlowIndexingHelpersTest do
watcher_poll_interval = Application.get_env(:pinchflat, :file_watcher_poll_interval) watcher_poll_interval = Application.get_env(:pinchflat, :file_watcher_poll_interval)
source = source_fixture(download_media: false) source = source_fixture(download_media: false)
stub(YtDlpRunnerMock, :run, fn _url, _opts, _ot, addl_opts -> stub(YtDlpRunnerMock, :run, fn _url, :get_media_attributes_for_collection, _opts, _ot, addl_opts ->
filepath = Keyword.get(addl_opts, :output_filepath) filepath = Keyword.get(addl_opts, :output_filepath)
File.write(filepath, source_attributes_return_fixture()) File.write(filepath, source_attributes_return_fixture())
@@ -382,7 +382,7 @@ defmodule Pinchflat.SlowIndexing.SlowIndexingHelpersTest do
profile = media_profile_fixture(%{shorts_behaviour: :exclude}) profile = media_profile_fixture(%{shorts_behaviour: :exclude})
source = source_fixture(%{media_profile_id: profile.id}) source = source_fixture(%{media_profile_id: profile.id})
stub(YtDlpRunnerMock, :run, fn _url, _opts, _ot, addl_opts -> stub(YtDlpRunnerMock, :run, fn _url, :get_media_attributes_for_collection, _opts, _ot, addl_opts ->
filepath = Keyword.get(addl_opts, :output_filepath) filepath = Keyword.get(addl_opts, :output_filepath)
contents = contents =
@@ -413,7 +413,7 @@ defmodule Pinchflat.SlowIndexing.SlowIndexingHelpersTest do
test "does not enqueue multiple download jobs for the same media items", %{source: source} do test "does not enqueue multiple download jobs for the same media items", %{source: source} do
watcher_poll_interval = Application.get_env(:pinchflat, :file_watcher_poll_interval) watcher_poll_interval = Application.get_env(:pinchflat, :file_watcher_poll_interval)
stub(YtDlpRunnerMock, :run, fn _url, _opts, _ot, addl_opts -> stub(YtDlpRunnerMock, :run, fn _url, :get_media_attributes_for_collection, _opts, _ot, addl_opts ->
filepath = Keyword.get(addl_opts, :output_filepath) filepath = Keyword.get(addl_opts, :output_filepath)
File.write(filepath, source_attributes_return_fixture()) File.write(filepath, source_attributes_return_fixture())
@@ -432,7 +432,7 @@ defmodule Pinchflat.SlowIndexing.SlowIndexingHelpersTest do
test "does not blow up if the file returns invalid json", %{source: source} do test "does not blow up if the file returns invalid json", %{source: source} do
watcher_poll_interval = Application.get_env(:pinchflat, :file_watcher_poll_interval) watcher_poll_interval = Application.get_env(:pinchflat, :file_watcher_poll_interval)
stub(YtDlpRunnerMock, :run, fn _url, _opts, _ot, addl_opts -> stub(YtDlpRunnerMock, :run, fn _url, :get_media_attributes_for_collection, _opts, _ot, addl_opts ->
filepath = Keyword.get(addl_opts, :output_filepath) filepath = Keyword.get(addl_opts, :output_filepath)
File.write(filepath, "INVALID") File.write(filepath, "INVALID")
+27 -27
View File
@@ -85,7 +85,7 @@ defmodule Pinchflat.SourcesTest do
describe "create_source/2" do describe "create_source/2" do
test "automatically sets the UUID" do test "automatically sets the UUID" do
expect(YtDlpRunnerMock, :run, &channel_mock/4) expect(YtDlpRunnerMock, :run, &channel_mock/5)
valid_attrs = %{ valid_attrs = %{
media_profile_id: media_profile_fixture().id, media_profile_id: media_profile_fixture().id,
@@ -97,7 +97,7 @@ defmodule Pinchflat.SourcesTest do
end end
test "UUID is not writable by the user" do test "UUID is not writable by the user" do
expect(YtDlpRunnerMock, :run, &channel_mock/4) expect(YtDlpRunnerMock, :run, &channel_mock/5)
valid_attrs = %{ valid_attrs = %{
media_profile_id: media_profile_fixture().id, media_profile_id: media_profile_fixture().id,
@@ -110,7 +110,7 @@ defmodule Pinchflat.SourcesTest do
end end
test "creates a source and adds name + ID from runner response for channels" do test "creates a source and adds name + ID from runner response for channels" do
expect(YtDlpRunnerMock, :run, &channel_mock/4) expect(YtDlpRunnerMock, :run, &channel_mock/5)
valid_attrs = %{ valid_attrs = %{
media_profile_id: media_profile_fixture().id, media_profile_id: media_profile_fixture().id,
@@ -123,7 +123,7 @@ defmodule Pinchflat.SourcesTest do
end end
test "creates a source and adds name + ID for playlists" do test "creates a source and adds name + ID for playlists" do
expect(YtDlpRunnerMock, :run, &playlist_mock/4) expect(YtDlpRunnerMock, :run, &playlist_mock/5)
valid_attrs = %{ valid_attrs = %{
media_profile_id: media_profile_fixture().id, media_profile_id: media_profile_fixture().id,
@@ -136,7 +136,7 @@ defmodule Pinchflat.SourcesTest do
end end
test "adds an error if the runner fails" do test "adds an error if the runner fails" do
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot, _addl -> {:error, "some error", 1} end) expect(YtDlpRunnerMock, :run, fn _url, :get_source_details, _opts, _ot, _addl -> {:error, "some error", 1} end)
valid_attrs = %{ valid_attrs = %{
media_profile_id: media_profile_fixture().id, media_profile_id: media_profile_fixture().id,
@@ -148,7 +148,7 @@ defmodule Pinchflat.SourcesTest do
end end
test "adds an error if the runner succeeds but the result was invalid JSON" do test "adds an error if the runner succeeds but the result was invalid JSON" do
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot, _addl -> {:ok, "Not JSON"} end) expect(YtDlpRunnerMock, :run, fn _url, :get_source_details, _opts, _ot, _addl -> {:ok, "Not JSON"} end)
valid_attrs = %{ valid_attrs = %{
media_profile_id: media_profile_fixture().id, media_profile_id: media_profile_fixture().id,
@@ -160,7 +160,7 @@ defmodule Pinchflat.SourcesTest do
end end
test "you can specify a custom custom_name" do test "you can specify a custom custom_name" do
expect(YtDlpRunnerMock, :run, &channel_mock/4) expect(YtDlpRunnerMock, :run, &channel_mock/5)
valid_attrs = %{ valid_attrs = %{
media_profile_id: media_profile_fixture().id, media_profile_id: media_profile_fixture().id,
@@ -174,7 +174,7 @@ defmodule Pinchflat.SourcesTest do
end end
test "friendly name is pulled from collection_name if not specified" do test "friendly name is pulled from collection_name if not specified" do
expect(YtDlpRunnerMock, :run, &channel_mock/4) expect(YtDlpRunnerMock, :run, &channel_mock/5)
valid_attrs = %{ valid_attrs = %{
media_profile_id: media_profile_fixture().id, media_profile_id: media_profile_fixture().id,
@@ -187,7 +187,7 @@ defmodule Pinchflat.SourcesTest do
end end
test "creation enforces uniqueness of collection_id scoped to the media_profile and title regex" do test "creation enforces uniqueness of collection_id scoped to the media_profile and title regex" do
expect(YtDlpRunnerMock, :run, 2, fn _url, _opts, _ot, _addl -> expect(YtDlpRunnerMock, :run, 2, fn _url, :get_source_details, _opts, _ot, _addl ->
{:ok, {:ok,
Phoenix.json_library().encode!(%{ Phoenix.json_library().encode!(%{
channel: "some channel name", channel: "some channel name",
@@ -208,7 +208,7 @@ defmodule Pinchflat.SourcesTest do
end end
test "creation lets you duplicate collection_ids and profiles as long as the regex is different" do test "creation lets you duplicate collection_ids and profiles as long as the regex is different" do
expect(YtDlpRunnerMock, :run, 2, fn _url, _opts, _ot, _addl -> expect(YtDlpRunnerMock, :run, 2, fn _url, :get_source_details, _opts, _ot, _addl ->
{:ok, {:ok,
Phoenix.json_library().encode!(%{ Phoenix.json_library().encode!(%{
channel: "some channel name", channel: "some channel name",
@@ -232,7 +232,7 @@ defmodule Pinchflat.SourcesTest do
end end
test "creation lets you duplicate collection_ids as long as the media profile is different" do test "creation lets you duplicate collection_ids as long as the media profile is different" do
expect(YtDlpRunnerMock, :run, 2, fn _url, _opts, _ot, _addl -> expect(YtDlpRunnerMock, :run, 2, fn _url, :get_source_details, _opts, _ot, _addl ->
{:ok, {:ok,
Phoenix.json_library().encode!(%{ Phoenix.json_library().encode!(%{
channel: "some channel name", channel: "some channel name",
@@ -256,8 +256,8 @@ defmodule Pinchflat.SourcesTest do
end end
test "collection_type is inferred from source details" do test "collection_type is inferred from source details" do
expect(YtDlpRunnerMock, :run, &channel_mock/4) expect(YtDlpRunnerMock, :run, &channel_mock/5)
expect(YtDlpRunnerMock, :run, &playlist_mock/4) expect(YtDlpRunnerMock, :run, &playlist_mock/5)
valid_attrs = %{ valid_attrs = %{
media_profile_id: media_profile_fixture().id, media_profile_id: media_profile_fixture().id,
@@ -276,13 +276,13 @@ defmodule Pinchflat.SourcesTest do
end end
test "creation with invalid data fails fast and does not call the runner" do test "creation with invalid data fails fast and does not call the runner" do
expect(YtDlpRunnerMock, :run, 0, &channel_mock/4) expect(YtDlpRunnerMock, :run, 0, &channel_mock/5)
assert {:error, %Ecto.Changeset{}} = Sources.create_source(@invalid_source_attrs) assert {:error, %Ecto.Changeset{}} = Sources.create_source(@invalid_source_attrs)
end end
test "creation will schedule the indexing task" do test "creation will schedule the indexing task" do
expect(YtDlpRunnerMock, :run, &channel_mock/4) expect(YtDlpRunnerMock, :run, &channel_mock/5)
valid_attrs = %{ valid_attrs = %{
media_profile_id: media_profile_fixture().id, media_profile_id: media_profile_fixture().id,
@@ -295,7 +295,7 @@ defmodule Pinchflat.SourcesTest do
end end
test "creation schedules an index test even if the index frequency is 0" do test "creation schedules an index test even if the index frequency is 0" do
expect(YtDlpRunnerMock, :run, &channel_mock/4) expect(YtDlpRunnerMock, :run, &channel_mock/5)
valid_attrs = %{ valid_attrs = %{
media_profile_id: media_profile_fixture().id, media_profile_id: media_profile_fixture().id,
@@ -309,7 +309,7 @@ defmodule Pinchflat.SourcesTest do
end end
test "fast_index forces the index frequency to be a default value" do test "fast_index forces the index frequency to be a default value" do
expect(YtDlpRunnerMock, :run, &channel_mock/4) expect(YtDlpRunnerMock, :run, &channel_mock/5)
valid_attrs = %{ valid_attrs = %{
media_profile_id: media_profile_fixture().id, media_profile_id: media_profile_fixture().id,
@@ -324,7 +324,7 @@ defmodule Pinchflat.SourcesTest do
end end
test "disabling fast index will not change the index frequency" do test "disabling fast index will not change the index frequency" do
expect(YtDlpRunnerMock, :run, &channel_mock/4) expect(YtDlpRunnerMock, :run, &channel_mock/5)
valid_attrs = %{ valid_attrs = %{
media_profile_id: media_profile_fixture().id, media_profile_id: media_profile_fixture().id,
@@ -339,7 +339,7 @@ defmodule Pinchflat.SourcesTest do
end end
test "creating will kickoff a metadata storage worker" do test "creating will kickoff a metadata storage worker" do
expect(YtDlpRunnerMock, :run, &channel_mock/4) expect(YtDlpRunnerMock, :run, &channel_mock/5)
valid_attrs = %{ valid_attrs = %{
media_profile_id: media_profile_fixture().id, media_profile_id: media_profile_fixture().id,
@@ -356,7 +356,7 @@ defmodule Pinchflat.SourcesTest do
describe "create_source/2 when testing options" do describe "create_source/2 when testing options" do
test "run_post_commit_tasks: false won't enqueue post-commit tasks" do test "run_post_commit_tasks: false won't enqueue post-commit tasks" do
expect(YtDlpRunnerMock, :run, &channel_mock/4) expect(YtDlpRunnerMock, :run, &channel_mock/5)
valid_attrs = %{ valid_attrs = %{
media_profile_id: media_profile_fixture().id, media_profile_id: media_profile_fixture().id,
@@ -380,7 +380,7 @@ defmodule Pinchflat.SourcesTest do
end end
test "updates with invalid data fails fast and does not call the runner" do test "updates with invalid data fails fast and does not call the runner" do
expect(YtDlpRunnerMock, :run, 0, &channel_mock/4) expect(YtDlpRunnerMock, :run, 0, &channel_mock/5)
source = source_fixture() source = source_fixture()
@@ -388,7 +388,7 @@ defmodule Pinchflat.SourcesTest do
end end
test "updating the original_url will re-fetch the source details for channels" do test "updating the original_url will re-fetch the source details for channels" do
expect(YtDlpRunnerMock, :run, &channel_mock/4) expect(YtDlpRunnerMock, :run, &channel_mock/5)
source = source_fixture() source = source_fixture()
update_attrs = %{original_url: "https://www.youtube.com/channel/abc123"} update_attrs = %{original_url: "https://www.youtube.com/channel/abc123"}
@@ -399,7 +399,7 @@ defmodule Pinchflat.SourcesTest do
end end
test "updating the original_url will re-fetch the source details for playlists" do test "updating the original_url will re-fetch the source details for playlists" do
expect(YtDlpRunnerMock, :run, &playlist_mock/4) expect(YtDlpRunnerMock, :run, &playlist_mock/5)
source = source_fixture() source = source_fixture()
update_attrs = %{original_url: "https://www.youtube.com/playlist?list=abc123"} update_attrs = %{original_url: "https://www.youtube.com/playlist?list=abc123"}
@@ -410,7 +410,7 @@ defmodule Pinchflat.SourcesTest do
end end
test "not updating the original_url will not re-fetch the source details" do test "not updating the original_url will not re-fetch the source details" do
expect(YtDlpRunnerMock, :run, 0, &channel_mock/4) expect(YtDlpRunnerMock, :run, 0, &channel_mock/5)
source = source_fixture() source = source_fixture()
update_attrs = %{name: "some updated name"} update_attrs = %{name: "some updated name"}
@@ -428,7 +428,7 @@ defmodule Pinchflat.SourcesTest do
end end
test "updating will kickoff a metadata storage worker if the original_url changes" do test "updating will kickoff a metadata storage worker if the original_url changes" do
expect(YtDlpRunnerMock, :run, &playlist_mock/4) expect(YtDlpRunnerMock, :run, &playlist_mock/5)
source = source_fixture() source = source_fixture()
update_attrs = %{original_url: "https://www.youtube.com/channel/cba321"} update_attrs = %{original_url: "https://www.youtube.com/channel/cba321"}
@@ -901,7 +901,7 @@ defmodule Pinchflat.SourcesTest do
end end
end end
defp playlist_mock(_url, _opts, _ot, _addl) do defp playlist_mock(_url, :get_source_details, _opts, _ot, _addl) do
{ {
:ok, :ok,
Phoenix.json_library().encode!(%{ Phoenix.json_library().encode!(%{
@@ -913,7 +913,7 @@ defmodule Pinchflat.SourcesTest do
} }
end end
defp channel_mock(_url, _opts, _ot, _addl) do defp channel_mock(_url, :get_source_details, _opts, _ot, _addl) do
channel_id = "some_channel_id_#{:rand.uniform(1_000_000)}" channel_id = "some_channel_id_#{:rand.uniform(1_000_000)}"
{ {
+16 -16
View File
@@ -13,30 +13,30 @@ defmodule Pinchflat.YtDlp.CommandRunnerTest do
end end
describe "run/4" do describe "run/4" do
test "it returns the output and status when the command succeeds" do test "returns the output and status when the command succeeds" do
assert {:ok, _output} = Runner.run(@media_url, [], "") assert {:ok, _output} = Runner.run(@media_url, :foo, [], "")
end end
test "it includes the media url as the first argument" do test "includes the media url as the first argument" do
assert {:ok, output} = Runner.run(@media_url, [:ignore_errors], "") assert {:ok, output} = Runner.run(@media_url, :foo, [:ignore_errors], "")
assert String.contains?(output, "#{@media_url} --ignore-errors") assert String.contains?(output, "#{@media_url} --ignore-errors")
end end
test "it automatically includes the --print-to-file flag" do test "automatically includes the --print-to-file flag" do
assert {:ok, output} = Runner.run(@media_url, [], "%(id)s") assert {:ok, output} = Runner.run(@media_url, :foo, [], "%(id)s")
assert String.contains?(output, "--print-to-file %(id)s /tmp/") assert String.contains?(output, "--print-to-file %(id)s /tmp/")
end end
test "it returns the output and status when the command fails" do test "returns the output and status when the command fails" do
wrap_executable("/bin/false", fn -> wrap_executable("/bin/false", fn ->
assert {:error, "", 1} = Runner.run(@media_url, [], "") assert {:error, "", 1} = Runner.run(@media_url, :foo, [], "")
end) end)
end end
test "optionally lets you specify an output_filepath" do test "optionally lets you specify an output_filepath" do
assert {:ok, output} = Runner.run(@media_url, [], "%(id)s", output_filepath: "/tmp/yt-dlp-output.json") assert {:ok, output} = Runner.run(@media_url, :foo, [], "%(id)s", output_filepath: "/tmp/yt-dlp-output.json")
assert String.contains?(output, "--print-to-file %(id)s /tmp/yt-dlp-output.json") assert String.contains?(output, "--print-to-file %(id)s /tmp/yt-dlp-output.json")
end end
@@ -54,7 +54,7 @@ defmodule Pinchflat.YtDlp.CommandRunnerTest do
test "includes cookie options when cookies.txt exists and enabled", %{cookie_file: cookie_file} do test "includes cookie options when cookies.txt exists and enabled", %{cookie_file: cookie_file} do
FilesystemUtils.write_p!(cookie_file, "cookie data") FilesystemUtils.write_p!(cookie_file, "cookie data")
assert {:ok, output} = Runner.run(@media_url, [], "", use_cookies: true) assert {:ok, output} = Runner.run(@media_url, :foo, [], "", use_cookies: true)
assert String.contains?(output, "--cookies #{cookie_file}") assert String.contains?(output, "--cookies #{cookie_file}")
end end
@@ -62,7 +62,7 @@ defmodule Pinchflat.YtDlp.CommandRunnerTest do
test "doesn't include cookie options when cookies.txt exists but disabled", %{cookie_file: cookie_file} do test "doesn't include cookie options when cookies.txt exists but disabled", %{cookie_file: cookie_file} do
FilesystemUtils.write_p!(cookie_file, "cookie data") FilesystemUtils.write_p!(cookie_file, "cookie data")
assert {:ok, output} = Runner.run(@media_url, [], "", use_cookies: false) assert {:ok, output} = Runner.run(@media_url, :foo, [], "", use_cookies: false)
refute String.contains?(output, "--cookies #{cookie_file}") refute String.contains?(output, "--cookies #{cookie_file}")
end end
@@ -70,7 +70,7 @@ defmodule Pinchflat.YtDlp.CommandRunnerTest do
test "doesn't include cookie options when cookies.txt blank", %{cookie_file: cookie_file} do test "doesn't include cookie options when cookies.txt blank", %{cookie_file: cookie_file} do
FilesystemUtils.write_p!(cookie_file, " \n \n ") FilesystemUtils.write_p!(cookie_file, " \n \n ")
assert {:ok, output} = Runner.run(@media_url, [], "", use_cookies: true) assert {:ok, output} = Runner.run(@media_url, :foo, [], "", use_cookies: true)
refute String.contains?(output, "--cookies") refute String.contains?(output, "--cookies")
refute String.contains?(output, cookie_file) refute String.contains?(output, cookie_file)
@@ -79,7 +79,7 @@ defmodule Pinchflat.YtDlp.CommandRunnerTest do
test "doesn't include cookie options when cookies.txt doesn't exist", %{cookie_file: cookie_file} do test "doesn't include cookie options when cookies.txt doesn't exist", %{cookie_file: cookie_file} do
File.rm(cookie_file) File.rm(cookie_file)
assert {:ok, output} = Runner.run(@media_url, [], "") assert {:ok, output} = Runner.run(@media_url, :foo, [], "")
refute String.contains?(output, "--cookies") refute String.contains?(output, "--cookies")
refute String.contains?(output, cookie_file) refute String.contains?(output, cookie_file)
@@ -91,19 +91,19 @@ defmodule Pinchflat.YtDlp.CommandRunnerTest do
describe "run/4 when testing global options" do describe "run/4 when testing global options" do
test "creates windows-safe filenames" do test "creates windows-safe filenames" do
assert {:ok, output} = Runner.run(@media_url, [], "") assert {:ok, output} = Runner.run(@media_url, :foo, [], "")
assert String.contains?(output, "--windows-filenames") assert String.contains?(output, "--windows-filenames")
end end
test "runs quietly" do test "runs quietly" do
assert {:ok, output} = Runner.run(@media_url, [], "") assert {:ok, output} = Runner.run(@media_url, :foo, [], "")
assert String.contains?(output, "--quiet") assert String.contains?(output, "--quiet")
end end
test "sets the cache directory" do test "sets the cache directory" do
assert {:ok, output} = Runner.run(@media_url, [], "") assert {:ok, output} = Runner.run(@media_url, :foo, [], "")
assert String.contains?(output, "--cache-dir /tmp/test/tmpfiles/yt-dlp-cache") assert String.contains?(output, "--cache-dir /tmp/test/tmpfiles/yt-dlp-cache")
end end
+26 -19
View File
@@ -10,7 +10,7 @@ defmodule Pinchflat.YtDlp.MediaCollectionTest do
describe "get_media_attributes_for_collection/2" do describe "get_media_attributes_for_collection/2" do
test "returns a list of video attributes with no blank elements" do test "returns a list of video attributes with no blank elements" do
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot, _addl_opts -> expect(YtDlpRunnerMock, :run, fn _url, :get_media_attributes_for_collection, _opts, _ot, _addl_opts ->
{:ok, source_attributes_return_fixture() <> "\n\n"} {:ok, source_attributes_return_fixture() <> "\n\n"}
end) end)
@@ -19,7 +19,7 @@ defmodule Pinchflat.YtDlp.MediaCollectionTest do
end end
test "passes the expected default args" do test "passes the expected default args" do
expect(YtDlpRunnerMock, :run, fn _url, opts, ot, _addl_opts -> expect(YtDlpRunnerMock, :run, fn _url, :get_media_attributes_for_collection, opts, ot, _addl_opts ->
assert opts == [:simulate, :skip_download, :ignore_no_formats_error, :no_warnings] assert opts == [:simulate, :skip_download, :ignore_no_formats_error, :no_warnings]
assert ot == Media.indexing_output_template() assert ot == Media.indexing_output_template()
@@ -30,13 +30,15 @@ defmodule Pinchflat.YtDlp.MediaCollectionTest do
end end
test "returns the error straight through when the command fails" do test "returns the error straight through when the command fails" do
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot, _addl_opts -> {:error, "Big issue", 1} end) expect(YtDlpRunnerMock, :run, fn _url, :get_media_attributes_for_collection, _opts, _ot, _addl_opts ->
{:error, "Big issue", 1}
end)
assert {:error, "Big issue", 1} = MediaCollection.get_media_attributes_for_collection(@channel_url) assert {:error, "Big issue", 1} = MediaCollection.get_media_attributes_for_collection(@channel_url)
end end
test "passes long additional command options" do test "passes long additional command options" do
expect(YtDlpRunnerMock, :run, fn _url, opts, _ot, _addl_opts -> expect(YtDlpRunnerMock, :run, fn _url, :get_media_attributes_for_collection, opts, _ot, _addl_opts ->
assert :foo in opts assert :foo in opts
{:ok, ""} {:ok, ""}
@@ -46,7 +48,7 @@ defmodule Pinchflat.YtDlp.MediaCollectionTest do
end end
test "passes additional args to runner" do test "passes additional args to runner" do
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot, addl_opts -> expect(YtDlpRunnerMock, :run, fn _url, :get_media_attributes_for_collection, _opts, _ot, addl_opts ->
assert [{:output_filepath, filepath} | _] = addl_opts assert [{:output_filepath, filepath} | _] = addl_opts
assert {:use_cookies, false} in addl_opts assert {:use_cookies, false} in addl_opts
assert String.ends_with?(filepath, ".json") assert String.ends_with?(filepath, ".json")
@@ -58,7 +60,10 @@ defmodule Pinchflat.YtDlp.MediaCollectionTest do
end end
test "supports an optional file_listener_handler that gets passed a filename" do test "supports an optional file_listener_handler that gets passed a filename" do
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot, _addl_opts -> {:ok, ""} end) expect(YtDlpRunnerMock, :run, fn _url, :get_media_attributes_for_collection, _opts, _ot, _addl_opts ->
{:ok, ""}
end)
current_self = self() current_self = self()
handler = fn filename -> handler = fn filename ->
@@ -73,7 +78,7 @@ defmodule Pinchflat.YtDlp.MediaCollectionTest do
end end
test "gracefully handles partially failed responses" do test "gracefully handles partially failed responses" do
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot, _addl_opts -> expect(YtDlpRunnerMock, :run, fn _url, :get_media_attributes_for_collection, _opts, _ot, _addl_opts ->
{:ok, "INVALID\n\n" <> source_attributes_return_fixture() <> "\nINVALID\n"} {:ok, "INVALID\n\n" <> source_attributes_return_fixture() <> "\nINVALID\n"}
end) end)
@@ -84,7 +89,7 @@ defmodule Pinchflat.YtDlp.MediaCollectionTest do
describe "get_source_details/1" do describe "get_source_details/1" do
test "returns a map with data on success" do test "returns a map with data on success" do
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot, _addl_opts -> expect(YtDlpRunnerMock, :run, fn _url, :get_source_details, _opts, _ot, _addl_opts ->
Phoenix.json_library().encode(%{ Phoenix.json_library().encode(%{
channel: "PinchflatTestChannel", channel: "PinchflatTestChannel",
channel_id: "UCQH2", channel_id: "UCQH2",
@@ -104,7 +109,7 @@ defmodule Pinchflat.YtDlp.MediaCollectionTest do
end end
test "passes the expected args to the runner" do test "passes the expected args to the runner" do
expect(YtDlpRunnerMock, :run, fn @channel_url, opts, ot, _addl_opts -> expect(YtDlpRunnerMock, :run, fn @channel_url, :get_source_details, opts, ot, _addl_opts ->
assert opts == [:simulate, :skip_download, :ignore_no_formats_error, playlist_end: 1] assert opts == [:simulate, :skip_download, :ignore_no_formats_error, playlist_end: 1]
assert ot == "%(.{channel,channel_id,playlist_id,playlist_title,filename})j" assert ot == "%(.{channel,channel_id,playlist_id,playlist_title,filename})j"
@@ -115,7 +120,7 @@ defmodule Pinchflat.YtDlp.MediaCollectionTest do
end end
test "passes custom args to the runner" do test "passes custom args to the runner" do
expect(YtDlpRunnerMock, :run, fn @channel_url, opts, _ot, _addl_opts -> expect(YtDlpRunnerMock, :run, fn @channel_url, :get_source_details, opts, _ot, _addl_opts ->
assert {:foo, :bar} in opts assert {:foo, :bar} in opts
{:ok, "{}"} {:ok, "{}"}
@@ -125,7 +130,7 @@ defmodule Pinchflat.YtDlp.MediaCollectionTest do
end end
test "passes additional args to the runner" do test "passes additional args to the runner" do
expect(YtDlpRunnerMock, :run, fn @channel_url, _opts, _ot, addl_opts -> expect(YtDlpRunnerMock, :run, fn @channel_url, :get_source_details, _opts, _ot, addl_opts ->
assert {:use_cookies, true} in addl_opts assert {:use_cookies, true} in addl_opts
{:ok, "{}"} {:ok, "{}"}
@@ -135,13 +140,13 @@ defmodule Pinchflat.YtDlp.MediaCollectionTest do
end end
test "returns an error if the runner returns an error" do test "returns an error if the runner returns an error" do
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot, _addl_opts -> {:error, "Big issue", 1} end) expect(YtDlpRunnerMock, :run, fn _url, :get_source_details, _opts, _ot, _addl_opts -> {:error, "Big issue", 1} end)
assert {:error, "Big issue", 1} = MediaCollection.get_source_details(@channel_url) assert {:error, "Big issue", 1} = MediaCollection.get_source_details(@channel_url)
end end
test "returns an error if the output is not JSON" do test "returns an error if the output is not JSON" do
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot, _addl_opts -> {:ok, "Not JSON"} end) expect(YtDlpRunnerMock, :run, fn _url, :get_source_details, _opts, _ot, _addl_opts -> {:ok, "Not JSON"} end)
assert {:error, "Error decoding JSON response"} = MediaCollection.get_source_details(@channel_url) assert {:error, "Error decoding JSON response"} = MediaCollection.get_source_details(@channel_url)
end end
@@ -149,7 +154,7 @@ defmodule Pinchflat.YtDlp.MediaCollectionTest do
describe "get_source_metadata/1" do describe "get_source_metadata/1" do
test "returns a map with data on success" do test "returns a map with data on success" do
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot, _addl_opts -> expect(YtDlpRunnerMock, :run, fn _url, :get_source_metadata, _opts, _ot, _addl_opts ->
Phoenix.json_library().encode(%{channel: "PinchflatTestChannel"}) Phoenix.json_library().encode(%{channel: "PinchflatTestChannel"})
end) end)
@@ -159,7 +164,7 @@ defmodule Pinchflat.YtDlp.MediaCollectionTest do
end end
test "passes the expected args to the backend runner" do test "passes the expected args to the backend runner" do
expect(YtDlpRunnerMock, :run, fn @channel_url, opts, ot, _addl_opts -> expect(YtDlpRunnerMock, :run, fn @channel_url, :get_source_metadata, opts, ot, _addl_opts ->
assert opts == [:skip_download, playlist_items: 0] assert opts == [:skip_download, playlist_items: 0]
assert ot == "playlist:%()j" assert ot == "playlist:%()j"
@@ -170,7 +175,7 @@ defmodule Pinchflat.YtDlp.MediaCollectionTest do
end end
test "passes additional args to the runner" do test "passes additional args to the runner" do
expect(YtDlpRunnerMock, :run, fn @channel_url, _opts, _ot, addl_opts -> expect(YtDlpRunnerMock, :run, fn @channel_url, :get_source_metadata, _opts, _ot, addl_opts ->
assert {:use_cookies, true} in addl_opts assert {:use_cookies, true} in addl_opts
{:ok, "{}"} {:ok, "{}"}
@@ -180,7 +185,7 @@ defmodule Pinchflat.YtDlp.MediaCollectionTest do
end end
test "passes custom args to the runner" do test "passes custom args to the runner" do
expect(YtDlpRunnerMock, :run, fn _url, opts, _ot, _addl_opts -> expect(YtDlpRunnerMock, :run, fn _url, :get_source_metadata, opts, _ot, _addl_opts ->
assert opts == [:skip_download, playlist_items: 1, real_opt: :yup] assert opts == [:skip_download, playlist_items: 1, real_opt: :yup]
{:ok, "{}"} {:ok, "{}"}
@@ -196,13 +201,15 @@ defmodule Pinchflat.YtDlp.MediaCollectionTest do
end end
test "returns an error if the runner returns an error" do test "returns an error if the runner returns an error" do
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot, _addl_opts -> {:error, "Big issue", 1} end) expect(YtDlpRunnerMock, :run, fn _url, :get_source_metadata, _opts, _ot, _addl_opts ->
{:error, "Big issue", 1}
end)
assert {:error, "Big issue", 1} = MediaCollection.get_source_metadata(@channel_url, playlist_items: 0) assert {:error, "Big issue", 1} = MediaCollection.get_source_metadata(@channel_url, playlist_items: 0)
end end
test "returns an error if the output is not JSON" do test "returns an error if the output is not JSON" do
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot, _addl_opts -> {:ok, "Not JSON"} end) expect(YtDlpRunnerMock, :run, fn _url, :get_source_metadata, _opts, _ot, _addl_opts -> {:ok, "Not JSON"} end)
assert {:error, %Jason.DecodeError{}} = MediaCollection.get_source_metadata(@channel_url, playlist_items: 0) assert {:error, %Jason.DecodeError{}} = MediaCollection.get_source_metadata(@channel_url, playlist_items: 0)
end end
+21 -21
View File
@@ -9,7 +9,7 @@ defmodule Pinchflat.YtDlp.MediaTest do
describe "download/3" do describe "download/3" do
test "calls the backend runner with the expected arguments" do test "calls the backend runner with the expected arguments" do
expect(YtDlpRunnerMock, :run, fn @media_url, opts, ot, addl -> expect(YtDlpRunnerMock, :run, fn @media_url, :download, opts, ot, addl ->
assert [:no_simulate] = opts assert [:no_simulate] = opts
assert "after_move:%()j" = ot assert "after_move:%()j" = ot
assert addl == [] assert addl == []
@@ -21,7 +21,7 @@ defmodule Pinchflat.YtDlp.MediaTest do
end end
test "passes along custom command args" do test "passes along custom command args" do
expect(YtDlpRunnerMock, :run, fn _url, opts, _ot, _addl -> expect(YtDlpRunnerMock, :run, fn _url, :download, opts, _ot, _addl ->
assert [:no_simulate, :custom_arg] = opts assert [:no_simulate, :custom_arg] = opts
{:ok, "{}"} {:ok, "{}"}
@@ -31,7 +31,7 @@ defmodule Pinchflat.YtDlp.MediaTest do
end end
test "passes along additional options" do test "passes along additional options" do
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot, addl -> expect(YtDlpRunnerMock, :run, fn _url, :download, _opts, _ot, addl ->
assert [addl_arg: true] = addl assert [addl_arg: true] = addl
{:ok, "{}"} {:ok, "{}"}
@@ -41,7 +41,7 @@ defmodule Pinchflat.YtDlp.MediaTest do
end end
test "parses and returns the generated file as JSON" do test "parses and returns the generated file as JSON" do
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot, _addl -> expect(YtDlpRunnerMock, :run, fn _url, :download, _opts, _ot, _addl ->
{:ok, render_metadata(:media_metadata)} {:ok, render_metadata(:media_metadata)}
end) end)
@@ -50,7 +50,7 @@ defmodule Pinchflat.YtDlp.MediaTest do
end end
test "returns errors" do test "returns errors" do
expect(YtDlpRunnerMock, :run, fn _url, _opt, _ot, _addl -> expect(YtDlpRunnerMock, :run, fn _url, :download, _opt, _ot, _addl ->
{:error, "something"} {:error, "something"}
end) end)
@@ -60,7 +60,7 @@ defmodule Pinchflat.YtDlp.MediaTest do
describe "get_downloadable_status/1" do describe "get_downloadable_status/1" do
test "returns :downloadable if the media was never live" do test "returns :downloadable if the media was never live" do
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot -> expect(YtDlpRunnerMock, :run, fn _url, :get_downloadable_status, _opts, _ot ->
{:ok, Phoenix.json_library().encode!(%{"live_status" => "not_live"})} {:ok, Phoenix.json_library().encode!(%{"live_status" => "not_live"})}
end) end)
@@ -68,7 +68,7 @@ defmodule Pinchflat.YtDlp.MediaTest do
end end
test "returns :downloadable if the media was live and has been processed" do test "returns :downloadable if the media was live and has been processed" do
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot -> expect(YtDlpRunnerMock, :run, fn _url, :get_downloadable_status, _opts, _ot ->
{:ok, Phoenix.json_library().encode!(%{"live_status" => "was_live"})} {:ok, Phoenix.json_library().encode!(%{"live_status" => "was_live"})}
end) end)
@@ -76,7 +76,7 @@ defmodule Pinchflat.YtDlp.MediaTest do
end end
test "returns :downloadable if the media's live_status is nil" do test "returns :downloadable if the media's live_status is nil" do
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot -> expect(YtDlpRunnerMock, :run, fn _url, :get_downloadable_status, _opts, _ot ->
{:ok, Phoenix.json_library().encode!(%{"live_status" => nil})} {:ok, Phoenix.json_library().encode!(%{"live_status" => nil})}
end) end)
@@ -84,7 +84,7 @@ defmodule Pinchflat.YtDlp.MediaTest do
end end
test "returns :ignorable if the media is currently live" do test "returns :ignorable if the media is currently live" do
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot -> expect(YtDlpRunnerMock, :run, fn _url, :get_downloadable_status, _opts, _ot ->
{:ok, Phoenix.json_library().encode!(%{"live_status" => "is_live"})} {:ok, Phoenix.json_library().encode!(%{"live_status" => "is_live"})}
end) end)
@@ -92,7 +92,7 @@ defmodule Pinchflat.YtDlp.MediaTest do
end end
test "returns :ignorable if the media is scheduled to be live" do test "returns :ignorable if the media is scheduled to be live" do
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot -> expect(YtDlpRunnerMock, :run, fn _url, :get_downloadable_status, _opts, _ot ->
{:ok, Phoenix.json_library().encode!(%{"live_status" => "is_upcoming"})} {:ok, Phoenix.json_library().encode!(%{"live_status" => "is_upcoming"})}
end) end)
@@ -100,7 +100,7 @@ defmodule Pinchflat.YtDlp.MediaTest do
end end
test "returns :ignorable if the media was live but hasn't been processed" do test "returns :ignorable if the media was live but hasn't been processed" do
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot -> expect(YtDlpRunnerMock, :run, fn _url, :get_downloadable_status, _opts, _ot ->
{:ok, Phoenix.json_library().encode!(%{"live_status" => "post_live"})} {:ok, Phoenix.json_library().encode!(%{"live_status" => "post_live"})}
end) end)
@@ -108,7 +108,7 @@ defmodule Pinchflat.YtDlp.MediaTest do
end end
test "returns an error if the downloadable status can't be determined" do test "returns an error if the downloadable status can't be determined" do
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot -> expect(YtDlpRunnerMock, :run, fn _url, :get_downloadable_status, _opts, _ot ->
{:ok, Phoenix.json_library().encode!(%{"live_status" => "what_tha"})} {:ok, Phoenix.json_library().encode!(%{"live_status" => "what_tha"})}
end) end)
@@ -118,7 +118,7 @@ defmodule Pinchflat.YtDlp.MediaTest do
describe "download_thumbnail/2" do describe "download_thumbnail/2" do
test "calls the backend runner with the expected arguments" do test "calls the backend runner with the expected arguments" do
expect(YtDlpRunnerMock, :run, fn @media_url, opts, ot, _addl -> expect(YtDlpRunnerMock, :run, fn @media_url, :download_thumbnail, opts, ot, _addl ->
assert opts == [:no_simulate, :skip_download, :write_thumbnail, {:convert_thumbnail, "jpg"}] assert opts == [:no_simulate, :skip_download, :write_thumbnail, {:convert_thumbnail, "jpg"}]
assert ot == "after_move:%()j" assert ot == "after_move:%()j"
@@ -129,7 +129,7 @@ defmodule Pinchflat.YtDlp.MediaTest do
end end
test "passes along custom command args" do test "passes along custom command args" do
expect(YtDlpRunnerMock, :run, fn _url, opts, _ot, _addl -> expect(YtDlpRunnerMock, :run, fn _url, :download_thumbnail, opts, _ot, _addl ->
assert :custom_arg in opts assert :custom_arg in opts
{:ok, "{}"} {:ok, "{}"}
@@ -139,7 +139,7 @@ defmodule Pinchflat.YtDlp.MediaTest do
end end
test "passes along additional options" do test "passes along additional options" do
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot, addl -> expect(YtDlpRunnerMock, :run, fn _url, :download_thumbnail, _opts, _ot, addl ->
assert [addl_arg: true] = addl assert [addl_arg: true] = addl
{:ok, "{}"} {:ok, "{}"}
@@ -149,7 +149,7 @@ defmodule Pinchflat.YtDlp.MediaTest do
end end
test "returns errors" do test "returns errors" do
expect(YtDlpRunnerMock, :run, fn _url, _opt, _ot, _addl -> expect(YtDlpRunnerMock, :run, fn _url, :download_thumbnail, _opt, _ot, _addl ->
{:error, "something"} {:error, "something"}
end) end)
@@ -159,7 +159,7 @@ defmodule Pinchflat.YtDlp.MediaTest do
describe "get_media_attributes/1" do describe "get_media_attributes/1" do
test "returns a list of video attributes" do test "returns a list of video attributes" do
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot, _addl -> expect(YtDlpRunnerMock, :run, fn _url, :get_media_attributes, _opts, _ot, _addl ->
{:ok, media_attributes_return_fixture()} {:ok, media_attributes_return_fixture()}
end) end)
@@ -168,7 +168,7 @@ defmodule Pinchflat.YtDlp.MediaTest do
end end
test "it passes the expected default args" do test "it passes the expected default args" do
expect(YtDlpRunnerMock, :run, fn _url, opts, ot, _addl -> expect(YtDlpRunnerMock, :run, fn _url, :get_media_attributes, opts, ot, _addl ->
assert opts == [:simulate, :skip_download] assert opts == [:simulate, :skip_download]
assert ot == Media.indexing_output_template() assert ot == Media.indexing_output_template()
@@ -179,7 +179,7 @@ defmodule Pinchflat.YtDlp.MediaTest do
end end
test "passes along additional command options" do test "passes along additional command options" do
expect(YtDlpRunnerMock, :run, fn _url, opts, _ot, _addl -> expect(YtDlpRunnerMock, :run, fn _url, :get_media_attributes, opts, _ot, _addl ->
assert [:simulate, :skip_download, :custom_arg] = opts assert [:simulate, :skip_download, :custom_arg] = opts
{:ok, media_attributes_return_fixture()} {:ok, media_attributes_return_fixture()}
end) end)
@@ -188,7 +188,7 @@ defmodule Pinchflat.YtDlp.MediaTest do
end end
test "passes along additional options" do test "passes along additional options" do
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot, addl -> expect(YtDlpRunnerMock, :run, fn _url, :get_media_attributes, _opts, _ot, addl ->
assert [addl_arg: true] = addl assert [addl_arg: true] = addl
{:ok, media_attributes_return_fixture()} {:ok, media_attributes_return_fixture()}
end) end)
@@ -197,7 +197,7 @@ defmodule Pinchflat.YtDlp.MediaTest do
end end
test "returns the error straight through when the command fails" do test "returns the error straight through when the command fails" do
expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot, _addl -> {:error, "Big issue", 1} end) expect(YtDlpRunnerMock, :run, fn _url, :get_media_attributes, _opts, _ot, _addl -> {:error, "Big issue", 1} end)
assert {:error, "Big issue", 1} = Media.get_media_attributes(@media_url) assert {:error, "Big issue", 1} = Media.get_media_attributes(@media_url)
end end
@@ -6,7 +6,6 @@ defmodule PinchflatWeb.Pages.JobTableLiveTest do
import Pinchflat.MediaFixtures import Pinchflat.MediaFixtures
import Pinchflat.SourcesFixtures import Pinchflat.SourcesFixtures
alias Pinchflat.Utils.StringUtils
alias Pinchflat.Pages.JobTableLive alias Pinchflat.Pages.JobTableLive
alias Pinchflat.Downloading.MediaDownloadWorker alias Pinchflat.Downloading.MediaDownloadWorker
alias Pinchflat.FastIndexing.FastIndexingWorker alias Pinchflat.FastIndexing.FastIndexingWorker
@@ -66,7 +66,7 @@ defmodule PinchflatWeb.SourceControllerTest do
describe "create source" do describe "create source" do
test "redirects to show when data is valid", %{conn: conn, create_attrs: create_attrs} do test "redirects to show when data is valid", %{conn: conn, create_attrs: create_attrs} do
expect(YtDlpRunnerMock, :run, 1, &runner_function_mock/4) expect(YtDlpRunnerMock, :run, 1, &runner_function_mock/5)
conn = post(conn, ~p"/sources", source: create_attrs) conn = post(conn, ~p"/sources", source: create_attrs)
assert %{id: id} = redirected_params(conn) assert %{id: id} = redirected_params(conn)
@@ -82,7 +82,7 @@ defmodule PinchflatWeb.SourceControllerTest do
end end
test "redirects to onboarding when onboarding", %{conn: conn, create_attrs: create_attrs} do test "redirects to onboarding when onboarding", %{conn: conn, create_attrs: create_attrs} do
expect(YtDlpRunnerMock, :run, 1, &runner_function_mock/4) expect(YtDlpRunnerMock, :run, 1, &runner_function_mock/5)
Settings.set(onboarding: true) Settings.set(onboarding: true)
conn = post(conn, ~p"/sources", source: create_attrs) conn = post(conn, ~p"/sources", source: create_attrs)
@@ -111,7 +111,7 @@ defmodule PinchflatWeb.SourceControllerTest do
setup [:create_source] setup [:create_source]
test "redirects when data is valid", %{conn: conn, source: source, update_attrs: update_attrs} do test "redirects when data is valid", %{conn: conn, source: source, update_attrs: update_attrs} do
expect(YtDlpRunnerMock, :run, 1, &runner_function_mock/4) expect(YtDlpRunnerMock, :run, 1, &runner_function_mock/5)
conn = put(conn, ~p"/sources/#{source}", source: update_attrs) conn = put(conn, ~p"/sources/#{source}", source: update_attrs)
assert redirected_to(conn) == ~p"/sources/#{source}" assert redirected_to(conn) == ~p"/sources/#{source}"
@@ -276,7 +276,7 @@ defmodule PinchflatWeb.SourceControllerTest do
%{source: source, media_item: media_item} %{source: source, media_item: media_item}
end end
defp runner_function_mock(_url, _opts, _ot, _addl) do defp runner_function_mock(_url, :get_source_details, _opts, _ot, _addl) do
{ {
:ok, :ok,
Phoenix.json_library().encode!(%{ Phoenix.json_library().encode!(%{