From a8b47f2d49b53a492a3b0ba80491923f32bc1192 Mon Sep 17 00:00:00 2001 From: Kieran Eglin Date: Tue, 26 Nov 2024 11:29:22 -0800 Subject: [PATCH] Added tests for get_downloadable_status method --- lib/pinchflat/yt_dlp/media.ex | 9 ++++- test/pinchflat/yt_dlp/media_test.exs | 58 ++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/lib/pinchflat/yt_dlp/media.ex b/lib/pinchflat/yt_dlp/media.ex index 253a378..5bd5654 100644 --- a/lib/pinchflat/yt_dlp/media.ex +++ b/lib/pinchflat/yt_dlp/media.ex @@ -49,7 +49,12 @@ defmodule Pinchflat.YtDlp.Media do end end - # TODO: test + @doc """ + Determines if the media at the given URL is ready to be downloaded. + Common examples of non-downloadable media are upcoming or in-progress live streams. + + Returns {:ok, :downloadable | :ignorable} | {:error, any} + """ def get_downloadable_status(url) do case backend_runner().run(url, [:simulate, :skip_download], "%(.{live_status})j") do {:ok, output} -> @@ -163,6 +168,8 @@ defmodule Pinchflat.YtDlp.Media do case response["live_status"] do status when status in ["is_live", "is_upcoming", "post_live"] -> {:ok, :ignorable} status when status in ["was_live", "not_live"] -> {:ok, :downloadable} + # This preserves my tenuous support for non-youtube sources. + nil -> {:ok, :downloadable} _ -> {:error, "Unknown live status: #{response["live_status"]}"} end end diff --git a/test/pinchflat/yt_dlp/media_test.exs b/test/pinchflat/yt_dlp/media_test.exs index 8a3d9ae..ef76e1e 100644 --- a/test/pinchflat/yt_dlp/media_test.exs +++ b/test/pinchflat/yt_dlp/media_test.exs @@ -58,6 +58,64 @@ defmodule Pinchflat.YtDlp.MediaTest do end end + describe "get_downloadable_status/1" do + test "returns :downloadable if the media was never live" do + expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot -> + {:ok, Phoenix.json_library().encode!(%{"live_status" => "not_live"})} + end) + + assert {:ok, :downloadable} = Media.get_downloadable_status(@media_url) + end + + test "returns :downloadable if the media was live and has been processed" do + expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot -> + {:ok, Phoenix.json_library().encode!(%{"live_status" => "was_live"})} + end) + + assert {:ok, :downloadable} = Media.get_downloadable_status(@media_url) + end + + test "returns :downloadable if the media's live_status is nil" do + expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot -> + {:ok, Phoenix.json_library().encode!(%{"live_status" => nil})} + end) + + assert {:ok, :downloadable} = Media.get_downloadable_status(@media_url) + end + + test "returns :ignorable if the media is currently live" do + expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot -> + {:ok, Phoenix.json_library().encode!(%{"live_status" => "is_live"})} + end) + + assert {:ok, :ignorable} = Media.get_downloadable_status(@media_url) + end + + test "returns :ignorable if the media is scheduled to be live" do + expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot -> + {:ok, Phoenix.json_library().encode!(%{"live_status" => "is_upcoming"})} + end) + + assert {:ok, :ignorable} = Media.get_downloadable_status(@media_url) + end + + test "returns :ignorable if the media was live but hasn't been processed" do + expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot -> + {:ok, Phoenix.json_library().encode!(%{"live_status" => "post_live"})} + end) + + assert {:ok, :ignorable} = Media.get_downloadable_status(@media_url) + end + + test "returns an error if the downloadable status can't be determined" do + expect(YtDlpRunnerMock, :run, fn _url, _opts, _ot -> + {:ok, Phoenix.json_library().encode!(%{"live_status" => "what_tha"})} + end) + + assert {:error, "Unknown live status: what_tha"} = Media.get_downloadable_status(@media_url) + end + end + describe "download_thumbnail/2" do test "calls the backend runner with the expected arguments" do expect(YtDlpRunnerMock, :run, fn @media_url, opts, ot, _addl ->