@@ -277,3 +277,136 @@ async def test_list_directory_endpoint_mocked(client, project_url):
277277 assert file_item ["file_path" ] == "file1.md"
278278 assert file_item ["title" ] == "File 1"
279279 assert file_item ["permalink" ] == "file-1"
280+
281+
282+ @pytest .mark .asyncio
283+ async def test_get_directory_structure_endpoint (test_graph , client , project_url ):
284+ """Test the get_directory_structure endpoint returns folders only."""
285+ # Call the endpoint
286+ response = await client .get (f"{ project_url } /directory/structure" )
287+
288+ # Verify response
289+ assert response .status_code == 200
290+ data = response .json ()
291+
292+ # Check that the response is a valid directory tree
293+ assert "name" in data
294+ assert "directory_path" in data
295+ assert "children" in data
296+ assert "type" in data
297+ assert data ["type" ] == "directory"
298+
299+ # Root should be present
300+ assert data ["name" ] == "Root"
301+ assert data ["directory_path" ] == "/"
302+
303+ # Should have the test directory
304+ assert len (data ["children" ]) == 1
305+ test_dir = data ["children" ][0 ]
306+ assert test_dir ["name" ] == "test"
307+ assert test_dir ["type" ] == "directory"
308+ assert test_dir ["directory_path" ] == "/test"
309+
310+ # Should NOT have any files (test_graph has files but no subdirectories)
311+ assert len (test_dir ["children" ]) == 0
312+
313+ # Verify no file metadata is present in directory nodes
314+ assert test_dir .get ("entity_id" ) is None
315+ assert test_dir .get ("content_type" ) is None
316+ assert test_dir .get ("title" ) is None
317+ assert test_dir .get ("permalink" ) is None
318+
319+
320+ @pytest .mark .asyncio
321+ async def test_get_directory_structure_empty (client , project_url ):
322+ """Test the get_directory_structure endpoint with empty database."""
323+ # Call the endpoint
324+ response = await client .get (f"{ project_url } /directory/structure" )
325+
326+ # Verify response
327+ assert response .status_code == 200
328+ data = response .json ()
329+
330+ # Should return root with no children
331+ assert data ["name" ] == "Root"
332+ assert data ["directory_path" ] == "/"
333+ assert data ["type" ] == "directory"
334+ assert len (data ["children" ]) == 0
335+
336+
337+ @pytest .mark .asyncio
338+ async def test_get_directory_structure_mocked (client , project_url ):
339+ """Test the get_directory_structure endpoint with mocked service."""
340+ # Create a mock directory structure (folders only, no files)
341+ mock_structure = DirectoryNode (
342+ name = "Root" ,
343+ directory_path = "/" ,
344+ type = "directory" ,
345+ children = [
346+ DirectoryNode (
347+ name = "docs" ,
348+ directory_path = "/docs" ,
349+ type = "directory" ,
350+ children = [
351+ DirectoryNode (
352+ name = "guides" ,
353+ directory_path = "/docs/guides" ,
354+ type = "directory" ,
355+ children = [],
356+ ),
357+ DirectoryNode (
358+ name = "api" ,
359+ directory_path = "/docs/api" ,
360+ type = "directory" ,
361+ children = [],
362+ ),
363+ ],
364+ ),
365+ DirectoryNode (name = "specs" , directory_path = "/specs" , type = "directory" , children = []),
366+ ],
367+ )
368+
369+ # Patch the directory service
370+ with patch (
371+ "basic_memory.services.directory_service.DirectoryService.get_directory_structure" ,
372+ return_value = mock_structure ,
373+ ):
374+ # Call the endpoint
375+ response = await client .get (f"{ project_url } /directory/structure" )
376+
377+ # Verify response
378+ assert response .status_code == 200
379+ data = response .json ()
380+
381+ # Check structure matches our mock (folders only)
382+ assert data ["name" ] == "Root"
383+ assert data ["directory_path" ] == "/"
384+ assert data ["type" ] == "directory"
385+ assert len (data ["children" ]) == 2
386+
387+ # Check docs directory
388+ docs = data ["children" ][0 ]
389+ assert docs ["name" ] == "docs"
390+ assert docs ["directory_path" ] == "/docs"
391+ assert docs ["type" ] == "directory"
392+ assert len (docs ["children" ]) == 2
393+
394+ # Check subdirectories
395+ guides = docs ["children" ][0 ]
396+ assert guides ["name" ] == "guides"
397+ assert guides ["directory_path" ] == "/docs/guides"
398+ assert guides ["type" ] == "directory"
399+ assert guides ["children" ] == []
400+
401+ api = docs ["children" ][1 ]
402+ assert api ["name" ] == "api"
403+ assert api ["directory_path" ] == "/docs/api"
404+ assert api ["type" ] == "directory"
405+ assert api ["children" ] == []
406+
407+ # Check specs directory
408+ specs = data ["children" ][1 ]
409+ assert specs ["name" ] == "specs"
410+ assert specs ["directory_path" ] == "/specs"
411+ assert specs ["type" ] == "directory"
412+ assert specs ["children" ] == []
0 commit comments