@@ -1,0 +1,107 @@
# Copyright 2026 Cole Christensen
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
defmodule ExGitObjectstore.ConfigurableSizeLimitTest do
use ExUnit.Case, async: true
alias ExGitObjectstore.{Object, Repo}
alias ExGitObjectstore.Object.Blob
alias ExGitObjectstore.Storage.Memory
defp repo_with_limit(max_object_size) do
{:ok, pid} = Memory.start_link()
Repo.new("test-repo", storage: {Memory, Memory.config(pid)}, max_object_size: max_object_size)
end
defp default_repo do
{:ok, pid} = Memory.start_link()
Repo.new("test-repo", storage: {Memory, Memory.config(pid)})
end
# -- Default behavior unchanged --
test "default max_object_size is 128MB" do
repo = default_repo()
assert repo.max_object_size == 128 * 1024 * 1024
end
test "small objects work with default limits" do
repo = default_repo()
ExGitObjectstore.init(repo)
blob = Blob.from_content("hello\n")
assert {:ok, _sha} = Object.write(repo, blob)
end
# -- Custom limits --
test "custom max_object_size is stored in repo" do
repo = repo_with_limit(1024)
assert repo.max_object_size == 1024
end
test "object read rejects objects exceeding custom limit" do
# Write with a generous limit, then read with a tight one
write_repo = repo_with_limit(1_000_000)
ExGitObjectstore.init(write_repo)
# Create a blob larger than 100 bytes
content = String.duplicate("x", 200)
blob = Blob.from_content(content)
{:ok, sha} = Object.write(write_repo, blob)
# Create a repo with the same storage but tight limit
read_repo = %{write_repo | max_object_size: 100}
assert {:error, {:object_too_large, _}} = Object.read(read_repo, sha)
end
test "object decode respects custom limit" do
content = String.duplicate("a", 500)
blob = Blob.from_content(content)
{:ok, compressed} = Object.encode(blob)
# Decode with tight limit
assert {:error, {:object_too_large, _}} = Object.decode(compressed, max_size: 100)
end
test "object decode works with default limit" do
content = String.duplicate("a", 500)
blob = Blob.from_content(content)
{:ok, compressed} = Object.encode(blob)
# Decode without limit uses default
assert {:ok, %Blob{}} = Object.decode(compressed)
end
# -- Error messages include configured max --
test "error message includes the configured limit" do
write_repo = repo_with_limit(1_000_000)
ExGitObjectstore.init(write_repo)
content = String.duplicate("y", 300)
blob = Blob.from_content(content)
{:ok, sha} = Object.write(write_repo, blob)
read_repo = %{write_repo | max_object_size: 50}
case Object.read(read_repo, sha) do
{:error, {:object_too_large, detail}} ->
assert detail =~ "50"
other ->
flunk("Expected {:error, {:object_too_large, _}}, got: #{inspect(other)}")
end
end
end