fp/apps/bright/test/bright/cache_test.exs
2025-03-10 17:51:35 -08:00

64 lines
2.2 KiB
Elixir

defmodule Bright.CacheTest do
use Bright.DataCase
alias Bright.Cache
require Logger
@sample_url "https://example.com/my_video.mp4"
## IDK what I'm doing here. Ideally I want a redis-like k/v store where I can temporarily put VODs and they expire after 12 hours or so.
## this would potentially speed up vod processing because it would prevent having to download the VOD from S3 during every Oban worker execution.
## BUT I don't want to implement it myself because of the idiom, "There are only two unsolved problems in CS. Naming things and cache invalidation"
## Meaning I don't think I can do any better than the experts in the field.
## Anyway, this is FEATURE CREEP! Solve the problem without caching and LET IT BE SLOW.
## To implement this cache before the system works is pre-mature optimization!
@cache_dir Application.fetch_env!(:bright, :cache_dir)
describe "cache" do
@tag :unit
test "get_cache_dir/0" do
assert Regex.match?(~r/\/futureporn/, Cache.get_cache_dir())
end
@tag :unit
test "generate_basename/1" do
# Test with a URL
url = @sample_url
filename = Cache.generate_basename(url)
assert Regex.match?(~r/^[a-zA-Z0-9]+\/my_video\.mp4$/, filename)
# Test with a file path
path = "/home/cj/Downloads/taco.mp4"
filename = Cache.generate_basename(path)
assert Regex.match?(~r/^[a-zA-Z0-9]+\/taco\.mp4$/, filename)
end
@tag :unit
test "generate_basename/2" do
filename = Cache.generate_basename(@sample_url, "png")
assert Regex.match?(~r/^[a-zA-Z0-9]+\/my_video\.png/, filename)
end
@tag :unit
test "generate_filename/1" do
filename = Cache.generate_filename(@sample_url)
assert Regex.match?(~r/\/futureporn\/.+\/my_video\.mp4/, filename)
filename = Cache.generate_filename("/home/cj/Downloads/test.mp4")
assert Regex.match?(~r/\/futureporn\/.+\/test\.mp4/, filename)
assert File.exists?(Path.dirname(filename))
assert not File.exists?(filename)
end
@tag :unit
test "generate_filename/2" do
filename = Cache.generate_filename(@sample_url, "png")
assert Regex.match?(~r/\/futureporn\/.+\/my_video\.png/, filename)
end
end
end