package services

import (
	"context"
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestBrowseDirectories(t *testing.T) {
	service := &LibraryService{}

	t.Run("blocks path traversal with ..", func(t *testing.T) {
		_, _, _, err := service.BrowseDirectories(context.Background(), "/etc/../root")
		assert.Error(t, err)
		assert.Contains(t, err.Error(), "path traversal not allowed")
	})

	t.Run("returns error for non-existent path", func(t *testing.T) {
		_, _, _, err := service.BrowseDirectories(context.Background(), "/nonexistent/path")
		assert.Error(t, err)
		assert.Contains(t, err.Error(), "path does not exist")
	})

	t.Run("returns error when path is a file", func(t *testing.T) {
		_, _, _, err := service.BrowseDirectories(context.Background(), "/etc/passwd")
		assert.Error(t, err)
		assert.Contains(t, err.Error(), "not a directory")
	})

	t.Run("returns subdirectories for valid path", func(t *testing.T) {
		dirs, currentPath, parentPath, err := service.BrowseDirectories(context.Background(), "/tmp")
		assert.NoError(t, err)
		assert.NotEmpty(t, dirs)
		assert.Equal(t, "/tmp", currentPath)
		assert.Equal(t, "/", parentPath)
	})
}
