Initial curated RAGcore source from c16667f0c9397e2bfc8dd0255b114fba23c8d05e

This commit is contained in:
Public Source Publisher committed 2026-10-01 01:34:21 +02:00
commit 7ca726caf2
659 files changed
+98171

No files matched your search

@@ -0,0 +1,93 @@
from __future__ import annotations
import hashlib
import os
from pathlib import Path
import pytest
from ragcore.application.storage import (
BlobIntegrityError,
HashMismatchError,
InvalidBlobDigestError,
UnsafeBlobPathError,
)
from ragcore.infrastructure.blob import FilesystemBlobStorage
async def _chunks(*values: bytes):
for value in values:
yield value
@pytest.mark.integration
async def test_identical_bytes_are_deduplicated(tmp_path: Path) -> None:
storage = FilesystemBlobStorage(tmp_path)
payload = b"same bytes, one authoritative blob"
expected = hashlib.sha256(payload).hexdigest()
first = await storage.put(_chunks(payload[:9], payload[9:]), original_name="one.txt")
second = await storage.put(_chunks(payload), original_name="retry.txt")
assert first.digest == second.digest == expected
assert first.size == second.size == len(payload)
assert first.storage_key == second.storage_key
assert storage.path_for(expected).is_file()
assert list((tmp_path / "blobs").rglob("*.blob")) == [storage.path_for(expected)]
@pytest.mark.integration
async def test_partial_stream_never_becomes_authoritative(tmp_path: Path) -> None:
storage = FilesystemBlobStorage(tmp_path)
async def interrupted():
yield b"partial"
raise OSError("source interrupted")
with pytest.raises(OSError, match="source interrupted"):
await storage.put(interrupted())
assert list((tmp_path / "blobs").rglob("*.blob")) == []
assert list((tmp_path / "tmp").glob("*.part")) == []
@pytest.mark.integration
async def test_hash_mismatch_fails_closed_without_blob(tmp_path: Path) -> None:
storage = FilesystemBlobStorage(tmp_path)
with pytest.raises(HashMismatchError):
await storage.put(_chunks(b"content"), expected_sha256="0" * 64)
assert list((tmp_path / "blobs").rglob("*.blob")) == []
@pytest.mark.integration
async def test_invalid_digest_and_symlink_destination_fail_closed(tmp_path: Path) -> None:
storage = FilesystemBlobStorage(tmp_path)
with pytest.raises(InvalidBlobDigestError):
storage.path_for("../outside")
payload = b"symlink target must never be replaced"
digest = hashlib.sha256(payload).hexdigest()
destination = storage.path_for(digest)
destination.parent.mkdir(parents=True)
outside = tmp_path / "outside.blob"
outside.write_bytes(b"unrelated")
os.symlink(outside, destination)
with pytest.raises(UnsafeBlobPathError):
await storage.put(_chunks(payload))
assert outside.read_bytes() == b"unrelated"
@pytest.mark.integration
async def test_existing_corrupt_content_address_is_rejected(tmp_path: Path) -> None:
storage = FilesystemBlobStorage(tmp_path)
payload = b"authoritative content"
digest = hashlib.sha256(payload).hexdigest()
destination = storage.path_for(digest)
destination.parent.mkdir(parents=True)
destination.write_bytes(b"corrupt")
with pytest.raises(BlobIntegrityError):
await storage.put(_chunks(payload))