Mogrify/Mogrifun: Asyncify

This commit is contained in:
Lain Soykaf 2025-02-25 17:08:21 +04:00
commit c31fabdebd
9 changed files with 90 additions and 40 deletions

View file

@ -3,11 +3,12 @@
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Upload.Filter.MogrifunTest do
use Pleroma.DataCase
import Mock
use Pleroma.DataCase, async: true
import Mox
alias Pleroma.Upload
alias Pleroma.Upload.Filter
alias Pleroma.MogrifyMock
test "apply mogrify filter" do
File.cp!(
@ -22,23 +23,12 @@ defmodule Pleroma.Upload.Filter.MogrifunTest do
tempfile: Path.absname("test/fixtures/image_tmp.jpg")
}
task =
Task.async(fn ->
assert_receive {:apply_filter, {}}, 4_000
end)
MogrifyMock
|> stub(:open, fn _file -> %{} end)
|> stub(:custom, fn _image, _action -> %{} end)
|> stub(:custom, fn _image, _action, _options -> %{} end)
|> stub(:save, fn _image, [in_place: true] -> :ok end)
with_mocks([
{Mogrify, [],
[
open: fn _f -> %Mogrify.Image{} end,
custom: fn _m, _a -> send(task.pid, {:apply_filter, {}}) end,
custom: fn _m, _a, _o -> send(task.pid, {:apply_filter, {}}) end,
save: fn _f, _o -> :ok end
]}
]) do
assert Filter.Mogrifun.filter(upload) == {:ok, :filtered}
end
Task.await(task)
assert Filter.Mogrifun.filter(upload) == {:ok, :filtered}
end
end

View file

@ -3,13 +3,18 @@
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Upload.Filter.MogrifyTest do
use Pleroma.DataCase
import Mock
use Pleroma.DataCase, async: true
import Mox
alias Pleroma.Upload.Filter
alias Pleroma.StaticStubbedConfigMock, as: ConfigMock
alias Pleroma.MogrifyMock
setup :verify_on_exit!
test "apply mogrify filter" do
clear_config(Filter.Mogrify, args: [{"tint", "40"}])
ConfigMock
|> stub(:get!, fn [Filter.Mogrify, :args] -> [{"tint", "40"}] end)
File.cp!(
"test/fixtures/image.jpg",
@ -23,19 +28,11 @@ defmodule Pleroma.Upload.Filter.MogrifyTest do
tempfile: Path.absname("test/fixtures/image_tmp.jpg")
}
task =
Task.async(fn ->
assert_receive {:apply_filter, {_, "tint", "40"}}, 4_000
end)
MogrifyMock
|> expect(:open, fn _file -> %{} end)
|> expect(:custom, fn _image, "tint", "40" -> %{} end)
|> expect(:save, fn _image, [in_place: true] -> :ok end)
with_mock Mogrify,
open: fn _f -> %Mogrify.Image{} end,
custom: fn _m, _a -> :ok end,
custom: fn m, a, o -> send(task.pid, {:apply_filter, {m, a, o}}) end,
save: fn _f, _o -> :ok end do
assert Filter.Mogrify.filter(upload) == {:ok, :filtered}
end
Task.await(task)
assert Filter.Mogrify.filter(upload) == {:ok, :filtered}
end
end

View file

@ -35,3 +35,4 @@ Mox.defmock(Pleroma.LoggerMock, for: Pleroma.Logging)
Mox.defmock(Pleroma.Uploaders.S3.ExAwsMock, for: Pleroma.Uploaders.S3.ExAwsAPI)
Mox.defmock(Pleroma.DateTimeMock, for: Pleroma.DateTime)
Mox.defmock(Pleroma.MogrifyMock, for: Pleroma.MogrifyBehaviour)

View file

@ -34,7 +34,13 @@ defmodule Pleroma.Test.StaticConfig do
@behaviour Pleroma.Config.Getting
@config Application.get_all_env(:pleroma)
@impl true
def get(path, default \\ nil) do
get_in(@config, path) || default
end
@impl true
def get!(path) do
get_in(@config, path)
end
end