mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-05-04 05:12:38 +02:00
feat(filesystem): enhance local mount path normalization and add error handling for missing parent directories
This commit is contained in:
parent
8c06709295
commit
c238a671c8
5 changed files with 160 additions and 5 deletions
|
|
@ -79,7 +79,7 @@ async def test_file_write_null_filename_uses_semantic_default_path():
|
|||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_file_write_null_filename_infers_json_extension():
|
||||
async def test_file_write_null_filename_defaults_to_markdown_path():
|
||||
llm = _FakeLLM(
|
||||
'{"intent":"file_write","confidence":0.71,"suggested_filename":null}'
|
||||
)
|
||||
|
|
@ -94,7 +94,7 @@ async def test_file_write_null_filename_infers_json_extension():
|
|||
assert result is not None
|
||||
contract = result["file_operation_contract"]
|
||||
assert contract["intent"] == FileOperationIntent.FILE_WRITE.value
|
||||
assert contract["suggested_path"] == "/notes.json"
|
||||
assert contract["suggested_path"] == "/notes.md"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
|
|
|
|||
|
|
@ -34,6 +34,11 @@ class _RuntimeNoSuggestedPath:
|
|||
state = {"file_operation_contract": {}}
|
||||
|
||||
|
||||
class _RuntimeWithSuggestedPath:
|
||||
def __init__(self, suggested_path: str) -> None:
|
||||
self.state = {"file_operation_contract": {"suggested_path": suggested_path}}
|
||||
|
||||
|
||||
def test_verify_written_content_prefers_raw_sync() -> None:
|
||||
middleware = SurfSenseFilesystemMiddleware.__new__(SurfSenseFilesystemMiddleware)
|
||||
expected = "line1\nline2"
|
||||
|
|
@ -162,3 +167,47 @@ def test_normalize_local_mount_path_prefixes_posix_absolute_path_for_linux_and_m
|
|||
resolved = middleware._normalize_local_mount_path("/var/log/app.log", runtime) # type: ignore[arg-type]
|
||||
|
||||
assert resolved == "/pc_backups/var/log/app.log"
|
||||
|
||||
|
||||
def test_normalize_local_mount_path_prefers_unique_existing_parent_mount(
|
||||
tmp_path: Path,
|
||||
) -> None:
|
||||
root_a = tmp_path / "RootA"
|
||||
root_b = tmp_path / "RootB"
|
||||
(root_a / "other").mkdir(parents=True)
|
||||
(root_b / "nested" / "deep").mkdir(parents=True)
|
||||
backend = MultiRootLocalFolderBackend(
|
||||
(("root_a", str(root_a)), ("root_b", str(root_b)))
|
||||
)
|
||||
runtime = _RuntimeNoSuggestedPath()
|
||||
middleware = SurfSenseFilesystemMiddleware.__new__(SurfSenseFilesystemMiddleware)
|
||||
middleware._get_backend = lambda _runtime: backend # type: ignore[method-assign]
|
||||
|
||||
resolved = middleware._normalize_local_mount_path( # type: ignore[arg-type]
|
||||
"/nested/deep/new-note.md",
|
||||
runtime,
|
||||
)
|
||||
|
||||
assert resolved == "/root_b/nested/deep/new-note.md"
|
||||
|
||||
|
||||
def test_normalize_local_mount_path_uses_suggested_mount_when_ambiguous(
|
||||
tmp_path: Path,
|
||||
) -> None:
|
||||
root_a = tmp_path / "RootA"
|
||||
root_b = tmp_path / "RootB"
|
||||
root_a.mkdir(parents=True)
|
||||
root_b.mkdir(parents=True)
|
||||
backend = MultiRootLocalFolderBackend(
|
||||
(("root_a", str(root_a)), ("root_b", str(root_b)))
|
||||
)
|
||||
runtime = _RuntimeWithSuggestedPath("/root_b/notes/context.md")
|
||||
middleware = SurfSenseFilesystemMiddleware.__new__(SurfSenseFilesystemMiddleware)
|
||||
middleware._get_backend = lambda _runtime: backend # type: ignore[method-assign]
|
||||
|
||||
resolved = middleware._normalize_local_mount_path( # type: ignore[arg-type]
|
||||
"/brand-new-note.md",
|
||||
runtime,
|
||||
)
|
||||
|
||||
assert resolved == "/root_b/brand-new-note.md"
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ pytestmark = pytest.mark.unit
|
|||
|
||||
def test_local_backend_write_read_edit_roundtrip(tmp_path: Path):
|
||||
backend = LocalFolderBackend(str(tmp_path))
|
||||
(tmp_path / "notes").mkdir()
|
||||
|
||||
write = backend.write("/notes/test.md", "line1\nline2")
|
||||
assert write.error is None
|
||||
|
|
@ -51,9 +52,20 @@ def test_local_backend_glob_and_grep(tmp_path: Path):
|
|||
|
||||
def test_local_backend_read_raw_returns_exact_content(tmp_path: Path):
|
||||
backend = LocalFolderBackend(str(tmp_path))
|
||||
(tmp_path / "notes").mkdir()
|
||||
expected = "# Title\n\nline 1\nline 2\n"
|
||||
write = backend.write("/notes/raw.md", expected)
|
||||
assert write.error is None
|
||||
|
||||
raw = backend.read_raw("/notes/raw.md")
|
||||
assert raw == expected
|
||||
|
||||
|
||||
def test_local_backend_write_rejects_missing_parent_directory(tmp_path: Path):
|
||||
backend = LocalFolderBackend(str(tmp_path))
|
||||
|
||||
write = backend.write("/tempoo/new-note.md", "# New note")
|
||||
|
||||
assert write.error is not None
|
||||
assert "parent directory" in write.error
|
||||
assert not (tmp_path / "tempoo").exists()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue